Project

General

Profile

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

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

1 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 android.content.SharedPreferences;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectQuaternion;
32
import org.distorted.library.effect.VertexEffectRotate;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedNode;
36
import org.distorted.library.main.DistortedTexture;
37 a706f8e0 Leszek Koltunski
import org.distorted.library.main.QuatHelper;
38 29b82486 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
39
import org.distorted.library.mesh.MeshFile;
40
import org.distorted.library.mesh.MeshJoined;
41
import org.distorted.library.message.EffectListener;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46 a57e6870 Leszek Koltunski
47 198c5bf0 Leszek Koltunski
import org.distorted.objectlib.helpers.FactoryCubit;
48
import org.distorted.objectlib.helpers.FactorySticker;
49 d887aa16 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectLibInterface;
50 198c5bf0 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectShape;
51
import org.distorted.objectlib.helpers.ObjectSticker;
52
import org.distorted.objectlib.helpers.ScrambleState;
53 82eb152a Leszek Koltunski
import org.distorted.objectlib.json.JsonReader;
54 29b82486 Leszek Koltunski
55
import java.io.DataInputStream;
56
import java.io.IOException;
57
import java.io.InputStream;
58
import java.util.Random;
59
60 59c20632 Leszek Koltunski
import static org.distorted.objectlib.main.Movement.MOVEMENT_TETRAHEDRON;
61
import static org.distorted.objectlib.main.Movement.MOVEMENT_HEXAHEDRON;
62
import static org.distorted.objectlib.main.Movement.MOVEMENT_OCTAHEDRON;
63
import static org.distorted.objectlib.main.Movement.MOVEMENT_DODECAHEDRON;
64
import static org.distorted.objectlib.main.Movement.MOVEMENT_SHAPECHANGE;
65
66 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 7ba38dd4 Leszek Koltunski
public abstract class TwistyObject
69 29b82486 Leszek Koltunski
  {
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_BLACK  = 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 82eb152a Leszek Koltunski
  protected int NUM_FACE_COLORS;
95
  protected int NUM_TEXTURES;
96
  protected Cubit[] CUBITS;
97 29b82486 Leszek Koltunski
98
  MeshBase[] mMeshes;
99 82eb152a Leszek Koltunski
  Static4D[] OBJECT_QUATS;
100
  int NUM_CUBITS;
101
  int NUM_AXIS;
102
  int NUM_QUATS;
103 332e1fb0 Leszek Koltunski
  int SHIFT;
104 82eb152a Leszek Koltunski
105
  private int mNumCubitFaces;
106
  private Static3D[] mAxis;
107
  private float[][] mCuts;
108
  private int[] mNumCuts;
109
  private float[][] mOrigPos;
110
  private Static4D mQuat;
111 a57e6870 Leszek Koltunski
  private final int[] mNumLayers;
112 59c20632 Leszek Koltunski
  private final float mSize;
113 82eb152a Leszek Koltunski
  private DistortedEffects mEffects;
114
  private VertexEffectRotate mRotateEffect;
115
  private Dynamic1D mRotationAngle;
116
  private Static3D mRotationAxis;
117
  private Static3D mObjectScale;
118
  private int[] mQuatDebug;
119
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
120
  private DistortedTexture mTexture;
121
  private float mInitScreenRatio;
122
  private int mSolvedFunctionIndex;
123
  private boolean mIsBandaged;
124 29b82486 Leszek Koltunski
  private float mObjectScreenRatio;
125
  private int[][] mSolvedQuats;
126
  private int[][] mQuatMult;
127
  private int[] mTmpQuats;
128
  private int mNumTexRows, mNumTexCols;
129
  private int mRotRowBitmap;
130 59c20632 Leszek Koltunski
  private int mCurrentRotAxis;
131 29b82486 Leszek Koltunski
  private MeshBase mMesh;
132 82eb152a Leszek Koltunski
  private TwistyObjectScrambler mScrambler;
133 59c20632 Leszek Koltunski
  private Movement mMovement;
134
  private boolean[][] mLayerRotatable;
135
  private int[][][] mEnabled;
136 7ba38dd4 Leszek Koltunski
  private DistortedNode mNode;
137 d887aa16 Leszek Koltunski
  private ObjectLibInterface mInterface;
138 29b82486 Leszek Koltunski
139
  //////////////////// SOLVED1 ////////////////////////
140
141
  private int[] mFaceMap;
142
  private int[][] mScramble;
143
  private int[] mColors;
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147 82eb152a Leszek Koltunski
  TwistyObject(InputStream jsonStream, Static4D quat, Static3D move, InputStream meshStream)
148
    {
149
    JsonReader reader = JsonReader.getInstance();
150
    reader.parseJsonFile(jsonStream);
151
    setReader(reader);
152
153
    mNumLayers = reader.getNumLayers();
154
    mSize      = reader.getSize();
155
    initialize(quat,move,meshStream);
156
    }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
  TwistyObject(int[] numLayers, float size, Static4D quat, Static3D move, InputStream meshStream)
161 29b82486 Leszek Koltunski
    {
162
    mNumLayers = numLayers;
163 82eb152a Leszek Koltunski
    mSize      = size;
164
    initialize(quat,move,meshStream);
165
    }
166
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169
  private void initialize(Static4D quat, Static3D move, InputStream stream)
170
    {
171
    mQuat = quat;
172 29b82486 Leszek Koltunski
    mOrigPos = getCubitPositions(mNumLayers);
173
    mAxis = getRotationAxis();
174
    mInitScreenRatio = getScreenRatio();
175
    mNumCubitFaces = getNumCubitFaces();
176
    mSolvedFunctionIndex = getSolvedFunctionIndex();
177
178 332e1fb0 Leszek Koltunski
    int numAxis = mAxis.length;
179
    SHIFT = -1;
180
181 29b82486 Leszek Koltunski
    mCuts = getCuts(mNumLayers);
182 332e1fb0 Leszek Koltunski
    mNumCuts = new int[numAxis];
183
    for(int i=0; i<numAxis; i++)
184 dfdb26a9 Leszek Koltunski
      {
185 332e1fb0 Leszek Koltunski
      if( SHIFT<mNumLayers[i] ) SHIFT = mNumLayers[i];
186 dfdb26a9 Leszek Koltunski
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
187
      }
188 29b82486 Leszek Koltunski
189
    OBJECT_QUATS = getQuats();
190
    NUM_CUBITS  = mOrigPos.length;
191
    NUM_FACE_COLORS = getNumFaceColors();
192
    NUM_TEXTURES = getNumStickerTypes(mNumLayers)*NUM_FACE_COLORS;
193
    NUM_AXIS = mAxis.length;
194
    NUM_QUATS = OBJECT_QUATS.length;
195
196
    int scramblingType = getScrambleType();
197
    ScrambleState[] states = getScrambleStates();
198 82eb152a Leszek Koltunski
    mScrambler = new TwistyObjectScrambler(scramblingType,NUM_AXIS,mNumLayers,states);
199 29b82486 Leszek Koltunski
200
    boolean bandaged=false;
201
202
    for(int c=0; c<NUM_CUBITS; c++)
203
      {
204
      if( mOrigPos[c].length>3 )
205
        {
206
        bandaged=true;
207
        break;
208
        }
209
      }
210
211
    mIsBandaged = bandaged;
212
    mQuatDebug = new int[NUM_CUBITS];
213
214
    mRotationAngle= new Dynamic1D();
215
    mRotationAxis = new Static3D(1,0,0);
216
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
217
218
    mRotationAngleStatic = new Static1D(0);
219
    mRotationAngleMiddle = new Static1D(0);
220
    mRotationAngleFinal  = new Static1D(0);
221
222 e7daa161 Leszek Koltunski
    mObjectScale = new Static3D(1,1,1);
223 7ba38dd4 Leszek Koltunski
    setObjectRatioNow(1.0f,720);
224 e7daa161 Leszek Koltunski
225 29b82486 Leszek Koltunski
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
226 82eb152a Leszek Koltunski
    MatrixEffectQuaternion quatEffect  = new MatrixEffectQuaternion(mQuat, CENTER);
227 b80e6524 Leszek Koltunski
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
228 29b82486 Leszek Koltunski
229
    mNumTexCols = NUM_STICKERS_IN_ROW;
230
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
231
232
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
233
234
    CUBITS = new Cubit[NUM_CUBITS];
235 82eb152a Leszek Koltunski
    createMeshAndCubits(stream);
236
    createDataStructuresForSolved(mNumLayers);
237 29b82486 Leszek Koltunski
238
    mTexture = new DistortedTexture();
239
    mEffects = new DistortedEffects();
240
241 9a316d92 Leszek Koltunski
    createTexture();
242
243 29b82486 Leszek Koltunski
    for(int q=0; q<NUM_QUATS; q++)
244
      {
245
      VertexEffectQuaternion vq = new VertexEffectQuaternion(OBJECT_QUATS[q],CENTER);
246
      vq.setMeshAssociation(0,q);
247
      mEffects.apply(vq);
248
      }
249
250
    mEffects.apply(mRotateEffect);
251
    mEffects.apply(quatEffect);
252
    mEffects.apply(scaleEffect);
253 b80e6524 Leszek Koltunski
    mEffects.apply(moveEffect);
254 29b82486 Leszek Koltunski
255 7ba38dd4 Leszek Koltunski
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
256 29b82486 Leszek Koltunski
    }
257
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260
  private Static3D getPos(float[] origPos)
261
    {
262
    int len = origPos.length/3;
263
    float sumX = 0.0f;
264
    float sumY = 0.0f;
265
    float sumZ = 0.0f;
266
267
    for(int i=0; i<len; i++)
268
      {
269
      sumX += origPos[3*i  ];
270
      sumY += origPos[3*i+1];
271
      sumZ += origPos[3*i+2];
272
      }
273
274
    sumX /= len;
275
    sumY /= len;
276
    sumZ /= len;
277
278
    return new Static3D(sumX,sumY,sumZ);
279
    }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283 82eb152a Leszek Koltunski
  private void createMeshAndCubits(InputStream stream)
284 29b82486 Leszek Koltunski
    {
285 82eb152a Leszek Koltunski
    if( stream!=null )
286 29b82486 Leszek Koltunski
      {
287 82eb152a Leszek Koltunski
      DataInputStream dos = new DataInputStream(stream);
288 29b82486 Leszek Koltunski
      mMesh = new MeshFile(dos);
289
290
      try
291
        {
292 82eb152a Leszek Koltunski
        stream.close();
293 29b82486 Leszek Koltunski
        }
294
      catch(IOException e)
295
        {
296
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
297
        }
298
299
      for(int i=0; i<NUM_CUBITS; i++)
300
        {
301
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
302
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
303
        }
304
305
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
306
      }
307
    else
308
      {
309
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
310
311
      for(int i=0; i<NUM_CUBITS; i++)
312
        {
313
        CUBITS[i] = new Cubit(this,mOrigPos[i], NUM_AXIS);
314
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
315
        Static3D pos = getPos(mOrigPos[i]);
316
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
317
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
318
        }
319
320
      mMesh = new MeshJoined(cubitMesh);
321
      resetAllTextureMaps();
322
      }
323
    }
324
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
327 a57e6870 Leszek Koltunski
  private MeshBase createCubitMesh(int cubit, int[] numLayers)
328 29b82486 Leszek Koltunski
    {
329
    int variant = getCubitVariant(cubit,numLayers);
330
331
    if( mMeshes==null )
332
      {
333
      FactoryCubit factory = FactoryCubit.getInstance();
334
      factory.clear();
335
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
336
      }
337
338
    if( mMeshes[variant]==null )
339
      {
340 e30c522a Leszek Koltunski
      ObjectShape shape = getObjectShape(variant);
341 29b82486 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
342
      factory.createNewFaceTransform(shape);
343
      mMeshes[variant] = factory.createRoundedSolid(shape);
344
      }
345
346
    MeshBase mesh = mMeshes[variant].copy(true);
347
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
348
    mesh.apply(quat,0xffffffff,0);
349
350
    return mesh;
351
    }
352
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355 a57e6870 Leszek Koltunski
  private void createDataStructuresForSolved(int[] numLayers)
356 29b82486 Leszek Koltunski
    {
357
    mTmpQuats = new int[NUM_QUATS];
358
    mSolvedQuats = new int[NUM_CUBITS][];
359
360
    for(int c=0; c<NUM_CUBITS; c++)
361
      {
362
      mSolvedQuats[c] = getSolvedQuats(c,numLayers);
363
      }
364
    }
365
366 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
367
368
  private int getMultQuat(int index1, int index2)
369
    {
370
    if( mQuatMult==null )
371
      {
372
      mQuatMult = new int[NUM_QUATS][NUM_QUATS];
373
374
      for(int i=0; i<NUM_QUATS; i++)
375
        for(int j=0; j<NUM_QUATS; j++) mQuatMult[i][j] = -1;
376
      }
377
378
    if( mQuatMult[index1][index2]==-1 )
379
      {
380
      mQuatMult[index1][index2] = mulQuat(index1,index2);
381
      }
382
383
    return mQuatMult[index1][index2];
384
    }
385
386 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
387
// This is used to build internal data structures for the generic 'isSolved()'
388
//
389
// if this is an internal cubit (all faces black): return -1
390
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
391
// Color index, i.e. the index into the 'FACE_COLORS' table.
392
// else (edge or corner cubit, more than one non-black face): return -2.
393
394 a57e6870 Leszek Koltunski
  protected int retCubitSolvedStatus(int cubit, int[] numLayers)
395 29b82486 Leszek Koltunski
    {
396 a75ae1ee Leszek Koltunski
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
397
    int variant = getCubitVariant(cubit,numLayers);
398 29b82486 Leszek Koltunski
399
    for(int face=0; face<mNumCubitFaces; face++)
400
      {
401 a75ae1ee Leszek Koltunski
      varColor = getVariantFaceColor(variant,face,numLayers);
402
      cubColor = getCubitFaceColor(cubit,face,numLayers);
403 29b82486 Leszek Koltunski
404 a75ae1ee Leszek Koltunski
      if( varColor>=0 && cubColor>=0 )
405 29b82486 Leszek Koltunski
        {
406
        numNonBlack++;
407 a75ae1ee Leszek Koltunski
        nonBlackIndex = cubColor;
408 29b82486 Leszek Koltunski
        }
409
      }
410
411
    if( numNonBlack==0 ) return -1;
412
    if( numNonBlack>=2 ) return -2;
413
414
    return nonBlackIndex;
415
    }
416
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418
419 c8bc83d9 Leszek Koltunski
  protected boolean shouldResetTextureMaps()
420 29b82486 Leszek Koltunski
    {
421
    return false;
422
    }
423
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
426 c8bc83d9 Leszek Koltunski
  protected int[] buildSolvedQuats(Static3D faceAx, Static4D[] quats)
427 29b82486 Leszek Koltunski
    {
428
    final float MAXD = 0.0001f;
429
    float x = faceAx.get0();
430
    float y = faceAx.get1();
431
    float z = faceAx.get2();
432
    float a,dx,dy,dz,qx,qy,qz;
433
    Static4D quat;
434
435
    int len = quats.length;
436
    int place = 0;
437
438
    for(int q=1; q<len; q++)
439
      {
440
      quat = quats[q];
441
      qx = quat.get0();
442
      qy = quat.get1();
443
      qz = quat.get2();
444
445
           if( x!=0.0f ) { a = qx/x; }
446
      else if( y!=0.0f ) { a = qy/y; }
447
      else               { a = qz/z; }
448
449
      dx = a*x-qx;
450
      dy = a*y-qy;
451
      dz = a*z-qz;
452
453
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
454
        {
455
        mTmpQuats[place++] = q;
456
        }
457
      }
458
459
    if( place!=0 )
460
      {
461
      int[] ret = new int[place];
462
      System.arraycopy(mTmpQuats,0,ret,0,place);
463
      return ret;
464
      }
465
466
    return null;
467
    }
468
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470
471 c8bc83d9 Leszek Koltunski
  private boolean isSolved0()
472 29b82486 Leszek Koltunski
    {
473
    int len, q1,q = CUBITS[0].mQuatIndex;
474
    int[] solved;
475
    boolean skip;
476
477
    for(int c=1; c<NUM_CUBITS; c++)
478
      {
479
      q1 = CUBITS[c].mQuatIndex;
480
481
      if( q1==q ) continue;
482
483
      skip = false;
484
      solved = mSolvedQuats[c];
485
      len = solved==null ? 0:solved.length;
486
487
      for(int i=0; i<len; i++)
488
        {
489
        if( q1==getMultQuat(q,solved[i]) )
490
          {
491
          skip = true;
492
          break;
493
          }
494
        }
495
496
      if( !skip ) return false;
497
      }
498
499
    return true;
500
    }
501
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
504
  private int computeScramble(int quatNum, int centerNum)
505
    {
506
    float MAXDIFF = 0.01f;
507
    float[] center= mOrigPos[centerNum];
508
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
509
    Static4D result = QuatHelper.rotateVectorByQuat(sc,OBJECT_QUATS[quatNum]);
510
511
    float x = result.get0();
512
    float y = result.get1();
513
    float z = result.get2();
514
515
    for(int c=0; c<NUM_CUBITS; c++)
516
      {
517
      float[] cent = mOrigPos[c];
518
519
      float qx = cent[0] - x;
520
      float qy = cent[1] - y;
521
      float qz = cent[2] - z;
522
523
      if( qx>-MAXDIFF && qx<MAXDIFF &&
524
          qy>-MAXDIFF && qy<MAXDIFF &&
525
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
526
      }
527
528
    return -1;
529
    }
530
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532
// Dino4 uses this. It is solved if and only if groups of cubits
533
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
534
// or
535
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
536
// are all the same color.
537
538 c8bc83d9 Leszek Koltunski
  private boolean isSolved1()
539 29b82486 Leszek Koltunski
    {
540
    if( mScramble==null )
541
      {
542
      mScramble = new int[NUM_QUATS][NUM_CUBITS];
543
      mColors   = new int[NUM_CUBITS];
544
545
      for(int q=0; q<NUM_QUATS; q++)
546
        for(int c=0; c<NUM_CUBITS; c++) mScramble[q][c] = computeScramble(q,c);
547
      }
548
549
    if( mFaceMap==null )
550
      {
551
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
552
      }
553
554
    for(int c=0; c<NUM_CUBITS; c++)
555
      {
556
      int index = mScramble[CUBITS[c].mQuatIndex][c];
557
      mColors[index] = mFaceMap[c];
558
      }
559
560
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
561
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
562
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
563
564
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
565
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
566
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
567
568
    return false;
569
    }
570
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572
// Dino6 uses this. It is solved if and only if:
573
//
574
// All four 'X' cubits (i.e. those whose longest edge goes along the X axis) are rotated
575
// by the same quaternion qX, similarly all four 'Y' cubits by the same qY and all four 'Z'
576
// by the same qZ, and then either:
577
//
578
// a) qX = qY = qZ
579
// b) qY = qX*Q2 and qZ = qX*Q8  (i.e. swap of WHITE and YELLOW faces)
580
// c) qX = qY*Q2 and qZ = qY*Q10 (i.e. swap of BLUE and GREEN faces)
581
// d) qX = qZ*Q8 and qY = qZ*Q10 (i.e. swap of RED and BROWN faces)
582
//
583
// BUT: cases b), c) and d) are really the same - it's all just a mirror image of the original.
584
//
585
// X cubits: 0, 2, 8, 10
586
// Y cubits: 1, 3, 9, 11
587
// Z cubits: 4, 5, 6, 7
588
589 c8bc83d9 Leszek Koltunski
  private boolean isSolved2()
590 29b82486 Leszek Koltunski
    {
591
    int qX = CUBITS[0].mQuatIndex;
592
    int qY = CUBITS[1].mQuatIndex;
593
    int qZ = CUBITS[4].mQuatIndex;
594
595
    if( CUBITS[2].mQuatIndex != qX || CUBITS[8].mQuatIndex != qX || CUBITS[10].mQuatIndex != qX ||
596
        CUBITS[3].mQuatIndex != qY || CUBITS[9].mQuatIndex != qY || CUBITS[11].mQuatIndex != qY ||
597
        CUBITS[5].mQuatIndex != qZ || CUBITS[6].mQuatIndex != qZ || CUBITS[ 7].mQuatIndex != qZ  )
598
      {
599
      return false;
600
      }
601
602
    return ( qX==qY && qX==qZ ) || ( qY==mulQuat(qX,2) && qZ==mulQuat(qX,8) );
603
    }
604
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606
// Square-2 is solved iff
607
// a) all of its cubits are rotated with the same quat
608
// b) its two 'middle' cubits are rotated with the same quat, the 6 'front' and 6 'back'
609
// edges and corners with this quat multiplied by QUATS[18] (i.e. those are upside down)
610
// and all the 12 left and right edges and corners also with the same quat multiplied by
611
// QUATS[12] - i.e. also upside down.
612
613 c8bc83d9 Leszek Koltunski
  private boolean isSolved3()
614 29b82486 Leszek Koltunski
    {
615
    int index = CUBITS[0].mQuatIndex;
616
617
    if( CUBITS[1].mQuatIndex!=index ) return false;
618
619
    boolean solved = true;
620
621
    for(int i=2; i<NUM_CUBITS; i++)
622
      {
623
      if( CUBITS[i].mQuatIndex!=index )
624
        {
625
        solved = false;
626
        break;
627
        }
628
      }
629
630
    if( solved ) return true;
631
632
    int indexX = mulQuat(index,12);  // QUATS[12] = 180deg (1,0,0)
633
    int indexZ = mulQuat(index,18);  // QUATS[18] = 180deg (0,0,1)
634
635
    for(int i= 2; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
636
    for(int i= 3; i<        18; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
637
    for(int i=18; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexX ) return false;
638
    for(int i=19; i<NUM_CUBITS; i+=2) if( CUBITS[i].mQuatIndex != indexZ ) return false;
639
640
    return true;
641
    }
642
643
///////////////////////////////////////////////////////////////////////////////////////////////////
644
645
  int computeRow(float[] pos, int axisIndex)
646
    {
647
    int ret=0;
648
    int len = pos.length / 3;
649
    Static3D axis = mAxis[axisIndex];
650
    float axisX = axis.get0();
651
    float axisY = axis.get1();
652
    float axisZ = axis.get2();
653
    float casted;
654
655
    for(int i=0; i<len; i++)
656
      {
657
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
658
      ret |= computeSingleRow(axisIndex,casted);
659
      }
660
661
    return ret;
662
    }
663
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
666
  private int computeSingleRow(int axisIndex,float casted)
667
    {
668
    int num = mNumCuts[axisIndex];
669
670
    for(int i=0; i<num; i++)
671
      {
672
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
673
      }
674
675
    return (1<<num);
676
    }
677
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679
680
  private boolean wasRotateApplied()
681
    {
682
    return mEffects.exists(mRotateEffect.getID());
683
    }
684
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686
687
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
688
    {
689
    return (CUBITS[cubit].getRotRow(axis) & rowBitmap) != 0;
690
    }
691
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
// note the minus in front of the sin() - we rotate counterclockwise
694
// when looking towards the direction where the axis increases in values.
695
696
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
697
    {
698
    Static3D axis = mAxis[axisIndex];
699
700
    while( angleInDegrees<0 ) angleInDegrees += 360;
701
    angleInDegrees %= 360;
702
    
703
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
704
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
705
706
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
707
    }
708
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710
711
  private synchronized void setupPosition(int[][] moves)
712
    {
713
    if( moves!=null )
714
      {
715
      Static4D quat;
716
      int index, axis, rowBitmap, angle;
717
      int[] basic = getBasicAngle();
718
719
      for(int[] move: moves)
720
        {
721
        axis     = move[0];
722
        rowBitmap= move[1];
723
        angle    = move[2]*(360/basic[axis]);
724
        quat     = makeQuaternion(axis,angle);
725
726
        for(int j=0; j<NUM_CUBITS; j++)
727
          if( belongsToRotation(j,axis,rowBitmap) )
728
            {
729
            index = CUBITS[j].removeRotationNow(quat);
730
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
731
            }
732
        }
733
      }
734
    }
735
736
///////////////////////////////////////////////////////////////////////////////////////////////////
737
738 f9a81f52 Leszek Koltunski
  public int getScrambleType()
739 29b82486 Leszek Koltunski
    {
740
    return 0;
741
    }
742
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744
745
  int computeBitmapFromRow(int rowBitmap, int axis)
746
    {
747
    if( mIsBandaged )
748
      {
749
      int bitmap, initBitmap=0;
750
751
      while( initBitmap!=rowBitmap )
752
        {
753
        initBitmap = rowBitmap;
754
755
        for(int cubit=0; cubit<NUM_CUBITS; cubit++)
756
          {
757
          bitmap = CUBITS[cubit].getRotRow(axis);
758
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
759
          }
760
        }
761
      }
762
763
    return rowBitmap;
764
    }
765
766
///////////////////////////////////////////////////////////////////////////////////////////////////
767
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
768
// Do so only if minimal Error is appropriately low (shape-shifting puzzles - Square-1)
769
770
  void clampPos(float[] pos, int offset)
771
    {
772
    float currError, minError = Float.MAX_VALUE;
773
    int minErrorIndex1 = -1;
774
    int minErrorIndex2 = -1;
775
776
    float x = pos[offset  ];
777
    float y = pos[offset+1];
778
    float z = pos[offset+2];
779
780
    float xo,yo,zo;
781
782
    for(int i=0; i<NUM_CUBITS; i++)
783
      {
784
      int len = mOrigPos[i].length / 3;
785
786
      for(int j=0; j<len; j++)
787
        {
788
        xo = mOrigPos[i][3*j  ];
789
        yo = mOrigPos[i][3*j+1];
790
        zo = mOrigPos[i][3*j+2];
791
792
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
793
794
        if( currError<minError )
795
          {
796
          minError = currError;
797
          minErrorIndex1 = i;
798
          minErrorIndex2 = j;
799
          }
800
        }
801
      }
802
803
    if( minError< 0.1f ) // TODO: 0.1 ?
804
      {
805
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
806
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
807
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
808
      }
809
    }
810
811
///////////////////////////////////////////////////////////////////////////////////////////////////
812
// remember about the double cover or unit quaternions!
813
814
  int mulQuat(int q1, int q2)
815
    {
816
    Static4D result = QuatHelper.quatMultiply(OBJECT_QUATS[q1],OBJECT_QUATS[q2]);
817
818
    float rX = result.get0();
819
    float rY = result.get1();
820
    float rZ = result.get2();
821
    float rW = result.get3();
822
823
    final float MAX_ERROR = 0.1f;
824
    float dX,dY,dZ,dW;
825
826
    for(int i=0; i<NUM_QUATS; i++)
827
      {
828
      dX = OBJECT_QUATS[i].get0() - rX;
829
      dY = OBJECT_QUATS[i].get1() - rY;
830
      dZ = OBJECT_QUATS[i].get2() - rZ;
831
      dW = OBJECT_QUATS[i].get3() - rW;
832
833
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
834
          dY<MAX_ERROR && dY>-MAX_ERROR &&
835
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
836
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
837
838
      dX = OBJECT_QUATS[i].get0() + rX;
839
      dY = OBJECT_QUATS[i].get1() + rY;
840
      dZ = OBJECT_QUATS[i].get2() + rZ;
841
      dW = OBJECT_QUATS[i].get3() + rW;
842
843
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
844
          dY<MAX_ERROR && dY>-MAX_ERROR &&
845
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
846
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
847
      }
848
849
    return -1;
850
    }
851
852 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
853
854
  private float getAngle()
855
    {
856
    int pointNum = mRotationAngle.getNumPoints();
857
858
    if( pointNum>=1 )
859
      {
860
      return mRotationAngle.getPoint(pointNum-1).get0();
861
      }
862
    else
863
      {
864 d887aa16 Leszek Koltunski
      mInterface.reportProblem("points in RotationAngle: "+pointNum);
865 c8bc83d9 Leszek Koltunski
      return 0;
866
      }
867
    }
868
869 d887aa16 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
870
871
  void setLibInterface(ObjectLibInterface inter)
872
    {
873
    mInterface = inter;
874 c8bc83d9 Leszek Koltunski
    }
875
876 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
877 198c5bf0 Leszek Koltunski
878 880beeea Leszek Koltunski
  void initializeObject(int[][] moves)
879 7c111294 Leszek Koltunski
    {
880
    solve();
881
    setupPosition(moves);
882
    }
883 198c5bf0 Leszek Koltunski
884 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
885 198c5bf0 Leszek Koltunski
886 880beeea Leszek Koltunski
  synchronized void removeRotationNow()
887 7c111294 Leszek Koltunski
    {
888
    float angle = getAngle();
889
    double nearestAngleInRadians = angle*Math.PI/180;
890
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
891
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
892 59c20632 Leszek Koltunski
    float axisX = mAxis[mCurrentRotAxis].get0();
893
    float axisY = mAxis[mCurrentRotAxis].get1();
894
    float axisZ = mAxis[mCurrentRotAxis].get2();
895 7c111294 Leszek Koltunski
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
896 198c5bf0 Leszek Koltunski
897 7c111294 Leszek Koltunski
    mRotationAngle.removeAll();
898
    mRotationAngleStatic.set0(0);
899
900
    for(int i=0; i<NUM_CUBITS; i++)
901 59c20632 Leszek Koltunski
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
902 198c5bf0 Leszek Koltunski
        {
903 7c111294 Leszek Koltunski
        int index = CUBITS[i].removeRotationNow(quat);
904
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
905 198c5bf0 Leszek Koltunski
        }
906
    }
907
908 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
909
910 880beeea Leszek Koltunski
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
911 c8bc83d9 Leszek Koltunski
    {
912 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
913
      {
914
      float angle = getAngle();
915
      mRotationAngleStatic.set0(angle);
916
      mRotationAngleFinal.set0(nearestAngleInDegrees);
917
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
918 c8bc83d9 Leszek Koltunski
919 7c111294 Leszek Koltunski
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
920
      mRotationAngle.resetToBeginning();
921
      mRotationAngle.removeAll();
922
      mRotationAngle.add(mRotationAngleStatic);
923
      mRotationAngle.add(mRotationAngleMiddle);
924
      mRotationAngle.add(mRotationAngleFinal);
925
      mRotateEffect.notifyWhenFinished(listener);
926 c8bc83d9 Leszek Koltunski
927 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
928
      }
929 c8bc83d9 Leszek Koltunski
930 7c111294 Leszek Koltunski
    return 0;
931 c8bc83d9 Leszek Koltunski
    }
932
933
///////////////////////////////////////////////////////////////////////////////////////////////////
934
935 880beeea Leszek Koltunski
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
936 c8bc83d9 Leszek Koltunski
    {
937 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
938
      {
939 59c20632 Leszek Koltunski
      mCurrentRotAxis = axis;
940 7c111294 Leszek Koltunski
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
941 c8bc83d9 Leszek Koltunski
942 7c111294 Leszek Koltunski
      mRotationAngleStatic.set0(0.0f);
943
      mRotationAxis.set( mAxis[axis] );
944
      mRotationAngle.setDuration(durationMillis);
945
      mRotationAngle.resetToBeginning();
946
      mRotationAngle.add(new Static1D(0));
947
      mRotationAngle.add(new Static1D(angle));
948 332e1fb0 Leszek Koltunski
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
949 7c111294 Leszek Koltunski
      mRotateEffect.notifyWhenFinished(listener);
950 c8bc83d9 Leszek Koltunski
951 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
952
      }
953
954
    return 0;
955 c8bc83d9 Leszek Koltunski
    }
956
957 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
958
959 880beeea Leszek Koltunski
  void continueRotation(float angleInDegrees)
960
    {
961
    mRotationAngleStatic.set0(angleInDegrees);
962
    }
963
964
///////////////////////////////////////////////////////////////////////////////////////////////////
965
966
  synchronized void beginNewRotation(int axis, int row )
967
    {
968
    if( axis<0 || axis>=NUM_AXIS )
969
      {
970
      android.util.Log.e("object", "invalid rotation axis: "+axis);
971
      return;
972
      }
973 a57e6870 Leszek Koltunski
    if( row<0 || row>=mNumLayers[axis] )
974 880beeea Leszek Koltunski
      {
975
      android.util.Log.e("object", "invalid rotation row: "+row);
976
      return;
977
      }
978
979 59c20632 Leszek Koltunski
    mCurrentRotAxis = axis;
980 880beeea Leszek Koltunski
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
981
    mRotationAngleStatic.set0(0.0f);
982
    mRotationAxis.set( mAxis[axis] );
983
    mRotationAngle.add(mRotationAngleStatic);
984 332e1fb0 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*SHIFT) , -1);
985 880beeea Leszek Koltunski
    }
986
987
///////////////////////////////////////////////////////////////////////////////////////////////////
988
989
  void setTextureMap(int cubit, int face, int newColor)
990 29b82486 Leszek Koltunski
    {
991 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
992
    final float ratioH = 1.0f/mNumTexRows;
993
    final Static4D[] maps = new Static4D[mNumCubitFaces];
994
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
995
    int col = newColor%mNumTexCols;
996
997
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
998
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
999 29b82486 Leszek Koltunski
    }
1000
1001
///////////////////////////////////////////////////////////////////////////////////////////////////
1002
1003 880beeea Leszek Koltunski
  void resetAllTextureMaps()
1004 29b82486 Leszek Koltunski
    {
1005 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
1006
    final float ratioH = 1.0f/mNumTexRows;
1007 a75ae1ee Leszek Koltunski
    int cubColor, varColor, color, variant, row, col;
1008 29b82486 Leszek Koltunski
1009 7c111294 Leszek Koltunski
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
1010 29b82486 Leszek Koltunski
      {
1011 7c111294 Leszek Koltunski
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1012 a75ae1ee Leszek Koltunski
      variant = getCubitVariant(cubit,mNumLayers);
1013 29b82486 Leszek Koltunski
1014 a75ae1ee Leszek Koltunski
      for(int face=0; face<mNumCubitFaces; face++)
1015 7c111294 Leszek Koltunski
        {
1016 a75ae1ee Leszek Koltunski
        cubColor = getCubitFaceColor(cubit,face,mNumLayers);
1017
        varColor = getVariantFaceColor(variant,face,mNumLayers);
1018
        color    = cubColor<0 || varColor<0 ? NUM_TEXTURES : varColor*NUM_FACE_COLORS + cubColor;
1019
        row      = (mNumTexRows-1) - color/mNumTexCols;
1020
        col      = color%mNumTexCols;
1021
1022
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1023 7c111294 Leszek Koltunski
        }
1024 29b82486 Leszek Koltunski
1025 7c111294 Leszek Koltunski
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1026
      }
1027 29b82486 Leszek Koltunski
    }
1028
1029
///////////////////////////////////////////////////////////////////////////////////////////////////
1030
1031 880beeea Leszek Koltunski
  void releaseResources()
1032 29b82486 Leszek Koltunski
    {
1033 7c111294 Leszek Koltunski
    mTexture.markForDeletion();
1034
    mMesh.markForDeletion();
1035
    mEffects.markForDeletion();
1036
1037
    for(int j=0; j<NUM_CUBITS; j++)
1038
      {
1039
      CUBITS[j].releaseResources();
1040
      }
1041 29b82486 Leszek Koltunski
    }
1042
1043
///////////////////////////////////////////////////////////////////////////////////////////////////
1044
1045 880beeea Leszek Koltunski
  synchronized void restorePreferences(SharedPreferences preferences)
1046 29b82486 Leszek Koltunski
    {
1047
    boolean error = false;
1048
1049
    for(int i=0; i<NUM_CUBITS; i++)
1050
      {
1051
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
1052
1053
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<NUM_QUATS)
1054
        {
1055
        CUBITS[i].modifyCurrentPosition(OBJECT_QUATS[mQuatDebug[i]]);
1056
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
1057
        }
1058
      else
1059
        {
1060
        error = true;
1061
        }
1062
      }
1063
1064
    if( error )
1065
      {
1066
      for(int i=0; i<NUM_CUBITS; i++)
1067
        {
1068
        CUBITS[i].solve();
1069
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
1070
        }
1071
      }
1072
    }
1073
1074
///////////////////////////////////////////////////////////////////////////////////////////////////
1075
1076 880beeea Leszek Koltunski
  void savePreferences(SharedPreferences.Editor editor)
1077 29b82486 Leszek Koltunski
    {
1078 7c111294 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
1079 29b82486 Leszek Koltunski
    }
1080
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082 7c111294 Leszek Koltunski
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1083 29b82486 Leszek Koltunski
1084 880beeea Leszek Koltunski
  void createTexture()
1085 29b82486 Leszek Koltunski
    {
1086 7c111294 Leszek Koltunski
    Bitmap bitmap;
1087 29b82486 Leszek Koltunski
1088 7c111294 Leszek Koltunski
    Paint paint = new Paint();
1089
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
1090
    Canvas canvas = new Canvas(bitmap);
1091 29b82486 Leszek Koltunski
1092 7c111294 Leszek Koltunski
    paint.setAntiAlias(true);
1093
    paint.setTextAlign(Paint.Align.CENTER);
1094
    paint.setStyle(Paint.Style.FILL);
1095
1096
    paint.setColor(COLOR_BLACK);
1097
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1098
1099 43a4ccff Leszek Koltunski
    int texture = 0;
1100 7c111294 Leszek Koltunski
    FactorySticker factory = FactorySticker.getInstance();
1101
1102
    for(int row=0; row<mNumTexRows; row++)
1103
      for(int col=0; col<mNumTexCols; col++)
1104 29b82486 Leszek Koltunski
        {
1105 43a4ccff Leszek Koltunski
        if( texture>=NUM_TEXTURES ) break;
1106
        ObjectSticker sticker = retSticker(texture/NUM_FACE_COLORS);
1107
        int color = getColor(texture%NUM_FACE_COLORS);
1108
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT, color, sticker);
1109
        texture++;
1110 29b82486 Leszek Koltunski
        }
1111
1112 7c111294 Leszek Koltunski
    if( !mTexture.setTexture(bitmap) )
1113
      {
1114
      int max = DistortedLibrary.getMaxTextureSize();
1115 d887aa16 Leszek Koltunski
      mInterface.reportProblem("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
1116 29b82486 Leszek Koltunski
      }
1117
    }
1118
1119 ee6bb8d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1120
1121 7ba38dd4 Leszek Koltunski
  void setObjectRatioNow(float sc, int nodeMinSize)
1122 ee6bb8d7 Leszek Koltunski
    {
1123
    mObjectScreenRatio = sc;
1124 59c20632 Leszek Koltunski
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeMinSize/mSize;
1125 ee6bb8d7 Leszek Koltunski
    mObjectScale.set(scale,scale,scale);
1126
    }
1127
1128 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1129
1130 7ba38dd4 Leszek Koltunski
  void setObjectRatio(float sizeChange, int nodeMinSize)
1131 29b82486 Leszek Koltunski
    {
1132 7c111294 Leszek Koltunski
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1133 29b82486 Leszek Koltunski
1134 7c111294 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1135
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1136
1137 7ba38dd4 Leszek Koltunski
    setObjectRatioNow(mObjectScreenRatio, nodeMinSize);
1138 29b82486 Leszek Koltunski
    }
1139
1140
///////////////////////////////////////////////////////////////////////////////////////////////////
1141
1142 880beeea Leszek Koltunski
  float getObjectRatio()
1143 29b82486 Leszek Koltunski
    {
1144 7c111294 Leszek Koltunski
    return mObjectScreenRatio*mInitScreenRatio;
1145
    }
1146 29b82486 Leszek Koltunski
1147 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1148
1149
  public float getRatio()
1150
    {
1151
    return mObjectScreenRatio;
1152
    }
1153
1154 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1155 29b82486 Leszek Koltunski
1156 880beeea Leszek Koltunski
  boolean isSolved()
1157 7c111294 Leszek Koltunski
    {
1158 880beeea Leszek Koltunski
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1159
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1160
    if( mSolvedFunctionIndex==2 ) return isSolved2();
1161
    if( mSolvedFunctionIndex==3 ) return isSolved3();
1162 7c111294 Leszek Koltunski
1163 880beeea Leszek Koltunski
    return false;
1164 29b82486 Leszek Koltunski
    }
1165
1166
///////////////////////////////////////////////////////////////////////////////////////////////////
1167 a57e6870 Leszek Koltunski
// only called with figuring out which cubit was touched in MODE_REPLACE, which is only used in
1168
// during setting up the initial position in the solver.
1169 29b82486 Leszek Koltunski
1170 880beeea Leszek Koltunski
  int getCubit(float[] point3D)
1171 29b82486 Leszek Koltunski
    {
1172 880beeea Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
1173
    int currentBest=-1;
1174 a57e6870 Leszek Koltunski
    float multiplier = mNumLayers[0];
1175 880beeea Leszek Koltunski
1176
    point3D[0] *= multiplier;
1177
    point3D[1] *= multiplier;
1178
    point3D[2] *= multiplier;
1179
1180
    for(int i=0; i<NUM_CUBITS; i++)
1181
      {
1182
      dist = CUBITS[i].getDistSquared(point3D);
1183
      if( dist<minDist )
1184
        {
1185
        minDist = dist;
1186
        currentBest = i;
1187
        }
1188
      }
1189
1190
    return currentBest;
1191 7c111294 Leszek Koltunski
    }
1192 29b82486 Leszek Koltunski
1193 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1194 29b82486 Leszek Koltunski
1195 880beeea Leszek Koltunski
  int computeNearestAngle(int axis, float angle, float speed)
1196 7c111294 Leszek Koltunski
    {
1197 880beeea Leszek Koltunski
    int[] basicArray = getBasicAngle();
1198
    int basicAngle   = basicArray[axis>=basicArray.length ? 0 : axis];
1199
    int nearestAngle = 360/basicAngle;
1200
1201
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1202
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1203
1204
    if( tmp!=0 ) return nearestAngle*tmp;
1205
1206
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1207 7c111294 Leszek Koltunski
    }
1208 29b82486 Leszek Koltunski
1209 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1210
// INTERNAL API - those are called from 'effects' package
1211 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1212
1213 880beeea Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1214 29b82486 Leszek Koltunski
    {
1215 880beeea Leszek Koltunski
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1216 7c111294 Leszek Koltunski
    }
1217 29b82486 Leszek Koltunski
1218
///////////////////////////////////////////////////////////////////////////////////////////////////
1219
1220 880beeea Leszek Koltunski
  public Static4D getRotationQuat()
1221 59c20632 Leszek Koltunski
    {
1222
    return mQuat;
1223
    }
1224
1225
///////////////////////////////////////////////////////////////////////////////////////////////////
1226
1227
  public float getSize()
1228
    {
1229
    return mSize;
1230
    }
1231 7c111294 Leszek Koltunski
1232 2df35810 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1233
1234
  public void apply(Effect effect, int position)
1235
    {
1236
    mEffects.apply(effect, position);
1237
    }
1238
1239
///////////////////////////////////////////////////////////////////////////////////////////////////
1240
1241
  public void remove(long effectID)
1242
    {
1243
    mEffects.abortById(effectID);
1244
    }
1245
1246 3e9df6aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1247
1248 758b028d Leszek Koltunski
  public MeshBase getObjectMesh()
1249 3e9df6aa Leszek Koltunski
    {
1250
    return mMesh;
1251
    }
1252
1253 758b028d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1254
1255
  public DistortedEffects getObjectEffects()
1256
    {
1257
    return mEffects;
1258
    }
1259
1260 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1261
// PUBLIC API
1262 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1263
1264 880beeea Leszek Koltunski
  public int getCubitFaceColorIndex(int cubit, int face)
1265 29b82486 Leszek Koltunski
    {
1266 880beeea Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACE_COLORS*cubit + face);
1267 29b82486 Leszek Koltunski
1268 880beeea Leszek Koltunski
    int x = (int)(texMap.get0()/texMap.get2());
1269
    int y = (int)(texMap.get1()/texMap.get3());
1270 29b82486 Leszek Koltunski
1271 880beeea Leszek Koltunski
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1272 29b82486 Leszek Koltunski
    }
1273
1274
///////////////////////////////////////////////////////////////////////////////////////////////////
1275
1276 a57e6870 Leszek Koltunski
  public int[] getNumLayers()
1277 29b82486 Leszek Koltunski
    {
1278 880beeea Leszek Koltunski
    return mNumLayers;
1279
    }
1280 29b82486 Leszek Koltunski
1281
///////////////////////////////////////////////////////////////////////////////////////////////////
1282
1283 880beeea Leszek Koltunski
  public synchronized void solve()
1284 29b82486 Leszek Koltunski
    {
1285 880beeea Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
1286
      {
1287
      CUBITS[i].solve();
1288
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
1289
      }
1290 29b82486 Leszek Koltunski
    }
1291
1292 7ba38dd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1293
1294
  public DistortedNode getNode()
1295
    {
1296
    return mNode;
1297
    }
1298
1299 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1300
1301 c8bc83d9 Leszek Koltunski
  public ObjectType getObjectType()
1302 29b82486 Leszek Koltunski
    {
1303 c8bc83d9 Leszek Koltunski
    return intGetObjectType(mNumLayers);
1304 29b82486 Leszek Koltunski
    }
1305
1306 59c20632 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1307
1308
  public Movement getMovement()
1309
    {
1310
    if( mMovement==null )
1311
      {
1312
      int[] numLayers = getNumLayers();
1313
      if( mCuts==null ) getCuts(numLayers);
1314
      if( mLayerRotatable==null ) mLayerRotatable = getLayerRotatable(numLayers);
1315
      if( mEnabled==null ) mEnabled = getEnabled();
1316
1317
      int movementType = getMovementType();
1318
      int movementSplit= getMovementSplit();
1319
1320
      switch(movementType)
1321
        {
1322
        case MOVEMENT_TETRAHEDRON : mMovement = new Movement4(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1323
                                    break;
1324
        case MOVEMENT_HEXAHEDRON  : mMovement = new Movement6(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1325
                                    break;
1326
        case MOVEMENT_OCTAHEDRON  : mMovement = new Movement8(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1327
                                    break;
1328
        case MOVEMENT_DODECAHEDRON: mMovement = new Movement12(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled);
1329
                                    break;
1330
        case MOVEMENT_SHAPECHANGE : float[] dist3D = getDist3D(numLayers);
1331
                                    mMovement = new MovementC(mAxis,mCuts,mLayerRotatable,mSize,movementSplit,mEnabled,dist3D);
1332
                                    break;
1333
        }
1334
      }
1335
    return mMovement;
1336
    }
1337
1338 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1339
1340 82eb152a Leszek Koltunski
  protected void setReader(JsonReader reader)
1341
    {
1342
    // empty
1343
    }
1344
1345
///////////////////////////////////////////////////////////////////////////////////////////////////
1346
1347 a57e6870 Leszek Koltunski
  protected abstract ObjectType intGetObjectType(int[] numLayers);
1348 59c20632 Leszek Koltunski
1349
  // for JSON only
1350
  public abstract int getSolvedFunctionIndex();
1351
  public abstract int getMovementType();
1352
  public abstract int getMovementSplit();
1353
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1354
  public abstract int[][][] getEnabled();
1355
  public abstract float[] getDist3D(int[] numLayers);
1356 f9a81f52 Leszek Koltunski
  public abstract ScrambleState[] getScrambleStates();
1357 7bbfc84f Leszek Koltunski
  public abstract float[][] getCuts(int[] numLayers);
1358 1bb09f88 Leszek Koltunski
  public abstract Static4D[] getQuats();
1359
  public abstract int getNumStickerTypes(int[] numLayers);
1360
  public abstract ObjectSticker retSticker(int sticker);
1361 e30c522a Leszek Koltunski
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1362
  public abstract ObjectShape getObjectShape(int variant);
1363
  public abstract int getNumCubitVariants(int[] numLayers);
1364 7b832206 Leszek Koltunski
  public abstract float[][] getCubitPositions(int[] numLayers);
1365
  public abstract Static4D getQuat(int cubit, int[] numLayers);
1366
  public abstract int[] getSolvedQuats(int cubit, int[] numLayers);
1367 a75ae1ee Leszek Koltunski
  public abstract int getCubitFaceColor(int cubit, int face, int[] numLayers);
1368
  public abstract int getVariantFaceColor(int variant, int face, int[] numLayers);
1369
  public abstract int getNumFaceColors();
1370
  public abstract int getNumCubitFaces();
1371 82eb152a Leszek Koltunski
  public abstract float getScreenRatio();
1372
  public abstract int getColor(int face);
1373 7b832206 Leszek Koltunski
1374 a75ae1ee Leszek Koltunski
  // not only for JSON
1375 29b82486 Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
1376
  public abstract int[] getBasicAngle();
1377 2289cab1 Leszek Koltunski
  public abstract int getNumFaces();
1378 e26eb4e7 Leszek Koltunski
  public abstract String getObjectName();
1379
  public abstract String getInventor();
1380
  public abstract int getYearOfInvention();
1381
  public abstract int getComplexity();
1382 7ba38dd4 Leszek Koltunski
  public abstract int getFOV();
1383 29b82486 Leszek Koltunski
  }