1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.objects;
|
21
|
|
22
|
import android.content.SharedPreferences;
|
23
|
|
24
|
import org.distorted.library.effect.MatrixEffectMove;
|
25
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
26
|
import org.distorted.library.effect.MatrixEffectRotate;
|
27
|
import org.distorted.library.main.DistortedEffects;
|
28
|
import org.distorted.library.main.DistortedNode;
|
29
|
import org.distorted.library.mesh.MeshBase;
|
30
|
import org.distorted.library.message.EffectListener;
|
31
|
import org.distorted.library.type.Dynamic1D;
|
32
|
import org.distorted.library.type.Static1D;
|
33
|
import org.distorted.library.type.Static3D;
|
34
|
import org.distorted.library.type.Static4D;
|
35
|
import org.distorted.main.RubikSurfaceView;
|
36
|
|
37
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
38
|
|
39
|
class Cubit
|
40
|
{
|
41
|
private static final Static3D matrCenter = new Static3D(0,0,0);
|
42
|
|
43
|
private final Static3D mOrigPosition;
|
44
|
|
45
|
private RubikObject mParent;
|
46
|
private MeshBase mMesh;
|
47
|
private Static3D mRotationAxis;
|
48
|
private MatrixEffectRotate mRotateEffect;
|
49
|
private Static3D mCurrentPosition;
|
50
|
private int mNumAxis;
|
51
|
|
52
|
Dynamic1D mRotationAngle;
|
53
|
DistortedNode mNode;
|
54
|
DistortedEffects mEffect;
|
55
|
Static4D mQuatScramble;
|
56
|
float[] mRotationRow;
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
// Because of quatMultiplication, errors can accumulate - so to avoid this, we
|
60
|
// correct the value of the 'scramble' quat to what it should be - one of the legal quats from the
|
61
|
// list LEGAL_QUATS.
|
62
|
//
|
63
|
// We also have to remember that the group of unit quaternions is a double-cover of rotations
|
64
|
// in 3D ( q represents the same rotation as -q ) - so invert if needed.
|
65
|
|
66
|
private void normalizeScrambleQuat(Static4D quat)
|
67
|
{
|
68
|
final float MAX_ERROR = 0.0001f;
|
69
|
|
70
|
float x = quat.get0();
|
71
|
float y = quat.get1();
|
72
|
float z = quat.get2();
|
73
|
float w = quat.get3();
|
74
|
float diff;
|
75
|
|
76
|
for(float legal: mParent.LEGAL_QUATS)
|
77
|
{
|
78
|
diff = x-legal;
|
79
|
if( diff*diff<MAX_ERROR ) x = legal;
|
80
|
diff = y-legal;
|
81
|
if( diff*diff<MAX_ERROR ) y = legal;
|
82
|
diff = z-legal;
|
83
|
if( diff*diff<MAX_ERROR ) z = legal;
|
84
|
diff = w-legal;
|
85
|
if( diff*diff<MAX_ERROR ) w = legal;
|
86
|
}
|
87
|
|
88
|
if( w<0 )
|
89
|
{
|
90
|
w = -w;
|
91
|
z = -z;
|
92
|
y = -y;
|
93
|
x = -x;
|
94
|
}
|
95
|
else if( w==0 )
|
96
|
{
|
97
|
if( z<0 )
|
98
|
{
|
99
|
z = -z;
|
100
|
y = -y;
|
101
|
x = -x;
|
102
|
}
|
103
|
else if( z==0 )
|
104
|
{
|
105
|
if( y<0 )
|
106
|
{
|
107
|
y = -y;
|
108
|
x = -x;
|
109
|
}
|
110
|
else if( y==0 )
|
111
|
{
|
112
|
if( x<0 )
|
113
|
{
|
114
|
x = -x;
|
115
|
}
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
|
120
|
quat.set(x,y,z,w);
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
private void modifyCurrentPosition(Static4D quat)
|
126
|
{
|
127
|
float cubitCenterX = mCurrentPosition.get0();
|
128
|
float cubitCenterY = mCurrentPosition.get1();
|
129
|
float cubitCenterZ = mCurrentPosition.get2();
|
130
|
|
131
|
Static4D cubitCenter = new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
|
132
|
Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
|
133
|
|
134
|
float rotatedX = rotatedCenter.get0();
|
135
|
float rotatedY = rotatedCenter.get1();
|
136
|
float rotatedZ = rotatedCenter.get2();
|
137
|
|
138
|
mCurrentPosition.set(rotatedX, rotatedY, rotatedZ);
|
139
|
mParent.clampPos(mCurrentPosition);
|
140
|
|
141
|
computeRotationRow();
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
// cast current position on axis; use mStart and mStep to compute the rotation row for each axis.
|
146
|
|
147
|
private void computeRotationRow()
|
148
|
{
|
149
|
float tmp;
|
150
|
Static3D axis;
|
151
|
float x = mCurrentPosition.get0();
|
152
|
float y = mCurrentPosition.get1();
|
153
|
float z = mCurrentPosition.get2();
|
154
|
|
155
|
for(int i=0; i<mNumAxis; i++)
|
156
|
{
|
157
|
axis = mParent.ROTATION_AXIS[i];
|
158
|
tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
|
159
|
mRotationRow[i] = (tmp-mParent.mStart)/mParent.mStep;
|
160
|
}
|
161
|
}
|
162
|
|
163
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
164
|
|
165
|
Cubit(RubikObject parent, MeshBase mesh, Static3D position)
|
166
|
{
|
167
|
float x = position.get0();
|
168
|
float y = position.get1();
|
169
|
float z = position.get2();
|
170
|
|
171
|
Static3D vector = new Static3D(x,y,z);
|
172
|
|
173
|
mParent = parent;
|
174
|
mMesh = mesh;
|
175
|
mOrigPosition = new Static3D(x,y,z);
|
176
|
mQuatScramble = new Static4D(0,0,0,1);
|
177
|
mRotationAngle = new Dynamic1D();
|
178
|
mRotationAxis = new Static3D(1,0,0);
|
179
|
mCurrentPosition = position;
|
180
|
mRotateEffect = new MatrixEffectRotate(mRotationAngle, mRotationAxis, matrCenter);
|
181
|
|
182
|
mNumAxis = mParent.ROTATION_AXIS.length;
|
183
|
mRotationRow = new float[mNumAxis];
|
184
|
computeRotationRow();
|
185
|
|
186
|
mEffect = new DistortedEffects();
|
187
|
mEffect.apply(mParent.mSinkEffect);
|
188
|
mEffect.apply( new MatrixEffectMove(vector) );
|
189
|
mEffect.apply( new MatrixEffectQuaternion(mQuatScramble, matrCenter));
|
190
|
mEffect.apply(mRotateEffect);
|
191
|
mEffect.apply(mParent.mQuatAEffect);
|
192
|
mEffect.apply(mParent.mQuatCEffect);
|
193
|
mEffect.apply(mParent.mScaleEffect);
|
194
|
|
195
|
mNode = new DistortedNode(mParent.mTexture,mEffect,mMesh);
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
void savePreferences(SharedPreferences.Editor editor)
|
201
|
{
|
202
|
String number = mOrigPosition.get0()+"_"+mOrigPosition.get1()+"_"+mOrigPosition.get2();
|
203
|
|
204
|
editor.putFloat("qx_"+number, mQuatScramble.get0());
|
205
|
editor.putFloat("qy_"+number, mQuatScramble.get1());
|
206
|
editor.putFloat("qz_"+number, mQuatScramble.get2());
|
207
|
editor.putFloat("qw_"+number, mQuatScramble.get3());
|
208
|
}
|
209
|
|
210
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
211
|
|
212
|
void restorePreferences(SharedPreferences preferences)
|
213
|
{
|
214
|
String number = mOrigPosition.get0()+"_"+mOrigPosition.get1()+"_"+mOrigPosition.get2();
|
215
|
|
216
|
float qx = preferences.getFloat("qx_"+number, 0.0f);
|
217
|
float qy = preferences.getFloat("qy_"+number, 0.0f);
|
218
|
float qz = preferences.getFloat("qz_"+number, 0.0f);
|
219
|
float qw = preferences.getFloat("qw_"+number, 1.0f);
|
220
|
|
221
|
mQuatScramble.set(qx,qy,qz,qw);
|
222
|
modifyCurrentPosition(mQuatScramble);
|
223
|
}
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
// return if the Cubit, when rotated with its own mQuatScramble, would have looked any different
|
227
|
// then if it were rotated by quaternion 'quat'.
|
228
|
// No it is not so simple as the quats need to be the same - imagine a 4x4x4 cube where the two
|
229
|
// middle squares get interchanged. No visible difference!
|
230
|
//
|
231
|
// So: this is true iff the cubit
|
232
|
// a) is a corner or edge and the quaternions are the same
|
233
|
// b) is inside one of the faces and after rotations by both quats it ends up on the same face.
|
234
|
|
235
|
boolean thereIsNoVisibleDifference(Static4D quat)
|
236
|
{
|
237
|
if ( mQuatScramble.get0()==quat.get0() &&
|
238
|
mQuatScramble.get1()==quat.get1() &&
|
239
|
mQuatScramble.get2()==quat.get2() &&
|
240
|
mQuatScramble.get3()==quat.get3() ) return true;
|
241
|
|
242
|
int belongsToHowManyFaces = 0;
|
243
|
int size = mParent.getSize()-1;
|
244
|
float row;
|
245
|
final float MAX_ERROR = 0.01f;
|
246
|
|
247
|
for(int i=0; i<mNumAxis; i++)
|
248
|
{
|
249
|
row = mRotationRow[i];
|
250
|
if( (row <MAX_ERROR && row >-MAX_ERROR) ||
|
251
|
(row-size<MAX_ERROR && row-size>-MAX_ERROR) ) belongsToHowManyFaces++;
|
252
|
}
|
253
|
|
254
|
switch(belongsToHowManyFaces)
|
255
|
{
|
256
|
case 0 : return true ; // 'inside' cubit that does not lie on any face
|
257
|
case 1 : // cubit that lies inside one of the faces
|
258
|
float cubitCenterX = mCurrentPosition.get0();
|
259
|
float cubitCenterY = mCurrentPosition.get1();
|
260
|
float cubitCenterZ = mCurrentPosition.get2();
|
261
|
|
262
|
Static4D cubitCenter = new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
|
263
|
Static4D rotated1 = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
|
264
|
Static4D rotated2 = RubikSurfaceView.rotateVectorByQuat( cubitCenter, mQuatScramble );
|
265
|
|
266
|
float row1, row2, row3, row4;
|
267
|
float ax,ay,az;
|
268
|
Static3D axis;
|
269
|
float x1 = rotated1.get0();
|
270
|
float y1 = rotated1.get1();
|
271
|
float z1 = rotated1.get2();
|
272
|
float x2 = rotated2.get0();
|
273
|
float y2 = rotated2.get1();
|
274
|
float z2 = rotated2.get2();
|
275
|
|
276
|
for(int i=0; i<mNumAxis; i++)
|
277
|
{
|
278
|
axis = mParent.ROTATION_AXIS[i];
|
279
|
ax = axis.get0();
|
280
|
ay = axis.get1();
|
281
|
az = axis.get2();
|
282
|
|
283
|
row1 = ((x1*ax + y1*ay + z1*az) - mParent.mStart) / mParent.mStep;
|
284
|
row2 = ((x2*ax + y2*ay + z2*az) - mParent.mStart) / mParent.mStep;
|
285
|
row3 = row1 - size;
|
286
|
row4 = row2 - size;
|
287
|
|
288
|
if( (row1<MAX_ERROR && row1>-MAX_ERROR && row2<MAX_ERROR && row2>-MAX_ERROR) ||
|
289
|
(row3<MAX_ERROR && row3>-MAX_ERROR && row4<MAX_ERROR && row4>-MAX_ERROR) )
|
290
|
{
|
291
|
return true;
|
292
|
}
|
293
|
}
|
294
|
return false;
|
295
|
default: return false; // edge or corner
|
296
|
}
|
297
|
}
|
298
|
|
299
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
300
|
|
301
|
long finishRotationNow(EffectListener listener)
|
302
|
{
|
303
|
int pointNum = mRotationAngle.getNumPoints();
|
304
|
|
305
|
if( pointNum>=1 )
|
306
|
{
|
307
|
float startingAngle = mRotationAngle.getPoint(pointNum-1).get0();
|
308
|
int nearestAngleInDegrees = mParent.computeNearestAngle(startingAngle);
|
309
|
mParent.mRotationAngleStatic.set0(startingAngle);
|
310
|
mParent.mRotationAngleFinal.set0(nearestAngleInDegrees);
|
311
|
mParent.mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-startingAngle)*0.2f );
|
312
|
return setUpCallback(listener);
|
313
|
}
|
314
|
|
315
|
return 0;
|
316
|
}
|
317
|
|
318
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
319
|
|
320
|
Static4D returnRotationQuat(int axis)
|
321
|
{
|
322
|
int pointNum = mRotationAngle.getNumPoints();
|
323
|
|
324
|
if( pointNum>=1 )
|
325
|
{
|
326
|
float axisX = mParent.ROTATION_AXIS[axis].get0();
|
327
|
float axisY = mParent.ROTATION_AXIS[axis].get1();
|
328
|
float axisZ = mParent.ROTATION_AXIS[axis].get2();
|
329
|
|
330
|
float startingAngle = mRotationAngle.getPoint(pointNum-1).get0();
|
331
|
int nearestAngleInDegrees = mParent.computeNearestAngle(startingAngle);
|
332
|
double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
|
333
|
float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
|
334
|
float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
|
335
|
return new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
336
|
}
|
337
|
|
338
|
return null;
|
339
|
}
|
340
|
|
341
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
342
|
|
343
|
void removeRotationNow(Static4D quat)
|
344
|
{
|
345
|
mRotationAngle.removeAll();
|
346
|
mQuatScramble.set(RubikSurfaceView.quatMultiply(quat,mQuatScramble));
|
347
|
normalizeScrambleQuat( mQuatScramble );
|
348
|
modifyCurrentPosition(quat);
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
|
353
|
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
|
354
|
// memory leak.
|
355
|
|
356
|
void releaseResources()
|
357
|
{
|
358
|
mMesh.markForDeletion();
|
359
|
mNode.markForDeletion();
|
360
|
}
|
361
|
|
362
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
363
|
|
364
|
void solve()
|
365
|
{
|
366
|
mQuatScramble.set(0,0,0,1);
|
367
|
mCurrentPosition.set(mOrigPosition);
|
368
|
computeRotationRow();
|
369
|
}
|
370
|
|
371
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
372
|
|
373
|
void beginNewRotation(int axis)
|
374
|
{
|
375
|
mRotationAxis.set( mParent.ROTATION_AXIS[axis] );
|
376
|
mRotationAngle.add(mParent.mRotationAngleStatic);
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
void addNewRotation(int axis, long durationMillis, int angle)
|
382
|
{
|
383
|
mRotationAxis.set( mParent.ROTATION_AXIS[axis] );
|
384
|
mRotationAngle.setDuration(durationMillis);
|
385
|
mRotationAngle.resetToBeginning();
|
386
|
mRotationAngle.add(new Static1D(0));
|
387
|
mRotationAngle.add(new Static1D(angle));
|
388
|
}
|
389
|
|
390
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
391
|
|
392
|
long setUpCallback(EffectListener listener)
|
393
|
{
|
394
|
mRotateEffect.notifyWhenFinished(listener);
|
395
|
return mRotateEffect.getID();
|
396
|
}
|
397
|
|
398
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
399
|
|
400
|
float getDistSquared(float[] point)
|
401
|
{
|
402
|
float dx = mCurrentPosition.get0() - point[0];
|
403
|
float dy = mCurrentPosition.get1() - point[1];
|
404
|
float dz = mCurrentPosition.get2() - point[2];
|
405
|
|
406
|
return dx*dx + dy*dy + dz*dz;
|
407
|
}
|
408
|
|
409
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
410
|
|
411
|
int getColorIndex(int face)
|
412
|
{
|
413
|
Static4D texMap = mMesh.getTextureMap(face);
|
414
|
return (int)(texMap.get0() / texMap.get2());
|
415
|
}
|
416
|
|
417
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
418
|
|
419
|
MeshBase getMesh()
|
420
|
{
|
421
|
return mMesh;
|
422
|
}
|
423
|
}
|