Project

General

Profile

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

magiccube / src / main / java / org / distorted / objectlib / TwistyObject.java @ 588ace55

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