Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 0e1437c1

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

    
55
import java.io.DataInputStream;
56
import java.io.IOException;
57
import java.io.InputStream;
58
import java.util.Random;
59

    
60
import static org.distorted.objectlib.main.Movement.MOVEMENT_TETRAHEDRON;
61
import static org.distorted.objectlib.main.Movement.MOVEMENT_HEXAHEDRON;
62
import static org.distorted.objectlib.main.Movement.MOVEMENT_OCTAHEDRON;
63
import static org.distorted.objectlib.main.Movement.MOVEMENT_DODECAHEDRON;
64
import static org.distorted.objectlib.main.Movement.MOVEMENT_SHAPECHANGE;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
public abstract class TwistyObject
69
  {
70
  public static final int COLOR_YELLOW = 0xffffff00;
71
  public static final int COLOR_WHITE  = 0xffffffff;
72
  public static final int COLOR_BLUE   = 0xff0000ff;
73
  public static final int COLOR_GREEN  = 0xff00bb00;
74
  public static final int COLOR_RED    = 0xff990000;
75
  public static final int COLOR_ORANGE = 0xffff6200;
76
  public static final int COLOR_GREY   = 0xff727c7b;
77
  public static final int COLOR_VIOLET = 0xff7700bb;
78
  public static final int COLOR_BLACK  = 0xff000000;
79

    
80
  public static final int TEXTURE_HEIGHT = 256;
81
  static final int NUM_STICKERS_IN_ROW = 4;
82

    
83
  public static final float SQ2 = (float)Math.sqrt(2);
84
  public static final float SQ3 = (float)Math.sqrt(3);
85
  public static final float SQ5 = (float)Math.sqrt(5);
86
  public static final float SQ6 = (float)Math.sqrt(6);
87

    
88
  private static final float MAX_SIZE_CHANGE = 1.35f;
89
  private static final float MIN_SIZE_CHANGE = 0.75f;
90

    
91
  private static final Static3D CENTER = new Static3D(0,0,0);
92
  private static final int POST_ROTATION_MILLISEC = 500;
93

    
94
  protected int NUM_FACE_COLORS;
95
  protected int NUM_TEXTURES;
96
  protected Cubit[] CUBITS;
97

    
98
  MeshBase[] mMeshes;
99
  Static4D[] OBJECT_QUATS;
100
  int NUM_CUBITS;
101
  int NUM_AXIS;
102
  int NUM_QUATS;
103
  int SHIFT;
104

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

    
139
  //////////////////// SOLVED1 ////////////////////////
140

    
141
  private int[] mFaceMap;
142
  private int[][] mScramble;
143
  private int[] mColors;
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  TwistyObject(InputStream jsonStream, Static4D quat, Static3D move, InputStream meshStream)
148
    {
149
    JsonReader reader = new JsonReader();
150
    reader.parseJsonFile(jsonStream);
151
    setReader(reader);
152

    
153
    mNumLayers = reader.getNumLayers();
154
    mSize      = reader.getSize();
155
    initialize(quat,move,meshStream);
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  TwistyObject(int[] numLayers, float size, Static4D quat, Static3D move, InputStream meshStream)
161
    {
162
    mNumLayers = numLayers;
163
    mSize      = size;
164
    initialize(quat,move,meshStream);
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void initialize(Static4D quat, Static3D move, InputStream stream)
170
    {
171
    mQuat = quat;
172
    mOrigPos = getCubitPositions(mNumLayers);
173
    mAxis = getRotationAxis();
174
    mInitScreenRatio = getScreenRatio();
175
    mNumCubitFaces = getNumCubitFaces();
176
    mSolvedFunctionIndex = getSolvedFunctionIndex();
177

    
178
    int numAxis = mAxis.length;
179
    SHIFT = -1;
180

    
181
    mCuts = getCuts(mNumLayers);
182
    mNumCuts = new int[numAxis];
183
    for(int i=0; i<numAxis; i++)
184
      {
185
      if( SHIFT<mNumLayers[i] ) SHIFT = mNumLayers[i];
186
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
187
      }
188

    
189
    OBJECT_QUATS = getQuats();
190
    NUM_CUBITS  = mOrigPos.length;
191
    NUM_FACE_COLORS = getNumFaceColors();
192
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
193
    NUM_AXIS = mAxis.length;
194
    NUM_QUATS = OBJECT_QUATS.length;
195

    
196
    int scramblingType = getScrambleType();
197
    ScrambleState[] states = getScrambleStates();
198
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
199

    
200
    boolean bandaged=false;
201

    
202
    for(int c=0; c<NUM_CUBITS; c++)
203
      {
204
      if( mOrigPos[c].length>3 )
205
        {
206
        bandaged=true;
207
        break;
208
        }
209
      }
210

    
211
    mIsBandaged = bandaged;
212
    mQuatDebug = new int[NUM_CUBITS];
213

    
214
    mRotationAngle= new Dynamic1D();
215
    mRotationAxis = new Static3D(1,0,0);
216
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
217

    
218
    mRotationAngleStatic = new Static1D(0);
219
    mRotationAngleMiddle = new Static1D(0);
220
    mRotationAngleFinal  = new Static1D(0);
221

    
222
    mObjectScale = new Static3D(1,1,1);
223
    setObjectRatioNow(1.0f,1.0f,720);
224

    
225
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
226
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(mQuat, CENTER);
227
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
228

    
229
    mNumTexCols = NUM_STICKERS_IN_ROW;
230
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
231

    
232
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
233

    
234
    CUBITS = new Cubit[NUM_CUBITS];
235
    createMeshAndCubits(stream);
236
    createDataStructuresForSolved(mNumLayers);
237

    
238
    mTexture = new DistortedTexture();
239
    mEffects = new DistortedEffects();
240

    
241
    createTexture();
242

    
243
    for(int q=0; q<NUM_QUATS; q++)
244
      {
245
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
246
      vq.setMeshAssociation(0,q);
247
      mEffects.apply(vq);
248
      }
249

    
250
    mEffects.apply(mRotateEffect);
251
    mEffects.apply(quatEffect);
252
    mEffects.apply(scaleEffect);
253
    mEffects.apply(moveEffect);
254

    
255
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  private Static3D getPos(float[] origPos)
261
    {
262
    int len = origPos.length/3;
263
    float sumX = 0.0f;
264
    float sumY = 0.0f;
265
    float sumZ = 0.0f;
266

    
267
    for(int i=0; i<len; i++)
268
      {
269
      sumX += origPos[3*i  ];
270
      sumY += origPos[3*i+1];
271
      sumZ += origPos[3*i+2];
272
      }
273

    
274
    sumX /= len;
275
    sumY /= len;
276
    sumZ /= len;
277

    
278
    return new Static3D(sumX,sumY,sumZ);
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  private void createMeshAndCubits(InputStream stream)
284
    {
285
    if( stream!=null )
286
      {
287
      DataInputStream dos = new DataInputStream(stream);
288
      mMesh = new MeshFile(dos);
289

    
290
      try
291
        {
292
        stream.close();
293
        }
294
      catch(IOException e)
295
        {
296
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
297
        }
298

    
299
      for(int i=0; i<NUM_CUBITS; i++)
300
        {
301
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
302
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
303
        }
304

    
305
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
306
      }
307
    else
308
      {
309
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
310

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

    
320
      mMesh = new MeshJoined(cubitMesh);
321
      resetAllTextureMaps();
322
      }
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  private MeshBase createCubitMesh(int cubit, int[] numLayers)
328
    {
329
    int variant = getCubitVariant(cubit,numLayers);
330

    
331
    if( mMeshes==null )
332
      {
333
      FactoryCubit factory = FactoryCubit.getInstance();
334
      factory.clear();
335
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
336
      }
337

    
338
    if( mMeshes[variant]==null )
339
      {
340
      ObjectShape shape = getObjectShape(variant);
341
      FactoryCubit factory = FactoryCubit.getInstance();
342
      factory.createNewFaceTransform(shape);
343
      mMeshes[variant] = factory.createRoundedSolid(shape);
344
      }
345

    
346
    MeshBase mesh = mMeshes[variant].copy(true);
347
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
348
    mesh.apply(quat,0xffffffff,0);
349

    
350
    return mesh;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  private void createDataStructuresForSolved(int[] numLayers)
356
    {
357
    mTmpQuats = new int[NUM_QUATS];
358
    mSolvedQuats = new int[NUM_CUBITS][];
359

    
360
    for(int c=0; c<NUM_CUBITS; c++)
361
      {
362
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
363
      }
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  private int getMultQuat(int index1, int index2)
369
    {
370
    if( mQuatMult==null )
371
      {
372
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
373

    
374
      for(int i=0; i<NUM_QUATS; i++)
375
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
376
      }
377

    
378
    if( mQuatMult[index1][index2]==-1 )
379
      {
380
      mQuatMult[index1][index2] = mulQuat(index1,index2);
381
      }
382

    
383
    return mQuatMult[index1][index2];
384
    }
385

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

    
394
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
395
    {
396
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
397
    int variant = getCubitVariant(cubit,numLayers);
398

    
399
    for(int face=0; face<mNumCubitFaces; face++)
400
      {
401
      varColor = getVariantFaceColor(variant,face,numLayers);
402
      cubColor = getCubitFaceColor(cubit,face,numLayers);
403

    
404
      if( varColor>=0 && cubColor>=0 )
405
        {
406
        numNonBlack++;
407
        nonBlackIndex = cubColor;
408
        }
409
      }
410

    
411
    if( numNonBlack==0 ) return -1;
412
    if( numNonBlack>=2 ) return -2;
413

    
414
    return nonBlackIndex;
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  protected boolean shouldResetTextureMaps()
420
    {
421
    return false;
422
    }
423

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

    
426
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
427
    {
428
    final float MAXD = 0.0001f;
429
    float x = faceAx.get0();
430
    float y = faceAx.get1();
431
    float z = faceAx.get2();
432
    float a,dx,dy,dz,qx,qy,qz;
433
    Static4D quat;
434

    
435
    int len = quats.length;
436
    int place = 0;
437

    
438
    for(int q=1; q<len; q++)
439
      {
440
      quat = quats[q];
441
      qx = quat.get0();
442
      qy = quat.get1();
443
      qz = quat.get2();
444

    
445
           if( x!=0.0f ) { a = qx/x; }
446
      else if( y!=0.0f ) { a = qy/y; }
447
      else               { a = qz/z; }
448

    
449
      dx = a*x-qx;
450
      dy = a*y-qy;
451
      dz = a*z-qz;
452

    
453
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
454
        {
455
        mTmpQuats[place++] = q;
456
        }
457
      }
458

    
459
    if( place!=0 )
460
      {
461
      int[] ret = new int[place];
462
      System.arraycopy(mTmpQuats,0,ret,0,place);
463
      return ret;
464
      }
465

    
466
    return null;
467
    }
468

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

    
471
  private boolean isSolved0()
472
    {
473
    int len, q1,q = CUBITS[0].mQuatIndex;
474
    int[] solved;
475
    boolean skip;
476

    
477
    for(int c=1; c<NUM_CUBITS; c++)
478
      {
479
      q1 = CUBITS[c].mQuatIndex;
480

    
481
      if( q1==q ) continue;
482

    
483
      skip = false;
484
      solved = mSolvedQuats[c];
485
      len = solved==null ? 0:solved.length;
486

    
487
      for(int i=0; i<len; i++)
488
        {
489
        if( q1==getMultQuat(q,solved[i]) )
490
          {
491
          skip = true;
492
          break;
493
          }
494
        }
495

    
496
      if( !skip ) return false;
497
      }
498

    
499
    return true;
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
  private int computeScramble(int quatNum, int centerNum)
505
    {
506
    float MAXDIFF = 0.01f;
507
    float[] center= mOrigPos[centerNum];
508
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
509
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
510

    
511
    float x = result.get0();
512
    float y = result.get1();
513
    float z = result.get2();
514

    
515
    for(int c=0; c<NUM_CUBITS; c++)
516
      {
517
      float[] cent = mOrigPos[c];
518

    
519
      float qx = cent[0] - x;
520
      float qy = cent[1] - y;
521
      float qz = cent[2] - z;
522

    
523
      if( qx>-MAXDIFF && qx<MAXDIFF &&
524
          qy>-MAXDIFF && qy<MAXDIFF &&
525
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
526
      }
527

    
528
    return -1;
529
    }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532
// Dino4 uses this. It is solved if and only if groups of cubits
533
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
534
// or
535
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
536
// are all the same color.
537

    
538
  private boolean isSolved1()
539
    {
540
    if( mScramble==null )
541
      {
542
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
543
      mColors   = new int[NUM_CUBITS];
544

    
545
      for(int q=0; q<NUM_QUATS; q++)
546
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
547
      }
548

    
549
    if( mFaceMap==null )
550
      {
551
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
552
      }
553

    
554
    for(int c=0; c<NUM_CUBITS; c++)
555
      {
556
      int index = mScramble[CUBITS[c].mQuatIndex][c];
557
      mColors[index] = mFaceMap[c];
558
      }
559

    
560
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
561
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
562
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
563

    
564
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
565
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
566
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
567

    
568
    return false;
569
    }
570

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

    
589
  private boolean isSolved2()
590
    {
591
    int qX = CUBITS[0].mQuatIndex;
592
    int qY = CUBITS[1].mQuatIndex;
593
    int qZ = CUBITS[4].mQuatIndex;
594

    
595
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
596
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
597
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
598
      {
599
      return false;
600
      }
601

    
602
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
603
    }
604

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

    
613
  private boolean isSolved3()
614
    {
615
    int index = CUBITS[0].mQuatIndex;
616

    
617
    if( CUBITS[1].mQuatIndex!=index ) return false;
618

    
619
    boolean solved = true;
620

    
621
    for(int i=2; i<NUM_CUBITS; i++)
622
      {
623
      if( CUBITS[i].mQuatIndex!=index )
624
        {
625
        solved = false;
626
        break;
627
        }
628
      }
629

    
630
    if( solved ) return true;
631

    
632
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
633
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
634

    
635
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
636
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
637
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
638
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
639

    
640
    return true;
641
    }
642

    
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644

    
645
  int computeRow(float[] pos, int axisIndex)
646
    {
647
    int ret=0;
648
    int len = pos.length / 3;
649
    Static3D axis = mAxis[axisIndex];
650
    float axisX = axis.get0();
651
    float axisY = axis.get1();
652
    float axisZ = axis.get2();
653
    float casted;
654

    
655
    for(int i=0; i<len; i++)
656
      {
657
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
658
      ret |= computeSingleRow(axisIndex,casted);
659
      }
660

    
661
    return ret;
662
    }
663

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665

    
666
  private int computeSingleRow(int axisIndex,float casted)
667
    {
668
    int num = mNumCuts[axisIndex];
669

    
670
    for(int i=0; i<num; i++)
671
      {
672
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
673
      }
674

    
675
    return (1<<num);
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

    
680
  private boolean wasRotateApplied()
681
    {
682
    return mEffects.exists(mRotateEffect.getID());
683
    }
684

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

    
687
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
688
    {
689
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
690
    }
691

    
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
// note the minus in front of the sin() - we rotate counterclockwise
694
// when looking towards the direction where the axis increases in values.
695

    
696
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
697
    {
698
    Static3D axis = mAxis[axisIndex];
699

    
700
    while( angleInDegrees<0 ) angleInDegrees += 360;
701
    angleInDegrees %= 360;
702
    
703
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
704
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
705

    
706
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
707
    }
708

    
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710

    
711
  private synchronized void setupPosition(int[][] moves)
712
    {
713
    if( moves!=null )
714
      {
715
      Static4D quat;
716
      int index, axis, rowBitmap, angle;
717
      int[] basic = getBasicAngle();
718

    
719
      for(int[] move: moves)
720
        {
721
        axis     = move[0];
722
        rowBitmap= move[1];
723
        angle    = move[2]*(360/basic[axis]);
724
        quat     = makeQuaternion(axis,angle);
725

    
726
        for(int j=0; j<NUM_CUBITS; j++)
727
          if( belongsToRotation(j,axis,rowBitmap) )
728
            {
729
            index = CUBITS[j].removeRotationNow(quat);
730
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
731
            }
732
        }
733
      }
734
    }
735

    
736
///////////////////////////////////////////////////////////////////////////////////////////////////
737

    
738
  public int getScrambleType()
739
    {
740
    return 0;
741
    }
742

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

    
745
  int computeBitmapFromRow(int rowBitmap, int axis)
746
    {
747
    if( mIsBandaged )
748
      {
749
      int bitmap, initBitmap=0;
750

    
751
      while( initBitmap!=rowBitmap )
752
        {
753
        initBitmap = rowBitmap;
754

    
755
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
756
          {
757
          bitmap = CUBITS[cubit].getRotRow(axis);
758
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
759
          }
760
        }
761
      }
762

    
763
    return rowBitmap;
764
    }
765

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

    
770
  void clampPos(float[] pos, int offset)
771
    {
772
    float currError, minError = Float.MAX_VALUE;
773
    int minErrorIndex1 = -1;
774
    int minErrorIndex2 = -1;
775

    
776
    float x = pos[offset  ];
777
    float y = pos[offset+1];
778
    float z = pos[offset+2];
779

    
780
    float xo,yo,zo;
781

    
782
    for(int i=0; i<NUM_CUBITS; i++)
783
      {
784
      int len = mOrigPos[i].length / 3;
785

    
786
      for(int j=0; j<len; j++)
787
        {
788
        xo = mOrigPos[i][3*j  ];
789
        yo = mOrigPos[i][3*j+1];
790
        zo = mOrigPos[i][3*j+2];
791

    
792
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
793

    
794
        if( currError<minError )
795
          {
796
          minError = currError;
797
          minErrorIndex1 = i;
798
          minErrorIndex2 = j;
799
          }
800
        }
801
      }
802

    
803
    if( minError< 0.1f ) // TODO: 0.1 ?
804
      {
805
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
806
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
807
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
808
      }
809
    }
810

    
811
///////////////////////////////////////////////////////////////////////////////////////////////////
812
// remember about the double cover or unit quaternions!
813

    
814
  int mulQuat(int q1, int q2)
815
    {
816
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
817

    
818
    float rX = result.get0();
819
    float rY = result.get1();
820
    float rZ = result.get2();
821
    float rW = result.get3();
822

    
823
    final float MAX_ERROR = 0.1f;
824
    float dX,dY,dZ,dW;
825

    
826
    for(int i=0; i<NUM_QUATS; i++)
827
      {
828
      dX = OBJECT_QUATS[i].get0() - rX;
829
      dY = OBJECT_QUATS[i].get1() - rY;
830
      dZ = OBJECT_QUATS[i].get2() - rZ;
831
      dW = OBJECT_QUATS[i].get3() - rW;
832

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

    
838
      dX = OBJECT_QUATS[i].get0() + rX;
839
      dY = OBJECT_QUATS[i].get1() + rY;
840
      dZ = OBJECT_QUATS[i].get2() + rZ;
841
      dW = OBJECT_QUATS[i].get3() + rW;
842

    
843
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
844
          dY<MAX_ERROR && dY>-MAX_ERROR &&
845
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
846
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
847
      }
848

    
849
    return -1;
850
    }
851

    
852
///////////////////////////////////////////////////////////////////////////////////////////////////
853

    
854
  private float getAngle()
855
    {
856
    int pointNum = mRotationAngle.getNumPoints();
857

    
858
    if( pointNum>=1 )
859
      {
860
      return mRotationAngle.getPoint(pointNum-1).get0();
861
      }
862
    else
863
      {
864
      mInterface.reportProblem("points in RotationAngle: "+pointNum);
865
      return 0;
866
      }
867
    }
868

    
869
///////////////////////////////////////////////////////////////////////////////////////////////////
870

    
871
  void setLibInterface(ObjectLibInterface inter)
872
    {
873
    mInterface = inter;
874
    }
875

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

    
878
  void initializeObject(int[][] moves)
879
    {
880
    solve();
881
    setupPosition(moves);
882
    }
883

    
884
///////////////////////////////////////////////////////////////////////////////////////////////////
885

    
886
  synchronized void removeRotationNow()
887
    {
888
    float angle = getAngle();
889
    double nearestAngleInRadians = angle*Math.PI/180;
890
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
891
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
892
    float axisX = mAxis[mCurrentRotAxis].get0();
893
    float axisY = mAxis[mCurrentRotAxis].get1();
894
    float axisZ = mAxis[mCurrentRotAxis].get2();
895
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
896

    
897
    mRotationAngle.removeAll();
898
    mRotationAngleStatic.set0(0);
899

    
900
    for(int i=0; i<NUM_CUBITS; i++)
901
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
902
        {
903
        int index = CUBITS[i].removeRotationNow(quat);
904
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
905
        }
906
    }
907

    
908
///////////////////////////////////////////////////////////////////////////////////////////////////
909

    
910
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
911
    {
912
    if( wasRotateApplied() )
913
      {
914
      float angle = getAngle();
915
      mRotationAngleStatic.set0(angle);
916
      mRotationAngleFinal.set0(nearestAngleInDegrees);
917
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
918

    
919
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
920
      mRotationAngle.resetToBeginning();
921
      mRotationAngle.removeAll();
922
      mRotationAngle.add(mRotationAngleStatic);
923
      mRotationAngle.add(mRotationAngleMiddle);
924
      mRotationAngle.add(mRotationAngleFinal);
925
      mRotateEffect.notifyWhenFinished(listener);
926

    
927
      return mRotateEffect.getID();
928
      }
929

    
930
    return 0;
931
    }
932

    
933
///////////////////////////////////////////////////////////////////////////////////////////////////
934

    
935
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
936
    {
937
    if( wasRotateApplied() )
938
      {
939
      mCurrentRotAxis = axis;
940
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
941

    
942
      mRotationAngleStatic.set0(0.0f);
943
      mRotationAxis.set( mAxis[axis] );
944
      mRotationAngle.setDuration(durationMillis);
945
      mRotationAngle.resetToBeginning();
946
      mRotationAngle.add(new Static1D(0));
947
      mRotationAngle.add(new Static1D(angle));
948
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
949
      mRotateEffect.notifyWhenFinished(listener);
950

    
951
      return mRotateEffect.getID();
952
      }
953

    
954
    return 0;
955
    }
956

    
957
///////////////////////////////////////////////////////////////////////////////////////////////////
958

    
959
  void continueRotation(float angleInDegrees)
960
    {
961
    mRotationAngleStatic.set0(angleInDegrees);
962
    }
963

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

    
966
  synchronized void beginNewRotation(int axis, int row )
967
    {
968
    if( axis<0 || axis>=NUM_AXIS )
969
      {
970
      android.util.Log.e("object", "invalid rotation axis: "+axis);
971
      return;
972
      }
973
    if( row<0 || row>=mNumLayers[axis] )
974
      {
975
      android.util.Log.e("object", "invalid rotation row: "+row);
976
      return;
977
      }
978

    
979
    mCurrentRotAxis = axis;
980
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
981
    mRotationAngleStatic.set0(0.0f);
982
    mRotationAxis.set( mAxis[axis] );
983
    mRotationAngle.add(mRotationAngleStatic);
984
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
985
    }
986

    
987
///////////////////////////////////////////////////////////////////////////////////////////////////
988

    
989
  void setTextureMap(int cubit, int face, int newColor)
990
    {
991
    final float ratioW = 1.0f/mNumTexCols;
992
    final float ratioH = 1.0f/mNumTexRows;
993
    final Static4D[] maps = new Static4D[mNumCubitFaces];
994
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
995
    int col = newColor%mNumTexCols;
996

    
997
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
998
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
999
    }
1000

    
1001
///////////////////////////////////////////////////////////////////////////////////////////////////
1002

    
1003
  void resetAllTextureMaps()
1004
    {
1005
    final float ratioW = 1.0f/mNumTexCols;
1006
    final float ratioH = 1.0f/mNumTexRows;
1007
    int cubColor, varColor, color, variant, row, col;
1008

    
1009
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1010
      {
1011
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1012
      variant = getCubitVariant(cubit,mNumLayers);
1013

    
1014
      for(int face=0; face<mNumCubitFaces; face++)
1015
        {
1016
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1017
        varColor = getVariantFaceColor(variant,face,mNumLayers);
1018
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1019
        row      = (mNumTexRows-1) - color/mNumTexCols;
1020
        col      = color%mNumTexCols;
1021

    
1022
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1023
        }
1024

    
1025
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1026
      }
1027
    }
1028

    
1029
///////////////////////////////////////////////////////////////////////////////////////////////////
1030

    
1031
  void releaseResources()
1032
    {
1033
    mTexture.markForDeletion();
1034
    mMesh.markForDeletion();
1035
    mEffects.markForDeletion();
1036

    
1037
    for(int j=0; j<NUM_CUBITS; j++)
1038
      {
1039
      CUBITS[j].releaseResources();
1040
      }
1041
    }
1042

    
1043
///////////////////////////////////////////////////////////////////////////////////////////////////
1044

    
1045
  synchronized void restorePreferences(SharedPreferences preferences)
1046
    {
1047
    boolean error = false;
1048

    
1049
    for(int i=0; i<NUM_CUBITS; i++)
1050
      {
1051
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1052

    
1053
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1054
        {
1055
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1056
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1057
        }
1058
      else
1059
        {
1060
        error = true;
1061
        }
1062
      }
1063

    
1064
    if( error )
1065
      {
1066
      for(int i=0; i<NUM_CUBITS; i++)
1067
        {
1068
        CUBITS[i].solve();
1069
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1070
        }
1071
      }
1072
    }
1073

    
1074
///////////////////////////////////////////////////////////////////////////////////////////////////
1075

    
1076
  void savePreferences(SharedPreferences.Editor editor)
1077
    {
1078
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1079
    }
1080

    
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1083

    
1084
  void createTexture()
1085
    {
1086
    Bitmap bitmap;
1087

    
1088
    Paint paint = new Paint();
1089
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1090
    Canvas canvas = new Canvas(bitmap);
1091

    
1092
    paint.setAntiAlias(true);
1093
    paint.setTextAlign(Paint.Align.CENTER);
1094
    paint.setStyle(Paint.Style.FILL);
1095

    
1096
    paint.setColor(COLOR_BLACK);
1097
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1098

    
1099
    int texture = 0;
1100
    FactorySticker factory = FactorySticker.getInstance();
1101

    
1102
    for(int row=0; row<mNumTexRows; row++)
1103
      for(int col=0; col<mNumTexCols; col++)
1104
        {
1105
        if( texture>=NUM_TEXTURES ) break;
1106
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1107
        int color = getColor(texture%NUM_FACE_COLORS);
1108
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1109
        texture++;
1110
        }
1111

    
1112
    if( !mTexture.setTexture(bitmap) )
1113
      {
1114
      int max = DistortedLibrary.getMaxTextureSize();
1115
      mInterface.reportProblem("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
1116
      }
1117
    }
1118

    
1119
///////////////////////////////////////////////////////////////////////////////////////////////////
1120

    
1121
  void setObjectRatioNow(float sc, float nodeScale, int nodeMinSize)
1122
    {
1123
    mObjectScreenRatio = sc*nodeScale;
1124
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeMinSize/mSize;
1125
    mObjectScale.set(scale,scale,scale);
1126
    }
1127

    
1128
///////////////////////////////////////////////////////////////////////////////////////////////////
1129

    
1130
  void setObjectRatio(float sizeChange, float nodeScale, int nodeMinSize)
1131
    {
1132
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1133

    
1134
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1135
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1136

    
1137
    setObjectRatioNow(mObjectScreenRatio, nodeScale, nodeMinSize);
1138
    }
1139

    
1140
///////////////////////////////////////////////////////////////////////////////////////////////////
1141

    
1142
  float getObjectRatio()
1143
    {
1144
    return mObjectScreenRatio*mInitScreenRatio;
1145
    }
1146

    
1147
///////////////////////////////////////////////////////////////////////////////////////////////////
1148

    
1149
  public float getRatio()
1150
    {
1151
    return mObjectScreenRatio;
1152
    }
1153

    
1154
///////////////////////////////////////////////////////////////////////////////////////////////////
1155

    
1156
  boolean isSolved()
1157
    {
1158
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1159
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1160
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1161
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1162

    
1163
    return false;
1164
    }
1165

    
1166
///////////////////////////////////////////////////////////////////////////////////////////////////
1167
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1168
// during setting up the initial position in the solver.
1169

    
1170
  int getCubit(float[] point3D)
1171
    {
1172
    float dist, minDist = Float.MAX_VALUE;
1173
    int currentBest=-1;
1174
    float multiplier = mNumLayers[0];
1175

    
1176
    point3D[0] *= multiplier;
1177
    point3D[1] *= multiplier;
1178
    point3D[2] *= multiplier;
1179

    
1180
    for(int i=0; i<NUM_CUBITS; i++)
1181
      {
1182
      dist = CUBITS[i].getDistSquared(point3D);
1183
      if( dist<minDist )
1184
        {
1185
        minDist = dist;
1186
        currentBest = i;
1187
        }
1188
      }
1189

    
1190
    return currentBest;
1191
    }
1192

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

    
1195
  int computeNearestAngle(int axis, float angle, float speed)
1196
    {
1197
    int[] basicArray = getBasicAngle();
1198
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1199
    int nearestAngle = 360/basicAngle;
1200

    
1201
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1202
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1203

    
1204
    if( tmp!=0 ) return nearestAngle*tmp;
1205

    
1206
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1207
    }
1208

    
1209
///////////////////////////////////////////////////////////////////////////////////////////////////
1210
// INTERNAL API - those are called from 'effects' package
1211
///////////////////////////////////////////////////////////////////////////////////////////////////
1212

    
1213
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1214
    {
1215
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1216
    }
1217

    
1218
///////////////////////////////////////////////////////////////////////////////////////////////////
1219

    
1220
  public Static4D getRotationQuat()
1221
    {
1222
    return mQuat;
1223
    }
1224

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

    
1227
  public float getSize()
1228
    {
1229
    return mSize;
1230
    }
1231

    
1232
///////////////////////////////////////////////////////////////////////////////////////////////////
1233

    
1234
  public void apply(Effect effect, int position)
1235
    {
1236
    mEffects.apply(effect, position);
1237
    }
1238

    
1239
///////////////////////////////////////////////////////////////////////////////////////////////////
1240

    
1241
  public void remove(long effectID)
1242
    {
1243
    mEffects.abortById(effectID);
1244
    }
1245

    
1246
///////////////////////////////////////////////////////////////////////////////////////////////////
1247

    
1248
  public MeshBase getObjectMesh()
1249
    {
1250
    return mMesh;
1251
    }
1252

    
1253
///////////////////////////////////////////////////////////////////////////////////////////////////
1254

    
1255
  public DistortedEffects getObjectEffects()
1256
    {
1257
    return mEffects;
1258
    }
1259

    
1260
///////////////////////////////////////////////////////////////////////////////////////////////////
1261
// PUBLIC API
1262
///////////////////////////////////////////////////////////////////////////////////////////////////
1263

    
1264
  public int getCubitFaceColorIndex(int cubit, int face)
1265
    {
1266
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1267

    
1268
    int x = (int)(texMap.get0()/texMap.get2());
1269
    int y = (int)(texMap.get1()/texMap.get3());
1270

    
1271
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1272
    }
1273

    
1274
///////////////////////////////////////////////////////////////////////////////////////////////////
1275

    
1276
  public int[] getNumLayers()
1277
    {
1278
    return mNumLayers;
1279
    }
1280

    
1281
///////////////////////////////////////////////////////////////////////////////////////////////////
1282

    
1283
  public synchronized void solve()
1284
    {
1285
    for(int i=0; i<NUM_CUBITS; i++)
1286
      {
1287
      CUBITS[i].solve();
1288
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1289
      }
1290
    }
1291

    
1292
///////////////////////////////////////////////////////////////////////////////////////////////////
1293

    
1294
  public DistortedNode getNode()
1295
    {
1296
    return mNode;
1297
    }
1298

    
1299
///////////////////////////////////////////////////////////////////////////////////////////////////
1300

    
1301
  public ObjectType getObjectType()
1302
    {
1303
    return intGetObjectType(mNumLayers);
1304
    }
1305

    
1306
///////////////////////////////////////////////////////////////////////////////////////////////////
1307

    
1308
  public Movement getMovement()
1309
    {
1310
    if( mMovement==null )
1311
      {
1312
      int[] numLayers = getNumLayers();
1313
      if( mCuts==null ) getCuts(numLayers);
1314
      if( mLayerRotatable==null ) mLayerRotatable = getLayerRotatable(numLayers);
1315
      if( mEnabled==null ) mEnabled = getEnabled();
1316

    
1317
      int movementType = getMovementType();
1318
      int movementSplit= getMovementSplit();
1319

    
1320
      switch(movementType)
1321
        {
1322
        case MOVEMENT_TETRAHEDRON : mMovement = new Movement4(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1323
                                    break;
1324
        case MOVEMENT_HEXAHEDRON  : mMovement = new Movement6(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1325
                                    break;
1326
        case MOVEMENT_OCTAHEDRON  : mMovement = new Movement8(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1327
                                    break;
1328
        case MOVEMENT_DODECAHEDRON: mMovement = new Movement12(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1329
                                    break;
1330
        case MOVEMENT_SHAPECHANGE : float[] dist3D = getDist3D(numLayers);
1331
                                    mMovement = new MovementC(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled,dist3D);
1332
                                    break;
1333
        }
1334
      }
1335
    return mMovement;
1336
    }
1337

    
1338
///////////////////////////////////////////////////////////////////////////////////////////////////
1339

    
1340
  public String getShortName()
1341
    {
1342
    return getObjectType().name();
1343
    }
1344

    
1345
///////////////////////////////////////////////////////////////////////////////////////////////////
1346

    
1347
  protected void setReader(JsonReader reader)
1348
    {
1349
    // empty
1350
    }
1351

    
1352
///////////////////////////////////////////////////////////////////////////////////////////////////
1353

    
1354
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1355

    
1356
  // for JSON only
1357
  public abstract int getSolvedFunctionIndex();
1358
  public abstract int getMovementType();
1359
  public abstract int getMovementSplit();
1360
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1361
  public abstract int[][][] getEnabled();
1362
  public abstract float[] getDist3D(int[] numLayers);
1363
  public abstract ScrambleState[] getScrambleStates();
1364
  public abstract float[][] getCuts(int[] numLayers);
1365
  public abstract Static4D[] getQuats();
1366
  public abstract int getNumStickerTypes(int[] numLayers);
1367
  public abstract ObjectSticker retSticker(int sticker);
1368
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1369
  public abstract ObjectShape getObjectShape(int variant);
1370
  public abstract int getNumCubitVariants(int[] numLayers);
1371
  public abstract float[][] getCubitPositions(int[] numLayers);
1372
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1373
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1374
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1375
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1376
  public abstract int getNumFaceColors();
1377
  public abstract int getNumCubitFaces();
1378
  public abstract float getScreenRatio();
1379
  public abstract int getColor(int face);
1380

    
1381
  // not only for JSON
1382
  public abstract Static3D[] getRotationAxis();
1383
  public abstract int[] getBasicAngle();
1384
  public abstract int getNumFaces();
1385
  public abstract String getObjectName();
1386
  public abstract String getInventor();
1387
  public abstract int getYearOfInvention();
1388
  public abstract int getComplexity();
1389
  public abstract int getFOV();
1390
  }
(16-16/18)