Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 96208efc

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