Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 1561a74f

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 MESH_NICE = 0;
68
  public static final int MESH_FAST = 1;
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_INTERNAL= 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 TouchControl mTouchControl;
134
  private boolean[][] mLayerRotatable;
135
  private int[][][] mEnabled;
136
  private DistortedNode mNode;
137
  private ObjectLibInterface mInterface;
138
  private Bitmap mBitmap;
139

    
140
  //////////////////// SOLVED1 ////////////////////////
141

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

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

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

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

    
190
    OBJECT_QUATS = getQuats();
191
    NUM_CUBITS  = mOrigPos.length;
192
    NUM_FACE_COLORS = getNumFaceColors();
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(scale,scale,scale);
223
    setObjectRatioNow(scale,720);
224

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

    
229
    CUBITS = new Cubit[NUM_CUBITS];
230
    boolean fromStream = (stream!=null && meshState==MESH_NICE);
231
    createMeshAndCubits(stream,meshState,fromStream);
232
    setUpTextures(fromStream);
233
    createDataStructuresForSolved(mNumLayers);
234

    
235
    mEffects = new DistortedEffects();
236

    
237
    for(int q=0; q<NUM_QUATS; q++)
238
      {
239
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
240
      vq.setMeshAssociation(0,q);
241
      mEffects.apply(vq);
242
      }
243

    
244
    mEffects.apply(mRotateEffect);
245
    mEffects.apply(quatEffect);
246
    mEffects.apply(scaleEffect);
247
    mEffects.apply(moveEffect);
248

    
249
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  private void setUpTextures(boolean fromStream)
255
    {
256
    mTexture = new DistortedTexture();
257
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
258
    mNumTexCols = NUM_STICKERS_IN_ROW;
259
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
260
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
261

    
262
    if( !fromStream || shouldResetTextureMaps() ) resetAllTextureMaps();
263

    
264
    setTexture();
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

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

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

    
283
    sumX /= len;
284
    sumY /= len;
285
    sumZ /= len;
286

    
287
    return new Static3D(sumX,sumY,sumZ);
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
  private void createMeshAndCubits(InputStream stream, int meshState, boolean fromStream)
293
    {
294
    if( fromStream )
295
      {
296
      DataInputStream dos = new DataInputStream(stream);
297
      mMesh = new MeshFile(dos);
298

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

    
308
      for(int i=0; i<NUM_CUBITS; i++)
309
        {
310
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
311
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
312
        }
313
      }
314
    else
315
      {
316
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
317

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

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

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

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

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

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

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

    
356
    return mesh;
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

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

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

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

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

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

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

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

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

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

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

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

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

    
420
    return nonBlackIndex;
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

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

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

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

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

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

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

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

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

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

    
472
    return null;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

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

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

    
487
      if( q1==q ) continue;
488

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

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

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

    
505
    return true;
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

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

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

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

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

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

    
534
    return -1;
535
    }
536

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

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

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

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

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

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

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

    
574
    return false;
575
    }
576

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

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

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

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

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

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

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

    
625
    boolean solved = true;
626

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

    
636
    if( solved ) return true;
637

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

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

    
646
    return true;
647
    }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650

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

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

    
667
    return ret;
668
    }
669

    
670
///////////////////////////////////////////////////////////////////////////////////////////////////
671

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

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

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

    
684
///////////////////////////////////////////////////////////////////////////////////////////////////
685

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

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692

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

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

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

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

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

    
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716

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

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

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

    
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743

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

    
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750

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

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

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

    
769
    return rowBitmap;
770
    }
771

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

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

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

    
786
    float xo,yo,zo;
787

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

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

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

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

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

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

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

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

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

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

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

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

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

    
855
    return -1;
856
    }
857

    
858
///////////////////////////////////////////////////////////////////////////////////////////////////
859

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

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

    
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876

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

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

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

    
890
///////////////////////////////////////////////////////////////////////////////////////////////////
891

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

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

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

    
914
///////////////////////////////////////////////////////////////////////////////////////////////////
915

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

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

    
933
      return mRotateEffect.getID();
934
      }
935

    
936
    return 0;
937
    }
938

    
939
///////////////////////////////////////////////////////////////////////////////////////////////////
940

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

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

    
957
      return mRotateEffect.getID();
958
      }
959

    
960
    return 0;
961
    }
962

    
963
///////////////////////////////////////////////////////////////////////////////////////////////////
964

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

    
970
///////////////////////////////////////////////////////////////////////////////////////////////////
971

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

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

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

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

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

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

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

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

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

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

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

    
1035
///////////////////////////////////////////////////////////////////////////////////////////////////
1036

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

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

    
1049
///////////////////////////////////////////////////////////////////////////////////////////////////
1050

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

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

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

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

    
1080
///////////////////////////////////////////////////////////////////////////////////////////////////
1081

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

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

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

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

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

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

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

    
1117
///////////////////////////////////////////////////////////////////////////////////////////////////
1118

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

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

    
1130
///////////////////////////////////////////////////////////////////////////////////////////////////
1131

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

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

    
1142
///////////////////////////////////////////////////////////////////////////////////////////////////
1143

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

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

    
1151
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1152
    }
1153

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

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

    
1161
///////////////////////////////////////////////////////////////////////////////////////////////////
1162

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

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

    
1170
  public float getObjectRatio()
1171
    {
1172
    return mObjectScreenRatio*mInitScreenRatio;
1173
    }
1174

    
1175
///////////////////////////////////////////////////////////////////////////////////////////////////
1176

    
1177
  boolean isSolved()
1178
    {
1179
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1180
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1181
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1182
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1183

    
1184
    return false;
1185
    }
1186

    
1187
///////////////////////////////////////////////////////////////////////////////////////////////////
1188
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1189
// during setting up the initial position in the solver.
1190

    
1191
  int getCubit(float[] point3D)
1192
    {
1193
    float dist, minDist = Float.MAX_VALUE;
1194
    int currentBest=-1;
1195
    float multiplier = mNumLayers[0];
1196

    
1197
    point3D[0] *= multiplier;
1198
    point3D[1] *= multiplier;
1199
    point3D[2] *= multiplier;
1200

    
1201
    for(int i=0; i<NUM_CUBITS; i++)
1202
      {
1203
      dist = CUBITS[i].getDistSquared(point3D);
1204
      if( dist<minDist )
1205
        {
1206
        minDist = dist;
1207
        currentBest = i;
1208
        }
1209
      }
1210

    
1211
    return currentBest;
1212
    }
1213

    
1214
///////////////////////////////////////////////////////////////////////////////////////////////////
1215

    
1216
  int computeNearestAngle(int axis, float angle, float speed)
1217
    {
1218
    int[] basicArray = getBasicAngle();
1219
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1220
    int nearestAngle = 360/basicAngle;
1221

    
1222
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1223
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1224

    
1225
    if( tmp!=0 ) return nearestAngle*tmp;
1226

    
1227
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1228
    }
1229

    
1230
///////////////////////////////////////////////////////////////////////////////////////////////////
1231
// INTERNAL API - those are called from 'effects' package
1232
///////////////////////////////////////////////////////////////////////////////////////////////////
1233

    
1234
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1235
    {
1236
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1237
    }
1238

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

    
1241
  public Static4D getRotationQuat()
1242
    {
1243
    return mQuat;
1244
    }
1245

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

    
1248
  public float getSize()
1249
    {
1250
    return mSize;
1251
    }
1252

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

    
1255
  public void apply(Effect effect, int position)
1256
    {
1257
    mEffects.apply(effect, position);
1258
    }
1259

    
1260
///////////////////////////////////////////////////////////////////////////////////////////////////
1261

    
1262
  public void remove(long effectID)
1263
    {
1264
    mEffects.abortById(effectID);
1265
    }
1266

    
1267
///////////////////////////////////////////////////////////////////////////////////////////////////
1268

    
1269
  public MeshBase getObjectMesh()
1270
    {
1271
    return mMesh;
1272
    }
1273

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

    
1276
  public DistortedEffects getObjectEffects()
1277
    {
1278
    return mEffects;
1279
    }
1280

    
1281
///////////////////////////////////////////////////////////////////////////////////////////////////
1282
// PUBLIC API
1283
///////////////////////////////////////////////////////////////////////////////////////////////////
1284

    
1285
  public int getCubitFaceColorIndex(int cubit, int face)
1286
    {
1287
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1288

    
1289
    int x = (int)(texMap.get0()/texMap.get2());
1290
    int y = (int)(texMap.get1()/texMap.get3());
1291

    
1292
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1293
    }
1294

    
1295
///////////////////////////////////////////////////////////////////////////////////////////////////
1296

    
1297
  public int[] getNumLayers()
1298
    {
1299
    return mNumLayers;
1300
    }
1301

    
1302
///////////////////////////////////////////////////////////////////////////////////////////////////
1303

    
1304
  public synchronized void solve()
1305
    {
1306
    for(int i=0; i<NUM_CUBITS; i++)
1307
      {
1308
      CUBITS[i].solve();
1309
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1310
      }
1311
    }
1312

    
1313
///////////////////////////////////////////////////////////////////////////////////////////////////
1314

    
1315
  public int getCubitQuatIndex(int cubit)
1316
    {
1317
    return CUBITS[cubit].mQuatIndex;
1318
    }
1319

    
1320
///////////////////////////////////////////////////////////////////////////////////////////////////
1321

    
1322
  public int getCubitRotRow(int cubit, int axis)
1323
    {
1324
    return CUBITS[cubit].getRotRow(axis);
1325
    }
1326

    
1327
///////////////////////////////////////////////////////////////////////////////////////////////////
1328

    
1329
  public Bitmap getStickerBitmap()
1330
    {
1331
    return mBitmap;
1332
    }
1333

    
1334
///////////////////////////////////////////////////////////////////////////////////////////////////
1335

    
1336
  public DistortedNode getNode()
1337
    {
1338
    return mNode;
1339
    }
1340

    
1341
///////////////////////////////////////////////////////////////////////////////////////////////////
1342

    
1343
  public ObjectType getObjectType()
1344
    {
1345
    return intGetObjectType(mNumLayers);
1346
    }
1347

    
1348
///////////////////////////////////////////////////////////////////////////////////////////////////
1349

    
1350
  public TouchControl getTouchControl()
1351
    {
1352
    if( mTouchControl==null )
1353
      {
1354
      switch(getTouchControlType())
1355
        {
1356
        case TC_TETRAHEDRON    : mTouchControl = new TouchControlTetrahedron(this);
1357
                                 break;
1358
        case TC_HEXAHEDRON     : mTouchControl = new TouchControlHexahedron(this);
1359
                                 break;
1360
        case TC_OCTAHEDRON     : mTouchControl = new TouchControlOctahedron(this);
1361
                                 break;
1362
        case TC_DODECAHEDRON   : mTouchControl = new TouchControlDodecahedron(this);
1363
                                 break;
1364
        case TC_CUBOID         : int[] numLayers = getNumLayers();
1365
                                 mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1366
                                 break;
1367
        case TC_CHANGING_MIRROR: mTouchControl = new TouchControlMirror(this);
1368
                                 break;
1369
        case TC_CHANGING_SQUARE: mTouchControl = new TouchControlSquare(this);
1370
                                 break;
1371
        }
1372
      }
1373
    return mTouchControl;
1374
    }
1375

    
1376
///////////////////////////////////////////////////////////////////////////////////////////////////
1377

    
1378
  public String getShortName()
1379
    {
1380
    return getObjectType().name();
1381
    }
1382

    
1383
///////////////////////////////////////////////////////////////////////////////////////////////////
1384

    
1385
  protected void setReader(JsonReader reader)
1386
    {
1387
    // empty
1388
    }
1389

    
1390
///////////////////////////////////////////////////////////////////////////////////////////////////
1391

    
1392
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1393

    
1394
  // for JSON only
1395
  public abstract int getSolvedFunctionIndex();
1396
  public abstract int getTouchControlType();
1397
  public abstract int getTouchControlSplit();
1398
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1399
  public abstract int[][][] getEnabled();
1400
  public abstract float[] getDist3D(int[] numLayers);
1401
  public abstract ScrambleState[] getScrambleStates();
1402
  public abstract float[][] getCuts(int[] numLayers);
1403
  public abstract Static4D[] getQuats();
1404
  public abstract int getNumStickerTypes(int[] numLayers);
1405
  public abstract ObjectSticker retSticker(int sticker);
1406
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1407
  public abstract ObjectShape getObjectShape(int variant);
1408
  public abstract int getNumCubitVariants(int[] numLayers);
1409
  public abstract float[][] getCubitPositions(int[] numLayers);
1410
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1411
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1412
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1413
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1414
  public abstract int getNumFaceColors();
1415
  public abstract int getNumCubitFaces();
1416
  public abstract float getScreenRatio();
1417
  public abstract int getColor(int face);
1418

    
1419
  // not only for JSON
1420
  public abstract Static3D[] getRotationAxis();
1421
  public abstract int[] getBasicAngle();
1422
  public abstract int getNumFaces();
1423
  public abstract String getObjectName();
1424
  public abstract String getInventor();
1425
  public abstract int getYearOfInvention();
1426
  public abstract int getComplexity();
1427
  public abstract int getFOV();
1428
  }
(11-11/13)