Project

General

Profile

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

magiccube / src / main / res / raw / compute_quats.c @ 92843d3b

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

    
5
#define DINO
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] = { {         0,       1,       0 } ,
17
                            { SQ2*SQ3/3, -1.0f/3,  -SQ2/3 } ,
18
                            {-SQ2*SQ3/3, -1.0f/3,  -SQ2/3 } ,
19
                            {         0, -1.0f/3, 2*SQ2/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 HELI
42
#define NUM_AXIS 6
43
#define BASIC_ANGLE 2
44

    
45
float axis[NUM_AXIS][3] = { {     0, +SQ2/2, -SQ2/2} ,
46
                            {     0, -SQ2/2, -SQ2/2} ,
47
                            {+SQ2/2,      0, -SQ2/2} ,
48
                            {-SQ2/2,      0, -SQ2/2} ,
49
                            {+SQ2/2, -SQ2/2,      0} ,
50
                            {-SQ2/2, -SQ2/2,      0} };
51

    
52
#endif
53

    
54
int inserted=0;
55

    
56
///////////////////////////////////////////////////////////////////
57
// q1*q2
58

    
59
void multiply_quats( float* q1, float* q2, float* output)
60
  {
61
  output[0] = q2[3]*q1[0] - q2[2]*q1[1] + q2[1]*q1[2] + q2[0]*q1[3];
62
  output[1] = q2[3]*q1[1] + q2[2]*q1[0] + q2[1]*q1[3] - q2[0]*q1[2];
63
  output[2] = q2[3]*q1[2] + q2[2]*q1[3] - q2[1]*q1[0] + q2[0]*q1[1];
64
  output[3] = q2[3]*q1[3] - q2[2]*q1[2] - q2[1]*q1[1] - q2[0]*q1[0];
65
  }
66

    
67
///////////////////////////////////////////////////////////////////
68
// sin(A/2)*x, sin(A/2)*y, sin(A/2)*z, cos(A/2)
69

    
70
void create_quat(float* axis, float angle, float* output)
71
  {
72
  float cosAngle = cos(angle/2);
73
  float sinAngle = sin(angle/2);
74

    
75
  output[0] = sinAngle*axis[0];
76
  output[1] = sinAngle*axis[1];
77
  output[2] = sinAngle*axis[2];
78
  output[3] = cosAngle;
79
  }
80

    
81
///////////////////////////////////////////////////////////////////
82
// double cover, so q == -q
83

    
84
int is_the_same(float* q1, float* q2)
85
  {
86
  const float MAX = 0.01f;
87

    
88
  float d0 = q1[0]-q2[0];
89
  float d1 = q1[1]-q2[1];
90
  float d2 = q1[2]-q2[2];
91
  float d3 = q1[3]-q2[3];
92

    
93
  if( d0<MAX && d0>-MAX &&
94
      d1<MAX && d1>-MAX &&
95
      d2<MAX && d2>-MAX &&
96
      d3<MAX && d3>-MAX  ) return 1;
97

    
98
  d0 = q1[0]+q2[0];
99
  d1 = q1[1]+q2[1];
100
  d2 = q1[2]+q2[2];
101
  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
  return 0;
109
  }
110

    
111
///////////////////////////////////////////////////////////////////
112

    
113
void insert(float* quat, float* to)
114
  {
115
  for(int i=0; i<inserted; i++)
116
    {
117
    if( is_the_same(quat,to+4*i)==1 ) return;
118
    }
119

    
120
  to[4*inserted+0] = quat[0];
121
  to[4*inserted+1] = quat[1];
122
  to[4*inserted+2] = quat[2];
123
  to[4*inserted+3] = quat[3];
124

    
125
  inserted++;
126
  }
127

    
128
///////////////////////////////////////////////////////////////////
129

    
130
int main(int argc, char** argv)
131
  {
132
  float tmp[4];
133
  float table[4*NUM_QUATS];
134
  int num;
135

    
136
  tmp[0] = 0.0f; tmp[1] = 0.0f; tmp[2] = 0.0f; tmp[3] = 1.0f;
137
  insert(tmp,table);
138

    
139
  for(int angle=1; angle<BASIC_ANGLE; angle++)
140
    for( int ax=0; ax<NUM_AXIS; ax++)
141
      {
142
      create_quat(axis[ax], 2*PI*angle/BASIC_ANGLE, tmp);
143
      insert(tmp,table); 
144
      }
145

    
146
  do
147
    {
148
    num = inserted;
149

    
150
    for(int i=0; i<num; i++)
151
      for(int j=0; j<num; j++)
152
        {
153
        multiply_quats( table+4*i, table+4*j, tmp);
154
        insert(tmp,table);
155
        }
156
    }
157
  while( num < inserted );
158

    
159
  printf("inserted: %d\n", inserted);
160

    
161
  for(int i=0; i<inserted; i++)
162
    {
163
    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] );
164
    }
165

    
166
return 0;
167
}
(1-1/23)