Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ e42a9e87

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