Project

General

Profile

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

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

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