1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 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.objectlib.main;
|
21
|
|
22
|
import java.io.DataInputStream;
|
23
|
import java.io.IOException;
|
24
|
import java.io.InputStream;
|
25
|
import java.util.Random;
|
26
|
|
27
|
import android.content.SharedPreferences;
|
28
|
import android.graphics.Bitmap;
|
29
|
import android.graphics.Canvas;
|
30
|
import android.graphics.Paint;
|
31
|
|
32
|
import org.distorted.library.effect.Effect;
|
33
|
import org.distorted.library.effect.MatrixEffectMove;
|
34
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
35
|
import org.distorted.library.effect.MatrixEffectScale;
|
36
|
import org.distorted.library.effect.VertexEffectQuaternion;
|
37
|
import org.distorted.library.effect.VertexEffectRotate;
|
38
|
import org.distorted.library.main.DistortedEffects;
|
39
|
import org.distorted.library.main.DistortedLibrary;
|
40
|
import org.distorted.library.main.DistortedNode;
|
41
|
import org.distorted.library.main.DistortedTexture;
|
42
|
import org.distorted.library.main.QuatHelper;
|
43
|
import org.distorted.library.mesh.MeshBase;
|
44
|
import org.distorted.library.mesh.MeshFile;
|
45
|
import org.distorted.library.mesh.MeshJoined;
|
46
|
import org.distorted.library.message.EffectListener;
|
47
|
import org.distorted.library.type.Dynamic1D;
|
48
|
import org.distorted.library.type.Static1D;
|
49
|
import org.distorted.library.type.Static3D;
|
50
|
import org.distorted.library.type.Static4D;
|
51
|
|
52
|
import org.distorted.objectlib.helpers.FactoryCubit;
|
53
|
import org.distorted.objectlib.helpers.FactorySticker;
|
54
|
import org.distorted.objectlib.helpers.ObjectFaceShape;
|
55
|
import org.distorted.objectlib.helpers.ObjectLibInterface;
|
56
|
import org.distorted.objectlib.helpers.ObjectShape;
|
57
|
import org.distorted.objectlib.helpers.ObjectSignature;
|
58
|
import org.distorted.objectlib.helpers.ObjectSticker;
|
59
|
import org.distorted.objectlib.helpers.QuatGroupGenerator;
|
60
|
import org.distorted.objectlib.scrambling.ScrambleState;
|
61
|
import org.distorted.objectlib.scrambling.ObjectScrambler;
|
62
|
import org.distorted.objectlib.json.JsonReader;
|
63
|
import org.distorted.objectlib.touchcontrol.*;
|
64
|
|
65
|
import static org.distorted.objectlib.touchcontrol.TouchControl.*;
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
public abstract class TwistyObject
|
70
|
{
|
71
|
public static final int MESH_NICE = 0;
|
72
|
public static final int MESH_FAST = 1;
|
73
|
|
74
|
public static final int MODE_ICON = 0;
|
75
|
public static final int MODE_NORM = 1;
|
76
|
|
77
|
public static final int COLOR_YELLOW = 0xffffff00;
|
78
|
public static final int COLOR_WHITE = 0xffffffff;
|
79
|
public static final int COLOR_BLUE = 0xff0000ff;
|
80
|
public static final int COLOR_GREEN = 0xff00bb00;
|
81
|
public static final int COLOR_RED = 0xff990000;
|
82
|
public static final int COLOR_ORANGE = 0xffff6200;
|
83
|
public static final int COLOR_GREY = 0xff727c7b;
|
84
|
public static final int COLOR_VIOLET = 0xff7700bb;
|
85
|
public static final int COLOR_STROKE = 0xff000000;
|
86
|
public static final int COLOR_INTERNAL = 0xff000000;
|
87
|
|
88
|
public static final int TEXTURE_HEIGHT = 256;
|
89
|
static final int NUM_STICKERS_IN_ROW = 4;
|
90
|
|
91
|
public static final float SQ2 = (float)Math.sqrt(2);
|
92
|
public static final float SQ3 = (float)Math.sqrt(3);
|
93
|
public static final float SQ5 = (float)Math.sqrt(5);
|
94
|
public static final float SQ6 = (float)Math.sqrt(6);
|
95
|
|
96
|
private static final float MAX_SIZE_CHANGE = 1.35f;
|
97
|
private static final float MIN_SIZE_CHANGE = 0.75f;
|
98
|
|
99
|
private static final Static3D CENTER = new Static3D(0,0,0);
|
100
|
private static final int POST_ROTATION_MILLISEC = 500;
|
101
|
|
102
|
protected float[][] mStickerCoords;
|
103
|
protected Static4D[] mObjectQuats;
|
104
|
int mNumAxis, mMaxNumLayers;
|
105
|
|
106
|
private int[][] mStickerVariants;
|
107
|
private float[] mStickerScales;
|
108
|
private Cubit[] mCubits;
|
109
|
private MeshBase[] mMeshes;
|
110
|
private int mNumCubits, mNumQuats, mNumFaceColors, mNumTextures;
|
111
|
private int mNumCubitFaces, mNumStickerTypes;
|
112
|
private Static3D[] mAxis;
|
113
|
private float[][] mCuts;
|
114
|
private int[] mNumCuts;
|
115
|
private float[][] mOrigPos;
|
116
|
private Static4D[] mOrigQuat;
|
117
|
private Static4D[] mMixupModeQuats;
|
118
|
private boolean mIsInMixupMode;
|
119
|
private Static4D mQuat;
|
120
|
private final int[] mNumLayers;
|
121
|
private final float mSize;
|
122
|
private DistortedEffects mEffects;
|
123
|
private VertexEffectRotate mRotateEffect;
|
124
|
private Dynamic1D mRotationAngle;
|
125
|
private Static3D mRotationAxis;
|
126
|
private Static3D mObjectScale;
|
127
|
private int[] mQuatDebug;
|
128
|
private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
|
129
|
private DistortedTexture mTexture;
|
130
|
private float mInitScreenRatio;
|
131
|
private int mSolvedFunctionIndex;
|
132
|
private boolean mIsBandaged;
|
133
|
private float mObjectScreenRatio;
|
134
|
private int[][] mSolvedQuats;
|
135
|
private int[][] mQuatMult;
|
136
|
private int[] mTmpQuats;
|
137
|
private int mNumTexRows, mNumTexCols;
|
138
|
private int mRotRowBitmap;
|
139
|
private int mCurrentRotAxis;
|
140
|
private MeshBase mMesh;
|
141
|
private ObjectScrambler mScrambler;
|
142
|
private TouchControl mTouchControl;
|
143
|
private DistortedNode mNode;
|
144
|
private ObjectLibInterface mInterface;
|
145
|
private Bitmap mBitmap;
|
146
|
private ObjectSticker[] mStickers;
|
147
|
private ObjectShape[] mShapes;
|
148
|
private int mNumCubitVariants;
|
149
|
private int[][] mCubitFaceColors;
|
150
|
private int[][] mVariantFaceIsOuter;
|
151
|
private int[][] mBasicAngles;
|
152
|
private int mIconMode;
|
153
|
private final InitData mInitData;
|
154
|
private float[][] mRowOffsets;
|
155
|
private boolean[] mBelongs;
|
156
|
private float[] mTmp;
|
157
|
|
158
|
//////////////////// SOLVED1 ////////////////////////
|
159
|
|
160
|
private int[] mFaceMap;
|
161
|
private int[][] mScramble;
|
162
|
private int[] mColors;
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
|
166
|
TwistyObject(InputStream jsonStream, int meshState, int iconMode, Static4D quat, Static3D move, float scale, InputStream meshStream)
|
167
|
{
|
168
|
JsonReader reader = JsonReader.getInstance();
|
169
|
reader.parseJsonFile(jsonStream);
|
170
|
setReader(reader);
|
171
|
|
172
|
mNumLayers = reader.getNumLayers();
|
173
|
mSize = reader.getSize();
|
174
|
mInitData = null;
|
175
|
initialize(meshState,iconMode,quat,move,scale,meshStream,true);
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
TwistyObject(InitData data, int meshState, int iconMode, float size, Static4D quat, Static3D move, float scale, InputStream meshStream)
|
181
|
{
|
182
|
mNumLayers = data.getNumLayers();
|
183
|
mSize = size;
|
184
|
mInitData = data;
|
185
|
initialize(meshState,iconMode,quat,move,scale,meshStream,false);
|
186
|
}
|
187
|
|
188
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189
|
|
190
|
private void initialize(int meshState, int iconMode, Static4D quat, Static3D move, float scale, InputStream stream, boolean fromJSON)
|
191
|
{
|
192
|
mIconMode = iconMode;
|
193
|
mQuat = quat;
|
194
|
mAxis = getRotationAxis();
|
195
|
mInitScreenRatio = getScreenRatio();
|
196
|
mSolvedFunctionIndex = getSolvedFunctionIndex();
|
197
|
mBasicAngles = getBasicAngles();
|
198
|
mObjectQuats = getQuats();
|
199
|
mNumQuats = mObjectQuats.length;
|
200
|
mOrigPos = getCubitPositions(mNumLayers);
|
201
|
mRowOffsets = new float[getNumFaces()][3];
|
202
|
mTmp = new float[4];
|
203
|
|
204
|
int numAxis = mAxis.length;
|
205
|
mMaxNumLayers = -1;
|
206
|
mCuts = getCuts(mNumLayers);
|
207
|
mNumCuts = new int[numAxis];
|
208
|
for(int i=0; i<numAxis; i++)
|
209
|
{
|
210
|
if( mMaxNumLayers<mNumLayers[i] ) mMaxNumLayers = mNumLayers[i];
|
211
|
mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
|
212
|
}
|
213
|
|
214
|
mNumCubits = mOrigPos.length;
|
215
|
mNumFaceColors = getNumFaceColors();
|
216
|
mNumAxis = mAxis.length;
|
217
|
mBelongs = new boolean[mNumCubits];
|
218
|
|
219
|
int scramblingType = getScrambleType();
|
220
|
ScrambleState[] states = getScrambleStates();
|
221
|
mScrambler = new ObjectScrambler(scramblingType, mNumAxis,mNumLayers,states);
|
222
|
|
223
|
boolean bandaged=false;
|
224
|
|
225
|
for( int c=0; c<mNumCubits; c++)
|
226
|
{
|
227
|
if( mOrigPos[c].length>3 )
|
228
|
{
|
229
|
bandaged=true;
|
230
|
break;
|
231
|
}
|
232
|
}
|
233
|
mIsBandaged = bandaged;
|
234
|
mQuatDebug = new int[mNumCubits];
|
235
|
|
236
|
mRotationAngle= new Dynamic1D();
|
237
|
mRotationAxis = new Static3D(1,0,0);
|
238
|
mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
|
239
|
|
240
|
mRotationAngleStatic = new Static1D(0);
|
241
|
mRotationAngleMiddle = new Static1D(0);
|
242
|
mRotationAngleFinal = new Static1D(0);
|
243
|
|
244
|
mObjectScale = new Static3D(scale,scale,scale);
|
245
|
setObjectRatioNow(scale,720);
|
246
|
|
247
|
mEffects = new DistortedEffects();
|
248
|
createQuaternionEffects();
|
249
|
|
250
|
MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
|
251
|
MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
|
252
|
MatrixEffectMove moveEffect = new MatrixEffectMove(move);
|
253
|
|
254
|
boolean fromDMESH = (stream!=null && meshState==MESH_NICE);
|
255
|
getQuatsAndShapes(fromDMESH,fromJSON);
|
256
|
createMeshAndCubits(stream,meshState,fromDMESH);
|
257
|
setUpTextures(fromDMESH,fromJSON);
|
258
|
createDataStructuresForSolved();
|
259
|
|
260
|
mEffects.apply(mRotateEffect);
|
261
|
mEffects.apply(quatEffect);
|
262
|
mEffects.apply(scaleEffect);
|
263
|
mEffects.apply(moveEffect);
|
264
|
|
265
|
mNode = new DistortedNode(mTexture,mEffects,mMesh);
|
266
|
}
|
267
|
|
268
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
269
|
|
270
|
private void createQuaternionEffects()
|
271
|
{
|
272
|
if( mNumQuats<=ObjectControl.MAX_QUATS )
|
273
|
{
|
274
|
mIsInMixupMode = false;
|
275
|
|
276
|
for( int q=0; q<mNumQuats; q++)
|
277
|
{
|
278
|
VertexEffectQuaternion vq = new VertexEffectQuaternion(mObjectQuats[q],CENTER);
|
279
|
vq.setMeshAssociation(0,q);
|
280
|
mEffects.apply(vq);
|
281
|
}
|
282
|
}
|
283
|
else if( mNumCubits<=ObjectControl.MAX_QUATS )
|
284
|
{
|
285
|
mIsInMixupMode = true;
|
286
|
mMixupModeQuats = new Static4D[mNumCubits];
|
287
|
|
288
|
for( int q=0; q<mNumCubits; q++)
|
289
|
{
|
290
|
mMixupModeQuats[q] = new Static4D(mObjectQuats[0]);
|
291
|
VertexEffectQuaternion vq = new VertexEffectQuaternion(mMixupModeQuats[q],CENTER);
|
292
|
vq.setMeshAssociation(0,q);
|
293
|
mEffects.apply(vq);
|
294
|
}
|
295
|
}
|
296
|
else
|
297
|
{
|
298
|
android.util.Log.e("D", "object has too many quaternions ("+mNumQuats+") or too many cubits ("+mNumCubits+")");
|
299
|
}
|
300
|
}
|
301
|
|
302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
303
|
|
304
|
private Static3D getPos(float[] origPos)
|
305
|
{
|
306
|
int len = origPos.length/3;
|
307
|
float sumX = 0.0f;
|
308
|
float sumY = 0.0f;
|
309
|
float sumZ = 0.0f;
|
310
|
|
311
|
for(int i=0; i<len; i++)
|
312
|
{
|
313
|
sumX += origPos[3*i ];
|
314
|
sumY += origPos[3*i+1];
|
315
|
sumZ += origPos[3*i+2];
|
316
|
}
|
317
|
|
318
|
sumX /= len;
|
319
|
sumY /= len;
|
320
|
sumZ /= len;
|
321
|
|
322
|
return new Static3D(sumX,sumY,sumZ);
|
323
|
}
|
324
|
|
325
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
326
|
|
327
|
private void createOuterFaces()
|
328
|
{
|
329
|
for(int v=0; v<mNumCubitVariants; v++)
|
330
|
{
|
331
|
int[][] indices = mShapes[v].getVertIndices();
|
332
|
int faces = indices.length;
|
333
|
mVariantFaceIsOuter[v] = new int[faces];
|
334
|
}
|
335
|
|
336
|
for( int cubit=0; cubit<mNumCubits; cubit++)
|
337
|
{
|
338
|
int variant = getCubitVariant(cubit,mNumLayers);
|
339
|
int[][] indices = mShapes[variant].getVertIndices();
|
340
|
int numFaces = indices.length;
|
341
|
|
342
|
for(int face=0; face<numFaces; face++)
|
343
|
if( getCubitFaceColor(cubit,face)>=0 )
|
344
|
{
|
345
|
mVariantFaceIsOuter[variant][face] = 1;
|
346
|
}
|
347
|
}
|
348
|
}
|
349
|
|
350
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
351
|
|
352
|
private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
|
353
|
{
|
354
|
mNumCubitVariants = getNumCubitVariants(mNumLayers);
|
355
|
|
356
|
if( !fromDMESH || !fromJSON )
|
357
|
{
|
358
|
FactoryCubit factory = FactoryCubit.getInstance();
|
359
|
factory.clear();
|
360
|
|
361
|
mOrigQuat = new Static4D[mNumCubits];
|
362
|
for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
|
363
|
|
364
|
mShapes = new ObjectShape[mNumCubitVariants];
|
365
|
for(int i=0; i<mNumCubitVariants; i++) mShapes[i] = getObjectShape(i);
|
366
|
mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
|
367
|
mVariantFaceIsOuter = new int[mNumCubitVariants][];
|
368
|
|
369
|
if( !fromJSON )
|
370
|
{
|
371
|
mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
|
372
|
createOuterFaces();
|
373
|
}
|
374
|
|
375
|
if( fromDMESH )
|
376
|
{
|
377
|
for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i], mVariantFaceIsOuter[i]);
|
378
|
}
|
379
|
}
|
380
|
}
|
381
|
|
382
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
383
|
|
384
|
private void createMeshAndCubits(InputStream stream, int meshState, boolean fromDMESH)
|
385
|
{
|
386
|
mCubits = new Cubit[mNumCubits];
|
387
|
|
388
|
if( fromDMESH )
|
389
|
{
|
390
|
DataInputStream dos = new DataInputStream(stream);
|
391
|
mMesh = new MeshFile(dos);
|
392
|
|
393
|
try
|
394
|
{
|
395
|
stream.close();
|
396
|
}
|
397
|
catch(IOException e)
|
398
|
{
|
399
|
android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
|
400
|
}
|
401
|
}
|
402
|
else
|
403
|
{
|
404
|
MeshBase[] cubitMesh = new MeshBase[mNumCubits];
|
405
|
|
406
|
for(int i=0; i<mNumCubits; i++)
|
407
|
{
|
408
|
cubitMesh[i] = createCubitMesh(i,mNumLayers,meshState,mNumCubitFaces);
|
409
|
Static3D pos = getPos(mOrigPos[i]);
|
410
|
cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
|
411
|
}
|
412
|
|
413
|
mMesh = new MeshJoined(cubitMesh);
|
414
|
}
|
415
|
|
416
|
for(int i=0; i<mNumCubits; i++)
|
417
|
{
|
418
|
mCubits[i] = new Cubit(this,mOrigPos[i],mNumAxis,i);
|
419
|
setCubitQuat(i,mCubits[i].computeAssociation(),0);
|
420
|
}
|
421
|
}
|
422
|
|
423
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
424
|
|
425
|
private MeshBase createCubitMesh(int cubit, int[] numLayers, int meshState, int numComponents)
|
426
|
{
|
427
|
int variant = getCubitVariant(cubit,numLayers);
|
428
|
|
429
|
if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
|
430
|
|
431
|
if( mMeshes[variant]==null )
|
432
|
{
|
433
|
ObjectFaceShape faceShape = getObjectFaceShape(variant);
|
434
|
FactoryCubit factory = FactoryCubit.getInstance();
|
435
|
factory.createNewFaceTransform(mShapes[variant],mVariantFaceIsOuter[variant]);
|
436
|
mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,meshState, numComponents);
|
437
|
}
|
438
|
|
439
|
MeshBase mesh = mMeshes[variant].copy(true);
|
440
|
MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
|
441
|
mesh.apply(quat,0xffffffff,0);
|
442
|
|
443
|
return mesh;
|
444
|
}
|
445
|
|
446
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
447
|
|
448
|
private void setUpTextures(boolean fromDMESH, boolean fromJSON)
|
449
|
{
|
450
|
mTexture = new DistortedTexture();
|
451
|
|
452
|
if( fromJSON )
|
453
|
{
|
454
|
mNumStickerTypes = getNumStickerTypes();
|
455
|
mNumCubitFaces = getNumCubitFaces();
|
456
|
}
|
457
|
else
|
458
|
{
|
459
|
FactoryCubit factory = FactoryCubit.getInstance();
|
460
|
mStickerCoords = factory.getStickerCoords();
|
461
|
mStickerVariants = factory.getStickerVariants();
|
462
|
mStickerScales = factory.getStickerScales();
|
463
|
adjustStickerCoords();
|
464
|
mNumStickerTypes = (mStickerCoords==null ? 0 : mStickerCoords.length);
|
465
|
}
|
466
|
|
467
|
mNumTextures= mNumFaceColors *mNumStickerTypes;
|
468
|
mNumTexCols = NUM_STICKERS_IN_ROW;
|
469
|
mNumTexRows = (mNumTextures+1)/NUM_STICKERS_IN_ROW;
|
470
|
if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
|
471
|
|
472
|
if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
|
473
|
setTexture();
|
474
|
}
|
475
|
|
476
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
477
|
|
478
|
private int getMultQuat(int index1, int index2)
|
479
|
{
|
480
|
if( mQuatMult==null )
|
481
|
{
|
482
|
mQuatMult = new int[mNumQuats][mNumQuats];
|
483
|
|
484
|
for(int i=0; i<mNumQuats; i++)
|
485
|
for(int j=0; j<mNumQuats; j++) mQuatMult[i][j] = -1;
|
486
|
}
|
487
|
|
488
|
if( mQuatMult[index1][index2]==-1 )
|
489
|
{
|
490
|
mQuatMult[index1][index2] = mulQuat(index1,index2);
|
491
|
}
|
492
|
|
493
|
return mQuatMult[index1][index2];
|
494
|
}
|
495
|
|
496
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
497
|
|
498
|
public InitData getInitData()
|
499
|
{
|
500
|
return mInitData;
|
501
|
}
|
502
|
|
503
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
504
|
|
505
|
public boolean isInIconMode()
|
506
|
{
|
507
|
return mIconMode==MODE_ICON;
|
508
|
}
|
509
|
|
510
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
511
|
|
512
|
public int getVariantFaceColor(int variant, int face)
|
513
|
{
|
514
|
return face>=mStickerVariants[variant].length ? -1 : mStickerVariants[variant][face];
|
515
|
}
|
516
|
|
517
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
518
|
|
519
|
public boolean shouldResetTextureMaps()
|
520
|
{
|
521
|
return false;
|
522
|
}
|
523
|
|
524
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
525
|
|
526
|
private void createDataStructuresForSolved()
|
527
|
{
|
528
|
mTmpQuats = new int[mNumQuats];
|
529
|
mSolvedQuats = getSolvedQuats();
|
530
|
}
|
531
|
|
532
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
533
|
// This is used to build internal data structures for the generic 'isSolved()'
|
534
|
//
|
535
|
// if this is an internal cubit (all faces black): return -1
|
536
|
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
|
537
|
// Color index, i.e. the index into the 'FACE_COLORS' table.
|
538
|
// else (edge or corner cubit, more than one non-black face): return -2.
|
539
|
|
540
|
protected int retCubitSolvedStatus(int cubit)
|
541
|
{
|
542
|
int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
|
543
|
int variant = getCubitVariant(cubit,mNumLayers);
|
544
|
|
545
|
for(int face=0; face<mNumCubitFaces; face++)
|
546
|
{
|
547
|
varColor = getVariantFaceColor(variant,face);
|
548
|
int numFaces = mCubitFaceColors[cubit].length;
|
549
|
cubColor = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
|
550
|
|
551
|
if( varColor>=0 && cubColor>=0 )
|
552
|
{
|
553
|
numNonBlack++;
|
554
|
nonBlackIndex = cubColor;
|
555
|
}
|
556
|
}
|
557
|
|
558
|
if( numNonBlack==0 ) return -1;
|
559
|
if( numNonBlack>=2 ) return -2;
|
560
|
|
561
|
return nonBlackIndex;
|
562
|
}
|
563
|
|
564
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
565
|
|
566
|
private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
|
567
|
{
|
568
|
final float MAXERR = 0.05f;
|
569
|
int numAxis = dist.length;
|
570
|
|
571
|
for(int i=0; i<numAxis; i++)
|
572
|
{
|
573
|
Static3D ax = faceAxis[i];
|
574
|
float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
|
575
|
if( len>mSize*dist[i]+MAXERR ) return true;
|
576
|
}
|
577
|
|
578
|
return false;
|
579
|
}
|
580
|
|
581
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
582
|
|
583
|
private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
|
584
|
{
|
585
|
float[][] vertices = mShapes[variant].getVertices();
|
586
|
Static3D[] axis = getFaceAxis();
|
587
|
float[] dist3D = getDist3D(mNumLayers);
|
588
|
|
589
|
for( float[] vertex : vertices)
|
590
|
{
|
591
|
float x = vertex[0];
|
592
|
float y = vertex[1];
|
593
|
float z = vertex[2];
|
594
|
|
595
|
QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
|
596
|
|
597
|
float mx = tmp[0] + px;
|
598
|
float my = tmp[1] + py;
|
599
|
float mz = tmp[2] + pz;
|
600
|
|
601
|
if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
|
602
|
}
|
603
|
|
604
|
return true;
|
605
|
}
|
606
|
|
607
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
608
|
|
609
|
private float computeAvg(float[] pos, int offset)
|
610
|
{
|
611
|
int len = pos.length/3;
|
612
|
float ret=0.0f;
|
613
|
for(int i=0; i<len; i++) ret += pos[3*i+offset];
|
614
|
ret /= len;
|
615
|
|
616
|
return ret;
|
617
|
}
|
618
|
|
619
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
620
|
|
621
|
protected void displayCubitQuats()
|
622
|
{
|
623
|
StringBuilder builder = new StringBuilder();
|
624
|
float[] tmp = new float[4];
|
625
|
float ERR = 0.01f;
|
626
|
|
627
|
for(int cubit=0; cubit<mNumCubits; cubit++)
|
628
|
{
|
629
|
builder.append(cubit);
|
630
|
builder.append(" : ");
|
631
|
|
632
|
int refCubit,variant = getCubitVariant(cubit,mNumLayers);
|
633
|
|
634
|
for(refCubit=0; refCubit<mNumCubits; refCubit++)
|
635
|
{
|
636
|
if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
|
637
|
}
|
638
|
|
639
|
float[] curpos = mOrigPos[cubit];
|
640
|
float[] refpos = mOrigPos[refCubit];
|
641
|
float refX = computeAvg(refpos,0);
|
642
|
float refY = computeAvg(refpos,1);
|
643
|
float refZ = computeAvg(refpos,2);
|
644
|
float curX = computeAvg(curpos,0);
|
645
|
float curY = computeAvg(curpos,1);
|
646
|
float curZ = computeAvg(curpos,2);
|
647
|
|
648
|
for(int quat=0; quat<mNumQuats; quat++)
|
649
|
{
|
650
|
QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
|
651
|
|
652
|
float dx = tmp[0]-curX;
|
653
|
float dy = tmp[1]-curY;
|
654
|
float dz = tmp[2]-curZ;
|
655
|
|
656
|
if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
|
657
|
{
|
658
|
if( doesNotStickOut(variant,curX,curY,curZ,tmp,mObjectQuats[quat]) )
|
659
|
{
|
660
|
builder.append(quat);
|
661
|
builder.append(',');
|
662
|
}
|
663
|
else
|
664
|
{
|
665
|
android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
|
666
|
}
|
667
|
}
|
668
|
}
|
669
|
|
670
|
builder.append('\n');
|
671
|
}
|
672
|
|
673
|
android.util.Log.e("D", "cubitQuats: \n"+builder.toString() );
|
674
|
}
|
675
|
|
676
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
677
|
|
678
|
protected int[] buildSolvedQuats(Static3D faceAx)
|
679
|
{
|
680
|
final float MAXD = 0.0001f;
|
681
|
float x = faceAx.get0();
|
682
|
float y = faceAx.get1();
|
683
|
float z = faceAx.get2();
|
684
|
float a,dx,dy,dz,qx,qy,qz;
|
685
|
Static4D quat;
|
686
|
int place = 0;
|
687
|
|
688
|
for(int q=1; q<mNumQuats; q++)
|
689
|
{
|
690
|
quat = mObjectQuats[q];
|
691
|
qx = quat.get0();
|
692
|
qy = quat.get1();
|
693
|
qz = quat.get2();
|
694
|
|
695
|
if( x!=0.0f ) { a = qx/x; }
|
696
|
else if( y!=0.0f ) { a = qy/y; }
|
697
|
else { a = qz/z; }
|
698
|
|
699
|
dx = a*x-qx;
|
700
|
dy = a*y-qy;
|
701
|
dz = a*z-qz;
|
702
|
|
703
|
if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
|
704
|
{
|
705
|
mTmpQuats[place++] = q;
|
706
|
}
|
707
|
}
|
708
|
|
709
|
if( place!=0 )
|
710
|
{
|
711
|
int[] ret = new int[place];
|
712
|
System.arraycopy(mTmpQuats,0,ret,0,place);
|
713
|
return ret;
|
714
|
}
|
715
|
|
716
|
return null;
|
717
|
}
|
718
|
|
719
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
720
|
|
721
|
public int getCubitRotationType(int cubit)
|
722
|
{
|
723
|
return Cubit.TYPE_NORMAL;
|
724
|
}
|
725
|
|
726
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
727
|
|
728
|
float[] getTrackingPoint(int cubitIndex, int cubitType)
|
729
|
{
|
730
|
if( cubitType!=Cubit.TYPE_NORMAL )
|
731
|
{
|
732
|
int variant = getCubitVariant(cubitIndex,mNumLayers);
|
733
|
int[][] indices = mShapes[variant].getVertIndices();
|
734
|
int faces = indices.length;
|
735
|
int outer=-1;
|
736
|
|
737
|
for(int i=0; i<faces; i++)
|
738
|
{
|
739
|
if( mVariantFaceIsOuter[variant][i]==1 )
|
740
|
{
|
741
|
outer=i;
|
742
|
break;
|
743
|
}
|
744
|
}
|
745
|
|
746
|
if( outer>=0 )
|
747
|
{
|
748
|
int vertIndex = indices[outer][0];
|
749
|
float[] vertices = mShapes[variant].getVertices()[vertIndex];
|
750
|
float[] ret = new float[3];
|
751
|
float[] curpos = mOrigPos[cubitIndex];
|
752
|
Static4D quat = mOrigQuat[cubitIndex];
|
753
|
QuatHelper.rotateVectorByQuat(mTmp, vertices[0], vertices[1], vertices[2], 1, quat);
|
754
|
|
755
|
ret[0] = mTmp[0]+computeAvg(curpos,0);
|
756
|
ret[1] = mTmp[1]+computeAvg(curpos,1);
|
757
|
ret[2] = mTmp[2]+computeAvg(curpos,2);
|
758
|
|
759
|
return ret;
|
760
|
}
|
761
|
else
|
762
|
{
|
763
|
android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
|
764
|
}
|
765
|
}
|
766
|
|
767
|
return null;
|
768
|
}
|
769
|
|
770
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
771
|
|
772
|
public int computeCurrentPuzzleFace(int type, float[] vertex)
|
773
|
{
|
774
|
if( type!=Cubit.TYPE_NORMAL )
|
775
|
{
|
776
|
Static3D[] axis = getFaceAxis();
|
777
|
float[] dist3D = getDist3D(mNumLayers);
|
778
|
final float MAXERR = 0.98f;
|
779
|
int numAxis = axis.length;
|
780
|
float x = vertex[0];
|
781
|
float y = vertex[1];
|
782
|
float z = vertex[2];
|
783
|
|
784
|
for(int i=0; i<numAxis; i++)
|
785
|
{
|
786
|
Static3D ax = axis[i];
|
787
|
float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
|
788
|
if( len>mSize*dist3D[i]*MAXERR ) return i;
|
789
|
}
|
790
|
}
|
791
|
|
792
|
return -1;
|
793
|
}
|
794
|
|
795
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
796
|
|
797
|
public float[] getCubitRowOffset(int cubitIndex)
|
798
|
{
|
799
|
return null;
|
800
|
}
|
801
|
|
802
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
803
|
|
804
|
void setRotationRowOffset(int puzzleFace, float[] offset)
|
805
|
{
|
806
|
mRowOffsets[puzzleFace][0] = offset[0];
|
807
|
mRowOffsets[puzzleFace][1] = offset[1];
|
808
|
mRowOffsets[puzzleFace][2] = offset[2];
|
809
|
}
|
810
|
|
811
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
812
|
|
813
|
public int[][] getSolvedQuats()
|
814
|
{
|
815
|
int[] groups = new int[mNumCubits];
|
816
|
int numGroups = 1;
|
817
|
int numFirst = 0;
|
818
|
|
819
|
for(int cubit=0; cubit<mNumCubits; cubit++)
|
820
|
{
|
821
|
groups[cubit] = retCubitSolvedStatus(cubit);
|
822
|
if( groups[cubit]>=0 ) numGroups++;
|
823
|
else numFirst++;
|
824
|
}
|
825
|
|
826
|
int firstIndex = 1;
|
827
|
int groupIndex = 1;
|
828
|
int[][] solvedQuats = new int[numGroups][];
|
829
|
solvedQuats[0] = new int[1+numFirst];
|
830
|
solvedQuats[0][0] = numFirst;
|
831
|
Static3D[] axis = getFaceAxis();
|
832
|
|
833
|
for(int cubit=0; cubit<mNumCubits; cubit++)
|
834
|
{
|
835
|
int group = groups[cubit];
|
836
|
|
837
|
if( group<0 )
|
838
|
{
|
839
|
solvedQuats[0][firstIndex] = cubit;
|
840
|
firstIndex++;
|
841
|
}
|
842
|
else
|
843
|
{
|
844
|
int[] quats = buildSolvedQuats(axis[group]);
|
845
|
int len = quats==null ? 0 : quats.length;
|
846
|
solvedQuats[groupIndex] = new int[2+len];
|
847
|
solvedQuats[groupIndex][0] = 1;
|
848
|
solvedQuats[groupIndex][1] = cubit;
|
849
|
for(int i=0; i<len; i++) solvedQuats[groupIndex][i+2] = quats[i];
|
850
|
groupIndex++;
|
851
|
}
|
852
|
}
|
853
|
/*
|
854
|
String dbg = "SOLVED GROUPS:\n";
|
855
|
|
856
|
for(int g=0; g<numGroups; g++)
|
857
|
{
|
858
|
int len = solvedQuats[g].length;
|
859
|
for(int i=0; i<len; i++) dbg += (" "+solvedQuats[g][i]);
|
860
|
dbg+="\n";
|
861
|
}
|
862
|
|
863
|
android.util.Log.e("D", dbg);
|
864
|
*/
|
865
|
return solvedQuats;
|
866
|
}
|
867
|
|
868
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
869
|
|
870
|
public int getSolvedFunctionIndex()
|
871
|
{
|
872
|
return 0;
|
873
|
}
|
874
|
|
875
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
876
|
// special SolvedQuats for the case where there are no corner of edge cubits.
|
877
|
// first row {0} - means there are no corners or edges.
|
878
|
// each next defines all cubits of a singe face (numCubits, firstCubit, cubit1,..,cubitN-1, quat0,..., quatM
|
879
|
|
880
|
private boolean isSolvedCentersOnly()
|
881
|
{
|
882
|
int numGroups = mSolvedQuats.length;
|
883
|
|
884
|
for(int group=1; group<numGroups; group++)
|
885
|
{
|
886
|
int numEntries= mSolvedQuats[group].length;
|
887
|
int numCubits = mSolvedQuats[group][0];
|
888
|
int firstCubit= mSolvedQuats[group][1];
|
889
|
int firstQuat = mCubits[firstCubit].mQuatIndex;
|
890
|
|
891
|
for(int cubit=2; cubit<=numCubits; cubit++)
|
892
|
{
|
893
|
int currCubit= mSolvedQuats[group][cubit];
|
894
|
int currQuat = mCubits[currCubit].mQuatIndex;
|
895
|
boolean isGood= (firstQuat==currQuat);
|
896
|
|
897
|
for(int q=numCubits+1; !isGood && q<numEntries; q++)
|
898
|
{
|
899
|
int quat = mSolvedQuats[group][q];
|
900
|
if( firstQuat == getMultQuat(currQuat,quat) ) isGood = true;
|
901
|
}
|
902
|
|
903
|
if( !isGood ) return false;
|
904
|
}
|
905
|
}
|
906
|
|
907
|
return true;
|
908
|
}
|
909
|
|
910
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
911
|
|
912
|
private boolean isSolved0()
|
913
|
{
|
914
|
if( mSolvedQuats[0][0]==0 ) return isSolvedCentersOnly();
|
915
|
|
916
|
for( int[] solvedQuat : mSolvedQuats )
|
917
|
{
|
918
|
int numCubits = solvedQuat[0];
|
919
|
int firstCubit= solvedQuat[1];
|
920
|
int quat = mCubits[firstCubit].mQuatIndex;
|
921
|
|
922
|
for( int cubit=2; cubit<=numCubits; cubit++ )
|
923
|
{
|
924
|
int c = solvedQuat[cubit];
|
925
|
if( quat != mCubits[c].mQuatIndex ) return false;
|
926
|
}
|
927
|
}
|
928
|
|
929
|
int cubit= mSolvedQuats[0][1];
|
930
|
int quat0= mCubits[cubit].mQuatIndex;
|
931
|
int numGroups = mSolvedQuats.length;
|
932
|
|
933
|
for(int group=1; group<numGroups; group++)
|
934
|
{
|
935
|
int firstCubit= mSolvedQuats[group][1];
|
936
|
int currQuat = mCubits[firstCubit].mQuatIndex;
|
937
|
|
938
|
if( quat0==currQuat ) continue;
|
939
|
|
940
|
boolean isGood= false;
|
941
|
int numEntries= mSolvedQuats[group].length;
|
942
|
int numCubits = mSolvedQuats[group][0];
|
943
|
|
944
|
for(int q=numCubits+1; q<numEntries; q++)
|
945
|
{
|
946
|
int quat = mSolvedQuats[group][q];
|
947
|
|
948
|
if( quat0 == getMultQuat(currQuat,quat) )
|
949
|
{
|
950
|
isGood = true;
|
951
|
break;
|
952
|
}
|
953
|
}
|
954
|
|
955
|
if( !isGood ) return false;
|
956
|
}
|
957
|
|
958
|
return true;
|
959
|
}
|
960
|
|
961
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
962
|
|
963
|
private int computeScramble(int quatNum, int centerNum)
|
964
|
{
|
965
|
float MAXDIFF = 0.01f;
|
966
|
float[] center= mOrigPos[centerNum];
|
967
|
Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
|
968
|
Static4D result = QuatHelper.rotateVectorByQuat(sc,mObjectQuats[quatNum]);
|
969
|
|
970
|
float x = result.get0();
|
971
|
float y = result.get1();
|
972
|
float z = result.get2();
|
973
|
|
974
|
for(int c=0; c<mNumCubits; c++)
|
975
|
{
|
976
|
float[] cent = mOrigPos[c];
|
977
|
|
978
|
float qx = cent[0] - x;
|
979
|
float qy = cent[1] - y;
|
980
|
float qz = cent[2] - z;
|
981
|
|
982
|
if( qx>-MAXDIFF && qx<MAXDIFF &&
|
983
|
qy>-MAXDIFF && qy<MAXDIFF &&
|
984
|
qz>-MAXDIFF && qz<MAXDIFF ) return c;
|
985
|
}
|
986
|
|
987
|
return -1;
|
988
|
}
|
989
|
|
990
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
991
|
// Dino4 uses this. It is solved if and only if groups of cubits
|
992
|
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
|
993
|
// or
|
994
|
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
|
995
|
// are all the same color.
|
996
|
|
997
|
private boolean isSolved1()
|
998
|
{
|
999
|
if( mScramble==null )
|
1000
|
{
|
1001
|
mScramble = new int[mNumQuats][mNumCubits];
|
1002
|
mColors = new int[mNumCubits];
|
1003
|
|
1004
|
for(int q=0; q<mNumQuats; q++)
|
1005
|
for(int c=0; c<mNumCubits; c++) mScramble[q][c] = computeScramble(q,c);
|
1006
|
}
|
1007
|
|
1008
|
if( mFaceMap==null )
|
1009
|
{
|
1010
|
mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
|
1011
|
}
|
1012
|
|
1013
|
for(int c=0; c<mNumCubits; c++)
|
1014
|
{
|
1015
|
int index = mScramble[mCubits[c].mQuatIndex][c];
|
1016
|
mColors[index] = mFaceMap[c];
|
1017
|
}
|
1018
|
|
1019
|
if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
|
1020
|
mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
|
1021
|
mColors[4]==mColors[8] && mColors[4]==mColors[9] ) return true;
|
1022
|
|
1023
|
if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
|
1024
|
mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
|
1025
|
mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
|
1026
|
|
1027
|
return false;
|
1028
|
}
|
1029
|
|
1030
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1031
|
|
1032
|
int computeRow(float[] pos, int axisIndex, int cubitType, int puzzleFace)
|
1033
|
{
|
1034
|
int ret=0;
|
1035
|
int len = pos.length / 3;
|
1036
|
Static3D axis = mAxis[axisIndex];
|
1037
|
float axisX = axis.get0();
|
1038
|
float axisY = axis.get1();
|
1039
|
float axisZ = axis.get2();
|
1040
|
float casted, xoff=0, yoff=0, zoff=0;
|
1041
|
|
1042
|
if( cubitType!=Cubit.TYPE_NORMAL )
|
1043
|
{
|
1044
|
xoff = mRowOffsets[puzzleFace][0];
|
1045
|
yoff = mRowOffsets[puzzleFace][1];
|
1046
|
zoff = mRowOffsets[puzzleFace][2];
|
1047
|
}
|
1048
|
|
1049
|
for(int i=0; i<len; i++)
|
1050
|
{
|
1051
|
casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
|
1052
|
ret |= computeSingleRow(axisIndex,casted);
|
1053
|
}
|
1054
|
|
1055
|
return ret;
|
1056
|
}
|
1057
|
|
1058
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1059
|
|
1060
|
private int computeSingleRow(int axisIndex,float casted)
|
1061
|
{
|
1062
|
int num = mNumCuts[axisIndex];
|
1063
|
|
1064
|
for(int i=0; i<num; i++)
|
1065
|
{
|
1066
|
if( casted<mCuts[axisIndex][i] ) return (1<<i);
|
1067
|
}
|
1068
|
|
1069
|
return (1<<num);
|
1070
|
}
|
1071
|
|
1072
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1073
|
|
1074
|
private boolean wasRotateApplied()
|
1075
|
{
|
1076
|
return mEffects.exists(mRotateEffect.getID());
|
1077
|
}
|
1078
|
|
1079
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1080
|
|
1081
|
private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
|
1082
|
{
|
1083
|
return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
|
1084
|
}
|
1085
|
|
1086
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1087
|
// note the minus in front of the sin() - we rotate counterclockwise
|
1088
|
// when looking towards the direction where the axis increases in values.
|
1089
|
|
1090
|
private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
|
1091
|
{
|
1092
|
Static3D axis = mAxis[axisIndex];
|
1093
|
|
1094
|
while( angleInDegrees<0 ) angleInDegrees += 360;
|
1095
|
angleInDegrees %= 360;
|
1096
|
|
1097
|
float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
|
1098
|
float sinA =-(float)Math.sqrt(1-cosA*cosA);
|
1099
|
|
1100
|
return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
|
1101
|
}
|
1102
|
|
1103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1104
|
|
1105
|
private synchronized void setupPosition(int[][] moves)
|
1106
|
{
|
1107
|
if( moves!=null )
|
1108
|
{
|
1109
|
Static4D quat;
|
1110
|
int index, axis, row, rowBitmap, angle;
|
1111
|
|
1112
|
for(int[] move: moves)
|
1113
|
{
|
1114
|
axis = move[0];
|
1115
|
rowBitmap= computeBitmapFromRow( move[1],axis );
|
1116
|
row = computeRowFromBitmap( move[1] );
|
1117
|
angle = move[2]*(360/mBasicAngles[axis][row]); // this assumes that all layers from
|
1118
|
// the bitmap have the same BasicAngle.
|
1119
|
// at the moment this is always true as
|
1120
|
// there are no bandaged objects with
|
1121
|
// different per-layer BasicAngles.
|
1122
|
quat = makeQuaternion(axis,angle);
|
1123
|
|
1124
|
for(int j=0; j<mNumCubits; j++)
|
1125
|
{
|
1126
|
mBelongs[j] = belongsToRotation(j,axis,rowBitmap);
|
1127
|
if( mBelongs[j] ) mCubits[j].rotateCubit(quat);
|
1128
|
}
|
1129
|
|
1130
|
for(int j=0; j<mNumCubits; j++)
|
1131
|
{
|
1132
|
if( mBelongs[j] )
|
1133
|
{
|
1134
|
index = mCubits[j].postRotateCubit(quat);
|
1135
|
setCubitQuat(j,mCubits[j].computeAssociation(),index);
|
1136
|
}
|
1137
|
else if( mCubits[j].getCubitType()==Cubit.TYPE_FOLLOWER )
|
1138
|
{
|
1139
|
mCubits[j].adjustRotRowPostRotateADecider();
|
1140
|
setCubitQuat(j,mCubits[j].computeAssociation(),mCubits[j].mQuatIndex);
|
1141
|
}
|
1142
|
}
|
1143
|
}
|
1144
|
}
|
1145
|
}
|
1146
|
|
1147
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1148
|
|
1149
|
public int getScrambleType()
|
1150
|
{
|
1151
|
return 0;
|
1152
|
}
|
1153
|
|
1154
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1155
|
|
1156
|
int computeBitmapFromRow(int rowBitmap, int axis)
|
1157
|
{
|
1158
|
if( mIsBandaged )
|
1159
|
{
|
1160
|
int bitmap, initBitmap=0;
|
1161
|
|
1162
|
while( initBitmap!=rowBitmap )
|
1163
|
{
|
1164
|
initBitmap = rowBitmap;
|
1165
|
|
1166
|
for(int cubit=0; cubit<mNumCubits; cubit++)
|
1167
|
{
|
1168
|
bitmap = mCubits[cubit].getRotRow(axis);
|
1169
|
if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
|
1170
|
}
|
1171
|
}
|
1172
|
}
|
1173
|
|
1174
|
return rowBitmap;
|
1175
|
}
|
1176
|
|
1177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1178
|
|
1179
|
private int computeRowFromBitmap(int rowBitmap)
|
1180
|
{
|
1181
|
int index = 0;
|
1182
|
|
1183
|
while(index<32)
|
1184
|
{
|
1185
|
if( (rowBitmap&0x1) != 0 ) return index;
|
1186
|
rowBitmap>>=1;
|
1187
|
index++;
|
1188
|
}
|
1189
|
return 0;
|
1190
|
}
|
1191
|
|
1192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1193
|
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
|
1194
|
// Do so only if minimal Error is appropriately low (shape-shifting puzzles - Square-1)
|
1195
|
|
1196
|
void clampPos(float[] pos, int offset)
|
1197
|
{
|
1198
|
float currError, minError = Float.MAX_VALUE;
|
1199
|
int minErrorIndex1 = -1;
|
1200
|
int minErrorIndex2 = -1;
|
1201
|
|
1202
|
float x = pos[offset ];
|
1203
|
float y = pos[offset+1];
|
1204
|
float z = pos[offset+2];
|
1205
|
|
1206
|
float xo,yo,zo;
|
1207
|
|
1208
|
for(int i=0; i<mNumCubits; i++)
|
1209
|
{
|
1210
|
int len = mOrigPos[i].length / 3;
|
1211
|
|
1212
|
for(int j=0; j<len; j++)
|
1213
|
{
|
1214
|
xo = mOrigPos[i][3*j ];
|
1215
|
yo = mOrigPos[i][3*j+1];
|
1216
|
zo = mOrigPos[i][3*j+2];
|
1217
|
|
1218
|
currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
|
1219
|
|
1220
|
if( currError<minError )
|
1221
|
{
|
1222
|
minError = currError;
|
1223
|
minErrorIndex1 = i;
|
1224
|
minErrorIndex2 = j;
|
1225
|
}
|
1226
|
}
|
1227
|
}
|
1228
|
|
1229
|
if( minError< 0.1f ) // TODO: 0.1 ?
|
1230
|
{
|
1231
|
pos[offset ] = mOrigPos[minErrorIndex1][3*minErrorIndex2 ];
|
1232
|
pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
|
1233
|
pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
|
1234
|
}
|
1235
|
}
|
1236
|
|
1237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1238
|
// remember about the double cover or unit quaternions!
|
1239
|
|
1240
|
int mulQuat(int q1, int q2)
|
1241
|
{
|
1242
|
Static4D result = QuatHelper.quatMultiply(mObjectQuats[q1],mObjectQuats[q2]);
|
1243
|
|
1244
|
float rX = result.get0();
|
1245
|
float rY = result.get1();
|
1246
|
float rZ = result.get2();
|
1247
|
float rW = result.get3();
|
1248
|
|
1249
|
final float MAX_ERROR = 0.1f;
|
1250
|
float dX,dY,dZ,dW;
|
1251
|
|
1252
|
for(int i=0; i<mNumQuats; i++)
|
1253
|
{
|
1254
|
dX = mObjectQuats[i].get0() - rX;
|
1255
|
dY = mObjectQuats[i].get1() - rY;
|
1256
|
dZ = mObjectQuats[i].get2() - rZ;
|
1257
|
dW = mObjectQuats[i].get3() - rW;
|
1258
|
|
1259
|
if( dX<MAX_ERROR && dX>-MAX_ERROR &&
|
1260
|
dY<MAX_ERROR && dY>-MAX_ERROR &&
|
1261
|
dZ<MAX_ERROR && dZ>-MAX_ERROR &&
|
1262
|
dW<MAX_ERROR && dW>-MAX_ERROR ) return i;
|
1263
|
|
1264
|
dX = mObjectQuats[i].get0() + rX;
|
1265
|
dY = mObjectQuats[i].get1() + rY;
|
1266
|
dZ = mObjectQuats[i].get2() + rZ;
|
1267
|
dW = mObjectQuats[i].get3() + rW;
|
1268
|
|
1269
|
if( dX<MAX_ERROR && dX>-MAX_ERROR &&
|
1270
|
dY<MAX_ERROR && dY>-MAX_ERROR &&
|
1271
|
dZ<MAX_ERROR && dZ>-MAX_ERROR &&
|
1272
|
dW<MAX_ERROR && dW>-MAX_ERROR ) return i;
|
1273
|
}
|
1274
|
|
1275
|
return -1;
|
1276
|
}
|
1277
|
|
1278
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1279
|
|
1280
|
private float getAngle()
|
1281
|
{
|
1282
|
int pointNum = mRotationAngle.getNumPoints();
|
1283
|
|
1284
|
if( pointNum>=1 )
|
1285
|
{
|
1286
|
return mRotationAngle.getPoint(pointNum-1).get0();
|
1287
|
}
|
1288
|
else
|
1289
|
{
|
1290
|
mInterface.reportProblem("points in RotationAngle: "+pointNum, false);
|
1291
|
return 0;
|
1292
|
}
|
1293
|
}
|
1294
|
|
1295
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1296
|
|
1297
|
void setLibInterface(ObjectLibInterface inter)
|
1298
|
{
|
1299
|
mInterface = inter;
|
1300
|
}
|
1301
|
|
1302
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1303
|
|
1304
|
void initializeObject(int[][] moves)
|
1305
|
{
|
1306
|
solve();
|
1307
|
setupPosition(moves);
|
1308
|
}
|
1309
|
|
1310
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1311
|
|
1312
|
synchronized void removeRotationNow()
|
1313
|
{
|
1314
|
float angle = getAngle();
|
1315
|
double nearestAngleInRadians = angle*Math.PI/180;
|
1316
|
float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
|
1317
|
float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
|
1318
|
float axisX = mAxis[mCurrentRotAxis].get0();
|
1319
|
float axisY = mAxis[mCurrentRotAxis].get1();
|
1320
|
float axisZ = mAxis[mCurrentRotAxis].get2();
|
1321
|
Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
|
1322
|
|
1323
|
mRotationAngle.removeAll();
|
1324
|
mRotationAngleStatic.set0(0);
|
1325
|
|
1326
|
for(int i=0; i<mNumCubits; i++)
|
1327
|
{
|
1328
|
mBelongs[i] = belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap);
|
1329
|
if( mBelongs[i] ) mCubits[i].rotateCubit(quat);
|
1330
|
}
|
1331
|
|
1332
|
for(int i=0; i<mNumCubits; i++)
|
1333
|
{
|
1334
|
if( mBelongs[i] )
|
1335
|
{
|
1336
|
int index = mCubits[i].postRotateCubit(quat);
|
1337
|
setCubitQuat(i,mCubits[i].computeAssociation(),index);
|
1338
|
}
|
1339
|
else if( mCubits[i].getCubitType()==Cubit.TYPE_FOLLOWER )
|
1340
|
{
|
1341
|
mCubits[i].adjustRotRowPostRotateADecider();
|
1342
|
setCubitQuat(i,mCubits[i].computeAssociation(),mCubits[i].mQuatIndex);
|
1343
|
}
|
1344
|
}
|
1345
|
}
|
1346
|
|
1347
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1348
|
|
1349
|
long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
|
1350
|
{
|
1351
|
if( wasRotateApplied() )
|
1352
|
{
|
1353
|
float angle = getAngle();
|
1354
|
mRotationAngleStatic.set0(angle);
|
1355
|
mRotationAngleFinal.set0(nearestAngleInDegrees);
|
1356
|
mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
|
1357
|
|
1358
|
mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
|
1359
|
mRotationAngle.resetToBeginning();
|
1360
|
mRotationAngle.removeAll();
|
1361
|
mRotationAngle.add(mRotationAngleStatic);
|
1362
|
mRotationAngle.add(mRotationAngleMiddle);
|
1363
|
mRotationAngle.add(mRotationAngleFinal);
|
1364
|
mRotateEffect.notifyWhenFinished(listener);
|
1365
|
|
1366
|
return mRotateEffect.getID();
|
1367
|
}
|
1368
|
|
1369
|
return 0;
|
1370
|
}
|
1371
|
|
1372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1373
|
|
1374
|
synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
|
1375
|
{
|
1376
|
if( wasRotateApplied() )
|
1377
|
{
|
1378
|
mCurrentRotAxis = axis;
|
1379
|
mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
|
1380
|
|
1381
|
mRotationAngleStatic.set0(0.0f);
|
1382
|
mRotationAxis.set( mAxis[axis] );
|
1383
|
mRotationAngle.setDuration(durationMillis);
|
1384
|
mRotationAngle.resetToBeginning();
|
1385
|
mRotationAngle.add(new Static1D(0));
|
1386
|
mRotationAngle.add(new Static1D(angle));
|
1387
|
mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
|
1388
|
mRotateEffect.notifyWhenFinished(listener);
|
1389
|
|
1390
|
return mRotateEffect.getID();
|
1391
|
}
|
1392
|
|
1393
|
return 0;
|
1394
|
}
|
1395
|
|
1396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1397
|
|
1398
|
void continueRotation(float angleInDegrees)
|
1399
|
{
|
1400
|
mRotationAngleStatic.set0(angleInDegrees);
|
1401
|
}
|
1402
|
|
1403
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1404
|
|
1405
|
synchronized void beginNewRotation(int axis, int row )
|
1406
|
{
|
1407
|
if( axis<0 || axis>=mNumAxis )
|
1408
|
{
|
1409
|
android.util.Log.e("object", "invalid rotation axis: "+axis);
|
1410
|
return;
|
1411
|
}
|
1412
|
if( row<0 || row>=mNumLayers[axis] )
|
1413
|
{
|
1414
|
android.util.Log.e("object", "invalid rotation row: "+row);
|
1415
|
return;
|
1416
|
}
|
1417
|
|
1418
|
mCurrentRotAxis = axis;
|
1419
|
mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
|
1420
|
mRotationAngleStatic.set0(0.0f);
|
1421
|
mRotationAxis.set( mAxis[axis] );
|
1422
|
mRotationAngle.add(mRotationAngleStatic);
|
1423
|
mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
|
1424
|
}
|
1425
|
|
1426
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1427
|
|
1428
|
void setTextureMap(int cubit, int face, int newColor)
|
1429
|
{
|
1430
|
final float ratioW = 1.0f/mNumTexCols;
|
1431
|
final float ratioH = 1.0f/mNumTexRows;
|
1432
|
final Static4D[] maps = new Static4D[mNumCubitFaces];
|
1433
|
int row = (mNumTexRows-1) - newColor/mNumTexCols;
|
1434
|
int col = newColor%mNumTexCols;
|
1435
|
|
1436
|
maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
|
1437
|
mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
|
1438
|
}
|
1439
|
|
1440
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1441
|
|
1442
|
private int getCubitFaceColor(int cubit, int face)
|
1443
|
{
|
1444
|
int puzzleFace = getCubitFaceMap(cubit,face);
|
1445
|
if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
|
1446
|
return puzzleFace;
|
1447
|
}
|
1448
|
|
1449
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1450
|
|
1451
|
public int getCubitFaceMap(int cubit, int face)
|
1452
|
{
|
1453
|
int numFaces = mCubitFaceColors[cubit].length;
|
1454
|
int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
|
1455
|
return puzzleFace<0 ? -1 : puzzleFace;
|
1456
|
}
|
1457
|
|
1458
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1459
|
|
1460
|
void resetAllTextureMaps()
|
1461
|
{
|
1462
|
final float ratioW = 1.0f/mNumTexCols;
|
1463
|
final float ratioH = 1.0f/mNumTexRows;
|
1464
|
int cubColor, varColor, color, variant, row, col;
|
1465
|
|
1466
|
for(int cubit=0; cubit<mNumCubits; cubit++)
|
1467
|
{
|
1468
|
final Static4D[] maps = new Static4D[mNumCubitFaces];
|
1469
|
variant = getCubitVariant(cubit,mNumLayers);
|
1470
|
|
1471
|
for(int face=0; face<mNumCubitFaces; face++)
|
1472
|
{
|
1473
|
cubColor = getCubitFaceColor(cubit,face);
|
1474
|
varColor = getVariantFaceColor(variant,face);
|
1475
|
color = cubColor<0 || varColor<0 ? mNumTextures : varColor*mNumFaceColors + cubColor;
|
1476
|
row = (mNumTexRows-1) - color/mNumTexCols;
|
1477
|
col = color%mNumTexCols;
|
1478
|
|
1479
|
maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
|
1480
|
}
|
1481
|
|
1482
|
mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
|
1483
|
}
|
1484
|
}
|
1485
|
|
1486
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1487
|
|
1488
|
void releaseResources()
|
1489
|
{
|
1490
|
mTexture.markForDeletion();
|
1491
|
mMesh.markForDeletion();
|
1492
|
mEffects.markForDeletion();
|
1493
|
|
1494
|
for(int j=0; j<mNumCubits; j++)
|
1495
|
{
|
1496
|
mCubits[j].releaseResources();
|
1497
|
}
|
1498
|
}
|
1499
|
|
1500
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1501
|
|
1502
|
private void setCubitQuat(int cubit, int andAssociation, int equAssociation)
|
1503
|
{
|
1504
|
if( !mIsInMixupMode )
|
1505
|
{
|
1506
|
mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
|
1507
|
}
|
1508
|
else
|
1509
|
{
|
1510
|
mMesh.setEffectAssociation(cubit,andAssociation,cubit);
|
1511
|
Static4D tmp = mObjectQuats[equAssociation];
|
1512
|
mMixupModeQuats[cubit].set(tmp);
|
1513
|
}
|
1514
|
}
|
1515
|
|
1516
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1517
|
|
1518
|
synchronized void restorePreferences(SharedPreferences preferences)
|
1519
|
{
|
1520
|
boolean error = false;
|
1521
|
String key = getShortName();
|
1522
|
|
1523
|
for(int i=0; i<mNumCubits; i++)
|
1524
|
{
|
1525
|
mQuatDebug[i] = mCubits[i].restorePreferences(key,preferences);
|
1526
|
|
1527
|
if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
|
1528
|
{
|
1529
|
mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]]);
|
1530
|
}
|
1531
|
else
|
1532
|
{
|
1533
|
error = true;
|
1534
|
}
|
1535
|
}
|
1536
|
|
1537
|
for(int i=0; i<mNumCubits; i++)
|
1538
|
{
|
1539
|
if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
|
1540
|
{
|
1541
|
mCubits[i].computeRotationRow();
|
1542
|
setCubitQuat(i,mCubits[i].computeAssociation(),mQuatDebug[i]);
|
1543
|
}
|
1544
|
else
|
1545
|
{
|
1546
|
error = true;
|
1547
|
}
|
1548
|
}
|
1549
|
|
1550
|
if( error )
|
1551
|
{
|
1552
|
for(int i=0; i<mNumCubits; i++)
|
1553
|
{
|
1554
|
mCubits[i].solve();
|
1555
|
setCubitQuat(i,mCubits[i].computeAssociation(),0);
|
1556
|
}
|
1557
|
}
|
1558
|
}
|
1559
|
|
1560
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1561
|
|
1562
|
void savePreferences(SharedPreferences.Editor editor)
|
1563
|
{
|
1564
|
String key = getShortName();
|
1565
|
for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,editor);
|
1566
|
}
|
1567
|
|
1568
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1569
|
|
1570
|
public void removePreferences(SharedPreferences.Editor editor)
|
1571
|
{
|
1572
|
String key = getShortName();
|
1573
|
for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,editor);
|
1574
|
}
|
1575
|
|
1576
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1577
|
|
1578
|
private float computeRadiusCorrection(float[] sticker, int curr, int len)
|
1579
|
{
|
1580
|
final float A = 0.8f; // 0<A<1
|
1581
|
|
1582
|
int prev = curr>0 ? curr-1 : len-1;
|
1583
|
int next = curr<len-1 ? curr+1 : 0;
|
1584
|
|
1585
|
float v1x = sticker[2*prev ]-sticker[2*curr ];
|
1586
|
float v1y = sticker[2*prev+1]-sticker[2*curr+1];
|
1587
|
float v2x = sticker[2*next ]-sticker[2*curr ];
|
1588
|
float v2y = sticker[2*next+1]-sticker[2*curr+1];
|
1589
|
|
1590
|
float len1= v1x*v1x+v1y*v1y;
|
1591
|
float len2= v2x*v2x+v2y*v2y;
|
1592
|
|
1593
|
float cos = (v1x*v2x+v1y*v2y) / ( (float)Math.sqrt(len1*len2) );
|
1594
|
|
1595
|
return 1-A*cos;
|
1596
|
}
|
1597
|
|
1598
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1599
|
|
1600
|
public ObjectSticker retSticker(int sticker)
|
1601
|
{
|
1602
|
if( mStickers==null )
|
1603
|
{
|
1604
|
float rad = getStickerRadius();
|
1605
|
float str = getStickerStroke();
|
1606
|
float[][] angles = getStickerAngles();
|
1607
|
int numStickers = mStickerCoords.length;
|
1608
|
mStickers = new ObjectSticker[numStickers];
|
1609
|
|
1610
|
for(int s=0; s<numStickers; s++)
|
1611
|
{
|
1612
|
float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
|
1613
|
float radius = rad / scale;
|
1614
|
float stroke = str / scale;
|
1615
|
int len = mStickerCoords[s].length/2;
|
1616
|
float[] radii = new float[len];
|
1617
|
for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
|
1618
|
mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
|
1619
|
}
|
1620
|
}
|
1621
|
|
1622
|
return mStickers[sticker];
|
1623
|
}
|
1624
|
|
1625
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1626
|
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
|
1627
|
|
1628
|
public void adjustStickerCoords()
|
1629
|
{
|
1630
|
|
1631
|
}
|
1632
|
|
1633
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1634
|
|
1635
|
public Static4D[] getQuats()
|
1636
|
{
|
1637
|
if( mObjectQuats==null )
|
1638
|
{
|
1639
|
mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
|
1640
|
}
|
1641
|
|
1642
|
return mObjectQuats;
|
1643
|
}
|
1644
|
|
1645
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1646
|
|
1647
|
public int getInternalColor()
|
1648
|
{
|
1649
|
return COLOR_INTERNAL;
|
1650
|
}
|
1651
|
|
1652
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1653
|
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
|
1654
|
|
1655
|
private void createTexture()
|
1656
|
{
|
1657
|
Paint paint = new Paint();
|
1658
|
mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_4444);
|
1659
|
Canvas canvas = new Canvas(mBitmap);
|
1660
|
|
1661
|
paint.setAntiAlias(true);
|
1662
|
paint.setTextAlign(Paint.Align.CENTER);
|
1663
|
paint.setStyle(Paint.Style.FILL);
|
1664
|
|
1665
|
paint.setColor(getInternalColor());
|
1666
|
canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
|
1667
|
|
1668
|
int texture = 0;
|
1669
|
FactorySticker factory = FactorySticker.getInstance();
|
1670
|
|
1671
|
for(int row=0; row<mNumTexRows; row++)
|
1672
|
for(int col=0; col<mNumTexCols; col++)
|
1673
|
{
|
1674
|
if( texture>=mNumTextures ) break;
|
1675
|
ObjectSticker sticker = retSticker(texture/mNumFaceColors);
|
1676
|
int color = getColor(texture% mNumFaceColors);
|
1677
|
factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color, sticker);
|
1678
|
texture++;
|
1679
|
}
|
1680
|
}
|
1681
|
|
1682
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1683
|
|
1684
|
void setTexture()
|
1685
|
{
|
1686
|
if( mBitmap==null ) createTexture();
|
1687
|
|
1688
|
if( !mTexture.setTextureAlreadyInverted(mBitmap) )
|
1689
|
{
|
1690
|
int max = DistortedLibrary.getMaxTextureSize();
|
1691
|
mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
|
1692
|
}
|
1693
|
}
|
1694
|
|
1695
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1696
|
|
1697
|
void setObjectRatioNow(float sc, int nodeSize)
|
1698
|
{
|
1699
|
mObjectScreenRatio = sc;
|
1700
|
float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
|
1701
|
mObjectScale.set(scale,scale,scale);
|
1702
|
|
1703
|
if( mTouchControl ==null ) mTouchControl = getTouchControl();
|
1704
|
mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
|
1705
|
}
|
1706
|
|
1707
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1708
|
|
1709
|
void setObjectRatio(float sizeChange, int nodeSize)
|
1710
|
{
|
1711
|
mObjectScreenRatio *= (1.0f+sizeChange)/2;
|
1712
|
|
1713
|
if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
|
1714
|
if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
|
1715
|
|
1716
|
setObjectRatioNow(mObjectScreenRatio, nodeSize);
|
1717
|
}
|
1718
|
|
1719
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1720
|
|
1721
|
void setNodeSize(int nodeSize)
|
1722
|
{
|
1723
|
setObjectRatioNow(mObjectScreenRatio, nodeSize);
|
1724
|
}
|
1725
|
|
1726
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1727
|
|
1728
|
public float getRatio()
|
1729
|
{
|
1730
|
return mObjectScreenRatio;
|
1731
|
}
|
1732
|
|
1733
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1734
|
|
1735
|
public float getObjectRatio()
|
1736
|
{
|
1737
|
return mObjectScreenRatio*mInitScreenRatio;
|
1738
|
}
|
1739
|
|
1740
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1741
|
|
1742
|
boolean isSolved()
|
1743
|
{
|
1744
|
if( mSolvedFunctionIndex==0 ) return isSolved0();
|
1745
|
if( mSolvedFunctionIndex==1 ) return isSolved1();
|
1746
|
|
1747
|
return false;
|
1748
|
}
|
1749
|
|
1750
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1751
|
|
1752
|
int computeNearestAngle(int basicAngle, float angle, float speed)
|
1753
|
{
|
1754
|
int nearestAngle = 360/basicAngle;
|
1755
|
int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
|
1756
|
if( angle< -(nearestAngle*0.5) ) tmp-=1;
|
1757
|
|
1758
|
if( tmp!=0 ) return nearestAngle*tmp;
|
1759
|
|
1760
|
return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
|
1761
|
}
|
1762
|
|
1763
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1764
|
// INTERNAL API - those are called from 'effects' package
|
1765
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1766
|
|
1767
|
public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
|
1768
|
{
|
1769
|
mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
|
1770
|
}
|
1771
|
|
1772
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1773
|
|
1774
|
public Static4D getRotationQuat()
|
1775
|
{
|
1776
|
return mQuat;
|
1777
|
}
|
1778
|
|
1779
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1780
|
|
1781
|
public float getSize()
|
1782
|
{
|
1783
|
return mSize;
|
1784
|
}
|
1785
|
|
1786
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1787
|
|
1788
|
public void apply(Effect effect, int position)
|
1789
|
{
|
1790
|
mEffects.apply(effect, position);
|
1791
|
}
|
1792
|
|
1793
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1794
|
|
1795
|
public void remove(long effectID)
|
1796
|
{
|
1797
|
mEffects.abortById(effectID);
|
1798
|
}
|
1799
|
|
1800
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1801
|
|
1802
|
public MeshBase getObjectMesh()
|
1803
|
{
|
1804
|
return mMesh;
|
1805
|
}
|
1806
|
|
1807
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1808
|
|
1809
|
public DistortedEffects getObjectEffects()
|
1810
|
{
|
1811
|
return mEffects;
|
1812
|
}
|
1813
|
|
1814
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1815
|
// PUBLIC API
|
1816
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1817
|
|
1818
|
public int getCubitFaceColorIndex(int cubit, int face)
|
1819
|
{
|
1820
|
Static4D texMap = mMesh.getTextureMap(mNumFaceColors *cubit + face);
|
1821
|
|
1822
|
int x = (int)(texMap.get0()/texMap.get2());
|
1823
|
int y = (int)(texMap.get1()/texMap.get3());
|
1824
|
|
1825
|
return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
|
1826
|
}
|
1827
|
|
1828
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1829
|
|
1830
|
public int[] getNumLayers()
|
1831
|
{
|
1832
|
return mNumLayers;
|
1833
|
}
|
1834
|
|
1835
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1836
|
|
1837
|
public synchronized void solve()
|
1838
|
{
|
1839
|
for(int i=0; i<mNumCubits; i++)
|
1840
|
{
|
1841
|
mCubits[i].solve();
|
1842
|
setCubitQuat(i,mCubits[i].computeAssociation(),0);
|
1843
|
}
|
1844
|
}
|
1845
|
|
1846
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1847
|
|
1848
|
public int getCubitQuatIndex(int cubit)
|
1849
|
{
|
1850
|
return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
|
1851
|
}
|
1852
|
|
1853
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1854
|
|
1855
|
public int getCubitRotRow(int cubit, int axis)
|
1856
|
{
|
1857
|
return mCubits[cubit].getRotRow(axis);
|
1858
|
}
|
1859
|
|
1860
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1861
|
|
1862
|
public Bitmap getStickerBitmap()
|
1863
|
{
|
1864
|
return mBitmap;
|
1865
|
}
|
1866
|
|
1867
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1868
|
|
1869
|
public DistortedNode getNode()
|
1870
|
{
|
1871
|
return mNode;
|
1872
|
}
|
1873
|
|
1874
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1875
|
|
1876
|
public int getNumStickerTypes()
|
1877
|
{
|
1878
|
return mNumStickerTypes;
|
1879
|
}
|
1880
|
|
1881
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1882
|
// this is here only so it can be overridden in TwistyJSON soo that we can get this from JSON.
|
1883
|
|
1884
|
public int getNumCubitFaces()
|
1885
|
{
|
1886
|
return 0;
|
1887
|
}
|
1888
|
|
1889
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1890
|
|
1891
|
public TouchControl getTouchControl()
|
1892
|
{
|
1893
|
if( mTouchControl==null )
|
1894
|
{
|
1895
|
switch(getTouchControlType())
|
1896
|
{
|
1897
|
case TC_TETRAHEDRON : mTouchControl = new TouchControlTetrahedron(this);
|
1898
|
break;
|
1899
|
case TC_HEXAHEDRON : mTouchControl = new TouchControlHexahedron(this);
|
1900
|
break;
|
1901
|
case TC_OCTAHEDRON : mTouchControl = new TouchControlOctahedron(this);
|
1902
|
break;
|
1903
|
case TC_DODECAHEDRON : mTouchControl = new TouchControlDodecahedron(this);
|
1904
|
break;
|
1905
|
case TC_CUBOID : int[] numLayers = getNumLayers();
|
1906
|
mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
|
1907
|
break;
|
1908
|
case TC_CHANGING_MIRROR : mTouchControl = new TouchControlMirror(this);
|
1909
|
break;
|
1910
|
case TC_CHANGING_SQUARE : mTouchControl = new TouchControlSquare(this);
|
1911
|
break;
|
1912
|
case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
|
1913
|
break;
|
1914
|
}
|
1915
|
}
|
1916
|
return mTouchControl;
|
1917
|
}
|
1918
|
|
1919
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1920
|
|
1921
|
protected void setReader(JsonReader reader)
|
1922
|
{
|
1923
|
// empty
|
1924
|
}
|
1925
|
|
1926
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1927
|
// for JSON only
|
1928
|
public abstract int getTouchControlType();
|
1929
|
public abstract int getTouchControlSplit();
|
1930
|
public abstract boolean[][] getLayerRotatable(int[] numLayers);
|
1931
|
public abstract int[][][] getEnabled();
|
1932
|
public abstract float[] getDist3D(int[] numLayers);
|
1933
|
public abstract Static3D[] getFaceAxis();
|
1934
|
public abstract ScrambleState[] getScrambleStates();
|
1935
|
public abstract float[][] getCuts(int[] numLayers);
|
1936
|
public abstract float getStickerRadius();
|
1937
|
public abstract float getStickerStroke();
|
1938
|
public abstract float[][] getStickerAngles();
|
1939
|
public abstract int getCubitVariant(int cubit, int[] numLayers);
|
1940
|
public abstract ObjectShape getObjectShape(int variant);
|
1941
|
public abstract ObjectFaceShape getObjectFaceShape(int variant);
|
1942
|
public abstract int getNumCubitVariants(int[] numLayers);
|
1943
|
public abstract float[][] getCubitPositions(int[] numLayers);
|
1944
|
public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
|
1945
|
public abstract int getNumFaceColors();
|
1946
|
public abstract float getScreenRatio();
|
1947
|
public abstract int getColor(int face);
|
1948
|
public abstract String getShortName();
|
1949
|
public abstract ObjectSignature getSignature();
|
1950
|
|
1951
|
// not only for JSON
|
1952
|
public abstract Static3D[] getRotationAxis();
|
1953
|
public abstract int[][] getBasicAngles();
|
1954
|
public abstract int getNumFaces();
|
1955
|
public abstract String getObjectName();
|
1956
|
public abstract String getInventor();
|
1957
|
public abstract int getYearOfInvention();
|
1958
|
public abstract int getComplexity();
|
1959
|
public abstract int getFOV();
|
1960
|
public abstract String[][] getTutorials();
|
1961
|
}
|