Project

General

Profile

Download (4.19 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / res / raw / compute_quats.c @ 6a083c6a

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

    
5
#define PYRA
6

    
7
#define SQ2 1.41421356237f
8
#define SQ3 1.73205080757f
9
#define PI  3.14159265358f
10
#define NUM_QUATS  100
11

    
12
#ifdef PYRA 
13
#define NUM_AXIS    4
14
#define BASIC_ANGLE 3
15

    
16
float axis[NUM_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
#define NUM_AXIS    3
24
#define BASIC_ANGLE 4
25

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

    
31
#ifdef DINO
32
#define NUM_AXIS    4
33
#define BASIC_ANGLE 3
34

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

    
41
#ifdef DIAM
42
#define NUM_AXIS    4
43
#define BASIC_ANGLE 3
44

    
45
float axis[NUM_AXIS][3] = { {+SQ3*SQ2/3,+SQ3/3,         0} ,
46
                            {-SQ3*SQ2/3,+SQ3/3,         0} ,
47
                            {         0,+SQ3/3,+SQ3*SQ2/3} ,
48
                            {         0,+SQ3/3,-SQ3*SQ2/3} };
49
#endif
50

    
51
#ifdef HELI
52
#define NUM_AXIS 6
53
#define BASIC_ANGLE 2
54

    
55
float axis[NUM_AXIS][3] = { {     0, +SQ2/2, -SQ2/2} ,
56
                            {     0, -SQ2/2, -SQ2/2} ,
57
                            {+SQ2/2,      0, -SQ2/2} ,
58
                            {-SQ2/2,      0, -SQ2/2} ,
59
                            {+SQ2/2, -SQ2/2,      0} ,
60
                            {-SQ2/2, -SQ2/2,      0} };
61

    
62
#endif
63

    
64
int inserted=0;
65

    
66
///////////////////////////////////////////////////////////////////
67
// q1*q2
68

    
69
void multiply_quats( float* q1, float* q2, float* output)
70
  {
71
  output[0] = q2[3]*q1[0] - q2[2]*q1[1] + q2[1]*q1[2] + q2[0]*q1[3];
72
  output[1] = q2[3]*q1[1] + q2[2]*q1[0] + q2[1]*q1[3] - q2[0]*q1[2];
73
  output[2] = q2[3]*q1[2] + q2[2]*q1[3] - q2[1]*q1[0] + q2[0]*q1[1];
74
  output[3] = q2[3]*q1[3] - q2[2]*q1[2] - q2[1]*q1[1] - q2[0]*q1[0];
75
  }
76

    
77
///////////////////////////////////////////////////////////////////
78
// sin(A/2)*x, sin(A/2)*y, sin(A/2)*z, cos(A/2)
79

    
80
void create_quat(float* axis, float angle, float* output)
81
  {
82
  float cosAngle = cos(angle/2);
83
  float sinAngle = sin(angle/2);
84

    
85
  output[0] = sinAngle*axis[0];
86
  output[1] = sinAngle*axis[1];
87
  output[2] = sinAngle*axis[2];
88
  output[3] = cosAngle;
89
  }
90

    
91
///////////////////////////////////////////////////////////////////
92
// double cover, so q == -q
93

    
94
int is_the_same(float* q1, float* q2)
95
  {
96
  const float MAX = 0.01f;
97

    
98
  float d0 = q1[0]-q2[0];
99
  float d1 = q1[1]-q2[1];
100
  float d2 = q1[2]-q2[2];
101
  float d3 = q1[3]-q2[3];
102

    
103
  if( d0<MAX && d0>-MAX &&
104
      d1<MAX && d1>-MAX &&
105
      d2<MAX && d2>-MAX &&
106
      d3<MAX && d3>-MAX  ) return 1;
107

    
108
  d0 = q1[0]+q2[0];
109
  d1 = q1[1]+q2[1];
110
  d2 = q1[2]+q2[2];
111
  d3 = q1[3]+q2[3];
112

    
113
  if( d0<MAX && d0>-MAX &&
114
      d1<MAX && d1>-MAX &&
115
      d2<MAX && d2>-MAX &&
116
      d3<MAX && d3>-MAX  ) return 1;
117

    
118
  return 0;
119
  }
120

    
121
///////////////////////////////////////////////////////////////////
122

    
123
void insert(float* quat, float* to)
124
  {
125
  for(int i=0; i<inserted; i++)
126
    {
127
    if( is_the_same(quat,to+4*i)==1 ) return;
128
    }
129

    
130
  to[4*inserted+0] = quat[0];
131
  to[4*inserted+1] = quat[1];
132
  to[4*inserted+2] = quat[2];
133
  to[4*inserted+3] = quat[3];
134

    
135
  inserted++;
136
  }
137

    
138
///////////////////////////////////////////////////////////////////
139

    
140
int main(int argc, char** argv)
141
  {
142
  float tmp[4];
143
  float table[4*NUM_QUATS];
144
  int num;
145

    
146
  tmp[0] = 0.0f; tmp[1] = 0.0f; tmp[2] = 0.0f; tmp[3] = 1.0f;
147
  insert(tmp,table);
148

    
149
  for(int angle=1; angle<BASIC_ANGLE; angle++)
150
    for( int ax=0; ax<NUM_AXIS; ax++)
151
      {
152
      create_quat(axis[ax], 2*PI*angle/BASIC_ANGLE, tmp);
153
      insert(tmp,table); 
154
      }
155

    
156
  do
157
    {
158
    num = inserted;
159

    
160
    for(int i=0; i<num; i++)
161
      for(int j=0; j<num; j++)
162
        {
163
        multiply_quats( table+4*i, table+4*j, tmp);
164
        insert(tmp,table);
165
        }
166
    }
167
  while( num < inserted );
168

    
169
  printf("inserted: %d\n", inserted);
170

    
171
  for(int i=0; i<inserted; i++)
172
    {
173
    printf( "%d %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] );
174
    }
175

    
176
return 0;
177
}
(5-5/36)