Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 4c9ca251

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.ObjectSticker;
58
import org.distorted.objectlib.helpers.ScrambleState;
59
import org.distorted.objectlib.json.JsonReader;
60
import org.distorted.objectlib.touchcontrol.*;
61

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
public abstract class TwistyObject
67
  {
68
  public static final int MESH_NICE = 0;
69
  public static final int MESH_FAST = 1;
70

    
71
  public static final int COLOR_YELLOW  = 0xffffff00;
72
  public static final int COLOR_WHITE   = 0xffffffff;
73
  public static final int COLOR_BLUE    = 0xff0000ff;
74
  public static final int COLOR_GREEN   = 0xff00bb00;
75
  public static final int COLOR_RED     = 0xff990000;
76
  public static final int COLOR_ORANGE  = 0xffff6200;
77
  public static final int COLOR_GREY    = 0xff727c7b;
78
  public static final int COLOR_VIOLET  = 0xff7700bb;
79
  public static final int COLOR_INTERNAL= 0xff000000;
80

    
81
  public static final int TEXTURE_HEIGHT = 256;
82
  static final int NUM_STICKERS_IN_ROW = 4;
83

    
84
  public static final float SQ2 = (float)Math.sqrt(2);
85
  public static final float SQ3 = (float)Math.sqrt(3);
86
  public static final float SQ5 = (float)Math.sqrt(5);
87
  public static final float SQ6 = (float)Math.sqrt(6);
88

    
89
  private static final float MAX_SIZE_CHANGE = 1.35f;
90
  private static final float MIN_SIZE_CHANGE = 0.75f;
91

    
92
  private static final Static3D CENTER = new Static3D(0,0,0);
93
  private static final int POST_ROTATION_MILLISEC = 500;
94

    
95
  protected int NUM_FACE_COLORS;
96
  protected int NUM_TEXTURES;
97
  protected Cubit[] CUBITS;
98
  protected float[][] mStickerCoords;
99
  protected int[][] mStickerVariants;
100
  protected float[] mStickerScales;
101

    
102
  MeshBase[] mMeshes;
103
  Static4D[] OBJECT_QUATS;
104
  int NUM_CUBITS;
105
  int NUM_AXIS;
106
  int NUM_QUATS;
107
  int SHIFT;
108

    
109
  private int mNumCubitFaces, mNumStickerTypes;
110
  private Static3D[] mAxis;
111
  private float[][] mCuts;
112
  private int[] mNumCuts;
113
  private float[][] mOrigPos;
114
  private Static4D[] mOrigQuat;
115
  private Static4D mQuat;
116
  private final int[] mNumLayers;
117
  private final float mSize;
118
  private DistortedEffects mEffects;
119
  private VertexEffectRotate mRotateEffect;
120
  private Dynamic1D mRotationAngle;
121
  private Static3D mRotationAxis;
122
  private Static3D mObjectScale;
123
  private int[] mQuatDebug;
124
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
125
  private DistortedTexture mTexture;
126
  private float mInitScreenRatio;
127
  private int mSolvedFunctionIndex;
128
  private boolean mIsBandaged;
129
  private float mObjectScreenRatio;
130
  private int[][] mSolvedQuats;
131
  private int[][] mQuatMult;
132
  private int[] mTmpQuats;
133
  private int mNumTexRows, mNumTexCols;
134
  private int mRotRowBitmap;
135
  private int mCurrentRotAxis;
136
  private MeshBase mMesh;
137
  private TwistyObjectScrambler mScrambler;
138
  private TouchControl mTouchControl;
139
  private DistortedNode mNode;
140
  private ObjectLibInterface mInterface;
141
  private Bitmap mBitmap;
142
  private ObjectSticker[] mStickers;
143
  private ObjectShape[] mShapes;
144
  private int mNumCubitVariants;
145

    
146
  //////////////////// SOLVED1 ////////////////////////
147

    
148
  private int[] mFaceMap;
149
  private int[][] mScramble;
150
  private int[] mColors;
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  TwistyObject(InputStream jsonStream, int meshState, Static4D quat, Static3D move, float scale, InputStream meshStream)
155
    {
156
    JsonReader reader = new JsonReader();
157
    reader.parseJsonFile(jsonStream);
158
    setReader(reader);
159

    
160
    mNumLayers = reader.getNumLayers();
161
    mSize      = reader.getSize();
162
    initialize(meshState,quat,move,scale,meshStream,true);
163
    }
164

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

    
167
  TwistyObject(int[] numLayers, int meshState, float size, Static4D quat, Static3D move, float scale, InputStream meshStream)
168
    {
169
    mNumLayers = numLayers;
170
    mSize      = size;
171
    initialize(meshState,quat,move,scale,meshStream,false);
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void initialize(int meshState,Static4D quat, Static3D move, float scale, InputStream stream, boolean fromJSON)
177
    {
178
    mQuat = quat;
179
    mOrigPos = getCubitPositions(mNumLayers);
180
    mAxis = getRotationAxis();
181
    mInitScreenRatio = getScreenRatio();
182
    mSolvedFunctionIndex = getSolvedFunctionIndex();
183

    
184
    int numAxis = mAxis.length;
185
    SHIFT = -1;
186

    
187
    mCuts = getCuts(mNumLayers);
188
    mNumCuts = new int[numAxis];
189
    for(int i=0; i<numAxis; i++)
190
      {
191
      if( SHIFT<mNumLayers[i] ) SHIFT = mNumLayers[i];
192
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
193
      }
194

    
195
    OBJECT_QUATS = getQuats();
196
    NUM_CUBITS  = mOrigPos.length;
197
    NUM_FACE_COLORS = getNumFaceColors();
198
    NUM_AXIS = mAxis.length;
199
    NUM_QUATS = OBJECT_QUATS.length;
200

    
201
    int scramblingType = getScrambleType();
202
    ScrambleState[] states = getScrambleStates();
203
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
204

    
205
    boolean bandaged=false;
206

    
207
    for(int c=0; c<NUM_CUBITS; c++)
208
      {
209
      if( mOrigPos[c].length>3 )
210
        {
211
        bandaged=true;
212
        break;
213
        }
214
      }
215

    
216
    mIsBandaged = bandaged;
217
    mQuatDebug = new int[NUM_CUBITS];
218

    
219
    mRotationAngle= new Dynamic1D();
220
    mRotationAxis = new Static3D(1,0,0);
221
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
222

    
223
    mRotationAngleStatic = new Static1D(0);
224
    mRotationAngleMiddle = new Static1D(0);
225
    mRotationAngleFinal  = new Static1D(0);
226

    
227
    mObjectScale = new Static3D(scale,scale,scale);
228
    setObjectRatioNow(scale,720);
229

    
230
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
231
    MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
232
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
233

    
234
    boolean fromDMESH = (stream!=null && meshState==MESH_NICE);
235
    getQuatsAndShapes(fromDMESH,fromJSON);
236
    createMeshAndCubits(stream,meshState,fromDMESH,fromJSON);
237
    setUpTextures(fromDMESH,fromJSON);
238
    createDataStructuresForSolved(mNumLayers);
239

    
240
    mEffects = new DistortedEffects();
241

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

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

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

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

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

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

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

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

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
283
    {
284
    mNumCubitVariants = getNumCubitVariants(mNumLayers);
285

    
286
    if( !fromDMESH || !fromJSON )
287
      {
288
      FactoryCubit factory = FactoryCubit.getInstance();
289
      factory.clear();
290

    
291
      mOrigQuat = new Static4D[NUM_CUBITS];
292
      for(int i=0; i<NUM_CUBITS; i++) mOrigQuat[i] = getQuat(i,mNumLayers);
293

    
294
      mShapes = new ObjectShape[mNumCubitVariants];
295
      for(int i=0; i<mNumCubitVariants; i++) mShapes[i] = getObjectShape(i);
296
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
297

    
298
      if( fromDMESH )
299
        {
300
        for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i]);
301
        }
302
      }
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  private void createMeshAndCubits(InputStream stream, int meshState, boolean fromDMESH, boolean fromJSON)
308
    {
309
    CUBITS = new Cubit[NUM_CUBITS];
310

    
311
    if( fromDMESH )
312
      {
313
      DataInputStream dos = new DataInputStream(stream);
314
      mMesh = new MeshFile(dos);
315

    
316
      try
317
        {
318
        stream.close();
319
        }
320
      catch(IOException e)
321
        {
322
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
323
        }
324

    
325
      for(int i=0; i<NUM_CUBITS; i++)
326
        {
327
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
328
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
329
        }
330
      }
331
    else
332
      {
333
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
334

    
335
      for(int i=0; i<NUM_CUBITS; i++)
336
        {
337
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
338
        cubitMesh[i] = createCubitMesh(i,mNumLayers,meshState,mNumCubitFaces);
339
        Static3D pos = getPos(mOrigPos[i]);
340
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
341
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
342
        }
343

    
344
      mMesh = new MeshJoined(cubitMesh);
345
      }
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int meshState, int numComponents)
351
    {
352
    int variant = getCubitVariant(cubit,numLayers);
353

    
354
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
355

    
356
    if( mMeshes[variant]==null )
357
      {
358
      ObjectFaceShape faceShape = getObjectFaceShape(variant);
359
      FactoryCubit factory = FactoryCubit.getInstance();
360
      factory.createNewFaceTransform(mShapes[variant]);
361
      mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,meshState, numComponents);
362
      }
363

    
364
    MeshBase mesh = mMeshes[variant].copy(true);
365
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
366
    mesh.apply(quat,0xffffffff,0);
367

    
368
    return mesh;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
374
    {
375
    mTexture = new DistortedTexture();
376

    
377
    if( fromJSON )
378
      {
379
      mNumStickerTypes = getNumStickerTypes();
380
      mNumCubitFaces = getNumCubitFaces();
381
      }
382
    else
383
      {
384
      FactoryCubit factory = FactoryCubit.getInstance();
385
      mStickerCoords   = factory.getOuterStickerCoords();
386
      mStickerVariants = factory.getStickerVariants();
387
      mStickerScales   = factory.getOuterStickerScales();
388
      mNumStickerTypes = mStickerCoords.length;
389
      adjustStickerCoords();
390
      }
391

    
392
    NUM_TEXTURES = NUM_FACE_COLORS*mNumStickerTypes;
393
    mNumTexCols = NUM_STICKERS_IN_ROW;
394
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
395
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
396

    
397
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
398
    setTexture();
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
  private void createDataStructuresForSolved(int[] numLayers)
404
    {
405
    mTmpQuats = new int[NUM_QUATS];
406
    mSolvedQuats = new int[NUM_CUBITS][];
407

    
408
    for(int c=0; c<NUM_CUBITS; c++)
409
      {
410
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
411
      }
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
  private int getMultQuat(int index1, int index2)
417
    {
418
    if( mQuatMult==null )
419
      {
420
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
421

    
422
      for(int i=0; i<NUM_QUATS; i++)
423
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
424
      }
425

    
426
    if( mQuatMult[index1][index2]==-1 )
427
      {
428
      mQuatMult[index1][index2] = mulQuat(index1,index2);
429
      }
430

    
431
    return mQuatMult[index1][index2];
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435

    
436
  public int getVariantFaceColor(int variant, int face)
437
    {
438
    return face>=mStickerVariants[variant].length ? -1 : mStickerVariants[variant][face];
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442
// This is used to build internal data structures for the generic 'isSolved()'
443
//
444
// if this is an internal cubit (all faces black): return -1
445
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
446
// Color index, i.e. the index into the 'FACE_COLORS' table.
447
// else (edge or corner cubit, more than one non-black face): return -2.
448

    
449
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
450
    {
451
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
452
    int variant = getCubitVariant(cubit,numLayers);
453

    
454
    for(int face=0; face<mNumCubitFaces; face++)
455
      {
456
      varColor = getVariantFaceColor(variant,face);
457
      cubColor = getCubitFaceColor(cubit,face,numLayers);
458

    
459
      if( varColor>=0 && cubColor>=0 )
460
        {
461
        numNonBlack++;
462
        nonBlackIndex = cubColor;
463
        }
464
      }
465

    
466
    if( numNonBlack==0 ) return -1;
467
    if( numNonBlack>=2 ) return -2;
468

    
469
    return nonBlackIndex;
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  public boolean shouldResetTextureMaps()
475
    {
476
    return false;
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
482
    {
483
    final float MAXD = 0.0001f;
484
    float x = faceAx.get0();
485
    float y = faceAx.get1();
486
    float z = faceAx.get2();
487
    float a,dx,dy,dz,qx,qy,qz;
488
    Static4D quat;
489

    
490
    int len = quats.length;
491
    int place = 0;
492

    
493
    for(int q=1; q<len; q++)
494
      {
495
      quat = quats[q];
496
      qx = quat.get0();
497
      qy = quat.get1();
498
      qz = quat.get2();
499

    
500
           if( x!=0.0f ) { a = qx/x; }
501
      else if( y!=0.0f ) { a = qy/y; }
502
      else               { a = qz/z; }
503

    
504
      dx = a*x-qx;
505
      dy = a*y-qy;
506
      dz = a*z-qz;
507

    
508
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
509
        {
510
        mTmpQuats[place++] = q;
511
        }
512
      }
513

    
514
    if( place!=0 )
515
      {
516
      int[] ret = new int[place];
517
      System.arraycopy(mTmpQuats,0,ret,0,place);
518
      return ret;
519
      }
520

    
521
    return null;
522
    }
523

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

    
526
  private boolean isSolved0()
527
    {
528
    int len, q1,q = CUBITS[0].mQuatIndex;
529
    int[] solved;
530
    boolean skip;
531

    
532
    for(int c=1; c<NUM_CUBITS; c++)
533
      {
534
      q1 = CUBITS[c].mQuatIndex;
535

    
536
      if( q1==q ) continue;
537

    
538
      skip = false;
539
      solved = mSolvedQuats[c];
540
      len = solved==null ? 0:solved.length;
541

    
542
      for(int i=0; i<len; i++)
543
        {
544
        if( q1==getMultQuat(q,solved[i]) )
545
          {
546
          skip = true;
547
          break;
548
          }
549
        }
550

    
551
      if( !skip ) return false;
552
      }
553

    
554
    return true;
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

    
559
  private int computeScramble(int quatNum, int centerNum)
560
    {
561
    float MAXDIFF = 0.01f;
562
    float[] center= mOrigPos[centerNum];
563
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
564
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
565

    
566
    float x = result.get0();
567
    float y = result.get1();
568
    float z = result.get2();
569

    
570
    for(int c=0; c<NUM_CUBITS; c++)
571
      {
572
      float[] cent = mOrigPos[c];
573

    
574
      float qx = cent[0] - x;
575
      float qy = cent[1] - y;
576
      float qz = cent[2] - z;
577

    
578
      if( qx>-MAXDIFF && qx<MAXDIFF &&
579
          qy>-MAXDIFF && qy<MAXDIFF &&
580
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
581
      }
582

    
583
    return -1;
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587
// Dino4 uses this. It is solved if and only if groups of cubits
588
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
589
// or
590
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
591
// are all the same color.
592

    
593
  private boolean isSolved1()
594
    {
595
    if( mScramble==null )
596
      {
597
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
598
      mColors   = new int[NUM_CUBITS];
599

    
600
      for(int q=0; q<NUM_QUATS; q++)
601
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
602
      }
603

    
604
    if( mFaceMap==null )
605
      {
606
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
607
      }
608

    
609
    for(int c=0; c<NUM_CUBITS; c++)
610
      {
611
      int index = mScramble[CUBITS[c].mQuatIndex][c];
612
      mColors[index] = mFaceMap[c];
613
      }
614

    
615
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
616
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
617
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
618

    
619
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
620
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
621
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
622

    
623
    return false;
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627
// Dino6 uses this. It is solved if and only if:
628
//
629
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
630
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
631
// by the same qZ, and then either:
632
//
633
// a) qX = qY = qZ
634
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
635
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
636
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
637
//
638
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
639
//
640
// X cubits: 0, 2, 8, 10
641
// Y cubits: 1, 3, 9, 11
642
// Z cubits: 4, 5, 6, 7
643

    
644
  private boolean isSolved2()
645
    {
646
    int qX = CUBITS[0].mQuatIndex;
647
    int qY = CUBITS[1].mQuatIndex;
648
    int qZ = CUBITS[4].mQuatIndex;
649

    
650
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
651
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
652
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
653
      {
654
      return false;
655
      }
656

    
657
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
658
    }
659

    
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661
// Square-2 is solved iff
662
// a) all of its cubits are rotated with the same quat
663
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
664
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
665
// and all the 12 left and right edges and corners also with the same quat multiplied by
666
// QUATS[12] - i.e. also upside down.
667

    
668
  private boolean isSolved3()
669
    {
670
    int index = CUBITS[0].mQuatIndex;
671

    
672
    if( CUBITS[1].mQuatIndex!=index ) return false;
673

    
674
    boolean solved = true;
675

    
676
    for(int i=2; i<NUM_CUBITS; i++)
677
      {
678
      if( CUBITS[i].mQuatIndex!=index )
679
        {
680
        solved = false;
681
        break;
682
        }
683
      }
684

    
685
    if( solved ) return true;
686

    
687
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
688
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
689

    
690
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
691
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
692
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
693
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
694

    
695
    return true;
696
    }
697

    
698
///////////////////////////////////////////////////////////////////////////////////////////////////
699

    
700
  int computeRow(float[] pos, int axisIndex)
701
    {
702
    int ret=0;
703
    int len = pos.length / 3;
704
    Static3D axis = mAxis[axisIndex];
705
    float axisX = axis.get0();
706
    float axisY = axis.get1();
707
    float axisZ = axis.get2();
708
    float casted;
709

    
710
    for(int i=0; i<len; i++)
711
      {
712
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
713
      ret |= computeSingleRow(axisIndex,casted);
714
      }
715

    
716
    return ret;
717
    }
718

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

    
721
  private int computeSingleRow(int axisIndex,float casted)
722
    {
723
    int num = mNumCuts[axisIndex];
724

    
725
    for(int i=0; i<num; i++)
726
      {
727
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
728
      }
729

    
730
    return (1<<num);
731
    }
732

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

    
735
  private boolean wasRotateApplied()
736
    {
737
    return mEffects.exists(mRotateEffect.getID());
738
    }
739

    
740
///////////////////////////////////////////////////////////////////////////////////////////////////
741

    
742
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
743
    {
744
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
745
    }
746

    
747
///////////////////////////////////////////////////////////////////////////////////////////////////
748
// note the minus in front of the sin() - we rotate counterclockwise
749
// when looking towards the direction where the axis increases in values.
750

    
751
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
752
    {
753
    Static3D axis = mAxis[axisIndex];
754

    
755
    while( angleInDegrees<0 ) angleInDegrees += 360;
756
    angleInDegrees %= 360;
757
    
758
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
759
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
760

    
761
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
762
    }
763

    
764
///////////////////////////////////////////////////////////////////////////////////////////////////
765

    
766
  private synchronized void setupPosition(int[][] moves)
767
    {
768
    if( moves!=null )
769
      {
770
      Static4D quat;
771
      int index, axis, rowBitmap, angle;
772
      int[] basic = getBasicAngle();
773

    
774
      for(int[] move: moves)
775
        {
776
        axis     = move[0];
777
        rowBitmap= move[1];
778
        angle    = move[2]*(360/basic[axis]);
779
        quat     = makeQuaternion(axis,angle);
780

    
781
        for(int j=0; j<NUM_CUBITS; j++)
782
          if( belongsToRotation(j,axis,rowBitmap) )
783
            {
784
            index = CUBITS[j].removeRotationNow(quat);
785
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
786
            }
787
        }
788
      }
789
    }
790

    
791
///////////////////////////////////////////////////////////////////////////////////////////////////
792

    
793
  public int getScrambleType()
794
    {
795
    return 0;
796
    }
797

    
798
///////////////////////////////////////////////////////////////////////////////////////////////////
799

    
800
  int computeBitmapFromRow(int rowBitmap, int axis)
801
    {
802
    if( mIsBandaged )
803
      {
804
      int bitmap, initBitmap=0;
805

    
806
      while( initBitmap!=rowBitmap )
807
        {
808
        initBitmap = rowBitmap;
809

    
810
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
811
          {
812
          bitmap = CUBITS[cubit].getRotRow(axis);
813
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
814
          }
815
        }
816
      }
817

    
818
    return rowBitmap;
819
    }
820

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

    
825
  void clampPos(float[] pos, int offset)
826
    {
827
    float currError, minError = Float.MAX_VALUE;
828
    int minErrorIndex1 = -1;
829
    int minErrorIndex2 = -1;
830

    
831
    float x = pos[offset  ];
832
    float y = pos[offset+1];
833
    float z = pos[offset+2];
834

    
835
    float xo,yo,zo;
836

    
837
    for(int i=0; i<NUM_CUBITS; i++)
838
      {
839
      int len = mOrigPos[i].length / 3;
840

    
841
      for(int j=0; j<len; j++)
842
        {
843
        xo = mOrigPos[i][3*j  ];
844
        yo = mOrigPos[i][3*j+1];
845
        zo = mOrigPos[i][3*j+2];
846

    
847
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
848

    
849
        if( currError<minError )
850
          {
851
          minError = currError;
852
          minErrorIndex1 = i;
853
          minErrorIndex2 = j;
854
          }
855
        }
856
      }
857

    
858
    if( minError< 0.1f ) // TODO: 0.1 ?
859
      {
860
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
861
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
862
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
863
      }
864
    }
865

    
866
///////////////////////////////////////////////////////////////////////////////////////////////////
867
// remember about the double cover or unit quaternions!
868

    
869
  int mulQuat(int q1, int q2)
870
    {
871
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
872

    
873
    float rX = result.get0();
874
    float rY = result.get1();
875
    float rZ = result.get2();
876
    float rW = result.get3();
877

    
878
    final float MAX_ERROR = 0.1f;
879
    float dX,dY,dZ,dW;
880

    
881
    for(int i=0; i<NUM_QUATS; i++)
882
      {
883
      dX = OBJECT_QUATS[i].get0() - rX;
884
      dY = OBJECT_QUATS[i].get1() - rY;
885
      dZ = OBJECT_QUATS[i].get2() - rZ;
886
      dW = OBJECT_QUATS[i].get3() - rW;
887

    
888
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
889
          dY<MAX_ERROR && dY>-MAX_ERROR &&
890
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
891
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
892

    
893
      dX = OBJECT_QUATS[i].get0() + rX;
894
      dY = OBJECT_QUATS[i].get1() + rY;
895
      dZ = OBJECT_QUATS[i].get2() + rZ;
896
      dW = OBJECT_QUATS[i].get3() + rW;
897

    
898
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
899
          dY<MAX_ERROR && dY>-MAX_ERROR &&
900
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
901
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
902
      }
903

    
904
    return -1;
905
    }
906

    
907
///////////////////////////////////////////////////////////////////////////////////////////////////
908

    
909
  private float getAngle()
910
    {
911
    int pointNum = mRotationAngle.getNumPoints();
912

    
913
    if( pointNum>=1 )
914
      {
915
      return mRotationAngle.getPoint(pointNum-1).get0();
916
      }
917
    else
918
      {
919
      mInterface.reportProblem("points in RotationAngle: "+pointNum, false);
920
      return 0;
921
      }
922
    }
923

    
924
///////////////////////////////////////////////////////////////////////////////////////////////////
925

    
926
  void setLibInterface(ObjectLibInterface inter)
927
    {
928
    mInterface = inter;
929
    }
930

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

    
933
  void initializeObject(int[][] moves)
934
    {
935
    solve();
936
    setupPosition(moves);
937
    }
938

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

    
941
  synchronized void removeRotationNow()
942
    {
943
    float angle = getAngle();
944
    double nearestAngleInRadians = angle*Math.PI/180;
945
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
946
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
947
    float axisX = mAxis[mCurrentRotAxis].get0();
948
    float axisY = mAxis[mCurrentRotAxis].get1();
949
    float axisZ = mAxis[mCurrentRotAxis].get2();
950
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
951

    
952
    mRotationAngle.removeAll();
953
    mRotationAngleStatic.set0(0);
954

    
955
    for(int i=0; i<NUM_CUBITS; i++)
956
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
957
        {
958
        int index = CUBITS[i].removeRotationNow(quat);
959
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
960
        }
961
    }
962

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

    
965
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
966
    {
967
    if( wasRotateApplied() )
968
      {
969
      float angle = getAngle();
970
      mRotationAngleStatic.set0(angle);
971
      mRotationAngleFinal.set0(nearestAngleInDegrees);
972
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
973

    
974
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
975
      mRotationAngle.resetToBeginning();
976
      mRotationAngle.removeAll();
977
      mRotationAngle.add(mRotationAngleStatic);
978
      mRotationAngle.add(mRotationAngleMiddle);
979
      mRotationAngle.add(mRotationAngleFinal);
980
      mRotateEffect.notifyWhenFinished(listener);
981

    
982
      return mRotateEffect.getID();
983
      }
984

    
985
    return 0;
986
    }
987

    
988
///////////////////////////////////////////////////////////////////////////////////////////////////
989

    
990
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
991
    {
992
    if( wasRotateApplied() )
993
      {
994
      mCurrentRotAxis = axis;
995
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
996

    
997
      mRotationAngleStatic.set0(0.0f);
998
      mRotationAxis.set( mAxis[axis] );
999
      mRotationAngle.setDuration(durationMillis);
1000
      mRotationAngle.resetToBeginning();
1001
      mRotationAngle.add(new Static1D(0));
1002
      mRotationAngle.add(new Static1D(angle));
1003
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
1004
      mRotateEffect.notifyWhenFinished(listener);
1005

    
1006
      return mRotateEffect.getID();
1007
      }
1008

    
1009
    return 0;
1010
    }
1011

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////
1013

    
1014
  void continueRotation(float angleInDegrees)
1015
    {
1016
    mRotationAngleStatic.set0(angleInDegrees);
1017
    }
1018

    
1019
///////////////////////////////////////////////////////////////////////////////////////////////////
1020

    
1021
  synchronized void beginNewRotation(int axis, int row )
1022
    {
1023
    if( axis<0 || axis>=NUM_AXIS )
1024
      {
1025
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1026
      return;
1027
      }
1028
    if( row<0 || row>=mNumLayers[axis] )
1029
      {
1030
      android.util.Log.e("object", "invalid rotation row: "+row);
1031
      return;
1032
      }
1033

    
1034
    mCurrentRotAxis = axis;
1035
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1036
    mRotationAngleStatic.set0(0.0f);
1037
    mRotationAxis.set( mAxis[axis] );
1038
    mRotationAngle.add(mRotationAngleStatic);
1039
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
1040
    }
1041

    
1042
///////////////////////////////////////////////////////////////////////////////////////////////////
1043

    
1044
  void setTextureMap(int cubit, int face, int newColor)
1045
    {
1046
    final float ratioW = 1.0f/mNumTexCols;
1047
    final float ratioH = 1.0f/mNumTexRows;
1048
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1049
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1050
    int col = newColor%mNumTexCols;
1051

    
1052
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1053
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1054
    }
1055

    
1056
///////////////////////////////////////////////////////////////////////////////////////////////////
1057

    
1058
  void resetAllTextureMaps()
1059
    {
1060
    final float ratioW = 1.0f/mNumTexCols;
1061
    final float ratioH = 1.0f/mNumTexRows;
1062
    int cubColor, varColor, color, variant, row, col;
1063

    
1064
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1065
      {
1066
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1067
      variant = getCubitVariant(cubit,mNumLayers);
1068

    
1069
      for(int face=0; face<mNumCubitFaces; face++)
1070
        {
1071
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1072
        varColor = getVariantFaceColor(variant,face);
1073
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1074
        row      = (mNumTexRows-1) - color/mNumTexCols;
1075
        col      = color%mNumTexCols;
1076

    
1077
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1078
        }
1079

    
1080
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1081
      }
1082
    }
1083

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

    
1086
  void releaseResources()
1087
    {
1088
    mTexture.markForDeletion();
1089
    mMesh.markForDeletion();
1090
    mEffects.markForDeletion();
1091

    
1092
    for(int j=0; j<NUM_CUBITS; j++)
1093
      {
1094
      CUBITS[j].releaseResources();
1095
      }
1096
    }
1097

    
1098
///////////////////////////////////////////////////////////////////////////////////////////////////
1099

    
1100
  synchronized void restorePreferences(SharedPreferences preferences)
1101
    {
1102
    boolean error = false;
1103

    
1104
    for(int i=0; i<NUM_CUBITS; i++)
1105
      {
1106
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1107

    
1108
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1109
        {
1110
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1111
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1112
        }
1113
      else
1114
        {
1115
        error = true;
1116
        }
1117
      }
1118

    
1119
    if( error )
1120
      {
1121
      for(int i=0; i<NUM_CUBITS; i++)
1122
        {
1123
        CUBITS[i].solve();
1124
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1125
        }
1126
      }
1127
    }
1128

    
1129
///////////////////////////////////////////////////////////////////////////////////////////////////
1130

    
1131
  void savePreferences(SharedPreferences.Editor editor)
1132
    {
1133
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1134
    }
1135

    
1136
///////////////////////////////////////////////////////////////////////////////////////////////////
1137

    
1138
  private float computeRadiusCorrection(float[] sticker, int curr, int len)
1139
    {
1140
    final float A = 0.8f;  // 0<A<1
1141

    
1142
    int prev = curr>0 ? curr-1 : len-1;
1143
    int next = curr<len-1 ? curr+1 : 0;
1144

    
1145
    float v1x = sticker[2*prev  ]-sticker[2*curr  ];
1146
    float v1y = sticker[2*prev+1]-sticker[2*curr+1];
1147
    float v2x = sticker[2*next  ]-sticker[2*curr  ];
1148
    float v2y = sticker[2*next+1]-sticker[2*curr+1];
1149

    
1150
    float len1= v1x*v1x+v1y*v1y;
1151
    float len2= v2x*v2x+v2y*v2y;
1152

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

    
1155
    return 1-A*cos;
1156
    }
1157

    
1158
///////////////////////////////////////////////////////////////////////////////////////////////////
1159

    
1160
  public ObjectSticker retSticker(int sticker)
1161
    {
1162
    if( mStickers==null )
1163
      {
1164
      float rad = getStickerRadius();
1165
      float str = getStickerStroke();
1166
      float[][] angles = getStickerAngles();
1167
      int numStickers = mStickerCoords.length;
1168
      mStickers = new ObjectSticker[numStickers];
1169

    
1170
      for(int s=0; s<numStickers; s++)
1171
        {
1172
        float scale = mStickerScales[s];
1173
        float radius = rad / scale;
1174
        float stroke = str / scale;
1175
        int len = mStickerCoords[s].length/2;
1176
        float[] radii = new float[len];
1177
        for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
1178
        mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
1179
        }
1180
      }
1181

    
1182
    return mStickers[sticker];
1183
    }
1184

    
1185
///////////////////////////////////////////////////////////////////////////////////////////////////
1186
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1187

    
1188
  public void adjustStickerCoords()
1189
    {
1190

    
1191
    }
1192

    
1193
///////////////////////////////////////////////////////////////////////////////////////////////////
1194
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1195

    
1196
  private void createTexture()
1197
    {
1198
    Paint paint = new Paint();
1199
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1200
    Canvas canvas = new Canvas(mBitmap);
1201

    
1202
    paint.setAntiAlias(true);
1203
    paint.setTextAlign(Paint.Align.CENTER);
1204
    paint.setStyle(Paint.Style.FILL);
1205

    
1206
    paint.setColor(COLOR_INTERNAL);
1207
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1208

    
1209
    int texture = 0;
1210
    FactorySticker factory = FactorySticker.getInstance();
1211

    
1212
    for(int row=0; row<mNumTexRows; row++)
1213
      for(int col=0; col<mNumTexCols; col++)
1214
        {
1215
        if( texture>=NUM_TEXTURES ) break;
1216
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1217
        int color = getColor(texture%NUM_FACE_COLORS);
1218
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1219
        texture++;
1220
        }
1221
    }
1222

    
1223
///////////////////////////////////////////////////////////////////////////////////////////////////
1224

    
1225
  void setTexture()
1226
    {
1227
    if( mBitmap==null ) createTexture();
1228

    
1229
    if( !mTexture.setTexture(mBitmap) )
1230
      {
1231
      int max = DistortedLibrary.getMaxTextureSize();
1232
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1233
      }
1234
    }
1235

    
1236
///////////////////////////////////////////////////////////////////////////////////////////////////
1237

    
1238
  void setObjectRatioNow(float sc, int nodeSize)
1239
    {
1240
    mObjectScreenRatio = sc;
1241
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1242
    mObjectScale.set(scale,scale,scale);
1243

    
1244
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1245
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1246
    }
1247

    
1248
///////////////////////////////////////////////////////////////////////////////////////////////////
1249

    
1250
  void setObjectRatio(float sizeChange, int nodeSize)
1251
    {
1252
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1253

    
1254
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1255
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1256

    
1257
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1258
    }
1259

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

    
1262
  void setNodeSize(int nodeSize)
1263
    {
1264
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1265
    }
1266

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

    
1269
  public float getRatio()
1270
    {
1271
    return mObjectScreenRatio;
1272
    }
1273

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

    
1276
  public float getObjectRatio()
1277
    {
1278
    return mObjectScreenRatio*mInitScreenRatio;
1279
    }
1280

    
1281
///////////////////////////////////////////////////////////////////////////////////////////////////
1282

    
1283
  boolean isSolved()
1284
    {
1285
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1286
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1287
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1288
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1289

    
1290
    return false;
1291
    }
1292

    
1293
///////////////////////////////////////////////////////////////////////////////////////////////////
1294

    
1295
  int computeNearestAngle(int axis, float angle, float speed)
1296
    {
1297
    int[] basicArray = getBasicAngle();
1298
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1299
    int nearestAngle = 360/basicAngle;
1300

    
1301
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1302
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1303

    
1304
    if( tmp!=0 ) return nearestAngle*tmp;
1305

    
1306
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1307
    }
1308

    
1309
///////////////////////////////////////////////////////////////////////////////////////////////////
1310
// INTERNAL API - those are called from 'effects' package
1311
///////////////////////////////////////////////////////////////////////////////////////////////////
1312

    
1313
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1314
    {
1315
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1316
    }
1317

    
1318
///////////////////////////////////////////////////////////////////////////////////////////////////
1319

    
1320
  public Static4D getRotationQuat()
1321
    {
1322
    return mQuat;
1323
    }
1324

    
1325
///////////////////////////////////////////////////////////////////////////////////////////////////
1326

    
1327
  public float getSize()
1328
    {
1329
    return mSize;
1330
    }
1331

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

    
1334
  public void apply(Effect effect, int position)
1335
    {
1336
    mEffects.apply(effect, position);
1337
    }
1338

    
1339
///////////////////////////////////////////////////////////////////////////////////////////////////
1340

    
1341
  public void remove(long effectID)
1342
    {
1343
    mEffects.abortById(effectID);
1344
    }
1345

    
1346
///////////////////////////////////////////////////////////////////////////////////////////////////
1347

    
1348
  public MeshBase getObjectMesh()
1349
    {
1350
    return mMesh;
1351
    }
1352

    
1353
///////////////////////////////////////////////////////////////////////////////////////////////////
1354

    
1355
  public DistortedEffects getObjectEffects()
1356
    {
1357
    return mEffects;
1358
    }
1359

    
1360
///////////////////////////////////////////////////////////////////////////////////////////////////
1361
// PUBLIC API
1362
///////////////////////////////////////////////////////////////////////////////////////////////////
1363

    
1364
  public int getCubitFaceColorIndex(int cubit, int face)
1365
    {
1366
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1367

    
1368
    int x = (int)(texMap.get0()/texMap.get2());
1369
    int y = (int)(texMap.get1()/texMap.get3());
1370

    
1371
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1372
    }
1373

    
1374
///////////////////////////////////////////////////////////////////////////////////////////////////
1375

    
1376
  public int[] getNumLayers()
1377
    {
1378
    return mNumLayers;
1379
    }
1380

    
1381
///////////////////////////////////////////////////////////////////////////////////////////////////
1382

    
1383
  public synchronized void solve()
1384
    {
1385
    for(int i=0; i<NUM_CUBITS; i++)
1386
      {
1387
      CUBITS[i].solve();
1388
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1389
      }
1390
    }
1391

    
1392
///////////////////////////////////////////////////////////////////////////////////////////////////
1393

    
1394
  public int getCubitQuatIndex(int cubit)
1395
    {
1396
    return CUBITS[cubit].mQuatIndex;
1397
    }
1398

    
1399
///////////////////////////////////////////////////////////////////////////////////////////////////
1400

    
1401
  public int getCubitRotRow(int cubit, int axis)
1402
    {
1403
    return CUBITS[cubit].getRotRow(axis);
1404
    }
1405

    
1406
///////////////////////////////////////////////////////////////////////////////////////////////////
1407

    
1408
  public Bitmap getStickerBitmap()
1409
    {
1410
    return mBitmap;
1411
    }
1412

    
1413
///////////////////////////////////////////////////////////////////////////////////////////////////
1414

    
1415
  public DistortedNode getNode()
1416
    {
1417
    return mNode;
1418
    }
1419

    
1420
///////////////////////////////////////////////////////////////////////////////////////////////////
1421

    
1422
  public ObjectType getObjectType()
1423
    {
1424
    return intGetObjectType(mNumLayers);
1425
    }
1426

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

    
1429
  public int getNumStickerTypes()
1430
    {
1431
    return mNumStickerTypes;
1432
    }
1433

    
1434
///////////////////////////////////////////////////////////////////////////////////////////////////
1435
// this is here only so it can be overridden in TwistyJSON soo that we can get this from JSON.
1436

    
1437
  public int getNumCubitFaces()
1438
    {
1439
    return 0;
1440
    }
1441

    
1442
///////////////////////////////////////////////////////////////////////////////////////////////////
1443

    
1444
  public TouchControl getTouchControl()
1445
    {
1446
    if( mTouchControl==null )
1447
      {
1448
      switch(getTouchControlType())
1449
        {
1450
        case TC_TETRAHEDRON    : mTouchControl = new TouchControlTetrahedron(this);
1451
                                 break;
1452
        case TC_HEXAHEDRON     : mTouchControl = new TouchControlHexahedron(this);
1453
                                 break;
1454
        case TC_OCTAHEDRON     : mTouchControl = new TouchControlOctahedron(this);
1455
                                 break;
1456
        case TC_DODECAHEDRON   : mTouchControl = new TouchControlDodecahedron(this);
1457
                                 break;
1458
        case TC_CUBOID         : int[] numLayers = getNumLayers();
1459
                                 mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1460
                                 break;
1461
        case TC_CHANGING_MIRROR: mTouchControl = new TouchControlMirror(this);
1462
                                 break;
1463
        case TC_CHANGING_SQUARE: mTouchControl = new TouchControlSquare(this);
1464
                                 break;
1465
        }
1466
      }
1467
    return mTouchControl;
1468
    }
1469

    
1470
///////////////////////////////////////////////////////////////////////////////////////////////////
1471

    
1472
  public String getShortName()
1473
    {
1474
    return getObjectType().name();
1475
    }
1476

    
1477
///////////////////////////////////////////////////////////////////////////////////////////////////
1478

    
1479
  protected void setReader(JsonReader reader)
1480
    {
1481
    // empty
1482
    }
1483

    
1484
///////////////////////////////////////////////////////////////////////////////////////////////////
1485

    
1486
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1487

    
1488
  // for JSON only
1489
  public abstract int getSolvedFunctionIndex();
1490
  public abstract int getTouchControlType();
1491
  public abstract int getTouchControlSplit();
1492
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1493
  public abstract int[][][] getEnabled();
1494
  public abstract float[] getDist3D(int[] numLayers);
1495
  public abstract Static3D[] getFaceAxis();
1496
  public abstract ScrambleState[] getScrambleStates();
1497
  public abstract float[][] getCuts(int[] numLayers);
1498
  public abstract Static4D[] getQuats();
1499
  public abstract float getStickerRadius();
1500
  public abstract float getStickerStroke();
1501
  public abstract float[][] getStickerAngles();
1502
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1503
  public abstract ObjectShape getObjectShape(int variant);
1504
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
1505
  public abstract int getNumCubitVariants(int[] numLayers);
1506
  public abstract float[][] getCubitPositions(int[] numLayers);
1507
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1508
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1509
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1510
  public abstract int getNumFaceColors();
1511
  public abstract float getScreenRatio();
1512
  public abstract int getColor(int face);
1513

    
1514
  // not only for JSON
1515
  public abstract Static3D[] getRotationAxis();
1516
  public abstract int[] getBasicAngle();
1517
  public abstract int getNumFaces();
1518
  public abstract String getObjectName();
1519
  public abstract String getInventor();
1520
  public abstract int getYearOfInvention();
1521
  public abstract int getComplexity();
1522
  public abstract int getFOV();
1523
  }
(11-11/13)