Project

General

Profile

Download (42.5 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ a57e6870

1 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 android.content.SharedPreferences;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
28
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29
30
import org.distorted.library.effect.Effect;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectQuaternion;
35
import org.distorted.library.effect.VertexEffectRotate;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedTexture;
40 a706f8e0 Leszek Koltunski
import org.distorted.library.main.QuatHelper;
41 29b82486 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
42
import org.distorted.library.mesh.MeshFile;
43
import org.distorted.library.mesh.MeshJoined;
44
import org.distorted.library.mesh.MeshSquare;
45
import org.distorted.library.message.EffectListener;
46
import org.distorted.library.type.Dynamic1D;
47
import org.distorted.library.type.Static1D;
48
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50 a57e6870 Leszek Koltunski
51 29b82486 Leszek Koltunski
import org.distorted.objectlib.BuildConfig;
52 198c5bf0 Leszek Koltunski
import org.distorted.objectlib.helpers.FactoryCubit;
53
import org.distorted.objectlib.helpers.FactorySticker;
54
import org.distorted.objectlib.helpers.ObjectShape;
55
import org.distorted.objectlib.helpers.ObjectSticker;
56
import org.distorted.objectlib.helpers.ScrambleState;
57 29b82486 Leszek Koltunski
58
import java.io.DataInputStream;
59
import java.io.IOException;
60
import java.io.InputStream;
61
import java.util.Random;
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
public abstract class TwistyObject extends DistortedNode
66
  {
67
  public static final int COLOR_YELLOW = 0xffffff00;
68
  public static final int COLOR_WHITE  = 0xffffffff;
69
  public static final int COLOR_BLUE   = 0xff0000ff;
70
  public static final int COLOR_GREEN  = 0xff00bb00;
71
  public static final int COLOR_RED    = 0xff990000;
72
  public static final int COLOR_ORANGE = 0xffff6200;
73
  public static final int COLOR_GREY   = 0xff727c7b;
74
  public static final int COLOR_VIOLET = 0xff7700bb;
75
  public static final int COLOR_BLACK  = 0xff000000;
76
77
  public static final int TEXTURE_HEIGHT = 256;
78
  static final int NUM_STICKERS_IN_ROW = 4;
79
80
  public static final float SQ2 = (float)Math.sqrt(2);
81
  public static final float SQ3 = (float)Math.sqrt(3);
82
  public static final float SQ5 = (float)Math.sqrt(5);
83
  public static final float SQ6 = (float)Math.sqrt(6);
84
85
  private static final float NODE_RATIO = 1.60f;
86
  private static final float MAX_SIZE_CHANGE = 1.35f;
87
  private static final float MIN_SIZE_CHANGE = 0.75f;
88
89
  private static final Static3D CENTER = new Static3D(0,0,0);
90
  private static final int POST_ROTATION_MILLISEC = 500;
91
92
  protected final int NUM_FACE_COLORS;
93
  protected final int NUM_TEXTURES;
94
  protected final Cubit[] CUBITS;
95
96
  MeshBase[] mMeshes;
97
  final Static4D[] OBJECT_QUATS;
98
  final int NUM_CUBITS;
99
  final int NUM_AXIS;
100
  final int NUM_QUATS;
101
102
  private final int mNumCubitFaces;
103
  private final Static3D[] mAxis;
104
  private final float[][] mCuts;
105
  private final int[] mNumCuts;
106
  private final int mNodeSize;
107
  private final float[][] mOrigPos;
108
  private final Static3D mNodeScale;
109
  private final Static4D mQuat;
110 a57e6870 Leszek Koltunski
  private final int[] mNumLayers;
111
  private final int mRealSize;
112 29b82486 Leszek Koltunski
  private final DistortedEffects mEffects;
113
  private final VertexEffectRotate mRotateEffect;
114
  private final Dynamic1D mRotationAngle;
115
  private final Static3D mRotationAxis;
116
  private final Static3D mObjectScale;
117
  private final int[] mQuatDebug;
118
  private final float mCameraDist;
119
  private final Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
120
  private final DistortedTexture mTexture;
121
  private final float mInitScreenRatio;
122
  private final int mSolvedFunctionIndex;
123
  private final boolean mIsBandaged;
124
  private float mObjectScreenRatio;
125
  private int[][] mSolvedQuats;
126
  private int[][] mQuatMult;
127
  private int[] mTmpQuats;
128
  private int mNumTexRows, mNumTexCols;
129
  private int mRotRowBitmap;
130
  private int mRotAxis;
131
  private MeshBase mMesh;
132
  private final TwistyObjectScrambler mScrambler;
133
134
  //////////////////// SOLVED1 ////////////////////////
135
136
  private int[] mFaceMap;
137
  private int[][] mScramble;
138
  private int[] mColors;
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 a57e6870 Leszek Koltunski
  TwistyObject(int[] numLayers, int realSize, Static4D quat, Static3D move, DistortedTexture nodeTexture,
143 81141862 Leszek Koltunski
               MeshSquare nodeMesh, DistortedEffects nodeEffects, Resources res, int screenWidth)
144 29b82486 Leszek Koltunski
    {
145
    super(nodeTexture,nodeEffects,nodeMesh);
146
147
    mNodeSize = screenWidth;
148
149
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
150
151
    mNumLayers = numLayers;
152
    mRealSize = realSize;
153
    mOrigPos = getCubitPositions(mNumLayers);
154
    mAxis = getRotationAxis();
155
    mInitScreenRatio = getScreenRatio();
156
    mObjectScreenRatio = 1.0f;
157
    mNumCubitFaces = getNumCubitFaces();
158
    mSolvedFunctionIndex = getSolvedFunctionIndex();
159
160
    mCuts = getCuts(mNumLayers);
161
    mNumCuts = new int[mAxis.length];
162
    if( mCuts==null ) for(int i=0; i<mAxis.length; i++) mNumCuts[i] = 0;
163
    else              for(int i=0; i<mAxis.length; i++) mNumCuts[i] = mCuts[i].length;
164
165
    OBJECT_QUATS = getQuats();
166
    NUM_CUBITS  = mOrigPos.length;
167
    NUM_FACE_COLORS = getNumFaceColors();
168
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
169
    NUM_AXIS = mAxis.length;
170
    NUM_QUATS = OBJECT_QUATS.length;
171
172
    int scramblingType = getScrambleType();
173
    ScrambleState[] states = getScrambleStates();
174
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,numLayers,states);
175
176
    boolean bandaged=false;
177
178
    for(int c=0; c<NUM_CUBITS; c++)
179
      {
180
      if( mOrigPos[c].length>3 )
181
        {
182
        bandaged=true;
183
        break;
184
        }
185
      }
186
187
    mIsBandaged = bandaged;
188
189
    mQuatDebug = new int[NUM_CUBITS];
190
191
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
192
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
193
194 e607083f Leszek Koltunski
    mNodeScale= new Static3D(screenWidth,NODE_RATIO*screenWidth,screenWidth);
195 29b82486 Leszek Koltunski
    mQuat = quat;
196
197
    mRotationAngle= new Dynamic1D();
198
    mRotationAxis = new Static3D(1,0,0);
199
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
200
201
    mRotationAngleStatic = new Static1D(0);
202
    mRotationAngleMiddle = new Static1D(0);
203
    mRotationAngleFinal  = new Static1D(0);
204
205
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
206
    mObjectScale = new Static3D(scale,scale,scale);
207
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
208
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
209 b80e6524 Leszek Koltunski
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
210 29b82486 Leszek Koltunski
211
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
212
    nodeEffects.apply(nodeScaleEffect);
213
214
    mNumTexCols = NUM_STICKERS_IN_ROW;
215
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
216
217
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
218
219
    CUBITS = new Cubit[NUM_CUBITS];
220 61aa85e4 Leszek Koltunski
    createMeshAndCubits(res);
221 29b82486 Leszek Koltunski
    createDataStructuresForSolved(numLayers);
222
223
    mTexture = new DistortedTexture();
224
    mEffects = new DistortedEffects();
225
226 9a316d92 Leszek Koltunski
    createTexture();
227
228 29b82486 Leszek Koltunski
    for(int q=0; q<NUM_QUATS; q++)
229
      {
230
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
231
      vq.setMeshAssociation(0,q);
232
      mEffects.apply(vq);
233
      }
234
235
    mEffects.apply(mRotateEffect);
236
    mEffects.apply(quatEffect);
237
    mEffects.apply(scaleEffect);
238 b80e6524 Leszek Koltunski
    mEffects.apply(moveEffect);
239 29b82486 Leszek Koltunski
240
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
241
    // order for the effect to be in front of the object, we need to set the center to be behind it.
242
    getMesh().setComponentCenter(0,0,0,-0.1f);
243
244
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
245
246
    float fov = getFOV();
247
    double halfFOV = fov * (Math.PI/360);
248
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
249
250
    setProjection( fov, 0.1f);
251
    }
252
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
255
  private Static3D getPos(float[] origPos)
256
    {
257
    int len = origPos.length/3;
258
    float sumX = 0.0f;
259
    float sumY = 0.0f;
260
    float sumZ = 0.0f;
261
262
    for(int i=0; i<len; i++)
263
      {
264
      sumX += origPos[3*i  ];
265
      sumY += origPos[3*i+1];
266
      sumZ += origPos[3*i+2];
267
      }
268
269
    sumX /= len;
270
    sumY /= len;
271
    sumZ /= len;
272
273
    return new Static3D(sumX,sumY,sumZ);
274
    }
275
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
278 61aa85e4 Leszek Koltunski
  private void createMeshAndCubits(Resources res)
279 29b82486 Leszek Koltunski
    {
280 4e1dc313 Leszek Koltunski
    int resourceID= getResource(mNumLayers);
281 29b82486 Leszek Koltunski
282 568d4698 Leszek Koltunski
    if( resourceID!=0 && !ObjectControl.isInCreateMesh() )
283 29b82486 Leszek Koltunski
      {
284
      InputStream is = res.openRawResource(resourceID);
285
      DataInputStream dos = new DataInputStream(is);
286
      mMesh = new MeshFile(dos);
287
288
      try
289
        {
290
        is.close();
291
        }
292
      catch(IOException e)
293
        {
294
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
295
        }
296
297
      for(int i=0; i<NUM_CUBITS; i++)
298
        {
299
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
300
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
301
        }
302
303
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
304
      }
305
    else
306
      {
307
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
308
309
      for(int i=0; i<NUM_CUBITS; i++)
310
        {
311
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
312
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
313
        Static3D pos = getPos(mOrigPos[i]);
314
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
315
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
316
        }
317
318
      mMesh = new MeshJoined(cubitMesh);
319
      resetAllTextureMaps();
320
      }
321
    }
322
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324
325 a57e6870 Leszek Koltunski
  private MeshBase createCubitMesh(int cubit, int[] numLayers)
326 29b82486 Leszek Koltunski
    {
327
    int variant = getCubitVariant(cubit,numLayers);
328
329
    if( mMeshes==null )
330
      {
331
      FactoryCubit factory = FactoryCubit.getInstance();
332
      factory.clear();
333
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
334
      }
335
336
    if( mMeshes[variant]==null )
337
      {
338
      ObjectShape shape = getObjectShape(cubit,numLayers);
339
      FactoryCubit factory = FactoryCubit.getInstance();
340
      factory.createNewFaceTransform(shape);
341
      mMeshes[variant] = factory.createRoundedSolid(shape);
342
      }
343
344
    MeshBase mesh = mMeshes[variant].copy(true);
345
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
346
    mesh.apply(quat,0xffffffff,0);
347
348
    return mesh;
349
    }
350
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352
353 a57e6870 Leszek Koltunski
  private void createDataStructuresForSolved(int[] numLayers)
354 29b82486 Leszek Koltunski
    {
355
    mTmpQuats = new int[NUM_QUATS];
356
    mSolvedQuats = new int[NUM_CUBITS][];
357
358
    for(int c=0; c<NUM_CUBITS; c++)
359
      {
360
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
361
      }
362
    }
363
364 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
365
366
  private int getMultQuat(int index1, int index2)
367
    {
368
    if( mQuatMult==null )
369
      {
370
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
371
372
      for(int i=0; i<NUM_QUATS; i++)
373
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
374
      }
375
376
    if( mQuatMult[index1][index2]==-1 )
377
      {
378
      mQuatMult[index1][index2] = mulQuat(index1,index2);
379
      }
380
381
    return mQuatMult[index1][index2];
382
    }
383
384 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
385
// This is used to build internal data structures for the generic 'isSolved()'
386
//
387
// if this is an internal cubit (all faces black): return -1
388
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
389
// Color index, i.e. the index into the 'FACE_COLORS' table.
390
// else (edge or corner cubit, more than one non-black face): return -2.
391
392 a57e6870 Leszek Koltunski
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
393 29b82486 Leszek Koltunski
    {
394
    int numNonBlack=0, nonBlackIndex=-1, color;
395
396
    for(int face=0; face<mNumCubitFaces; face++)
397
      {
398
      color = getFaceColor(cubit,face,numLayers);
399
400
      if( color<NUM_TEXTURES )
401
        {
402
        numNonBlack++;
403
        nonBlackIndex = color%NUM_FACE_COLORS;
404
        }
405
      }
406
407
    if( numNonBlack==0 ) return -1;
408
    if( numNonBlack>=2 ) return -2;
409
410
    return nonBlackIndex;
411
    }
412
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415 c8bc83d9 Leszek Koltunski
  protected boolean shouldResetTextureMaps()
416 29b82486 Leszek Koltunski
    {
417
    return false;
418
    }
419
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
422 c8bc83d9 Leszek Koltunski
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
423 29b82486 Leszek Koltunski
    {
424
    final float MAXD = 0.0001f;
425
    float x = faceAx.get0();
426
    float y = faceAx.get1();
427
    float z = faceAx.get2();
428
    float a,dx,dy,dz,qx,qy,qz;
429
    Static4D quat;
430
431
    int len = quats.length;
432
    int place = 0;
433
434
    for(int q=1; q<len; q++)
435
      {
436
      quat = quats[q];
437
      qx = quat.get0();
438
      qy = quat.get1();
439
      qz = quat.get2();
440
441
           if( x!=0.0f ) { a = qx/x; }
442
      else if( y!=0.0f ) { a = qy/y; }
443
      else               { a = qz/z; }
444
445
      dx = a*x-qx;
446
      dy = a*y-qy;
447
      dz = a*z-qz;
448
449
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
450
        {
451
        mTmpQuats[place++] = q;
452
        }
453
      }
454
455
    if( place!=0 )
456
      {
457
      int[] ret = new int[place];
458
      System.arraycopy(mTmpQuats,0,ret,0,place);
459
      return ret;
460
      }
461
462
    return null;
463
    }
464
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466
467 c8bc83d9 Leszek Koltunski
  private boolean isSolved0()
468 29b82486 Leszek Koltunski
    {
469
    int len, q1,q = CUBITS[0].mQuatIndex;
470
    int[] solved;
471
    boolean skip;
472
473
    for(int c=1; c<NUM_CUBITS; c++)
474
      {
475
      q1 = CUBITS[c].mQuatIndex;
476
477
      if( q1==q ) continue;
478
479
      skip = false;
480
      solved = mSolvedQuats[c];
481
      len = solved==null ? 0:solved.length;
482
483
      for(int i=0; i<len; i++)
484
        {
485
        if( q1==getMultQuat(q,solved[i]) )
486
          {
487
          skip = true;
488
          break;
489
          }
490
        }
491
492
      if( !skip ) return false;
493
      }
494
495
    return true;
496
    }
497
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
500
  private int computeScramble(int quatNum, int centerNum)
501
    {
502
    float MAXDIFF = 0.01f;
503
    float[] center= mOrigPos[centerNum];
504
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
505
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
506
507
    float x = result.get0();
508
    float y = result.get1();
509
    float z = result.get2();
510
511
    for(int c=0; c<NUM_CUBITS; c++)
512
      {
513
      float[] cent = mOrigPos[c];
514
515
      float qx = cent[0] - x;
516
      float qy = cent[1] - y;
517
      float qz = cent[2] - z;
518
519
      if( qx>-MAXDIFF && qx<MAXDIFF &&
520
          qy>-MAXDIFF && qy<MAXDIFF &&
521
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
522
      }
523
524
    return -1;
525
    }
526
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528
// Dino4 uses this. It is solved if and only if groups of cubits
529
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
530
// or
531
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
532
// are all the same color.
533
534 c8bc83d9 Leszek Koltunski
  private boolean isSolved1()
535 29b82486 Leszek Koltunski
    {
536
    if( mScramble==null )
537
      {
538
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
539
      mColors   = new int[NUM_CUBITS];
540
541
      for(int q=0; q<NUM_QUATS; q++)
542
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
543
      }
544
545
    if( mFaceMap==null )
546
      {
547
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
548
      }
549
550
    for(int c=0; c<NUM_CUBITS; c++)
551
      {
552
      int index = mScramble[CUBITS[c].mQuatIndex][c];
553
      mColors[index] = mFaceMap[c];
554
      }
555
556
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
557
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
558
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
559
560
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
561
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
562
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
563
564
    return false;
565
    }
566
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568
// Dino6 uses this. It is solved if and only if:
569
//
570
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
571
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
572
// by the same qZ, and then either:
573
//
574
// a) qX = qY = qZ
575
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
576
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
577
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
578
//
579
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
580
//
581
// X cubits: 0, 2, 8, 10
582
// Y cubits: 1, 3, 9, 11
583
// Z cubits: 4, 5, 6, 7
584
585 c8bc83d9 Leszek Koltunski
  private boolean isSolved2()
586 29b82486 Leszek Koltunski
    {
587
    int qX = CUBITS[0].mQuatIndex;
588
    int qY = CUBITS[1].mQuatIndex;
589
    int qZ = CUBITS[4].mQuatIndex;
590
591
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
592
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
593
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
594
      {
595
      return false;
596
      }
597
598
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
599
    }
600
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602
// Square-2 is solved iff
603
// a) all of its cubits are rotated with the same quat
604
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
605
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
606
// and all the 12 left and right edges and corners also with the same quat multiplied by
607
// QUATS[12] - i.e. also upside down.
608
609 c8bc83d9 Leszek Koltunski
  private boolean isSolved3()
610 29b82486 Leszek Koltunski
    {
611
    int index = CUBITS[0].mQuatIndex;
612
613
    if( CUBITS[1].mQuatIndex!=index ) return false;
614
615
    boolean solved = true;
616
617
    for(int i=2; i<NUM_CUBITS; i++)
618
      {
619
      if( CUBITS[i].mQuatIndex!=index )
620
        {
621
        solved = false;
622
        break;
623
        }
624
      }
625
626
    if( solved ) return true;
627
628
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
629
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
630
631
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
632
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
633
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
634
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
635
636
    return true;
637
    }
638
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640
641
  int computeRow(float[] pos, int axisIndex)
642
    {
643
    int ret=0;
644
    int len = pos.length / 3;
645
    Static3D axis = mAxis[axisIndex];
646
    float axisX = axis.get0();
647
    float axisY = axis.get1();
648
    float axisZ = axis.get2();
649
    float casted;
650
651
    for(int i=0; i<len; i++)
652
      {
653
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
654
      ret |= computeSingleRow(axisIndex,casted);
655
      }
656
657
    return ret;
658
    }
659
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661
662
  private int computeSingleRow(int axisIndex,float casted)
663
    {
664
    int num = mNumCuts[axisIndex];
665
666
    for(int i=0; i<num; i++)
667
      {
668
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
669
      }
670
671
    return (1<<num);
672
    }
673
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675
676
  private boolean wasRotateApplied()
677
    {
678
    return mEffects.exists(mRotateEffect.getID());
679
    }
680
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682
683
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
684
    {
685
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
686
    }
687
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689
// note the minus in front of the sin() - we rotate counterclockwise
690
// when looking towards the direction where the axis increases in values.
691
692
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
693
    {
694
    Static3D axis = mAxis[axisIndex];
695
696
    while( angleInDegrees<0 ) angleInDegrees += 360;
697
    angleInDegrees %= 360;
698
    
699
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
700
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
701
702
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
703
    }
704
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706
707
  private synchronized void setupPosition(int[][] moves)
708
    {
709
    if( moves!=null )
710
      {
711
      Static4D quat;
712
      int index, axis, rowBitmap, angle;
713
      int[] basic = getBasicAngle();
714
715
      for(int[] move: moves)
716
        {
717
        axis     = move[0];
718
        rowBitmap= move[1];
719
        angle    = move[2]*(360/basic[axis]);
720
        quat     = makeQuaternion(axis,angle);
721
722
        for(int j=0; j<NUM_CUBITS; j++)
723
          if( belongsToRotation(j,axis,rowBitmap) )
724
            {
725
            index = CUBITS[j].removeRotationNow(quat);
726
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
727
            }
728
        }
729
      }
730
    }
731
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733
734 0310ac32 Leszek Koltunski
  protected int getScrambleType()
735 29b82486 Leszek Koltunski
    {
736
    return 0;
737
    }
738
739
///////////////////////////////////////////////////////////////////////////////////////////////////
740
741
  int computeBitmapFromRow(int rowBitmap, int axis)
742
    {
743
    if( mIsBandaged )
744
      {
745
      int bitmap, initBitmap=0;
746
747
      while( initBitmap!=rowBitmap )
748
        {
749
        initBitmap = rowBitmap;
750
751
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
752
          {
753
          bitmap = CUBITS[cubit].getRotRow(axis);
754
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
755
          }
756
        }
757
      }
758
759
    return rowBitmap;
760
    }
761
762
///////////////////////////////////////////////////////////////////////////////////////////////////
763
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
764
// Do so only if minimal Error is appropriately low (shape-shifting puzzles - Square-1)
765
766
  void clampPos(float[] pos, int offset)
767
    {
768
    float currError, minError = Float.MAX_VALUE;
769
    int minErrorIndex1 = -1;
770
    int minErrorIndex2 = -1;
771
772
    float x = pos[offset  ];
773
    float y = pos[offset+1];
774
    float z = pos[offset+2];
775
776
    float xo,yo,zo;
777
778
    for(int i=0; i<NUM_CUBITS; i++)
779
      {
780
      int len = mOrigPos[i].length / 3;
781
782
      for(int j=0; j<len; j++)
783
        {
784
        xo = mOrigPos[i][3*j  ];
785
        yo = mOrigPos[i][3*j+1];
786
        zo = mOrigPos[i][3*j+2];
787
788
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
789
790
        if( currError<minError )
791
          {
792
          minError = currError;
793
          minErrorIndex1 = i;
794
          minErrorIndex2 = j;
795
          }
796
        }
797
      }
798
799
    if( minError< 0.1f ) // TODO: 0.1 ?
800
      {
801
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
802
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
803
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
804
      }
805
    }
806
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808
// remember about the double cover or unit quaternions!
809
810
  int mulQuat(int q1, int q2)
811
    {
812
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
813
814
    float rX = result.get0();
815
    float rY = result.get1();
816
    float rZ = result.get2();
817
    float rW = result.get3();
818
819
    final float MAX_ERROR = 0.1f;
820
    float dX,dY,dZ,dW;
821
822
    for(int i=0; i<NUM_QUATS; i++)
823
      {
824
      dX = OBJECT_QUATS[i].get0() - rX;
825
      dY = OBJECT_QUATS[i].get1() - rY;
826
      dZ = OBJECT_QUATS[i].get2() - rZ;
827
      dW = OBJECT_QUATS[i].get3() - rW;
828
829
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
830
          dY<MAX_ERROR && dY>-MAX_ERROR &&
831
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
832
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
833
834
      dX = OBJECT_QUATS[i].get0() + rX;
835
      dY = OBJECT_QUATS[i].get1() + rY;
836
      dZ = OBJECT_QUATS[i].get2() + rZ;
837
      dW = OBJECT_QUATS[i].get3() + rW;
838
839
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
840
          dY<MAX_ERROR && dY>-MAX_ERROR &&
841
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
842
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
843
      }
844
845
    return -1;
846
    }
847
848 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
849
850
  private float getAngle()
851
    {
852
    int pointNum = mRotationAngle.getNumPoints();
853
854
    if( pointNum>=1 )
855
      {
856
      return mRotationAngle.getPoint(pointNum-1).get0();
857
      }
858
    else
859
      {
860
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
861
      crashlytics.log("points in RotationAngle: "+pointNum);
862
      return 0;
863
      }
864
    }
865
866
///////////////////////////////////////////////////////////////////////////////////////////////////
867
868
  private void recordQuatsState(String message)
869
    {
870
    StringBuilder quats = new StringBuilder();
871
872
    for(int j=0; j<NUM_CUBITS; j++)
873
      {
874
      quats.append(mQuatDebug[j]);
875
      quats.append(" ");
876
      }
877
878
    String name = intGetObjectType(mNumLayers).name();
879
880
    if( BuildConfig.DEBUG )
881
      {
882
      android.util.Log.e("quats" , quats.toString());
883
      android.util.Log.e("object", name);
884
      }
885
    else
886
      {
887
      Exception ex = new Exception(message);
888
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
889
      crashlytics.setCustomKey("quats" , quats.toString());
890
      crashlytics.setCustomKey("object", name);
891
      crashlytics.recordException(ex);
892
      }
893
    }
894
895 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
896 198c5bf0 Leszek Koltunski
897 880beeea Leszek Koltunski
  void initializeObject(int[][] moves)
898 7c111294 Leszek Koltunski
    {
899
    solve();
900
    setupPosition(moves);
901
    }
902 198c5bf0 Leszek Koltunski
903 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
904 198c5bf0 Leszek Koltunski
905 880beeea Leszek Koltunski
  synchronized void removeRotationNow()
906 7c111294 Leszek Koltunski
    {
907
    float angle = getAngle();
908
    double nearestAngleInRadians = angle*Math.PI/180;
909
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
910
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
911
    float axisX = mAxis[mRotAxis].get0();
912
    float axisY = mAxis[mRotAxis].get1();
913
    float axisZ = mAxis[mRotAxis].get2();
914
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
915 198c5bf0 Leszek Koltunski
916 7c111294 Leszek Koltunski
    mRotationAngle.removeAll();
917
    mRotationAngleStatic.set0(0);
918
919
    for(int i=0; i<NUM_CUBITS; i++)
920
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
921 198c5bf0 Leszek Koltunski
        {
922 7c111294 Leszek Koltunski
        int index = CUBITS[i].removeRotationNow(quat);
923
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
924 198c5bf0 Leszek Koltunski
        }
925
    }
926
927 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
928
929 880beeea Leszek Koltunski
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
930 c8bc83d9 Leszek Koltunski
    {
931 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
932
      {
933
      float angle = getAngle();
934
      mRotationAngleStatic.set0(angle);
935
      mRotationAngleFinal.set0(nearestAngleInDegrees);
936
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
937 c8bc83d9 Leszek Koltunski
938 7c111294 Leszek Koltunski
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
939
      mRotationAngle.resetToBeginning();
940
      mRotationAngle.removeAll();
941
      mRotationAngle.add(mRotationAngleStatic);
942
      mRotationAngle.add(mRotationAngleMiddle);
943
      mRotationAngle.add(mRotationAngleFinal);
944
      mRotateEffect.notifyWhenFinished(listener);
945 c8bc83d9 Leszek Koltunski
946 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
947
      }
948 c8bc83d9 Leszek Koltunski
949 7c111294 Leszek Koltunski
    return 0;
950 c8bc83d9 Leszek Koltunski
    }
951
952
///////////////////////////////////////////////////////////////////////////////////////////////////
953
954 880beeea Leszek Koltunski
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
955 c8bc83d9 Leszek Koltunski
    {
956 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
957
      {
958
      mRotAxis     = axis;
959
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
960 c8bc83d9 Leszek Koltunski
961 7c111294 Leszek Koltunski
      mRotationAngleStatic.set0(0.0f);
962
      mRotationAxis.set( mAxis[axis] );
963
      mRotationAngle.setDuration(durationMillis);
964
      mRotationAngle.resetToBeginning();
965
      mRotationAngle.add(new Static1D(0));
966
      mRotationAngle.add(new Static1D(angle));
967
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
968
      mRotateEffect.notifyWhenFinished(listener);
969 c8bc83d9 Leszek Koltunski
970 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
971
      }
972
973
    return 0;
974 c8bc83d9 Leszek Koltunski
    }
975
976 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
977
978 880beeea Leszek Koltunski
  void continueRotation(float angleInDegrees)
979
    {
980
    mRotationAngleStatic.set0(angleInDegrees);
981
    }
982
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984
985
  synchronized void beginNewRotation(int axis, int row )
986
    {
987
    if( axis<0 || axis>=NUM_AXIS )
988
      {
989
      android.util.Log.e("object", "invalid rotation axis: "+axis);
990
      return;
991
      }
992 a57e6870 Leszek Koltunski
    if( row<0 || row>=mNumLayers[axis] )
993 880beeea Leszek Koltunski
      {
994
      android.util.Log.e("object", "invalid rotation row: "+row);
995
      return;
996
      }
997
998
    mRotAxis     = axis;
999
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1000
    mRotationAngleStatic.set0(0.0f);
1001
    mRotationAxis.set( mAxis[axis] );
1002
    mRotationAngle.add(mRotationAngleStatic);
1003
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
1004
    }
1005
1006
///////////////////////////////////////////////////////////////////////////////////////////////////
1007
1008
  void setTextureMap(int cubit, int face, int newColor)
1009 29b82486 Leszek Koltunski
    {
1010 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
1011
    final float ratioH = 1.0f/mNumTexRows;
1012
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1013
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1014
    int col = newColor%mNumTexCols;
1015
1016
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1017
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1018 29b82486 Leszek Koltunski
    }
1019
1020
///////////////////////////////////////////////////////////////////////////////////////////////////
1021
1022 880beeea Leszek Koltunski
  void resetAllTextureMaps()
1023 29b82486 Leszek Koltunski
    {
1024 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
1025
    final float ratioH = 1.0f/mNumTexRows;
1026
    int color, row, col;
1027 29b82486 Leszek Koltunski
1028 7c111294 Leszek Koltunski
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1029 29b82486 Leszek Koltunski
      {
1030 7c111294 Leszek Koltunski
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1031 29b82486 Leszek Koltunski
1032 7c111294 Leszek Koltunski
      for(int cubitface=0; cubitface<mNumCubitFaces; cubitface++)
1033
        {
1034
        color = getFaceColor(cubit,cubitface,mNumLayers);
1035
        row = (mNumTexRows-1) - color/mNumTexCols;
1036
        col = color%mNumTexCols;
1037
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1038
        }
1039 29b82486 Leszek Koltunski
1040 7c111294 Leszek Koltunski
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1041
      }
1042 29b82486 Leszek Koltunski
    }
1043
1044
///////////////////////////////////////////////////////////////////////////////////////////////////
1045
1046 880beeea Leszek Koltunski
  void releaseResources()
1047 29b82486 Leszek Koltunski
    {
1048 7c111294 Leszek Koltunski
    mTexture.markForDeletion();
1049
    mMesh.markForDeletion();
1050
    mEffects.markForDeletion();
1051
1052
    for(int j=0; j<NUM_CUBITS; j++)
1053
      {
1054
      CUBITS[j].releaseResources();
1055
      }
1056 29b82486 Leszek Koltunski
    }
1057
1058
///////////////////////////////////////////////////////////////////////////////////////////////////
1059
1060 880beeea Leszek Koltunski
  synchronized void restorePreferences(SharedPreferences preferences)
1061 29b82486 Leszek Koltunski
    {
1062
    boolean error = false;
1063
1064
    for(int i=0; i<NUM_CUBITS; i++)
1065
      {
1066
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1067
1068
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1069
        {
1070
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1071
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1072
        }
1073
      else
1074
        {
1075
        error = true;
1076
        }
1077
      }
1078
1079
    if( error )
1080
      {
1081
      for(int i=0; i<NUM_CUBITS; i++)
1082
        {
1083
        CUBITS[i].solve();
1084
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1085
        }
1086
      recordQuatsState("Failed to restorePreferences");
1087
      }
1088
    }
1089
1090
///////////////////////////////////////////////////////////////////////////////////////////////////
1091
1092 880beeea Leszek Koltunski
  void savePreferences(SharedPreferences.Editor editor)
1093 29b82486 Leszek Koltunski
    {
1094 7c111294 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1095 29b82486 Leszek Koltunski
    }
1096
1097
///////////////////////////////////////////////////////////////////////////////////////////////////
1098
1099 880beeea Leszek Koltunski
  void recomputeScaleFactor(int scrWidth)
1100 29b82486 Leszek Koltunski
    {
1101 7c111294 Leszek Koltunski
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
1102 29b82486 Leszek Koltunski
    }
1103
1104
///////////////////////////////////////////////////////////////////////////////////////////////////
1105 7c111294 Leszek Koltunski
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1106 29b82486 Leszek Koltunski
1107 880beeea Leszek Koltunski
  void createTexture()
1108 29b82486 Leszek Koltunski
    {
1109 7c111294 Leszek Koltunski
    Bitmap bitmap;
1110 29b82486 Leszek Koltunski
1111 7c111294 Leszek Koltunski
    Paint paint = new Paint();
1112
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1113
    Canvas canvas = new Canvas(bitmap);
1114 29b82486 Leszek Koltunski
1115 7c111294 Leszek Koltunski
    paint.setAntiAlias(true);
1116
    paint.setTextAlign(Paint.Align.CENTER);
1117
    paint.setStyle(Paint.Style.FILL);
1118
1119
    paint.setColor(COLOR_BLACK);
1120
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1121
1122
    int face = 0;
1123
    FactorySticker factory = FactorySticker.getInstance();
1124
1125
    for(int row=0; row<mNumTexRows; row++)
1126
      for(int col=0; col<mNumTexCols; col++)
1127 29b82486 Leszek Koltunski
        {
1128 7c111294 Leszek Koltunski
        if( face>=NUM_TEXTURES ) break;
1129
        ObjectSticker sticker = retSticker(face);
1130
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, getColor(face%NUM_FACE_COLORS), sticker);
1131
        face++;
1132 29b82486 Leszek Koltunski
        }
1133
1134 7c111294 Leszek Koltunski
    if( !mTexture.setTexture(bitmap) )
1135
      {
1136
      int max = DistortedLibrary.getMaxTextureSize();
1137
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
1138
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
1139 29b82486 Leszek Koltunski
      }
1140
    }
1141
1142 ee6bb8d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1143
1144
  void setObjectRatioNow(float sc)
1145
    {
1146
    mObjectScreenRatio = sc;
1147
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
1148
    mObjectScale.set(scale,scale,scale);
1149
    }
1150
1151 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1152
1153 880beeea Leszek Koltunski
  void setObjectRatio(float sizeChange)
1154 29b82486 Leszek Koltunski
    {
1155 7c111294 Leszek Koltunski
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1156 29b82486 Leszek Koltunski
1157 7c111294 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1158
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1159
1160
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
1161
    mObjectScale.set(scale,scale,scale);
1162 29b82486 Leszek Koltunski
    }
1163
1164
///////////////////////////////////////////////////////////////////////////////////////////////////
1165
1166 880beeea Leszek Koltunski
  float getObjectRatio()
1167 29b82486 Leszek Koltunski
    {
1168 7c111294 Leszek Koltunski
    return mObjectScreenRatio*mInitScreenRatio;
1169
    }
1170 29b82486 Leszek Koltunski
1171 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1172
1173
  public float getRatio()
1174
    {
1175
    return mObjectScreenRatio;
1176
    }
1177
1178 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1179 29b82486 Leszek Koltunski
1180 880beeea Leszek Koltunski
  boolean isSolved()
1181 7c111294 Leszek Koltunski
    {
1182 880beeea Leszek Koltunski
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1183
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1184
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1185
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1186 7c111294 Leszek Koltunski
1187 880beeea Leszek Koltunski
    return false;
1188 29b82486 Leszek Koltunski
    }
1189
1190
///////////////////////////////////////////////////////////////////////////////////////////////////
1191 a57e6870 Leszek Koltunski
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1192
// during setting up the initial position in the solver.
1193 29b82486 Leszek Koltunski
1194 880beeea Leszek Koltunski
  int getCubit(float[] point3D)
1195 29b82486 Leszek Koltunski
    {
1196 880beeea Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
1197
    int currentBest=-1;
1198 a57e6870 Leszek Koltunski
    float multiplier = mNumLayers[0];
1199 880beeea Leszek Koltunski
1200
    point3D[0] *= multiplier;
1201
    point3D[1] *= multiplier;
1202
    point3D[2] *= multiplier;
1203
1204
    for(int i=0; i<NUM_CUBITS; i++)
1205
      {
1206
      dist = CUBITS[i].getDistSquared(point3D);
1207
      if( dist<minDist )
1208
        {
1209
        minDist = dist;
1210
        currentBest = i;
1211
        }
1212
      }
1213
1214
    return currentBest;
1215 7c111294 Leszek Koltunski
    }
1216 29b82486 Leszek Koltunski
1217 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1218 29b82486 Leszek Koltunski
1219 880beeea Leszek Koltunski
  int computeNearestAngle(int axis, float angle, float speed)
1220 7c111294 Leszek Koltunski
    {
1221 880beeea Leszek Koltunski
    int[] basicArray = getBasicAngle();
1222
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1223
    int nearestAngle = 360/basicAngle;
1224
1225
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1226
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1227
1228
    if( tmp!=0 ) return nearestAngle*tmp;
1229
1230
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1231 7c111294 Leszek Koltunski
    }
1232 29b82486 Leszek Koltunski
1233 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1234
1235 880beeea Leszek Koltunski
  float getCameraDist()
1236 7c111294 Leszek Koltunski
    {
1237 880beeea Leszek Koltunski
    return mCameraDist;
1238 29b82486 Leszek Koltunski
    }
1239
1240 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1241
// INTERNAL API - those are called from 'effects' package
1242 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1243
1244 880beeea Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1245 29b82486 Leszek Koltunski
    {
1246 880beeea Leszek Koltunski
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1247 7c111294 Leszek Koltunski
    }
1248 29b82486 Leszek Koltunski
1249 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1250 29b82486 Leszek Koltunski
1251 880beeea Leszek Koltunski
  public int getNodeSize()
1252 7c111294 Leszek Koltunski
    {
1253 880beeea Leszek Koltunski
    return mNodeSize;
1254 29b82486 Leszek Koltunski
    }
1255
1256
///////////////////////////////////////////////////////////////////////////////////////////////////
1257
1258 880beeea Leszek Koltunski
  public Static4D getRotationQuat()
1259 7c111294 Leszek Koltunski
      {
1260 880beeea Leszek Koltunski
      return mQuat;
1261 7c111294 Leszek Koltunski
      }
1262
1263 2df35810 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1264
1265
  public void apply(Effect effect, int position)
1266
    {
1267
    mEffects.apply(effect, position);
1268
    }
1269
1270
///////////////////////////////////////////////////////////////////////////////////////////////////
1271
1272
  public void remove(long effectID)
1273
    {
1274
    mEffects.abortById(effectID);
1275
    }
1276
1277 3e9df6aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1278
1279 758b028d Leszek Koltunski
  public MeshBase getObjectMesh()
1280 3e9df6aa Leszek Koltunski
    {
1281
    return mMesh;
1282
    }
1283
1284 758b028d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1285
1286
  public DistortedEffects getObjectEffects()
1287
    {
1288
    return mEffects;
1289
    }
1290
1291 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1292
// PUBLIC API
1293 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1294
1295 880beeea Leszek Koltunski
  public int getCubitFaceColorIndex(int cubit, int face)
1296 29b82486 Leszek Koltunski
    {
1297 880beeea Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1298 29b82486 Leszek Koltunski
1299 880beeea Leszek Koltunski
    int x = (int)(texMap.get0()/texMap.get2());
1300
    int y = (int)(texMap.get1()/texMap.get3());
1301 29b82486 Leszek Koltunski
1302 880beeea Leszek Koltunski
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1303 29b82486 Leszek Koltunski
    }
1304
1305
///////////////////////////////////////////////////////////////////////////////////////////////////
1306
1307 a57e6870 Leszek Koltunski
  public int[] getNumLayers()
1308 29b82486 Leszek Koltunski
    {
1309 880beeea Leszek Koltunski
    return mNumLayers;
1310
    }
1311 29b82486 Leszek Koltunski
1312
///////////////////////////////////////////////////////////////////////////////////////////////////
1313
1314 880beeea Leszek Koltunski
  public synchronized void solve()
1315 29b82486 Leszek Koltunski
    {
1316 880beeea Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
1317
      {
1318
      CUBITS[i].solve();
1319
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1320
      }
1321 29b82486 Leszek Koltunski
    }
1322
1323
///////////////////////////////////////////////////////////////////////////////////////////////////
1324
1325 c8bc83d9 Leszek Koltunski
  public ObjectType getObjectType()
1326 29b82486 Leszek Koltunski
    {
1327 c8bc83d9 Leszek Koltunski
    return intGetObjectType(mNumLayers);
1328 29b82486 Leszek Koltunski
    }
1329
1330
///////////////////////////////////////////////////////////////////////////////////////////////////
1331
1332 0310ac32 Leszek Koltunski
  protected abstract int getFOV();
1333
  protected abstract float getScreenRatio();
1334
  protected abstract int getNumFaceColors();
1335
  protected abstract int getColor(int face);
1336 a57e6870 Leszek Koltunski
  protected abstract float[][] getCuts(int[] numLayers);
1337 29b82486 Leszek Koltunski
  protected abstract int getNumCubitFaces();
1338
  protected abstract Static4D[] getQuats();
1339 a57e6870 Leszek Koltunski
  protected abstract float[][] getCubitPositions(int[] numLayers);
1340
  protected abstract int getCubitVariant(int cubit, int[] numLayers);
1341
  protected abstract int getNumCubitVariants(int[] numLayers);
1342
  protected abstract Static4D getQuat(int cubit, int[] numLayers);
1343
  protected abstract ObjectShape getObjectShape(int cubit, int[] numLayers);
1344
  protected abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1345 29b82486 Leszek Koltunski
  protected abstract int getSolvedFunctionIndex();
1346
  protected abstract ScrambleState[] getScrambleStates();
1347 a57e6870 Leszek Koltunski
  protected abstract int getNumStickerTypes(int[] numLayers);
1348 29b82486 Leszek Koltunski
  protected abstract ObjectSticker retSticker(int face);
1349 a57e6870 Leszek Koltunski
  protected abstract int getFaceColor(int cubit, int cubitface, int[] numLayers);
1350
  protected abstract int getResource(int[] numLayers);
1351
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1352 7c111294 Leszek Koltunski
  protected abstract Movement getMovement();
1353 29b82486 Leszek Koltunski
1354
  public abstract Static3D[] getRotationAxis();
1355
  public abstract int[] getBasicAngle();
1356 2289cab1 Leszek Koltunski
  public abstract int getNumFaces();
1357 a57e6870 Leszek Koltunski
  public abstract int getObjectName(int[] numLayers);
1358
  public abstract int getInventor(int[] numLayers);
1359
  public abstract int getComplexity(int[] numLayers);
1360 29b82486 Leszek Koltunski
  }