Project

General

Profile

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

magiccube / src / main / res / raw / compute_quats.c @ 418aa554

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 PYRAMIX 
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 }, {0,1,0}, {0,0,1} };
27
#endif
28

    
29
#ifdef DINO
30
#define NUM_AXIS    4
31
#define BASIC_ANGLE 3
32

    
33
float axis[NUM_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
float* quats;
40
float* table;
41
int inserted=0;
42

    
43
///////////////////////////////////////////////////////////////////
44
// q1*q2
45

    
46
void multiply_quats( float* q1, float* q2, float* output)
47
  {
48
  output[0] = q2[3]*q1[0] - q2[2]*q1[1] + q2[1]*q1[2] + q2[0]*q1[3];
49
  output[1] = q2[3]*q1[1] + q2[2]*q1[0] + q2[1]*q1[3] - q2[0]*q1[2];
50
  output[2] = q2[3]*q1[2] + q2[2]*q1[3] - q2[1]*q1[0] + q2[0]*q1[1];
51
  output[3] = q2[3]*q1[3] - q2[2]*q1[2] - q2[1]*q1[1] - q2[0]*q1[0];
52
  }
53

    
54
///////////////////////////////////////////////////////////////////
55
// sin(A/2)*x, sin(A/2)*y, sin(A/2)*z, cos(A/2)
56

    
57
void create_quat(float* axis, float angle, float* output)
58
  {
59
  float cosAngle = cos(angle/2);
60
  float sinAngle = sin(angle/2);
61

    
62
  output[0] = sinAngle*axis[0];
63
  output[1] = sinAngle*axis[1];
64
  output[2] = sinAngle*axis[2];
65
  output[3] = cosAngle;
66
  }
67

    
68
///////////////////////////////////////////////////////////////////
69
// double cover, so q == -q
70

    
71
int is_the_same(float* q1, float* q2)
72
  {
73
  const float MAX = 0.01f;
74

    
75
  float d0 = q1[0]-q2[0];
76
  float d1 = q1[1]-q2[1];
77
  float d2 = q1[2]-q2[2];
78
  float d3 = q1[3]-q2[3];
79

    
80
  if( d0<MAX && d0>-MAX &&
81
      d1<MAX && d1>-MAX &&
82
      d2<MAX && d2>-MAX &&
83
      d3<MAX && d3>-MAX  ) return 1;
84

    
85
  d0 = q1[0]+q2[0];
86
  d1 = q1[1]+q2[1];
87
  d2 = q1[2]+q2[2];
88
  d3 = q1[3]+q2[3];
89

    
90
  if( d0<MAX && d0>-MAX &&
91
      d1<MAX && d1>-MAX &&
92
      d2<MAX && d2>-MAX &&
93
      d3<MAX && d3>-MAX  ) return 1;
94

    
95
  return 0;
96
  }
97

    
98
///////////////////////////////////////////////////////////////////
99

    
100
void insert(float* quat, float* to)
101
  {
102
  for(int i=0; i<inserted; i++)
103
    {
104
    if( is_the_same(quat,to+4*i)==1 ) return;
105
    }
106

    
107
  to[4*inserted+0] = quat[0];
108
  to[4*inserted+1] = quat[1];
109
  to[4*inserted+2] = quat[2];
110
  to[4*inserted+3] = quat[3];
111

    
112
  inserted++;
113
  }
114

    
115
///////////////////////////////////////////////////////////////////
116

    
117
int main(int argc, char** argv)
118
  {
119
  float tmp[4];
120
  int num = 1+NUM_AXIS*(BASIC_ANGLE-1);
121

    
122
  quats = (float*) malloc(4*sizeof(float)*num      );
123
  table = (float*) malloc(4*sizeof(float)*NUM_QUATS);
124

    
125
  tmp[0] = 0.0f; tmp[1] = 0.0f; tmp[2] = 0.0f; tmp[3] = 1.0f;
126
  insert(tmp,quats);
127

    
128
  for(int angle=1; angle<BASIC_ANGLE; angle++)
129
    for( int ax=0; ax<NUM_AXIS; ax++)
130
      {
131
      create_quat(axis[ax], 2*PI*angle/BASIC_ANGLE, tmp);
132
      insert(tmp,quats); 
133
      }
134

    
135
  inserted=0;
136

    
137
  for(int i=0; i<num; i++)
138
    for(int j=0; j<num; j++)
139
      {
140
      multiply_quats( quats+4*i, quats+4*j, tmp);
141
      insert(tmp,table);
142
      }
143

    
144
  printf("inserted: %d\n", inserted);
145

    
146
  for(int i=0; i<inserted; i++)
147
    {
148
    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] );
149
    }
150

    
151
return 0;
152
}
(1-1/20)