Project

General

Profile

Download (5.47 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / res / raw / compute_quats.c @ d309baa5

1
#include <stdio.h>
2
#include <math.h>
3
#include <stdlib.h>
4

    
5
#define SQUARE
6

    
7
#define SQ2 1.41421356237f
8
#define SQ3 1.73205080757f
9
#define SQ5 2.23606797750f
10
#define PI  3.14159265358f
11
#define NUM_QUATS  200
12

    
13
#ifdef PYRA 
14
int basic[] = { 3,3,3,3 };
15

    
16
float axis[][3] = { { SQ2*SQ3/3,   SQ3/3,          0 } ,
17
                    {-SQ2*SQ3/3,   SQ3/3,          0 } ,
18
                    {         0,  -SQ3/3, -SQ2*SQ3/3 } ,
19
                    {         0,  -SQ3/3,  SQ2*SQ3/3 } };
20
#endif
21

    
22
#ifdef CUBE
23
int basic[] = { 4,4,4 };
24

    
25
float axis[][3] = { { 1,0,0 } , 
26
                    { 0,1,0 } , 
27
                    { 0,0,1 } };
28
#endif
29

    
30
#ifdef DINO
31
int basic[] = { 3,3,3,3 };
32

    
33
float axis[][3] = { {+SQ3/3,+SQ3/3,+SQ3/3} , 
34
                    {+SQ3/3,+SQ3/3,-SQ3/3} , 
35
                    {+SQ3/3,-SQ3/3,+SQ3/3} , 
36
                    {+SQ3/3,-SQ3/3,-SQ3/3} };
37
#endif
38

    
39
#ifdef DIAM
40
int basic[] = { 3,3,3,3 };
41

    
42
float axis[][3] = { {+SQ3*SQ2/3,+SQ3/3,         0} ,
43
                    {-SQ3*SQ2/3,+SQ3/3,         0} ,
44
                    {         0,+SQ3/3,+SQ3*SQ2/3} ,
45
                    {         0,+SQ3/3,-SQ3*SQ2/3} };
46
#endif
47

    
48
#ifdef HELI
49
int basic[] = { 2,2,2,2,2,2 };
50

    
51
float axis[][3] = { {     0, +SQ2/2, -SQ2/2} ,
52
                    {     0, -SQ2/2, -SQ2/2} ,
53
                    {+SQ2/2,      0, -SQ2/2} ,
54
                    {-SQ2/2,      0, -SQ2/2} ,
55
                    {+SQ2/2, -SQ2/2,      0} ,
56
                    {-SQ2/2, -SQ2/2,      0} };
57

    
58
#endif
59

    
60
#ifdef ULTI
61
int basic[] = { 3,3,3,3 };
62

    
63
float axis[][3] = { { 5*SQ5/16 + 11.0f/16,              0.0f   ,    SQ5/8  + 1.0f/4 } ,
64
                    {   SQ5/8  +  1.0f/4 ,  5*SQ5/16 + 11.0f/16,             0.0f   } ,
65
                    {-3*SQ5/16 -  7.0f/16,  3*SQ5/16 +  7.0f/16,  3*SQ5/16 + 7.0f/16} ,
66
                    {             0.0f   ,    SQ5/4  +  1.0f/2 , -5*SQ5/8  -11.0f/8 } };
67

    
68
#endif
69

    
70
#ifdef SQUARE
71
#define COS15 (0.25f*SQ2*(SQ3+1))
72
#define SIN15 (0.25f*SQ2*(SQ3-1))
73

    
74
int basic[] = { 12, 2 };
75

    
76
float axis[][3] = { {     0, 1,     0 } ,
77
                    { COS15, 0, SIN15 } };
78
#endif
79

    
80
int inserted=0;
81

    
82
///////////////////////////////////////////////////////////////////
83
// q1*q2
84

    
85
void multiply_quats( float* q1, float* q2, float* output)
86
  {
87
  output[0] = q2[3]*q1[0] - q2[2]*q1[1] + q2[1]*q1[2] + q2[0]*q1[3];
88
  output[1] = q2[3]*q1[1] + q2[2]*q1[0] + q2[1]*q1[3] - q2[0]*q1[2];
89
  output[2] = q2[3]*q1[2] + q2[2]*q1[3] - q2[1]*q1[0] + q2[0]*q1[1];
90
  output[3] = q2[3]*q1[3] - q2[2]*q1[2] - q2[1]*q1[1] - q2[0]*q1[0];
91
  }
92

    
93
///////////////////////////////////////////////////////////////////
94
// sin(A/2)*x, sin(A/2)*y, sin(A/2)*z, cos(A/2)
95

    
96
void create_quat(float* axis, float angle, float* output)
97
  {
98
  float cosAngle = cos(angle/2);
99
  float sinAngle = sin(angle/2);
100

    
101
  output[0] = sinAngle*axis[0];
102
  output[1] = sinAngle*axis[1];
103
  output[2] = sinAngle*axis[2];
104
  output[3] = cosAngle;
105
  }
106

    
107
///////////////////////////////////////////////////////////////////
108
// double cover, so q == -q
109

    
110
int is_the_same(float* q1, float* q2)
111
  {
112
  const float MAX = 0.01f;
113

    
114
  float d0 = q1[0]-q2[0];
115
  float d1 = q1[1]-q2[1];
116
  float d2 = q1[2]-q2[2];
117
  float d3 = q1[3]-q2[3];
118

    
119
  if( d0<MAX && d0>-MAX &&
120
      d1<MAX && d1>-MAX &&
121
      d2<MAX && d2>-MAX &&
122
      d3<MAX && d3>-MAX  ) return 1;
123

    
124
  d0 = q1[0]+q2[0];
125
  d1 = q1[1]+q2[1];
126
  d2 = q1[2]+q2[2];
127
  d3 = q1[3]+q2[3];
128

    
129
  if( d0<MAX && d0>-MAX &&
130
      d1<MAX && d1>-MAX &&
131
      d2<MAX && d2>-MAX &&
132
      d3<MAX && d3>-MAX  ) return 1;
133

    
134
  return 0;
135
  }
136

    
137
///////////////////////////////////////////////////////////////////
138

    
139
int getIndexInTable(float* quat, float* table)
140
  {
141
  for(int i=0; i<inserted; i++)
142
    {
143
    if( is_the_same(quat,table+4*i)==1 ) return i;
144
    }
145

    
146
  return -1;
147
  }
148

    
149
///////////////////////////////////////////////////////////////////
150

    
151
void insert(float* quat, float* table)
152
  {
153
  if( getIndexInTable(quat,table)<0 )
154
    {
155
    if( inserted<NUM_QUATS )
156
      {
157
      table[4*inserted+0] = quat[0];
158
      table[4*inserted+1] = quat[1];
159
      table[4*inserted+2] = quat[2];
160
      table[4*inserted+3] = quat[3];
161

    
162
      inserted++;
163
      }
164
    else printf("Error: inserted=%d\n", inserted);
165
    }
166
  }
167

    
168
///////////////////////////////////////////////////////////////////
169

    
170
void normalize(int index)
171
  {
172
  float* a = axis[index];
173

    
174
  float len = (float)(sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]));
175

    
176
  a[0] /= len;  
177
  a[1] /= len;  
178
  a[2] /= len;  
179
  }
180

    
181
///////////////////////////////////////////////////////////////////
182

    
183
int main(int argc, char** argv)
184
  {
185
  float tmp[4];
186
  float table[4*NUM_QUATS];
187
  int num,NUM_AXIS = sizeof(axis) / sizeof(axis[0]);
188

    
189
  tmp[0] = 0.0f; tmp[1] = 0.0f; tmp[2] = 0.0f; tmp[3] = 1.0f;
190
  insert(tmp,table);
191

    
192
  for(int ax=0; ax<NUM_AXIS; ax++)
193
    {
194
    normalize(ax);
195
    }
196

    
197
  for( int ax=0; ax<NUM_AXIS; ax++)
198
    for(int angle=1; angle<basic[ax]; angle++)
199
      {
200
      create_quat(axis[ax], 2*PI*angle/basic[ax], tmp);
201
      insert(tmp,table); 
202
      }
203

    
204
  do
205
    {
206
    num = inserted;
207

    
208
    for(int i=0; i<num; i++)
209
      for(int j=0; j<num; j++)
210
        {
211
        multiply_quats( table+4*i, table+4*j, tmp);
212
        insert(tmp,table);
213
        }
214
    }
215
  while( num < inserted );
216

    
217
  printf("\n");
218

    
219
  for(int i=0; i<inserted; i++)
220
    {
221
    printf( "%2d %7.4f %7.4f %7.4f %7.4f\n", i, table[4*i], table[4*i+1], table[4*i+2], table[4*i+3] );
222
    }
223

    
224
  printf("\n");
225

    
226
  for(int i=0; i<inserted; i++)
227
    {
228
    printf("{");
229

    
230
    for(int j=0; j<inserted; j++)
231
      {
232
      multiply_quats( table+4*i, table+4*j, tmp);
233
      num = getIndexInTable(tmp,table);
234
      printf("%3d," , num);
235
      }
236
    printf("},\n");
237
    }
238
 
239
return 0;
240
}
(5-5/32)