Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 3a1efb32

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 java.io.DataInputStream;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.util.Random;
26

    
27
import android.content.SharedPreferences;
28
import android.graphics.Bitmap;
29
import android.graphics.Canvas;
30
import android.graphics.Paint;
31

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

    
52
import org.distorted.objectlib.helpers.FactoryCubit;
53
import org.distorted.objectlib.helpers.FactorySticker;
54
import org.distorted.objectlib.helpers.ObjectLibInterface;
55
import org.distorted.objectlib.helpers.ObjectShape;
56
import org.distorted.objectlib.helpers.ObjectSticker;
57
import org.distorted.objectlib.helpers.ScrambleState;
58
import org.distorted.objectlib.json.JsonReader;
59
import org.distorted.objectlib.touchcontrol.*;
60

    
61
import static org.distorted.objectlib.touchcontrol.TouchControl.*;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

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

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

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

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

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

    
137
  //////////////////// SOLVED1 ////////////////////////
138

    
139
  private int[] mFaceMap;
140
  private int[][] mScramble;
141
  private int[] mColors;
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

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

    
151
    mNumLayers = reader.getNumLayers();
152
    mSize      = reader.getSize();
153
    initialize(quat,move,scale,meshStream);
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

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

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

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

    
176
    int numAxis = mAxis.length;
177
    SHIFT = -1;
178

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

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

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

    
198
    boolean bandaged=false;
199

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

    
209
    mIsBandaged = bandaged;
210
    mQuatDebug = new int[NUM_CUBITS];
211

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

    
216
    mRotationAngleStatic = new Static1D(0);
217
    mRotationAngleMiddle = new Static1D(0);
218
    mRotationAngleFinal  = new Static1D(0);
219

    
220
    mObjectScale = new Static3D(scale,scale,scale);
221
    setObjectRatioNow(scale,720);
222

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

    
227
    mNumTexCols = NUM_STICKERS_IN_ROW;
228
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
229

    
230
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
231

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

    
236
    mTexture = new DistortedTexture();
237
    mEffects = new DistortedEffects();
238

    
239
    setTexture();
240

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

    
248
    mEffects.apply(mRotateEffect);
249
    mEffects.apply(quatEffect);
250
    mEffects.apply(scaleEffect);
251
    mEffects.apply(moveEffect);
252

    
253
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

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

    
272
    sumX /= len;
273
    sumY /= len;
274
    sumZ /= len;
275

    
276
    return new Static3D(sumX,sumY,sumZ);
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

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

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

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

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

    
318
      mMesh = new MeshJoined(cubitMesh);
319
      resetAllTextureMaps();
320
      }
321
    }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324

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

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

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

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

    
348
    return mesh;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

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

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

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

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

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

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

    
381
    return mQuatMult[index1][index2];
382
    }
383

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

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

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

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

    
409
    if( numNonBlack==0 ) return -1;
410
    if( numNonBlack>=2 ) return -2;
411

    
412
    return nonBlackIndex;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public boolean shouldResetTextureMaps()
418
    {
419
    return false;
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

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

    
433
    int len = quats.length;
434
    int place = 0;
435

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

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

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

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

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

    
464
    return null;
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

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

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

    
479
      if( q1==q ) continue;
480

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

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

    
494
      if( !skip ) return false;
495
      }
496

    
497
    return true;
498
    }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

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

    
509
    float x = result.get0();
510
    float y = result.get1();
511
    float z = result.get2();
512

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

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

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

    
526
    return -1;
527
    }
528

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

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

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

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

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

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

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

    
566
    return false;
567
    }
568

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

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

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

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

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

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

    
615
    if( CUBITS[1].mQuatIndex!=index ) return false;
616

    
617
    boolean solved = true;
618

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

    
628
    if( solved ) return true;
629

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

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

    
638
    return true;
639
    }
640

    
641
///////////////////////////////////////////////////////////////////////////////////////////////////
642

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

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

    
659
    return ret;
660
    }
661

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663

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

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

    
673
    return (1<<num);
674
    }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677

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

    
683
///////////////////////////////////////////////////////////////////////////////////////////////////
684

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

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

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

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

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

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708

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

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

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

    
734
///////////////////////////////////////////////////////////////////////////////////////////////////
735

    
736
  public int getScrambleType()
737
    {
738
    return 0;
739
    }
740

    
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742

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

    
749
      while( initBitmap!=rowBitmap )
750
        {
751
        initBitmap = rowBitmap;
752

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

    
761
    return rowBitmap;
762
    }
763

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

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

    
774
    float x = pos[offset  ];
775
    float y = pos[offset+1];
776
    float z = pos[offset+2];
777

    
778
    float xo,yo,zo;
779

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

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

    
790
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
791

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

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

    
809
///////////////////////////////////////////////////////////////////////////////////////////////////
810
// remember about the double cover or unit quaternions!
811

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

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

    
821
    final float MAX_ERROR = 0.1f;
822
    float dX,dY,dZ,dW;
823

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

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

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

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

    
847
    return -1;
848
    }
849

    
850
///////////////////////////////////////////////////////////////////////////////////////////////////
851

    
852
  private float getAngle()
853
    {
854
    int pointNum = mRotationAngle.getNumPoints();
855

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

    
867
///////////////////////////////////////////////////////////////////////////////////////////////////
868

    
869
  void setLibInterface(ObjectLibInterface inter)
870
    {
871
    mInterface = inter;
872
    }
873

    
874
///////////////////////////////////////////////////////////////////////////////////////////////////
875

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

    
882
///////////////////////////////////////////////////////////////////////////////////////////////////
883

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

    
895
    mRotationAngle.removeAll();
896
    mRotationAngleStatic.set0(0);
897

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

    
906
///////////////////////////////////////////////////////////////////////////////////////////////////
907

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

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

    
925
      return mRotateEffect.getID();
926
      }
927

    
928
    return 0;
929
    }
930

    
931
///////////////////////////////////////////////////////////////////////////////////////////////////
932

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

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

    
949
      return mRotateEffect.getID();
950
      }
951

    
952
    return 0;
953
    }
954

    
955
///////////////////////////////////////////////////////////////////////////////////////////////////
956

    
957
  void continueRotation(float angleInDegrees)
958
    {
959
    mRotationAngleStatic.set0(angleInDegrees);
960
    }
961

    
962
///////////////////////////////////////////////////////////////////////////////////////////////////
963

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

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

    
985
///////////////////////////////////////////////////////////////////////////////////////////////////
986

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

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

    
999
///////////////////////////////////////////////////////////////////////////////////////////////////
1000

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

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

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

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

    
1023
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1024
      }
1025
    }
1026

    
1027
///////////////////////////////////////////////////////////////////////////////////////////////////
1028

    
1029
  void releaseResources()
1030
    {
1031
    mTexture.markForDeletion();
1032
    mMesh.markForDeletion();
1033
    mEffects.markForDeletion();
1034

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

    
1041
///////////////////////////////////////////////////////////////////////////////////////////////////
1042

    
1043
  synchronized void restorePreferences(SharedPreferences preferences)
1044
    {
1045
    boolean error = false;
1046

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

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

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

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

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

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

    
1082
  private void createTexture()
1083
    {
1084
    Paint paint = new Paint();
1085
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1086
    Canvas canvas = new Canvas(mBitmap);
1087

    
1088
    paint.setAntiAlias(true);
1089
    paint.setTextAlign(Paint.Align.CENTER);
1090
    paint.setStyle(Paint.Style.FILL);
1091

    
1092
    paint.setColor(COLOR_BLACK);
1093
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1094

    
1095
    int texture = 0;
1096
    FactorySticker factory = FactorySticker.getInstance();
1097

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

    
1109
///////////////////////////////////////////////////////////////////////////////////////////////////
1110

    
1111
  void setTexture()
1112
    {
1113
    if( mBitmap==null ) createTexture();
1114

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

    
1122
///////////////////////////////////////////////////////////////////////////////////////////////////
1123

    
1124
  void setObjectRatioNow(float sc, int nodeSize)
1125
    {
1126
    mObjectScreenRatio = sc;
1127
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1128
    mObjectScale.set(scale,scale,scale);
1129

    
1130
    if( mTouchControl ==null ) mTouchControl = getMovement();
1131
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1132
    }
1133

    
1134
///////////////////////////////////////////////////////////////////////////////////////////////////
1135

    
1136
  void setObjectRatio(float sizeChange, int nodeSize)
1137
    {
1138
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1139

    
1140
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1141
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1142

    
1143
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1144
    }
1145

    
1146
///////////////////////////////////////////////////////////////////////////////////////////////////
1147

    
1148
  void setNodeSize(int nodeSize)
1149
    {
1150
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1151
    }
1152

    
1153
///////////////////////////////////////////////////////////////////////////////////////////////////
1154

    
1155
  public float getRatio()
1156
    {
1157
    return mObjectScreenRatio;
1158
    }
1159

    
1160
///////////////////////////////////////////////////////////////////////////////////////////////////
1161

    
1162
  boolean isSolved()
1163
    {
1164
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1165
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1166
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1167
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1168

    
1169
    return false;
1170
    }
1171

    
1172
///////////////////////////////////////////////////////////////////////////////////////////////////
1173
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1174
// during setting up the initial position in the solver.
1175

    
1176
  int getCubit(float[] point3D)
1177
    {
1178
    float dist, minDist = Float.MAX_VALUE;
1179
    int currentBest=-1;
1180
    float multiplier = mNumLayers[0];
1181

    
1182
    point3D[0] *= multiplier;
1183
    point3D[1] *= multiplier;
1184
    point3D[2] *= multiplier;
1185

    
1186
    for(int i=0; i<NUM_CUBITS; i++)
1187
      {
1188
      dist = CUBITS[i].getDistSquared(point3D);
1189
      if( dist<minDist )
1190
        {
1191
        minDist = dist;
1192
        currentBest = i;
1193
        }
1194
      }
1195

    
1196
    return currentBest;
1197
    }
1198

    
1199
///////////////////////////////////////////////////////////////////////////////////////////////////
1200

    
1201
  int computeNearestAngle(int axis, float angle, float speed)
1202
    {
1203
    int[] basicArray = getBasicAngle();
1204
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1205
    int nearestAngle = 360/basicAngle;
1206

    
1207
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1208
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1209

    
1210
    if( tmp!=0 ) return nearestAngle*tmp;
1211

    
1212
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1213
    }
1214

    
1215
///////////////////////////////////////////////////////////////////////////////////////////////////
1216
// INTERNAL API - those are called from 'effects' package
1217
///////////////////////////////////////////////////////////////////////////////////////////////////
1218

    
1219
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1220
    {
1221
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1222
    }
1223

    
1224
///////////////////////////////////////////////////////////////////////////////////////////////////
1225

    
1226
  public Static4D getRotationQuat()
1227
    {
1228
    return mQuat;
1229
    }
1230

    
1231
///////////////////////////////////////////////////////////////////////////////////////////////////
1232

    
1233
  public float getSize()
1234
    {
1235
    return mSize;
1236
    }
1237

    
1238
///////////////////////////////////////////////////////////////////////////////////////////////////
1239

    
1240
  public void apply(Effect effect, int position)
1241
    {
1242
    mEffects.apply(effect, position);
1243
    }
1244

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

    
1247
  public void remove(long effectID)
1248
    {
1249
    mEffects.abortById(effectID);
1250
    }
1251

    
1252
///////////////////////////////////////////////////////////////////////////////////////////////////
1253

    
1254
  public MeshBase getObjectMesh()
1255
    {
1256
    return mMesh;
1257
    }
1258

    
1259
///////////////////////////////////////////////////////////////////////////////////////////////////
1260

    
1261
  public DistortedEffects getObjectEffects()
1262
    {
1263
    return mEffects;
1264
    }
1265

    
1266
///////////////////////////////////////////////////////////////////////////////////////////////////
1267
// PUBLIC API
1268
///////////////////////////////////////////////////////////////////////////////////////////////////
1269

    
1270
  public int getCubitFaceColorIndex(int cubit, int face)
1271
    {
1272
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1273

    
1274
    int x = (int)(texMap.get0()/texMap.get2());
1275
    int y = (int)(texMap.get1()/texMap.get3());
1276

    
1277
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1278
    }
1279

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

    
1282
  public int[] getNumLayers()
1283
    {
1284
    return mNumLayers;
1285
    }
1286

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

    
1289
  public synchronized void solve()
1290
    {
1291
    for(int i=0; i<NUM_CUBITS; i++)
1292
      {
1293
      CUBITS[i].solve();
1294
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1295
      }
1296
    }
1297

    
1298
///////////////////////////////////////////////////////////////////////////////////////////////////
1299

    
1300
  public int getCubitQuatIndex(int cubit)
1301
    {
1302
    return CUBITS[cubit].mQuatIndex;
1303
    }
1304

    
1305
///////////////////////////////////////////////////////////////////////////////////////////////////
1306

    
1307
  public Bitmap getStickerBitmap()
1308
    {
1309
    return mBitmap;
1310
    }
1311

    
1312
///////////////////////////////////////////////////////////////////////////////////////////////////
1313

    
1314
  public DistortedNode getNode()
1315
    {
1316
    return mNode;
1317
    }
1318

    
1319
///////////////////////////////////////////////////////////////////////////////////////////////////
1320

    
1321
  public ObjectType getObjectType()
1322
    {
1323
    return intGetObjectType(mNumLayers);
1324
    }
1325

    
1326
///////////////////////////////////////////////////////////////////////////////////////////////////
1327

    
1328
  public TouchControl getMovement()
1329
    {
1330
    if( mTouchControl==null )
1331
      {
1332
      switch(getMovementType())
1333
        {
1334
        case TC_TETRAHEDRON : mTouchControl = new TouchControlTetrahedron(this);
1335
                              break;
1336
        case TC_HEXAHEDRON  : mTouchControl = new TouchControlHexahedron(this);
1337
                              break;
1338
        case TC_OCTAHEDRON  : mTouchControl = new TouchControlOctahedron(this);
1339
                              break;
1340
        case TC_DODECAHEDRON: mTouchControl = new TouchControlDodecahedron(this);
1341
                              break;
1342
        case TC_CUBOID      : int[] numLayers = getNumLayers();
1343
                              mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1344
                              break;
1345
        case TC_CHANGING    : mTouchControl = new TouchControlShapeChanging(this);
1346
                              break;
1347
        }
1348
      }
1349
    return mTouchControl;
1350
    }
1351

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

    
1354
  public String getShortName()
1355
    {
1356
    return getObjectType().name();
1357
    }
1358

    
1359
///////////////////////////////////////////////////////////////////////////////////////////////////
1360

    
1361
  protected void setReader(JsonReader reader)
1362
    {
1363
    // empty
1364
    }
1365

    
1366
///////////////////////////////////////////////////////////////////////////////////////////////////
1367

    
1368
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1369

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

    
1395
  // not only for JSON
1396
  public abstract Static3D[] getRotationAxis();
1397
  public abstract int[] getBasicAngle();
1398
  public abstract int getNumFaces();
1399
  public abstract String getObjectName();
1400
  public abstract String getInventor();
1401
  public abstract int getYearOfInvention();
1402
  public abstract int getComplexity();
1403
  public abstract int getFOV();
1404
  }
(10-10/12)