Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 43668bcc

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
    mTextureBorderMultiplier = os.getFloat(key+"_border", 1.0f);
1471
    mTextureCornerMultiplier = os.getFloat(key+"_corner", 1.0f);
1472
    String colors = os.getString(key+"_colors", null);
1473
    boolean different = false;
1474

    
1475
    if( colors!=null ) different = restoreColors(colors,mColorTable);
1476

    
1477
    if( different || mTextureBorderMultiplier!=1.0f || mTextureCornerMultiplier!=1.0f )
1478
      {
1479
      createTexture(mTextureBorderMultiplier,mTextureCornerMultiplier);
1480
      setTexture();
1481
      }
1482
    }
1483

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

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

    
1491
    os.putFloat(key+"_border", mTextureBorderMultiplier);
1492
    os.putFloat(key+"_corner", mTextureCornerMultiplier);
1493
    os.putString(key+"_colors", createColors(mColorTable) );
1494
    }
1495

    
1496
///////////////////////////////////////////////////////////////////////////////////////////////////
1497

    
1498
  public void removePreferences(OperatingSystemInterface os)
1499
    {
1500
    String key = getShortName();
1501
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,os);
1502

    
1503
    os.remove(key+"_border");
1504
    os.remove(key+"_corner");
1505
    os.remove(key+"_colors");
1506
    }
1507

    
1508
///////////////////////////////////////////////////////////////////////////////////////////////////
1509

    
1510
  private String createColors(int[] table)
1511
    {
1512
    StringBuilder sb = new StringBuilder();
1513
    int len = table!=null ? table.length : 0;
1514

    
1515
    for(int i=0; i<len; i++)
1516
      {
1517
      sb.append(table[i]);
1518
      sb.append(',');
1519
      }
1520

    
1521
    return sb.toString();
1522
    }
1523

    
1524
///////////////////////////////////////////////////////////////////////////////////////////////////
1525

    
1526
  private boolean restoreColors(String colors, int[] table)
1527
    {
1528
    String[] parts = colors.split(",");
1529
    int len = parts.length;
1530
    boolean ret = false;
1531

    
1532
    try
1533
      {
1534
      for(int s=0; s<len; s++)
1535
        {
1536
        table[s] = Integer.parseInt(parts[s]);
1537
        if( table[s]!=mOriginalColorTable[s] ) ret = true;
1538
        }
1539
      }
1540
    catch(NumberFormatException nfe)
1541
      {
1542
      for(int s=0; s<len; s++) table[s] = mOriginalColorTable[s];
1543
      return false;
1544
      }
1545

    
1546
    return ret;
1547
    }
1548

    
1549
///////////////////////////////////////////////////////////////////////////////////////////////////
1550

    
1551
  private float computeRadiusCorrection(float[][] sticker, int curr, int len)
1552
    {
1553
    final float A = 0.8f;  // 0<A<1
1554

    
1555
    int prev = curr>0 ? curr-1 : len-1;
1556
    int next = curr<len-1 ? curr+1 : 0;
1557

    
1558
    float v1x = sticker[prev][0]-sticker[curr][0];
1559
    float v1y = sticker[prev][1]-sticker[curr][1];
1560
    float v2x = sticker[next][0]-sticker[curr][0];
1561
    float v2y = sticker[next][1]-sticker[curr][1];
1562

    
1563
    float len1= v1x*v1x+v1y*v1y;
1564
    float len2= v2x*v2x+v2y*v2y;
1565

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

    
1568
    return 1-A*cos;
1569
    }
1570

    
1571
///////////////////////////////////////////////////////////////////////////////////////////////////
1572
// Radius of the sphere circumscribed on the puzzle. Needed for pillowing.
1573
//
1574
// This won't work correctly for pillowing off-center puzzles (e.g. mirrors) - for those we'd need
1575
// to introduce the concept of a 'sink center' as well.
1576
//
1577
// public because needed in TouchControlShapemod
1578

    
1579
  public float getCircumscribedRadius()
1580
    {
1581
    switch(mNumPuzzleFaces)
1582
      {
1583
      case  4: return (SQ6/4)*mSize;
1584
      case  6: return (SQ3/2)*mSize;
1585
      case  8: return (SQ2/2)*mSize;
1586
      case 12: return (SQ3/2)*((SQ5+1)/2)*mSize;
1587
      case 16: return 0.50f*mSize;
1588
      }
1589

    
1590
    return 0.0f;
1591
    }
1592

    
1593
///////////////////////////////////////////////////////////////////////////////////////////////////
1594
// might be overridden in subclasses which want per-edge radii
1595

    
1596
  protected float[][][] getStickerRadii()
1597
    {
1598
    float radius = getStickerRadius();
1599
    int numStickers = mStickerCoords.length;
1600
    float[][][] ret = new float[numStickers][][];
1601

    
1602
    for(int s=0; s<numStickers; s++)
1603
      {
1604
      int numLoops = mStickerCoords[s].length;
1605
      ret[s] = new float[numLoops][];
1606

    
1607
      for(int l=0; l<numLoops; l++)
1608
        {
1609
        int numVertices = mStickerCoords[s][l].length;
1610
        ret[s][l] = new float[numVertices];
1611
        for(int v=0; v<numVertices; v++) ret[s][l][v] = radius;
1612
        }
1613
      }
1614

    
1615
    return ret;
1616
    }
1617

    
1618
///////////////////////////////////////////////////////////////////////////////////////////////////
1619
// might be overridden in subclasses which want per-edge strokes
1620

    
1621
  protected float[][][] getStickerStrokes()
1622
    {
1623
    float stroke = getStickerStroke();
1624
    int numStickers = mStickerCoords.length;
1625
    float[][][] ret = new float[numStickers][][];
1626

    
1627
    for(int s=0; s<numStickers; s++)
1628
      {
1629
      int numLoops = mStickerCoords[s].length;
1630
      ret[s] = new float[numLoops][];
1631

    
1632
      for(int l=0; l<numLoops; l++)
1633
        {
1634
        int numVertices = mStickerCoords[s][l].length;
1635
        ret[s][l] = new float[numVertices];
1636
        for(int v=0; v<numVertices; v++) ret[s][l][v] = stroke;
1637
        }
1638
      }
1639

    
1640
    return ret;
1641
    }
1642

    
1643
///////////////////////////////////////////////////////////////////////////////////////////////////
1644

    
1645
  public ObjectSticker retSticker(int sticker)
1646
    {
1647
    if( mStickers==null )
1648
      {
1649
      float[][][] radii   = getStickerRadii();
1650
      float[][][] strokes = getStickerStrokes();
1651
      float[][][] angles  = getStickerAngles();
1652
      int numStickers = mStickerCoords.length;
1653
      mStickers = new ObjectSticker[numStickers];
1654

    
1655
      for(int s=0; s<numStickers; s++)
1656
        {
1657
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1658
        int numLoops = mStickerCoords[s].length;
1659
        float[][] rad = new float[numLoops][];
1660
        float[][] str = new float[numLoops][];
1661

    
1662
        for(int l=0; l<numLoops; l++)
1663
          {
1664
          int numVerts = mStickerCoords[s][l].length;
1665
          rad[l] = new float[numVerts];
1666
          str[l] = new float[numVerts];
1667
          float[] st = strokes[s][l];
1668
          float[] ra = radii[s][l];
1669

    
1670
          for(int v=0; v<numVerts; v++)
1671
            {
1672
            rad[l][v] = ra[v] * computeRadiusCorrection(mStickerCoords[s][l],v,numVerts) / scale;
1673
            str[l][v] = st[v] / scale;
1674
            }
1675
          }
1676

    
1677
        mStickers[s] = new ObjectSticker(mStickerCoords[s], (angles==null ? null : angles[s]) ,rad, str);
1678
        }
1679
      }
1680

    
1681
    return mStickers[sticker];
1682
    }
1683

    
1684
///////////////////////////////////////////////////////////////////////////////////////////////////
1685
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1686

    
1687
  public void adjustStickerCoords()
1688
    {
1689

    
1690
    }
1691

    
1692
///////////////////////////////////////////////////////////////////////////////////////////////////
1693

    
1694
  public Static4D[] getQuats()
1695
    {
1696
    if( mObjectQuats==null )
1697
      {
1698
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1699
      }
1700

    
1701
    return mObjectQuats;
1702
    }
1703

    
1704
///////////////////////////////////////////////////////////////////////////////////////////////////
1705

    
1706
  public int[][] getVariantFaceIsOuter()
1707
    {
1708
    return mVariantFaceIsOuter;
1709
    }
1710

    
1711
///////////////////////////////////////////////////////////////////////////////////////////////////
1712

    
1713
  public int getInternalColor()
1714
    {
1715
    return COLOR_INTERNAL;
1716
    }
1717

    
1718
///////////////////////////////////////////////////////////////////////////////////////////////////
1719

    
1720
  public float getTextureBorders()
1721
    {
1722
    return mTextureBorderMultiplier;
1723
    }
1724

    
1725
///////////////////////////////////////////////////////////////////////////////////////////////////
1726

    
1727
  public float getTextureCorners()
1728
    {
1729
    return mTextureCornerMultiplier;
1730
    }
1731

    
1732
///////////////////////////////////////////////////////////////////////////////////////////////////
1733
// the getFaceColors + final INTERNAL_COLOR in a grid (so that we do not exceed the maximum texture size)
1734

    
1735
  void createTexture(float border, float corner)
1736
    {
1737
    Paint paint = new Paint();
1738
    Canvas canvas = new Canvas(mBitmap);
1739

    
1740
    paint.setAntiAlias(true);
1741
    paint.setTextAlign(Paint.Align.CENTER);
1742
    paint.setStyle(Paint.Style.FILL);
1743
    paint.setColor(getInternalColor());
1744
    canvas.drawRect(0, 0, mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, paint);
1745

    
1746
    mTextureBorderMultiplier = border;
1747
    mTextureCornerMultiplier = corner;
1748

    
1749
    int texture = 0;
1750
    FactorySticker factory = FactorySticker.getInstance();
1751

    
1752
    for(int row=0; row<mNumTexRows; row++)
1753
      for(int col=0; col<mNumTexCols; col++)
1754
        {
1755
        if( texture<mNumTextures-mNumOverrides )
1756
          {
1757
          ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1758
          int color = mColorTable[texture%mNumFaceColors];
1759
          factory.drawRoundedPolygons( canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color,
1760
                                       mTexHeight, sticker,mTextureBorderMultiplier,mTextureCornerMultiplier);
1761
          }
1762
        else if( texture>mNumTextures-mNumOverrides && texture<=mNumTextures )
1763
          {
1764
          int color = mStickerOverrides[mNumTextures-texture].getColor();
1765
          factory.drawSolidColor(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color, mTexHeight);
1766
          }
1767

    
1768
        texture++;
1769
        }
1770
    }
1771

    
1772
///////////////////////////////////////////////////////////////////////////////////////////////////
1773

    
1774
  private void changeColorInTexture(int oldColor, int newColor)
1775
    {
1776
    Paint paint = new Paint();
1777
    Canvas canvas = new Canvas(mBitmap);
1778
    paint.setAntiAlias(true);
1779
    int texture = 0;
1780
    FactorySticker factory = FactorySticker.getInstance();
1781

    
1782
    for(int row=0; row<mNumTexRows; row++)
1783
      for(int col=0; col<mNumTexCols; col++)
1784
        {
1785
        if( texture<mNumTextures-mNumOverrides )
1786
          {
1787
          int color = mOriginalColorTable[texture%mNumFaceColors];
1788

    
1789
          if( color==oldColor )
1790
            {
1791
            ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1792
            factory.drawRoundedPolygons( canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight,
1793
                                         newColor, mTexHeight, sticker,mTextureBorderMultiplier,mTextureCornerMultiplier);
1794
            }
1795
          }
1796

    
1797
        texture++;
1798
        }
1799
    }
1800

    
1801
///////////////////////////////////////////////////////////////////////////////////////////////////
1802

    
1803
  void setTexture()
1804
    {
1805
    if( mBitmap==null )
1806
      {
1807
      mBitmap = Bitmap.createBitmap( mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, Bitmap.Config.ARGB_4444);
1808
      createTexture(mTextureBorderMultiplier,mTextureCornerMultiplier);
1809
      }
1810

    
1811
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1812
      {
1813
      int max = DistortedLibrary.getMaxTextureSize();
1814
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1815
      }
1816
    }
1817

    
1818
///////////////////////////////////////////////////////////////////////////////////////////////////
1819

    
1820
  void setObjectRatioNow(float sc, int nodeSize)
1821
    {
1822
    mObjectScreenRatio = sc;
1823
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1824
    mObjectScale.set(scale,scale,scale);
1825

    
1826
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1827
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1828
    }
1829

    
1830
///////////////////////////////////////////////////////////////////////////////////////////////////
1831

    
1832
  void setObjectRatio(float sizeChange, int nodeSize)
1833
    {
1834
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1835

    
1836
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1837
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1838

    
1839
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1840
    }
1841

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

    
1844
  void setNodeSize(int nodeSize)
1845
    {
1846
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1847
    }
1848

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

    
1851
  public float getRatio()
1852
    {
1853
    return mObjectScreenRatio;
1854
    }
1855

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

    
1858
  public float getObjectRatio()
1859
    {
1860
    return mObjectScreenRatio*mInitScreenRatio;
1861
    }
1862

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

    
1865
  boolean isSolved()
1866
    {
1867
    boolean solved = mSolved.isSolved(mCubits);
1868
    return mRotation.isSolved(solved);
1869
    }
1870

    
1871
///////////////////////////////////////////////////////////////////////////////////////////////////
1872
// INTERNAL API - those are called from 'effects' package
1873
///////////////////////////////////////////////////////////////////////////////////////////////////
1874

    
1875
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1876
    {
1877
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1878
    }
1879

    
1880
///////////////////////////////////////////////////////////////////////////////////////////////////
1881

    
1882
  public Static4D getRotationQuat()
1883
    {
1884
    return mQuat;
1885
    }
1886

    
1887
///////////////////////////////////////////////////////////////////////////////////////////////////
1888

    
1889
  public float getSize()
1890
    {
1891
    return mSize;
1892
    }
1893

    
1894
///////////////////////////////////////////////////////////////////////////////////////////////////
1895

    
1896
  public void applyEffect(Effect effect, int position)
1897
    {
1898
    mEffects.apply(effect, position);
1899
    }
1900

    
1901
///////////////////////////////////////////////////////////////////////////////////////////////////
1902

    
1903
  public void removeEffect(long effectID)
1904
    {
1905
    mEffects.abortById(effectID);
1906
    }
1907

    
1908
///////////////////////////////////////////////////////////////////////////////////////////////////
1909

    
1910
  public MeshBase getObjectMesh()
1911
    {
1912
    return mMesh;
1913
    }
1914

    
1915
///////////////////////////////////////////////////////////////////////////////////////////////////
1916

    
1917
  public DistortedEffects getObjectEffects()
1918
    {
1919
    return mEffects;
1920
    }
1921

    
1922
///////////////////////////////////////////////////////////////////////////////////////////////////
1923

    
1924
  public int getCubitType(int cubit)
1925
    {
1926
    return mCubits[cubit].getType();
1927
    }
1928

    
1929
///////////////////////////////////////////////////////////////////////////////////////////////////
1930

    
1931
  public float[] getCubitOffset(int cubit)
1932
    {
1933
    return mCubits[cubit].getOffset();
1934
    }
1935

    
1936
///////////////////////////////////////////////////////////////////////////////////////////////////
1937

    
1938
  public ObjectStickerOverride[] getStickerOverrides()
1939
    {
1940
    return null;
1941
    }
1942

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

    
1945
  public boolean getError()
1946
    {
1947
    return mError;
1948
    }
1949

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

    
1952
  public String getErrorString()
1953
    {
1954
    return mErrorString;
1955
    }
1956

    
1957
///////////////////////////////////////////////////////////////////////////////////////////////////
1958
// PUBLIC API
1959
///////////////////////////////////////////////////////////////////////////////////////////////////
1960

    
1961
  public int getCubitFaceStickerIndex(int cubit, int face)
1962
    {
1963
    Static4D texMap = mMesh.getTextureMap(mNumCubitFaces*cubit + face);
1964

    
1965
    int x = (int)(texMap.get0()/texMap.get2());
1966
    int y = (int)(texMap.get1()/texMap.get3());
1967

    
1968
    return (mNumTexRows-1-y)*mNumTexCols + x;
1969
    }
1970

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

    
1973
  public int[] getNumLayers()
1974
    {
1975
    return mNumLayers;
1976
    }
1977

    
1978
///////////////////////////////////////////////////////////////////////////////////////////////////
1979

    
1980
  public synchronized void solve()
1981
    {
1982
    for(int i=0; i<mNumCubits; i++) mCubits[i].solve();
1983

    
1984
    recomputeFaceOffsets();
1985

    
1986
    for(int i=0; i<mNumCubits; i++)
1987
      {
1988
      mCubits[i].computeRotationRow();
1989
      setCubitQuat(i,0);
1990
      }
1991
    }
1992

    
1993
///////////////////////////////////////////////////////////////////////////////////////////////////
1994

    
1995
  public int getCubitQuatIndex(int cubit)
1996
    {
1997
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1998
    }
1999

    
2000
///////////////////////////////////////////////////////////////////////////////////////////////////
2001

    
2002
  public int getCubitRotRow(int cubit, int axis)
2003
    {
2004
    return mCubits[cubit].getRotRow(axis);
2005
    }
2006

    
2007
///////////////////////////////////////////////////////////////////////////////////////////////////
2008

    
2009
  public Bitmap getStickerBitmap()
2010
    {
2011
    return mBitmap;
2012
    }
2013

    
2014
///////////////////////////////////////////////////////////////////////////////////////////////////
2015

    
2016
  public DistortedNode getNode()
2017
    {
2018
    return mNode;
2019
    }
2020

    
2021
///////////////////////////////////////////////////////////////////////////////////////////////////
2022

    
2023
  public int getNumStickerTypes()
2024
    {
2025
    return mNumStickerTypes;
2026
    }
2027

    
2028
///////////////////////////////////////////////////////////////////////////////////////////////////
2029

    
2030
  public String reportState()
2031
    {
2032
    StringBuilder builder = new StringBuilder();
2033

    
2034
    for(int i=0; i<mNumCubits; i++ )
2035
      {
2036
      if( i>0 ) builder.append('.');
2037
      builder.append(mCubits[i].mQuatIndex);
2038
      }
2039

    
2040
    return builder.toString();
2041
    }
2042

    
2043
///////////////////////////////////////////////////////////////////////////////////////////////////
2044
// this is here only so it can be overridden in TwistyJSON so that we can get this from JSON.
2045

    
2046
  public int getNumCubitFaces()
2047
    {
2048
    return 0;
2049
    }
2050

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

    
2056
  public float getPillowCoeff()
2057
    {
2058
    return 1.0f;
2059
    }
2060

    
2061
///////////////////////////////////////////////////////////////////////////////////////////////////
2062

    
2063
  public TouchControl getTouchControl()
2064
    {
2065
    if( mTouchControl==null )
2066
      {
2067
      switch(getTouchControlType())
2068
        {
2069
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
2070
                                   break;
2071
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
2072
                                   break;
2073
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
2074
                                   break;
2075
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
2076
                                   break;
2077
        case TC_ICOSAHEDRON      : mTouchControl = new TouchControlIcosahedron(this);
2078
                                   break;
2079
        case TC_CUBOID           : int[] numLayers = getNumLayers();
2080
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
2081
                                   break;
2082
        case TC_BALL             : mTouchControl = new TouchControlBall(this);
2083
                                   break;
2084
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
2085
                                   break;
2086
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
2087
                                   break;
2088
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
2089
                                   break;
2090
        }
2091
      }
2092
    return mTouchControl;
2093
    }
2094

    
2095
///////////////////////////////////////////////////////////////////////////////////////////////////
2096

    
2097
  public float[][] returnRotationFactor()
2098
    {
2099
    float[][] factor = new float[mNumAxis][];
2100

    
2101
    for(int ax=0; ax<mNumAxis; ax++)
2102
      {
2103
      int numL = mNumLayers[ax];
2104
      factor[ax] = new float[numL];
2105
      for(int la=0; la<numL; la++) factor[ax][la] = 1.0f;
2106
      }
2107

    
2108
    return factor;
2109
    }
2110

    
2111
///////////////////////////////////////////////////////////////////////////////////////////////////
2112

    
2113
  protected void setReader(JsonReader reader)
2114
    {
2115
    // empty
2116
    }
2117

    
2118
///////////////////////////////////////////////////////////////////////////////////////////////////
2119

    
2120
  public String getInventor()
2121
    {
2122
    return mMetadata.getAuthor();
2123
    }
2124

    
2125
///////////////////////////////////////////////////////////////////////////////////////////////////
2126

    
2127
  public int getYearOfInvention()
2128
    {
2129
    return mMetadata.getYearOfInvention();
2130
    }
2131

    
2132
///////////////////////////////////////////////////////////////////////////////////////////////////
2133

    
2134
  public float getComplexity()
2135
    {
2136
    return mMetadata.getDifficulty();
2137
    }
2138

    
2139
///////////////////////////////////////////////////////////////////////////////////////////////////
2140

    
2141
  public String getObjectName()
2142
    {
2143
    return mMetadata.getObjectName();
2144
    }
2145

    
2146
///////////////////////////////////////////////////////////////////////////////////////////////////
2147

    
2148
  public ObjectSignature getSignature()
2149
    {
2150
    return mMetadata.getSignature();
2151
    }
2152

    
2153
///////////////////////////////////////////////////////////////////////////////////////////////////
2154
  // for JSON only
2155
  public abstract int getTouchControlType();
2156
  public abstract int getTouchControlSplit();
2157
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
2158
  public abstract int[][][] getEnabled();
2159
  public abstract float[] getDist3D(int[] numLayers);
2160
  public abstract Static3D[] getFaceAxis();
2161
  public abstract int[][] getScrambleEdges();
2162
  public abstract float[][] getCuts(int[] numLayers);
2163
  public abstract float getStickerRadius();
2164
  public abstract float getStickerStroke();
2165
  public abstract float[][][] getStickerAngles();
2166
  public abstract int getCubitVariant(int cubit, int[] numLayers);
2167
  public abstract ObjectShape getObjectShape(int variant);
2168
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
2169
  public abstract ObjectVertexEffects getVertexEffects(int variant);
2170
  public abstract int getNumCubitVariants(int[] numLayers);
2171
  public abstract float[][] getCubitPositions(int[] numLayers);
2172
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
2173
  public abstract float getScreenRatio();
2174
  public abstract int[] getColorTable();
2175
  public abstract String getShortName();
2176

    
2177
  // not only for JSON
2178
  public abstract Static3D[] getRotationAxis();
2179
  public abstract int[][] getBasicAngles();
2180
  public abstract int getNumFaces();
2181
  public abstract int getFOV();
2182
  public abstract String[][] getTutorials();
2183
  }
(6-6/9)