1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2022 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.objectlib.helpers;
|
11
|
|
12
|
import org.distorted.library.helpers.QuatHelper;
|
13
|
import org.distorted.library.type.Static3D;
|
14
|
import org.distorted.library.type.Static4D;
|
15
|
|
16
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
17
|
|
18
|
public class QuatGroupGenerator
|
19
|
{
|
20
|
private static final int MAX_GROUP_SIZE = 256;
|
21
|
private static final double PI = Math.PI;
|
22
|
private static final Static4D[] mTable= new Static4D[MAX_GROUP_SIZE];
|
23
|
|
24
|
private static int mInserted;
|
25
|
|
26
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
27
|
// sin(A/2)*x, sin(A/2)*y, sin(A/2)*z, cos(A/2)
|
28
|
|
29
|
private static void createQuat(Static3D axis, double angle, Static4D output)
|
30
|
{
|
31
|
float cosAngle = (float)Math.cos(angle/2);
|
32
|
float sinAngle = (float)Math.sin(angle/2);
|
33
|
|
34
|
output.set0( sinAngle*axis.get0() );
|
35
|
output.set1( sinAngle*axis.get1() );
|
36
|
output.set2( sinAngle*axis.get2() );
|
37
|
output.set3( cosAngle );
|
38
|
}
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
// double cover, so q == -q
|
42
|
|
43
|
private static boolean quatTheSame(Static4D q1, Static4D q2)
|
44
|
{
|
45
|
final float MAX_ERROR = 0.01f;
|
46
|
|
47
|
float q10 = q1.get0();
|
48
|
float q11 = q1.get1();
|
49
|
float q12 = q1.get2();
|
50
|
float q13 = q1.get3();
|
51
|
|
52
|
float q20 = q2.get0();
|
53
|
float q21 = q2.get1();
|
54
|
float q22 = q2.get2();
|
55
|
float q23 = q2.get3();
|
56
|
|
57
|
float d0 = q10-q20;
|
58
|
float d1 = q11-q21;
|
59
|
float d2 = q12-q22;
|
60
|
float d3 = q13-q23;
|
61
|
|
62
|
if( d0<MAX_ERROR && d0>-MAX_ERROR &&
|
63
|
d1<MAX_ERROR && d1>-MAX_ERROR &&
|
64
|
d2<MAX_ERROR && d2>-MAX_ERROR &&
|
65
|
d3<MAX_ERROR && d3>-MAX_ERROR ) return true;
|
66
|
|
67
|
d0 = q10+q20;
|
68
|
d1 = q11+q21;
|
69
|
d2 = q12+q22;
|
70
|
d3 = q13+q23;
|
71
|
|
72
|
if( d0<MAX_ERROR && d0>-MAX_ERROR &&
|
73
|
d1<MAX_ERROR && d1>-MAX_ERROR &&
|
74
|
d2<MAX_ERROR && d2>-MAX_ERROR &&
|
75
|
d3<MAX_ERROR && d3>-MAX_ERROR ) return true;
|
76
|
|
77
|
return false;
|
78
|
}
|
79
|
|
80
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
81
|
|
82
|
private static int getIndexInTable(Static4D quat, Static4D[] table)
|
83
|
{
|
84
|
for(int i=0; i<mInserted; i++)
|
85
|
{
|
86
|
if( quatTheSame(quat,table[i]) ) return i;
|
87
|
}
|
88
|
|
89
|
return -1;
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
private static boolean insertQuat(Static4D quat, Static4D[] table)
|
95
|
{
|
96
|
if( getIndexInTable(quat,table)<0 )
|
97
|
{
|
98
|
if( mInserted<MAX_GROUP_SIZE )
|
99
|
{
|
100
|
table[mInserted] = new Static4D(quat);
|
101
|
mInserted++;
|
102
|
return true;
|
103
|
}
|
104
|
return false;
|
105
|
}
|
106
|
|
107
|
return true;
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
// PUBLIC API
|
112
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
113
|
|
114
|
public static Static4D[] computeGroup(Static3D[] rotAxis, int[][] basicAngles)
|
115
|
{
|
116
|
Static4D quat = new Static4D(0,0,0,1);
|
117
|
int num,numAxis = rotAxis.length;
|
118
|
mInserted = 0;
|
119
|
|
120
|
insertQuat(quat,mTable);
|
121
|
|
122
|
for( int ax=0; ax<numAxis; ax++)
|
123
|
{
|
124
|
int numLayers = basicAngles[ax].length;
|
125
|
|
126
|
for( int layer=0; layer<numLayers; layer++)
|
127
|
{
|
128
|
int numAngles = basicAngles[ax][layer];
|
129
|
|
130
|
for(int angle=1; angle<numAngles; angle++)
|
131
|
{
|
132
|
createQuat(rotAxis[ax], (2*PI*angle)/numAngles, quat);
|
133
|
insertQuat(quat,mTable);
|
134
|
}
|
135
|
}
|
136
|
}
|
137
|
|
138
|
do
|
139
|
{
|
140
|
num = mInserted;
|
141
|
|
142
|
for(int i=0; i<num; i++)
|
143
|
for(int j=0; j<num; j++)
|
144
|
{
|
145
|
quat = QuatHelper.quatMultiply(mTable[i], mTable[j]);
|
146
|
|
147
|
if( !insertQuat(quat,mTable) )
|
148
|
{
|
149
|
i=num;j=num;
|
150
|
android.util.Log.e("QuatGroupGenerator", "OVERFLOW");
|
151
|
}
|
152
|
}
|
153
|
}
|
154
|
while( num < mInserted );
|
155
|
|
156
|
Static4D[] table = new Static4D[mInserted];
|
157
|
|
158
|
for(int i=0; i<mInserted; i++)
|
159
|
{
|
160
|
table[i] = new Static4D(mTable[i]);
|
161
|
}
|
162
|
|
163
|
return table;
|
164
|
}
|
165
|
|
166
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
private static String printRotation(Static4D quat)
|
169
|
{
|
170
|
final float MAX_ERR = 0.00001f;
|
171
|
float cos = quat.get3();
|
172
|
float sin = (float)Math.sqrt(1-cos*cos);
|
173
|
float x = sin==0 ? 0 : quat.get0()/sin;
|
174
|
float y = sin==0 ? 0 : quat.get1()/sin;
|
175
|
float z = sin==0 ? 0 : quat.get2()/sin;
|
176
|
float halfAngle = (float)Math.acos(cos);
|
177
|
int ang = (int) ( (360*halfAngle/Math.PI) + 0.5f);
|
178
|
|
179
|
if( x<MAX_ERR && x>-MAX_ERR ) x = 0.0f;
|
180
|
if( y<MAX_ERR && y>-MAX_ERR ) y = 0.0f;
|
181
|
if( z<MAX_ERR && z>-MAX_ERR ) z = 0.0f;
|
182
|
|
183
|
if( x<0.5f+MAX_ERR && x>0.5f-MAX_ERR ) x = 0.5f;
|
184
|
if( y<0.5f+MAX_ERR && y>0.5f-MAX_ERR ) y = 0.5f;
|
185
|
if( z<0.5f+MAX_ERR && z>0.5f-MAX_ERR ) z = 0.5f;
|
186
|
|
187
|
if( x<-0.5f+MAX_ERR && x>-0.5f-MAX_ERR ) x = -0.5f;
|
188
|
if( y<-0.5f+MAX_ERR && y>-0.5f-MAX_ERR ) y = -0.5f;
|
189
|
if( z<-0.5f+MAX_ERR && z>-0.5f-MAX_ERR ) z = -0.5f;
|
190
|
|
191
|
if( x<1.0f+MAX_ERR && x>1.0f-MAX_ERR ) x = 1.0f;
|
192
|
if( y<1.0f+MAX_ERR && y>1.0f-MAX_ERR ) y = 1.0f;
|
193
|
if( z<1.0f+MAX_ERR && z>1.0f-MAX_ERR ) z = 1.0f;
|
194
|
|
195
|
if( x<-1.0f+MAX_ERR && x>-1.0f-MAX_ERR ) x = -1.0f;
|
196
|
if( y<-1.0f+MAX_ERR && y>-1.0f-MAX_ERR ) y = -1.0f;
|
197
|
if( z<-1.0f+MAX_ERR && z>-1.0f-MAX_ERR ) z = -1.0f;
|
198
|
|
199
|
return "("+x+" "+y+" "+z+") "+ang;
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
private static String printQuat(Static4D quat)
|
205
|
{
|
206
|
final float MAX_ERR = 0.00001f;
|
207
|
|
208
|
float x = quat.get0();
|
209
|
float y = quat.get1();
|
210
|
float z = quat.get2();
|
211
|
float w = quat.get3();
|
212
|
|
213
|
if( x<MAX_ERR && x>-MAX_ERR ) x = 0.0f;
|
214
|
if( y<MAX_ERR && y>-MAX_ERR ) y = 0.0f;
|
215
|
if( z<MAX_ERR && z>-MAX_ERR ) z = 0.0f;
|
216
|
if( w<MAX_ERR && w>-MAX_ERR ) w = 0.0f;
|
217
|
|
218
|
if( x<0.5f+MAX_ERR && x>0.5f-MAX_ERR ) x = 0.5f;
|
219
|
if( y<0.5f+MAX_ERR && y>0.5f-MAX_ERR ) y = 0.5f;
|
220
|
if( z<0.5f+MAX_ERR && z>0.5f-MAX_ERR ) z = 0.5f;
|
221
|
if( w<0.5f+MAX_ERR && w>0.5f-MAX_ERR ) w = 0.5f;
|
222
|
|
223
|
if( x<-0.5f+MAX_ERR && x>-0.5f-MAX_ERR ) x = -0.5f;
|
224
|
if( y<-0.5f+MAX_ERR && y>-0.5f-MAX_ERR ) y = -0.5f;
|
225
|
if( z<-0.5f+MAX_ERR && z>-0.5f-MAX_ERR ) z = -0.5f;
|
226
|
if( w<-0.5f+MAX_ERR && w>-0.5f-MAX_ERR ) w = -0.5f;
|
227
|
|
228
|
if( x<1.0f+MAX_ERR && x>1.0f-MAX_ERR ) x = 1.0f;
|
229
|
if( y<1.0f+MAX_ERR && y>1.0f-MAX_ERR ) y = 1.0f;
|
230
|
if( z<1.0f+MAX_ERR && z>1.0f-MAX_ERR ) z = 1.0f;
|
231
|
if( w<1.0f+MAX_ERR && w>1.0f-MAX_ERR ) w = 1.0f;
|
232
|
|
233
|
if( x<-1.0f+MAX_ERR && x>-1.0f-MAX_ERR ) x = -1.0f;
|
234
|
if( y<-1.0f+MAX_ERR && y>-1.0f-MAX_ERR ) y = -1.0f;
|
235
|
if( z<-1.0f+MAX_ERR && z>-1.0f-MAX_ERR ) z = -1.0f;
|
236
|
if( w<-1.0f+MAX_ERR && w>-1.0f-MAX_ERR ) w = -1.0f;
|
237
|
|
238
|
return (x+" "+y+" "+z+" "+w);
|
239
|
}
|
240
|
|
241
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242
|
|
243
|
public static void showQuats(Static4D[] table)
|
244
|
{
|
245
|
int len = table.length;
|
246
|
|
247
|
for (int i=0; i<len; i++)
|
248
|
{
|
249
|
String tmp = printQuat(table[i]);
|
250
|
android.util.Log.e("D", "QUAT"+i+": ("+tmp+")");
|
251
|
}
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
public static void showRotations(Static4D[] table)
|
257
|
{
|
258
|
int len = table.length;
|
259
|
|
260
|
for (int i=0; i<len; i++)
|
261
|
{
|
262
|
String tmp = printRotation(table[i]);
|
263
|
android.util.Log.e("D", "QUAT"+i+": "+tmp);
|
264
|
}
|
265
|
}
|
266
|
|
267
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
268
|
|
269
|
public static void showCubitQuats(Static4D[] table, float[][] pos, int startIndex, int stopIndex)
|
270
|
{
|
271
|
StringBuilder builder = new StringBuilder();
|
272
|
float MAXERR = 0.01f;
|
273
|
float[] tmp = new float[4];
|
274
|
float[] vec = pos[startIndex];
|
275
|
int numQuats = table.length;
|
276
|
float X = vec[0];
|
277
|
float Y = vec[1];
|
278
|
float Z = vec[2];
|
279
|
|
280
|
for(int cubit=startIndex; cubit<=stopIndex; cubit++)
|
281
|
{
|
282
|
float x = pos[cubit][0];
|
283
|
float y = pos[cubit][1];
|
284
|
float z = pos[cubit][2];
|
285
|
|
286
|
for(int quat=0; quat<numQuats; quat++)
|
287
|
{
|
288
|
QuatHelper.rotateVectorByQuat(tmp,X,Y,Z,0,table[quat]);
|
289
|
|
290
|
float dx = tmp[0]-x;
|
291
|
float dy = tmp[1]-y;
|
292
|
float dz = tmp[2]-z;
|
293
|
|
294
|
if( dx>-MAXERR && dx<MAXERR && dy>-MAXERR && dy<MAXERR && dz>-MAXERR && dz<MAXERR )
|
295
|
{
|
296
|
builder.append(' ');
|
297
|
builder.append(quat);
|
298
|
}
|
299
|
}
|
300
|
|
301
|
android.util.Log.e("D", "Cubit: "+cubit+" quats: "+builder );
|
302
|
builder.delete(0, builder.length());
|
303
|
}
|
304
|
}
|
305
|
}
|