Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 40e77224

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
      int[][] indices = mShapes[variant].getVertIndices();
734
      int outer=-1, faces = indices.length;
735

    
736
      // object must have been created from JSON
737
      if( mVariantFaceIsOuter==null || mVariantFaceIsOuter[variant]==null )
738
        {
739
        mVariantFaceIsOuter = getVariantFaceIsOuter();
740
        }
741

    
742
      for(int i=0; i<faces; i++)
743
        {
744
        if( mVariantFaceIsOuter[variant][i]==1 )
745
          {
746
          outer=i;
747
          break;
748
          }
749
        }
750

    
751
      if( outer>=0 )
752
        {
753
        int vertIndex = indices[outer][0];
754
        float[] vertices = mShapes[variant].getVertices()[vertIndex];
755
        float[] ret = new float[3];
756
        float[] curpos = mOrigPos[cubitIndex];
757
        Static4D quat = mOrigQuat[cubitIndex];
758
        QuatHelper.rotateVectorByQuat(mTmp, vertices[0], vertices[1], vertices[2], 1, quat);
759

    
760
        ret[0] = mTmp[0]+computeAvg(curpos,0);
761
        ret[1] = mTmp[1]+computeAvg(curpos,1);
762
        ret[2] = mTmp[2]+computeAvg(curpos,2);
763

    
764
        return ret;
765
        }
766
      else
767
        {
768
        android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
769
        }
770
      }
771

    
772
    return null;
773
    }
774

    
775
///////////////////////////////////////////////////////////////////////////////////////////////////
776

    
777
  public int computeCurrentPuzzleFace(int type, float[] vertex)
778
    {
779
    if( type!=Cubit.TYPE_NORMAL )
780
      {
781
      Static3D[] axis = getFaceAxis();
782
      float[] dist3D = getDist3D(mNumLayers);
783
      final float MAXERR = 0.98f;
784
      int numAxis = axis.length;
785
      float x = vertex[0];
786
      float y = vertex[1];
787
      float z = vertex[2];
788

    
789
      for(int i=0; i<numAxis; i++)
790
        {
791
        Static3D ax = axis[i];
792
        float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
793
        if( len>mSize*dist3D[i]*MAXERR ) return i;
794
        }
795
      }
796

    
797
    return -1;
798
    }
799

    
800
///////////////////////////////////////////////////////////////////////////////////////////////////
801

    
802
  public float[] getCubitRowOffset(int cubitIndex)
803
    {
804
    return null;
805
    }
806

    
807
///////////////////////////////////////////////////////////////////////////////////////////////////
808

    
809
  void setRotationRowOffset(int puzzleFace, float[] offset)
810
    {
811
    mRowOffsets[puzzleFace][0] = offset[0];
812
    mRowOffsets[puzzleFace][1] = offset[1];
813
    mRowOffsets[puzzleFace][2] = offset[2];
814
    }
815

    
816
///////////////////////////////////////////////////////////////////////////////////////////////////
817

    
818
  public int[][] getSolvedQuats()
819
    {
820
    int[] groups = new int[mNumCubits];
821
    int numGroups = 1;
822
    int numFirst  = 0;
823

    
824
    for(int cubit=0; cubit<mNumCubits; cubit++)
825
      {
826
      groups[cubit] = retCubitSolvedStatus(cubit);
827
      if( groups[cubit]>=0 ) numGroups++;
828
      else                   numFirst++;
829
      }
830

    
831
    int firstIndex = 1;
832
    int groupIndex = 1;
833
    int[][] solvedQuats = new int[numGroups][];
834
    solvedQuats[0] = new int[1+numFirst];
835
    solvedQuats[0][0] = numFirst;
836
    Static3D[] axis = getFaceAxis();
837

    
838
    for(int cubit=0; cubit<mNumCubits; cubit++)
839
      {
840
      int group = groups[cubit];
841

    
842
      if( group<0 )
843
        {
844
        solvedQuats[0][firstIndex] = cubit;
845
        firstIndex++;
846
        }
847
      else
848
        {
849
        int[] quats = buildSolvedQuats(axis[group]);
850
        int len = quats==null ? 0 : quats.length;
851
        solvedQuats[groupIndex] = new int[2+len];
852
        solvedQuats[groupIndex][0] = 1;
853
        solvedQuats[groupIndex][1] = cubit;
854
        for(int i=0; i<len; i++) solvedQuats[groupIndex][i+2] = quats[i];
855
        groupIndex++;
856
        }
857
      }
858
/*
859
    String dbg = "SOLVED GROUPS:\n";
860

    
861
    for(int g=0; g<numGroups; g++)
862
      {
863
      int len = solvedQuats[g].length;
864
      for(int i=0; i<len; i++) dbg += (" "+solvedQuats[g][i]);
865
      dbg+="\n";
866
      }
867

    
868
    android.util.Log.e("D", dbg);
869
*/
870
    return solvedQuats;
871
    }
872

    
873
///////////////////////////////////////////////////////////////////////////////////////////////////
874

    
875
  public int getSolvedFunctionIndex()
876
    {
877
    return 0;
878
    }
879

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

    
885
  private boolean isSolvedCentersOnly()
886
    {
887
    int numGroups = mSolvedQuats.length;
888

    
889
    for(int group=1; group<numGroups; group++)
890
      {
891
      int numEntries= mSolvedQuats[group].length;
892
      int numCubits = mSolvedQuats[group][0];
893
      int firstCubit= mSolvedQuats[group][1];
894
      int firstQuat = mCubits[firstCubit].mQuatIndex;
895

    
896
      for(int cubit=2; cubit<=numCubits; cubit++)
897
        {
898
        int currCubit= mSolvedQuats[group][cubit];
899
        int currQuat = mCubits[currCubit].mQuatIndex;
900
        boolean isGood= (firstQuat==currQuat);
901

    
902
        for(int q=numCubits+1; !isGood && q<numEntries; q++)
903
          {
904
          int quat = mSolvedQuats[group][q];
905
          if( firstQuat == getMultQuat(currQuat,quat) ) isGood = true;
906
          }
907

    
908
        if( !isGood ) return false;
909
        }
910
      }
911

    
912
    return true;
913
    }
914

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

    
917
  private boolean isSolved0()
918
    {
919
    if( mSolvedQuats[0][0]==0 ) return isSolvedCentersOnly();
920

    
921
    for( int[] solvedQuat : mSolvedQuats )
922
      {
923
      int numCubits = solvedQuat[0];
924
      int firstCubit= solvedQuat[1];
925
      int quat = mCubits[firstCubit].mQuatIndex;
926

    
927
      for( int cubit=2; cubit<=numCubits; cubit++ )
928
        {
929
        int c = solvedQuat[cubit];
930
        if( quat != mCubits[c].mQuatIndex ) return false;
931
        }
932
      }
933

    
934
    int cubit= mSolvedQuats[0][1];
935
    int quat0= mCubits[cubit].mQuatIndex;
936
    int numGroups = mSolvedQuats.length;
937

    
938
    for(int group=1; group<numGroups; group++)
939
      {
940
      int firstCubit= mSolvedQuats[group][1];
941
      int currQuat  = mCubits[firstCubit].mQuatIndex;
942

    
943
      if( quat0==currQuat ) continue;
944

    
945
      boolean isGood= false;
946
      int numEntries= mSolvedQuats[group].length;
947
      int numCubits = mSolvedQuats[group][0];
948

    
949
      for(int q=numCubits+1; q<numEntries; q++)
950
        {
951
        int quat = mSolvedQuats[group][q];
952

    
953
        if( quat0 == getMultQuat(currQuat,quat) )
954
          {
955
          isGood = true;
956
          break;
957
          }
958
        }
959

    
960
      if( !isGood ) return false;
961
      }
962

    
963
    return true;
964
    }
965

    
966
///////////////////////////////////////////////////////////////////////////////////////////////////
967

    
968
  private int computeScramble(int quatNum, int centerNum)
969
    {
970
    float MAXDIFF = 0.01f;
971
    float[] center= mOrigPos[centerNum];
972
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
973
    Static4D result = QuatHelper.rotateVectorByQuat(sc,mObjectQuats[quatNum]);
974

    
975
    float x = result.get0();
976
    float y = result.get1();
977
    float z = result.get2();
978

    
979
    for(int c=0; c<mNumCubits; c++)
980
      {
981
      float[] cent = mOrigPos[c];
982

    
983
      float qx = cent[0] - x;
984
      float qy = cent[1] - y;
985
      float qz = cent[2] - z;
986

    
987
      if( qx>-MAXDIFF && qx<MAXDIFF &&
988
          qy>-MAXDIFF && qy<MAXDIFF &&
989
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
990
      }
991

    
992
    return -1;
993
    }
994

    
995
///////////////////////////////////////////////////////////////////////////////////////////////////
996
// Dino4 uses this. It is solved if and only if groups of cubits
997
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
998
// or
999
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
1000
// are all the same color.
1001

    
1002
  private boolean isSolved1()
1003
    {
1004
    if( mScramble==null )
1005
      {
1006
      mScramble = new int[mNumQuats][mNumCubits];
1007
      mColors   = new int[mNumCubits];
1008

    
1009
      for(int q=0; q<mNumQuats; q++)
1010
        for(int c=0; c<mNumCubits; c++) mScramble[q][c] = computeScramble(q,c);
1011
      }
1012

    
1013
    if( mFaceMap==null )
1014
      {
1015
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
1016
      }
1017

    
1018
    for(int c=0; c<mNumCubits; c++)
1019
      {
1020
      int index = mScramble[mCubits[c].mQuatIndex][c];
1021
      mColors[index] = mFaceMap[c];
1022
      }
1023

    
1024
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
1025
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
1026
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
1027

    
1028
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
1029
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
1030
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
1031

    
1032
    return false;
1033
    }
1034

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

    
1037
  int computeRow(float[] pos, int axisIndex, int cubitType, int puzzleFace)
1038
    {
1039
    int ret=0;
1040
    int len = pos.length / 3;
1041
    Static3D axis = mAxis[axisIndex];
1042
    float axisX = axis.get0();
1043
    float axisY = axis.get1();
1044
    float axisZ = axis.get2();
1045
    float casted, xoff=0, yoff=0, zoff=0;
1046

    
1047
    if( cubitType!=Cubit.TYPE_NORMAL )
1048
      {
1049
      xoff = mRowOffsets[puzzleFace][0];
1050
      yoff = mRowOffsets[puzzleFace][1];
1051
      zoff = mRowOffsets[puzzleFace][2];
1052
      }
1053

    
1054
    for(int i=0; i<len; i++)
1055
      {
1056
      casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
1057
      ret |= computeSingleRow(axisIndex,casted);
1058
      }
1059

    
1060
    return ret;
1061
    }
1062

    
1063
///////////////////////////////////////////////////////////////////////////////////////////////////
1064

    
1065
  private int computeSingleRow(int axisIndex,float casted)
1066
    {
1067
    int num = mNumCuts[axisIndex];
1068

    
1069
    for(int i=0; i<num; i++)
1070
      {
1071
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
1072
      }
1073

    
1074
    return (1<<num);
1075
    }
1076

    
1077
///////////////////////////////////////////////////////////////////////////////////////////////////
1078

    
1079
  private boolean wasRotateApplied()
1080
    {
1081
    return mEffects.exists(mRotateEffect.getID());
1082
    }
1083

    
1084
///////////////////////////////////////////////////////////////////////////////////////////////////
1085

    
1086
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
1087
    {
1088
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
1089
    }
1090

    
1091
///////////////////////////////////////////////////////////////////////////////////////////////////
1092
// note the minus in front of the sin() - we rotate counterclockwise
1093
// when looking towards the direction where the axis increases in values.
1094

    
1095
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
1096
    {
1097
    Static3D axis = mAxis[axisIndex];
1098

    
1099
    while( angleInDegrees<0 ) angleInDegrees += 360;
1100
    angleInDegrees %= 360;
1101
    
1102
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
1103
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
1104

    
1105
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
1106
    }
1107

    
1108
///////////////////////////////////////////////////////////////////////////////////////////////////
1109

    
1110
  private synchronized void setupPosition(int[][] moves)
1111
    {
1112
    if( moves!=null )
1113
      {
1114
      Static4D quat;
1115
      int index, axis, row, rowBitmap, angle;
1116

    
1117
      for(int[] move: moves)
1118
        {
1119
        axis     = move[0];
1120
        rowBitmap= computeBitmapFromRow( move[1],axis );
1121
        row      = computeRowFromBitmap( move[1] );
1122
        angle    = move[2]*(360/mBasicAngles[axis][row]);   // this assumes that all layers from
1123
                                                            // the bitmap have the same BasicAngle.
1124
                                                            // at the moment this is always true as
1125
                                                            // there are no bandaged objects with
1126
                                                            // different per-layer BasicAngles.
1127
        quat = makeQuaternion(axis,angle);
1128

    
1129
        for(int j=0; j<mNumCubits; j++)
1130
          {
1131
          mBelongs[j] = belongsToRotation(j,axis,rowBitmap);
1132
          if( mBelongs[j] ) mCubits[j].rotateCubit(quat);
1133
          }
1134

    
1135
        for(int j=0; j<mNumCubits; j++)
1136
          {
1137
          if( mBelongs[j] )
1138
            {
1139
            index = mCubits[j].postRotateCubit(quat);
1140
            setCubitQuat(j,mCubits[j].computeAssociation(),index);
1141
            }
1142
          else if( mCubits[j].getType()==Cubit.TYPE_FOLLOWER )
1143
            {
1144
            mCubits[j].adjustRotRowPostRotateADecider();
1145
            setCubitQuat(j,mCubits[j].computeAssociation(),mCubits[j].mQuatIndex);
1146
            }
1147
          }
1148
        }
1149
      }
1150
    }
1151

    
1152
///////////////////////////////////////////////////////////////////////////////////////////////////
1153

    
1154
  public int getScrambleType()
1155
    {
1156
    return 0;
1157
    }
1158

    
1159
///////////////////////////////////////////////////////////////////////////////////////////////////
1160

    
1161
  int computeBitmapFromRow(int rowBitmap, int axis)
1162
    {
1163
    if( mIsBandaged )
1164
      {
1165
      int bitmap, initBitmap=0;
1166

    
1167
      while( initBitmap!=rowBitmap )
1168
        {
1169
        initBitmap = rowBitmap;
1170

    
1171
        for(int cubit=0; cubit<mNumCubits; cubit++)
1172
          {
1173
          bitmap = mCubits[cubit].getRotRow(axis);
1174
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
1175
          }
1176
        }
1177
      }
1178

    
1179
    return rowBitmap;
1180
    }
1181

    
1182
///////////////////////////////////////////////////////////////////////////////////////////////////
1183

    
1184
  private int computeRowFromBitmap(int rowBitmap)
1185
    {
1186
    int index = 0;
1187

    
1188
    while(index<32)
1189
      {
1190
      if( (rowBitmap&0x1) != 0 ) return index;
1191
      rowBitmap>>=1;
1192
      index++;
1193
      }
1194
    return 0;
1195
    }
1196

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

    
1201
  void clampPos(float[] pos, int offset)
1202
    {
1203
    float currError, minError = Float.MAX_VALUE;
1204
    int minErrorIndex1 = -1;
1205
    int minErrorIndex2 = -1;
1206

    
1207
    float x = pos[offset  ];
1208
    float y = pos[offset+1];
1209
    float z = pos[offset+2];
1210

    
1211
    float xo,yo,zo;
1212

    
1213
    for(int i=0; i<mNumCubits; i++)
1214
      {
1215
      int len = mOrigPos[i].length / 3;
1216

    
1217
      for(int j=0; j<len; j++)
1218
        {
1219
        xo = mOrigPos[i][3*j  ];
1220
        yo = mOrigPos[i][3*j+1];
1221
        zo = mOrigPos[i][3*j+2];
1222

    
1223
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1224

    
1225
        if( currError<minError )
1226
          {
1227
          minError = currError;
1228
          minErrorIndex1 = i;
1229
          minErrorIndex2 = j;
1230
          }
1231
        }
1232
      }
1233

    
1234
    if( minError< 0.1f ) // TODO: 0.1 ?
1235
      {
1236
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1237
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1238
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1239
      }
1240
    }
1241

    
1242
///////////////////////////////////////////////////////////////////////////////////////////////////
1243
// remember about the double cover or unit quaternions!
1244

    
1245
  int mulQuat(int q1, int q2)
1246
    {
1247
    Static4D result = QuatHelper.quatMultiply(mObjectQuats[q1],mObjectQuats[q2]);
1248

    
1249
    float rX = result.get0();
1250
    float rY = result.get1();
1251
    float rZ = result.get2();
1252
    float rW = result.get3();
1253

    
1254
    final float MAX_ERROR = 0.1f;
1255
    float dX,dY,dZ,dW;
1256

    
1257
    for(int i=0; i<mNumQuats; i++)
1258
      {
1259
      dX = mObjectQuats[i].get0() - rX;
1260
      dY = mObjectQuats[i].get1() - rY;
1261
      dZ = mObjectQuats[i].get2() - rZ;
1262
      dW = mObjectQuats[i].get3() - rW;
1263

    
1264
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
1265
          dY<MAX_ERROR && dY>-MAX_ERROR &&
1266
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
1267
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
1268

    
1269
      dX = mObjectQuats[i].get0() + rX;
1270
      dY = mObjectQuats[i].get1() + rY;
1271
      dZ = mObjectQuats[i].get2() + rZ;
1272
      dW = mObjectQuats[i].get3() + rW;
1273

    
1274
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
1275
          dY<MAX_ERROR && dY>-MAX_ERROR &&
1276
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
1277
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
1278
      }
1279

    
1280
    return -1;
1281
    }
1282

    
1283
///////////////////////////////////////////////////////////////////////////////////////////////////
1284

    
1285
  private float getAngle()
1286
    {
1287
    int pointNum = mRotationAngle.getNumPoints();
1288

    
1289
    if( pointNum>=1 )
1290
      {
1291
      return mRotationAngle.getPoint(pointNum-1).get0();
1292
      }
1293
    else
1294
      {
1295
      mInterface.reportProblem("points in RotationAngle: "+pointNum, false);
1296
      return 0;
1297
      }
1298
    }
1299

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

    
1302
  void setLibInterface(ObjectLibInterface inter)
1303
    {
1304
    mInterface = inter;
1305
    }
1306

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

    
1309
  void initializeObject(int[][] moves)
1310
    {
1311
    solve();
1312
    setupPosition(moves);
1313
    }
1314

    
1315
///////////////////////////////////////////////////////////////////////////////////////////////////
1316

    
1317
  synchronized void removeRotationNow()
1318
    {
1319
    float angle = getAngle();
1320
    double nearestAngleInRadians = angle*Math.PI/180;
1321
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1322
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1323
    float axisX = mAxis[mCurrentRotAxis].get0();
1324
    float axisY = mAxis[mCurrentRotAxis].get1();
1325
    float axisZ = mAxis[mCurrentRotAxis].get2();
1326
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1327

    
1328
    mRotationAngle.removeAll();
1329
    mRotationAngleStatic.set0(0);
1330

    
1331
    for(int i=0; i<mNumCubits; i++)
1332
      {
1333
      mBelongs[i] = belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap);
1334
      if( mBelongs[i] ) mCubits[i].rotateCubit(quat);
1335
      }
1336

    
1337
    for(int i=0; i<mNumCubits; i++)
1338
      {
1339
      if( mBelongs[i] )
1340
        {
1341
        int index = mCubits[i].postRotateCubit(quat);
1342
        setCubitQuat(i,mCubits[i].computeAssociation(),index);
1343
        }
1344
      else if( mCubits[i].getType()==Cubit.TYPE_FOLLOWER )
1345
        {
1346
        mCubits[i].adjustRotRowPostRotateADecider();
1347
        setCubitQuat(i,mCubits[i].computeAssociation(),mCubits[i].mQuatIndex);
1348
        }
1349
      }
1350
    }
1351

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

    
1354
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1355
    {
1356
    if( wasRotateApplied() )
1357
      {
1358
      float angle = getAngle();
1359
      mRotationAngleStatic.set0(angle);
1360
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1361
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1362

    
1363
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1364
      mRotationAngle.resetToBeginning();
1365
      mRotationAngle.removeAll();
1366
      mRotationAngle.add(mRotationAngleStatic);
1367
      mRotationAngle.add(mRotationAngleMiddle);
1368
      mRotationAngle.add(mRotationAngleFinal);
1369
      mRotateEffect.notifyWhenFinished(listener);
1370

    
1371
      return mRotateEffect.getID();
1372
      }
1373

    
1374
    return 0;
1375
    }
1376

    
1377
///////////////////////////////////////////////////////////////////////////////////////////////////
1378

    
1379
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1380
    {
1381
    if( wasRotateApplied() )
1382
      {
1383
      mCurrentRotAxis = axis;
1384
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1385

    
1386
      mRotationAngleStatic.set0(0.0f);
1387
      mRotationAxis.set( mAxis[axis] );
1388
      mRotationAngle.setDuration(durationMillis);
1389
      mRotationAngle.resetToBeginning();
1390
      mRotationAngle.add(new Static1D(0));
1391
      mRotationAngle.add(new Static1D(angle));
1392
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1393
      mRotateEffect.notifyWhenFinished(listener);
1394

    
1395
      return mRotateEffect.getID();
1396
      }
1397

    
1398
    return 0;
1399
    }
1400

    
1401
///////////////////////////////////////////////////////////////////////////////////////////////////
1402

    
1403
  void continueRotation(float angleInDegrees)
1404
    {
1405
    mRotationAngleStatic.set0(angleInDegrees);
1406
    }
1407

    
1408
///////////////////////////////////////////////////////////////////////////////////////////////////
1409

    
1410
  synchronized void beginNewRotation(int axis, int row )
1411
    {
1412
    if( axis<0 || axis>=mNumAxis )
1413
      {
1414
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1415
      return;
1416
      }
1417
    if( row<0 || row>=mNumLayers[axis] )
1418
      {
1419
      android.util.Log.e("object", "invalid rotation row: "+row);
1420
      return;
1421
      }
1422

    
1423
    mCurrentRotAxis = axis;
1424
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1425
    mRotationAngleStatic.set0(0.0f);
1426
    mRotationAxis.set( mAxis[axis] );
1427
    mRotationAngle.add(mRotationAngleStatic);
1428
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1429
    }
1430

    
1431
///////////////////////////////////////////////////////////////////////////////////////////////////
1432

    
1433
  void setTextureMap(int cubit, int face, int newColor)
1434
    {
1435
    final float ratioW = 1.0f/mNumTexCols;
1436
    final float ratioH = 1.0f/mNumTexRows;
1437
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1438
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1439
    int col = newColor%mNumTexCols;
1440

    
1441
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1442
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1443
    }
1444

    
1445
///////////////////////////////////////////////////////////////////////////////////////////////////
1446

    
1447
  private int getCubitFaceColor(int cubit, int face)
1448
    {
1449
    int puzzleFace = getCubitFaceMap(cubit,face);
1450
    if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
1451
    return puzzleFace;
1452
    }
1453

    
1454
///////////////////////////////////////////////////////////////////////////////////////////////////
1455

    
1456
  public int getCubitFaceMap(int cubit, int face)
1457
    {
1458
    int numFaces = mCubitFaceColors[cubit].length;
1459
    int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
1460
    return puzzleFace<0 ? -1 : puzzleFace;
1461
    }
1462

    
1463
///////////////////////////////////////////////////////////////////////////////////////////////////
1464

    
1465
  void resetAllTextureMaps()
1466
    {
1467
    final float ratioW = 1.0f/mNumTexCols;
1468
    final float ratioH = 1.0f/mNumTexRows;
1469
    int cubColor, varColor, color, variant, row, col;
1470

    
1471
    for(int cubit=0; cubit<mNumCubits; cubit++)
1472
      {
1473
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1474
      variant = getCubitVariant(cubit,mNumLayers);
1475

    
1476
      for(int face=0; face<mNumCubitFaces; face++)
1477
        {
1478
        cubColor = getCubitFaceColor(cubit,face);
1479
        varColor = getVariantFaceColor(variant,face);
1480
        color    = cubColor<0 || varColor<0 ? mNumTextures : varColor*mNumFaceColors + cubColor;
1481
        row      = (mNumTexRows-1) - color/mNumTexCols;
1482
        col      = color%mNumTexCols;
1483

    
1484
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1485
        }
1486

    
1487
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1488
      }
1489
    }
1490

    
1491
///////////////////////////////////////////////////////////////////////////////////////////////////
1492

    
1493
  void releaseResources()
1494
    {
1495
    mTexture.markForDeletion();
1496
    mMesh.markForDeletion();
1497
    mEffects.markForDeletion();
1498

    
1499
    for(int j=0; j<mNumCubits; j++)
1500
      {
1501
      mCubits[j].releaseResources();
1502
      }
1503
    }
1504

    
1505
///////////////////////////////////////////////////////////////////////////////////////////////////
1506

    
1507
  private void setCubitQuat(int cubit, int andAssociation, int equAssociation)
1508
    {
1509
    if( !mIsInMixupMode )
1510
      {
1511
      mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
1512
      }
1513
    else
1514
      {
1515
      mMesh.setEffectAssociation(cubit,andAssociation,cubit);
1516
      Static4D tmp = mObjectQuats[equAssociation];
1517
      mMixupModeQuats[cubit].set(tmp);
1518
      }
1519
    }
1520

    
1521
///////////////////////////////////////////////////////////////////////////////////////////////////
1522

    
1523
  synchronized void restorePreferences(SharedPreferences preferences)
1524
    {
1525
    boolean error = false;
1526
    String key = getShortName();
1527

    
1528
    for(int i=0; i<mNumCubits; i++)
1529
      {
1530
      mQuatDebug[i] = mCubits[i].restorePreferences(key,preferences);
1531

    
1532
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1533
        {
1534
        mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]]);
1535
        }
1536
      else
1537
        {
1538
        error = true;
1539
        }
1540
      }
1541

    
1542
    for(int i=0; i<mNumCubits; i++)
1543
      {
1544
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1545
        {
1546
        mCubits[i].computeRotationRow();
1547
        setCubitQuat(i,mCubits[i].computeAssociation(),mQuatDebug[i]);
1548
        }
1549
      else
1550
        {
1551
        error = true;
1552
        }
1553
      }
1554

    
1555
    if( error )
1556
      {
1557
      for(int i=0; i<mNumCubits; i++)
1558
        {
1559
        mCubits[i].solve();
1560
        setCubitQuat(i,mCubits[i].computeAssociation(),0);
1561
        }
1562
      }
1563
    }
1564

    
1565
///////////////////////////////////////////////////////////////////////////////////////////////////
1566

    
1567
  void savePreferences(SharedPreferences.Editor editor)
1568
    {
1569
    String key = getShortName();
1570
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,editor);
1571
    }
1572

    
1573
///////////////////////////////////////////////////////////////////////////////////////////////////
1574

    
1575
  public void removePreferences(SharedPreferences.Editor editor)
1576
    {
1577
    String key = getShortName();
1578
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,editor);
1579
    }
1580

    
1581
///////////////////////////////////////////////////////////////////////////////////////////////////
1582

    
1583
  private float computeRadiusCorrection(float[] sticker, int curr, int len)
1584
    {
1585
    final float A = 0.8f;  // 0<A<1
1586

    
1587
    int prev = curr>0 ? curr-1 : len-1;
1588
    int next = curr<len-1 ? curr+1 : 0;
1589

    
1590
    float v1x = sticker[2*prev  ]-sticker[2*curr  ];
1591
    float v1y = sticker[2*prev+1]-sticker[2*curr+1];
1592
    float v2x = sticker[2*next  ]-sticker[2*curr  ];
1593
    float v2y = sticker[2*next+1]-sticker[2*curr+1];
1594

    
1595
    float len1= v1x*v1x+v1y*v1y;
1596
    float len2= v2x*v2x+v2y*v2y;
1597

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

    
1600
    return 1-A*cos;
1601
    }
1602

    
1603
///////////////////////////////////////////////////////////////////////////////////////////////////
1604

    
1605
  public ObjectSticker retSticker(int sticker)
1606
    {
1607
    if( mStickers==null )
1608
      {
1609
      float rad = getStickerRadius();
1610
      float str = getStickerStroke();
1611
      float[][] angles = getStickerAngles();
1612
      int numStickers = mStickerCoords.length;
1613
      mStickers = new ObjectSticker[numStickers];
1614

    
1615
      for(int s=0; s<numStickers; s++)
1616
        {
1617
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1618
        float radius = rad / scale;
1619
        float stroke = str / scale;
1620
        int len = mStickerCoords[s].length/2;
1621
        float[] radii = new float[len];
1622
        for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
1623
        mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
1624
        }
1625
      }
1626

    
1627
    return mStickers[sticker];
1628
    }
1629

    
1630
///////////////////////////////////////////////////////////////////////////////////////////////////
1631
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1632

    
1633
  public void adjustStickerCoords()
1634
    {
1635

    
1636
    }
1637

    
1638
///////////////////////////////////////////////////////////////////////////////////////////////////
1639

    
1640
  public Static4D[] getQuats()
1641
    {
1642
    if( mObjectQuats==null )
1643
      {
1644
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1645
      }
1646

    
1647
    return mObjectQuats;
1648
    }
1649

    
1650
///////////////////////////////////////////////////////////////////////////////////////////////////
1651

    
1652
  public int[][] getVariantFaceIsOuter()
1653
    {
1654
    return mVariantFaceIsOuter;
1655
    }
1656

    
1657
///////////////////////////////////////////////////////////////////////////////////////////////////
1658

    
1659
  public int getInternalColor()
1660
    {
1661
    return COLOR_INTERNAL;
1662
    }
1663

    
1664
///////////////////////////////////////////////////////////////////////////////////////////////////
1665
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1666

    
1667
  private void createTexture()
1668
    {
1669
    Paint paint = new Paint();
1670
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_4444);
1671
    Canvas canvas = new Canvas(mBitmap);
1672

    
1673
    paint.setAntiAlias(true);
1674
    paint.setTextAlign(Paint.Align.CENTER);
1675
    paint.setStyle(Paint.Style.FILL);
1676

    
1677
    paint.setColor(getInternalColor());
1678
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1679

    
1680
    int texture = 0;
1681
    FactorySticker factory = FactorySticker.getInstance();
1682

    
1683
    for(int row=0; row<mNumTexRows; row++)
1684
      for(int col=0; col<mNumTexCols; col++)
1685
        {
1686
        if( texture>=mNumTextures ) break;
1687
        ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1688
        int color = getColor(texture% mNumFaceColors);
1689
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color, sticker);
1690
        texture++;
1691
        }
1692
    }
1693

    
1694
///////////////////////////////////////////////////////////////////////////////////////////////////
1695

    
1696
  void setTexture()
1697
    {
1698
    if( mBitmap==null ) createTexture();
1699

    
1700
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1701
      {
1702
      int max = DistortedLibrary.getMaxTextureSize();
1703
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1704
      }
1705
    }
1706

    
1707
///////////////////////////////////////////////////////////////////////////////////////////////////
1708

    
1709
  void setObjectRatioNow(float sc, int nodeSize)
1710
    {
1711
    mObjectScreenRatio = sc;
1712
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1713
    mObjectScale.set(scale,scale,scale);
1714

    
1715
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1716
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1717
    }
1718

    
1719
///////////////////////////////////////////////////////////////////////////////////////////////////
1720

    
1721
  void setObjectRatio(float sizeChange, int nodeSize)
1722
    {
1723
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1724

    
1725
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1726
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1727

    
1728
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1729
    }
1730

    
1731
///////////////////////////////////////////////////////////////////////////////////////////////////
1732

    
1733
  void setNodeSize(int nodeSize)
1734
    {
1735
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1736
    }
1737

    
1738
///////////////////////////////////////////////////////////////////////////////////////////////////
1739

    
1740
  public float getRatio()
1741
    {
1742
    return mObjectScreenRatio;
1743
    }
1744

    
1745
///////////////////////////////////////////////////////////////////////////////////////////////////
1746

    
1747
  public float getObjectRatio()
1748
    {
1749
    return mObjectScreenRatio*mInitScreenRatio;
1750
    }
1751

    
1752
///////////////////////////////////////////////////////////////////////////////////////////////////
1753

    
1754
  boolean isSolved()
1755
    {
1756
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1757
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1758

    
1759
    return false;
1760
    }
1761

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

    
1764
  int computeNearestAngle(int basicAngle, float angle, float speed)
1765
    {
1766
    int nearestAngle = 360/basicAngle;
1767
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1768
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1769

    
1770
    if( tmp!=0 ) return nearestAngle*tmp;
1771

    
1772
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1773
    }
1774

    
1775
///////////////////////////////////////////////////////////////////////////////////////////////////
1776
// INTERNAL API - those are called from 'effects' package
1777
///////////////////////////////////////////////////////////////////////////////////////////////////
1778

    
1779
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1780
    {
1781
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1782
    }
1783

    
1784
///////////////////////////////////////////////////////////////////////////////////////////////////
1785

    
1786
  public Static4D getRotationQuat()
1787
    {
1788
    return mQuat;
1789
    }
1790

    
1791
///////////////////////////////////////////////////////////////////////////////////////////////////
1792

    
1793
  public float getSize()
1794
    {
1795
    return mSize;
1796
    }
1797

    
1798
///////////////////////////////////////////////////////////////////////////////////////////////////
1799

    
1800
  public void apply(Effect effect, int position)
1801
    {
1802
    mEffects.apply(effect, position);
1803
    }
1804

    
1805
///////////////////////////////////////////////////////////////////////////////////////////////////
1806

    
1807
  public void remove(long effectID)
1808
    {
1809
    mEffects.abortById(effectID);
1810
    }
1811

    
1812
///////////////////////////////////////////////////////////////////////////////////////////////////
1813

    
1814
  public MeshBase getObjectMesh()
1815
    {
1816
    return mMesh;
1817
    }
1818

    
1819
///////////////////////////////////////////////////////////////////////////////////////////////////
1820

    
1821
  public DistortedEffects getObjectEffects()
1822
    {
1823
    return mEffects;
1824
    }
1825

    
1826
///////////////////////////////////////////////////////////////////////////////////////////////////
1827

    
1828
  public int getCubitType(int cubit)
1829
    {
1830
    return mCubits[cubit].getType();
1831
    }
1832

    
1833
///////////////////////////////////////////////////////////////////////////////////////////////////
1834

    
1835
  public float[] getCubitOffset(int cubit)
1836
    {
1837
    return mCubits[cubit].getOffset();
1838
    }
1839

    
1840
///////////////////////////////////////////////////////////////////////////////////////////////////
1841
// PUBLIC API
1842
///////////////////////////////////////////////////////////////////////////////////////////////////
1843

    
1844
  public int getCubitFaceColorIndex(int cubit, int face)
1845
    {
1846
    Static4D texMap = mMesh.getTextureMap(mNumFaceColors *cubit + face);
1847

    
1848
    int x = (int)(texMap.get0()/texMap.get2());
1849
    int y = (int)(texMap.get1()/texMap.get3());
1850

    
1851
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1852
    }
1853

    
1854
///////////////////////////////////////////////////////////////////////////////////////////////////
1855

    
1856
  public int[] getNumLayers()
1857
    {
1858
    return mNumLayers;
1859
    }
1860

    
1861
///////////////////////////////////////////////////////////////////////////////////////////////////
1862

    
1863
  public synchronized void solve()
1864
    {
1865
    for(int i=0; i<mNumCubits; i++)
1866
      {
1867
      mCubits[i].solve();
1868
      setCubitQuat(i,mCubits[i].computeAssociation(),0);
1869
      }
1870
    }
1871

    
1872
///////////////////////////////////////////////////////////////////////////////////////////////////
1873

    
1874
  public int getCubitQuatIndex(int cubit)
1875
    {
1876
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1877
    }
1878

    
1879
///////////////////////////////////////////////////////////////////////////////////////////////////
1880

    
1881
  public int getCubitRotRow(int cubit, int axis)
1882
    {
1883
    return mCubits[cubit].getRotRow(axis);
1884
    }
1885

    
1886
///////////////////////////////////////////////////////////////////////////////////////////////////
1887

    
1888
  public Bitmap getStickerBitmap()
1889
    {
1890
    return mBitmap;
1891
    }
1892

    
1893
///////////////////////////////////////////////////////////////////////////////////////////////////
1894

    
1895
  public DistortedNode getNode()
1896
    {
1897
    return mNode;
1898
    }
1899

    
1900
///////////////////////////////////////////////////////////////////////////////////////////////////
1901

    
1902
  public int getNumStickerTypes()
1903
    {
1904
    return mNumStickerTypes;
1905
    }
1906

    
1907
///////////////////////////////////////////////////////////////////////////////////////////////////
1908
// this is here only so it can be overridden in TwistyJSON soo that we can get this from JSON.
1909

    
1910
  public int getNumCubitFaces()
1911
    {
1912
    return 0;
1913
    }
1914

    
1915
///////////////////////////////////////////////////////////////////////////////////////////////////
1916

    
1917
  public TouchControl getTouchControl()
1918
    {
1919
    if( mTouchControl==null )
1920
      {
1921
      switch(getTouchControlType())
1922
        {
1923
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
1924
                                   break;
1925
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
1926
                                   break;
1927
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
1928
                                   break;
1929
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
1930
                                   break;
1931
        case TC_CUBOID           : int[] numLayers = getNumLayers();
1932
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1933
                                   break;
1934
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
1935
                                   break;
1936
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
1937
                                   break;
1938
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
1939
                                   break;
1940
        }
1941
      }
1942
    return mTouchControl;
1943
    }
1944

    
1945
///////////////////////////////////////////////////////////////////////////////////////////////////
1946

    
1947
  protected void setReader(JsonReader reader)
1948
    {
1949
    // empty
1950
    }
1951

    
1952
///////////////////////////////////////////////////////////////////////////////////////////////////
1953
  // for JSON only
1954
  public abstract int getTouchControlType();
1955
  public abstract int getTouchControlSplit();
1956
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1957
  public abstract int[][][] getEnabled();
1958
  public abstract float[] getDist3D(int[] numLayers);
1959
  public abstract Static3D[] getFaceAxis();
1960
  public abstract ScrambleState[] getScrambleStates();
1961
  public abstract float[][] getCuts(int[] numLayers);
1962
  public abstract float getStickerRadius();
1963
  public abstract float getStickerStroke();
1964
  public abstract float[][] getStickerAngles();
1965
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1966
  public abstract ObjectShape getObjectShape(int variant);
1967
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
1968
  public abstract int getNumCubitVariants(int[] numLayers);
1969
  public abstract float[][] getCubitPositions(int[] numLayers);
1970
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
1971
  public abstract int getNumFaceColors();
1972
  public abstract float getScreenRatio();
1973
  public abstract int getColor(int face);
1974
  public abstract String getShortName();
1975
  public abstract ObjectSignature getSignature();
1976

    
1977
  // not only for JSON
1978
  public abstract Static3D[] getRotationAxis();
1979
  public abstract int[][] getBasicAngles();
1980
  public abstract int getNumFaces();
1981
  public abstract String getObjectName();
1982
  public abstract String getInventor();
1983
  public abstract int getYearOfInvention();
1984
  public abstract int getComplexity();
1985
  public abstract int getFOV();
1986
  public abstract String[][] getTutorials();
1987
  }
(12-12/13)