Project

General

Profile

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

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

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.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26

    
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectQuaternion;
32
import org.distorted.library.effect.VertexEffectRotate;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedNode;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.QuatHelper;
38
import org.distorted.library.mesh.MeshBase;
39
import org.distorted.library.mesh.MeshFile;
40
import org.distorted.library.mesh.MeshJoined;
41
import org.distorted.library.message.EffectListener;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46

    
47
import org.distorted.objectlib.helpers.FactoryCubit;
48
import org.distorted.objectlib.helpers.FactorySticker;
49
import org.distorted.objectlib.helpers.ObjectLibInterface;
50
import org.distorted.objectlib.helpers.ObjectShape;
51
import org.distorted.objectlib.helpers.ObjectSticker;
52
import org.distorted.objectlib.helpers.ScrambleState;
53
import org.distorted.objectlib.json.JsonReader;
54
import org.distorted.objectlib.touchcontrol.TouchControl;
55
import org.distorted.objectlib.touchcontrol.TouchControlCuboids;
56
import org.distorted.objectlib.touchcontrol.TouchControlDodecahedron;
57
import org.distorted.objectlib.touchcontrol.TouchControlHexahedron;
58
import org.distorted.objectlib.touchcontrol.TouchControlOctahedron;
59
import org.distorted.objectlib.touchcontrol.TouchControlTetrahedron;
60

    
61
import java.io.DataInputStream;
62
import java.io.IOException;
63
import java.io.InputStream;
64
import java.util.Random;
65

    
66
import static org.distorted.objectlib.touchcontrol.TouchControl.TC_TETRAHEDRON;
67
import static org.distorted.objectlib.touchcontrol.TouchControl.TC_HEXAHEDRON;
68
import static org.distorted.objectlib.touchcontrol.TouchControl.TC_OCTAHEDRON;
69
import static org.distorted.objectlib.touchcontrol.TouchControl.TC_DODECAHEDRON;
70
import static org.distorted.objectlib.touchcontrol.TouchControl.TC_CUBOID;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
public abstract class TwistyObject
75
  {
76
  public static final int COLOR_YELLOW = 0xffffff00;
77
  public static final int COLOR_WHITE  = 0xffffffff;
78
  public static final int COLOR_BLUE   = 0xff0000ff;
79
  public static final int COLOR_GREEN  = 0xff00bb00;
80
  public static final int COLOR_RED    = 0xff990000;
81
  public static final int COLOR_ORANGE = 0xffff6200;
82
  public static final int COLOR_GREY   = 0xff727c7b;
83
  public static final int COLOR_VIOLET = 0xff7700bb;
84
  public static final int COLOR_BLACK  = 0xff000000;
85

    
86
  public static final int TEXTURE_HEIGHT = 256;
87
  static final int NUM_STICKERS_IN_ROW = 4;
88

    
89
  public static final float SQ2 = (float)Math.sqrt(2);
90
  public static final float SQ3 = (float)Math.sqrt(3);
91
  public static final float SQ5 = (float)Math.sqrt(5);
92
  public static final float SQ6 = (float)Math.sqrt(6);
93

    
94
  private static final float MAX_SIZE_CHANGE = 1.35f;
95
  private static final float MIN_SIZE_CHANGE = 0.75f;
96

    
97
  private static final Static3D CENTER = new Static3D(0,0,0);
98
  private static final int POST_ROTATION_MILLISEC = 500;
99

    
100
  protected int NUM_FACE_COLORS;
101
  protected int NUM_TEXTURES;
102
  protected Cubit[] CUBITS;
103

    
104
  MeshBase[] mMeshes;
105
  Static4D[] OBJECT_QUATS;
106
  int NUM_CUBITS;
107
  int NUM_AXIS;
108
  int NUM_QUATS;
109
  int SHIFT;
110

    
111
  private int mNumCubitFaces;
112
  private Static3D[] mAxis;
113
  private float[][] mCuts;
114
  private int[] mNumCuts;
115
  private float[][] mOrigPos;
116
  private Static4D mQuat;
117
  private final int[] mNumLayers;
118
  private final float mSize;
119
  private DistortedEffects mEffects;
120
  private VertexEffectRotate mRotateEffect;
121
  private Dynamic1D mRotationAngle;
122
  private Static3D mRotationAxis;
123
  private Static3D mObjectScale;
124
  private int[] mQuatDebug;
125
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
126
  private DistortedTexture mTexture;
127
  private float mInitScreenRatio;
128
  private int mSolvedFunctionIndex;
129
  private boolean mIsBandaged;
130
  private float mObjectScreenRatio;
131
  private int[][] mSolvedQuats;
132
  private int[][] mQuatMult;
133
  private int[] mTmpQuats;
134
  private int mNumTexRows, mNumTexCols;
135
  private int mRotRowBitmap;
136
  private int mCurrentRotAxis;
137
  private MeshBase mMesh;
138
  private TwistyObjectScrambler mScrambler;
139
  private TouchControl mTouchControl;
140
  private boolean[][] mLayerRotatable;
141
  private int[][][] mEnabled;
142
  private DistortedNode mNode;
143
  private ObjectLibInterface mInterface;
144
  private Bitmap mBitmap;
145

    
146
  //////////////////// SOLVED1 ////////////////////////
147

    
148
  private int[] mFaceMap;
149
  private int[][] mScramble;
150
  private int[] mColors;
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  TwistyObject(InputStream jsonStream, Static4D quat, Static3D move, float scale, InputStream meshStream)
155
    {
156
    JsonReader reader = new JsonReader();
157
    reader.parseJsonFile(jsonStream);
158
    setReader(reader);
159

    
160
    mNumLayers = reader.getNumLayers();
161
    mSize      = reader.getSize();
162
    initialize(quat,move,scale,meshStream);
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  TwistyObject(int[] numLayers, float size, Static4D quat, Static3D move, float scale, InputStream meshStream)
168
    {
169
    mNumLayers = numLayers;
170
    mSize      = size;
171
    initialize(quat,move,scale,meshStream);
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void initialize(Static4D quat, Static3D move, float scale, InputStream stream)
177
    {
178
    mQuat = quat;
179
    mOrigPos = getCubitPositions(mNumLayers);
180
    mAxis = getRotationAxis();
181
    mInitScreenRatio = getScreenRatio();
182
    mNumCubitFaces = getNumCubitFaces();
183
    mSolvedFunctionIndex = getSolvedFunctionIndex();
184

    
185
    int numAxis = mAxis.length;
186
    SHIFT = -1;
187

    
188
    mCuts = getCuts(mNumLayers);
189
    mNumCuts = new int[numAxis];
190
    for(int i=0; i<numAxis; i++)
191
      {
192
      if( SHIFT<mNumLayers[i] ) SHIFT = mNumLayers[i];
193
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
194
      }
195

    
196
    OBJECT_QUATS = getQuats();
197
    NUM_CUBITS  = mOrigPos.length;
198
    NUM_FACE_COLORS = getNumFaceColors();
199
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
200
    NUM_AXIS = mAxis.length;
201
    NUM_QUATS = OBJECT_QUATS.length;
202

    
203
    int scramblingType = getScrambleType();
204
    ScrambleState[] states = getScrambleStates();
205
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
206

    
207
    boolean bandaged=false;
208

    
209
    for(int c=0; c<NUM_CUBITS; c++)
210
      {
211
      if( mOrigPos[c].length>3 )
212
        {
213
        bandaged=true;
214
        break;
215
        }
216
      }
217

    
218
    mIsBandaged = bandaged;
219
    mQuatDebug = new int[NUM_CUBITS];
220

    
221
    mRotationAngle= new Dynamic1D();
222
    mRotationAxis = new Static3D(1,0,0);
223
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
224

    
225
    mRotationAngleStatic = new Static1D(0);
226
    mRotationAngleMiddle = new Static1D(0);
227
    mRotationAngleFinal  = new Static1D(0);
228

    
229
    mObjectScale = new Static3D(scale,scale,scale);
230
    setObjectRatioNow(scale,720);
231

    
232
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
233
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(mQuat, CENTER);
234
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
235

    
236
    mNumTexCols = NUM_STICKERS_IN_ROW;
237
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
238

    
239
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
240

    
241
    CUBITS = new Cubit[NUM_CUBITS];
242
    createMeshAndCubits(stream);
243
    createDataStructuresForSolved(mNumLayers);
244

    
245
    mTexture = new DistortedTexture();
246
    mEffects = new DistortedEffects();
247

    
248
    setTexture();
249

    
250
    for(int q=0; q<NUM_QUATS; q++)
251
      {
252
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
253
      vq.setMeshAssociation(0,q);
254
      mEffects.apply(vq);
255
      }
256

    
257
    mEffects.apply(mRotateEffect);
258
    mEffects.apply(quatEffect);
259
    mEffects.apply(scaleEffect);
260
    mEffects.apply(moveEffect);
261

    
262
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  private Static3D getPos(float[] origPos)
268
    {
269
    int len = origPos.length/3;
270
    float sumX = 0.0f;
271
    float sumY = 0.0f;
272
    float sumZ = 0.0f;
273

    
274
    for(int i=0; i<len; i++)
275
      {
276
      sumX += origPos[3*i  ];
277
      sumY += origPos[3*i+1];
278
      sumZ += origPos[3*i+2];
279
      }
280

    
281
    sumX /= len;
282
    sumY /= len;
283
    sumZ /= len;
284

    
285
    return new Static3D(sumX,sumY,sumZ);
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  private void createMeshAndCubits(InputStream stream)
291
    {
292
    if( stream!=null )
293
      {
294
      DataInputStream dos = new DataInputStream(stream);
295
      mMesh = new MeshFile(dos);
296

    
297
      try
298
        {
299
        stream.close();
300
        }
301
      catch(IOException e)
302
        {
303
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
304
        }
305

    
306
      for(int i=0; i<NUM_CUBITS; i++)
307
        {
308
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
309
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
310
        }
311

    
312
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
313
      }
314
    else
315
      {
316
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
317

    
318
      for(int i=0; i<NUM_CUBITS; i++)
319
        {
320
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
321
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
322
        Static3D pos = getPos(mOrigPos[i]);
323
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
324
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
325
        }
326

    
327
      mMesh = new MeshJoined(cubitMesh);
328
      resetAllTextureMaps();
329
      }
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  private MeshBase createCubitMesh(int cubit, int[] numLayers)
335
    {
336
    int variant = getCubitVariant(cubit,numLayers);
337

    
338
    if( mMeshes==null )
339
      {
340
      FactoryCubit factory = FactoryCubit.getInstance();
341
      factory.clear();
342
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
343
      }
344

    
345
    if( mMeshes[variant]==null )
346
      {
347
      ObjectShape shape = getObjectShape(variant);
348
      FactoryCubit factory = FactoryCubit.getInstance();
349
      factory.createNewFaceTransform(shape);
350
      mMeshes[variant] = factory.createRoundedSolid(shape);
351
      }
352

    
353
    MeshBase mesh = mMeshes[variant].copy(true);
354
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
355
    mesh.apply(quat,0xffffffff,0);
356

    
357
    return mesh;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

    
362
  private void createDataStructuresForSolved(int[] numLayers)
363
    {
364
    mTmpQuats = new int[NUM_QUATS];
365
    mSolvedQuats = new int[NUM_CUBITS][];
366

    
367
    for(int c=0; c<NUM_CUBITS; c++)
368
      {
369
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
370
      }
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  private int getMultQuat(int index1, int index2)
376
    {
377
    if( mQuatMult==null )
378
      {
379
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
380

    
381
      for(int i=0; i<NUM_QUATS; i++)
382
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
383
      }
384

    
385
    if( mQuatMult[index1][index2]==-1 )
386
      {
387
      mQuatMult[index1][index2] = mulQuat(index1,index2);
388
      }
389

    
390
    return mQuatMult[index1][index2];
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
// This is used to build internal data structures for the generic 'isSolved()'
395
//
396
// if this is an internal cubit (all faces black): return -1
397
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
398
// Color index, i.e. the index into the 'FACE_COLORS' table.
399
// else (edge or corner cubit, more than one non-black face): return -2.
400

    
401
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
402
    {
403
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
404
    int variant = getCubitVariant(cubit,numLayers);
405

    
406
    for(int face=0; face<mNumCubitFaces; face++)
407
      {
408
      varColor = getVariantFaceColor(variant,face,numLayers);
409
      cubColor = getCubitFaceColor(cubit,face,numLayers);
410

    
411
      if( varColor>=0 && cubColor>=0 )
412
        {
413
        numNonBlack++;
414
        nonBlackIndex = cubColor;
415
        }
416
      }
417

    
418
    if( numNonBlack==0 ) return -1;
419
    if( numNonBlack>=2 ) return -2;
420

    
421
    return nonBlackIndex;
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public boolean shouldResetTextureMaps()
427
    {
428
    return false;
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
434
    {
435
    final float MAXD = 0.0001f;
436
    float x = faceAx.get0();
437
    float y = faceAx.get1();
438
    float z = faceAx.get2();
439
    float a,dx,dy,dz,qx,qy,qz;
440
    Static4D quat;
441

    
442
    int len = quats.length;
443
    int place = 0;
444

    
445
    for(int q=1; q<len; q++)
446
      {
447
      quat = quats[q];
448
      qx = quat.get0();
449
      qy = quat.get1();
450
      qz = quat.get2();
451

    
452
           if( x!=0.0f ) { a = qx/x; }
453
      else if( y!=0.0f ) { a = qy/y; }
454
      else               { a = qz/z; }
455

    
456
      dx = a*x-qx;
457
      dy = a*y-qy;
458
      dz = a*z-qz;
459

    
460
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
461
        {
462
        mTmpQuats[place++] = q;
463
        }
464
      }
465

    
466
    if( place!=0 )
467
      {
468
      int[] ret = new int[place];
469
      System.arraycopy(mTmpQuats,0,ret,0,place);
470
      return ret;
471
      }
472

    
473
    return null;
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
  private boolean isSolved0()
479
    {
480
    int len, q1,q = CUBITS[0].mQuatIndex;
481
    int[] solved;
482
    boolean skip;
483

    
484
    for(int c=1; c<NUM_CUBITS; c++)
485
      {
486
      q1 = CUBITS[c].mQuatIndex;
487

    
488
      if( q1==q ) continue;
489

    
490
      skip = false;
491
      solved = mSolvedQuats[c];
492
      len = solved==null ? 0:solved.length;
493

    
494
      for(int i=0; i<len; i++)
495
        {
496
        if( q1==getMultQuat(q,solved[i]) )
497
          {
498
          skip = true;
499
          break;
500
          }
501
        }
502

    
503
      if( !skip ) return false;
504
      }
505

    
506
    return true;
507
    }
508

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510

    
511
  private int computeScramble(int quatNum, int centerNum)
512
    {
513
    float MAXDIFF = 0.01f;
514
    float[] center= mOrigPos[centerNum];
515
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
516
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
517

    
518
    float x = result.get0();
519
    float y = result.get1();
520
    float z = result.get2();
521

    
522
    for(int c=0; c<NUM_CUBITS; c++)
523
      {
524
      float[] cent = mOrigPos[c];
525

    
526
      float qx = cent[0] - x;
527
      float qy = cent[1] - y;
528
      float qz = cent[2] - z;
529

    
530
      if( qx>-MAXDIFF && qx<MAXDIFF &&
531
          qy>-MAXDIFF && qy<MAXDIFF &&
532
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
533
      }
534

    
535
    return -1;
536
    }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539
// Dino4 uses this. It is solved if and only if groups of cubits
540
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
541
// or
542
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
543
// are all the same color.
544

    
545
  private boolean isSolved1()
546
    {
547
    if( mScramble==null )
548
      {
549
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
550
      mColors   = new int[NUM_CUBITS];
551

    
552
      for(int q=0; q<NUM_QUATS; q++)
553
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
554
      }
555

    
556
    if( mFaceMap==null )
557
      {
558
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
559
      }
560

    
561
    for(int c=0; c<NUM_CUBITS; c++)
562
      {
563
      int index = mScramble[CUBITS[c].mQuatIndex][c];
564
      mColors[index] = mFaceMap[c];
565
      }
566

    
567
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
568
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
569
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
570

    
571
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
572
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
573
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
574

    
575
    return false;
576
    }
577

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579
// Dino6 uses this. It is solved if and only if:
580
//
581
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
582
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
583
// by the same qZ, and then either:
584
//
585
// a) qX = qY = qZ
586
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
587
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
588
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
589
//
590
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
591
//
592
// X cubits: 0, 2, 8, 10
593
// Y cubits: 1, 3, 9, 11
594
// Z cubits: 4, 5, 6, 7
595

    
596
  private boolean isSolved2()
597
    {
598
    int qX = CUBITS[0].mQuatIndex;
599
    int qY = CUBITS[1].mQuatIndex;
600
    int qZ = CUBITS[4].mQuatIndex;
601

    
602
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
603
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
604
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
605
      {
606
      return false;
607
      }
608

    
609
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613
// Square-2 is solved iff
614
// a) all of its cubits are rotated with the same quat
615
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
616
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
617
// and all the 12 left and right edges and corners also with the same quat multiplied by
618
// QUATS[12] - i.e. also upside down.
619

    
620
  private boolean isSolved3()
621
    {
622
    int index = CUBITS[0].mQuatIndex;
623

    
624
    if( CUBITS[1].mQuatIndex!=index ) return false;
625

    
626
    boolean solved = true;
627

    
628
    for(int i=2; i<NUM_CUBITS; i++)
629
      {
630
      if( CUBITS[i].mQuatIndex!=index )
631
        {
632
        solved = false;
633
        break;
634
        }
635
      }
636

    
637
    if( solved ) return true;
638

    
639
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
640
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
641

    
642
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
643
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
644
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
645
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
646

    
647
    return true;
648
    }
649

    
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651

    
652
  int computeRow(float[] pos, int axisIndex)
653
    {
654
    int ret=0;
655
    int len = pos.length / 3;
656
    Static3D axis = mAxis[axisIndex];
657
    float axisX = axis.get0();
658
    float axisY = axis.get1();
659
    float axisZ = axis.get2();
660
    float casted;
661

    
662
    for(int i=0; i<len; i++)
663
      {
664
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
665
      ret |= computeSingleRow(axisIndex,casted);
666
      }
667

    
668
    return ret;
669
    }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672

    
673
  private int computeSingleRow(int axisIndex,float casted)
674
    {
675
    int num = mNumCuts[axisIndex];
676

    
677
    for(int i=0; i<num; i++)
678
      {
679
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
680
      }
681

    
682
    return (1<<num);
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686

    
687
  private boolean wasRotateApplied()
688
    {
689
    return mEffects.exists(mRotateEffect.getID());
690
    }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693

    
694
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
695
    {
696
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
697
    }
698

    
699
///////////////////////////////////////////////////////////////////////////////////////////////////
700
// note the minus in front of the sin() - we rotate counterclockwise
701
// when looking towards the direction where the axis increases in values.
702

    
703
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
704
    {
705
    Static3D axis = mAxis[axisIndex];
706

    
707
    while( angleInDegrees<0 ) angleInDegrees += 360;
708
    angleInDegrees %= 360;
709
    
710
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
711
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
712

    
713
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
714
    }
715

    
716
///////////////////////////////////////////////////////////////////////////////////////////////////
717

    
718
  private synchronized void setupPosition(int[][] moves)
719
    {
720
    if( moves!=null )
721
      {
722
      Static4D quat;
723
      int index, axis, rowBitmap, angle;
724
      int[] basic = getBasicAngle();
725

    
726
      for(int[] move: moves)
727
        {
728
        axis     = move[0];
729
        rowBitmap= move[1];
730
        angle    = move[2]*(360/basic[axis]);
731
        quat     = makeQuaternion(axis,angle);
732

    
733
        for(int j=0; j<NUM_CUBITS; j++)
734
          if( belongsToRotation(j,axis,rowBitmap) )
735
            {
736
            index = CUBITS[j].removeRotationNow(quat);
737
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
738
            }
739
        }
740
      }
741
    }
742

    
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744

    
745
  public int getScrambleType()
746
    {
747
    return 0;
748
    }
749

    
750
///////////////////////////////////////////////////////////////////////////////////////////////////
751

    
752
  int computeBitmapFromRow(int rowBitmap, int axis)
753
    {
754
    if( mIsBandaged )
755
      {
756
      int bitmap, initBitmap=0;
757

    
758
      while( initBitmap!=rowBitmap )
759
        {
760
        initBitmap = rowBitmap;
761

    
762
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
763
          {
764
          bitmap = CUBITS[cubit].getRotRow(axis);
765
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
766
          }
767
        }
768
      }
769

    
770
    return rowBitmap;
771
    }
772

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

    
777
  void clampPos(float[] pos, int offset)
778
    {
779
    float currError, minError = Float.MAX_VALUE;
780
    int minErrorIndex1 = -1;
781
    int minErrorIndex2 = -1;
782

    
783
    float x = pos[offset  ];
784
    float y = pos[offset+1];
785
    float z = pos[offset+2];
786

    
787
    float xo,yo,zo;
788

    
789
    for(int i=0; i<NUM_CUBITS; i++)
790
      {
791
      int len = mOrigPos[i].length / 3;
792

    
793
      for(int j=0; j<len; j++)
794
        {
795
        xo = mOrigPos[i][3*j  ];
796
        yo = mOrigPos[i][3*j+1];
797
        zo = mOrigPos[i][3*j+2];
798

    
799
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
800

    
801
        if( currError<minError )
802
          {
803
          minError = currError;
804
          minErrorIndex1 = i;
805
          minErrorIndex2 = j;
806
          }
807
        }
808
      }
809

    
810
    if( minError< 0.1f ) // TODO: 0.1 ?
811
      {
812
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
813
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
814
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
815
      }
816
    }
817

    
818
///////////////////////////////////////////////////////////////////////////////////////////////////
819
// remember about the double cover or unit quaternions!
820

    
821
  int mulQuat(int q1, int q2)
822
    {
823
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
824

    
825
    float rX = result.get0();
826
    float rY = result.get1();
827
    float rZ = result.get2();
828
    float rW = result.get3();
829

    
830
    final float MAX_ERROR = 0.1f;
831
    float dX,dY,dZ,dW;
832

    
833
    for(int i=0; i<NUM_QUATS; i++)
834
      {
835
      dX = OBJECT_QUATS[i].get0() - rX;
836
      dY = OBJECT_QUATS[i].get1() - rY;
837
      dZ = OBJECT_QUATS[i].get2() - rZ;
838
      dW = OBJECT_QUATS[i].get3() - rW;
839

    
840
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
841
          dY<MAX_ERROR && dY>-MAX_ERROR &&
842
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
843
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
844

    
845
      dX = OBJECT_QUATS[i].get0() + rX;
846
      dY = OBJECT_QUATS[i].get1() + rY;
847
      dZ = OBJECT_QUATS[i].get2() + rZ;
848
      dW = OBJECT_QUATS[i].get3() + rW;
849

    
850
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
851
          dY<MAX_ERROR && dY>-MAX_ERROR &&
852
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
853
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
854
      }
855

    
856
    return -1;
857
    }
858

    
859
///////////////////////////////////////////////////////////////////////////////////////////////////
860

    
861
  private float getAngle()
862
    {
863
    int pointNum = mRotationAngle.getNumPoints();
864

    
865
    if( pointNum>=1 )
866
      {
867
      return mRotationAngle.getPoint(pointNum-1).get0();
868
      }
869
    else
870
      {
871
      mInterface.reportProblem("points in RotationAngle: "+pointNum);
872
      return 0;
873
      }
874
    }
875

    
876
///////////////////////////////////////////////////////////////////////////////////////////////////
877

    
878
  void setLibInterface(ObjectLibInterface inter)
879
    {
880
    mInterface = inter;
881
    }
882

    
883
///////////////////////////////////////////////////////////////////////////////////////////////////
884

    
885
  void initializeObject(int[][] moves)
886
    {
887
    solve();
888
    setupPosition(moves);
889
    }
890

    
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892

    
893
  synchronized void removeRotationNow()
894
    {
895
    float angle = getAngle();
896
    double nearestAngleInRadians = angle*Math.PI/180;
897
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
898
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
899
    float axisX = mAxis[mCurrentRotAxis].get0();
900
    float axisY = mAxis[mCurrentRotAxis].get1();
901
    float axisZ = mAxis[mCurrentRotAxis].get2();
902
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
903

    
904
    mRotationAngle.removeAll();
905
    mRotationAngleStatic.set0(0);
906

    
907
    for(int i=0; i<NUM_CUBITS; i++)
908
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
909
        {
910
        int index = CUBITS[i].removeRotationNow(quat);
911
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
912
        }
913
    }
914

    
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916

    
917
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
918
    {
919
    if( wasRotateApplied() )
920
      {
921
      float angle = getAngle();
922
      mRotationAngleStatic.set0(angle);
923
      mRotationAngleFinal.set0(nearestAngleInDegrees);
924
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
925

    
926
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
927
      mRotationAngle.resetToBeginning();
928
      mRotationAngle.removeAll();
929
      mRotationAngle.add(mRotationAngleStatic);
930
      mRotationAngle.add(mRotationAngleMiddle);
931
      mRotationAngle.add(mRotationAngleFinal);
932
      mRotateEffect.notifyWhenFinished(listener);
933

    
934
      return mRotateEffect.getID();
935
      }
936

    
937
    return 0;
938
    }
939

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

    
942
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
943
    {
944
    if( wasRotateApplied() )
945
      {
946
      mCurrentRotAxis = axis;
947
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
948

    
949
      mRotationAngleStatic.set0(0.0f);
950
      mRotationAxis.set( mAxis[axis] );
951
      mRotationAngle.setDuration(durationMillis);
952
      mRotationAngle.resetToBeginning();
953
      mRotationAngle.add(new Static1D(0));
954
      mRotationAngle.add(new Static1D(angle));
955
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
956
      mRotateEffect.notifyWhenFinished(listener);
957

    
958
      return mRotateEffect.getID();
959
      }
960

    
961
    return 0;
962
    }
963

    
964
///////////////////////////////////////////////////////////////////////////////////////////////////
965

    
966
  void continueRotation(float angleInDegrees)
967
    {
968
    mRotationAngleStatic.set0(angleInDegrees);
969
    }
970

    
971
///////////////////////////////////////////////////////////////////////////////////////////////////
972

    
973
  synchronized void beginNewRotation(int axis, int row )
974
    {
975
    if( axis<0 || axis>=NUM_AXIS )
976
      {
977
      android.util.Log.e("object", "invalid rotation axis: "+axis);
978
      return;
979
      }
980
    if( row<0 || row>=mNumLayers[axis] )
981
      {
982
      android.util.Log.e("object", "invalid rotation row: "+row);
983
      return;
984
      }
985

    
986
    mCurrentRotAxis = axis;
987
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
988
    mRotationAngleStatic.set0(0.0f);
989
    mRotationAxis.set( mAxis[axis] );
990
    mRotationAngle.add(mRotationAngleStatic);
991
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
992
    }
993

    
994
///////////////////////////////////////////////////////////////////////////////////////////////////
995

    
996
  void setTextureMap(int cubit, int face, int newColor)
997
    {
998
    final float ratioW = 1.0f/mNumTexCols;
999
    final float ratioH = 1.0f/mNumTexRows;
1000
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1001
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1002
    int col = newColor%mNumTexCols;
1003

    
1004
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1005
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1006
    }
1007

    
1008
///////////////////////////////////////////////////////////////////////////////////////////////////
1009

    
1010
  void resetAllTextureMaps()
1011
    {
1012
    final float ratioW = 1.0f/mNumTexCols;
1013
    final float ratioH = 1.0f/mNumTexRows;
1014
    int cubColor, varColor, color, variant, row, col;
1015

    
1016
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1017
      {
1018
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1019
      variant = getCubitVariant(cubit,mNumLayers);
1020

    
1021
      for(int face=0; face<mNumCubitFaces; face++)
1022
        {
1023
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1024
        varColor = getVariantFaceColor(variant,face,mNumLayers);
1025
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1026
        row      = (mNumTexRows-1) - color/mNumTexCols;
1027
        col      = color%mNumTexCols;
1028

    
1029
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1030
        }
1031

    
1032
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1033
      }
1034
    }
1035

    
1036
///////////////////////////////////////////////////////////////////////////////////////////////////
1037

    
1038
  void releaseResources()
1039
    {
1040
    mTexture.markForDeletion();
1041
    mMesh.markForDeletion();
1042
    mEffects.markForDeletion();
1043

    
1044
    for(int j=0; j<NUM_CUBITS; j++)
1045
      {
1046
      CUBITS[j].releaseResources();
1047
      }
1048
    }
1049

    
1050
///////////////////////////////////////////////////////////////////////////////////////////////////
1051

    
1052
  synchronized void restorePreferences(SharedPreferences preferences)
1053
    {
1054
    boolean error = false;
1055

    
1056
    for(int i=0; i<NUM_CUBITS; i++)
1057
      {
1058
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1059

    
1060
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1061
        {
1062
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1063
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1064
        }
1065
      else
1066
        {
1067
        error = true;
1068
        }
1069
      }
1070

    
1071
    if( error )
1072
      {
1073
      for(int i=0; i<NUM_CUBITS; i++)
1074
        {
1075
        CUBITS[i].solve();
1076
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1077
        }
1078
      }
1079
    }
1080

    
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082

    
1083
  void savePreferences(SharedPreferences.Editor editor)
1084
    {
1085
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1086
    }
1087

    
1088
///////////////////////////////////////////////////////////////////////////////////////////////////
1089
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1090

    
1091
  private void createTexture()
1092
    {
1093
    Paint paint = new Paint();
1094
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1095
    Canvas canvas = new Canvas(mBitmap);
1096

    
1097
    paint.setAntiAlias(true);
1098
    paint.setTextAlign(Paint.Align.CENTER);
1099
    paint.setStyle(Paint.Style.FILL);
1100

    
1101
    paint.setColor(COLOR_BLACK);
1102
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1103

    
1104
    int texture = 0;
1105
    FactorySticker factory = FactorySticker.getInstance();
1106

    
1107
    for(int row=0; row<mNumTexRows; row++)
1108
      for(int col=0; col<mNumTexCols; col++)
1109
        {
1110
        if( texture>=NUM_TEXTURES ) break;
1111
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1112
        int color = getColor(texture%NUM_FACE_COLORS);
1113
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1114
        texture++;
1115
        }
1116
    }
1117

    
1118
///////////////////////////////////////////////////////////////////////////////////////////////////
1119

    
1120
  void setTexture()
1121
    {
1122
    if( mBitmap==null ) createTexture();
1123

    
1124
    if( !mTexture.setTexture(mBitmap) )
1125
      {
1126
      int max = DistortedLibrary.getMaxTextureSize();
1127
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max);
1128
      }
1129
    }
1130

    
1131
///////////////////////////////////////////////////////////////////////////////////////////////////
1132

    
1133
  void setObjectRatioNow(float sc, int nodeSize)
1134
    {
1135
    mObjectScreenRatio = sc;
1136
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1137
    mObjectScale.set(scale,scale,scale);
1138

    
1139
    if( mTouchControl ==null ) mTouchControl = getMovement();
1140
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1141
    }
1142

    
1143
///////////////////////////////////////////////////////////////////////////////////////////////////
1144

    
1145
  void setObjectRatio(float sizeChange, int nodeSize)
1146
    {
1147
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1148

    
1149
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1150
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1151

    
1152
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1153
    }
1154

    
1155
///////////////////////////////////////////////////////////////////////////////////////////////////
1156

    
1157
  void setNodeSize(int nodeSize)
1158
    {
1159
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1160
    }
1161

    
1162
///////////////////////////////////////////////////////////////////////////////////////////////////
1163

    
1164
  public float getRatio()
1165
    {
1166
    return mObjectScreenRatio;
1167
    }
1168

    
1169
///////////////////////////////////////////////////////////////////////////////////////////////////
1170

    
1171
  boolean isSolved()
1172
    {
1173
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1174
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1175
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1176
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1177

    
1178
    return false;
1179
    }
1180

    
1181
///////////////////////////////////////////////////////////////////////////////////////////////////
1182
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1183
// during setting up the initial position in the solver.
1184

    
1185
  int getCubit(float[] point3D)
1186
    {
1187
    float dist, minDist = Float.MAX_VALUE;
1188
    int currentBest=-1;
1189
    float multiplier = mNumLayers[0];
1190

    
1191
    point3D[0] *= multiplier;
1192
    point3D[1] *= multiplier;
1193
    point3D[2] *= multiplier;
1194

    
1195
    for(int i=0; i<NUM_CUBITS; i++)
1196
      {
1197
      dist = CUBITS[i].getDistSquared(point3D);
1198
      if( dist<minDist )
1199
        {
1200
        minDist = dist;
1201
        currentBest = i;
1202
        }
1203
      }
1204

    
1205
    return currentBest;
1206
    }
1207

    
1208
///////////////////////////////////////////////////////////////////////////////////////////////////
1209

    
1210
  int computeNearestAngle(int axis, float angle, float speed)
1211
    {
1212
    int[] basicArray = getBasicAngle();
1213
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1214
    int nearestAngle = 360/basicAngle;
1215

    
1216
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1217
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1218

    
1219
    if( tmp!=0 ) return nearestAngle*tmp;
1220

    
1221
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1222
    }
1223

    
1224
///////////////////////////////////////////////////////////////////////////////////////////////////
1225
// INTERNAL API - those are called from 'effects' package
1226
///////////////////////////////////////////////////////////////////////////////////////////////////
1227

    
1228
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1229
    {
1230
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1231
    }
1232

    
1233
///////////////////////////////////////////////////////////////////////////////////////////////////
1234

    
1235
  public Static4D getRotationQuat()
1236
    {
1237
    return mQuat;
1238
    }
1239

    
1240
///////////////////////////////////////////////////////////////////////////////////////////////////
1241

    
1242
  public float getSize()
1243
    {
1244
    return mSize;
1245
    }
1246

    
1247
///////////////////////////////////////////////////////////////////////////////////////////////////
1248

    
1249
  public void apply(Effect effect, int position)
1250
    {
1251
    mEffects.apply(effect, position);
1252
    }
1253

    
1254
///////////////////////////////////////////////////////////////////////////////////////////////////
1255

    
1256
  public void remove(long effectID)
1257
    {
1258
    mEffects.abortById(effectID);
1259
    }
1260

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

    
1263
  public MeshBase getObjectMesh()
1264
    {
1265
    return mMesh;
1266
    }
1267

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

    
1270
  public DistortedEffects getObjectEffects()
1271
    {
1272
    return mEffects;
1273
    }
1274

    
1275
///////////////////////////////////////////////////////////////////////////////////////////////////
1276
// PUBLIC API
1277
///////////////////////////////////////////////////////////////////////////////////////////////////
1278

    
1279
  public int getCubitFaceColorIndex(int cubit, int face)
1280
    {
1281
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1282

    
1283
    int x = (int)(texMap.get0()/texMap.get2());
1284
    int y = (int)(texMap.get1()/texMap.get3());
1285

    
1286
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1287
    }
1288

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

    
1291
  public int[] getNumLayers()
1292
    {
1293
    return mNumLayers;
1294
    }
1295

    
1296
///////////////////////////////////////////////////////////////////////////////////////////////////
1297

    
1298
  public synchronized void solve()
1299
    {
1300
    for(int i=0; i<NUM_CUBITS; i++)
1301
      {
1302
      CUBITS[i].solve();
1303
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1304
      }
1305
    }
1306

    
1307
///////////////////////////////////////////////////////////////////////////////////////////////////
1308

    
1309
  public Bitmap getStickerBitmap()
1310
    {
1311
    return mBitmap;
1312
    }
1313

    
1314
///////////////////////////////////////////////////////////////////////////////////////////////////
1315

    
1316
  public DistortedNode getNode()
1317
    {
1318
    return mNode;
1319
    }
1320

    
1321
///////////////////////////////////////////////////////////////////////////////////////////////////
1322

    
1323
  public ObjectType getObjectType()
1324
    {
1325
    return intGetObjectType(mNumLayers);
1326
    }
1327

    
1328
///////////////////////////////////////////////////////////////////////////////////////////////////
1329

    
1330
  public TouchControl getMovement()
1331
    {
1332
    if( mTouchControl ==null )
1333
      {
1334
      int[] numLayers = getNumLayers();
1335
      if( mCuts==null ) getCuts(numLayers);
1336
      if( mLayerRotatable==null ) mLayerRotatable = getLayerRotatable(numLayers);
1337
      if( mEnabled==null ) mEnabled = getEnabled();
1338

    
1339
      int movementType = getMovementType();
1340
      int movementSplit= getMovementSplit();
1341

    
1342
      switch(movementType)
1343
        {
1344
        case TC_TETRAHEDRON : mTouchControl = new TouchControlTetrahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1345
                              break;
1346
        case TC_HEXAHEDRON  : mTouchControl = new TouchControlHexahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1347
                              break;
1348
        case TC_OCTAHEDRON  : mTouchControl = new TouchControlOctahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1349
                              break;
1350
        case TC_DODECAHEDRON: mTouchControl = new TouchControlDodecahedron(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1351
                              break;
1352
        case TC_CUBOID      : float[] dist3D = getDist3D(numLayers);
1353
                              mTouchControl = new TouchControlCuboids(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled,dist3D);
1354
                              break;
1355
        }
1356
      }
1357
    return mTouchControl;
1358
    }
1359

    
1360
///////////////////////////////////////////////////////////////////////////////////////////////////
1361

    
1362
  public String getShortName()
1363
    {
1364
    return getObjectType().name();
1365
    }
1366

    
1367
///////////////////////////////////////////////////////////////////////////////////////////////////
1368

    
1369
  protected void setReader(JsonReader reader)
1370
    {
1371
    // empty
1372
    }
1373

    
1374
///////////////////////////////////////////////////////////////////////////////////////////////////
1375

    
1376
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1377

    
1378
  // for JSON only
1379
  public abstract int getSolvedFunctionIndex();
1380
  public abstract int getMovementType();
1381
  public abstract int getMovementSplit();
1382
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1383
  public abstract int[][][] getEnabled();
1384
  public abstract float[] getDist3D(int[] numLayers);
1385
  public abstract ScrambleState[] getScrambleStates();
1386
  public abstract float[][] getCuts(int[] numLayers);
1387
  public abstract Static4D[] getQuats();
1388
  public abstract int getNumStickerTypes(int[] numLayers);
1389
  public abstract ObjectSticker retSticker(int sticker);
1390
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1391
  public abstract ObjectShape getObjectShape(int variant);
1392
  public abstract int getNumCubitVariants(int[] numLayers);
1393
  public abstract float[][] getCubitPositions(int[] numLayers);
1394
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1395
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1396
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1397
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1398
  public abstract int getNumFaceColors();
1399
  public abstract int getNumCubitFaces();
1400
  public abstract float getScreenRatio();
1401
  public abstract int getColor(int face);
1402

    
1403
  // not only for JSON
1404
  public abstract Static3D[] getRotationAxis();
1405
  public abstract int[] getBasicAngle();
1406
  public abstract int getNumFaces();
1407
  public abstract String getObjectName();
1408
  public abstract String getInventor();
1409
  public abstract int getYearOfInvention();
1410
  public abstract int getComplexity();
1411
  public abstract int getFOV();
1412
  }
(10-10/12)