Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 198c5bf0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objectlib.main;
21

    
22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27

    
28
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29

    
30
import org.distorted.library.effect.Effect;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectQuaternion;
35
import org.distorted.library.effect.VertexEffectRotate;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.main.QuatHelper;
41
import org.distorted.library.mesh.MeshBase;
42
import org.distorted.library.mesh.MeshFile;
43
import org.distorted.library.mesh.MeshJoined;
44
import org.distorted.library.mesh.MeshSquare;
45
import org.distorted.library.message.EffectListener;
46
import org.distorted.library.type.Dynamic1D;
47
import org.distorted.library.type.Static1D;
48
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50
import org.distorted.objectlib.BuildConfig;
51
import org.distorted.objectlib.helpers.FactoryCubit;
52
import org.distorted.objectlib.helpers.FactorySticker;
53
import org.distorted.objectlib.helpers.ObjectShape;
54
import org.distorted.objectlib.helpers.ObjectSticker;
55
import org.distorted.objectlib.helpers.ScrambleState;
56

    
57
import java.io.DataInputStream;
58
import java.io.IOException;
59
import java.io.InputStream;
60
import java.util.Random;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
public abstract class TwistyObject extends DistortedNode
65
  {
66
  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

    
76
  public static final int TEXTURE_HEIGHT = 256;
77
  static final int NUM_STICKERS_IN_ROW = 4;
78

    
79
  public static final float SQ2 = (float)Math.sqrt(2);
80
  public static final float SQ3 = (float)Math.sqrt(3);
81
  public static final float SQ5 = (float)Math.sqrt(5);
82
  public static final float SQ6 = (float)Math.sqrt(6);
83

    
84
  private static final float NODE_RATIO = 1.60f;
85
  private static final float MAX_SIZE_CHANGE = 1.35f;
86
  private static final float MIN_SIZE_CHANGE = 0.75f;
87

    
88
  private static final Static3D CENTER = new Static3D(0,0,0);
89
  private static final int POST_ROTATION_MILLISEC = 500;
90

    
91
  protected final int NUM_FACE_COLORS;
92
  protected final int NUM_TEXTURES;
93
  protected final Cubit[] CUBITS;
94

    
95
  MeshBase[] mMeshes;
96
  final Static4D[] OBJECT_QUATS;
97
  final int NUM_CUBITS;
98
  final int NUM_AXIS;
99
  final int NUM_QUATS;
100

    
101
  private final int mNumCubitFaces;
102
  private final Static3D[] mAxis;
103
  private final float[][] mCuts;
104
  private final int[] mNumCuts;
105
  private final int mNodeSize;
106
  private final float[][] mOrigPos;
107
  private final Static3D mNodeScale;
108
  private final Static4D mQuat;
109
  private final int mNumLayers, mRealSize;
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
  private final float mCameraDist;
117
  private final Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
118
  private final DistortedTexture mTexture;
119
  private final float mInitScreenRatio;
120
  private final int mSolvedFunctionIndex;
121
  private final boolean mIsBandaged;
122
  private float mObjectScreenRatio;
123
  private int[][] mSolvedQuats;
124
  private int[][] mQuatMult;
125
  private int[] mTmpQuats;
126
  private int mNumTexRows, mNumTexCols;
127
  private int mRotRowBitmap;
128
  private int mRotAxis;
129
  private MeshBase mMesh;
130
  private final TwistyObjectScrambler mScrambler;
131

    
132
  //////////////////// SOLVED1 ////////////////////////
133

    
134
  private int[] mFaceMap;
135
  private int[][] mScramble;
136
  private int[] mColors;
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  TwistyObject(int numLayers, int realSize, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
141
               DistortedEffects nodeEffects, int[][] moves, Resources res, int screenWidth)
142
    {
143
    super(nodeTexture,nodeEffects,nodeMesh);
144

    
145
    mNodeSize = screenWidth;
146

    
147
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
148

    
149
    mNumLayers = numLayers;
150
    mRealSize = realSize;
151
    mOrigPos = getCubitPositions(mNumLayers);
152
    mAxis = getRotationAxis();
153
    mInitScreenRatio = getScreenRatio();
154
    mObjectScreenRatio = 1.0f;
155
    mNumCubitFaces = getNumCubitFaces();
156
    mSolvedFunctionIndex = getSolvedFunctionIndex();
157

    
158
    mCuts = getCuts(mNumLayers);
159
    mNumCuts = new int[mAxis.length];
160
    if( mCuts==null ) for(int i=0; i<mAxis.length; i++) mNumCuts[i] = 0;
161
    else              for(int i=0; i<mAxis.length; i++) mNumCuts[i] = mCuts[i].length;
162

    
163
    OBJECT_QUATS = getQuats();
164
    NUM_CUBITS  = mOrigPos.length;
165
    NUM_FACE_COLORS = getNumFaceColors();
166
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
167
    NUM_AXIS = mAxis.length;
168
    NUM_QUATS = OBJECT_QUATS.length;
169

    
170
    int scramblingType = getScrambleType();
171
    ScrambleState[] states = getScrambleStates();
172
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,numLayers,states);
173

    
174
    boolean bandaged=false;
175

    
176
    for(int c=0; c<NUM_CUBITS; c++)
177
      {
178
      if( mOrigPos[c].length>3 )
179
        {
180
        bandaged=true;
181
        break;
182
        }
183
      }
184

    
185
    mIsBandaged = bandaged;
186

    
187
    mQuatDebug = new int[NUM_CUBITS];
188

    
189
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
190
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
191

    
192
    mNodeScale= new Static3D(screenWidth,NODE_RATIO*screenWidth,screenWidth);
193
    mQuat = quat;
194

    
195
    mRotationAngle= new Dynamic1D();
196
    mRotationAxis = new Static3D(1,0,0);
197
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
198

    
199
    mRotationAngleStatic = new Static1D(0);
200
    mRotationAngleMiddle = new Static1D(0);
201
    mRotationAngleFinal  = new Static1D(0);
202

    
203
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
204
    mObjectScale = new Static3D(scale,scale,scale);
205
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
206
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
207

    
208
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
209
    nodeEffects.apply(nodeScaleEffect);
210

    
211
    mNumTexCols = NUM_STICKERS_IN_ROW;
212
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
213

    
214
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
215

    
216
    CUBITS = new Cubit[NUM_CUBITS];
217
    createMeshAndCubits(res);
218
    createDataStructuresForSolved(numLayers);
219

    
220
    mTexture = new DistortedTexture();
221
    mEffects = new DistortedEffects();
222

    
223
    createTexture();
224

    
225
    for(int q=0; q<NUM_QUATS; q++)
226
      {
227
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
228
      vq.setMeshAssociation(0,q);
229
      mEffects.apply(vq);
230
      }
231

    
232
    mEffects.apply(mRotateEffect);
233
    mEffects.apply(quatEffect);
234
    mEffects.apply(scaleEffect);
235

    
236
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
237
    // order for the effect to be in front of the object, we need to set the center to be behind it.
238
    getMesh().setComponentCenter(0,0,0,-0.1f);
239

    
240
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
241

    
242
    setupPosition(moves);
243

    
244
    float fov = getFOV();
245
    double halfFOV = fov * (Math.PI/360);
246
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
247

    
248
    setProjection( fov, 0.1f);
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  private Static3D getPos(float[] origPos)
254
    {
255
    int len = origPos.length/3;
256
    float sumX = 0.0f;
257
    float sumY = 0.0f;
258
    float sumZ = 0.0f;
259

    
260
    for(int i=0; i<len; i++)
261
      {
262
      sumX += origPos[3*i  ];
263
      sumY += origPos[3*i+1];
264
      sumZ += origPos[3*i+2];
265
      }
266

    
267
    sumX /= len;
268
    sumY /= len;
269
    sumZ /= len;
270

    
271
    return new Static3D(sumX,sumY,sumZ);
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  private void createMeshAndCubits(Resources res)
277
    {
278
    int resourceID= getResource(mNumLayers);
279

    
280
    if( resourceID!=0 )
281
      {
282
      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
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
298
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
299
        }
300

    
301
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
302
      }
303
    else
304
      {
305
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
306

    
307
      for(int i=0; i<NUM_CUBITS; i++)
308
        {
309
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
310
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
311
        Static3D pos = getPos(mOrigPos[i]);
312
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
313
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
314
        }
315

    
316
      mMesh = new MeshJoined(cubitMesh);
317
      resetAllTextureMaps();
318
      }
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  private void createDataStructuresForSolved(int numLayers)
352
    {
353
    mTmpQuats = new int[NUM_QUATS];
354
    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

    
364
  private int getMultQuat(int index1, int index2)
365
    {
366
    if( mQuatMult==null )
367
      {
368
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
369

    
370
      for(int i=0; i<NUM_QUATS; i++)
371
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
372
      }
373

    
374
    if( mQuatMult[index1][index2]==-1 )
375
      {
376
      mQuatMult[index1][index2] = mulQuat(index1,index2);
377
      }
378

    
379
    return mQuatMult[index1][index2];
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383
// This is used to build internal data structures for the generic 'isSolved()'
384
//
385
// if this is an internal cubit (all faces black): return -1
386
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
387
// Color index, i.e. the index into the 'FACE_COLORS' table.
388
// else (edge or corner cubit, more than one non-black face): return -2.
389

    
390
  protected int retCubitSolvedStatus(int cubit, int numLayers)
391
    {
392
    int numNonBlack=0, nonBlackIndex=-1, color;
393

    
394
    for(int face=0; face<mNumCubitFaces; face++)
395
      {
396
      color = getFaceColor(cubit,face,numLayers);
397

    
398
      if( color<NUM_TEXTURES )
399
        {
400
        numNonBlack++;
401
        nonBlackIndex = color%NUM_FACE_COLORS;
402
        }
403
      }
404

    
405
    if( numNonBlack==0 ) return -1;
406
    if( numNonBlack>=2 ) return -2;
407

    
408
    return nonBlackIndex;
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
  protected boolean shouldResetTextureMaps()
414
    {
415
    return false;
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
421
    {
422
    final float MAXD = 0.0001f;
423
    float x = faceAx.get0();
424
    float y = faceAx.get1();
425
    float z = faceAx.get2();
426
    float a,dx,dy,dz,qx,qy,qz;
427
    Static4D quat;
428

    
429
    int len = quats.length;
430
    int place = 0;
431

    
432
    for(int q=1; q<len; q++)
433
      {
434
      quat = quats[q];
435
      qx = quat.get0();
436
      qy = quat.get1();
437
      qz = quat.get2();
438

    
439
           if( x!=0.0f ) { a = qx/x; }
440
      else if( y!=0.0f ) { a = qy/y; }
441
      else               { a = qz/z; }
442

    
443
      dx = a*x-qx;
444
      dy = a*y-qy;
445
      dz = a*z-qz;
446

    
447
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
448
        {
449
        mTmpQuats[place++] = q;
450
        }
451
      }
452

    
453
    if( place!=0 )
454
      {
455
      int[] ret = new int[place];
456
      System.arraycopy(mTmpQuats,0,ret,0,place);
457
      return ret;
458
      }
459

    
460
    return null;
461
    }
462

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464

    
465
  private boolean isSolved0()
466
    {
467
    int len, q1,q = CUBITS[0].mQuatIndex;
468
    int[] solved;
469
    boolean skip;
470

    
471
    for(int c=1; c<NUM_CUBITS; c++)
472
      {
473
      q1 = CUBITS[c].mQuatIndex;
474

    
475
      if( q1==q ) continue;
476

    
477
      skip = false;
478
      solved = mSolvedQuats[c];
479
      len = solved==null ? 0:solved.length;
480

    
481
      for(int i=0; i<len; i++)
482
        {
483
        if( q1==getMultQuat(q,solved[i]) )
484
          {
485
          skip = true;
486
          break;
487
          }
488
        }
489

    
490
      if( !skip ) return false;
491
      }
492

    
493
    return true;
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  private int computeScramble(int quatNum, int centerNum)
499
    {
500
    float MAXDIFF = 0.01f;
501
    float[] center= mOrigPos[centerNum];
502
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
503
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
504

    
505
    float x = result.get0();
506
    float y = result.get1();
507
    float z = result.get2();
508

    
509
    for(int c=0; c<NUM_CUBITS; c++)
510
      {
511
      float[] cent = mOrigPos[c];
512

    
513
      float qx = cent[0] - x;
514
      float qy = cent[1] - y;
515
      float qz = cent[2] - z;
516

    
517
      if( qx>-MAXDIFF && qx<MAXDIFF &&
518
          qy>-MAXDIFF && qy<MAXDIFF &&
519
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
520
      }
521

    
522
    return -1;
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526
// Dino4 uses this. It is solved if and only if groups of cubits
527
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
528
// or
529
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
530
// are all the same color.
531

    
532
  private boolean isSolved1()
533
    {
534
    if( mScramble==null )
535
      {
536
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
537
      mColors   = new int[NUM_CUBITS];
538

    
539
      for(int q=0; q<NUM_QUATS; q++)
540
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
541
      }
542

    
543
    if( mFaceMap==null )
544
      {
545
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
546
      }
547

    
548
    for(int c=0; c<NUM_CUBITS; c++)
549
      {
550
      int index = mScramble[CUBITS[c].mQuatIndex][c];
551
      mColors[index] = mFaceMap[c];
552
      }
553

    
554
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
555
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
556
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
557

    
558
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
559
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
560
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
561

    
562
    return false;
563
    }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566
// Dino6 uses this. It is solved if and only if:
567
//
568
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
569
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
570
// by the same qZ, and then either:
571
//
572
// a) qX = qY = qZ
573
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
574
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
575
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
576
//
577
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
578
//
579
// X cubits: 0, 2, 8, 10
580
// Y cubits: 1, 3, 9, 11
581
// Z cubits: 4, 5, 6, 7
582

    
583
  private boolean isSolved2()
584
    {
585
    int qX = CUBITS[0].mQuatIndex;
586
    int qY = CUBITS[1].mQuatIndex;
587
    int qZ = CUBITS[4].mQuatIndex;
588

    
589
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
590
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
591
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
592
      {
593
      return false;
594
      }
595

    
596
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
597
    }
598

    
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600
// Square-2 is solved iff
601
// a) all of its cubits are rotated with the same quat
602
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
603
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
604
// and all the 12 left and right edges and corners also with the same quat multiplied by
605
// QUATS[12] - i.e. also upside down.
606

    
607
  private boolean isSolved3()
608
    {
609
    int index = CUBITS[0].mQuatIndex;
610

    
611
    if( CUBITS[1].mQuatIndex!=index ) return false;
612

    
613
    boolean solved = true;
614

    
615
    for(int i=2; i<NUM_CUBITS; i++)
616
      {
617
      if( CUBITS[i].mQuatIndex!=index )
618
        {
619
        solved = false;
620
        break;
621
        }
622
      }
623

    
624
    if( solved ) return true;
625

    
626
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
627
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
628

    
629
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
630
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
631
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
632
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
633

    
634
    return true;
635
    }
636

    
637
///////////////////////////////////////////////////////////////////////////////////////////////////
638

    
639
  int computeRow(float[] pos, int axisIndex)
640
    {
641
    int ret=0;
642
    int len = pos.length / 3;
643
    Static3D axis = mAxis[axisIndex];
644
    float axisX = axis.get0();
645
    float axisY = axis.get1();
646
    float axisZ = axis.get2();
647
    float casted;
648

    
649
    for(int i=0; i<len; i++)
650
      {
651
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
652
      ret |= computeSingleRow(axisIndex,casted);
653
      }
654

    
655
    return ret;
656
    }
657

    
658
///////////////////////////////////////////////////////////////////////////////////////////////////
659

    
660
  private int computeSingleRow(int axisIndex,float casted)
661
    {
662
    int num = mNumCuts[axisIndex];
663

    
664
    for(int i=0; i<num; i++)
665
      {
666
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
667
      }
668

    
669
    return (1<<num);
670
    }
671

    
672
///////////////////////////////////////////////////////////////////////////////////////////////////
673

    
674
  private boolean wasRotateApplied()
675
    {
676
    return mEffects.exists(mRotateEffect.getID());
677
    }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680

    
681
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
682
    {
683
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
684
    }
685

    
686
///////////////////////////////////////////////////////////////////////////////////////////////////
687
// note the minus in front of the sin() - we rotate counterclockwise
688
// when looking towards the direction where the axis increases in values.
689

    
690
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
691
    {
692
    Static3D axis = mAxis[axisIndex];
693

    
694
    while( angleInDegrees<0 ) angleInDegrees += 360;
695
    angleInDegrees %= 360;
696
    
697
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
698
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
699

    
700
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
701
    }
702

    
703
///////////////////////////////////////////////////////////////////////////////////////////////////
704

    
705
  private synchronized void setupPosition(int[][] moves)
706
    {
707
    if( moves!=null )
708
      {
709
      Static4D quat;
710
      int index, axis, rowBitmap, angle;
711
      int[] basic = getBasicAngle();
712

    
713
      for(int[] move: moves)
714
        {
715
        axis     = move[0];
716
        rowBitmap= move[1];
717
        angle    = move[2]*(360/basic[axis]);
718
        quat     = makeQuaternion(axis,angle);
719

    
720
        for(int j=0; j<NUM_CUBITS; j++)
721
          if( belongsToRotation(j,axis,rowBitmap) )
722
            {
723
            index = CUBITS[j].removeRotationNow(quat);
724
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
725
            }
726
        }
727
      }
728
    }
729

    
730
///////////////////////////////////////////////////////////////////////////////////////////////////
731

    
732
  protected int getScrambleType()
733
    {
734
    return 0;
735
    }
736

    
737
///////////////////////////////////////////////////////////////////////////////////////////////////
738

    
739
  int computeBitmapFromRow(int rowBitmap, int axis)
740
    {
741
    if( mIsBandaged )
742
      {
743
      int bitmap, initBitmap=0;
744

    
745
      while( initBitmap!=rowBitmap )
746
        {
747
        initBitmap = rowBitmap;
748

    
749
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
750
          {
751
          bitmap = CUBITS[cubit].getRotRow(axis);
752
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
753
          }
754
        }
755
      }
756

    
757
    return rowBitmap;
758
    }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
762
// Do so only if minimal Error is appropriately low (shape-shifting puzzles - Square-1)
763

    
764
  void clampPos(float[] pos, int offset)
765
    {
766
    float currError, minError = Float.MAX_VALUE;
767
    int minErrorIndex1 = -1;
768
    int minErrorIndex2 = -1;
769

    
770
    float x = pos[offset  ];
771
    float y = pos[offset+1];
772
    float z = pos[offset+2];
773

    
774
    float xo,yo,zo;
775

    
776
    for(int i=0; i<NUM_CUBITS; i++)
777
      {
778
      int len = mOrigPos[i].length / 3;
779

    
780
      for(int j=0; j<len; j++)
781
        {
782
        xo = mOrigPos[i][3*j  ];
783
        yo = mOrigPos[i][3*j+1];
784
        zo = mOrigPos[i][3*j+2];
785

    
786
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
787

    
788
        if( currError<minError )
789
          {
790
          minError = currError;
791
          minErrorIndex1 = i;
792
          minErrorIndex2 = j;
793
          }
794
        }
795
      }
796

    
797
    if( minError< 0.1f ) // TODO: 0.1 ?
798
      {
799
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
800
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
801
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
802
      }
803
    }
804

    
805
///////////////////////////////////////////////////////////////////////////////////////////////////
806
// remember about the double cover or unit quaternions!
807

    
808
  int mulQuat(int q1, int q2)
809
    {
810
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
811

    
812
    float rX = result.get0();
813
    float rY = result.get1();
814
    float rZ = result.get2();
815
    float rW = result.get3();
816

    
817
    final float MAX_ERROR = 0.1f;
818
    float dX,dY,dZ,dW;
819

    
820
    for(int i=0; i<NUM_QUATS; i++)
821
      {
822
      dX = OBJECT_QUATS[i].get0() - rX;
823
      dY = OBJECT_QUATS[i].get1() - rY;
824
      dZ = OBJECT_QUATS[i].get2() - rZ;
825
      dW = OBJECT_QUATS[i].get3() - rW;
826

    
827
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
828
          dY<MAX_ERROR && dY>-MAX_ERROR &&
829
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
830
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
831

    
832
      dX = OBJECT_QUATS[i].get0() + rX;
833
      dY = OBJECT_QUATS[i].get1() + rY;
834
      dZ = OBJECT_QUATS[i].get2() + rZ;
835
      dW = OBJECT_QUATS[i].get3() + rW;
836

    
837
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
838
          dY<MAX_ERROR && dY>-MAX_ERROR &&
839
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
840
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
841
      }
842

    
843
    return -1;
844
    }
845

    
846
///////////////////////////////////////////////////////////////////////////////////////////////////
847

    
848
  private float getAngle()
849
    {
850
    int pointNum = mRotationAngle.getNumPoints();
851

    
852
    if( pointNum>=1 )
853
      {
854
      return mRotationAngle.getPoint(pointNum-1).get0();
855
      }
856
    else
857
      {
858
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
859
      crashlytics.log("points in RotationAngle: "+pointNum);
860
      return 0;
861
      }
862
    }
863

    
864
///////////////////////////////////////////////////////////////////////////////////////////////////
865

    
866
  private void recordQuatsState(String message)
867
    {
868
    StringBuilder quats = new StringBuilder();
869

    
870
    for(int j=0; j<NUM_CUBITS; j++)
871
      {
872
      quats.append(mQuatDebug[j]);
873
      quats.append(" ");
874
      }
875

    
876
    String name = intGetObjectType(mNumLayers).name();
877

    
878
    if( BuildConfig.DEBUG )
879
      {
880
      android.util.Log.e("quats" , quats.toString());
881
      android.util.Log.e("object", name);
882
      }
883
    else
884
      {
885
      Exception ex = new Exception(message);
886
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
887
      crashlytics.setCustomKey("quats" , quats.toString());
888
      crashlytics.setCustomKey("object", name);
889
      crashlytics.recordException(ex);
890
      }
891
    }
892

    
893
///////////////////////////////////////////////////////////////////////////////////////////////////
894
// PUBLIC API
895
///////////////////////////////////////////////////////////////////////////////////////////////////
896

    
897
  public boolean isSolved()
898
    {
899
    if( mSolvedFunctionIndex==0 ) return isSolved0();
900
    if( mSolvedFunctionIndex==1 ) return isSolved1();
901
    if( mSolvedFunctionIndex==2 ) return isSolved2();
902
    if( mSolvedFunctionIndex==3 ) return isSolved3();
903

    
904
    return false;
905
    }
906

    
907
///////////////////////////////////////////////////////////////////////////////////////////////////
908
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
909

    
910
  public void createTexture()
911
    {
912
    Bitmap bitmap;
913

    
914
    Paint paint = new Paint();
915
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
916
    Canvas canvas = new Canvas(bitmap);
917

    
918
    paint.setAntiAlias(true);
919
    paint.setTextAlign(Paint.Align.CENTER);
920
    paint.setStyle(Paint.Style.FILL);
921

    
922
    paint.setColor(COLOR_BLACK);
923
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
924

    
925
    int face = 0;
926
    FactorySticker factory = FactorySticker.getInstance();
927

    
928
    for(int row=0; row<mNumTexRows; row++)
929
      for(int col=0; col<mNumTexCols; col++)
930
        {
931
        if( face>=NUM_TEXTURES ) break;
932
        ObjectSticker sticker = retSticker(face);
933
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, getColor(face%NUM_FACE_COLORS), sticker);
934
        face++;
935
        }
936

    
937
    if( !mTexture.setTexture(bitmap) )
938
      {
939
      int max = DistortedLibrary.getMaxTextureSize();
940
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
941
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
942
      }
943
    }
944

    
945
///////////////////////////////////////////////////////////////////////////////////////////////////
946

    
947
  public void setObjectRatio(float sizeChange)
948
    {
949
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
950

    
951
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
952
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
953

    
954
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
955
    mObjectScale.set(scale,scale,scale);
956
    }
957

    
958
///////////////////////////////////////////////////////////////////////////////////////////////////
959

    
960
  public float getObjectRatio()
961
    {
962
    return mObjectScreenRatio*mInitScreenRatio;
963
    }
964

    
965
///////////////////////////////////////////////////////////////////////////////////////////////////
966

    
967
  public int getCubitFaceColorIndex(int cubit, int face)
968
    {
969
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
970

    
971
    int x = (int)(texMap.get0()/texMap.get2());
972
    int y = (int)(texMap.get1()/texMap.get3());
973

    
974
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
975
    }
976

    
977
///////////////////////////////////////////////////////////////////////////////////////////////////
978

    
979
  public int getNumLayers()
980
    {
981
    return mNumLayers;
982
    }
983

    
984
///////////////////////////////////////////////////////////////////////////////////////////////////
985

    
986
  public void continueRotation(float angleInDegrees)
987
    {
988
    mRotationAngleStatic.set0(angleInDegrees);
989
    }
990

    
991
///////////////////////////////////////////////////////////////////////////////////////////////////
992

    
993
  public Static4D getRotationQuat()
994
      {
995
      return mQuat;
996
      }
997

    
998
///////////////////////////////////////////////////////////////////////////////////////////////////
999

    
1000
  public void recomputeScaleFactor(int scrWidth)
1001
    {
1002
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
1003
    }
1004

    
1005
///////////////////////////////////////////////////////////////////////////////////////////////////
1006

    
1007
  public void savePreferences(SharedPreferences.Editor editor)
1008
    {
1009
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1010
    }
1011

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////
1013

    
1014
  public synchronized void restorePreferences(SharedPreferences preferences)
1015
    {
1016
    boolean error = false;
1017

    
1018
    for(int i=0; i<NUM_CUBITS; i++)
1019
      {
1020
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1021

    
1022
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1023
        {
1024
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1025
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1026
        }
1027
      else
1028
        {
1029
        error = true;
1030
        }
1031
      }
1032

    
1033
    if( error )
1034
      {
1035
      for(int i=0; i<NUM_CUBITS; i++)
1036
        {
1037
        CUBITS[i].solve();
1038
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1039
        }
1040
      recordQuatsState("Failed to restorePreferences");
1041
      }
1042
    }
1043

    
1044
///////////////////////////////////////////////////////////////////////////////////////////////////
1045

    
1046
  public void releaseResources()
1047
    {
1048
    mTexture.markForDeletion();
1049
    mMesh.markForDeletion();
1050
    mEffects.markForDeletion();
1051

    
1052
    for(int j=0; j<NUM_CUBITS; j++)
1053
      {
1054
      CUBITS[j].releaseResources();
1055
      }
1056
    }
1057

    
1058
///////////////////////////////////////////////////////////////////////////////////////////////////
1059

    
1060
  public void apply(Effect effect, int position)
1061
    {
1062
    mEffects.apply(effect, position);
1063
    }
1064

    
1065
///////////////////////////////////////////////////////////////////////////////////////////////////
1066

    
1067
  public void remove(long effectID)
1068
    {
1069
    mEffects.abortById(effectID);
1070
    }
1071

    
1072
///////////////////////////////////////////////////////////////////////////////////////////////////
1073

    
1074
  public synchronized void solve()
1075
    {
1076
    for(int i=0; i<NUM_CUBITS; i++)
1077
      {
1078
      CUBITS[i].solve();
1079
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1080
      }
1081
    }
1082

    
1083
///////////////////////////////////////////////////////////////////////////////////////////////////
1084

    
1085
  public void resetAllTextureMaps()
1086
    {
1087
    final float ratioW = 1.0f/mNumTexCols;
1088
    final float ratioH = 1.0f/mNumTexRows;
1089
    int color, row, col;
1090

    
1091
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1092
      {
1093
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1094

    
1095
      for(int cubitface=0; cubitface<mNumCubitFaces; cubitface++)
1096
        {
1097
        color = getFaceColor(cubit,cubitface,mNumLayers);
1098
        row = (mNumTexRows-1) - color/mNumTexCols;
1099
        col = color%mNumTexCols;
1100
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1101
        }
1102

    
1103
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1104
      }
1105
    }
1106

    
1107
///////////////////////////////////////////////////////////////////////////////////////////////////
1108

    
1109
  public void setTextureMap(int cubit, int face, int newColor)
1110
    {
1111
    final float ratioW = 1.0f/mNumTexCols;
1112
    final float ratioH = 1.0f/mNumTexRows;
1113
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1114
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1115
    int col = newColor%mNumTexCols;
1116

    
1117
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1118
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1119
    }
1120

    
1121
///////////////////////////////////////////////////////////////////////////////////////////////////
1122

    
1123
  public synchronized void beginNewRotation(int axis, int row )
1124
    {
1125
    if( axis<0 || axis>=NUM_AXIS )
1126
      {
1127
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1128
      return;
1129
      }
1130
    if( row<0 || row>=mNumLayers )
1131
      {
1132
      android.util.Log.e("object", "invalid rotation row: "+row);
1133
      return;
1134
      }
1135

    
1136
    mRotAxis     = axis;
1137
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1138
    mRotationAngleStatic.set0(0.0f);
1139
    mRotationAxis.set( mAxis[axis] );
1140
    mRotationAngle.add(mRotationAngleStatic);
1141
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
1142
    }
1143

    
1144
///////////////////////////////////////////////////////////////////////////////////////////////////
1145

    
1146
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1147
    {
1148
    if( wasRotateApplied() )
1149
      {
1150
      mRotAxis     = axis;
1151
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1152

    
1153
      mRotationAngleStatic.set0(0.0f);
1154
      mRotationAxis.set( mAxis[axis] );
1155
      mRotationAngle.setDuration(durationMillis);
1156
      mRotationAngle.resetToBeginning();
1157
      mRotationAngle.add(new Static1D(0));
1158
      mRotationAngle.add(new Static1D(angle));
1159
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
1160
      mRotateEffect.notifyWhenFinished(listener);
1161

    
1162
      return mRotateEffect.getID();
1163
      }
1164

    
1165
    return 0;
1166
    }
1167

    
1168
///////////////////////////////////////////////////////////////////////////////////////////////////
1169

    
1170
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1171
    {
1172
    if( wasRotateApplied() )
1173
      {
1174
      float angle = getAngle();
1175
      mRotationAngleStatic.set0(angle);
1176
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1177
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1178

    
1179
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1180
      mRotationAngle.resetToBeginning();
1181
      mRotationAngle.removeAll();
1182
      mRotationAngle.add(mRotationAngleStatic);
1183
      mRotationAngle.add(mRotationAngleMiddle);
1184
      mRotationAngle.add(mRotationAngleFinal);
1185
      mRotateEffect.notifyWhenFinished(listener);
1186

    
1187
      return mRotateEffect.getID();
1188
      }
1189

    
1190
    return 0;
1191
    }
1192

    
1193
///////////////////////////////////////////////////////////////////////////////////////////////////
1194

    
1195
  public synchronized void removeRotationNow()
1196
    {
1197
    float angle = getAngle();
1198
    double nearestAngleInRadians = angle*Math.PI/180;
1199
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1200
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1201
    float axisX = mAxis[mRotAxis].get0();
1202
    float axisY = mAxis[mRotAxis].get1();
1203
    float axisZ = mAxis[mRotAxis].get2();
1204
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1205

    
1206
    mRotationAngle.removeAll();
1207
    mRotationAngleStatic.set0(0);
1208

    
1209
    for(int i=0; i<NUM_CUBITS; i++)
1210
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
1211
        {
1212
        int index = CUBITS[i].removeRotationNow(quat);
1213
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
1214
        }
1215
    }
1216

    
1217
///////////////////////////////////////////////////////////////////////////////////////////////////
1218

    
1219
  public void initializeObject(int[][] moves)
1220
    {
1221
    solve();
1222
    setupPosition(moves);
1223
    }
1224

    
1225
///////////////////////////////////////////////////////////////////////////////////////////////////
1226

    
1227
  public int getCubit(float[] point3D)
1228
    {
1229
    float dist, minDist = Float.MAX_VALUE;
1230
    int currentBest=-1;
1231
    float multiplier = returnMultiplier();
1232

    
1233
    point3D[0] *= multiplier;
1234
    point3D[1] *= multiplier;
1235
    point3D[2] *= multiplier;
1236

    
1237
    for(int i=0; i<NUM_CUBITS; i++)
1238
      {
1239
      dist = CUBITS[i].getDistSquared(point3D);
1240
      if( dist<minDist )
1241
        {
1242
        minDist = dist;
1243
        currentBest = i;
1244
        }
1245
      }
1246

    
1247
    return currentBest;
1248
    }
1249

    
1250
///////////////////////////////////////////////////////////////////////////////////////////////////
1251

    
1252
  public int computeNearestAngle(int axis, float angle, float speed)
1253
    {
1254
    int[] basicArray = getBasicAngle();
1255
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1256
    int nearestAngle = 360/basicAngle;
1257

    
1258
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1259
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1260

    
1261
    if( tmp!=0 ) return nearestAngle*tmp;
1262

    
1263
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1264
    }
1265

    
1266
///////////////////////////////////////////////////////////////////////////////////////////////////
1267

    
1268
  public float getCameraDist()
1269
    {
1270
    return mCameraDist;
1271
    }
1272

    
1273
///////////////////////////////////////////////////////////////////////////////////////////////////
1274

    
1275
  public int getNodeSize()
1276
    {
1277
    return mNodeSize;
1278
    }
1279

    
1280
///////////////////////////////////////////////////////////////////////////////////////////////////
1281

    
1282
  public ObjectType getObjectType()
1283
    {
1284
    return intGetObjectType(mNumLayers);
1285
    }
1286

    
1287
///////////////////////////////////////////////////////////////////////////////////////////////////
1288

    
1289
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1290
    {
1291
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1292
    }
1293

    
1294
///////////////////////////////////////////////////////////////////////////////////////////////////
1295

    
1296
  protected abstract int getFOV();
1297
  protected abstract float returnMultiplier();
1298
  protected abstract float getScreenRatio();
1299
  protected abstract int getNumFaceColors();
1300
  protected abstract int getColor(int face);
1301
  protected abstract float[][] getCuts(int numLayers);
1302
  protected abstract int getNumCubitFaces();
1303
  protected abstract Static4D[] getQuats();
1304
  protected abstract float[][] getCubitPositions(int numLayers);
1305
  protected abstract int getCubitVariant(int cubit, int numLayers);
1306
  protected abstract int getNumCubitVariants(int numLayers);
1307
  protected abstract Static4D getQuat(int cubit, int numLayers);
1308
  protected abstract ObjectShape getObjectShape(int cubit, int numLayers);
1309
  protected abstract int[] getSolvedQuats(int cubit, int numLayers);
1310
  protected abstract int getSolvedFunctionIndex();
1311
  protected abstract ScrambleState[] getScrambleStates();
1312
  protected abstract int getNumStickerTypes(int numLayers);
1313
  protected abstract ObjectSticker retSticker(int face);
1314
  protected abstract int getFaceColor(int cubit, int cubitface, int numLayers);
1315
  protected abstract int getResource(int mNumLayers);
1316
  protected abstract ObjectType intGetObjectType(int numLayers);
1317

    
1318
  public abstract Movement getMovement();
1319
  public abstract Static3D[] getRotationAxis();
1320
  public abstract int[] getBasicAngle();
1321
  public abstract int getObjectName(int numLayers);
1322
  public abstract int getInventor(int numLayers);
1323
  public abstract int getComplexity(int numLayers);
1324
  }
(12-12/13)