Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ ff60e713

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.ObjectStickerOverride;
60
import org.distorted.objectlib.helpers.QuatGroupGenerator;
61
import org.distorted.objectlib.scrambling.ScrambleState;
62
import org.distorted.objectlib.scrambling.ObjectScrambler;
63
import org.distorted.objectlib.json.JsonReader;
64
import org.distorted.objectlib.touchcontrol.*;
65

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

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

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

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

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

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

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

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

    
161
  //////////////////// SOLVED1 ////////////////////////
162

    
163
  private int[] mFaceMap;
164
  private int[][] mScramble;
165
  private int[] mColors;
166

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

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

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

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

    
218
    mNumCubits = mOrigPos.length;
219
    mNumFaceColors = getNumFaceColors();
220
    mNumAxis = mAxis.length;
221
    mBelongs = new boolean[mNumCubits];
222

    
223
    int scramblingType = getScrambleType();
224
    ScrambleState[] states = getScrambleStates();
225
    mScrambler = new ObjectScrambler(scramblingType, mNumAxis,mNumLayers,states);
226

    
227
    boolean bandaged=false;
228

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

    
240
    mRotationAngle= new Dynamic1D();
241
    mRotationAxis = new Static3D(1,0,0);
242
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
243

    
244
    mRotationAngleStatic = new Static1D(0);
245
    mRotationAngleMiddle = new Static1D(0);
246
    mRotationAngleFinal  = new Static1D(0);
247

    
248
    mObjectScale = new Static3D(scale,scale,scale);
249
    setObjectRatioNow(scale,720);
250

    
251
    mEffects = new DistortedEffects();
252
    createQuaternionEffects();
253

    
254
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
255
    MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
256
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
257

    
258
    boolean fromDMESH = (stream!=null && meshState==MESH_NICE);
259
    getQuatsAndShapes(fromDMESH,fromJSON);
260
    createMeshAndCubits(stream,meshState,fromDMESH);
261
    setUpTextures(fromDMESH,fromJSON);
262
    createDataStructuresForSolved();
263

    
264
    mEffects.apply(mRotateEffect);
265
    mEffects.apply(quatEffect);
266
    mEffects.apply(scaleEffect);
267
    mEffects.apply(moveEffect);
268

    
269
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  private void createQuaternionEffects()
275
    {
276
    if( mNumQuats<=ObjectControl.MAX_QUATS )
277
      {
278
      mIsInMixupMode = false;
279

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

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

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  private Static3D getPos(float[] origPos)
309
    {
310
    int len = origPos.length/3;
311
    float sumX = 0.0f;
312
    float sumY = 0.0f;
313
    float sumZ = 0.0f;
314

    
315
    for(int i=0; i<len; i++)
316
      {
317
      sumX += origPos[3*i  ];
318
      sumY += origPos[3*i+1];
319
      sumZ += origPos[3*i+2];
320
      }
321

    
322
    sumX /= len;
323
    sumY /= len;
324
    sumZ /= len;
325

    
326
    return new Static3D(sumX,sumY,sumZ);
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  private void createOuterFaces()
332
    {
333
    for(int v=0; v<mNumCubitVariants; v++)
334
      {
335
      int[][] indices = mShapes[v].getVertIndices();
336
      int faces = indices.length;
337
      mVariantFaceIsOuter[v] = new int[faces];
338
      }
339

    
340
    for( int cubit=0; cubit<mNumCubits; cubit++)
341
      {
342
      int variant = getCubitVariant(cubit,mNumLayers);
343
      int[][] indices = mShapes[variant].getVertIndices();
344
      int numFaces = indices.length;
345

    
346
      for(int face=0; face<numFaces; face++)
347
        if( getCubitFaceColor(cubit,face)>=0 )
348
          {
349
          mVariantFaceIsOuter[variant][face] = 1;
350
          }
351
      }
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
357
    {
358
    mNumCubitVariants = getNumCubitVariants(mNumLayers);
359

    
360
    if( !fromDMESH || !fromJSON )
361
      {
362
      FactoryCubit factory = FactoryCubit.getInstance();
363
      factory.clear();
364

    
365
      mOrigQuat = new Static4D[mNumCubits];
366
      for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
367

    
368
      mShapes = new ObjectShape[mNumCubitVariants];
369
      for(int i=0; i<mNumCubitVariants; i++) mShapes[i] = getObjectShape(i);
370
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
371
      mVariantFaceIsOuter = new int[mNumCubitVariants][];
372

    
373
      if( !fromJSON )
374
        {
375
        mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
376
        createOuterFaces();
377
        }
378

    
379
      if( fromDMESH )
380
        {
381
        for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i], mVariantFaceIsOuter[i]);
382
        }
383
      }
384
    }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  private void createMeshAndCubits(InputStream stream, int meshState, boolean fromDMESH)
389
    {
390
    mCubits = new Cubit[mNumCubits];
391

    
392
    if( fromDMESH )
393
      {
394
      DataInputStream dos = new DataInputStream(stream);
395
      mMesh = new MeshFile(dos);
396

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

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

    
417
      mMesh = new MeshJoined(cubitMesh);
418
      }
419

    
420
    for(int i=0; i<mNumCubits; i++)
421
      {
422
      mCubits[i] = new Cubit(this,mOrigPos[i],mNumAxis,i);
423
      setCubitQuat(i,mCubits[i].computeAssociation(),0);
424
      }
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int meshState, int numComponents)
430
    {
431
    int variant = getCubitVariant(cubit,numLayers);
432

    
433
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
434

    
435
    if( mMeshes[variant]==null )
436
      {
437
      ObjectFaceShape faceShape = getObjectFaceShape(variant);
438
      FactoryCubit factory = FactoryCubit.getInstance();
439
      factory.createNewFaceTransform(mShapes[variant],mVariantFaceIsOuter[variant]);
440
      mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,meshState, numComponents);
441
      }
442

    
443
    MeshBase mesh = mMeshes[variant].copy(true);
444
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
445
    mesh.apply(quat,0xffffffff,0);
446

    
447
    return mesh;
448
    }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
453
    {
454
    mTexture = new DistortedTexture();
455

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

    
471
    mStickerOverrides = getStickerOverrides();
472
    mNumOverrides = mStickerOverrides==null ? 0 : mStickerOverrides.length;
473

    
474
    mNumTextures= mNumFaceColors*mNumStickerTypes + mNumOverrides;
475
    mNumTexCols = NUM_STICKERS_IN_ROW;
476
    mNumTexRows = (mNumTextures+1)/NUM_STICKERS_IN_ROW;
477
    if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
478

    
479
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
480
    setTexture();
481
    }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
  private int getMultQuat(int index1, int index2)
486
    {
487
    if( mQuatMult==null )
488
      {
489
      mQuatMult = new int[mNumQuats][mNumQuats];
490

    
491
      for(int i=0; i<mNumQuats; i++)
492
        for(int j=0; j<mNumQuats; j++) mQuatMult[i][j] = -1;
493
      }
494

    
495
    if( mQuatMult[index1][index2]==-1 )
496
      {
497
      mQuatMult[index1][index2] = mulQuat(index1,index2);
498
      }
499

    
500
    return mQuatMult[index1][index2];
501
    }
502

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

    
505
  public InitData getInitData()
506
    {
507
    return mInitData;
508
    }
509

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

    
512
  public boolean isInIconMode()
513
    {
514
    return mIconMode==MODE_ICON;
515
    }
516

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

    
519
  public int getVariantFaceColor(int variant, int face)
520
    {
521
    return face>=mStickerVariants[variant].length ? -1 : mStickerVariants[variant][face];
522
    }
523

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

    
526
  public boolean shouldResetTextureMaps()
527
    {
528
    return false;
529
    }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
  private void createDataStructuresForSolved()
534
    {
535
    mTmpQuats = new int[mNumQuats];
536
    mSolvedQuats = getSolvedQuats();
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540
// This is used to build internal data structures for the generic 'isSolved()'
541
//
542
// if this is an internal cubit (all faces black): return -1
543
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
544
// Color index, i.e. the index into the 'FACE_COLORS' table.
545
// else (edge or corner cubit, more than one non-black face): return -2.
546

    
547
  protected int retCubitSolvedStatus(int cubit)
548
    {
549
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
550
    int variant = getCubitVariant(cubit,mNumLayers);
551

    
552
    for(int face=0; face<mNumCubitFaces; face++)
553
      {
554
      varColor = getVariantFaceColor(variant,face);
555
      int numFaces = mCubitFaceColors[cubit].length;
556
      cubColor = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
557

    
558
      if( varColor>=0 && cubColor>=0 )
559
        {
560
        numNonBlack++;
561
        nonBlackIndex = cubColor;
562
        }
563
      }
564

    
565
    if( numNonBlack==0 ) return -1;
566
    if( numNonBlack>=2 ) return -2;
567

    
568
    return nonBlackIndex;
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
574
    {
575
    final float MAXERR = 0.05f;
576
    int numAxis = dist.length;
577

    
578
    for(int i=0; i<numAxis; i++)
579
      {
580
      Static3D ax = faceAxis[i];
581
      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
582
      if( len>mSize*dist[i]+MAXERR ) return true;
583
      }
584

    
585
    return false;
586
    }
587

    
588
///////////////////////////////////////////////////////////////////////////////////////////////////
589

    
590
  private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
591
    {
592
    float[][] vertices = mShapes[variant].getVertices();
593
    Static3D[] axis = getFaceAxis();
594
    float[] dist3D = getDist3D(mNumLayers);
595

    
596
    for( float[] vertex : vertices)
597
      {
598
      float x = vertex[0];
599
      float y = vertex[1];
600
      float z = vertex[2];
601

    
602
      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
603

    
604
      float mx = tmp[0] + px;
605
      float my = tmp[1] + py;
606
      float mz = tmp[2] + pz;
607

    
608
      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
609
      }
610

    
611
    return true;
612
    }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615

    
616
  private float computeAvg(float[] pos, int offset)
617
    {
618
    int len = pos.length/3;
619
    float ret=0.0f;
620
    for(int i=0; i<len; i++) ret += pos[3*i+offset];
621
    ret /= len;
622

    
623
    return ret;
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627

    
628
  protected void displayCubitQuats()
629
    {
630
    StringBuilder builder = new StringBuilder();
631
    float[] tmp = new float[4];
632
    float ERR = 0.01f;
633

    
634
    for(int cubit=0; cubit<mNumCubits; cubit++)
635
      {
636
      builder.append(cubit);
637
      builder.append(" : ");
638

    
639
      int refCubit,variant = getCubitVariant(cubit,mNumLayers);
640

    
641
      for(refCubit=0; refCubit<mNumCubits; refCubit++)
642
        {
643
        if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
644
        }
645

    
646
      float[] curpos = mOrigPos[cubit];
647
      float[] refpos = mOrigPos[refCubit];
648
      float refX = computeAvg(refpos,0);
649
      float refY = computeAvg(refpos,1);
650
      float refZ = computeAvg(refpos,2);
651
      float curX = computeAvg(curpos,0);
652
      float curY = computeAvg(curpos,1);
653
      float curZ = computeAvg(curpos,2);
654

    
655
      for(int quat=0; quat<mNumQuats; quat++)
656
        {
657
        QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
658

    
659
        float dx = tmp[0]-curX;
660
        float dy = tmp[1]-curY;
661
        float dz = tmp[2]-curZ;
662

    
663
        if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
664
          {
665
          if( doesNotStickOut(variant,curX,curY,curZ,tmp,mObjectQuats[quat]) )
666
            {
667
            builder.append(quat);
668
            builder.append(',');
669
            }
670
          else
671
            {
672
            android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
673
            }
674
          }
675
        }
676

    
677
      builder.append('\n');
678
      }
679

    
680
    android.util.Log.e("D", "cubitQuats: \n"+builder.toString() );
681
    }
682

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

    
685
  protected int[] buildSolvedQuats(Static3D faceAx)
686
    {
687
    final float MAXD = 0.0001f;
688
    float x = faceAx.get0();
689
    float y = faceAx.get1();
690
    float z = faceAx.get2();
691
    float a,dx,dy,dz,qx,qy,qz;
692
    Static4D quat;
693
    int place = 0;
694

    
695
    for(int q=1; q<mNumQuats; q++)
696
      {
697
      quat = mObjectQuats[q];
698
      qx = quat.get0();
699
      qy = quat.get1();
700
      qz = quat.get2();
701

    
702
           if( x!=0.0f ) { a = qx/x; }
703
      else if( y!=0.0f ) { a = qy/y; }
704
      else               { a = qz/z; }
705

    
706
      dx = a*x-qx;
707
      dy = a*y-qy;
708
      dz = a*z-qz;
709

    
710
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
711
        {
712
        mTmpQuats[place++] = q;
713
        }
714
      }
715

    
716
    if( place!=0 )
717
      {
718
      int[] ret = new int[place];
719
      System.arraycopy(mTmpQuats,0,ret,0,place);
720
      return ret;
721
      }
722

    
723
    return null;
724
    }
725

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

    
728
  public int getCubitRotationType(int cubit)
729
    {
730
    return Cubit.TYPE_NORMAL;
731
    }
732

    
733
///////////////////////////////////////////////////////////////////////////////////////////////////
734

    
735
  float[] getTrackingPoint(int cubitIndex, int cubitType)
736
    {
737
    if( cubitType!=Cubit.TYPE_NORMAL )
738
      {
739
      int variant = getCubitVariant(cubitIndex,mNumLayers);
740

    
741
      // object must have been created from JSON
742
      if( mVariantFaceIsOuter==null || mVariantFaceIsOuter[variant]==null )
743
        {
744
        mVariantFaceIsOuter = getVariantFaceIsOuter();
745
        }
746
      if( mShapes==null )
747
        {
748
        mShapes = new ObjectShape[mNumCubitVariants];
749
        }
750
      if( mShapes[variant]==null )
751
        {
752
        mShapes[variant] = getObjectShape(variant);
753
        }
754
      if( mOrigQuat==null )
755
        {
756
        mOrigQuat = new Static4D[mNumCubits];
757
        }
758
      if( mOrigQuat[cubitIndex]==null )
759
        {
760
        mOrigQuat[cubitIndex] = getCubitQuats(cubitIndex,mNumLayers);
761
        }
762

    
763
      int[][] indices = mShapes[variant].getVertIndices();
764
      int outer=-1, faces = indices.length;
765

    
766
      for(int i=0; i<faces; i++)
767
        {
768
        if( mVariantFaceIsOuter[variant][i]==1 )
769
          {
770
          outer=i;
771
          break;
772
          }
773
        }
774

    
775
      if( outer>=0 )
776
        {
777
        int vertIndex = indices[outer][0];
778
        float[] vertices = mShapes[variant].getVertices()[vertIndex];
779
        float[] ret = new float[3];
780
        float[] curpos = mOrigPos[cubitIndex];
781
        Static4D quat = mOrigQuat[cubitIndex];
782
        QuatHelper.rotateVectorByQuat(mTmp, vertices[0], vertices[1], vertices[2], 1, quat);
783

    
784
        ret[0] = mTmp[0]+computeAvg(curpos,0);
785
        ret[1] = mTmp[1]+computeAvg(curpos,1);
786
        ret[2] = mTmp[2]+computeAvg(curpos,2);
787

    
788
        return ret;
789
        }
790
      else
791
        {
792
        android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
793
        }
794
      }
795

    
796
    return null;
797
    }
798

    
799
///////////////////////////////////////////////////////////////////////////////////////////////////
800

    
801
  public int computeCurrentPuzzleFace(int type, float[] vertex)
802
    {
803
    if( type!=Cubit.TYPE_NORMAL )
804
      {
805
      Static3D[] axis = getFaceAxis();
806
      float[] dist3D = getDist3D(mNumLayers);
807
      final float MAXERR = 0.98f;
808
      int numAxis = axis.length;
809
      float x = vertex[0];
810
      float y = vertex[1];
811
      float z = vertex[2];
812

    
813
      for(int i=0; i<numAxis; i++)
814
        {
815
        Static3D ax = axis[i];
816
        float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
817
        if( len>mSize*dist3D[i]*MAXERR ) return i;
818
        }
819
      }
820

    
821
    return -1;
822
    }
823

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

    
826
  public float[] getCubitRowOffset(int cubitIndex)
827
    {
828
    return null;
829
    }
830

    
831
///////////////////////////////////////////////////////////////////////////////////////////////////
832

    
833
  void setRotationRowOffset(int puzzleFace, float[] offset)
834
    {
835
    mRowOffsets[puzzleFace][0] = offset[0];
836
    mRowOffsets[puzzleFace][1] = offset[1];
837
    mRowOffsets[puzzleFace][2] = offset[2];
838
    }
839

    
840
///////////////////////////////////////////////////////////////////////////////////////////////////
841

    
842
  public int[][] getSolvedQuats()
843
    {
844
    int[] groups = new int[mNumCubits];
845
    int numGroups = 1;
846
    int numFirst  = 0;
847

    
848
    for(int cubit=0; cubit<mNumCubits; cubit++)
849
      {
850
      groups[cubit] = retCubitSolvedStatus(cubit);
851
      if( groups[cubit]>=0 ) numGroups++;
852
      else                   numFirst++;
853
      }
854

    
855
    int firstIndex = 1;
856
    int groupIndex = 1;
857
    int[][] solvedQuats = new int[numGroups][];
858
    solvedQuats[0] = new int[1+numFirst];
859
    solvedQuats[0][0] = numFirst;
860
    Static3D[] axis = getFaceAxis();
861

    
862
    for(int cubit=0; cubit<mNumCubits; cubit++)
863
      {
864
      int group = groups[cubit];
865

    
866
      if( group<0 )
867
        {
868
        solvedQuats[0][firstIndex] = cubit;
869
        firstIndex++;
870
        }
871
      else
872
        {
873
        int[] quats = buildSolvedQuats(axis[group]);
874
        int len = quats==null ? 0 : quats.length;
875
        solvedQuats[groupIndex] = new int[2+len];
876
        solvedQuats[groupIndex][0] = 1;
877
        solvedQuats[groupIndex][1] = cubit;
878
        for(int i=0; i<len; i++) solvedQuats[groupIndex][i+2] = quats[i];
879
        groupIndex++;
880
        }
881
      }
882
/*
883
    String dbg = "SOLVED GROUPS:\n";
884

    
885
    for(int g=0; g<numGroups; g++)
886
      {
887
      int len = solvedQuats[g].length;
888
      for(int i=0; i<len; i++) dbg += (" "+solvedQuats[g][i]);
889
      dbg+="\n";
890
      }
891

    
892
    android.util.Log.e("D", dbg);
893
*/
894
    return solvedQuats;
895
    }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898

    
899
  public int getSolvedFunctionIndex()
900
    {
901
    return 0;
902
    }
903

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

    
909
  private boolean isSolvedCentersOnly()
910
    {
911
    int numGroups = mSolvedQuats.length;
912

    
913
    for(int group=1; group<numGroups; group++)
914
      {
915
      int numEntries= mSolvedQuats[group].length;
916
      int numCubits = mSolvedQuats[group][0];
917
      int firstCubit= mSolvedQuats[group][1];
918
      int firstQuat = mCubits[firstCubit].mQuatIndex;
919

    
920
      for(int cubit=2; cubit<=numCubits; cubit++)
921
        {
922
        int currCubit= mSolvedQuats[group][cubit];
923
        int currQuat = mCubits[currCubit].mQuatIndex;
924
        boolean isGood= (firstQuat==currQuat);
925

    
926
        for(int q=numCubits+1; !isGood && q<numEntries; q++)
927
          {
928
          int quat = mSolvedQuats[group][q];
929
          if( firstQuat == getMultQuat(currQuat,quat) ) isGood = true;
930
          }
931

    
932
        if( !isGood ) return false;
933
        }
934
      }
935

    
936
    return true;
937
    }
938

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

    
941
  private boolean isSolved0()
942
    {
943
    if( mSolvedQuats[0][0]==0 ) return isSolvedCentersOnly();
944

    
945
    for( int[] solvedQuat : mSolvedQuats )
946
      {
947
      int numCubits = solvedQuat[0];
948
      int firstCubit= solvedQuat[1];
949
      int quat = mCubits[firstCubit].mQuatIndex;
950

    
951
      for( int cubit=2; cubit<=numCubits; cubit++ )
952
        {
953
        int c = solvedQuat[cubit];
954
        if( quat != mCubits[c].mQuatIndex ) return false;
955
        }
956
      }
957

    
958
    int cubit= mSolvedQuats[0][1];
959
    int quat0= mCubits[cubit].mQuatIndex;
960
    int numGroups = mSolvedQuats.length;
961

    
962
    for(int group=1; group<numGroups; group++)
963
      {
964
      int firstCubit= mSolvedQuats[group][1];
965
      int currQuat  = mCubits[firstCubit].mQuatIndex;
966

    
967
      if( quat0==currQuat ) continue;
968

    
969
      boolean isGood= false;
970
      int numEntries= mSolvedQuats[group].length;
971
      int numCubits = mSolvedQuats[group][0];
972

    
973
      for(int q=numCubits+1; q<numEntries; q++)
974
        {
975
        int quat = mSolvedQuats[group][q];
976

    
977
        if( quat0 == getMultQuat(currQuat,quat) )
978
          {
979
          isGood = true;
980
          break;
981
          }
982
        }
983

    
984
      if( !isGood ) return false;
985
      }
986

    
987
    return true;
988
    }
989

    
990
///////////////////////////////////////////////////////////////////////////////////////////////////
991

    
992
  private int computeScramble(int quatNum, int centerNum)
993
    {
994
    float MAXDIFF = 0.01f;
995
    float[] center= mOrigPos[centerNum];
996
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
997
    Static4D result = QuatHelper.rotateVectorByQuat(sc,mObjectQuats[quatNum]);
998

    
999
    float x = result.get0();
1000
    float y = result.get1();
1001
    float z = result.get2();
1002

    
1003
    for(int c=0; c<mNumCubits; c++)
1004
      {
1005
      float[] cent = mOrigPos[c];
1006

    
1007
      float qx = cent[0] - x;
1008
      float qy = cent[1] - y;
1009
      float qz = cent[2] - z;
1010

    
1011
      if( qx>-MAXDIFF && qx<MAXDIFF &&
1012
          qy>-MAXDIFF && qy<MAXDIFF &&
1013
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
1014
      }
1015

    
1016
    return -1;
1017
    }
1018

    
1019
///////////////////////////////////////////////////////////////////////////////////////////////////
1020
// Dino4 uses this. It is solved if and only if groups of cubits
1021
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
1022
// or
1023
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
1024
// are all the same color.
1025

    
1026
  private boolean isSolved1()
1027
    {
1028
    if( mScramble==null )
1029
      {
1030
      mScramble = new int[mNumQuats][mNumCubits];
1031
      mColors   = new int[mNumCubits];
1032

    
1033
      for(int q=0; q<mNumQuats; q++)
1034
        for(int c=0; c<mNumCubits; c++) mScramble[q][c] = computeScramble(q,c);
1035
      }
1036

    
1037
    if( mFaceMap==null )
1038
      {
1039
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
1040
      }
1041

    
1042
    for(int c=0; c<mNumCubits; c++)
1043
      {
1044
      int index = mScramble[mCubits[c].mQuatIndex][c];
1045
      mColors[index] = mFaceMap[c];
1046
      }
1047

    
1048
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
1049
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
1050
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
1051

    
1052
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
1053
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
1054
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
1055

    
1056
    return false;
1057
    }
1058

    
1059
///////////////////////////////////////////////////////////////////////////////////////////////////
1060

    
1061
  int computeRow(float[] pos, int axisIndex, int cubitType, int puzzleFace)
1062
    {
1063
    int ret=0;
1064
    int len = pos.length / 3;
1065
    Static3D axis = mAxis[axisIndex];
1066
    float axisX = axis.get0();
1067
    float axisY = axis.get1();
1068
    float axisZ = axis.get2();
1069
    float casted, xoff=0, yoff=0, zoff=0;
1070

    
1071
    if( cubitType!=Cubit.TYPE_NORMAL )
1072
      {
1073
      xoff = mRowOffsets[puzzleFace][0];
1074
      yoff = mRowOffsets[puzzleFace][1];
1075
      zoff = mRowOffsets[puzzleFace][2];
1076
      }
1077

    
1078
    for(int i=0; i<len; i++)
1079
      {
1080
      casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
1081
      ret |= computeSingleRow(axisIndex,casted);
1082
      }
1083

    
1084
    return ret;
1085
    }
1086

    
1087
///////////////////////////////////////////////////////////////////////////////////////////////////
1088

    
1089
  private int computeSingleRow(int axisIndex,float casted)
1090
    {
1091
    int num = mNumCuts[axisIndex];
1092

    
1093
    for(int i=0; i<num; i++)
1094
      {
1095
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
1096
      }
1097

    
1098
    return (1<<num);
1099
    }
1100

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

    
1103
  private boolean wasRotateApplied()
1104
    {
1105
    return mEffects.exists(mRotateEffect.getID());
1106
    }
1107

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

    
1110
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
1111
    {
1112
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
1113
    }
1114

    
1115
///////////////////////////////////////////////////////////////////////////////////////////////////
1116
// note the minus in front of the sin() - we rotate counterclockwise
1117
// when looking towards the direction where the axis increases in values.
1118

    
1119
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
1120
    {
1121
    Static3D axis = mAxis[axisIndex];
1122

    
1123
    while( angleInDegrees<0 ) angleInDegrees += 360;
1124
    angleInDegrees %= 360;
1125
    
1126
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
1127
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
1128

    
1129
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
1130
    }
1131

    
1132
///////////////////////////////////////////////////////////////////////////////////////////////////
1133

    
1134
  private synchronized void setupPosition(int[][] moves)
1135
    {
1136
    if( moves!=null )
1137
      {
1138
      Static4D quat;
1139
      int index, axis, row, rowBitmap, angle;
1140

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

    
1153
        for(int j=0; j<mNumCubits; j++)
1154
          {
1155
          mBelongs[j] = belongsToRotation(j,axis,rowBitmap);
1156
          if( mBelongs[j] ) mCubits[j].rotateCubit(quat);
1157
          }
1158

    
1159
        recomputeFaceOffsets();
1160

    
1161
        for(int j=0; j<mNumCubits; j++)
1162
          {
1163
          if( mBelongs[j] )
1164
            {
1165
            index = mCubits[j].postRotateCubit(quat);
1166
            setCubitQuat(j,mCubits[j].computeAssociation(),index);
1167
            }
1168
          else if( mCubits[j].getType()==Cubit.TYPE_FOLLOWER )
1169
            {
1170
            mCubits[j].computeRotationRow();
1171
            setCubitQuat(j,mCubits[j].computeAssociation(),mCubits[j].mQuatIndex);
1172
            }
1173
          }
1174
        }
1175
      }
1176
    }
1177

    
1178
///////////////////////////////////////////////////////////////////////////////////////////////////
1179

    
1180
  public int getScrambleType()
1181
    {
1182
    return 0;
1183
    }
1184

    
1185
///////////////////////////////////////////////////////////////////////////////////////////////////
1186

    
1187
  int computeBitmapFromRow(int rowBitmap, int axis)
1188
    {
1189
    if( mIsBandaged )
1190
      {
1191
      int bitmap, initBitmap=0;
1192

    
1193
      while( initBitmap!=rowBitmap )
1194
        {
1195
        initBitmap = rowBitmap;
1196

    
1197
        for(int cubit=0; cubit<mNumCubits; cubit++)
1198
          {
1199
          bitmap = mCubits[cubit].getRotRow(axis);
1200
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
1201
          }
1202
        }
1203
      }
1204

    
1205
    return rowBitmap;
1206
    }
1207

    
1208
///////////////////////////////////////////////////////////////////////////////////////////////////
1209

    
1210
  private int computeRowFromBitmap(int rowBitmap)
1211
    {
1212
    int index = 0;
1213

    
1214
    while(index<32)
1215
      {
1216
      if( (rowBitmap&0x1) != 0 ) return index;
1217
      rowBitmap>>=1;
1218
      index++;
1219
      }
1220
    return 0;
1221
    }
1222

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

    
1227
  void clampPos(float[] pos, int offset)
1228
    {
1229
    float currError, minError = Float.MAX_VALUE;
1230
    int minErrorIndex1 = -1;
1231
    int minErrorIndex2 = -1;
1232

    
1233
    float x = pos[offset  ];
1234
    float y = pos[offset+1];
1235
    float z = pos[offset+2];
1236

    
1237
    float xo,yo,zo;
1238

    
1239
    for(int i=0; i<mNumCubits; i++)
1240
      {
1241
      int len = mOrigPos[i].length / 3;
1242

    
1243
      for(int j=0; j<len; j++)
1244
        {
1245
        xo = mOrigPos[i][3*j  ];
1246
        yo = mOrigPos[i][3*j+1];
1247
        zo = mOrigPos[i][3*j+2];
1248

    
1249
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1250

    
1251
        if( currError<minError )
1252
          {
1253
          minError = currError;
1254
          minErrorIndex1 = i;
1255
          minErrorIndex2 = j;
1256
          }
1257
        }
1258
      }
1259

    
1260
    if( minError< 0.1f ) // TODO: 0.1 ?
1261
      {
1262
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1263
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1264
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1265
      }
1266
    }
1267

    
1268
///////////////////////////////////////////////////////////////////////////////////////////////////
1269
// remember about the double cover or unit quaternions!
1270

    
1271
  int mulQuat(int q1, int q2)
1272
    {
1273
    Static4D result = QuatHelper.quatMultiply(mObjectQuats[q1],mObjectQuats[q2]);
1274

    
1275
    float rX = result.get0();
1276
    float rY = result.get1();
1277
    float rZ = result.get2();
1278
    float rW = result.get3();
1279

    
1280
    final float MAX_ERROR = 0.1f;
1281
    float dX,dY,dZ,dW;
1282

    
1283
    for(int i=0; i<mNumQuats; i++)
1284
      {
1285
      dX = mObjectQuats[i].get0() - rX;
1286
      dY = mObjectQuats[i].get1() - rY;
1287
      dZ = mObjectQuats[i].get2() - rZ;
1288
      dW = mObjectQuats[i].get3() - rW;
1289

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

    
1295
      dX = mObjectQuats[i].get0() + rX;
1296
      dY = mObjectQuats[i].get1() + rY;
1297
      dZ = mObjectQuats[i].get2() + rZ;
1298
      dW = mObjectQuats[i].get3() + rW;
1299

    
1300
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
1301
          dY<MAX_ERROR && dY>-MAX_ERROR &&
1302
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
1303
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
1304
      }
1305

    
1306
    return -1;
1307
    }
1308

    
1309
///////////////////////////////////////////////////////////////////////////////////////////////////
1310

    
1311
  private float getAngle()
1312
    {
1313
    int pointNum = mRotationAngle.getNumPoints();
1314

    
1315
    if( pointNum>=1 )
1316
      {
1317
      return mRotationAngle.getPoint(pointNum-1).get0();
1318
      }
1319
    else
1320
      {
1321
      mInterface.reportProblem("points in RotationAngle: "+pointNum, false);
1322
      return 0;
1323
      }
1324
    }
1325

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

    
1328
  void setLibInterface(ObjectLibInterface inter)
1329
    {
1330
    mInterface = inter;
1331
    }
1332

    
1333
///////////////////////////////////////////////////////////////////////////////////////////////////
1334

    
1335
  void initializeObject(int[][] moves)
1336
    {
1337
    solve();
1338
    setupPosition(moves);
1339
    }
1340

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

    
1343
  synchronized void removeRotationNow()
1344
    {
1345
    float angle = getAngle();
1346
    double nearestAngleInRadians = angle*Math.PI/180;
1347
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1348
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1349
    float axisX = mAxis[mCurrentRotAxis].get0();
1350
    float axisY = mAxis[mCurrentRotAxis].get1();
1351
    float axisZ = mAxis[mCurrentRotAxis].get2();
1352
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1353

    
1354
    mRotationAngle.removeAll();
1355
    mRotationAngleStatic.set0(0);
1356

    
1357
    for(int i=0; i<mNumCubits; i++)
1358
      {
1359
      mBelongs[i] = belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap);
1360
      if( mBelongs[i] ) mCubits[i].rotateCubit(quat);
1361
      }
1362

    
1363
    recomputeFaceOffsets();
1364

    
1365
    for(int i=0; i<mNumCubits; i++)
1366
      {
1367
      if( mBelongs[i] )
1368
        {
1369
        int index = mCubits[i].postRotateCubit(quat);
1370
        setCubitQuat(i,mCubits[i].computeAssociation(),index);
1371
        }
1372
      else if( mCubits[i].getType()==Cubit.TYPE_FOLLOWER )
1373
        {
1374
        mCubits[i].computeRotationRow();
1375
        setCubitQuat(i,mCubits[i].computeAssociation(),mCubits[i].mQuatIndex);
1376
        }
1377
      }
1378
    }
1379

    
1380
///////////////////////////////////////////////////////////////////////////////////////////////////
1381

    
1382
  private void recomputeFaceOffsets()
1383
    {
1384
    for(int i=0; i<mNumPuzzleFaces; i++)
1385
      {
1386
      mRowOffsets[i][0] =0;
1387
      mRowOffsets[i][1] =0;
1388
      mRowOffsets[i][2] =0;
1389
      }
1390

    
1391
    for(int i=0; i<mNumCubits; i++)
1392
      if( mCubits[i].getType()==Cubit.TYPE_DECIDER )
1393
        {
1394
        float[] offset = mCubits[i].getOffset();
1395
        int face = mCubits[i].getPuzzleFace();
1396
        mRowOffsets[face][0] = offset[0];
1397
        mRowOffsets[face][1] = offset[1];
1398
        mRowOffsets[face][2] = offset[2];
1399
        }
1400
    }
1401

    
1402
///////////////////////////////////////////////////////////////////////////////////////////////////
1403

    
1404
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1405
    {
1406
    if( wasRotateApplied() )
1407
      {
1408
      float angle = getAngle();
1409
      mRotationAngleStatic.set0(angle);
1410
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1411
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1412

    
1413
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1414
      mRotationAngle.resetToBeginning();
1415
      mRotationAngle.removeAll();
1416
      mRotationAngle.add(mRotationAngleStatic);
1417
      mRotationAngle.add(mRotationAngleMiddle);
1418
      mRotationAngle.add(mRotationAngleFinal);
1419
      mRotateEffect.notifyWhenFinished(listener);
1420

    
1421
      return mRotateEffect.getID();
1422
      }
1423

    
1424
    return 0;
1425
    }
1426

    
1427
///////////////////////////////////////////////////////////////////////////////////////////////////
1428

    
1429
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1430
    {
1431
    if( wasRotateApplied() )
1432
      {
1433
      mCurrentRotAxis = axis;
1434
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1435

    
1436
      mRotationAngleStatic.set0(0.0f);
1437
      mRotationAxis.set( mAxis[axis] );
1438
      mRotationAngle.setDuration(durationMillis);
1439
      mRotationAngle.resetToBeginning();
1440
      mRotationAngle.add(new Static1D(0));
1441
      mRotationAngle.add(new Static1D(angle));
1442
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1443
      mRotateEffect.notifyWhenFinished(listener);
1444

    
1445
      return mRotateEffect.getID();
1446
      }
1447

    
1448
    return 0;
1449
    }
1450

    
1451
///////////////////////////////////////////////////////////////////////////////////////////////////
1452

    
1453
  void continueRotation(float angleInDegrees)
1454
    {
1455
    mRotationAngleStatic.set0(angleInDegrees);
1456
    }
1457

    
1458
///////////////////////////////////////////////////////////////////////////////////////////////////
1459

    
1460
  synchronized void beginNewRotation(int axis, int row )
1461
    {
1462
    if( axis<0 || axis>=mNumAxis )
1463
      {
1464
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1465
      return;
1466
      }
1467
    if( row<0 || row>=mNumLayers[axis] )
1468
      {
1469
      android.util.Log.e("object", "invalid rotation row: "+row);
1470
      return;
1471
      }
1472

    
1473
    mCurrentRotAxis = axis;
1474
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1475
    mRotationAngleStatic.set0(0.0f);
1476
    mRotationAxis.set( mAxis[axis] );
1477
    mRotationAngle.add(mRotationAngleStatic);
1478
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1479
    }
1480

    
1481
///////////////////////////////////////////////////////////////////////////////////////////////////
1482

    
1483
  void setTextureMap(int cubit, int face, int newColor)
1484
    {
1485
    final float ratioW = 1.0f/mNumTexCols;
1486
    final float ratioH = 1.0f/mNumTexRows;
1487
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1488
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1489
    int col = newColor%mNumTexCols;
1490

    
1491
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1492
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1493
    }
1494

    
1495
///////////////////////////////////////////////////////////////////////////////////////////////////
1496

    
1497
  private int getCubitFaceColor(int cubit, int face)
1498
    {
1499
    int puzzleFace = getCubitFaceMap(cubit,face);
1500
    if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
1501
    return puzzleFace;
1502
    }
1503

    
1504
///////////////////////////////////////////////////////////////////////////////////////////////////
1505

    
1506
  public int getCubitFaceMap(int cubit, int face)
1507
    {
1508
    int numFaces = mCubitFaceColors[cubit].length;
1509
    int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
1510
    return puzzleFace<0 ? -1 : puzzleFace;
1511
    }
1512

    
1513
///////////////////////////////////////////////////////////////////////////////////////////////////
1514

    
1515
  void resetAllTextureMaps()
1516
    {
1517
    final float ratioW = 1.0f/mNumTexCols;
1518
    final float ratioH = 1.0f/mNumTexRows;
1519
    int cubColor, varColor, color, variant, row, col;
1520

    
1521
    for(int cubit=0; cubit<mNumCubits; cubit++)
1522
      {
1523
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1524
      variant = getCubitVariant(cubit,mNumLayers);
1525

    
1526
      for(int face=0; face<mNumCubitFaces; face++)
1527
        {
1528
        cubColor = getCubitFaceColor(cubit,face);
1529
        varColor = getVariantFaceColor(variant,face);
1530
        color    = cubColor<0 || varColor<0 ? mNumTextures-mNumOverrides : varColor*mNumFaceColors + cubColor;
1531
        row      = (mNumTexRows-1) - color/mNumTexCols;
1532
        col      = color%mNumTexCols;
1533

    
1534
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1535
        }
1536

    
1537
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1538
      }
1539
    }
1540

    
1541
///////////////////////////////////////////////////////////////////////////////////////////////////
1542

    
1543
  void releaseResources()
1544
    {
1545
    mTexture.markForDeletion();
1546
    mMesh.markForDeletion();
1547
    mEffects.markForDeletion();
1548

    
1549
    for(int j=0; j<mNumCubits; j++)
1550
      {
1551
      mCubits[j].releaseResources();
1552
      }
1553
    }
1554

    
1555
///////////////////////////////////////////////////////////////////////////////////////////////////
1556

    
1557
  private void setCubitQuat(int cubit, int andAssociation, int equAssociation)
1558
    {
1559
    if( !mIsInMixupMode )
1560
      {
1561
      mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
1562
      }
1563
    else
1564
      {
1565
      mMesh.setEffectAssociation(cubit,andAssociation,cubit);
1566
      Static4D tmp = mObjectQuats[equAssociation];
1567
      mMixupModeQuats[cubit].set(tmp);
1568
      }
1569
    }
1570

    
1571
///////////////////////////////////////////////////////////////////////////////////////////////////
1572

    
1573
  synchronized void restorePreferences(SharedPreferences preferences)
1574
    {
1575
    boolean error = false;
1576
    String key = getShortName();
1577

    
1578
    for(int i=0; i<mNumCubits; i++)
1579
      {
1580
      mQuatDebug[i] = mCubits[i].restorePreferences(key,preferences);
1581

    
1582
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1583
        {
1584
        mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]]);
1585
        }
1586
      else
1587
        {
1588
        error = true;
1589
        }
1590
      }
1591

    
1592
    for(int i=0; i<mNumCubits; i++)
1593
      {
1594
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1595
        {
1596
        mCubits[i].computeRotationRow();
1597
        setCubitQuat(i,mCubits[i].computeAssociation(),mQuatDebug[i]);
1598
        }
1599
      else
1600
        {
1601
        error = true;
1602
        }
1603
      }
1604

    
1605
    if( error )
1606
      {
1607
      for(int i=0; i<mNumCubits; i++)
1608
        {
1609
        mCubits[i].solve();
1610
        setCubitQuat(i,mCubits[i].computeAssociation(),0);
1611
        }
1612
      }
1613
    }
1614

    
1615
///////////////////////////////////////////////////////////////////////////////////////////////////
1616

    
1617
  void savePreferences(SharedPreferences.Editor editor)
1618
    {
1619
    String key = getShortName();
1620
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,editor);
1621
    }
1622

    
1623
///////////////////////////////////////////////////////////////////////////////////////////////////
1624

    
1625
  public void removePreferences(SharedPreferences.Editor editor)
1626
    {
1627
    String key = getShortName();
1628
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,editor);
1629
    }
1630

    
1631
///////////////////////////////////////////////////////////////////////////////////////////////////
1632

    
1633
  private float computeRadiusCorrection(float[] sticker, int curr, int len)
1634
    {
1635
    final float A = 0.8f;  // 0<A<1
1636

    
1637
    int prev = curr>0 ? curr-1 : len-1;
1638
    int next = curr<len-1 ? curr+1 : 0;
1639

    
1640
    float v1x = sticker[2*prev  ]-sticker[2*curr  ];
1641
    float v1y = sticker[2*prev+1]-sticker[2*curr+1];
1642
    float v2x = sticker[2*next  ]-sticker[2*curr  ];
1643
    float v2y = sticker[2*next+1]-sticker[2*curr+1];
1644

    
1645
    float len1= v1x*v1x+v1y*v1y;
1646
    float len2= v2x*v2x+v2y*v2y;
1647

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

    
1650
    return 1-A*cos;
1651
    }
1652

    
1653
///////////////////////////////////////////////////////////////////////////////////////////////////
1654

    
1655
  public ObjectSticker retSticker(int sticker)
1656
    {
1657
    if( mStickers==null )
1658
      {
1659
      float rad = getStickerRadius();
1660
      float str = getStickerStroke();
1661
      float[][] angles = getStickerAngles();
1662
      int numStickers = mStickerCoords.length;
1663
      mStickers = new ObjectSticker[numStickers];
1664

    
1665
      for(int s=0; s<numStickers; s++)
1666
        {
1667
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1668
        float radius = rad / scale;
1669
        float stroke = str / scale;
1670
        int len = mStickerCoords[s].length/2;
1671
        float[] radii = new float[len];
1672
        for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
1673
        mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
1674
        }
1675
      }
1676

    
1677
    return mStickers[sticker];
1678
    }
1679

    
1680
///////////////////////////////////////////////////////////////////////////////////////////////////
1681
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1682

    
1683
  public void adjustStickerCoords()
1684
    {
1685

    
1686
    }
1687

    
1688
///////////////////////////////////////////////////////////////////////////////////////////////////
1689

    
1690
  public Static4D[] getQuats()
1691
    {
1692
    if( mObjectQuats==null )
1693
      {
1694
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1695
      }
1696

    
1697
    return mObjectQuats;
1698
    }
1699

    
1700
///////////////////////////////////////////////////////////////////////////////////////////////////
1701

    
1702
  public int[][] getVariantFaceIsOuter()
1703
    {
1704
    return mVariantFaceIsOuter;
1705
    }
1706

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

    
1709
  public int getInternalColor()
1710
    {
1711
    return COLOR_INTERNAL;
1712
    }
1713

    
1714
///////////////////////////////////////////////////////////////////////////////////////////////////
1715
// the getFaceColors + final INTERNAL_COLOR in a grid (so that we do not exceed the maximum texture size)
1716

    
1717
  private void createTexture()
1718
    {
1719
    Paint paint = new Paint();
1720
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_4444);
1721
    Canvas canvas = new Canvas(mBitmap);
1722

    
1723
    paint.setAntiAlias(true);
1724
    paint.setTextAlign(Paint.Align.CENTER);
1725
    paint.setStyle(Paint.Style.FILL);
1726
    paint.setColor(getInternalColor());
1727
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1728

    
1729
    int texture = 0;
1730
    FactorySticker factory = FactorySticker.getInstance();
1731

    
1732
    for(int row=0; row<mNumTexRows; row++)
1733
      for(int col=0; col<mNumTexCols; col++)
1734
        {
1735
        if( texture<mNumTextures-mNumOverrides )
1736
          {
1737
          ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1738
          int color = getColor(texture%mNumFaceColors);
1739
          factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color, sticker);
1740
          }
1741
        else if( texture>mNumTextures-mNumOverrides && texture<=mNumTextures )
1742
          {
1743
          int color = mStickerOverrides[mNumTextures-texture].getColor();
1744
          factory.drawSolidColor(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color);
1745
          }
1746

    
1747
        texture++;
1748
        }
1749
    }
1750

    
1751
///////////////////////////////////////////////////////////////////////////////////////////////////
1752

    
1753
  void setTexture()
1754
    {
1755
    if( mBitmap==null ) createTexture();
1756

    
1757
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1758
      {
1759
      int max = DistortedLibrary.getMaxTextureSize();
1760
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1761
      }
1762
    }
1763

    
1764
///////////////////////////////////////////////////////////////////////////////////////////////////
1765

    
1766
  void setObjectRatioNow(float sc, int nodeSize)
1767
    {
1768
    mObjectScreenRatio = sc;
1769
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1770
    mObjectScale.set(scale,scale,scale);
1771

    
1772
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1773
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1774
    }
1775

    
1776
///////////////////////////////////////////////////////////////////////////////////////////////////
1777

    
1778
  void setObjectRatio(float sizeChange, int nodeSize)
1779
    {
1780
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1781

    
1782
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1783
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1784

    
1785
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1786
    }
1787

    
1788
///////////////////////////////////////////////////////////////////////////////////////////////////
1789

    
1790
  void setNodeSize(int nodeSize)
1791
    {
1792
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1793
    }
1794

    
1795
///////////////////////////////////////////////////////////////////////////////////////////////////
1796

    
1797
  public float getRatio()
1798
    {
1799
    return mObjectScreenRatio;
1800
    }
1801

    
1802
///////////////////////////////////////////////////////////////////////////////////////////////////
1803

    
1804
  public float getObjectRatio()
1805
    {
1806
    return mObjectScreenRatio*mInitScreenRatio;
1807
    }
1808

    
1809
///////////////////////////////////////////////////////////////////////////////////////////////////
1810

    
1811
  boolean isSolved()
1812
    {
1813
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1814
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1815

    
1816
    return false;
1817
    }
1818

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

    
1821
  int computeNearestAngle(int basicAngle, float angle, float speed)
1822
    {
1823
    int nearestAngle = 360/basicAngle;
1824
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1825
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1826

    
1827
    if( tmp!=0 ) return nearestAngle*tmp;
1828

    
1829
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1830
    }
1831

    
1832
///////////////////////////////////////////////////////////////////////////////////////////////////
1833
// INTERNAL API - those are called from 'effects' package
1834
///////////////////////////////////////////////////////////////////////////////////////////////////
1835

    
1836
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1837
    {
1838
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1839
    }
1840

    
1841
///////////////////////////////////////////////////////////////////////////////////////////////////
1842

    
1843
  public Static4D getRotationQuat()
1844
    {
1845
    return mQuat;
1846
    }
1847

    
1848
///////////////////////////////////////////////////////////////////////////////////////////////////
1849

    
1850
  public float getSize()
1851
    {
1852
    return mSize;
1853
    }
1854

    
1855
///////////////////////////////////////////////////////////////////////////////////////////////////
1856

    
1857
  public void apply(Effect effect, int position)
1858
    {
1859
    mEffects.apply(effect, position);
1860
    }
1861

    
1862
///////////////////////////////////////////////////////////////////////////////////////////////////
1863

    
1864
  public void remove(long effectID)
1865
    {
1866
    mEffects.abortById(effectID);
1867
    }
1868

    
1869
///////////////////////////////////////////////////////////////////////////////////////////////////
1870

    
1871
  public MeshBase getObjectMesh()
1872
    {
1873
    return mMesh;
1874
    }
1875

    
1876
///////////////////////////////////////////////////////////////////////////////////////////////////
1877

    
1878
  public DistortedEffects getObjectEffects()
1879
    {
1880
    return mEffects;
1881
    }
1882

    
1883
///////////////////////////////////////////////////////////////////////////////////////////////////
1884

    
1885
  public int getCubitType(int cubit)
1886
    {
1887
    return mCubits[cubit].getType();
1888
    }
1889

    
1890
///////////////////////////////////////////////////////////////////////////////////////////////////
1891

    
1892
  public float[] getCubitOffset(int cubit)
1893
    {
1894
    return mCubits[cubit].getOffset();
1895
    }
1896

    
1897
///////////////////////////////////////////////////////////////////////////////////////////////////
1898

    
1899
  public ObjectStickerOverride[] getStickerOverrides()
1900
    {
1901
    return null;
1902
    }
1903

    
1904
///////////////////////////////////////////////////////////////////////////////////////////////////
1905
// PUBLIC API
1906
///////////////////////////////////////////////////////////////////////////////////////////////////
1907

    
1908
  public int getCubitFaceColorIndex(int cubit, int face)
1909
    {
1910
    Static4D texMap = mMesh.getTextureMap(mNumFaceColors *cubit + face);
1911

    
1912
    int x = (int)(texMap.get0()/texMap.get2());
1913
    int y = (int)(texMap.get1()/texMap.get3());
1914

    
1915
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1916
    }
1917

    
1918
///////////////////////////////////////////////////////////////////////////////////////////////////
1919

    
1920
  public int[] getNumLayers()
1921
    {
1922
    return mNumLayers;
1923
    }
1924

    
1925
///////////////////////////////////////////////////////////////////////////////////////////////////
1926

    
1927
  public synchronized void solve()
1928
    {
1929
    for(int i=0; i<mNumCubits; i++)
1930
      {
1931
      mCubits[i].solve();
1932
      }
1933

    
1934
    recomputeFaceOffsets();
1935

    
1936
    for(int i=0; i<mNumCubits; i++)
1937
      {
1938
      mCubits[i].computeRotationRow();
1939
      setCubitQuat(i,mCubits[i].computeAssociation(),0);
1940
      }
1941
    }
1942

    
1943
///////////////////////////////////////////////////////////////////////////////////////////////////
1944

    
1945
  public int getCubitQuatIndex(int cubit)
1946
    {
1947
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1948
    }
1949

    
1950
///////////////////////////////////////////////////////////////////////////////////////////////////
1951

    
1952
  public int getCubitRotRow(int cubit, int axis)
1953
    {
1954
    return mCubits[cubit].getRotRow(axis);
1955
    }
1956

    
1957
///////////////////////////////////////////////////////////////////////////////////////////////////
1958

    
1959
  public Bitmap getStickerBitmap()
1960
    {
1961
    return mBitmap;
1962
    }
1963

    
1964
///////////////////////////////////////////////////////////////////////////////////////////////////
1965

    
1966
  public DistortedNode getNode()
1967
    {
1968
    return mNode;
1969
    }
1970

    
1971
///////////////////////////////////////////////////////////////////////////////////////////////////
1972

    
1973
  public int getNumStickerTypes()
1974
    {
1975
    return mNumStickerTypes;
1976
    }
1977

    
1978
///////////////////////////////////////////////////////////////////////////////////////////////////
1979
// this is here only so it can be overridden in TwistyJSON soo that we can get this from JSON.
1980

    
1981
  public int getNumCubitFaces()
1982
    {
1983
    return 0;
1984
    }
1985

    
1986
///////////////////////////////////////////////////////////////////////////////////////////////////
1987

    
1988
  public TouchControl getTouchControl()
1989
    {
1990
    if( mTouchControl==null )
1991
      {
1992
      switch(getTouchControlType())
1993
        {
1994
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
1995
                                   break;
1996
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
1997
                                   break;
1998
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
1999
                                   break;
2000
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
2001
                                   break;
2002
        case TC_CUBOID           : int[] numLayers = getNumLayers();
2003
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
2004
                                   break;
2005
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
2006
                                   break;
2007
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
2008
                                   break;
2009
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
2010
                                   break;
2011
        }
2012
      }
2013
    return mTouchControl;
2014
    }
2015

    
2016
///////////////////////////////////////////////////////////////////////////////////////////////////
2017

    
2018
  protected void setReader(JsonReader reader)
2019
    {
2020
    // empty
2021
    }
2022

    
2023
///////////////////////////////////////////////////////////////////////////////////////////////////
2024
  // for JSON only
2025
  public abstract int getTouchControlType();
2026
  public abstract int getTouchControlSplit();
2027
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
2028
  public abstract int[][][] getEnabled();
2029
  public abstract float[] getDist3D(int[] numLayers);
2030
  public abstract Static3D[] getFaceAxis();
2031
  public abstract ScrambleState[] getScrambleStates();
2032
  public abstract float[][] getCuts(int[] numLayers);
2033
  public abstract float getStickerRadius();
2034
  public abstract float getStickerStroke();
2035
  public abstract float[][] getStickerAngles();
2036
  public abstract int getCubitVariant(int cubit, int[] numLayers);
2037
  public abstract ObjectShape getObjectShape(int variant);
2038
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
2039
  public abstract int getNumCubitVariants(int[] numLayers);
2040
  public abstract float[][] getCubitPositions(int[] numLayers);
2041
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
2042
  public abstract int getNumFaceColors();
2043
  public abstract float getScreenRatio();
2044
  public abstract int getColor(int face);
2045
  public abstract String getShortName();
2046
  public abstract ObjectSignature getSignature();
2047

    
2048
  // not only for JSON
2049
  public abstract Static3D[] getRotationAxis();
2050
  public abstract int[][] getBasicAngles();
2051
  public abstract int getNumFaces();
2052
  public abstract String getObjectName();
2053
  public abstract String getInventor();
2054
  public abstract int getYearOfInvention();
2055
  public abstract int getComplexity();
2056
  public abstract int getFOV();
2057
  public abstract String[][] getTutorials();
2058
  }
(12-12/13)