Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ c8bc83d9

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

    
52
import java.io.DataInputStream;
53
import java.io.IOException;
54
import java.io.InputStream;
55
import java.util.Random;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
public abstract class TwistyObject extends DistortedNode
60
  {
61
  public static final int COLOR_YELLOW = 0xffffff00;
62
  public static final int COLOR_WHITE  = 0xffffffff;
63
  public static final int COLOR_BLUE   = 0xff0000ff;
64
  public static final int COLOR_GREEN  = 0xff00bb00;
65
  public static final int COLOR_RED    = 0xff990000;
66
  public static final int COLOR_ORANGE = 0xffff6200;
67
  public static final int COLOR_GREY   = 0xff727c7b;
68
  public static final int COLOR_VIOLET = 0xff7700bb;
69
  public static final int COLOR_BLACK  = 0xff000000;
70

    
71
  public static final int TEXTURE_HEIGHT = 256;
72
  static final int NUM_STICKERS_IN_ROW = 4;
73

    
74
  public static final float SQ2 = (float)Math.sqrt(2);
75
  public static final float SQ3 = (float)Math.sqrt(3);
76
  public static final float SQ5 = (float)Math.sqrt(5);
77
  public static final float SQ6 = (float)Math.sqrt(6);
78

    
79
  private static final float NODE_RATIO = 1.60f;
80
  private static final float MAX_SIZE_CHANGE = 1.35f;
81
  private static final float MIN_SIZE_CHANGE = 0.75f;
82

    
83
  private static final Static3D CENTER = new Static3D(0,0,0);
84
  private static final int POST_ROTATION_MILLISEC = 500;
85

    
86
  protected final int NUM_FACE_COLORS;
87
  protected final int NUM_TEXTURES;
88
  protected final Cubit[] CUBITS;
89

    
90
  MeshBase[] mMeshes;
91
  final Static4D[] OBJECT_QUATS;
92
  final int NUM_CUBITS;
93
  final int NUM_AXIS;
94
  final int NUM_QUATS;
95

    
96
  private final int mNumCubitFaces;
97
  private final Static3D[] mAxis;
98
  private final float[][] mCuts;
99
  private final int[] mNumCuts;
100
  private final int mNodeSize;
101
  private final float[][] mOrigPos;
102
  private final Static3D mNodeScale;
103
  private final Static4D mQuat;
104
  private final int mNumLayers, mRealSize;
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
  private final float mCameraDist;
112
  private final Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
113
  private final DistortedTexture mTexture;
114
  private final float mInitScreenRatio;
115
  private final int mSolvedFunctionIndex;
116
  private final boolean mIsBandaged;
117
  private float mObjectScreenRatio;
118
  private int[][] mSolvedQuats;
119
  private int[][] mQuatMult;
120
  private int[] mTmpQuats;
121
  private int mNumTexRows, mNumTexCols;
122
  private int mRotRowBitmap;
123
  private int mRotAxis;
124
  private MeshBase mMesh;
125
  private final TwistyObjectScrambler mScrambler;
126

    
127
  //////////////////// SOLVED1 ////////////////////////
128

    
129
  private int[] mFaceMap;
130
  private int[][] mScramble;
131
  private int[] mColors;
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  TwistyObject(int numLayers, int realSize, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
136
               DistortedEffects nodeEffects, int[][] moves, Resources res, int screenWidth)
137
    {
138
    super(nodeTexture,nodeEffects,nodeMesh);
139

    
140
    mNodeSize = screenWidth;
141

    
142
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
143

    
144
    mNumLayers = numLayers;
145
    mRealSize = realSize;
146
    mOrigPos = getCubitPositions(mNumLayers);
147
    mAxis = getRotationAxis();
148
    mInitScreenRatio = getScreenRatio();
149
    mObjectScreenRatio = 1.0f;
150
    mNumCubitFaces = getNumCubitFaces();
151
    mSolvedFunctionIndex = getSolvedFunctionIndex();
152

    
153
    mCuts = getCuts(mNumLayers);
154
    mNumCuts = new int[mAxis.length];
155
    if( mCuts==null ) for(int i=0; i<mAxis.length; i++) mNumCuts[i] = 0;
156
    else              for(int i=0; i<mAxis.length; i++) mNumCuts[i] = mCuts[i].length;
157

    
158
    OBJECT_QUATS = getQuats();
159
    NUM_CUBITS  = mOrigPos.length;
160
    NUM_FACE_COLORS = getNumFaceColors();
161
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
162
    NUM_AXIS = mAxis.length;
163
    NUM_QUATS = OBJECT_QUATS.length;
164

    
165
    int scramblingType = getScrambleType();
166
    ScrambleState[] states = getScrambleStates();
167
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,numLayers,states);
168

    
169
    boolean bandaged=false;
170

    
171
    for(int c=0; c<NUM_CUBITS; c++)
172
      {
173
      if( mOrigPos[c].length>3 )
174
        {
175
        bandaged=true;
176
        break;
177
        }
178
      }
179

    
180
    mIsBandaged = bandaged;
181

    
182
    mQuatDebug = new int[NUM_CUBITS];
183

    
184
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
185
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
186

    
187
    mNodeScale= new Static3D(screenWidth,NODE_RATIO*screenWidth,screenWidth);
188
    mQuat = quat;
189

    
190
    mRotationAngle= new Dynamic1D();
191
    mRotationAxis = new Static3D(1,0,0);
192
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
193

    
194
    mRotationAngleStatic = new Static1D(0);
195
    mRotationAngleMiddle = new Static1D(0);
196
    mRotationAngleFinal  = new Static1D(0);
197

    
198
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
199
    mObjectScale = new Static3D(scale,scale,scale);
200
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
201
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(quat, CENTER);
202

    
203
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
204
    nodeEffects.apply(nodeScaleEffect);
205

    
206
    mNumTexCols = NUM_STICKERS_IN_ROW;
207
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
208

    
209
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
210

    
211
    CUBITS = new Cubit[NUM_CUBITS];
212
    createMeshAndCubits(res);
213
    createDataStructuresForSolved(numLayers);
214

    
215
    mTexture = new DistortedTexture();
216
    mEffects = new DistortedEffects();
217

    
218
    createTexture();
219

    
220
    for(int q=0; q<NUM_QUATS; q++)
221
      {
222
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
223
      vq.setMeshAssociation(0,q);
224
      mEffects.apply(vq);
225
      }
226

    
227
    mEffects.apply(mRotateEffect);
228
    mEffects.apply(quatEffect);
229
    mEffects.apply(scaleEffect);
230

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

    
235
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
236

    
237
    setupPosition(moves);
238

    
239
    float fov = getFOV();
240
    double halfFOV = fov * (Math.PI/360);
241
    mCameraDist = 0.5f*NODE_RATIO / (float)Math.tan(halfFOV);
242

    
243
    setProjection( fov, 0.1f);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  private Static3D getPos(float[] origPos)
249
    {
250
    int len = origPos.length/3;
251
    float sumX = 0.0f;
252
    float sumY = 0.0f;
253
    float sumZ = 0.0f;
254

    
255
    for(int i=0; i<len; i++)
256
      {
257
      sumX += origPos[3*i  ];
258
      sumY += origPos[3*i+1];
259
      sumZ += origPos[3*i+2];
260
      }
261

    
262
    sumX /= len;
263
    sumY /= len;
264
    sumZ /= len;
265

    
266
    return new Static3D(sumX,sumY,sumZ);
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  private void createMeshAndCubits(Resources res)
272
    {
273
    int resourceID= getResource(mNumLayers);
274

    
275
    if( resourceID!=0 )
276
      {
277
      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
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
293
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
294
        }
295

    
296
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
297
      }
298
    else
299
      {
300
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
301

    
302
      for(int i=0; i<NUM_CUBITS; i++)
303
        {
304
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
305
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
306
        Static3D pos = getPos(mOrigPos[i]);
307
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
308
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
309
        }
310

    
311
      mMesh = new MeshJoined(cubitMesh);
312
      resetAllTextureMaps();
313
      }
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  private void createDataStructuresForSolved(int numLayers)
347
    {
348
    mTmpQuats = new int[NUM_QUATS];
349
    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

    
359
  private int getMultQuat(int index1, int index2)
360
    {
361
    if( mQuatMult==null )
362
      {
363
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
364

    
365
      for(int i=0; i<NUM_QUATS; i++)
366
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
367
      }
368

    
369
    if( mQuatMult[index1][index2]==-1 )
370
      {
371
      mQuatMult[index1][index2] = mulQuat(index1,index2);
372
      }
373

    
374
    return mQuatMult[index1][index2];
375
    }
376

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

    
385
  protected int retCubitSolvedStatus(int cubit, int numLayers)
386
    {
387
    int numNonBlack=0, nonBlackIndex=-1, color;
388

    
389
    for(int face=0; face<mNumCubitFaces; face++)
390
      {
391
      color = getFaceColor(cubit,face,numLayers);
392

    
393
      if( color<NUM_TEXTURES )
394
        {
395
        numNonBlack++;
396
        nonBlackIndex = color%NUM_FACE_COLORS;
397
        }
398
      }
399

    
400
    if( numNonBlack==0 ) return -1;
401
    if( numNonBlack>=2 ) return -2;
402

    
403
    return nonBlackIndex;
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  protected boolean shouldResetTextureMaps()
409
    {
410
    return false;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
416
    {
417
    final float MAXD = 0.0001f;
418
    float x = faceAx.get0();
419
    float y = faceAx.get1();
420
    float z = faceAx.get2();
421
    float a,dx,dy,dz,qx,qy,qz;
422
    Static4D quat;
423

    
424
    int len = quats.length;
425
    int place = 0;
426

    
427
    for(int q=1; q<len; q++)
428
      {
429
      quat = quats[q];
430
      qx = quat.get0();
431
      qy = quat.get1();
432
      qz = quat.get2();
433

    
434
           if( x!=0.0f ) { a = qx/x; }
435
      else if( y!=0.0f ) { a = qy/y; }
436
      else               { a = qz/z; }
437

    
438
      dx = a*x-qx;
439
      dy = a*y-qy;
440
      dz = a*z-qz;
441

    
442
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
443
        {
444
        mTmpQuats[place++] = q;
445
        }
446
      }
447

    
448
    if( place!=0 )
449
      {
450
      int[] ret = new int[place];
451
      System.arraycopy(mTmpQuats,0,ret,0,place);
452
      return ret;
453
      }
454

    
455
    return null;
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
  private boolean isSolved0()
461
    {
462
    int len, q1,q = CUBITS[0].mQuatIndex;
463
    int[] solved;
464
    boolean skip;
465

    
466
    for(int c=1; c<NUM_CUBITS; c++)
467
      {
468
      q1 = CUBITS[c].mQuatIndex;
469

    
470
      if( q1==q ) continue;
471

    
472
      skip = false;
473
      solved = mSolvedQuats[c];
474
      len = solved==null ? 0:solved.length;
475

    
476
      for(int i=0; i<len; i++)
477
        {
478
        if( q1==getMultQuat(q,solved[i]) )
479
          {
480
          skip = true;
481
          break;
482
          }
483
        }
484

    
485
      if( !skip ) return false;
486
      }
487

    
488
    return true;
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  private int computeScramble(int quatNum, int centerNum)
494
    {
495
    float MAXDIFF = 0.01f;
496
    float[] center= mOrigPos[centerNum];
497
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
498
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
499

    
500
    float x = result.get0();
501
    float y = result.get1();
502
    float z = result.get2();
503

    
504
    for(int c=0; c<NUM_CUBITS; c++)
505
      {
506
      float[] cent = mOrigPos[c];
507

    
508
      float qx = cent[0] - x;
509
      float qy = cent[1] - y;
510
      float qz = cent[2] - z;
511

    
512
      if( qx>-MAXDIFF && qx<MAXDIFF &&
513
          qy>-MAXDIFF && qy<MAXDIFF &&
514
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
515
      }
516

    
517
    return -1;
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521
// Dino4 uses this. It is solved if and only if groups of cubits
522
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
523
// or
524
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
525
// are all the same color.
526

    
527
  private boolean isSolved1()
528
    {
529
    if( mScramble==null )
530
      {
531
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
532
      mColors   = new int[NUM_CUBITS];
533

    
534
      for(int q=0; q<NUM_QUATS; q++)
535
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
536
      }
537

    
538
    if( mFaceMap==null )
539
      {
540
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
541
      }
542

    
543
    for(int c=0; c<NUM_CUBITS; c++)
544
      {
545
      int index = mScramble[CUBITS[c].mQuatIndex][c];
546
      mColors[index] = mFaceMap[c];
547
      }
548

    
549
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
550
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
551
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
552

    
553
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
554
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
555
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
556

    
557
    return false;
558
    }
559

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

    
578
  private boolean isSolved2()
579
    {
580
    int qX = CUBITS[0].mQuatIndex;
581
    int qY = CUBITS[1].mQuatIndex;
582
    int qZ = CUBITS[4].mQuatIndex;
583

    
584
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
585
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
586
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
587
      {
588
      return false;
589
      }
590

    
591
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
592
    }
593

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

    
602
  private boolean isSolved3()
603
    {
604
    int index = CUBITS[0].mQuatIndex;
605

    
606
    if( CUBITS[1].mQuatIndex!=index ) return false;
607

    
608
    boolean solved = true;
609

    
610
    for(int i=2; i<NUM_CUBITS; i++)
611
      {
612
      if( CUBITS[i].mQuatIndex!=index )
613
        {
614
        solved = false;
615
        break;
616
        }
617
      }
618

    
619
    if( solved ) return true;
620

    
621
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
622
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
623

    
624
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
625
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
626
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
627
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
628

    
629
    return true;
630
    }
631

    
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633

    
634
  int computeRow(float[] pos, int axisIndex)
635
    {
636
    int ret=0;
637
    int len = pos.length / 3;
638
    Static3D axis = mAxis[axisIndex];
639
    float axisX = axis.get0();
640
    float axisY = axis.get1();
641
    float axisZ = axis.get2();
642
    float casted;
643

    
644
    for(int i=0; i<len; i++)
645
      {
646
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
647
      ret |= computeSingleRow(axisIndex,casted);
648
      }
649

    
650
    return ret;
651
    }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654

    
655
  private int computeSingleRow(int axisIndex,float casted)
656
    {
657
    int num = mNumCuts[axisIndex];
658

    
659
    for(int i=0; i<num; i++)
660
      {
661
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
662
      }
663

    
664
    return (1<<num);
665
    }
666

    
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668

    
669
  private boolean wasRotateApplied()
670
    {
671
    return mEffects.exists(mRotateEffect.getID());
672
    }
673

    
674
///////////////////////////////////////////////////////////////////////////////////////////////////
675

    
676
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
677
    {
678
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
679
    }
680

    
681
///////////////////////////////////////////////////////////////////////////////////////////////////
682
// note the minus in front of the sin() - we rotate counterclockwise
683
// when looking towards the direction where the axis increases in values.
684

    
685
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
686
    {
687
    Static3D axis = mAxis[axisIndex];
688

    
689
    while( angleInDegrees<0 ) angleInDegrees += 360;
690
    angleInDegrees %= 360;
691
    
692
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
693
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
694

    
695
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
696
    }
697

    
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699

    
700
  private synchronized void setupPosition(int[][] moves)
701
    {
702
    if( moves!=null )
703
      {
704
      Static4D quat;
705
      int index, axis, rowBitmap, angle;
706
      int[] basic = getBasicAngle();
707

    
708
      for(int[] move: moves)
709
        {
710
        axis     = move[0];
711
        rowBitmap= move[1];
712
        angle    = move[2]*(360/basic[axis]);
713
        quat     = makeQuaternion(axis,angle);
714

    
715
        for(int j=0; j<NUM_CUBITS; j++)
716
          if( belongsToRotation(j,axis,rowBitmap) )
717
            {
718
            index = CUBITS[j].removeRotationNow(quat);
719
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
720
            }
721
        }
722
      }
723
    }
724

    
725
///////////////////////////////////////////////////////////////////////////////////////////////////
726

    
727
  protected int getScrambleType()
728
    {
729
    return 0;
730
    }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733

    
734
  int computeBitmapFromRow(int rowBitmap, int axis)
735
    {
736
    if( mIsBandaged )
737
      {
738
      int bitmap, initBitmap=0;
739

    
740
      while( initBitmap!=rowBitmap )
741
        {
742
        initBitmap = rowBitmap;
743

    
744
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
745
          {
746
          bitmap = CUBITS[cubit].getRotRow(axis);
747
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
748
          }
749
        }
750
      }
751

    
752
    return rowBitmap;
753
    }
754

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

    
759
  void clampPos(float[] pos, int offset)
760
    {
761
    float currError, minError = Float.MAX_VALUE;
762
    int minErrorIndex1 = -1;
763
    int minErrorIndex2 = -1;
764

    
765
    float x = pos[offset  ];
766
    float y = pos[offset+1];
767
    float z = pos[offset+2];
768

    
769
    float xo,yo,zo;
770

    
771
    for(int i=0; i<NUM_CUBITS; i++)
772
      {
773
      int len = mOrigPos[i].length / 3;
774

    
775
      for(int j=0; j<len; j++)
776
        {
777
        xo = mOrigPos[i][3*j  ];
778
        yo = mOrigPos[i][3*j+1];
779
        zo = mOrigPos[i][3*j+2];
780

    
781
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
782

    
783
        if( currError<minError )
784
          {
785
          minError = currError;
786
          minErrorIndex1 = i;
787
          minErrorIndex2 = j;
788
          }
789
        }
790
      }
791

    
792
    if( minError< 0.1f ) // TODO: 0.1 ?
793
      {
794
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
795
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
796
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
797
      }
798
    }
799

    
800
///////////////////////////////////////////////////////////////////////////////////////////////////
801
// remember about the double cover or unit quaternions!
802

    
803
  int mulQuat(int q1, int q2)
804
    {
805
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
806

    
807
    float rX = result.get0();
808
    float rY = result.get1();
809
    float rZ = result.get2();
810
    float rW = result.get3();
811

    
812
    final float MAX_ERROR = 0.1f;
813
    float dX,dY,dZ,dW;
814

    
815
    for(int i=0; i<NUM_QUATS; i++)
816
      {
817
      dX = OBJECT_QUATS[i].get0() - rX;
818
      dY = OBJECT_QUATS[i].get1() - rY;
819
      dZ = OBJECT_QUATS[i].get2() - rZ;
820
      dW = OBJECT_QUATS[i].get3() - rW;
821

    
822
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
823
          dY<MAX_ERROR && dY>-MAX_ERROR &&
824
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
825
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
826

    
827
      dX = OBJECT_QUATS[i].get0() + rX;
828
      dY = OBJECT_QUATS[i].get1() + rY;
829
      dZ = OBJECT_QUATS[i].get2() + rZ;
830
      dW = OBJECT_QUATS[i].get3() + rW;
831

    
832
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
833
          dY<MAX_ERROR && dY>-MAX_ERROR &&
834
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
835
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
836
      }
837

    
838
    return -1;
839
    }
840

    
841
///////////////////////////////////////////////////////////////////////////////////////////////////
842
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
843

    
844
  private void createTexture()
845
    {
846
    Bitmap bitmap;
847

    
848
    Paint paint = new Paint();
849
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
850
    Canvas canvas = new Canvas(bitmap);
851

    
852
    paint.setAntiAlias(true);
853
    paint.setTextAlign(Paint.Align.CENTER);
854
    paint.setStyle(Paint.Style.FILL);
855

    
856
    paint.setColor(COLOR_BLACK);
857
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
858

    
859
    int face = 0;
860
    FactorySticker factory = FactorySticker.getInstance();
861

    
862
    for(int row=0; row<mNumTexRows; row++)
863
      for(int col=0; col<mNumTexCols; col++)
864
        {
865
        if( face>=NUM_TEXTURES ) break;
866
        ObjectSticker sticker = retSticker(face);
867
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, getColor(face%NUM_FACE_COLORS), sticker);
868
        face++;
869
        }
870

    
871
    if( !mTexture.setTexture(bitmap) )
872
      {
873
      int max = DistortedLibrary.getMaxTextureSize();
874
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
875
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
876
      }
877
    }
878

    
879
///////////////////////////////////////////////////////////////////////////////////////////////////
880

    
881
  private float getAngle()
882
    {
883
    int pointNum = mRotationAngle.getNumPoints();
884

    
885
    if( pointNum>=1 )
886
      {
887
      return mRotationAngle.getPoint(pointNum-1).get0();
888
      }
889
    else
890
      {
891
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
892
      crashlytics.log("points in RotationAngle: "+pointNum);
893
      return 0;
894
      }
895
    }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898

    
899
  private void recordQuatsState(String message)
900
    {
901
    StringBuilder quats = new StringBuilder();
902

    
903
    for(int j=0; j<NUM_CUBITS; j++)
904
      {
905
      quats.append(mQuatDebug[j]);
906
      quats.append(" ");
907
      }
908

    
909
    String name = intGetObjectType(mNumLayers).name();
910

    
911
    if( BuildConfig.DEBUG )
912
      {
913
      android.util.Log.e("quats" , quats.toString());
914
      android.util.Log.e("object", name);
915
      }
916
    else
917
      {
918
      Exception ex = new Exception(message);
919
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
920
      crashlytics.setCustomKey("quats" , quats.toString());
921
      crashlytics.setCustomKey("object", name);
922
      crashlytics.recordException(ex);
923
      }
924
    }
925

    
926
///////////////////////////////////////////////////////////////////////////////////////////////////
927
// PUBLIC API
928
///////////////////////////////////////////////////////////////////////////////////////////////////
929

    
930
  public boolean isSolved()
931
    {
932
    if( mSolvedFunctionIndex==0 ) return isSolved0();
933
    if( mSolvedFunctionIndex==1 ) return isSolved1();
934
    if( mSolvedFunctionIndex==2 ) return isSolved2();
935
    if( mSolvedFunctionIndex==3 ) return isSolved3();
936

    
937
    return false;
938
    }
939

    
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941

    
942
  public void setObjectRatio(float sizeChange)
943
    {
944
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
945

    
946
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
947
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
948

    
949
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
950
    mObjectScale.set(scale,scale,scale);
951
    }
952

    
953
///////////////////////////////////////////////////////////////////////////////////////////////////
954

    
955
  public float getObjectRatio()
956
    {
957
    return mObjectScreenRatio*mInitScreenRatio;
958
    }
959

    
960
///////////////////////////////////////////////////////////////////////////////////////////////////
961

    
962
  public int getCubitFaceColorIndex(int cubit, int face)
963
    {
964
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
965

    
966
    int x = (int)(texMap.get0()/texMap.get2());
967
    int y = (int)(texMap.get1()/texMap.get3());
968

    
969
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
970
    }
971

    
972
///////////////////////////////////////////////////////////////////////////////////////////////////
973

    
974
  public int getNumLayers()
975
    {
976
    return mNumLayers;
977
    }
978

    
979
///////////////////////////////////////////////////////////////////////////////////////////////////
980

    
981
  public void continueRotation(float angleInDegrees)
982
    {
983
    mRotationAngleStatic.set0(angleInDegrees);
984
    }
985

    
986
///////////////////////////////////////////////////////////////////////////////////////////////////
987

    
988
  public Static4D getRotationQuat()
989
      {
990
      return mQuat;
991
      }
992

    
993
///////////////////////////////////////////////////////////////////////////////////////////////////
994

    
995
  public void recomputeScaleFactor(int scrWidth)
996
    {
997
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
998
    }
999

    
1000
///////////////////////////////////////////////////////////////////////////////////////////////////
1001

    
1002
  public void savePreferences(SharedPreferences.Editor editor)
1003
    {
1004
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1005
    }
1006

    
1007
///////////////////////////////////////////////////////////////////////////////////////////////////
1008

    
1009
  public synchronized void restorePreferences(SharedPreferences preferences)
1010
    {
1011
    boolean error = false;
1012

    
1013
    for(int i=0; i<NUM_CUBITS; i++)
1014
      {
1015
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1016

    
1017
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1018
        {
1019
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1020
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1021
        }
1022
      else
1023
        {
1024
        error = true;
1025
        }
1026
      }
1027

    
1028
    if( error )
1029
      {
1030
      for(int i=0; i<NUM_CUBITS; i++)
1031
        {
1032
        CUBITS[i].solve();
1033
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1034
        }
1035
      recordQuatsState("Failed to restorePreferences");
1036
      }
1037
    }
1038

    
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040

    
1041
  public void releaseResources()
1042
    {
1043
    mTexture.markForDeletion();
1044
    mMesh.markForDeletion();
1045
    mEffects.markForDeletion();
1046

    
1047
    for(int j=0; j<NUM_CUBITS; j++)
1048
      {
1049
      CUBITS[j].releaseResources();
1050
      }
1051
    }
1052

    
1053
///////////////////////////////////////////////////////////////////////////////////////////////////
1054

    
1055
  public void apply(Effect effect, int position)
1056
    {
1057
    mEffects.apply(effect, position);
1058
    }
1059

    
1060
///////////////////////////////////////////////////////////////////////////////////////////////////
1061

    
1062
  public void remove(long effectID)
1063
    {
1064
    mEffects.abortById(effectID);
1065
    }
1066

    
1067
///////////////////////////////////////////////////////////////////////////////////////////////////
1068

    
1069
  public synchronized void solve()
1070
    {
1071
    for(int i=0; i<NUM_CUBITS; i++)
1072
      {
1073
      CUBITS[i].solve();
1074
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1075
      }
1076
    }
1077

    
1078
///////////////////////////////////////////////////////////////////////////////////////////////////
1079

    
1080
  public void resetAllTextureMaps()
1081
    {
1082
    final float ratioW = 1.0f/mNumTexCols;
1083
    final float ratioH = 1.0f/mNumTexRows;
1084
    int color, row, col;
1085

    
1086
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1087
      {
1088
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1089

    
1090
      for(int cubitface=0; cubitface<mNumCubitFaces; cubitface++)
1091
        {
1092
        color = getFaceColor(cubit,cubitface,mNumLayers);
1093
        row = (mNumTexRows-1) - color/mNumTexCols;
1094
        col = color%mNumTexCols;
1095
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1096
        }
1097

    
1098
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1099
      }
1100
    }
1101

    
1102
///////////////////////////////////////////////////////////////////////////////////////////////////
1103

    
1104
  public void setTextureMap(int cubit, int face, int newColor)
1105
    {
1106
    final float ratioW = 1.0f/mNumTexCols;
1107
    final float ratioH = 1.0f/mNumTexRows;
1108
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1109
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1110
    int col = newColor%mNumTexCols;
1111

    
1112
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1113
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1114
    }
1115

    
1116
///////////////////////////////////////////////////////////////////////////////////////////////////
1117

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

    
1131
    mRotAxis     = axis;
1132
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1133
    mRotationAngleStatic.set0(0.0f);
1134
    mRotationAxis.set( mAxis[axis] );
1135
    mRotationAngle.add(mRotationAngleStatic);
1136
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
1137
    }
1138

    
1139
///////////////////////////////////////////////////////////////////////////////////////////////////
1140

    
1141
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1142
    {
1143
    if( wasRotateApplied() )
1144
      {
1145
      mRotAxis     = axis;
1146
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1147

    
1148
      mRotationAngleStatic.set0(0.0f);
1149
      mRotationAxis.set( mAxis[axis] );
1150
      mRotationAngle.setDuration(durationMillis);
1151
      mRotationAngle.resetToBeginning();
1152
      mRotationAngle.add(new Static1D(0));
1153
      mRotationAngle.add(new Static1D(angle));
1154
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*ObjectType.MAX_OBJECT_SIZE) , -1);
1155
      mRotateEffect.notifyWhenFinished(listener);
1156

    
1157
      return mRotateEffect.getID();
1158
      }
1159

    
1160
    return 0;
1161
    }
1162

    
1163
///////////////////////////////////////////////////////////////////////////////////////////////////
1164

    
1165
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1166
    {
1167
    if( wasRotateApplied() )
1168
      {
1169
      float angle = getAngle();
1170
      mRotationAngleStatic.set0(angle);
1171
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1172
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1173

    
1174
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1175
      mRotationAngle.resetToBeginning();
1176
      mRotationAngle.removeAll();
1177
      mRotationAngle.add(mRotationAngleStatic);
1178
      mRotationAngle.add(mRotationAngleMiddle);
1179
      mRotationAngle.add(mRotationAngleFinal);
1180
      mRotateEffect.notifyWhenFinished(listener);
1181

    
1182
      return mRotateEffect.getID();
1183
      }
1184

    
1185
    return 0;
1186
    }
1187

    
1188
///////////////////////////////////////////////////////////////////////////////////////////////////
1189

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

    
1201
    mRotationAngle.removeAll();
1202
    mRotationAngleStatic.set0(0);
1203

    
1204
    for(int i=0; i<NUM_CUBITS; i++)
1205
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
1206
        {
1207
        int index = CUBITS[i].removeRotationNow(quat);
1208
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
1209
        }
1210
    }
1211

    
1212
///////////////////////////////////////////////////////////////////////////////////////////////////
1213

    
1214
  public void initializeObject(int[][] moves)
1215
    {
1216
    solve();
1217
    setupPosition(moves);
1218
    }
1219

    
1220
///////////////////////////////////////////////////////////////////////////////////////////////////
1221

    
1222
  public int getCubit(float[] point3D)
1223
    {
1224
    float dist, minDist = Float.MAX_VALUE;
1225
    int currentBest=-1;
1226
    float multiplier = returnMultiplier();
1227

    
1228
    point3D[0] *= multiplier;
1229
    point3D[1] *= multiplier;
1230
    point3D[2] *= multiplier;
1231

    
1232
    for(int i=0; i<NUM_CUBITS; i++)
1233
      {
1234
      dist = CUBITS[i].getDistSquared(point3D);
1235
      if( dist<minDist )
1236
        {
1237
        minDist = dist;
1238
        currentBest = i;
1239
        }
1240
      }
1241

    
1242
    return currentBest;
1243
    }
1244

    
1245
///////////////////////////////////////////////////////////////////////////////////////////////////
1246

    
1247
  public int computeNearestAngle(int axis, float angle, float speed)
1248
    {
1249
    int[] basicArray = getBasicAngle();
1250
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1251
    int nearestAngle = 360/basicAngle;
1252

    
1253
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1254
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1255

    
1256
    if( tmp!=0 ) return nearestAngle*tmp;
1257

    
1258
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1259
    }
1260

    
1261
///////////////////////////////////////////////////////////////////////////////////////////////////
1262

    
1263
  public float getCameraDist()
1264
    {
1265
    return mCameraDist;
1266
    }
1267

    
1268
///////////////////////////////////////////////////////////////////////////////////////////////////
1269

    
1270
  public int getNodeSize()
1271
    {
1272
    return mNodeSize;
1273
    }
1274

    
1275
///////////////////////////////////////////////////////////////////////////////////////////////////
1276

    
1277
  public ObjectType getObjectType()
1278
    {
1279
    return intGetObjectType(mNumLayers);
1280
    }
1281

    
1282
///////////////////////////////////////////////////////////////////////////////////////////////////
1283

    
1284
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1285
    {
1286
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1287
    }
1288

    
1289
///////////////////////////////////////////////////////////////////////////////////////////////////
1290

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

    
1313
  public abstract Movement getMovement();
1314
  public abstract Static3D[] getRotationAxis();
1315
  public abstract int[] getBasicAngle();
1316
  public abstract int getObjectName(int numLayers);
1317
  public abstract int getInventor(int numLayers);
1318
  public abstract int getComplexity(int numLayers);
1319
  }
(19-19/20)