Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 9c7d220a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.main;
11

    
12
import java.io.DataInputStream;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.util.Random;
16

    
17
import android.graphics.Bitmap;
18
import android.graphics.Canvas;
19
import android.graphics.Paint;
20

    
21
import org.distorted.library.effect.Effect;
22
import org.distorted.library.effect.MatrixEffectMove;
23
import org.distorted.library.effect.MatrixEffectQuaternion;
24
import org.distorted.library.effect.MatrixEffectScale;
25
import org.distorted.library.effect.VertexEffectQuaternion;
26
import org.distorted.library.effect.VertexEffectSink;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedLibrary;
29
import org.distorted.library.main.DistortedNode;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.helpers.QuatHelper;
32
import org.distorted.library.mesh.MeshBase;
33
import org.distorted.library.mesh.MeshFile;
34
import org.distorted.library.mesh.MeshJoined;
35
import org.distorted.library.message.EffectListener;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import org.distorted.objectlib.helpers.FactoryCubit;
41
import org.distorted.objectlib.helpers.FactorySticker;
42
import org.distorted.objectlib.helpers.ObjectFaceShape;
43
import org.distorted.objectlib.helpers.ObjectLibInterface;
44
import org.distorted.objectlib.helpers.ObjectShape;
45
import org.distorted.objectlib.metadata.Metadata;
46
import org.distorted.objectlib.signature.ObjectSignature;
47
import org.distorted.objectlib.helpers.ObjectSticker;
48
import org.distorted.objectlib.helpers.ObjectStickerOverride;
49
import org.distorted.objectlib.helpers.ObjectVertexEffects;
50
import org.distorted.objectlib.helpers.OperatingSystemInterface;
51
import org.distorted.objectlib.helpers.QuatGroupGenerator;
52
import org.distorted.objectlib.scrambling.ObjectScrambler;
53
import org.distorted.objectlib.json.JsonReader;
54
import org.distorted.objectlib.scrambling.ScrambleEdgeGenerator;
55
import org.distorted.objectlib.tablebases.ImplementedTablebasesList;
56
import org.distorted.objectlib.tablebases.TablebasesAbstract;
57
import org.distorted.objectlib.touchcontrol.*;
58

    
59
import static org.distorted.objectlib.touchcontrol.TouchControl.*;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
public abstract class TwistyObject
64
  {
65
  public static final int MODE_ICON = 0;
66
  public static final int MODE_NORM = 1;
67

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

    
79
  public static final int COLOR_RED_TET  = 0xffff1111;
80
  public static final int COLOR_BLUE_TET = 0xff4444ff;
81

    
82
  private static final int DEFAULT_TEXTURE_HEIGHT = 256;
83
  private static final int DEFAULT_TEXTURE_ROWS   = 8;
84

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

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

    
93
  private static final Static3D CENTER = new Static3D(0,0,0);
94

    
95
  protected float[][][][] mStickerCoords;
96
  protected Static4D[] mObjectQuats;
97

    
98
  private int[][] mStickerVariants;
99
  private float[] mStickerScales;
100
  private TwistyObjectCubit[] mCubits;
101
  private TwistyObjectSolved mSolved;
102
  private MeshBase[] mMeshes;
103
  private int mNumCubits, mNumQuats, mNumFaceColors, mNumTextures, mNumOverrides;
104
  private int mNumCubitFaces, mNumStickerTypes;
105
  private Static3D[] mAxis;
106
  private float[][] mCuts;
107
  private int[][] mMinimalCubiesInRow;
108
  private int[] mNumCuts;
109
  private float[][] mOrigPos;
110
  private Static4D[] mOrigQuat;
111
  private Static4D[] mMixupModeQuats;
112
  private boolean mIsInMixupMode;
113
  private Static4D mQuat;
114
  private int[] mNumLayers;
115
  private float mSize;
116
  private DistortedEffects mEffects;
117
  private Static3D mObjectScale;
118
  private int[] mQuatDebug;
119
  private DistortedTexture mTexture;
120
  private float mInitScreenRatio;
121
  private boolean mIsBandaged;
122
  private float mObjectScreenRatio;
123
  private int mNumTexRows, mNumTexCols, mTexHeight;
124
  private MeshBase mMesh;
125
  private ObjectScrambler mScrambler;
126
  private TouchControl mTouchControl;
127
  private DistortedNode mNode;
128
  private ObjectLibInterface mInterface;
129
  private Bitmap mBitmap;
130
  private ObjectSticker[] mStickers;
131
  private ObjectShape[] mShapes;
132
  private int mNumCubitVariants;
133
  private int[][] mCubitFaceColors;
134
  private int[][] mVariantFaceIsOuter;
135
  private int[][] mBasicAngles;
136
  private int mIconMode;
137
  private Metadata mMetadata;
138
  private float[][] mRowOffsets;
139
  private boolean[] mBelongs;
140
  private float[] mTmp;
141
  private int mNumPuzzleFaces;
142
  private ObjectStickerOverride[] mStickerOverrides;
143
  private boolean mError;
144
  private String mErrorString;
145
  private int mMaxNumLayers;
146
  private int mNumAxis;
147
  private boolean mThereAreDeciders;
148
  private TwistyLayerRotations mRotation;
149
  private int[] mColorTable;
150
  private int[] mOriginalColorTable;
151
  private float mTextureBorderMultiplier, mTextureCornerMultiplier;
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  TwistyObject(int iconMode, Static4D quat, Static3D move, float scale, InitAssets asset)
156
    {
157
    try
158
      {
159
      InputStream jsonStream = asset!=null ? asset.getJsonStream(): null;
160
      JsonReader reader = new JsonReader();
161
      reader.parseJsonFile(jsonStream);
162
      setReader(reader);
163
      mNumLayers = reader.getNumLayers();
164
      mSize      = reader.getSize();
165
      mMetadata  = null;
166
      initialize(iconMode,quat,move,scale,asset,true);
167
      if( asset!=null ) asset.close();
168
      mError = false;
169
      mErrorString=null;
170
      }
171
    catch(Exception ex)
172
      {
173
      mError = true;
174
      mErrorString = ex.getMessage();
175
      }
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  public TwistyObject(int iconMode, float size, Static4D quat, Static3D move, float scale, Metadata meta, InitAssets asset)
181
    {
182
    mNumLayers = meta.getNumLayers();
183
    mSize      = size;
184
    mMetadata  = meta;
185
    initialize(iconMode,quat,move,scale,asset,false);
186
    if( asset!=null ) asset.close();
187
    mError = false;
188
    mErrorString = null;
189
    }
190

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

    
193
  private void debugQuat(Static4D quat, int cubitIndex, float axisX, float axisY, float axisZ, float angle, int place)
194
    {
195
    float[] tracking = mCubits[cubitIndex].getTrackingPoint();
196

    
197
    if( tracking!=null )
198
      {
199
      String problem = (getShortName()+" "+cubitIndex+" "+quat.get0()+" "+quat.get1()+" "+quat.get2()+" "+quat.get3());
200
      problem += (" "+angle+" "+place+" "+tracking[0]+" "+tracking[1]+" "+tracking[2]);
201
      problem += (" "+axisX+" "+axisY+" "+axisZ);
202

    
203
      mInterface.reportProblem(problem, true);
204
      }
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  private void initialize(int iconMode, Static4D quat, Static3D move, float scale, InitAssets asset, boolean fromJSON)
210
    {
211
    mIconMode = iconMode;
212
    mQuat = quat;
213
    mAxis = getRotationAxis();
214
    mInitScreenRatio = getScreenRatio();
215
    mBasicAngles = getBasicAngles();
216
    mObjectQuats = getQuats();
217
    mNumQuats = mObjectQuats.length;
218
    mOrigPos = getCubitPositions(mNumLayers);
219
    mNumPuzzleFaces = getNumFaces();
220
    mRowOffsets = new float[mNumPuzzleFaces][3];
221
    mTmp = new float[4];
222
    mNumAxis = mAxis.length;
223
    mCuts = getCuts(mNumLayers);
224
    mNumCuts = new int[mNumAxis];
225
    mMaxNumLayers = -1;
226
    for(int i=0; i<mNumAxis; i++)
227
      {
228
      if( mMaxNumLayers<mNumLayers[i] ) mMaxNumLayers = mNumLayers[i];
229
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
230
      }
231

    
232
    mOriginalColorTable = getColorTable();
233
    mNumFaceColors = mOriginalColorTable.length;
234
    mColorTable = new int[mNumFaceColors];
235
    recreateFaceColors();
236
    mTextureBorderMultiplier = 1.0f;
237
    mTextureCornerMultiplier = 1.0f;
238

    
239
    mMinimalCubiesInRow = getMinimalCubiesInRow();
240
    mNumCubits = mOrigPos.length;
241

    
242
    mBelongs = new boolean[mNumCubits];
243

    
244
    int scramblingType = getScrambleType();
245
    int[][] edges = getScrambleEdges();
246
    int[][] algorithms = getScrambleAlgorithms();
247

    
248
    OperatingSystemInterface os = asset==null ? null : asset.getOS();
249
    TablebasesAbstract tablebase = os!=null ? getTablebase(os) : null;
250
    mScrambler = new ObjectScrambler(scramblingType,mNumAxis,mNumLayers,algorithms,edges,tablebase);
251

    
252
    boolean bandaged=false;
253

    
254
    for( int c=0; c<mNumCubits; c++)
255
      {
256
      if( mOrigPos[c].length>3 )
257
        {
258
        bandaged=true;
259
        break;
260
        }
261
      }
262
    mIsBandaged = bandaged;
263
    mQuatDebug = new int[mNumCubits];
264

    
265
    mObjectScale = new Static3D(scale,scale,scale);
266
    setObjectRatioNow(scale,720);
267

    
268
    mEffects = new DistortedEffects();
269
    createQuaternionEffects();
270

    
271
    mRotation = new TwistyLayerRotations(this,mAxis,mNumLayers,getGhostAngle(),mEffects);
272

    
273
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
274
    MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
275
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
276

    
277
    InputStream meshStream = asset!=null ? asset.getMeshStream(): null;
278
    boolean fromDMESH = (meshStream!=null);
279
    getQuatsAndShapes(fromDMESH,fromJSON);
280
    createMeshAndCubits(meshStream,fromDMESH);
281
    setUpTextures(fromDMESH,fromJSON);
282

    
283
    int index = getSolvedFunctionIndex();
284
    mSolved = new TwistyObjectSolved(this,mOrigPos,index);
285
    mSolved.setupSolvedQuats(getSolvedQuats());
286

    
287
    mEffects.apply(quatEffect);
288
    mEffects.apply(scaleEffect);
289
    mEffects.apply(moveEffect);
290

    
291
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  public void recreateFaceColors()
297
    {
298
    for(int i=0; i<mNumFaceColors; i++) mColorTable[i] = mOriginalColorTable[i];
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
// in degrees so that everything can be treated modulo 360
303

    
304
  public int getGhostAngle()
305
    {
306
    return 0;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  private TablebasesAbstract getTablebase(OperatingSystemInterface os)
312
    {
313
    String shortName = getShortName();
314
    return ImplementedTablebasesList.createPacked(os,shortName);
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  private void createQuaternionEffects()
320
    {
321
    if( mNumQuats<=ObjectControl.MAX_QUATS )
322
      {
323
      mIsInMixupMode = false;
324

    
325
      for( int q=0; q<mNumQuats; q++)
326
        {
327
        VertexEffectQuaternion vq = new VertexEffectQuaternion(mObjectQuats[q],CENTER);
328
        vq.setMeshAssociation(0,q);
329
        mEffects.apply(vq);
330
        }
331
      }
332
    else if( mNumCubits<=ObjectControl.MAX_QUATS )
333
      {
334
      mIsInMixupMode = true;
335
      mMixupModeQuats = new Static4D[mNumCubits];
336

    
337
      for( int q=0; q<mNumCubits; q++)
338
        {
339
        mMixupModeQuats[q] = new Static4D(mObjectQuats[0]);
340
        VertexEffectQuaternion vq = new VertexEffectQuaternion(mMixupModeQuats[q],CENTER);
341
        vq.setMeshAssociation(0,q);
342
        mEffects.apply(vq);
343
        }
344
      }
345
    else
346
      {
347
      android.util.Log.e("D", "object has too many quaternions ("+mNumQuats+") or too many cubits ("+mNumCubits+")");
348
      }
349
    }
350

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

    
353
  private Static3D getPos(float[] origPos)
354
    {
355
    int len = origPos.length/3;
356
    float sumX = 0.0f;
357
    float sumY = 0.0f;
358
    float sumZ = 0.0f;
359

    
360
    for(int i=0; i<len; i++)
361
      {
362
      sumX += origPos[3*i  ];
363
      sumY += origPos[3*i+1];
364
      sumZ += origPos[3*i+2];
365
      }
366

    
367
    sumX /= len;
368
    sumY /= len;
369
    sumZ /= len;
370

    
371
    return new Static3D(sumX,sumY,sumZ);
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375

    
376
  private void createOuterFaces()
377
    {
378
    for(int v=0; v<mNumCubitVariants; v++)
379
      {
380
      int numFaces = mShapes[v].getNumFaces();
381
      mVariantFaceIsOuter[v] = new int[numFaces];
382
      }
383

    
384
    for( int cubit=0; cubit<mNumCubits; cubit++)
385
      {
386
      int variant = getCubitVariant(cubit,mNumLayers);
387
      int numFaces = mShapes[variant].getNumFaces();
388

    
389
      for(int face=0; face<numFaces; face++)
390
        if( getCubitFaceColor(cubit,face)>=0 )
391
          {
392
          mVariantFaceIsOuter[variant][face] = 1;
393
          }
394
      }
395
    }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
  private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
400
    {
401
    mNumCubitVariants = getNumCubitVariants(mNumLayers);
402

    
403
    if( !fromDMESH || !fromJSON )
404
      {
405
      FactoryCubit factory = FactoryCubit.getInstance();
406
      factory.clear();
407

    
408
      mShapes = new ObjectShape[mNumCubitVariants];
409
      for(int v=0; v<mNumCubitVariants; v++) mShapes[v] = getObjectShape(v);
410
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
411
      mVariantFaceIsOuter = new int[mNumCubitVariants][];
412

    
413
      mOrigQuat = new Static4D[mNumCubits];
414
      for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
415

    
416
      if( !fromJSON )
417
        {
418
        mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
419
        createOuterFaces();
420
        }
421

    
422
      if( fromDMESH )
423
        {
424
        for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i], mVariantFaceIsOuter[i]);
425
        }
426
      }
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  private void createMeshAndCubits(InputStream stream, boolean fromDMESH)
432
    {
433
    mCubits = new TwistyObjectCubit[mNumCubits];
434

    
435
    if( fromDMESH )
436
      {
437
      DataInputStream dos = new DataInputStream(stream);
438
      mMesh = new MeshFile(dos);
439

    
440
      try
441
        {
442
        stream.close();
443
        }
444
      catch(IOException e)
445
        {
446
        android.util.Log.e("meshFile", "Error closing InputStream: "+e);
447
        }
448
      }
449
    else
450
      {
451
      MeshBase[] cubitMesh = new MeshBase[mNumCubits];
452

    
453
      for(int i=0; i<mNumCubits; i++)
454
        {
455
        cubitMesh[i] = createCubitMesh(i,mNumLayers,mNumCubitFaces);
456
        Static3D pos = getPos(mOrigPos[i]);
457
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
458
        }
459

    
460
      mMesh = new MeshJoined(cubitMesh);
461

    
462
      float pillowCoeff = getPillowCoeff();
463

    
464
      if( pillowCoeff!=1.0f )
465
        {
466
        float radius = getCircumscribedRadius();
467
        Static1D coeff = new Static1D(pillowCoeff);
468
        Static4D region= new Static4D(0,0,0,radius);
469
        VertexEffectSink sink = new VertexEffectSink(coeff,CENTER,region);
470
        mMesh.apply(sink);
471
        }
472
      }
473

    
474
    for(int i=0; i<mNumCubits; i++)
475
      {
476
      mCubits[i] = new TwistyObjectCubit(this,mOrigPos[i],mNumAxis,mMaxNumLayers,i);
477
      setCubitQuat(i,0);
478

    
479
      if( !mThereAreDeciders && mCubits[i].getType()==TwistyObjectCubit.TYPE_DECIDER )
480
        {
481
        mThereAreDeciders = true;
482
        }
483
      }
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int numComponents)
489
    {
490
    int variant = getCubitVariant(cubit,numLayers);
491

    
492
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
493

    
494
    if( mMeshes[variant]==null )
495
      {
496
      ObjectFaceShape faceShape = getObjectFaceShape(variant);
497
      ObjectVertexEffects effects = getVertexEffects(variant);
498
      FactoryCubit factory = FactoryCubit.getInstance();
499
      factory.createNewFaceTransform(mShapes[variant],mVariantFaceIsOuter[variant]);
500
      mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,effects,numComponents);
501
      }
502

    
503
    MeshBase mesh = mMeshes[variant].copy(true);
504
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
505
    mesh.apply(quat,0xffffffff,0);
506

    
507
    return mesh;
508
    }
509

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

    
512
  private void figureOutBitmapDimensions(int numTextures)
513
    {
514
    int maxSize = DistortedLibrary.getMaxTextureSize();
515

    
516
    mTexHeight  = DEFAULT_TEXTURE_HEIGHT;
517

    
518
    while(true)
519
      {
520
      mNumTexCols = DEFAULT_TEXTURE_ROWS;
521
      mNumTexRows = numTextures/mNumTexCols + 1;
522

    
523
      if( mNumTexRows*mTexHeight <= maxSize &&
524
          mNumTexCols*mTexHeight <= maxSize  ) break;
525

    
526
      mTexHeight/=2;
527
      }
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
533
    {
534
    mTexture = new DistortedTexture();
535

    
536
    if( fromJSON )
537
      {
538
      mNumStickerTypes = getNumStickerTypes();
539
      mNumCubitFaces = getNumCubitFaces();
540
      }
541
    else
542
      {
543
      FactoryCubit factory = FactoryCubit.getInstance();
544
      mStickerCoords   = factory.getStickerCoords();
545
      mStickerVariants = factory.getStickerVariants();
546
      mStickerScales   = factory.getStickerScales();
547
      adjustStickerCoords();
548
      mNumStickerTypes = (mStickerCoords==null ? 0 : mStickerCoords.length);
549
      }
550

    
551
    mStickerOverrides = getStickerOverrides();
552
    mNumOverrides = mStickerOverrides==null ? 0 : mStickerOverrides.length;
553

    
554
    mNumTextures= mNumFaceColors*mNumStickerTypes + mNumOverrides;
555
    figureOutBitmapDimensions(mNumTextures);
556
    if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
557

    
558
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
559
    else overrideCubitFaceColor();
560

    
561
    setTexture();
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
  public Metadata getMetadata()
567
    {
568
    return mMetadata;
569
    }
570

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

    
573
  public boolean isInIconMode()
574
    {
575
    return mIconMode==MODE_ICON;
576
    }
577

    
578
///////////////////////////////////////////////////////////////////////////////////////////////////
579

    
580
  public int getVariantStickerShape(int variant, int face)
581
    {
582
    if( variant <mStickerVariants.length )
583
      {
584
      int[] var = mStickerVariants[variant];
585
      return face>=var.length ? -1 : var[face];
586
      }
587
    return -1;
588
    }
589

    
590
///////////////////////////////////////////////////////////////////////////////////////////////////
591

    
592
  public boolean shouldResetTextureMaps()
593
    {
594
    return false;
595
    }
596

    
597
///////////////////////////////////////////////////////////////////////////////////////////////////
598

    
599
  public int[][] getScrambleAlgorithms()
600
    {
601
    return ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngles);
602
    }
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605

    
606
  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
607
    {
608
    final float MAXERR = 0.05f;
609
    int numAxis = dist.length;
610

    
611
    for(int i=0; i<numAxis; i++)
612
      {
613
      Static3D ax = faceAxis[i];
614
      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
615
      if( len>mSize*dist[i]+MAXERR ) return true;
616
      }
617

    
618
    return false;
619
    }
620

    
621
///////////////////////////////////////////////////////////////////////////////////////////////////
622

    
623
  private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
624
    {
625
    float[][] vertices = mShapes[variant].getVertices();
626
    Static3D[] axis = getFaceAxis();
627
    float[] dist3D = getDist3D(mNumLayers);
628

    
629
    for( float[] vertex : vertices)
630
      {
631
      float x = vertex[0];
632
      float y = vertex[1];
633
      float z = vertex[2];
634

    
635
      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
636

    
637
      float mx = tmp[0] + px;
638
      float my = tmp[1] + py;
639
      float mz = tmp[2] + pz;
640

    
641
      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
642
      }
643

    
644
    return true;
645
    }
646

    
647
///////////////////////////////////////////////////////////////////////////////////////////////////
648

    
649
  private float computeAvg(float[] pos, int offset)
650
    {
651
    int len = pos.length/3;
652
    float ret=0.0f;
653
    for(int i=0; i<len; i++) ret += pos[3*i+offset];
654
    ret /= len;
655

    
656
    return ret;
657
    }
658

    
659
///////////////////////////////////////////////////////////////////////////////////////////////////
660

    
661
  protected void displayCubitQuats()
662
    {
663
    StringBuilder builder = new StringBuilder();
664
    float[] tmp = new float[4];
665
    float ERR = 0.01f;
666

    
667
    for(int cubit=0; cubit<mNumCubits; cubit++)
668
      {
669
      builder.append(cubit);
670
      builder.append(" : ");
671

    
672
      int refCubit,variant = getCubitVariant(cubit,mNumLayers);
673

    
674
      for(refCubit=0; refCubit<mNumCubits; refCubit++)
675
        {
676
        if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
677
        }
678

    
679
      float[] curpos = mOrigPos[cubit];
680
      float[] refpos = mOrigPos[refCubit];
681
      float refX = computeAvg(refpos,0);
682
      float refY = computeAvg(refpos,1);
683
      float refZ = computeAvg(refpos,2);
684
      float curX = computeAvg(curpos,0);
685
      float curY = computeAvg(curpos,1);
686
      float curZ = computeAvg(curpos,2);
687

    
688
      for(int quat=0; quat<mNumQuats; quat++)
689
        {
690
        QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
691

    
692
        float dx = tmp[0]-curX;
693
        float dy = tmp[1]-curY;
694
        float dz = tmp[2]-curZ;
695

    
696
        if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
697
          {
698
          if( doesNotStickOut(variant,curX,curY,curZ,tmp,mObjectQuats[quat]) )
699
            {
700
            builder.append(quat);
701
            builder.append(',');
702
            }
703
          else
704
            {
705
            android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
706
            }
707
          }
708
        }
709

    
710
      builder.append('\n');
711
      }
712

    
713
    android.util.Log.e("D", "cubitQuats: \n"+builder );
714
    }
715

    
716
///////////////////////////////////////////////////////////////////////////////////////////////////
717

    
718
  public int getCubitRotationType(int cubit)
719
    {
720
    return TwistyObjectCubit.TYPE_NORMAL;
721
    }
722

    
723
///////////////////////////////////////////////////////////////////////////////////////////////////
724

    
725
  float[] getTrackingPoint(int cubitIndex, int cubitType)
726
    {
727
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
728
      {
729
      int variant = getCubitVariant(cubitIndex,mNumLayers);
730

    
731
      // object must have been created from JSON
732
      if( mVariantFaceIsOuter==null || mVariantFaceIsOuter[variant]==null )
733
        {
734
        mVariantFaceIsOuter = getVariantFaceIsOuter();
735
        }
736
      if( mShapes==null )
737
        {
738
        mShapes = new ObjectShape[mNumCubitVariants];
739
        }
740
      if( mShapes[variant]==null )
741
        {
742
        mShapes[variant] = getObjectShape(variant);
743
        }
744
      if( mOrigQuat==null )
745
        {
746
        mOrigQuat = new Static4D[mNumCubits];
747
        }
748
      if( mOrigQuat[cubitIndex]==null )
749
        {
750
        mOrigQuat[cubitIndex] = getCubitQuats(cubitIndex,mNumLayers);
751
        }
752

    
753
      int outer=-1, faces = mShapes[variant].getNumFaces();
754

    
755
      for(int i=0; i<faces; i++)
756
        {
757
        if( mVariantFaceIsOuter[variant][i]==1 )
758
          {
759
          outer=i;
760
          break;
761
          }
762
        }
763

    
764
      if( outer>=0 )
765
        {
766
        float[] v = mShapes[variant].getFirstVertexInFace(outer);
767
        float[] ret = new float[3];
768
        float[] curpos = mOrigPos[cubitIndex];
769
        Static4D quat = mOrigQuat[cubitIndex];
770
        QuatHelper.rotateVectorByQuat(mTmp, v[0], v[1], v[2], 1, quat);
771

    
772
        ret[0] = mTmp[0]+computeAvg(curpos,0);
773
        ret[1] = mTmp[1]+computeAvg(curpos,1);
774
        ret[2] = mTmp[2]+computeAvg(curpos,2);
775

    
776
        return ret;
777
        }
778
      else
779
        {
780
        android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
781
        }
782
      }
783

    
784
    return null;
785
    }
786

    
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788

    
789
  public int computeCurrentPuzzleFace(int type, float[] vertex)
790
    {
791
    if( type!=TwistyObjectCubit.TYPE_NORMAL )
792
      {
793
      Static3D[] axis = getFaceAxis();
794
      float[] dist3D = getDist3D(mNumLayers);
795
      final float MAXERR = 0.98f;
796
      int numAxis = axis.length;
797
      float x = vertex[0];
798
      float y = vertex[1];
799
      float z = vertex[2];
800

    
801
      for(int i=0; i<numAxis; i++)
802
        {
803
        Static3D ax = axis[i];
804
        float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
805
        if( len>mSize*dist3D[i]*MAXERR ) return i;
806
        }
807

    
808
      return -2;
809
      }
810

    
811
    return -1;
812
    }
813

    
814
///////////////////////////////////////////////////////////////////////////////////////////////////
815

    
816
  public float[] getCubitRowOffset(int cubitIndex)
817
    {
818
    return null;
819
    }
820

    
821
///////////////////////////////////////////////////////////////////////////////////////////////////
822

    
823
  void setRotationRowOffset(int puzzleFace, float[] offset)
824
    {
825
    mRowOffsets[puzzleFace][0] = offset[0];
826
    mRowOffsets[puzzleFace][1] = offset[1];
827
    mRowOffsets[puzzleFace][2] = offset[2];
828
    }
829

    
830
///////////////////////////////////////////////////////////////////////////////////////////////////
831

    
832
  int getNumAxis()
833
    {
834
    return mNumAxis;
835
    }
836

    
837
///////////////////////////////////////////////////////////////////////////////////////////////////
838

    
839
  public int[][] getSolvedQuats()
840
    {
841
    return null;
842
    }
843

    
844
///////////////////////////////////////////////////////////////////////////////////////////////////
845

    
846
  protected int[][] getOldSolvedQuats()
847
    {
848
    return mSolved.getSolvedQuats(mNumLayers,mNumCubitFaces,mCubitFaceColors);
849
    }
850

    
851
///////////////////////////////////////////////////////////////////////////////////////////////////
852
// 0: old, groups-of-quaternions based function.
853
// Still used by: Masterball, Mirror Sqewb, Mirror Jing, Mirror Pyraminx & the Penrose Cubes.
854
//
855
// 1: specific function only for the Dino4.
856
//
857
// 2: the new default; automatic way based on automatic detection of monochromatic puzzle surfaces.
858

    
859
  public int getSolvedFunctionIndex()
860
    {
861
    return 2;
862
    }
863

    
864
///////////////////////////////////////////////////////////////////////////////////////////////////
865

    
866
  int computeRow(float[] pos, int axisIndex, int cubitType, int puzzleFace)
867
    {
868
    int ret=0;
869
    int len = pos.length / 3;
870
    Static3D axis = mAxis[axisIndex];
871
    float axisX = axis.get0();
872
    float axisY = axis.get1();
873
    float axisZ = axis.get2();
874
    float casted, xoff=0, yoff=0, zoff=0;
875

    
876
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
877
      {
878
      xoff = mRowOffsets[puzzleFace][0];
879
      yoff = mRowOffsets[puzzleFace][1];
880
      zoff = mRowOffsets[puzzleFace][2];
881
      }
882

    
883
    for(int i=0; i<len; i++)
884
      {
885
      casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
886
      ret |= computeSingleRow(axisIndex,casted);
887
      }
888

    
889
    return ret;
890
    }
891

    
892
///////////////////////////////////////////////////////////////////////////////////////////////////
893

    
894
  private int computeSingleRow(int axisIndex,float casted)
895
    {
896
    int num = mNumCuts[axisIndex];
897
    float[] cuts = mCuts[axisIndex];
898

    
899
    for(int i=0; i<num; i++)
900
      {
901
      if( casted<cuts[i] ) return (1<<i);
902
      }
903

    
904
    return (1<<num);
905
    }
906

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

    
909
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
910
    {
911
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
912
    }
913

    
914
///////////////////////////////////////////////////////////////////////////////////////////////////
915
// note the minus in front of the sin() - we rotate counterclockwise
916
// when looking towards the direction where the axis increases in values.
917

    
918
  private Static4D makeQuaternion(float axisX, float axisY, float axisZ, int angleInDegrees)
919
    {
920
    while( angleInDegrees<0 ) angleInDegrees += 360;
921
    angleInDegrees %= 360;
922
    
923
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
924
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
925

    
926
    return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
927
    }
928

    
929
///////////////////////////////////////////////////////////////////////////////////////////////////
930

    
931
  void rotateAllCubits(int axisIndex, int rowBitmap, int angle)
932
    {
933
    Static3D axis = mAxis[axisIndex];
934
    float axisX = axis.get0();
935
    float axisY = axis.get1();
936
    float axisZ = axis.get2();
937
    Static4D quat = makeQuaternion(axisX,axisY,axisZ,angle);
938

    
939
    for(int i=0; i<mNumCubits; i++)
940
      {
941
      mBelongs[i] = belongsToRotation(i,axisIndex,rowBitmap);
942
      if( mBelongs[i] )
943
        {
944
        boolean result = mCubits[i].rotateCubit(quat,false);
945
        if( !result ) debugQuat(quat,i,axisX,axisY,axisZ,angle,1);
946
        }
947
      }
948

    
949
    recomputeFaceOffsets();
950

    
951
    for(int i=0; i<mNumCubits; i++)
952
      {
953
      if( mBelongs[i] )
954
        {
955
        int index = mCubits[i].postRotateCubit(quat);
956
        setCubitQuat(i,index);
957
        }
958
      else if( mCubits[i].getType()==TwistyObjectCubit.TYPE_FOLLOWER )
959
        {
960
        mCubits[i].computeRotationRow();
961
        setCubitQuat(i,mCubits[i].mQuatIndex);
962
        }
963
      }
964
    }
965

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

    
968
  private synchronized void setupPosition(int[][] moves)
969
    {
970
    if( moves!=null )
971
      {
972
      int axisIndex, row, rowBitmap, basic, angle;
973

    
974
      for(int[] move: moves)
975
        {
976
        axisIndex= move[0];
977
        rowBitmap= computeBandagedBitmap( move[1],axisIndex);
978
        row      = computeRowFromBitmap( move[1] );
979
        basic    = mBasicAngles[axisIndex][row];
980
        angle    = move[2]*(360/basic);   // this assumes that all layers from
981
                                          // the bitmap have the same BasicAngle.
982
                                          // at the moment this is always true as
983
                                          // there are no bandaged objects with
984
                                          // different per-layer BasicAngles.
985

    
986
        rotateAllCubits(axisIndex,rowBitmap,angle);
987
        }
988

    
989
      for(int i=0; i<mNumCubits; i++)
990
        {
991
        float[] pos = mCubits[i].getCurrentPos();
992
        int len = pos.length/3;
993
        for(int j=0; j<len; j++) clampPos(pos,3*j);
994
        }
995
      }
996
    }
997

    
998
///////////////////////////////////////////////////////////////////////////////////////////////////
999

    
1000
  public int getScrambleType()
1001
    {
1002
    return ObjectScrambler.SCRAMBLING_ALGORITHMS;
1003
    }
1004

    
1005
///////////////////////////////////////////////////////////////////////////////////////////////////
1006

    
1007
  public int[][] getMinimalCubiesInRow()
1008
    {
1009
    return null;
1010
    }
1011

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

    
1014
  int computeBandagedBitmap(int rowBitmap, int axisIndex)
1015
    {
1016
    if( mIsBandaged )
1017
      {
1018
      int bitmap, initBitmap=0;
1019

    
1020
      while( initBitmap!=rowBitmap )
1021
        {
1022
        initBitmap = rowBitmap;
1023

    
1024
        for(int cubit=0; cubit<mNumCubits; cubit++)
1025
          {
1026
          bitmap = mCubits[cubit].getRotRow(axisIndex);
1027
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
1028
          }
1029
        }
1030
      }
1031

    
1032
    if( mMinimalCubiesInRow!=null )
1033
      {
1034
      int[] minC = mMinimalCubiesInRow[axisIndex];
1035
      int numL = minC.length;
1036
      int[] numC = new int[numL];
1037

    
1038
      for(int cubit=0; cubit<mNumCubits; cubit++)
1039
        {
1040
        int bitmap = mCubits[cubit].getRotRow(axisIndex);
1041

    
1042
        for(int i=0; i<numL; i++)
1043
          {
1044
          if( (bitmap&0x1)!=0 ) numC[i]++;
1045
          bitmap>>=1;
1046
          }
1047
        }
1048

    
1049
      int bitmap,initBitmap = 0;
1050

    
1051
      while( initBitmap!=rowBitmap )
1052
        {
1053
        initBitmap = rowBitmap;
1054
        bitmap = rowBitmap;
1055
        int last = 0;
1056

    
1057
        for(int i=0; i<numL; i++)
1058
          {
1059
          if( numC[i]<minC[i] && numC[i]>0 )
1060
            {
1061
            if( (bitmap&0x1)!=0 )
1062
              {
1063
              if( i>0     ) rowBitmap |= (1<<(i-1));
1064
              if( i<numL-1) rowBitmap |= (1<<(i+1));
1065
              }
1066
            else if( (bitmap&0x2)!=0 || last>0 ) // we are not rotating this row, but
1067
                                                 // we are rotating the next or the prev
1068
              {
1069
              rowBitmap |= (1<<i);
1070
              }
1071
            }
1072

    
1073
          last = (bitmap&0x1);
1074
          bitmap>>=1;
1075
          }
1076
        }
1077
      }
1078

    
1079
    return rowBitmap;
1080
    }
1081

    
1082
///////////////////////////////////////////////////////////////////////////////////////////////////
1083

    
1084
  private int computeRowFromBitmap(int rowBitmap)
1085
    {
1086
    int index = 0;
1087

    
1088
    while(index<32)
1089
      {
1090
      if( (rowBitmap&0x1) != 0 ) return index;
1091
      rowBitmap>>=1;
1092
      index++;
1093
      }
1094
    return 0;
1095
    }
1096

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

    
1101
  void clampPos(float[] pos, int offset)
1102
    {
1103
    float currError, minError = Float.MAX_VALUE;
1104
    int minErrorIndex1 = -1;
1105
    int minErrorIndex2 = -1;
1106

    
1107
    float x = pos[offset  ];
1108
    float y = pos[offset+1];
1109
    float z = pos[offset+2];
1110

    
1111
    float xo,yo,zo;
1112

    
1113
    for(int i=0; i<mNumCubits; i++)
1114
      {
1115
      int len = mOrigPos[i].length / 3;
1116

    
1117
      for(int j=0; j<len; j++)
1118
        {
1119
        xo = mOrigPos[i][3*j  ];
1120
        yo = mOrigPos[i][3*j+1];
1121
        zo = mOrigPos[i][3*j+2];
1122

    
1123
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1124

    
1125
        if( currError<minError )
1126
          {
1127
          minError = currError;
1128
          minErrorIndex1 = i;
1129
          minErrorIndex2 = j;
1130
          }
1131
        }
1132
      }
1133

    
1134
    if( minError< 0.05f ) // TODO: 0.05 ?
1135
      {
1136
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1137
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1138
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1139
      }
1140
    }
1141

    
1142
///////////////////////////////////////////////////////////////////////////////////////////////////
1143

    
1144
  void setLibInterface(ObjectLibInterface inter)
1145
    {
1146
    mInterface = inter;
1147
    }
1148

    
1149
///////////////////////////////////////////////////////////////////////////////////////////////////
1150

    
1151
  void applyScrambles(int[][] moves)
1152
    {
1153
    setupPosition(moves);
1154
    }
1155

    
1156
///////////////////////////////////////////////////////////////////////////////////////////////////
1157

    
1158
  void initializeObject(int[][] moves)
1159
    {
1160
    solve();
1161
    setupPosition(moves);
1162
    }
1163

    
1164
///////////////////////////////////////////////////////////////////////////////////////////////////
1165

    
1166
  private void recomputeFaceOffsets()
1167
    {
1168
    if( mThereAreDeciders )
1169
      {
1170
      for(int i=0; i<mNumPuzzleFaces; i++)
1171
        {
1172
        mRowOffsets[i][0] =0;
1173
        mRowOffsets[i][1] =0;
1174
        mRowOffsets[i][2] =0;
1175
        }
1176

    
1177
      for(int i=0; i<mNumCubits; i++)
1178
        if( mCubits[i].getType()==TwistyObjectCubit.TYPE_DECIDER )
1179
          {
1180
          float[] offset = mCubits[i].getOffset();
1181
          int face = mCubits[i].getPuzzleFace();
1182
          mRowOffsets[face][0] = offset[0];
1183
          mRowOffsets[face][1] = offset[1];
1184
          mRowOffsets[face][2] = offset[2];
1185
          }
1186
      }
1187
    }
1188

    
1189
///////////////////////////////////////////////////////////////////////////////////////////////////
1190

    
1191
  synchronized float removeRotation()
1192
    {
1193
    return mRotation.removeRotation();
1194
    }
1195

    
1196
///////////////////////////////////////////////////////////////////////////////////////////////////
1197

    
1198
  long finishRotation(ObjectPreRender pre, int finishAx, int finishRow, float finishAngle, float avgSpeed)
1199
    {
1200
    int basicAngle = mBasicAngles[finishAx][finishRow];
1201
    return mRotation.finishRotation(pre,finishAx,finishRow,basicAngle,finishAngle,avgSpeed);
1202
    }
1203

    
1204
///////////////////////////////////////////////////////////////////////////////////////////////////
1205

    
1206
  void continueRotation(float angleInDegrees)
1207
    {
1208
    mRotation.continueRotation(angleInDegrees);
1209
    }
1210

    
1211
///////////////////////////////////////////////////////////////////////////////////////////////////
1212

    
1213
  synchronized long addRotation(EffectListener listener, int axis, int rowBitmap, int angle, long durationMillis )
1214
    {
1215
    return mRotation.addRotation(listener,axis,rowBitmap,angle,durationMillis);
1216
    }
1217

    
1218
///////////////////////////////////////////////////////////////////////////////////////////////////
1219

    
1220
  void enableGhostAxis(int axNum, boolean enable)
1221
    {
1222
    mTouchControl.enableGhostAxis(axNum,enable);
1223
    }
1224

    
1225
///////////////////////////////////////////////////////////////////////////////////////////////////
1226

    
1227
  synchronized boolean beginRotation(int axis, int row )
1228
    {
1229
    if( axis<0 || axis>=mNumAxis )
1230
      {
1231
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1232
      return false;
1233
      }
1234

    
1235
    return mRotation.beginRotation(axis,row);
1236
    }
1237

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

    
1240
  void setTextureMap(int cubit, int face, int color)
1241
    {
1242
    int variant  = getCubitVariant(cubit,mNumLayers);
1243
    int shape    = getVariantStickerShape(variant,face);
1244
    int texIndex = color<0 || shape<0 ? mNumTextures-mNumOverrides : shape*mNumFaceColors + color;
1245
    int row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1246
    int col      = texIndex%mNumTexCols;
1247

    
1248
    final float ratioW = 1.0f/mNumTexCols;
1249
    final float ratioH = 1.0f/mNumTexRows;
1250
    final Static4D[] maps = new Static4D[1];
1251
    maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1252
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1253
    }
1254

    
1255
///////////////////////////////////////////////////////////////////////////////////////////////////
1256
// figure out the whole face to which (cubit,face) belongs, repaint all (cubit,face) pairs to
1257
// the new color.
1258

    
1259
  void repaintPuzzleFace(int cubit, int face, int newColor)
1260
    {
1261
    int oldColorIndex = getCubitFaceColor(cubit,face);
1262

    
1263
    if( oldColorIndex<0 || oldColorIndex>=mNumFaceColors )
1264
      {
1265
      android.util.Log.e("D", "error in TwistyObject.repaintPuzzleFace, index="+oldColorIndex);
1266
      return;
1267
      }
1268

    
1269
    int oldColor = mColorTable[oldColorIndex];
1270

    
1271
    if( oldColor!=newColor )
1272
      {
1273
      oldColor = mOriginalColorTable[oldColorIndex];
1274
      changeColorInTexture(oldColor,newColor);
1275
      mColorTable[oldColorIndex] = newColor;
1276
      setTexture();
1277
      }
1278
    }
1279

    
1280
///////////////////////////////////////////////////////////////////////////////////////////////////
1281

    
1282
  int getCubitFaceColor(int cubit, int face)
1283
    {
1284
    int puzzleFace = getCubitFaceMap(cubit,face);
1285
    if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
1286
    return puzzleFace;
1287
    }
1288

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

    
1291
  public int getCubitFaceMap(int cubit, int face)
1292
    {
1293
    int numFaces = mCubitFaceColors[cubit].length;
1294
    int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
1295
    return puzzleFace<0 ? -1 : puzzleFace;
1296
    }
1297

    
1298
///////////////////////////////////////////////////////////////////////////////////////////////////
1299
// this would return the REAL value of outer; the 'getCubitFaceColor()>=0' returns if the face is
1300
// colored, which for example in case of Container (which has colored internal faces) is not the
1301
// same thing
1302

    
1303
  boolean faceIsOuter(int cubit, int face)
1304
    {
1305
    if( mCubitFaceColors==null )
1306
      {
1307
      if( mShapes==null )
1308
        {
1309
        mShapes = new ObjectShape[mNumCubitVariants];
1310
        }
1311

    
1312
      if( mShapes[0]==null )
1313
        {
1314
        for(int v=0; v<mNumCubitVariants; v++) mShapes[v] = getObjectShape(v);
1315
        }
1316

    
1317
      if( mOrigQuat==null )
1318
        {
1319
        mOrigQuat = new Static4D[mNumCubits];
1320
        }
1321

    
1322
      if( mOrigQuat[0]==null )
1323
        {
1324
        for(int c=0; c<mNumCubits; c++) mOrigQuat[c] = getCubitQuats(c,mNumLayers);
1325
        }
1326

    
1327
      mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
1328
      }
1329

    
1330
    return mCubitFaceColors[cubit][face]>=0;
1331
    }
1332

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

    
1335
  void resetAllTextureMaps()
1336
    {
1337
    final float ratioW = 1.0f/mNumTexCols;
1338
    final float ratioH = 1.0f/mNumTexRows;
1339
    int cubColor, stiShape, texIndex, variant, row, col;
1340

    
1341
    for(int cubit=0; cubit<mNumCubits; cubit++)
1342
      {
1343
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1344
      variant = getCubitVariant(cubit,mNumLayers);
1345

    
1346
      for(int face=0; face<mNumCubitFaces; face++)
1347
        {
1348
        cubColor = getCubitFaceColor(cubit,face);
1349
        stiShape = getVariantStickerShape(variant,face);
1350
        texIndex = cubColor<0 || stiShape<0 ? mNumTextures-mNumOverrides : stiShape*mNumFaceColors + cubColor;
1351
        row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1352
        col      = texIndex%mNumTexCols;
1353

    
1354
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1355
        }
1356

    
1357
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1358
      }
1359

    
1360
    overrideCubitFaceColor();
1361
    }
1362

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

    
1365
  private void overrideCubitFaceColor()
1366
    {
1367
    final float ratioW = 1.0f/mNumTexCols;
1368
    final float ratioH = 1.0f/mNumTexRows;
1369

    
1370
    for(int i=0; i<mNumOverrides; i++)
1371
      {
1372
      int[] cubitFaces = mStickerOverrides[i].getCubitFaces();
1373
      int length = cubitFaces.length/2;
1374

    
1375
      for(int j=0; j<length; j++)
1376
        {
1377
        final Static4D[] maps = new Static4D[1];
1378
        int color = mNumTextures-mNumOverrides+1+i;
1379
        int row   = (mNumTexRows-1) - color/mNumTexCols;
1380
        int col   = color%mNumTexCols;
1381
        int cubit = cubitFaces[2*j];
1382
        int face  = cubitFaces[2*j+1];
1383
        maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1384
        mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1385
        }
1386
      }
1387
    }
1388

    
1389
///////////////////////////////////////////////////////////////////////////////////////////////////
1390

    
1391
  void releaseResources()
1392
    {
1393
    mTexture.markForDeletion();
1394
    mMesh.markForDeletion();
1395
    mEffects.markForDeletion();
1396

    
1397
    if( mBitmap!=null )
1398
      {
1399
      mBitmap.recycle();
1400
      mBitmap = null;
1401
      }
1402

    
1403
    for(int j=0; j<mNumCubits; j++)
1404
      {
1405
      mCubits[j].releaseResources();
1406
      }
1407
    }
1408

    
1409
///////////////////////////////////////////////////////////////////////////////////////////////////
1410

    
1411
  private void setCubitQuat(int cubit, int equAssociation)
1412
    {
1413
    int andAssociation = mCubits[cubit].computeAssociation();
1414

    
1415
    if( !mIsInMixupMode )
1416
      {
1417
      mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
1418
      }
1419
    else
1420
      {
1421
      mMesh.setEffectAssociation(cubit,andAssociation,cubit);
1422
      Static4D tmp = mObjectQuats[equAssociation];
1423
      mMixupModeQuats[cubit].set(tmp);
1424
      }
1425
    }
1426

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

    
1429
  synchronized void restorePreferences(OperatingSystemInterface os)
1430
    {
1431
    boolean error = false;
1432
    String key = getShortName();
1433

    
1434
    for(int i=0; i<mNumCubits; i++)
1435
      {
1436
      mQuatDebug[i] = mCubits[i].restorePreferences(key,os);
1437

    
1438
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1439
        {
1440
        boolean result = mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]],true);
1441
        if( !result ) debugQuat(mObjectQuats[mQuatDebug[i]],i,0,0,0,mQuatDebug[i],3);
1442
        }
1443
      else { error = true; break; }
1444
      }
1445

    
1446
    if( !error )
1447
      {
1448
      recomputeFaceOffsets();
1449

    
1450
      for(int i=0; i<mNumCubits; i++)
1451
        {
1452
        if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1453
          {
1454
          mCubits[i].computeRotationRow();
1455
          setCubitQuat(i,mQuatDebug[i]);
1456
          }
1457
        else { error = true; break; }
1458
        }
1459
      }
1460

    
1461
    if( error )
1462
      {
1463
      for(int i=0; i<mNumCubits; i++)
1464
        {
1465
        mCubits[i].solve();
1466
        setCubitQuat(i,0);
1467
        }
1468
      }
1469
    }
1470

    
1471
///////////////////////////////////////////////////////////////////////////////////////////////////
1472

    
1473
  void savePreferences(OperatingSystemInterface os)
1474
    {
1475
    String key = getShortName();
1476
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,os);
1477

    
1478
    os.putFloat(key+"_border", mTextureBorderMultiplier);
1479
    os.putFloat(key+"_corner", mTextureCornerMultiplier);
1480
    os.putString(key+"_colors", createColors(mColorTable) );
1481
    }
1482

    
1483
///////////////////////////////////////////////////////////////////////////////////////////////////
1484

    
1485
  public void removePreferences(OperatingSystemInterface os)
1486
    {
1487
    String key = getShortName();
1488
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,os);
1489

    
1490
    os.remove(key+"_border");
1491
    os.remove(key+"_corner");
1492
    os.remove(key+"_colors");
1493
    }
1494

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

    
1497
  private String createColors(int[] table)
1498
    {
1499
    StringBuilder sb = new StringBuilder();
1500
    int len = table!=null ? table.length : 0;
1501

    
1502
    for(int i=0; i<len; i++)
1503
      {
1504
      sb.append(table[i]);
1505
      sb.append(',');
1506
      }
1507

    
1508
    return sb.toString();
1509
    }
1510

    
1511
///////////////////////////////////////////////////////////////////////////////////////////////////
1512

    
1513
  private float computeRadiusCorrection(float[][] sticker, int curr, int len)
1514
    {
1515
    final float A = 0.8f;  // 0<A<1
1516

    
1517
    int prev = curr>0 ? curr-1 : len-1;
1518
    int next = curr<len-1 ? curr+1 : 0;
1519

    
1520
    float v1x = sticker[prev][0]-sticker[curr][0];
1521
    float v1y = sticker[prev][1]-sticker[curr][1];
1522
    float v2x = sticker[next][0]-sticker[curr][0];
1523
    float v2y = sticker[next][1]-sticker[curr][1];
1524

    
1525
    float len1= v1x*v1x+v1y*v1y;
1526
    float len2= v2x*v2x+v2y*v2y;
1527

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

    
1530
    return 1-A*cos;
1531
    }
1532

    
1533
///////////////////////////////////////////////////////////////////////////////////////////////////
1534
// Radius of the sphere circumscribed on the puzzle. Needed for pillowing.
1535
//
1536
// This won't work correctly for pillowing off-center puzzles (e.g. mirrors) - for those we'd need
1537
// to introduce the concept of a 'sink center' as well.
1538
//
1539
// public because needed in TouchControlShapemod
1540

    
1541
  public float getCircumscribedRadius()
1542
    {
1543
    switch(mNumPuzzleFaces)
1544
      {
1545
      case  4: return (SQ6/4)*mSize;
1546
      case  6: return (SQ3/2)*mSize;
1547
      case  8: return (SQ2/2)*mSize;
1548
      case 12: return (SQ3/2)*((SQ5+1)/2)*mSize;
1549
      case 16: return 0.50f*mSize;
1550
      }
1551

    
1552
    return 0.0f;
1553
    }
1554

    
1555
///////////////////////////////////////////////////////////////////////////////////////////////////
1556
// might be overridden in subclasses which want per-edge radii
1557

    
1558
  protected float[][][] getStickerRadii()
1559
    {
1560
    float radius = getStickerRadius();
1561
    int numStickers = mStickerCoords.length;
1562
    float[][][] ret = new float[numStickers][][];
1563

    
1564
    for(int s=0; s<numStickers; s++)
1565
      {
1566
      int numLoops = mStickerCoords[s].length;
1567
      ret[s] = new float[numLoops][];
1568

    
1569
      for(int l=0; l<numLoops; l++)
1570
        {
1571
        int numVertices = mStickerCoords[s][l].length;
1572
        ret[s][l] = new float[numVertices];
1573
        for(int v=0; v<numVertices; v++) ret[s][l][v] = radius;
1574
        }
1575
      }
1576

    
1577
    return ret;
1578
    }
1579

    
1580
///////////////////////////////////////////////////////////////////////////////////////////////////
1581
// might be overridden in subclasses which want per-edge strokes
1582

    
1583
  protected float[][][] getStickerStrokes()
1584
    {
1585
    float stroke = getStickerStroke();
1586
    int numStickers = mStickerCoords.length;
1587
    float[][][] ret = new float[numStickers][][];
1588

    
1589
    for(int s=0; s<numStickers; s++)
1590
      {
1591
      int numLoops = mStickerCoords[s].length;
1592
      ret[s] = new float[numLoops][];
1593

    
1594
      for(int l=0; l<numLoops; l++)
1595
        {
1596
        int numVertices = mStickerCoords[s][l].length;
1597
        ret[s][l] = new float[numVertices];
1598
        for(int v=0; v<numVertices; v++) ret[s][l][v] = stroke;
1599
        }
1600
      }
1601

    
1602
    return ret;
1603
    }
1604

    
1605
///////////////////////////////////////////////////////////////////////////////////////////////////
1606

    
1607
  public ObjectSticker retSticker(int sticker)
1608
    {
1609
    if( mStickers==null )
1610
      {
1611
      float[][][] radii   = getStickerRadii();
1612
      float[][][] strokes = getStickerStrokes();
1613
      float[][][] angles  = getStickerAngles();
1614
      int numStickers = mStickerCoords.length;
1615
      mStickers = new ObjectSticker[numStickers];
1616

    
1617
      for(int s=0; s<numStickers; s++)
1618
        {
1619
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1620
        int numLoops = mStickerCoords[s].length;
1621
        float[][] rad = new float[numLoops][];
1622
        float[][] str = new float[numLoops][];
1623

    
1624
        for(int l=0; l<numLoops; l++)
1625
          {
1626
          int numVerts = mStickerCoords[s][l].length;
1627
          rad[l] = new float[numVerts];
1628
          str[l] = new float[numVerts];
1629
          float[] st = strokes[s][l];
1630
          float[] ra = radii[s][l];
1631

    
1632
          for(int v=0; v<numVerts; v++)
1633
            {
1634
            rad[l][v] = ra[v] * computeRadiusCorrection(mStickerCoords[s][l],v,numVerts) / scale;
1635
            str[l][v] = st[v] / scale;
1636
            }
1637
          }
1638

    
1639
        mStickers[s] = new ObjectSticker(mStickerCoords[s], (angles==null ? null : angles[s]) ,rad, str);
1640
        }
1641
      }
1642

    
1643
    return mStickers[sticker];
1644
    }
1645

    
1646
///////////////////////////////////////////////////////////////////////////////////////////////////
1647
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1648

    
1649
  public void adjustStickerCoords()
1650
    {
1651

    
1652
    }
1653

    
1654
///////////////////////////////////////////////////////////////////////////////////////////////////
1655

    
1656
  public Static4D[] getQuats()
1657
    {
1658
    if( mObjectQuats==null )
1659
      {
1660
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1661
      }
1662

    
1663
    return mObjectQuats;
1664
    }
1665

    
1666
///////////////////////////////////////////////////////////////////////////////////////////////////
1667

    
1668
  public int[][] getVariantFaceIsOuter()
1669
    {
1670
    return mVariantFaceIsOuter;
1671
    }
1672

    
1673
///////////////////////////////////////////////////////////////////////////////////////////////////
1674

    
1675
  public int getInternalColor()
1676
    {
1677
    return COLOR_INTERNAL;
1678
    }
1679

    
1680
///////////////////////////////////////////////////////////////////////////////////////////////////
1681
// the getFaceColors + final INTERNAL_COLOR in a grid (so that we do not exceed the maximum texture size)
1682

    
1683
  void createTexture(float borders, float corners)
1684
    {
1685
    Paint paint = new Paint();
1686
    Canvas canvas = new Canvas(mBitmap);
1687

    
1688
    paint.setAntiAlias(true);
1689
    paint.setTextAlign(Paint.Align.CENTER);
1690
    paint.setStyle(Paint.Style.FILL);
1691
    paint.setColor(getInternalColor());
1692
    canvas.drawRect(0, 0, mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, paint);
1693

    
1694
    mTextureBorderMultiplier = borders;
1695
    mTextureCornerMultiplier = corners;
1696

    
1697
    int texture = 0;
1698
    FactorySticker factory = FactorySticker.getInstance();
1699

    
1700
    for(int row=0; row<mNumTexRows; row++)
1701
      for(int col=0; col<mNumTexCols; col++)
1702
        {
1703
        if( texture<mNumTextures-mNumOverrides )
1704
          {
1705
          ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1706
          int color = mColorTable[texture%mNumFaceColors];
1707
          factory.drawRoundedPolygons( canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color,
1708
                                       mTexHeight, sticker,mTextureBorderMultiplier,mTextureCornerMultiplier);
1709
          }
1710
        else if( texture>mNumTextures-mNumOverrides && texture<=mNumTextures )
1711
          {
1712
          int color = mStickerOverrides[mNumTextures-texture].getColor();
1713
          factory.drawSolidColor(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color, mTexHeight);
1714
          }
1715

    
1716
        texture++;
1717
        }
1718
    }
1719

    
1720
///////////////////////////////////////////////////////////////////////////////////////////////////
1721

    
1722
  private void changeColorInTexture(int oldColor, int newColor)
1723
    {
1724
    Paint paint = new Paint();
1725
    Canvas canvas = new Canvas(mBitmap);
1726
    paint.setAntiAlias(true);
1727
    int texture = 0;
1728
    FactorySticker factory = FactorySticker.getInstance();
1729

    
1730
    for(int row=0; row<mNumTexRows; row++)
1731
      for(int col=0; col<mNumTexCols; col++)
1732
        {
1733
        if( texture<mNumTextures-mNumOverrides )
1734
          {
1735
          int color = mOriginalColorTable[texture%mNumFaceColors];
1736

    
1737
          if( color==oldColor )
1738
            {
1739
            ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1740
            factory.drawRoundedPolygons( canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight,
1741
                                         newColor, mTexHeight, sticker,mTextureBorderMultiplier,mTextureCornerMultiplier);
1742
            }
1743
          }
1744

    
1745
        texture++;
1746
        }
1747
    }
1748

    
1749
///////////////////////////////////////////////////////////////////////////////////////////////////
1750

    
1751
  void setTexture()
1752
    {
1753
    if( mBitmap==null )
1754
      {
1755
      mBitmap = Bitmap.createBitmap( mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, Bitmap.Config.ARGB_4444);
1756
      createTexture(1.0f,1.0f);
1757
      }
1758

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

    
1766
///////////////////////////////////////////////////////////////////////////////////////////////////
1767

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

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

    
1778
///////////////////////////////////////////////////////////////////////////////////////////////////
1779

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

    
1784
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1785
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1786

    
1787
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1788
    }
1789

    
1790
///////////////////////////////////////////////////////////////////////////////////////////////////
1791

    
1792
  void setNodeSize(int nodeSize)
1793
    {
1794
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1795
    }
1796

    
1797
///////////////////////////////////////////////////////////////////////////////////////////////////
1798

    
1799
  public float getRatio()
1800
    {
1801
    return mObjectScreenRatio;
1802
    }
1803

    
1804
///////////////////////////////////////////////////////////////////////////////////////////////////
1805

    
1806
  public float getObjectRatio()
1807
    {
1808
    return mObjectScreenRatio*mInitScreenRatio;
1809
    }
1810

    
1811
///////////////////////////////////////////////////////////////////////////////////////////////////
1812

    
1813
  boolean isSolved()
1814
    {
1815
    boolean solved = mSolved.isSolved(mCubits);
1816
    return mRotation.isSolved(solved);
1817
    }
1818

    
1819
///////////////////////////////////////////////////////////////////////////////////////////////////
1820
// INTERNAL API - those are called from 'effects' package
1821
///////////////////////////////////////////////////////////////////////////////////////////////////
1822

    
1823
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1824
    {
1825
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1826
    }
1827

    
1828
///////////////////////////////////////////////////////////////////////////////////////////////////
1829

    
1830
  public Static4D getRotationQuat()
1831
    {
1832
    return mQuat;
1833
    }
1834

    
1835
///////////////////////////////////////////////////////////////////////////////////////////////////
1836

    
1837
  public float getSize()
1838
    {
1839
    return mSize;
1840
    }
1841

    
1842
///////////////////////////////////////////////////////////////////////////////////////////////////
1843

    
1844
  public void applyEffect(Effect effect, int position)
1845
    {
1846
    mEffects.apply(effect, position);
1847
    }
1848

    
1849
///////////////////////////////////////////////////////////////////////////////////////////////////
1850

    
1851
  public void removeEffect(long effectID)
1852
    {
1853
    mEffects.abortById(effectID);
1854
    }
1855

    
1856
///////////////////////////////////////////////////////////////////////////////////////////////////
1857

    
1858
  public MeshBase getObjectMesh()
1859
    {
1860
    return mMesh;
1861
    }
1862

    
1863
///////////////////////////////////////////////////////////////////////////////////////////////////
1864

    
1865
  public DistortedEffects getObjectEffects()
1866
    {
1867
    return mEffects;
1868
    }
1869

    
1870
///////////////////////////////////////////////////////////////////////////////////////////////////
1871

    
1872
  public int getCubitType(int cubit)
1873
    {
1874
    return mCubits[cubit].getType();
1875
    }
1876

    
1877
///////////////////////////////////////////////////////////////////////////////////////////////////
1878

    
1879
  public float[] getCubitOffset(int cubit)
1880
    {
1881
    return mCubits[cubit].getOffset();
1882
    }
1883

    
1884
///////////////////////////////////////////////////////////////////////////////////////////////////
1885

    
1886
  public ObjectStickerOverride[] getStickerOverrides()
1887
    {
1888
    return null;
1889
    }
1890

    
1891
///////////////////////////////////////////////////////////////////////////////////////////////////
1892

    
1893
  public boolean getError()
1894
    {
1895
    return mError;
1896
    }
1897

    
1898
///////////////////////////////////////////////////////////////////////////////////////////////////
1899

    
1900
  public String getErrorString()
1901
    {
1902
    return mErrorString;
1903
    }
1904

    
1905
///////////////////////////////////////////////////////////////////////////////////////////////////
1906
// PUBLIC API
1907
///////////////////////////////////////////////////////////////////////////////////////////////////
1908

    
1909
  public int getCubitFaceStickerIndex(int cubit, int face)
1910
    {
1911
    Static4D texMap = mMesh.getTextureMap(mNumCubitFaces*cubit + face);
1912

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

    
1916
    return (mNumTexRows-1-y)*mNumTexCols + x;
1917
    }
1918

    
1919
///////////////////////////////////////////////////////////////////////////////////////////////////
1920

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

    
1926
///////////////////////////////////////////////////////////////////////////////////////////////////
1927

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

    
1932
    recomputeFaceOffsets();
1933

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

    
1941
///////////////////////////////////////////////////////////////////////////////////////////////////
1942

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

    
1948
///////////////////////////////////////////////////////////////////////////////////////////////////
1949

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

    
1955
///////////////////////////////////////////////////////////////////////////////////////////////////
1956

    
1957
  public Bitmap getStickerBitmap()
1958
    {
1959
    return mBitmap;
1960
    }
1961

    
1962
///////////////////////////////////////////////////////////////////////////////////////////////////
1963

    
1964
  public DistortedNode getNode()
1965
    {
1966
    return mNode;
1967
    }
1968

    
1969
///////////////////////////////////////////////////////////////////////////////////////////////////
1970

    
1971
  public int getNumStickerTypes()
1972
    {
1973
    return mNumStickerTypes;
1974
    }
1975

    
1976
///////////////////////////////////////////////////////////////////////////////////////////////////
1977

    
1978
  public String reportState()
1979
    {
1980
    StringBuilder builder = new StringBuilder();
1981

    
1982
    for(int i=0; i<mNumCubits; i++ )
1983
      {
1984
      if( i>0 ) builder.append('.');
1985
      builder.append(mCubits[i].mQuatIndex);
1986
      }
1987

    
1988
    return builder.toString();
1989
    }
1990

    
1991
///////////////////////////////////////////////////////////////////////////////////////////////////
1992
// this is here only so it can be overridden in TwistyJSON so that we can get this from JSON.
1993

    
1994
  public int getNumCubitFaces()
1995
    {
1996
    return 0;
1997
    }
1998

    
1999
///////////////////////////////////////////////////////////////////////////////////////////////////
2000
// 1.0 - i.e. no pillowing - by default.
2001
// The coeff is really param of the 'sink' vertex effect - if it is not equal to 1.0, we apply the
2002
// sink effect [centered at (0,0,0)] to the whole mesh as the last step of composing it.
2003

    
2004
  public float getPillowCoeff()
2005
    {
2006
    return 1.0f;
2007
    }
2008

    
2009
///////////////////////////////////////////////////////////////////////////////////////////////////
2010

    
2011
  public TouchControl getTouchControl()
2012
    {
2013
    if( mTouchControl==null )
2014
      {
2015
      switch(getTouchControlType())
2016
        {
2017
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
2018
                                   break;
2019
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
2020
                                   break;
2021
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
2022
                                   break;
2023
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
2024
                                   break;
2025
        case TC_ICOSAHEDRON      : mTouchControl = new TouchControlIcosahedron(this);
2026
                                   break;
2027
        case TC_CUBOID           : int[] numLayers = getNumLayers();
2028
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
2029
                                   break;
2030
        case TC_BALL             : mTouchControl = new TouchControlBall(this);
2031
                                   break;
2032
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
2033
                                   break;
2034
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
2035
                                   break;
2036
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
2037
                                   break;
2038
        }
2039
      }
2040
    return mTouchControl;
2041
    }
2042

    
2043
///////////////////////////////////////////////////////////////////////////////////////////////////
2044

    
2045
  public float[][] returnRotationFactor()
2046
    {
2047
    float[][] factor = new float[mNumAxis][];
2048

    
2049
    for(int ax=0; ax<mNumAxis; ax++)
2050
      {
2051
      int numL = mNumLayers[ax];
2052
      factor[ax] = new float[numL];
2053
      for(int la=0; la<numL; la++) factor[ax][la] = 1.0f;
2054
      }
2055

    
2056
    return factor;
2057
    }
2058

    
2059
///////////////////////////////////////////////////////////////////////////////////////////////////
2060

    
2061
  protected void setReader(JsonReader reader)
2062
    {
2063
    // empty
2064
    }
2065

    
2066
///////////////////////////////////////////////////////////////////////////////////////////////////
2067

    
2068
  public String getInventor()
2069
    {
2070
    return mMetadata.getAuthor();
2071
    }
2072

    
2073
///////////////////////////////////////////////////////////////////////////////////////////////////
2074

    
2075
  public int getYearOfInvention()
2076
    {
2077
    return mMetadata.getYearOfInvention();
2078
    }
2079

    
2080
///////////////////////////////////////////////////////////////////////////////////////////////////
2081

    
2082
  public float getComplexity()
2083
    {
2084
    return mMetadata.getDifficulty();
2085
    }
2086

    
2087
///////////////////////////////////////////////////////////////////////////////////////////////////
2088

    
2089
  public String getObjectName()
2090
    {
2091
    return mMetadata.getObjectName();
2092
    }
2093

    
2094
///////////////////////////////////////////////////////////////////////////////////////////////////
2095

    
2096
  public ObjectSignature getSignature()
2097
    {
2098
    return mMetadata.getSignature();
2099
    }
2100

    
2101
///////////////////////////////////////////////////////////////////////////////////////////////////
2102
  // for JSON only
2103
  public abstract int getTouchControlType();
2104
  public abstract int getTouchControlSplit();
2105
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
2106
  public abstract int[][][] getEnabled();
2107
  public abstract float[] getDist3D(int[] numLayers);
2108
  public abstract Static3D[] getFaceAxis();
2109
  public abstract int[][] getScrambleEdges();
2110
  public abstract float[][] getCuts(int[] numLayers);
2111
  public abstract float getStickerRadius();
2112
  public abstract float getStickerStroke();
2113
  public abstract float[][][] getStickerAngles();
2114
  public abstract int getCubitVariant(int cubit, int[] numLayers);
2115
  public abstract ObjectShape getObjectShape(int variant);
2116
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
2117
  public abstract ObjectVertexEffects getVertexEffects(int variant);
2118
  public abstract int getNumCubitVariants(int[] numLayers);
2119
  public abstract float[][] getCubitPositions(int[] numLayers);
2120
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
2121
  public abstract float getScreenRatio();
2122
  public abstract int[] getColorTable();
2123
  public abstract String getShortName();
2124

    
2125
  // not only for JSON
2126
  public abstract Static3D[] getRotationAxis();
2127
  public abstract int[][] getBasicAngles();
2128
  public abstract int getNumFaces();
2129
  public abstract int getFOV();
2130
  public abstract String[][] getTutorials();
2131
  }
(6-6/9)