Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 39c6b370

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.ObjectFaceShape;
55
import org.distorted.objectlib.helpers.ObjectLibInterface;
56
import org.distorted.objectlib.helpers.ObjectShape;
57
import org.distorted.objectlib.helpers.ObjectSignature;
58
import org.distorted.objectlib.helpers.ObjectSticker;
59
import org.distorted.objectlib.helpers.QuatGroupGenerator;
60
import org.distorted.objectlib.scrambling.ScrambleState;
61
import org.distorted.objectlib.scrambling.ObjectScrambler;
62
import org.distorted.objectlib.json.JsonReader;
63
import org.distorted.objectlib.touchcontrol.*;
64

    
65
import static org.distorted.objectlib.touchcontrol.TouchControl.*;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
public abstract class TwistyObject
70
  {
71
  public static final int MESH_NICE = 0;
72
  public static final int MESH_FAST = 1;
73

    
74
  public static final int MODE_ICON = 0;
75
  public static final int MODE_NORM = 1;
76

    
77
  public static final int COLOR_YELLOW   = 0xffffff00;
78
  public static final int COLOR_WHITE    = 0xffffffff;
79
  public static final int COLOR_BLUE     = 0xff0000ff;
80
  public static final int COLOR_GREEN    = 0xff00bb00;
81
  public static final int COLOR_RED      = 0xff990000;
82
  public static final int COLOR_ORANGE   = 0xffff6200;
83
  public static final int COLOR_GREY     = 0xff727c7b;
84
  public static final int COLOR_VIOLET   = 0xff7700bb;
85
  public static final int COLOR_STROKE   = 0xff000000;
86
  public static final int COLOR_INTERNAL = 0xff000000;
87

    
88
  public static final int TEXTURE_HEIGHT = 256;
89
  static final int NUM_STICKERS_IN_ROW = 4;
90

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

    
96
  private static final float MAX_SIZE_CHANGE = 1.35f;
97
  private static final float MIN_SIZE_CHANGE = 0.75f;
98

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

    
102
  protected float[][] mStickerCoords;
103
  protected Static4D[] mObjectQuats;
104
  int mNumAxis, mMaxNumLayers;
105

    
106
  private int[][] mStickerVariants;
107
  private float[] mStickerScales;
108
  private Cubit[] mCubits;
109
  private MeshBase[] mMeshes;
110
  private int mNumCubits, mNumQuats, mNumFaceColors, mNumTextures;
111
  private int mNumCubitFaces, mNumStickerTypes;
112
  private Static3D[] mAxis;
113
  private float[][] mCuts;
114
  private int[] mNumCuts;
115
  private float[][] mOrigPos;
116
  private Static4D[] mOrigQuat;
117
  private Static4D[] mMixupModeQuats;
118
  private boolean mIsInMixupMode;
119
  private Static4D mQuat;
120
  private final int[] mNumLayers;
121
  private final float mSize;
122
  private DistortedEffects mEffects;
123
  private VertexEffectRotate mRotateEffect;
124
  private Dynamic1D mRotationAngle;
125
  private Static3D mRotationAxis;
126
  private Static3D mObjectScale;
127
  private int[] mQuatDebug;
128
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
129
  private DistortedTexture mTexture;
130
  private float mInitScreenRatio;
131
  private int mSolvedFunctionIndex;
132
  private boolean mIsBandaged;
133
  private float mObjectScreenRatio;
134
  private int[][] mSolvedQuats;
135
  private int[][] mQuatMult;
136
  private int[] mTmpQuats;
137
  private int mNumTexRows, mNumTexCols;
138
  private int mRotRowBitmap;
139
  private int mCurrentRotAxis;
140
  private MeshBase mMesh;
141
  private ObjectScrambler mScrambler;
142
  private TouchControl mTouchControl;
143
  private DistortedNode mNode;
144
  private ObjectLibInterface mInterface;
145
  private Bitmap mBitmap;
146
  private ObjectSticker[] mStickers;
147
  private ObjectShape[] mShapes;
148
  private int mNumCubitVariants;
149
  private int[][] mCubitFaceColors;
150
  private int[][] mVariantFaceIsOuter;
151
  private int[][] mBasicAngles;
152
  private int mIconMode;
153
  private final InitData mInitData;
154
  private float[][] mRowOffsets;
155
  private boolean[] mBelongs;
156
  private float[] mTmp;
157

    
158
  //////////////////// SOLVED1 ////////////////////////
159

    
160
  private int[] mFaceMap;
161
  private int[][] mScramble;
162
  private int[] mColors;
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  TwistyObject(InputStream jsonStream, int meshState, int iconMode, Static4D quat, Static3D move, float scale, InputStream meshStream)
167
    {
168
    JsonReader reader = JsonReader.getInstance();
169
    reader.parseJsonFile(jsonStream);
170
    setReader(reader);
171

    
172
    mNumLayers = reader.getNumLayers();
173
    mSize      = reader.getSize();
174
    mInitData  = null;
175
    initialize(meshState,iconMode,quat,move,scale,meshStream,true);
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  TwistyObject(InitData data, int meshState, int iconMode, float size, Static4D quat, Static3D move, float scale, InputStream meshStream)
181
    {
182
    mNumLayers = data.getNumLayers();
183
    mSize      = size;
184
    mInitData  = data;
185
    initialize(meshState,iconMode,quat,move,scale,meshStream,false);
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  private void initialize(int meshState, int iconMode, Static4D quat, Static3D move, float scale, InputStream stream, boolean fromJSON)
191
    {
192
    mIconMode = iconMode;
193
    mQuat = quat;
194
    mAxis = getRotationAxis();
195
    mInitScreenRatio = getScreenRatio();
196
    mSolvedFunctionIndex = getSolvedFunctionIndex();
197
    mBasicAngles = getBasicAngles();
198
    mObjectQuats = getQuats();
199
    mNumQuats = mObjectQuats.length;
200
    mOrigPos = getCubitPositions(mNumLayers);
201
    mRowOffsets = new float[getNumFaces()][3];
202
    mTmp = new float[4];
203

    
204
    int numAxis = mAxis.length;
205
    mMaxNumLayers = -1;
206
    mCuts = getCuts(mNumLayers);
207
    mNumCuts = new int[numAxis];
208
    for(int i=0; i<numAxis; i++)
209
      {
210
      if( mMaxNumLayers<mNumLayers[i] ) mMaxNumLayers = mNumLayers[i];
211
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
212
      }
213

    
214
    mNumCubits = mOrigPos.length;
215
    mNumFaceColors = getNumFaceColors();
216
    mNumAxis = mAxis.length;
217
    mBelongs = new boolean[mNumCubits];
218

    
219
    int scramblingType = getScrambleType();
220
    ScrambleState[] states = getScrambleStates();
221
    mScrambler = new ObjectScrambler(scramblingType, mNumAxis,mNumLayers,states);
222

    
223
    boolean bandaged=false;
224

    
225
    for( int c=0; c<mNumCubits; c++)
226
      {
227
      if( mOrigPos[c].length>3 )
228
        {
229
        bandaged=true;
230
        break;
231
        }
232
      }
233
    mIsBandaged = bandaged;
234
    mQuatDebug = new int[mNumCubits];
235

    
236
    mRotationAngle= new Dynamic1D();
237
    mRotationAxis = new Static3D(1,0,0);
238
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
239

    
240
    mRotationAngleStatic = new Static1D(0);
241
    mRotationAngleMiddle = new Static1D(0);
242
    mRotationAngleFinal  = new Static1D(0);
243

    
244
    mObjectScale = new Static3D(scale,scale,scale);
245
    setObjectRatioNow(scale,720);
246

    
247
    mEffects = new DistortedEffects();
248
    createQuaternionEffects();
249

    
250
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
251
    MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
252
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
253

    
254
    boolean fromDMESH = (stream!=null && meshState==MESH_NICE);
255
    getQuatsAndShapes(fromDMESH,fromJSON);
256
    createMeshAndCubits(stream,meshState,fromDMESH);
257
    setUpTextures(fromDMESH,fromJSON);
258
    createDataStructuresForSolved();
259

    
260
    mEffects.apply(mRotateEffect);
261
    mEffects.apply(quatEffect);
262
    mEffects.apply(scaleEffect);
263
    mEffects.apply(moveEffect);
264

    
265
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private void createQuaternionEffects()
271
    {
272
    if( mNumQuats<=ObjectControl.MAX_QUATS )
273
      {
274
      mIsInMixupMode = false;
275

    
276
      for( int q=0; q<mNumQuats; q++)
277
        {
278
        VertexEffectQuaternion vq = new VertexEffectQuaternion(mObjectQuats[q],CENTER);
279
        vq.setMeshAssociation(0,q);
280
        mEffects.apply(vq);
281
        }
282
      }
283
    else if( mNumCubits<=ObjectControl.MAX_QUATS )
284
      {
285
      mIsInMixupMode = true;
286
      mMixupModeQuats = new Static4D[mNumCubits];
287

    
288
      for( int q=0; q<mNumCubits; q++)
289
        {
290
        mMixupModeQuats[q] = new Static4D(mObjectQuats[0]);
291
        VertexEffectQuaternion vq = new VertexEffectQuaternion(mMixupModeQuats[q],CENTER);
292
        vq.setMeshAssociation(0,q);
293
        mEffects.apply(vq);
294
        }
295
      }
296
    else
297
      {
298
      android.util.Log.e("D", "object has too many quaternions ("+mNumQuats+") or too many cubits ("+mNumCubits+")");
299
      }
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  private Static3D getPos(float[] origPos)
305
    {
306
    int len = origPos.length/3;
307
    float sumX = 0.0f;
308
    float sumY = 0.0f;
309
    float sumZ = 0.0f;
310

    
311
    for(int i=0; i<len; i++)
312
      {
313
      sumX += origPos[3*i  ];
314
      sumY += origPos[3*i+1];
315
      sumZ += origPos[3*i+2];
316
      }
317

    
318
    sumX /= len;
319
    sumY /= len;
320
    sumZ /= len;
321

    
322
    return new Static3D(sumX,sumY,sumZ);
323
    }
324

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

    
327
  private void createOuterFaces()
328
    {
329
    for(int v=0; v<mNumCubitVariants; v++)
330
      {
331
      int[][] indices = mShapes[v].getVertIndices();
332
      int faces = indices.length;
333
      mVariantFaceIsOuter[v] = new int[faces];
334
      }
335

    
336
    for( int cubit=0; cubit<mNumCubits; cubit++)
337
      {
338
      int variant = getCubitVariant(cubit,mNumLayers);
339
      int[][] indices = mShapes[variant].getVertIndices();
340
      int numFaces = indices.length;
341

    
342
      for(int face=0; face<numFaces; face++)
343
        if( getCubitFaceColor(cubit,face)>=0 )
344
          {
345
          mVariantFaceIsOuter[variant][face] = 1;
346
          }
347
      }
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
  private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
353
    {
354
    mNumCubitVariants = getNumCubitVariants(mNumLayers);
355

    
356
    if( !fromDMESH || !fromJSON )
357
      {
358
      FactoryCubit factory = FactoryCubit.getInstance();
359
      factory.clear();
360

    
361
      mOrigQuat = new Static4D[mNumCubits];
362
      for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
363

    
364
      mShapes = new ObjectShape[mNumCubitVariants];
365
      for(int i=0; i<mNumCubitVariants; i++) mShapes[i] = getObjectShape(i);
366
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
367
      mVariantFaceIsOuter = new int[mNumCubitVariants][];
368

    
369
      if( !fromJSON )
370
        {
371
        mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
372
        createOuterFaces();
373
        }
374

    
375
      if( fromDMESH )
376
        {
377
        for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i], mVariantFaceIsOuter[i]);
378
        }
379
      }
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  private void createMeshAndCubits(InputStream stream, int meshState, boolean fromDMESH)
385
    {
386
    mCubits = new Cubit[mNumCubits];
387

    
388
    if( fromDMESH )
389
      {
390
      DataInputStream dos = new DataInputStream(stream);
391
      mMesh = new MeshFile(dos);
392

    
393
      try
394
        {
395
        stream.close();
396
        }
397
      catch(IOException e)
398
        {
399
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
400
        }
401
      }
402
    else
403
      {
404
      MeshBase[] cubitMesh = new MeshBase[mNumCubits];
405

    
406
      for(int i=0; i<mNumCubits; i++)
407
        {
408
        cubitMesh[i] = createCubitMesh(i,mNumLayers,meshState,mNumCubitFaces);
409
        Static3D pos = getPos(mOrigPos[i]);
410
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
411
        }
412

    
413
      mMesh = new MeshJoined(cubitMesh);
414
      }
415

    
416
    for(int i=0; i<mNumCubits; i++)
417
      {
418
      mCubits[i] = new Cubit(this,mOrigPos[i],mNumAxis,i);
419
      setCubitQuat(i,mCubits[i].computeAssociation(),0);
420
      }
421
    }
422

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

    
425
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int meshState, int numComponents)
426
    {
427
    int variant = getCubitVariant(cubit,numLayers);
428

    
429
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
430

    
431
    if( mMeshes[variant]==null )
432
      {
433
      ObjectFaceShape faceShape = getObjectFaceShape(variant);
434
      FactoryCubit factory = FactoryCubit.getInstance();
435
      factory.createNewFaceTransform(mShapes[variant],mVariantFaceIsOuter[variant]);
436
      mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,meshState, numComponents);
437
      }
438

    
439
    MeshBase mesh = mMeshes[variant].copy(true);
440
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
441
    mesh.apply(quat,0xffffffff,0);
442

    
443
    return mesh;
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
449
    {
450
    mTexture = new DistortedTexture();
451

    
452
    if( fromJSON )
453
      {
454
      mNumStickerTypes = getNumStickerTypes();
455
      mNumCubitFaces = getNumCubitFaces();
456
      }
457
    else
458
      {
459
      FactoryCubit factory = FactoryCubit.getInstance();
460
      mStickerCoords   = factory.getStickerCoords();
461
      mStickerVariants = factory.getStickerVariants();
462
      mStickerScales   = factory.getStickerScales();
463
      adjustStickerCoords();
464
      mNumStickerTypes = (mStickerCoords==null ? 0 : mStickerCoords.length);
465
      }
466

    
467
    mNumTextures= mNumFaceColors *mNumStickerTypes;
468
    mNumTexCols = NUM_STICKERS_IN_ROW;
469
    mNumTexRows = (mNumTextures+1)/NUM_STICKERS_IN_ROW;
470
    if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
471

    
472
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
473
    setTexture();
474
    }
475

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

    
478
  private int getMultQuat(int index1, int index2)
479
    {
480
    if( mQuatMult==null )
481
      {
482
      mQuatMult = new int[mNumQuats][mNumQuats];
483

    
484
      for(int i=0; i<mNumQuats; i++)
485
        for(int j=0; j<mNumQuats; j++) mQuatMult[i][j] = -1;
486
      }
487

    
488
    if( mQuatMult[index1][index2]==-1 )
489
      {
490
      mQuatMult[index1][index2] = mulQuat(index1,index2);
491
      }
492

    
493
    return mQuatMult[index1][index2];
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public InitData getInitData()
499
    {
500
    return mInitData;
501
    }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
  public boolean isInIconMode()
506
    {
507
    return mIconMode==MODE_ICON;
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
  public int getVariantFaceColor(int variant, int face)
513
    {
514
    return face>=mStickerVariants[variant].length ? -1 : mStickerVariants[variant][face];
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
  public boolean shouldResetTextureMaps()
520
    {
521
    return false;
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
  private void createDataStructuresForSolved()
527
    {
528
    mTmpQuats = new int[mNumQuats];
529
    mSolvedQuats = getSolvedQuats();
530
    }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533
// This is used to build internal data structures for the generic 'isSolved()'
534
//
535
// if this is an internal cubit (all faces black): return -1
536
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
537
// Color index, i.e. the index into the 'FACE_COLORS' table.
538
// else (edge or corner cubit, more than one non-black face): return -2.
539

    
540
  protected int retCubitSolvedStatus(int cubit)
541
    {
542
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
543
    int variant = getCubitVariant(cubit,mNumLayers);
544

    
545
    for(int face=0; face<mNumCubitFaces; face++)
546
      {
547
      varColor = getVariantFaceColor(variant,face);
548
      int numFaces = mCubitFaceColors[cubit].length;
549
      cubColor = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
550

    
551
      if( varColor>=0 && cubColor>=0 )
552
        {
553
        numNonBlack++;
554
        nonBlackIndex = cubColor;
555
        }
556
      }
557

    
558
    if( numNonBlack==0 ) return -1;
559
    if( numNonBlack>=2 ) return -2;
560

    
561
    return nonBlackIndex;
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
567
    {
568
    final float MAXERR = 0.05f;
569
    int numAxis = dist.length;
570

    
571
    for(int i=0; i<numAxis; i++)
572
      {
573
      Static3D ax = faceAxis[i];
574
      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
575
      if( len>mSize*dist[i]+MAXERR ) return true;
576
      }
577

    
578
    return false;
579
    }
580

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582

    
583
  private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
584
    {
585
    float[][] vertices = mShapes[variant].getVertices();
586
    Static3D[] axis = getFaceAxis();
587
    float[] dist3D = getDist3D(mNumLayers);
588

    
589
    for( float[] vertex : vertices)
590
      {
591
      float x = vertex[0];
592
      float y = vertex[1];
593
      float z = vertex[2];
594

    
595
      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
596

    
597
      float mx = tmp[0] + px;
598
      float my = tmp[1] + py;
599
      float mz = tmp[2] + pz;
600

    
601
      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
602
      }
603

    
604
    return true;
605
    }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608

    
609
  private float computeAvg(float[] pos, int offset)
610
    {
611
    int len = pos.length/3;
612
    float ret=0.0f;
613
    for(int i=0; i<len; i++) ret += pos[3*i+offset];
614
    ret /= len;
615

    
616
    return ret;
617
    }
618

    
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620

    
621
  protected void displayCubitQuats()
622
    {
623
    StringBuilder builder = new StringBuilder();
624
    float[] tmp = new float[4];
625
    float ERR = 0.01f;
626

    
627
    for(int cubit=0; cubit<mNumCubits; cubit++)
628
      {
629
      builder.append(cubit);
630
      builder.append(" : ");
631

    
632
      int refCubit,variant = getCubitVariant(cubit,mNumLayers);
633

    
634
      for(refCubit=0; refCubit<mNumCubits; refCubit++)
635
        {
636
        if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
637
        }
638

    
639
      float[] curpos = mOrigPos[cubit];
640
      float[] refpos = mOrigPos[refCubit];
641
      float refX = computeAvg(refpos,0);
642
      float refY = computeAvg(refpos,1);
643
      float refZ = computeAvg(refpos,2);
644
      float curX = computeAvg(curpos,0);
645
      float curY = computeAvg(curpos,1);
646
      float curZ = computeAvg(curpos,2);
647

    
648
      for(int quat=0; quat<mNumQuats; quat++)
649
        {
650
        QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
651

    
652
        float dx = tmp[0]-curX;
653
        float dy = tmp[1]-curY;
654
        float dz = tmp[2]-curZ;
655

    
656
        if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
657
          {
658
          if( doesNotStickOut(variant,curX,curY,curZ,tmp,mObjectQuats[quat]) )
659
            {
660
            builder.append(quat);
661
            builder.append(',');
662
            }
663
          else
664
            {
665
            android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
666
            }
667
          }
668
        }
669

    
670
      builder.append('\n');
671
      }
672

    
673
    android.util.Log.e("D", "cubitQuats: \n"+builder.toString() );
674
    }
675

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

    
678
  protected int[] buildSolvedQuats(Static3D faceAx)
679
    {
680
    final float MAXD = 0.0001f;
681
    float x = faceAx.get0();
682
    float y = faceAx.get1();
683
    float z = faceAx.get2();
684
    float a,dx,dy,dz,qx,qy,qz;
685
    Static4D quat;
686
    int place = 0;
687

    
688
    for(int q=1; q<mNumQuats; q++)
689
      {
690
      quat = mObjectQuats[q];
691
      qx = quat.get0();
692
      qy = quat.get1();
693
      qz = quat.get2();
694

    
695
           if( x!=0.0f ) { a = qx/x; }
696
      else if( y!=0.0f ) { a = qy/y; }
697
      else               { a = qz/z; }
698

    
699
      dx = a*x-qx;
700
      dy = a*y-qy;
701
      dz = a*z-qz;
702

    
703
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
704
        {
705
        mTmpQuats[place++] = q;
706
        }
707
      }
708

    
709
    if( place!=0 )
710
      {
711
      int[] ret = new int[place];
712
      System.arraycopy(mTmpQuats,0,ret,0,place);
713
      return ret;
714
      }
715

    
716
    return null;
717
    }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720

    
721
  public int getCubitRotationType(int cubit)
722
    {
723
    return Cubit.TYPE_NORMAL;
724
    }
725

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727

    
728
  float[] getTrackingPoint(int cubitIndex, int cubitType)
729
    {
730
    if( cubitType!=Cubit.TYPE_NORMAL )
731
      {
732
      int variant = getCubitVariant(cubitIndex,mNumLayers);
733

    
734
      // object must have been created from JSON
735
      if( mVariantFaceIsOuter==null || mVariantFaceIsOuter[variant]==null )
736
        {
737
        mVariantFaceIsOuter = getVariantFaceIsOuter();
738
        }
739
      if( mShapes==null )
740
        {
741
        mShapes = new ObjectShape[mNumCubitVariants];
742
        }
743
      if( mShapes[variant]==null )
744
        {
745
        mShapes[variant] = getObjectShape(variant);
746
        }
747
      if( mOrigQuat==null )
748
        {
749
        mOrigQuat = new Static4D[mNumCubits];
750
        }
751
      if( mOrigQuat[cubitIndex]==null )
752
        {
753
        mOrigQuat[cubitIndex] = getCubitQuats(cubitIndex,mNumLayers);
754
        }
755

    
756
      int[][] indices = mShapes[variant].getVertIndices();
757
      int outer=-1, faces = indices.length;
758

    
759
      for(int i=0; i<faces; i++)
760
        {
761
        if( mVariantFaceIsOuter[variant][i]==1 )
762
          {
763
          outer=i;
764
          break;
765
          }
766
        }
767

    
768
      if( outer>=0 )
769
        {
770
        int vertIndex = indices[outer][0];
771
        float[] vertices = mShapes[variant].getVertices()[vertIndex];
772
        float[] ret = new float[3];
773
        float[] curpos = mOrigPos[cubitIndex];
774
        Static4D quat = mOrigQuat[cubitIndex];
775
        QuatHelper.rotateVectorByQuat(mTmp, vertices[0], vertices[1], vertices[2], 1, quat);
776

    
777
        ret[0] = mTmp[0]+computeAvg(curpos,0);
778
        ret[1] = mTmp[1]+computeAvg(curpos,1);
779
        ret[2] = mTmp[2]+computeAvg(curpos,2);
780

    
781
        return ret;
782
        }
783
      else
784
        {
785
        android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
786
        }
787
      }
788

    
789
    return null;
790
    }
791

    
792
///////////////////////////////////////////////////////////////////////////////////////////////////
793

    
794
  public int computeCurrentPuzzleFace(int type, float[] vertex)
795
    {
796
    if( type!=Cubit.TYPE_NORMAL )
797
      {
798
      Static3D[] axis = getFaceAxis();
799
      float[] dist3D = getDist3D(mNumLayers);
800
      final float MAXERR = 0.98f;
801
      int numAxis = axis.length;
802
      float x = vertex[0];
803
      float y = vertex[1];
804
      float z = vertex[2];
805

    
806
      for(int i=0; i<numAxis; i++)
807
        {
808
        Static3D ax = axis[i];
809
        float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
810
        if( len>mSize*dist3D[i]*MAXERR ) return i;
811
        }
812
      }
813

    
814
    return -1;
815
    }
816

    
817
///////////////////////////////////////////////////////////////////////////////////////////////////
818

    
819
  public float[] getCubitRowOffset(int cubitIndex)
820
    {
821
    return null;
822
    }
823

    
824
///////////////////////////////////////////////////////////////////////////////////////////////////
825

    
826
  void setRotationRowOffset(int puzzleFace, float[] offset)
827
    {
828
    mRowOffsets[puzzleFace][0] = offset[0];
829
    mRowOffsets[puzzleFace][1] = offset[1];
830
    mRowOffsets[puzzleFace][2] = offset[2];
831
    }
832

    
833
///////////////////////////////////////////////////////////////////////////////////////////////////
834

    
835
  public int[][] getSolvedQuats()
836
    {
837
    int[] groups = new int[mNumCubits];
838
    int numGroups = 1;
839
    int numFirst  = 0;
840

    
841
    for(int cubit=0; cubit<mNumCubits; cubit++)
842
      {
843
      groups[cubit] = retCubitSolvedStatus(cubit);
844
      if( groups[cubit]>=0 ) numGroups++;
845
      else                   numFirst++;
846
      }
847

    
848
    int firstIndex = 1;
849
    int groupIndex = 1;
850
    int[][] solvedQuats = new int[numGroups][];
851
    solvedQuats[0] = new int[1+numFirst];
852
    solvedQuats[0][0] = numFirst;
853
    Static3D[] axis = getFaceAxis();
854

    
855
    for(int cubit=0; cubit<mNumCubits; cubit++)
856
      {
857
      int group = groups[cubit];
858

    
859
      if( group<0 )
860
        {
861
        solvedQuats[0][firstIndex] = cubit;
862
        firstIndex++;
863
        }
864
      else
865
        {
866
        int[] quats = buildSolvedQuats(axis[group]);
867
        int len = quats==null ? 0 : quats.length;
868
        solvedQuats[groupIndex] = new int[2+len];
869
        solvedQuats[groupIndex][0] = 1;
870
        solvedQuats[groupIndex][1] = cubit;
871
        for(int i=0; i<len; i++) solvedQuats[groupIndex][i+2] = quats[i];
872
        groupIndex++;
873
        }
874
      }
875
/*
876
    String dbg = "SOLVED GROUPS:\n";
877

    
878
    for(int g=0; g<numGroups; g++)
879
      {
880
      int len = solvedQuats[g].length;
881
      for(int i=0; i<len; i++) dbg += (" "+solvedQuats[g][i]);
882
      dbg+="\n";
883
      }
884

    
885
    android.util.Log.e("D", dbg);
886
*/
887
    return solvedQuats;
888
    }
889

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

    
892
  public int getSolvedFunctionIndex()
893
    {
894
    return 0;
895
    }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898
// special SolvedQuats for the case where there are no corner of edge cubits.
899
// first row {0} - means there are no corners or edges.
900
// each next defines all cubits of a singe face (numCubits, firstCubit, cubit1,..,cubitN-1, quat0,..., quatM
901

    
902
  private boolean isSolvedCentersOnly()
903
    {
904
    int numGroups = mSolvedQuats.length;
905

    
906
    for(int group=1; group<numGroups; group++)
907
      {
908
      int numEntries= mSolvedQuats[group].length;
909
      int numCubits = mSolvedQuats[group][0];
910
      int firstCubit= mSolvedQuats[group][1];
911
      int firstQuat = mCubits[firstCubit].mQuatIndex;
912

    
913
      for(int cubit=2; cubit<=numCubits; cubit++)
914
        {
915
        int currCubit= mSolvedQuats[group][cubit];
916
        int currQuat = mCubits[currCubit].mQuatIndex;
917
        boolean isGood= (firstQuat==currQuat);
918

    
919
        for(int q=numCubits+1; !isGood && q<numEntries; q++)
920
          {
921
          int quat = mSolvedQuats[group][q];
922
          if( firstQuat == getMultQuat(currQuat,quat) ) isGood = true;
923
          }
924

    
925
        if( !isGood ) return false;
926
        }
927
      }
928

    
929
    return true;
930
    }
931

    
932
///////////////////////////////////////////////////////////////////////////////////////////////////
933

    
934
  private boolean isSolved0()
935
    {
936
    if( mSolvedQuats[0][0]==0 ) return isSolvedCentersOnly();
937

    
938
    for( int[] solvedQuat : mSolvedQuats )
939
      {
940
      int numCubits = solvedQuat[0];
941
      int firstCubit= solvedQuat[1];
942
      int quat = mCubits[firstCubit].mQuatIndex;
943

    
944
      for( int cubit=2; cubit<=numCubits; cubit++ )
945
        {
946
        int c = solvedQuat[cubit];
947
        if( quat != mCubits[c].mQuatIndex ) return false;
948
        }
949
      }
950

    
951
    int cubit= mSolvedQuats[0][1];
952
    int quat0= mCubits[cubit].mQuatIndex;
953
    int numGroups = mSolvedQuats.length;
954

    
955
    for(int group=1; group<numGroups; group++)
956
      {
957
      int firstCubit= mSolvedQuats[group][1];
958
      int currQuat  = mCubits[firstCubit].mQuatIndex;
959

    
960
      if( quat0==currQuat ) continue;
961

    
962
      boolean isGood= false;
963
      int numEntries= mSolvedQuats[group].length;
964
      int numCubits = mSolvedQuats[group][0];
965

    
966
      for(int q=numCubits+1; q<numEntries; q++)
967
        {
968
        int quat = mSolvedQuats[group][q];
969

    
970
        if( quat0 == getMultQuat(currQuat,quat) )
971
          {
972
          isGood = true;
973
          break;
974
          }
975
        }
976

    
977
      if( !isGood ) return false;
978
      }
979

    
980
    return true;
981
    }
982

    
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984

    
985
  private int computeScramble(int quatNum, int centerNum)
986
    {
987
    float MAXDIFF = 0.01f;
988
    float[] center= mOrigPos[centerNum];
989
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
990
    Static4D result = QuatHelper.rotateVectorByQuat(sc,mObjectQuats[quatNum]);
991

    
992
    float x = result.get0();
993
    float y = result.get1();
994
    float z = result.get2();
995

    
996
    for(int c=0; c<mNumCubits; c++)
997
      {
998
      float[] cent = mOrigPos[c];
999

    
1000
      float qx = cent[0] - x;
1001
      float qy = cent[1] - y;
1002
      float qz = cent[2] - z;
1003

    
1004
      if( qx>-MAXDIFF && qx<MAXDIFF &&
1005
          qy>-MAXDIFF && qy<MAXDIFF &&
1006
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
1007
      }
1008

    
1009
    return -1;
1010
    }
1011

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////
1013
// Dino4 uses this. It is solved if and only if groups of cubits
1014
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
1015
// or
1016
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
1017
// are all the same color.
1018

    
1019
  private boolean isSolved1()
1020
    {
1021
    if( mScramble==null )
1022
      {
1023
      mScramble = new int[mNumQuats][mNumCubits];
1024
      mColors   = new int[mNumCubits];
1025

    
1026
      for(int q=0; q<mNumQuats; q++)
1027
        for(int c=0; c<mNumCubits; c++) mScramble[q][c] = computeScramble(q,c);
1028
      }
1029

    
1030
    if( mFaceMap==null )
1031
      {
1032
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
1033
      }
1034

    
1035
    for(int c=0; c<mNumCubits; c++)
1036
      {
1037
      int index = mScramble[mCubits[c].mQuatIndex][c];
1038
      mColors[index] = mFaceMap[c];
1039
      }
1040

    
1041
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
1042
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
1043
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
1044

    
1045
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
1046
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
1047
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
1048

    
1049
    return false;
1050
    }
1051

    
1052
///////////////////////////////////////////////////////////////////////////////////////////////////
1053

    
1054
  int computeRow(float[] pos, int axisIndex, int cubitType, int puzzleFace)
1055
    {
1056
    int ret=0;
1057
    int len = pos.length / 3;
1058
    Static3D axis = mAxis[axisIndex];
1059
    float axisX = axis.get0();
1060
    float axisY = axis.get1();
1061
    float axisZ = axis.get2();
1062
    float casted, xoff=0, yoff=0, zoff=0;
1063

    
1064
    if( cubitType!=Cubit.TYPE_NORMAL )
1065
      {
1066
      xoff = mRowOffsets[puzzleFace][0];
1067
      yoff = mRowOffsets[puzzleFace][1];
1068
      zoff = mRowOffsets[puzzleFace][2];
1069
      }
1070

    
1071
    for(int i=0; i<len; i++)
1072
      {
1073
      casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
1074
      ret |= computeSingleRow(axisIndex,casted);
1075
      }
1076

    
1077
    return ret;
1078
    }
1079

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

    
1082
  private int computeSingleRow(int axisIndex,float casted)
1083
    {
1084
    int num = mNumCuts[axisIndex];
1085

    
1086
    for(int i=0; i<num; i++)
1087
      {
1088
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
1089
      }
1090

    
1091
    return (1<<num);
1092
    }
1093

    
1094
///////////////////////////////////////////////////////////////////////////////////////////////////
1095

    
1096
  private boolean wasRotateApplied()
1097
    {
1098
    return mEffects.exists(mRotateEffect.getID());
1099
    }
1100

    
1101
///////////////////////////////////////////////////////////////////////////////////////////////////
1102

    
1103
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
1104
    {
1105
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
1106
    }
1107

    
1108
///////////////////////////////////////////////////////////////////////////////////////////////////
1109
// note the minus in front of the sin() - we rotate counterclockwise
1110
// when looking towards the direction where the axis increases in values.
1111

    
1112
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
1113
    {
1114
    Static3D axis = mAxis[axisIndex];
1115

    
1116
    while( angleInDegrees<0 ) angleInDegrees += 360;
1117
    angleInDegrees %= 360;
1118
    
1119
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
1120
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
1121

    
1122
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
1123
    }
1124

    
1125
///////////////////////////////////////////////////////////////////////////////////////////////////
1126

    
1127
  private synchronized void setupPosition(int[][] moves)
1128
    {
1129
    if( moves!=null )
1130
      {
1131
      Static4D quat;
1132
      int index, axis, row, rowBitmap, angle;
1133

    
1134
      for(int[] move: moves)
1135
        {
1136
        axis     = move[0];
1137
        rowBitmap= computeBitmapFromRow( move[1],axis );
1138
        row      = computeRowFromBitmap( move[1] );
1139
        angle    = move[2]*(360/mBasicAngles[axis][row]);   // this assumes that all layers from
1140
                                                            // the bitmap have the same BasicAngle.
1141
                                                            // at the moment this is always true as
1142
                                                            // there are no bandaged objects with
1143
                                                            // different per-layer BasicAngles.
1144
        quat = makeQuaternion(axis,angle);
1145

    
1146
        for(int j=0; j<mNumCubits; j++)
1147
          {
1148
          mBelongs[j] = belongsToRotation(j,axis,rowBitmap);
1149
          if( mBelongs[j] ) mCubits[j].rotateCubit(quat);
1150
          }
1151

    
1152
        for(int j=0; j<mNumCubits; j++)
1153
          {
1154
          if( mBelongs[j] )
1155
            {
1156
            index = mCubits[j].postRotateCubit(quat);
1157
            setCubitQuat(j,mCubits[j].computeAssociation(),index);
1158
            }
1159
          else if( mCubits[j].getType()==Cubit.TYPE_FOLLOWER )
1160
            {
1161
            mCubits[j].adjustRotRowPostRotateADecider();
1162
            setCubitQuat(j,mCubits[j].computeAssociation(),mCubits[j].mQuatIndex);
1163
            }
1164
          }
1165
        }
1166
      }
1167
    }
1168

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

    
1171
  public int getScrambleType()
1172
    {
1173
    return 0;
1174
    }
1175

    
1176
///////////////////////////////////////////////////////////////////////////////////////////////////
1177

    
1178
  int computeBitmapFromRow(int rowBitmap, int axis)
1179
    {
1180
    if( mIsBandaged )
1181
      {
1182
      int bitmap, initBitmap=0;
1183

    
1184
      while( initBitmap!=rowBitmap )
1185
        {
1186
        initBitmap = rowBitmap;
1187

    
1188
        for(int cubit=0; cubit<mNumCubits; cubit++)
1189
          {
1190
          bitmap = mCubits[cubit].getRotRow(axis);
1191
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
1192
          }
1193
        }
1194
      }
1195

    
1196
    return rowBitmap;
1197
    }
1198

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

    
1201
  private int computeRowFromBitmap(int rowBitmap)
1202
    {
1203
    int index = 0;
1204

    
1205
    while(index<32)
1206
      {
1207
      if( (rowBitmap&0x1) != 0 ) return index;
1208
      rowBitmap>>=1;
1209
      index++;
1210
      }
1211
    return 0;
1212
    }
1213

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

    
1218
  void clampPos(float[] pos, int offset)
1219
    {
1220
    float currError, minError = Float.MAX_VALUE;
1221
    int minErrorIndex1 = -1;
1222
    int minErrorIndex2 = -1;
1223

    
1224
    float x = pos[offset  ];
1225
    float y = pos[offset+1];
1226
    float z = pos[offset+2];
1227

    
1228
    float xo,yo,zo;
1229

    
1230
    for(int i=0; i<mNumCubits; i++)
1231
      {
1232
      int len = mOrigPos[i].length / 3;
1233

    
1234
      for(int j=0; j<len; j++)
1235
        {
1236
        xo = mOrigPos[i][3*j  ];
1237
        yo = mOrigPos[i][3*j+1];
1238
        zo = mOrigPos[i][3*j+2];
1239

    
1240
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1241

    
1242
        if( currError<minError )
1243
          {
1244
          minError = currError;
1245
          minErrorIndex1 = i;
1246
          minErrorIndex2 = j;
1247
          }
1248
        }
1249
      }
1250

    
1251
    if( minError< 0.1f ) // TODO: 0.1 ?
1252
      {
1253
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1254
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1255
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1256
      }
1257
    }
1258

    
1259
///////////////////////////////////////////////////////////////////////////////////////////////////
1260
// remember about the double cover or unit quaternions!
1261

    
1262
  int mulQuat(int q1, int q2)
1263
    {
1264
    Static4D result = QuatHelper.quatMultiply(mObjectQuats[q1],mObjectQuats[q2]);
1265

    
1266
    float rX = result.get0();
1267
    float rY = result.get1();
1268
    float rZ = result.get2();
1269
    float rW = result.get3();
1270

    
1271
    final float MAX_ERROR = 0.1f;
1272
    float dX,dY,dZ,dW;
1273

    
1274
    for(int i=0; i<mNumQuats; i++)
1275
      {
1276
      dX = mObjectQuats[i].get0() - rX;
1277
      dY = mObjectQuats[i].get1() - rY;
1278
      dZ = mObjectQuats[i].get2() - rZ;
1279
      dW = mObjectQuats[i].get3() - rW;
1280

    
1281
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
1282
          dY<MAX_ERROR && dY>-MAX_ERROR &&
1283
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
1284
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
1285

    
1286
      dX = mObjectQuats[i].get0() + rX;
1287
      dY = mObjectQuats[i].get1() + rY;
1288
      dZ = mObjectQuats[i].get2() + rZ;
1289
      dW = mObjectQuats[i].get3() + rW;
1290

    
1291
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
1292
          dY<MAX_ERROR && dY>-MAX_ERROR &&
1293
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
1294
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
1295
      }
1296

    
1297
    return -1;
1298
    }
1299

    
1300
///////////////////////////////////////////////////////////////////////////////////////////////////
1301

    
1302
  private float getAngle()
1303
    {
1304
    int pointNum = mRotationAngle.getNumPoints();
1305

    
1306
    if( pointNum>=1 )
1307
      {
1308
      return mRotationAngle.getPoint(pointNum-1).get0();
1309
      }
1310
    else
1311
      {
1312
      mInterface.reportProblem("points in RotationAngle: "+pointNum, false);
1313
      return 0;
1314
      }
1315
    }
1316

    
1317
///////////////////////////////////////////////////////////////////////////////////////////////////
1318

    
1319
  void setLibInterface(ObjectLibInterface inter)
1320
    {
1321
    mInterface = inter;
1322
    }
1323

    
1324
///////////////////////////////////////////////////////////////////////////////////////////////////
1325

    
1326
  void initializeObject(int[][] moves)
1327
    {
1328
    solve();
1329
    setupPosition(moves);
1330
    }
1331

    
1332
///////////////////////////////////////////////////////////////////////////////////////////////////
1333

    
1334
  synchronized void removeRotationNow()
1335
    {
1336
    float angle = getAngle();
1337
    double nearestAngleInRadians = angle*Math.PI/180;
1338
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1339
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1340
    float axisX = mAxis[mCurrentRotAxis].get0();
1341
    float axisY = mAxis[mCurrentRotAxis].get1();
1342
    float axisZ = mAxis[mCurrentRotAxis].get2();
1343
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1344

    
1345
    mRotationAngle.removeAll();
1346
    mRotationAngleStatic.set0(0);
1347

    
1348
    for(int i=0; i<mNumCubits; i++)
1349
      {
1350
      mBelongs[i] = belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap);
1351
      if( mBelongs[i] ) mCubits[i].rotateCubit(quat);
1352
      }
1353

    
1354
    for(int i=0; i<mNumCubits; i++)
1355
      {
1356
      if( mBelongs[i] )
1357
        {
1358
        int index = mCubits[i].postRotateCubit(quat);
1359
        setCubitQuat(i,mCubits[i].computeAssociation(),index);
1360
        }
1361
      else if( mCubits[i].getType()==Cubit.TYPE_FOLLOWER )
1362
        {
1363
        mCubits[i].adjustRotRowPostRotateADecider();
1364
        setCubitQuat(i,mCubits[i].computeAssociation(),mCubits[i].mQuatIndex);
1365
        }
1366
      }
1367
    }
1368

    
1369
///////////////////////////////////////////////////////////////////////////////////////////////////
1370

    
1371
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1372
    {
1373
    if( wasRotateApplied() )
1374
      {
1375
      float angle = getAngle();
1376
      mRotationAngleStatic.set0(angle);
1377
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1378
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1379

    
1380
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1381
      mRotationAngle.resetToBeginning();
1382
      mRotationAngle.removeAll();
1383
      mRotationAngle.add(mRotationAngleStatic);
1384
      mRotationAngle.add(mRotationAngleMiddle);
1385
      mRotationAngle.add(mRotationAngleFinal);
1386
      mRotateEffect.notifyWhenFinished(listener);
1387

    
1388
      return mRotateEffect.getID();
1389
      }
1390

    
1391
    return 0;
1392
    }
1393

    
1394
///////////////////////////////////////////////////////////////////////////////////////////////////
1395

    
1396
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1397
    {
1398
    if( wasRotateApplied() )
1399
      {
1400
      mCurrentRotAxis = axis;
1401
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1402

    
1403
      mRotationAngleStatic.set0(0.0f);
1404
      mRotationAxis.set( mAxis[axis] );
1405
      mRotationAngle.setDuration(durationMillis);
1406
      mRotationAngle.resetToBeginning();
1407
      mRotationAngle.add(new Static1D(0));
1408
      mRotationAngle.add(new Static1D(angle));
1409
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1410
      mRotateEffect.notifyWhenFinished(listener);
1411

    
1412
      return mRotateEffect.getID();
1413
      }
1414

    
1415
    return 0;
1416
    }
1417

    
1418
///////////////////////////////////////////////////////////////////////////////////////////////////
1419

    
1420
  void continueRotation(float angleInDegrees)
1421
    {
1422
    mRotationAngleStatic.set0(angleInDegrees);
1423
    }
1424

    
1425
///////////////////////////////////////////////////////////////////////////////////////////////////
1426

    
1427
  synchronized void beginNewRotation(int axis, int row )
1428
    {
1429
    if( axis<0 || axis>=mNumAxis )
1430
      {
1431
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1432
      return;
1433
      }
1434
    if( row<0 || row>=mNumLayers[axis] )
1435
      {
1436
      android.util.Log.e("object", "invalid rotation row: "+row);
1437
      return;
1438
      }
1439

    
1440
    mCurrentRotAxis = axis;
1441
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1442
    mRotationAngleStatic.set0(0.0f);
1443
    mRotationAxis.set( mAxis[axis] );
1444
    mRotationAngle.add(mRotationAngleStatic);
1445
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1446
    }
1447

    
1448
///////////////////////////////////////////////////////////////////////////////////////////////////
1449

    
1450
  void setTextureMap(int cubit, int face, int newColor)
1451
    {
1452
    final float ratioW = 1.0f/mNumTexCols;
1453
    final float ratioH = 1.0f/mNumTexRows;
1454
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1455
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1456
    int col = newColor%mNumTexCols;
1457

    
1458
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1459
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1460
    }
1461

    
1462
///////////////////////////////////////////////////////////////////////////////////////////////////
1463

    
1464
  private int getCubitFaceColor(int cubit, int face)
1465
    {
1466
    int puzzleFace = getCubitFaceMap(cubit,face);
1467
    if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
1468
    return puzzleFace;
1469
    }
1470

    
1471
///////////////////////////////////////////////////////////////////////////////////////////////////
1472

    
1473
  public int getCubitFaceMap(int cubit, int face)
1474
    {
1475
    int numFaces = mCubitFaceColors[cubit].length;
1476
    int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
1477
    return puzzleFace<0 ? -1 : puzzleFace;
1478
    }
1479

    
1480
///////////////////////////////////////////////////////////////////////////////////////////////////
1481

    
1482
  void resetAllTextureMaps()
1483
    {
1484
    final float ratioW = 1.0f/mNumTexCols;
1485
    final float ratioH = 1.0f/mNumTexRows;
1486
    int cubColor, varColor, color, variant, row, col;
1487

    
1488
    for(int cubit=0; cubit<mNumCubits; cubit++)
1489
      {
1490
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1491
      variant = getCubitVariant(cubit,mNumLayers);
1492

    
1493
      for(int face=0; face<mNumCubitFaces; face++)
1494
        {
1495
        cubColor = getCubitFaceColor(cubit,face);
1496
        varColor = getVariantFaceColor(variant,face);
1497
        color    = cubColor<0 || varColor<0 ? mNumTextures : varColor*mNumFaceColors + cubColor;
1498
        row      = (mNumTexRows-1) - color/mNumTexCols;
1499
        col      = color%mNumTexCols;
1500

    
1501
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1502
        }
1503

    
1504
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1505
      }
1506
    }
1507

    
1508
///////////////////////////////////////////////////////////////////////////////////////////////////
1509

    
1510
  void releaseResources()
1511
    {
1512
    mTexture.markForDeletion();
1513
    mMesh.markForDeletion();
1514
    mEffects.markForDeletion();
1515

    
1516
    for(int j=0; j<mNumCubits; j++)
1517
      {
1518
      mCubits[j].releaseResources();
1519
      }
1520
    }
1521

    
1522
///////////////////////////////////////////////////////////////////////////////////////////////////
1523

    
1524
  private void setCubitQuat(int cubit, int andAssociation, int equAssociation)
1525
    {
1526
    if( !mIsInMixupMode )
1527
      {
1528
      mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
1529
      }
1530
    else
1531
      {
1532
      mMesh.setEffectAssociation(cubit,andAssociation,cubit);
1533
      Static4D tmp = mObjectQuats[equAssociation];
1534
      mMixupModeQuats[cubit].set(tmp);
1535
      }
1536
    }
1537

    
1538
///////////////////////////////////////////////////////////////////////////////////////////////////
1539

    
1540
  synchronized void restorePreferences(SharedPreferences preferences)
1541
    {
1542
    boolean error = false;
1543
    String key = getShortName();
1544

    
1545
    for(int i=0; i<mNumCubits; i++)
1546
      {
1547
      mQuatDebug[i] = mCubits[i].restorePreferences(key,preferences);
1548

    
1549
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1550
        {
1551
        mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]]);
1552
        }
1553
      else
1554
        {
1555
        error = true;
1556
        }
1557
      }
1558

    
1559
    for(int i=0; i<mNumCubits; i++)
1560
      {
1561
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1562
        {
1563
        mCubits[i].computeRotationRow();
1564
        setCubitQuat(i,mCubits[i].computeAssociation(),mQuatDebug[i]);
1565
        }
1566
      else
1567
        {
1568
        error = true;
1569
        }
1570
      }
1571

    
1572
    if( error )
1573
      {
1574
      for(int i=0; i<mNumCubits; i++)
1575
        {
1576
        mCubits[i].solve();
1577
        setCubitQuat(i,mCubits[i].computeAssociation(),0);
1578
        }
1579
      }
1580
    }
1581

    
1582
///////////////////////////////////////////////////////////////////////////////////////////////////
1583

    
1584
  void savePreferences(SharedPreferences.Editor editor)
1585
    {
1586
    String key = getShortName();
1587
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,editor);
1588
    }
1589

    
1590
///////////////////////////////////////////////////////////////////////////////////////////////////
1591

    
1592
  public void removePreferences(SharedPreferences.Editor editor)
1593
    {
1594
    String key = getShortName();
1595
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,editor);
1596
    }
1597

    
1598
///////////////////////////////////////////////////////////////////////////////////////////////////
1599

    
1600
  private float computeRadiusCorrection(float[] sticker, int curr, int len)
1601
    {
1602
    final float A = 0.8f;  // 0<A<1
1603

    
1604
    int prev = curr>0 ? curr-1 : len-1;
1605
    int next = curr<len-1 ? curr+1 : 0;
1606

    
1607
    float v1x = sticker[2*prev  ]-sticker[2*curr  ];
1608
    float v1y = sticker[2*prev+1]-sticker[2*curr+1];
1609
    float v2x = sticker[2*next  ]-sticker[2*curr  ];
1610
    float v2y = sticker[2*next+1]-sticker[2*curr+1];
1611

    
1612
    float len1= v1x*v1x+v1y*v1y;
1613
    float len2= v2x*v2x+v2y*v2y;
1614

    
1615
    float cos = (v1x*v2x+v1y*v2y) / ( (float)Math.sqrt(len1*len2) );
1616

    
1617
    return 1-A*cos;
1618
    }
1619

    
1620
///////////////////////////////////////////////////////////////////////////////////////////////////
1621

    
1622
  public ObjectSticker retSticker(int sticker)
1623
    {
1624
    if( mStickers==null )
1625
      {
1626
      float rad = getStickerRadius();
1627
      float str = getStickerStroke();
1628
      float[][] angles = getStickerAngles();
1629
      int numStickers = mStickerCoords.length;
1630
      mStickers = new ObjectSticker[numStickers];
1631

    
1632
      for(int s=0; s<numStickers; s++)
1633
        {
1634
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1635
        float radius = rad / scale;
1636
        float stroke = str / scale;
1637
        int len = mStickerCoords[s].length/2;
1638
        float[] radii = new float[len];
1639
        for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
1640
        mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
1641
        }
1642
      }
1643

    
1644
    return mStickers[sticker];
1645
    }
1646

    
1647
///////////////////////////////////////////////////////////////////////////////////////////////////
1648
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1649

    
1650
  public void adjustStickerCoords()
1651
    {
1652

    
1653
    }
1654

    
1655
///////////////////////////////////////////////////////////////////////////////////////////////////
1656

    
1657
  public Static4D[] getQuats()
1658
    {
1659
    if( mObjectQuats==null )
1660
      {
1661
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1662
      }
1663

    
1664
    return mObjectQuats;
1665
    }
1666

    
1667
///////////////////////////////////////////////////////////////////////////////////////////////////
1668

    
1669
  public int[][] getVariantFaceIsOuter()
1670
    {
1671
    return mVariantFaceIsOuter;
1672
    }
1673

    
1674
///////////////////////////////////////////////////////////////////////////////////////////////////
1675

    
1676
  public int getInternalColor()
1677
    {
1678
    return COLOR_INTERNAL;
1679
    }
1680

    
1681
///////////////////////////////////////////////////////////////////////////////////////////////////
1682
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1683

    
1684
  private void createTexture()
1685
    {
1686
    Paint paint = new Paint();
1687
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_4444);
1688
    Canvas canvas = new Canvas(mBitmap);
1689

    
1690
    paint.setAntiAlias(true);
1691
    paint.setTextAlign(Paint.Align.CENTER);
1692
    paint.setStyle(Paint.Style.FILL);
1693

    
1694
    paint.setColor(getInternalColor());
1695
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1696

    
1697
    int texture = 0;
1698
    FactorySticker factory = FactorySticker.getInstance();
1699

    
1700
    for(int row=0; row<mNumTexRows; row++)
1701
      for(int col=0; col<mNumTexCols; col++)
1702
        {
1703
        if( texture>=mNumTextures ) break;
1704
        ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1705
        int color = getColor(texture% mNumFaceColors);
1706
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color, sticker);
1707
        texture++;
1708
        }
1709
    }
1710

    
1711
///////////////////////////////////////////////////////////////////////////////////////////////////
1712

    
1713
  void setTexture()
1714
    {
1715
    if( mBitmap==null ) createTexture();
1716

    
1717
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1718
      {
1719
      int max = DistortedLibrary.getMaxTextureSize();
1720
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1721
      }
1722
    }
1723

    
1724
///////////////////////////////////////////////////////////////////////////////////////////////////
1725

    
1726
  void setObjectRatioNow(float sc, int nodeSize)
1727
    {
1728
    mObjectScreenRatio = sc;
1729
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1730
    mObjectScale.set(scale,scale,scale);
1731

    
1732
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1733
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1734
    }
1735

    
1736
///////////////////////////////////////////////////////////////////////////////////////////////////
1737

    
1738
  void setObjectRatio(float sizeChange, int nodeSize)
1739
    {
1740
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1741

    
1742
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1743
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1744

    
1745
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1746
    }
1747

    
1748
///////////////////////////////////////////////////////////////////////////////////////////////////
1749

    
1750
  void setNodeSize(int nodeSize)
1751
    {
1752
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1753
    }
1754

    
1755
///////////////////////////////////////////////////////////////////////////////////////////////////
1756

    
1757
  public float getRatio()
1758
    {
1759
    return mObjectScreenRatio;
1760
    }
1761

    
1762
///////////////////////////////////////////////////////////////////////////////////////////////////
1763

    
1764
  public float getObjectRatio()
1765
    {
1766
    return mObjectScreenRatio*mInitScreenRatio;
1767
    }
1768

    
1769
///////////////////////////////////////////////////////////////////////////////////////////////////
1770

    
1771
  boolean isSolved()
1772
    {
1773
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1774
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1775

    
1776
    return false;
1777
    }
1778

    
1779
///////////////////////////////////////////////////////////////////////////////////////////////////
1780

    
1781
  int computeNearestAngle(int basicAngle, float angle, float speed)
1782
    {
1783
    int nearestAngle = 360/basicAngle;
1784
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1785
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1786

    
1787
    if( tmp!=0 ) return nearestAngle*tmp;
1788

    
1789
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1790
    }
1791

    
1792
///////////////////////////////////////////////////////////////////////////////////////////////////
1793
// INTERNAL API - those are called from 'effects' package
1794
///////////////////////////////////////////////////////////////////////////////////////////////////
1795

    
1796
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1797
    {
1798
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1799
    }
1800

    
1801
///////////////////////////////////////////////////////////////////////////////////////////////////
1802

    
1803
  public Static4D getRotationQuat()
1804
    {
1805
    return mQuat;
1806
    }
1807

    
1808
///////////////////////////////////////////////////////////////////////////////////////////////////
1809

    
1810
  public float getSize()
1811
    {
1812
    return mSize;
1813
    }
1814

    
1815
///////////////////////////////////////////////////////////////////////////////////////////////////
1816

    
1817
  public void apply(Effect effect, int position)
1818
    {
1819
    mEffects.apply(effect, position);
1820
    }
1821

    
1822
///////////////////////////////////////////////////////////////////////////////////////////////////
1823

    
1824
  public void remove(long effectID)
1825
    {
1826
    mEffects.abortById(effectID);
1827
    }
1828

    
1829
///////////////////////////////////////////////////////////////////////////////////////////////////
1830

    
1831
  public MeshBase getObjectMesh()
1832
    {
1833
    return mMesh;
1834
    }
1835

    
1836
///////////////////////////////////////////////////////////////////////////////////////////////////
1837

    
1838
  public DistortedEffects getObjectEffects()
1839
    {
1840
    return mEffects;
1841
    }
1842

    
1843
///////////////////////////////////////////////////////////////////////////////////////////////////
1844

    
1845
  public int getCubitType(int cubit)
1846
    {
1847
    return mCubits[cubit].getType();
1848
    }
1849

    
1850
///////////////////////////////////////////////////////////////////////////////////////////////////
1851

    
1852
  public float[] getCubitOffset(int cubit)
1853
    {
1854
    return mCubits[cubit].getOffset();
1855
    }
1856

    
1857
///////////////////////////////////////////////////////////////////////////////////////////////////
1858
// PUBLIC API
1859
///////////////////////////////////////////////////////////////////////////////////////////////////
1860

    
1861
  public int getCubitFaceColorIndex(int cubit, int face)
1862
    {
1863
    Static4D texMap = mMesh.getTextureMap(mNumFaceColors *cubit + face);
1864

    
1865
    int x = (int)(texMap.get0()/texMap.get2());
1866
    int y = (int)(texMap.get1()/texMap.get3());
1867

    
1868
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1869
    }
1870

    
1871
///////////////////////////////////////////////////////////////////////////////////////////////////
1872

    
1873
  public int[] getNumLayers()
1874
    {
1875
    return mNumLayers;
1876
    }
1877

    
1878
///////////////////////////////////////////////////////////////////////////////////////////////////
1879

    
1880
  public synchronized void solve()
1881
    {
1882
    for(int i=0; i<mNumCubits; i++)
1883
      {
1884
      mCubits[i].solve();
1885
      setCubitQuat(i,mCubits[i].computeAssociation(),0);
1886
      }
1887
    }
1888

    
1889
///////////////////////////////////////////////////////////////////////////////////////////////////
1890

    
1891
  public int getCubitQuatIndex(int cubit)
1892
    {
1893
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1894
    }
1895

    
1896
///////////////////////////////////////////////////////////////////////////////////////////////////
1897

    
1898
  public int getCubitRotRow(int cubit, int axis)
1899
    {
1900
    return mCubits[cubit].getRotRow(axis);
1901
    }
1902

    
1903
///////////////////////////////////////////////////////////////////////////////////////////////////
1904

    
1905
  public Bitmap getStickerBitmap()
1906
    {
1907
    return mBitmap;
1908
    }
1909

    
1910
///////////////////////////////////////////////////////////////////////////////////////////////////
1911

    
1912
  public DistortedNode getNode()
1913
    {
1914
    return mNode;
1915
    }
1916

    
1917
///////////////////////////////////////////////////////////////////////////////////////////////////
1918

    
1919
  public int getNumStickerTypes()
1920
    {
1921
    return mNumStickerTypes;
1922
    }
1923

    
1924
///////////////////////////////////////////////////////////////////////////////////////////////////
1925
// this is here only so it can be overridden in TwistyJSON soo that we can get this from JSON.
1926

    
1927
  public int getNumCubitFaces()
1928
    {
1929
    return 0;
1930
    }
1931

    
1932
///////////////////////////////////////////////////////////////////////////////////////////////////
1933

    
1934
  public TouchControl getTouchControl()
1935
    {
1936
    if( mTouchControl==null )
1937
      {
1938
      switch(getTouchControlType())
1939
        {
1940
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
1941
                                   break;
1942
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
1943
                                   break;
1944
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
1945
                                   break;
1946
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
1947
                                   break;
1948
        case TC_CUBOID           : int[] numLayers = getNumLayers();
1949
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1950
                                   break;
1951
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
1952
                                   break;
1953
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
1954
                                   break;
1955
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
1956
                                   break;
1957
        }
1958
      }
1959
    return mTouchControl;
1960
    }
1961

    
1962
///////////////////////////////////////////////////////////////////////////////////////////////////
1963

    
1964
  protected void setReader(JsonReader reader)
1965
    {
1966
    // empty
1967
    }
1968

    
1969
///////////////////////////////////////////////////////////////////////////////////////////////////
1970
  // for JSON only
1971
  public abstract int getTouchControlType();
1972
  public abstract int getTouchControlSplit();
1973
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1974
  public abstract int[][][] getEnabled();
1975
  public abstract float[] getDist3D(int[] numLayers);
1976
  public abstract Static3D[] getFaceAxis();
1977
  public abstract ScrambleState[] getScrambleStates();
1978
  public abstract float[][] getCuts(int[] numLayers);
1979
  public abstract float getStickerRadius();
1980
  public abstract float getStickerStroke();
1981
  public abstract float[][] getStickerAngles();
1982
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1983
  public abstract ObjectShape getObjectShape(int variant);
1984
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
1985
  public abstract int getNumCubitVariants(int[] numLayers);
1986
  public abstract float[][] getCubitPositions(int[] numLayers);
1987
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
1988
  public abstract int getNumFaceColors();
1989
  public abstract float getScreenRatio();
1990
  public abstract int getColor(int face);
1991
  public abstract String getShortName();
1992
  public abstract ObjectSignature getSignature();
1993

    
1994
  // not only for JSON
1995
  public abstract Static3D[] getRotationAxis();
1996
  public abstract int[][] getBasicAngles();
1997
  public abstract int getNumFaces();
1998
  public abstract String getObjectName();
1999
  public abstract String getInventor();
2000
  public abstract int getYearOfInvention();
2001
  public abstract int getComplexity();
2002
  public abstract int getFOV();
2003
  public abstract String[][] getTutorials();
2004
  }
(12-12/13)