Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objectlib.main;
21

    
22
import java.io.DataInputStream;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.util.Random;
26

    
27
import android.content.SharedPreferences;
28
import android.graphics.Bitmap;
29
import android.graphics.Canvas;
30
import android.graphics.Paint;
31

    
32
import org.distorted.library.effect.Effect;
33
import org.distorted.library.effect.MatrixEffectMove;
34
import org.distorted.library.effect.MatrixEffectQuaternion;
35
import org.distorted.library.effect.MatrixEffectScale;
36
import org.distorted.library.effect.VertexEffectQuaternion;
37
import org.distorted.library.effect.VertexEffectRotate;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.library.main.DistortedNode;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.main.QuatHelper;
43
import org.distorted.library.mesh.MeshBase;
44
import org.distorted.library.mesh.MeshFile;
45
import org.distorted.library.mesh.MeshJoined;
46
import org.distorted.library.message.EffectListener;
47
import org.distorted.library.type.Dynamic1D;
48
import org.distorted.library.type.Static1D;
49
import org.distorted.library.type.Static3D;
50
import org.distorted.library.type.Static4D;
51

    
52
import org.distorted.objectlib.helpers.FactoryCubit;
53
import org.distorted.objectlib.helpers.FactorySticker;
54
import org.distorted.objectlib.helpers.ObjectLibInterface;
55
import org.distorted.objectlib.helpers.ObjectShape;
56
import org.distorted.objectlib.helpers.ObjectSticker;
57
import org.distorted.objectlib.helpers.ScrambleState;
58
import org.distorted.objectlib.json.JsonReader;
59
import org.distorted.objectlib.touchcontrol.*;
60

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

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

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

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

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

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

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

    
142
  //////////////////// SOLVED1 ////////////////////////
143

    
144
  private int[] mFaceMap;
145
  private int[][] mScramble;
146
  private int[] mColors;
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

    
181
    int numAxis = mAxis.length;
182
    SHIFT = -1;
183

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

    
192
    OBJECT_QUATS = getQuats();
193
    NUM_CUBITS  = mOrigPos.length;
194
    NUM_FACE_COLORS = getNumFaceColors();
195
    NUM_AXIS = mAxis.length;
196
    NUM_QUATS = OBJECT_QUATS.length;
197

    
198
    int scramblingType = getScrambleType();
199
    ScrambleState[] states = getScrambleStates();
200
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
201

    
202
    boolean bandaged=false;
203

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

    
213
    mIsBandaged = bandaged;
214
    mQuatDebug = new int[NUM_CUBITS];
215

    
216
    mRotationAngle= new Dynamic1D();
217
    mRotationAxis = new Static3D(1,0,0);
218
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
219

    
220
    mRotationAngleStatic = new Static1D(0);
221
    mRotationAngleMiddle = new Static1D(0);
222
    mRotationAngleFinal  = new Static1D(0);
223

    
224
    mObjectScale = new Static3D(scale,scale,scale);
225
    setObjectRatioNow(scale,720);
226

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

    
231
    CUBITS = new Cubit[NUM_CUBITS];
232
    boolean fromDMESH = (stream!=null && meshState==MESH_NICE);
233
    createMeshAndCubits(stream,meshState,fromDMESH);
234
    setUpTextures(fromDMESH,fromJSON);
235
    createDataStructuresForSolved(mNumLayers);
236

    
237
    mEffects = new DistortedEffects();
238

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

    
246
    mEffects.apply(mRotateEffect);
247
    mEffects.apply(quatEffect);
248
    mEffects.apply(scaleEffect);
249
    mEffects.apply(moveEffect);
250

    
251
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
257
    {
258
    mTexture = new DistortedTexture();
259

    
260
    if( !fromJSON )
261
      {
262
      FactoryCubit factory = FactoryCubit.getInstance();
263
      mStickerCoords   = factory.getOuterStickerCoords();
264
      mStickerVariants = factory.getStickerVariants();
265
      mStickerScales   = factory.getOuterStickerScales();
266
      mNumStickerTypes = mStickerCoords.length;
267
      }
268
    else
269
      {
270
      mNumStickerTypes = getNumStickerTypes();
271
      }
272

    
273
    adjustStickerCoords();
274

    
275
    NUM_TEXTURES = NUM_FACE_COLORS*mNumStickerTypes;
276
    mNumTexCols = NUM_STICKERS_IN_ROW;
277
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
278
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
279

    
280
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
281
    setTexture();
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  private Static3D getPos(float[] origPos)
287
    {
288
    int len = origPos.length/3;
289
    float sumX = 0.0f;
290
    float sumY = 0.0f;
291
    float sumZ = 0.0f;
292

    
293
    for(int i=0; i<len; i++)
294
      {
295
      sumX += origPos[3*i  ];
296
      sumY += origPos[3*i+1];
297
      sumZ += origPos[3*i+2];
298
      }
299

    
300
    sumX /= len;
301
    sumY /= len;
302
    sumZ /= len;
303

    
304
    return new Static3D(sumX,sumY,sumZ);
305
    }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
  private void createMeshAndCubits(InputStream stream, int meshState, boolean fromDMESH)
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);
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
      FactoryCubit factory = FactoryCubit.getInstance();
347
      factory.printStickerCoords();
348
      }
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int meshState)
354
    {
355
    int variant = getCubitVariant(cubit,numLayers);
356

    
357
    if( mMeshes==null )
358
      {
359
      FactoryCubit factory = FactoryCubit.getInstance();
360
      factory.clear();
361
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
362
      }
363

    
364
    if( mMeshes[variant]==null )
365
      {
366
      ObjectShape shape = getObjectShape(variant);
367
      FactoryCubit factory = FactoryCubit.getInstance();
368
      factory.createNewFaceTransform(shape);
369
      mMeshes[variant] = factory.createRoundedSolid(shape,meshState);
370
      }
371

    
372
    MeshBase mesh = mMeshes[variant].copy(true);
373
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
374
    mesh.apply(quat,0xffffffff,0);
375

    
376
    return mesh;
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  private void createDataStructuresForSolved(int[] numLayers)
382
    {
383
    mTmpQuats = new int[NUM_QUATS];
384
    mSolvedQuats = new int[NUM_CUBITS][];
385

    
386
    for(int c=0; c<NUM_CUBITS; c++)
387
      {
388
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
389
      }
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  private int getMultQuat(int index1, int index2)
395
    {
396
    if( mQuatMult==null )
397
      {
398
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
399

    
400
      for(int i=0; i<NUM_QUATS; i++)
401
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
402
      }
403

    
404
    if( mQuatMult[index1][index2]==-1 )
405
      {
406
      mQuatMult[index1][index2] = mulQuat(index1,index2);
407
      }
408

    
409
    return mQuatMult[index1][index2];
410
    }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413
// This is used to build internal data structures for the generic 'isSolved()'
414
//
415
// if this is an internal cubit (all faces black): return -1
416
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
417
// Color index, i.e. the index into the 'FACE_COLORS' table.
418
// else (edge or corner cubit, more than one non-black face): return -2.
419

    
420
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
421
    {
422
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
423
    int variant = getCubitVariant(cubit,numLayers);
424

    
425
    for(int face=0; face<mNumCubitFaces; face++)
426
      {
427
      varColor = getVariantFaceColor(variant,face,numLayers);
428
      cubColor = getCubitFaceColor(cubit,face,numLayers);
429

    
430
      if( varColor>=0 && cubColor>=0 )
431
        {
432
        numNonBlack++;
433
        nonBlackIndex = cubColor;
434
        }
435
      }
436

    
437
    if( numNonBlack==0 ) return -1;
438
    if( numNonBlack>=2 ) return -2;
439

    
440
    return nonBlackIndex;
441
    }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
  public boolean shouldResetTextureMaps()
446
    {
447
    return false;
448
    }
449

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

    
452
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
453
    {
454
    final float MAXD = 0.0001f;
455
    float x = faceAx.get0();
456
    float y = faceAx.get1();
457
    float z = faceAx.get2();
458
    float a,dx,dy,dz,qx,qy,qz;
459
    Static4D quat;
460

    
461
    int len = quats.length;
462
    int place = 0;
463

    
464
    for(int q=1; q<len; q++)
465
      {
466
      quat = quats[q];
467
      qx = quat.get0();
468
      qy = quat.get1();
469
      qz = quat.get2();
470

    
471
           if( x!=0.0f ) { a = qx/x; }
472
      else if( y!=0.0f ) { a = qy/y; }
473
      else               { a = qz/z; }
474

    
475
      dx = a*x-qx;
476
      dy = a*y-qy;
477
      dz = a*z-qz;
478

    
479
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
480
        {
481
        mTmpQuats[place++] = q;
482
        }
483
      }
484

    
485
    if( place!=0 )
486
      {
487
      int[] ret = new int[place];
488
      System.arraycopy(mTmpQuats,0,ret,0,place);
489
      return ret;
490
      }
491

    
492
    return null;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
  private boolean isSolved0()
498
    {
499
    int len, q1,q = CUBITS[0].mQuatIndex;
500
    int[] solved;
501
    boolean skip;
502

    
503
    for(int c=1; c<NUM_CUBITS; c++)
504
      {
505
      q1 = CUBITS[c].mQuatIndex;
506

    
507
      if( q1==q ) continue;
508

    
509
      skip = false;
510
      solved = mSolvedQuats[c];
511
      len = solved==null ? 0:solved.length;
512

    
513
      for(int i=0; i<len; i++)
514
        {
515
        if( q1==getMultQuat(q,solved[i]) )
516
          {
517
          skip = true;
518
          break;
519
          }
520
        }
521

    
522
      if( !skip ) return false;
523
      }
524

    
525
    return true;
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  private int computeScramble(int quatNum, int centerNum)
531
    {
532
    float MAXDIFF = 0.01f;
533
    float[] center= mOrigPos[centerNum];
534
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
535
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
536

    
537
    float x = result.get0();
538
    float y = result.get1();
539
    float z = result.get2();
540

    
541
    for(int c=0; c<NUM_CUBITS; c++)
542
      {
543
      float[] cent = mOrigPos[c];
544

    
545
      float qx = cent[0] - x;
546
      float qy = cent[1] - y;
547
      float qz = cent[2] - z;
548

    
549
      if( qx>-MAXDIFF && qx<MAXDIFF &&
550
          qy>-MAXDIFF && qy<MAXDIFF &&
551
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
552
      }
553

    
554
    return -1;
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558
// Dino4 uses this. It is solved if and only if groups of cubits
559
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
560
// or
561
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
562
// are all the same color.
563

    
564
  private boolean isSolved1()
565
    {
566
    if( mScramble==null )
567
      {
568
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
569
      mColors   = new int[NUM_CUBITS];
570

    
571
      for(int q=0; q<NUM_QUATS; q++)
572
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
573
      }
574

    
575
    if( mFaceMap==null )
576
      {
577
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
578
      }
579

    
580
    for(int c=0; c<NUM_CUBITS; c++)
581
      {
582
      int index = mScramble[CUBITS[c].mQuatIndex][c];
583
      mColors[index] = mFaceMap[c];
584
      }
585

    
586
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
587
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
588
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
589

    
590
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
591
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
592
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
593

    
594
    return false;
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598
// Dino6 uses this. It is solved if and only if:
599
//
600
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
601
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
602
// by the same qZ, and then either:
603
//
604
// a) qX = qY = qZ
605
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
606
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
607
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
608
//
609
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
610
//
611
// X cubits: 0, 2, 8, 10
612
// Y cubits: 1, 3, 9, 11
613
// Z cubits: 4, 5, 6, 7
614

    
615
  private boolean isSolved2()
616
    {
617
    int qX = CUBITS[0].mQuatIndex;
618
    int qY = CUBITS[1].mQuatIndex;
619
    int qZ = CUBITS[4].mQuatIndex;
620

    
621
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
622
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
623
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
624
      {
625
      return false;
626
      }
627

    
628
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
629
    }
630

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632
// Square-2 is solved iff
633
// a) all of its cubits are rotated with the same quat
634
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
635
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
636
// and all the 12 left and right edges and corners also with the same quat multiplied by
637
// QUATS[12] - i.e. also upside down.
638

    
639
  private boolean isSolved3()
640
    {
641
    int index = CUBITS[0].mQuatIndex;
642

    
643
    if( CUBITS[1].mQuatIndex!=index ) return false;
644

    
645
    boolean solved = true;
646

    
647
    for(int i=2; i<NUM_CUBITS; i++)
648
      {
649
      if( CUBITS[i].mQuatIndex!=index )
650
        {
651
        solved = false;
652
        break;
653
        }
654
      }
655

    
656
    if( solved ) return true;
657

    
658
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
659
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
660

    
661
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
662
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
663
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
664
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
665

    
666
    return true;
667
    }
668

    
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670

    
671
  int computeRow(float[] pos, int axisIndex)
672
    {
673
    int ret=0;
674
    int len = pos.length / 3;
675
    Static3D axis = mAxis[axisIndex];
676
    float axisX = axis.get0();
677
    float axisY = axis.get1();
678
    float axisZ = axis.get2();
679
    float casted;
680

    
681
    for(int i=0; i<len; i++)
682
      {
683
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
684
      ret |= computeSingleRow(axisIndex,casted);
685
      }
686

    
687
    return ret;
688
    }
689

    
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691

    
692
  private int computeSingleRow(int axisIndex,float casted)
693
    {
694
    int num = mNumCuts[axisIndex];
695

    
696
    for(int i=0; i<num; i++)
697
      {
698
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
699
      }
700

    
701
    return (1<<num);
702
    }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705

    
706
  private boolean wasRotateApplied()
707
    {
708
    return mEffects.exists(mRotateEffect.getID());
709
    }
710

    
711
///////////////////////////////////////////////////////////////////////////////////////////////////
712

    
713
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
714
    {
715
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
716
    }
717

    
718
///////////////////////////////////////////////////////////////////////////////////////////////////
719
// note the minus in front of the sin() - we rotate counterclockwise
720
// when looking towards the direction where the axis increases in values.
721

    
722
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
723
    {
724
    Static3D axis = mAxis[axisIndex];
725

    
726
    while( angleInDegrees<0 ) angleInDegrees += 360;
727
    angleInDegrees %= 360;
728
    
729
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
730
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
731

    
732
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
733
    }
734

    
735
///////////////////////////////////////////////////////////////////////////////////////////////////
736

    
737
  private synchronized void setupPosition(int[][] moves)
738
    {
739
    if( moves!=null )
740
      {
741
      Static4D quat;
742
      int index, axis, rowBitmap, angle;
743
      int[] basic = getBasicAngle();
744

    
745
      for(int[] move: moves)
746
        {
747
        axis     = move[0];
748
        rowBitmap= move[1];
749
        angle    = move[2]*(360/basic[axis]);
750
        quat     = makeQuaternion(axis,angle);
751

    
752
        for(int j=0; j<NUM_CUBITS; j++)
753
          if( belongsToRotation(j,axis,rowBitmap) )
754
            {
755
            index = CUBITS[j].removeRotationNow(quat);
756
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
757
            }
758
        }
759
      }
760
    }
761

    
762
///////////////////////////////////////////////////////////////////////////////////////////////////
763

    
764
  public int getScrambleType()
765
    {
766
    return 0;
767
    }
768

    
769
///////////////////////////////////////////////////////////////////////////////////////////////////
770

    
771
  int computeBitmapFromRow(int rowBitmap, int axis)
772
    {
773
    if( mIsBandaged )
774
      {
775
      int bitmap, initBitmap=0;
776

    
777
      while( initBitmap!=rowBitmap )
778
        {
779
        initBitmap = rowBitmap;
780

    
781
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
782
          {
783
          bitmap = CUBITS[cubit].getRotRow(axis);
784
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
785
          }
786
        }
787
      }
788

    
789
    return rowBitmap;
790
    }
791

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

    
796
  void clampPos(float[] pos, int offset)
797
    {
798
    float currError, minError = Float.MAX_VALUE;
799
    int minErrorIndex1 = -1;
800
    int minErrorIndex2 = -1;
801

    
802
    float x = pos[offset  ];
803
    float y = pos[offset+1];
804
    float z = pos[offset+2];
805

    
806
    float xo,yo,zo;
807

    
808
    for(int i=0; i<NUM_CUBITS; i++)
809
      {
810
      int len = mOrigPos[i].length / 3;
811

    
812
      for(int j=0; j<len; j++)
813
        {
814
        xo = mOrigPos[i][3*j  ];
815
        yo = mOrigPos[i][3*j+1];
816
        zo = mOrigPos[i][3*j+2];
817

    
818
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
819

    
820
        if( currError<minError )
821
          {
822
          minError = currError;
823
          minErrorIndex1 = i;
824
          minErrorIndex2 = j;
825
          }
826
        }
827
      }
828

    
829
    if( minError< 0.1f ) // TODO: 0.1 ?
830
      {
831
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
832
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
833
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
834
      }
835
    }
836

    
837
///////////////////////////////////////////////////////////////////////////////////////////////////
838
// remember about the double cover or unit quaternions!
839

    
840
  int mulQuat(int q1, int q2)
841
    {
842
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
843

    
844
    float rX = result.get0();
845
    float rY = result.get1();
846
    float rZ = result.get2();
847
    float rW = result.get3();
848

    
849
    final float MAX_ERROR = 0.1f;
850
    float dX,dY,dZ,dW;
851

    
852
    for(int i=0; i<NUM_QUATS; i++)
853
      {
854
      dX = OBJECT_QUATS[i].get0() - rX;
855
      dY = OBJECT_QUATS[i].get1() - rY;
856
      dZ = OBJECT_QUATS[i].get2() - rZ;
857
      dW = OBJECT_QUATS[i].get3() - rW;
858

    
859
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
860
          dY<MAX_ERROR && dY>-MAX_ERROR &&
861
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
862
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
863

    
864
      dX = OBJECT_QUATS[i].get0() + rX;
865
      dY = OBJECT_QUATS[i].get1() + rY;
866
      dZ = OBJECT_QUATS[i].get2() + rZ;
867
      dW = OBJECT_QUATS[i].get3() + rW;
868

    
869
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
870
          dY<MAX_ERROR && dY>-MAX_ERROR &&
871
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
872
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
873
      }
874

    
875
    return -1;
876
    }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879

    
880
  private float getAngle()
881
    {
882
    int pointNum = mRotationAngle.getNumPoints();
883

    
884
    if( pointNum>=1 )
885
      {
886
      return mRotationAngle.getPoint(pointNum-1).get0();
887
      }
888
    else
889
      {
890
      mInterface.reportProblem("points in RotationAngle: "+pointNum, false);
891
      return 0;
892
      }
893
    }
894

    
895
///////////////////////////////////////////////////////////////////////////////////////////////////
896

    
897
  void setLibInterface(ObjectLibInterface inter)
898
    {
899
    mInterface = inter;
900
    }
901

    
902
///////////////////////////////////////////////////////////////////////////////////////////////////
903

    
904
  void initializeObject(int[][] moves)
905
    {
906
    solve();
907
    setupPosition(moves);
908
    }
909

    
910
///////////////////////////////////////////////////////////////////////////////////////////////////
911

    
912
  synchronized void removeRotationNow()
913
    {
914
    float angle = getAngle();
915
    double nearestAngleInRadians = angle*Math.PI/180;
916
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
917
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
918
    float axisX = mAxis[mCurrentRotAxis].get0();
919
    float axisY = mAxis[mCurrentRotAxis].get1();
920
    float axisZ = mAxis[mCurrentRotAxis].get2();
921
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
922

    
923
    mRotationAngle.removeAll();
924
    mRotationAngleStatic.set0(0);
925

    
926
    for(int i=0; i<NUM_CUBITS; i++)
927
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
928
        {
929
        int index = CUBITS[i].removeRotationNow(quat);
930
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
931
        }
932
    }
933

    
934
///////////////////////////////////////////////////////////////////////////////////////////////////
935

    
936
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
937
    {
938
    if( wasRotateApplied() )
939
      {
940
      float angle = getAngle();
941
      mRotationAngleStatic.set0(angle);
942
      mRotationAngleFinal.set0(nearestAngleInDegrees);
943
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
944

    
945
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
946
      mRotationAngle.resetToBeginning();
947
      mRotationAngle.removeAll();
948
      mRotationAngle.add(mRotationAngleStatic);
949
      mRotationAngle.add(mRotationAngleMiddle);
950
      mRotationAngle.add(mRotationAngleFinal);
951
      mRotateEffect.notifyWhenFinished(listener);
952

    
953
      return mRotateEffect.getID();
954
      }
955

    
956
    return 0;
957
    }
958

    
959
///////////////////////////////////////////////////////////////////////////////////////////////////
960

    
961
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
962
    {
963
    if( wasRotateApplied() )
964
      {
965
      mCurrentRotAxis = axis;
966
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
967

    
968
      mRotationAngleStatic.set0(0.0f);
969
      mRotationAxis.set( mAxis[axis] );
970
      mRotationAngle.setDuration(durationMillis);
971
      mRotationAngle.resetToBeginning();
972
      mRotationAngle.add(new Static1D(0));
973
      mRotationAngle.add(new Static1D(angle));
974
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
975
      mRotateEffect.notifyWhenFinished(listener);
976

    
977
      return mRotateEffect.getID();
978
      }
979

    
980
    return 0;
981
    }
982

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

    
985
  void continueRotation(float angleInDegrees)
986
    {
987
    mRotationAngleStatic.set0(angleInDegrees);
988
    }
989

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

    
992
  synchronized void beginNewRotation(int axis, int row )
993
    {
994
    if( axis<0 || axis>=NUM_AXIS )
995
      {
996
      android.util.Log.e("object", "invalid rotation axis: "+axis);
997
      return;
998
      }
999
    if( row<0 || row>=mNumLayers[axis] )
1000
      {
1001
      android.util.Log.e("object", "invalid rotation row: "+row);
1002
      return;
1003
      }
1004

    
1005
    mCurrentRotAxis = axis;
1006
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1007
    mRotationAngleStatic.set0(0.0f);
1008
    mRotationAxis.set( mAxis[axis] );
1009
    mRotationAngle.add(mRotationAngleStatic);
1010
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
1011
    }
1012

    
1013
///////////////////////////////////////////////////////////////////////////////////////////////////
1014

    
1015
  void setTextureMap(int cubit, int face, int newColor)
1016
    {
1017
    final float ratioW = 1.0f/mNumTexCols;
1018
    final float ratioH = 1.0f/mNumTexRows;
1019
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1020
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1021
    int col = newColor%mNumTexCols;
1022

    
1023
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1024
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1025
    }
1026

    
1027
///////////////////////////////////////////////////////////////////////////////////////////////////
1028

    
1029
  void resetAllTextureMaps()
1030
    {
1031
    final float ratioW = 1.0f/mNumTexCols;
1032
    final float ratioH = 1.0f/mNumTexRows;
1033
    int cubColor, varColor, color, variant, row, col;
1034

    
1035
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1036
      {
1037
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1038
      variant = getCubitVariant(cubit,mNumLayers);
1039

    
1040
      for(int face=0; face<mNumCubitFaces; face++)
1041
        {
1042
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1043
        varColor = getVariantFaceColor(variant,face,mNumLayers);
1044
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1045
        row      = (mNumTexRows-1) - color/mNumTexCols;
1046
        col      = color%mNumTexCols;
1047

    
1048
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1049
        }
1050

    
1051
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1052
      }
1053
    }
1054

    
1055
///////////////////////////////////////////////////////////////////////////////////////////////////
1056

    
1057
  void releaseResources()
1058
    {
1059
    mTexture.markForDeletion();
1060
    mMesh.markForDeletion();
1061
    mEffects.markForDeletion();
1062

    
1063
    for(int j=0; j<NUM_CUBITS; j++)
1064
      {
1065
      CUBITS[j].releaseResources();
1066
      }
1067
    }
1068

    
1069
///////////////////////////////////////////////////////////////////////////////////////////////////
1070

    
1071
  synchronized void restorePreferences(SharedPreferences preferences)
1072
    {
1073
    boolean error = false;
1074

    
1075
    for(int i=0; i<NUM_CUBITS; i++)
1076
      {
1077
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1078

    
1079
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1080
        {
1081
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1082
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1083
        }
1084
      else
1085
        {
1086
        error = true;
1087
        }
1088
      }
1089

    
1090
    if( error )
1091
      {
1092
      for(int i=0; i<NUM_CUBITS; i++)
1093
        {
1094
        CUBITS[i].solve();
1095
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1096
        }
1097
      }
1098
    }
1099

    
1100
///////////////////////////////////////////////////////////////////////////////////////////////////
1101

    
1102
  void savePreferences(SharedPreferences.Editor editor)
1103
    {
1104
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1105
    }
1106

    
1107
///////////////////////////////////////////////////////////////////////////////////////////////////
1108

    
1109
  private float computeRadiusCorrection(float[] sticker, int curr, int len)
1110
    {
1111
    final float A = 0.8f;  // 0<A<1
1112

    
1113
    int prev = curr>0 ? curr-1 : len-1;
1114
    int next = curr<len-1 ? curr+1 : 0;
1115

    
1116
    float v1x = sticker[2*prev  ]-sticker[2*curr  ];
1117
    float v1y = sticker[2*prev+1]-sticker[2*curr+1];
1118
    float v2x = sticker[2*next  ]-sticker[2*curr  ];
1119
    float v2y = sticker[2*next+1]-sticker[2*curr+1];
1120

    
1121
    float len1= v1x*v1x+v1y*v1y;
1122
    float len2= v2x*v2x+v2y*v2y;
1123

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

    
1126
    return 1-A*cos;
1127
    }
1128

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

    
1131
  public ObjectSticker retSticker(int sticker)
1132
    {
1133
    if( mStickers==null )
1134
      {
1135
      float rad = getStickerRadius();
1136
      float str = getStickerStroke();
1137
      float[][] angles = getStickerAngles();
1138
      int numStickers = mStickerCoords.length;
1139
      mStickers = new ObjectSticker[numStickers];
1140

    
1141
      for(int s=0; s<numStickers; s++)
1142
        {
1143
        float scale = mStickerScales[s];
1144
        float radius = rad / scale;
1145
        float stroke = str / scale;
1146
        int len = mStickerCoords[s].length/2;
1147
        float[] radii = new float[len];
1148
        for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
1149
        mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
1150
        }
1151
      }
1152

    
1153
    return mStickers[sticker];
1154
    }
1155

    
1156
///////////////////////////////////////////////////////////////////////////////////////////////////
1157
// some objects (currently only Kilominx) might want to change the stickers.
1158

    
1159
  public void adjustStickerCoords()
1160
    {
1161

    
1162
    }
1163

    
1164
///////////////////////////////////////////////////////////////////////////////////////////////////
1165
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1166

    
1167
  private void createTexture()
1168
    {
1169
    Paint paint = new Paint();
1170
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1171
    Canvas canvas = new Canvas(mBitmap);
1172

    
1173
    paint.setAntiAlias(true);
1174
    paint.setTextAlign(Paint.Align.CENTER);
1175
    paint.setStyle(Paint.Style.FILL);
1176

    
1177
    paint.setColor(COLOR_INTERNAL);
1178
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1179

    
1180
    int texture = 0;
1181
    FactorySticker factory = FactorySticker.getInstance();
1182

    
1183
    for(int row=0; row<mNumTexRows; row++)
1184
      for(int col=0; col<mNumTexCols; col++)
1185
        {
1186
        if( texture>=NUM_TEXTURES ) break;
1187
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1188
        int color = getColor(texture%NUM_FACE_COLORS);
1189
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1190
        texture++;
1191
        }
1192
    }
1193

    
1194
///////////////////////////////////////////////////////////////////////////////////////////////////
1195

    
1196
  void setTexture()
1197
    {
1198
    if( mBitmap==null ) createTexture();
1199

    
1200
    if( !mTexture.setTexture(mBitmap) )
1201
      {
1202
      int max = DistortedLibrary.getMaxTextureSize();
1203
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1204
      }
1205
    }
1206

    
1207
///////////////////////////////////////////////////////////////////////////////////////////////////
1208

    
1209
  void setObjectRatioNow(float sc, int nodeSize)
1210
    {
1211
    mObjectScreenRatio = sc;
1212
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1213
    mObjectScale.set(scale,scale,scale);
1214

    
1215
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1216
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1217
    }
1218

    
1219
///////////////////////////////////////////////////////////////////////////////////////////////////
1220

    
1221
  void setObjectRatio(float sizeChange, int nodeSize)
1222
    {
1223
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1224

    
1225
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1226
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1227

    
1228
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1229
    }
1230

    
1231
///////////////////////////////////////////////////////////////////////////////////////////////////
1232

    
1233
  void setNodeSize(int nodeSize)
1234
    {
1235
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1236
    }
1237

    
1238
///////////////////////////////////////////////////////////////////////////////////////////////////
1239

    
1240
  public float getRatio()
1241
    {
1242
    return mObjectScreenRatio;
1243
    }
1244

    
1245
///////////////////////////////////////////////////////////////////////////////////////////////////
1246

    
1247
  public float getObjectRatio()
1248
    {
1249
    return mObjectScreenRatio*mInitScreenRatio;
1250
    }
1251

    
1252
///////////////////////////////////////////////////////////////////////////////////////////////////
1253

    
1254
  boolean isSolved()
1255
    {
1256
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1257
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1258
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1259
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1260

    
1261
    return false;
1262
    }
1263

    
1264
///////////////////////////////////////////////////////////////////////////////////////////////////
1265

    
1266
  int computeNearestAngle(int axis, float angle, float speed)
1267
    {
1268
    int[] basicArray = getBasicAngle();
1269
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1270
    int nearestAngle = 360/basicAngle;
1271

    
1272
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1273
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1274

    
1275
    if( tmp!=0 ) return nearestAngle*tmp;
1276

    
1277
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1278
    }
1279

    
1280
///////////////////////////////////////////////////////////////////////////////////////////////////
1281
// INTERNAL API - those are called from 'effects' package
1282
///////////////////////////////////////////////////////////////////////////////////////////////////
1283

    
1284
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1285
    {
1286
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1287
    }
1288

    
1289
///////////////////////////////////////////////////////////////////////////////////////////////////
1290

    
1291
  public Static4D getRotationQuat()
1292
    {
1293
    return mQuat;
1294
    }
1295

    
1296
///////////////////////////////////////////////////////////////////////////////////////////////////
1297

    
1298
  public float getSize()
1299
    {
1300
    return mSize;
1301
    }
1302

    
1303
///////////////////////////////////////////////////////////////////////////////////////////////////
1304

    
1305
  public void apply(Effect effect, int position)
1306
    {
1307
    mEffects.apply(effect, position);
1308
    }
1309

    
1310
///////////////////////////////////////////////////////////////////////////////////////////////////
1311

    
1312
  public void remove(long effectID)
1313
    {
1314
    mEffects.abortById(effectID);
1315
    }
1316

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

    
1319
  public MeshBase getObjectMesh()
1320
    {
1321
    return mMesh;
1322
    }
1323

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

    
1326
  public DistortedEffects getObjectEffects()
1327
    {
1328
    return mEffects;
1329
    }
1330

    
1331
///////////////////////////////////////////////////////////////////////////////////////////////////
1332
// PUBLIC API
1333
///////////////////////////////////////////////////////////////////////////////////////////////////
1334

    
1335
  public int getCubitFaceColorIndex(int cubit, int face)
1336
    {
1337
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1338

    
1339
    int x = (int)(texMap.get0()/texMap.get2());
1340
    int y = (int)(texMap.get1()/texMap.get3());
1341

    
1342
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1343
    }
1344

    
1345
///////////////////////////////////////////////////////////////////////////////////////////////////
1346

    
1347
  public int[] getNumLayers()
1348
    {
1349
    return mNumLayers;
1350
    }
1351

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

    
1354
  public synchronized void solve()
1355
    {
1356
    for(int i=0; i<NUM_CUBITS; i++)
1357
      {
1358
      CUBITS[i].solve();
1359
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1360
      }
1361
    }
1362

    
1363
///////////////////////////////////////////////////////////////////////////////////////////////////
1364

    
1365
  public int getCubitQuatIndex(int cubit)
1366
    {
1367
    return CUBITS[cubit].mQuatIndex;
1368
    }
1369

    
1370
///////////////////////////////////////////////////////////////////////////////////////////////////
1371

    
1372
  public int getCubitRotRow(int cubit, int axis)
1373
    {
1374
    return CUBITS[cubit].getRotRow(axis);
1375
    }
1376

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

    
1379
  public Bitmap getStickerBitmap()
1380
    {
1381
    return mBitmap;
1382
    }
1383

    
1384
///////////////////////////////////////////////////////////////////////////////////////////////////
1385

    
1386
  public DistortedNode getNode()
1387
    {
1388
    return mNode;
1389
    }
1390

    
1391
///////////////////////////////////////////////////////////////////////////////////////////////////
1392

    
1393
  public ObjectType getObjectType()
1394
    {
1395
    return intGetObjectType(mNumLayers);
1396
    }
1397

    
1398
///////////////////////////////////////////////////////////////////////////////////////////////////
1399

    
1400
  public int getNumStickerTypes()
1401
    {
1402
    return mNumStickerTypes;
1403
    }
1404

    
1405
///////////////////////////////////////////////////////////////////////////////////////////////////
1406

    
1407
  public TouchControl getTouchControl()
1408
    {
1409
    if( mTouchControl==null )
1410
      {
1411
      switch(getTouchControlType())
1412
        {
1413
        case TC_TETRAHEDRON    : mTouchControl = new TouchControlTetrahedron(this);
1414
                                 break;
1415
        case TC_HEXAHEDRON     : mTouchControl = new TouchControlHexahedron(this);
1416
                                 break;
1417
        case TC_OCTAHEDRON     : mTouchControl = new TouchControlOctahedron(this);
1418
                                 break;
1419
        case TC_DODECAHEDRON   : mTouchControl = new TouchControlDodecahedron(this);
1420
                                 break;
1421
        case TC_CUBOID         : int[] numLayers = getNumLayers();
1422
                                 mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1423
                                 break;
1424
        case TC_CHANGING_MIRROR: mTouchControl = new TouchControlMirror(this);
1425
                                 break;
1426
        case TC_CHANGING_SQUARE: mTouchControl = new TouchControlSquare(this);
1427
                                 break;
1428
        }
1429
      }
1430
    return mTouchControl;
1431
    }
1432

    
1433
///////////////////////////////////////////////////////////////////////////////////////////////////
1434

    
1435
  public String getShortName()
1436
    {
1437
    return getObjectType().name();
1438
    }
1439

    
1440
///////////////////////////////////////////////////////////////////////////////////////////////////
1441

    
1442
  protected void setReader(JsonReader reader)
1443
    {
1444
    // empty
1445
    }
1446

    
1447
///////////////////////////////////////////////////////////////////////////////////////////////////
1448

    
1449
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1450

    
1451
  // for JSON only
1452
  public abstract int getSolvedFunctionIndex();
1453
  public abstract int getTouchControlType();
1454
  public abstract int getTouchControlSplit();
1455
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1456
  public abstract int[][][] getEnabled();
1457
  public abstract float[] getDist3D(int[] numLayers);
1458
  public abstract ScrambleState[] getScrambleStates();
1459
  public abstract float[][] getCuts(int[] numLayers);
1460
  public abstract Static4D[] getQuats();
1461
  public abstract float getStickerRadius();
1462
  public abstract float getStickerStroke();
1463
  public abstract float[][] getStickerAngles();
1464
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1465
  public abstract ObjectShape getObjectShape(int variant);
1466
  public abstract int getNumCubitVariants(int[] numLayers);
1467
  public abstract float[][] getCubitPositions(int[] numLayers);
1468
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1469
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1470
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1471
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1472
  public abstract int getNumFaceColors();
1473
  public abstract int getNumCubitFaces();
1474
  public abstract float getScreenRatio();
1475
  public abstract int getColor(int face);
1476

    
1477
  // not only for JSON
1478
  public abstract Static3D[] getRotationAxis();
1479
  public abstract int[] getBasicAngle();
1480
  public abstract int getNumFaces();
1481
  public abstract String getObjectName();
1482
  public abstract String getInventor();
1483
  public abstract int getYearOfInvention();
1484
  public abstract int getComplexity();
1485
  public abstract int getFOV();
1486
  }
(11-11/13)