Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 962b8ff6

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

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

    
201
      mInterface.reportProblem(problem, true);
202
      }
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

    
230
    recreateFaceColors();
231
    mMinimalCubiesInRow = getMinimalCubiesInRow();
232
    mNumCubits = mOrigPos.length;
233

    
234
    mBelongs = new boolean[mNumCubits];
235

    
236
    int scramblingType = getScrambleType();
237
    int[][] edges = getScrambleEdges();
238
    int[][] algorithms = getScrambleAlgorithms();
239

    
240
    OperatingSystemInterface os = asset==null ? null : asset.getOS();
241
    TablebasesAbstract tablebase = os!=null ? getTablebase(os) : null;
242
    mScrambler = new ObjectScrambler(scramblingType,mNumAxis,mNumLayers,algorithms,edges,tablebase);
243

    
244
    boolean bandaged=false;
245

    
246
    for( int c=0; c<mNumCubits; c++)
247
      {
248
      if( mOrigPos[c].length>3 )
249
        {
250
        bandaged=true;
251
        break;
252
        }
253
      }
254
    mIsBandaged = bandaged;
255
    mQuatDebug = new int[mNumCubits];
256

    
257
    mObjectScale = new Static3D(scale,scale,scale);
258
    setObjectRatioNow(scale,720);
259

    
260
    mEffects = new DistortedEffects();
261
    createQuaternionEffects();
262

    
263
    mRotation = new TwistyLayerRotations(this,mAxis,mNumLayers,getGhostAngle(),mEffects);
264

    
265
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
266
    MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
267
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
268

    
269
    InputStream meshStream = asset!=null ? asset.getMeshStream(): null;
270
    boolean fromDMESH = (meshStream!=null);
271
    getQuatsAndShapes(fromDMESH,fromJSON);
272
    createMeshAndCubits(meshStream,fromDMESH);
273
    setUpTextures(fromDMESH,fromJSON);
274

    
275
    int index = getSolvedFunctionIndex();
276
    mSolved = new TwistyObjectSolved(this,mOrigPos,index);
277
    mSolved.setupSolvedQuats(getSolvedQuats());
278

    
279
    mEffects.apply(quatEffect);
280
    mEffects.apply(scaleEffect);
281
    mEffects.apply(moveEffect);
282

    
283
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  public void recreateFaceColors()
289
    {
290
    int[] tmp = getColorTable();
291
    mNumFaceColors = tmp.length;
292
    mColorTable = new int[mNumFaceColors];
293
    for(int i=0; i<mNumFaceColors; i++) mColorTable[i] = tmp[i];
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
// in degrees so that everything can be treated modulo 360
298

    
299
  public int getGhostAngle()
300
    {
301
    return 0;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  private TablebasesAbstract getTablebase(OperatingSystemInterface os)
307
    {
308
    String shortName = getShortName();
309
    return ImplementedTablebasesList.createPacked(os,shortName);
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  private void createQuaternionEffects()
315
    {
316
    if( mNumQuats<=ObjectControl.MAX_QUATS )
317
      {
318
      mIsInMixupMode = false;
319

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

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

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  private Static3D getPos(float[] origPos)
349
    {
350
    int len = origPos.length/3;
351
    float sumX = 0.0f;
352
    float sumY = 0.0f;
353
    float sumZ = 0.0f;
354

    
355
    for(int i=0; i<len; i++)
356
      {
357
      sumX += origPos[3*i  ];
358
      sumY += origPos[3*i+1];
359
      sumZ += origPos[3*i+2];
360
      }
361

    
362
    sumX /= len;
363
    sumY /= len;
364
    sumZ /= len;
365

    
366
    return new Static3D(sumX,sumY,sumZ);
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  private void createOuterFaces()
372
    {
373
    for(int v=0; v<mNumCubitVariants; v++)
374
      {
375
      int numFaces = mShapes[v].getNumFaces();
376
      mVariantFaceIsOuter[v] = new int[numFaces];
377
      }
378

    
379
    for( int cubit=0; cubit<mNumCubits; cubit++)
380
      {
381
      int variant = getCubitVariant(cubit,mNumLayers);
382
      int numFaces = mShapes[variant].getNumFaces();
383

    
384
      for(int face=0; face<numFaces; face++)
385
        if( getCubitFaceColor(cubit,face)>=0 )
386
          {
387
          mVariantFaceIsOuter[variant][face] = 1;
388
          }
389
      }
390
    }
391

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

    
394
  private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
395
    {
396
    mNumCubitVariants = getNumCubitVariants(mNumLayers);
397

    
398
    if( !fromDMESH || !fromJSON )
399
      {
400
      FactoryCubit factory = FactoryCubit.getInstance();
401
      factory.clear();
402

    
403
      mShapes = new ObjectShape[mNumCubitVariants];
404
      for(int v=0; v<mNumCubitVariants; v++) mShapes[v] = getObjectShape(v);
405
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
406
      mVariantFaceIsOuter = new int[mNumCubitVariants][];
407

    
408
      mOrigQuat = new Static4D[mNumCubits];
409
      for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
410

    
411
      if( !fromJSON )
412
        {
413
        mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
414
        createOuterFaces();
415
        }
416

    
417
      if( fromDMESH )
418
        {
419
        for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i], mVariantFaceIsOuter[i]);
420
        }
421
      }
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  private void createMeshAndCubits(InputStream stream, boolean fromDMESH)
427
    {
428
    mCubits = new TwistyObjectCubit[mNumCubits];
429

    
430
    if( fromDMESH )
431
      {
432
      DataInputStream dos = new DataInputStream(stream);
433
      mMesh = new MeshFile(dos);
434

    
435
      try
436
        {
437
        stream.close();
438
        }
439
      catch(IOException e)
440
        {
441
        android.util.Log.e("meshFile", "Error closing InputStream: "+e);
442
        }
443
      }
444
    else
445
      {
446
      MeshBase[] cubitMesh = new MeshBase[mNumCubits];
447

    
448
      for(int i=0; i<mNumCubits; i++)
449
        {
450
        cubitMesh[i] = createCubitMesh(i,mNumLayers,mNumCubitFaces);
451
        Static3D pos = getPos(mOrigPos[i]);
452
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
453
        }
454

    
455
      mMesh = new MeshJoined(cubitMesh);
456

    
457
      float pillowCoeff = getPillowCoeff();
458

    
459
      if( pillowCoeff!=1.0f )
460
        {
461
        float radius = getCircumscribedRadius();
462
        Static1D coeff = new Static1D(pillowCoeff);
463
        Static4D region= new Static4D(0,0,0,radius);
464
        VertexEffectSink sink = new VertexEffectSink(coeff,CENTER,region);
465
        mMesh.apply(sink);
466
        }
467
      }
468

    
469
    for(int i=0; i<mNumCubits; i++)
470
      {
471
      mCubits[i] = new TwistyObjectCubit(this,mOrigPos[i],mNumAxis,mMaxNumLayers,i);
472
      setCubitQuat(i,0);
473

    
474
      if( !mThereAreDeciders && mCubits[i].getType()==TwistyObjectCubit.TYPE_DECIDER )
475
        {
476
        mThereAreDeciders = true;
477
        }
478
      }
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int numComponents)
484
    {
485
    int variant = getCubitVariant(cubit,numLayers);
486

    
487
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
488

    
489
    if( mMeshes[variant]==null )
490
      {
491
      ObjectFaceShape faceShape = getObjectFaceShape(variant);
492
      ObjectVertexEffects effects = getVertexEffects(variant);
493
      FactoryCubit factory = FactoryCubit.getInstance();
494
      factory.createNewFaceTransform(mShapes[variant],mVariantFaceIsOuter[variant]);
495
      mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,effects,numComponents);
496
      }
497

    
498
    MeshBase mesh = mMeshes[variant].copy(true);
499
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
500
    mesh.apply(quat,0xffffffff,0);
501

    
502
    return mesh;
503
    }
504

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

    
507
  private void figureOutBitmapDimensions(int numTextures)
508
    {
509
    int maxSize = DistortedLibrary.getMaxTextureSize();
510

    
511
    mTexHeight  = DEFAULT_TEXTURE_HEIGHT;
512

    
513
    while(true)
514
      {
515
      mNumTexCols = DEFAULT_TEXTURE_ROWS;
516
      mNumTexRows = numTextures/mNumTexCols + 1;
517

    
518
      if( mNumTexRows*mTexHeight <= maxSize &&
519
          mNumTexCols*mTexHeight <= maxSize  ) break;
520

    
521
      mTexHeight/=2;
522
      }
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

    
527
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
528
    {
529
    mTexture = new DistortedTexture();
530

    
531
    if( fromJSON )
532
      {
533
      mNumStickerTypes = getNumStickerTypes();
534
      mNumCubitFaces = getNumCubitFaces();
535
      }
536
    else
537
      {
538
      FactoryCubit factory = FactoryCubit.getInstance();
539
      mStickerCoords   = factory.getStickerCoords();
540
      mStickerVariants = factory.getStickerVariants();
541
      mStickerScales   = factory.getStickerScales();
542
      adjustStickerCoords();
543
      mNumStickerTypes = (mStickerCoords==null ? 0 : mStickerCoords.length);
544
      }
545

    
546
    mStickerOverrides = getStickerOverrides();
547
    mNumOverrides = mStickerOverrides==null ? 0 : mStickerOverrides.length;
548

    
549
    mNumTextures= mNumFaceColors*mNumStickerTypes + mNumOverrides;
550
    figureOutBitmapDimensions(mNumTextures);
551
    if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
552

    
553
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
554
    else overrideCubitFaceColor();
555

    
556
    setTexture();
557
    }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560

    
561
  public Metadata getMetadata()
562
    {
563
    return mMetadata;
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  public boolean isInIconMode()
569
    {
570
    return mIconMode==MODE_ICON;
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
  public int getVariantStickerShape(int variant, int face)
576
    {
577
    if( variant <mStickerVariants.length )
578
      {
579
      int[] var = mStickerVariants[variant];
580
      return face>=var.length ? -1 : var[face];
581
      }
582
    return -1;
583
    }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
  public boolean shouldResetTextureMaps()
588
    {
589
    return false;
590
    }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

    
594
  public int[][] getScrambleAlgorithms()
595
    {
596
    return ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngles);
597
    }
598

    
599
///////////////////////////////////////////////////////////////////////////////////////////////////
600

    
601
  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
602
    {
603
    final float MAXERR = 0.05f;
604
    int numAxis = dist.length;
605

    
606
    for(int i=0; i<numAxis; i++)
607
      {
608
      Static3D ax = faceAxis[i];
609
      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
610
      if( len>mSize*dist[i]+MAXERR ) return true;
611
      }
612

    
613
    return false;
614
    }
615

    
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

    
618
  private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
619
    {
620
    float[][] vertices = mShapes[variant].getVertices();
621
    Static3D[] axis = getFaceAxis();
622
    float[] dist3D = getDist3D(mNumLayers);
623

    
624
    for( float[] vertex : vertices)
625
      {
626
      float x = vertex[0];
627
      float y = vertex[1];
628
      float z = vertex[2];
629

    
630
      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
631

    
632
      float mx = tmp[0] + px;
633
      float my = tmp[1] + py;
634
      float mz = tmp[2] + pz;
635

    
636
      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
637
      }
638

    
639
    return true;
640
    }
641

    
642
///////////////////////////////////////////////////////////////////////////////////////////////////
643

    
644
  private float computeAvg(float[] pos, int offset)
645
    {
646
    int len = pos.length/3;
647
    float ret=0.0f;
648
    for(int i=0; i<len; i++) ret += pos[3*i+offset];
649
    ret /= len;
650

    
651
    return ret;
652
    }
653

    
654
///////////////////////////////////////////////////////////////////////////////////////////////////
655

    
656
  protected void displayCubitQuats()
657
    {
658
    StringBuilder builder = new StringBuilder();
659
    float[] tmp = new float[4];
660
    float ERR = 0.01f;
661

    
662
    for(int cubit=0; cubit<mNumCubits; cubit++)
663
      {
664
      builder.append(cubit);
665
      builder.append(" : ");
666

    
667
      int refCubit,variant = getCubitVariant(cubit,mNumLayers);
668

    
669
      for(refCubit=0; refCubit<mNumCubits; refCubit++)
670
        {
671
        if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
672
        }
673

    
674
      float[] curpos = mOrigPos[cubit];
675
      float[] refpos = mOrigPos[refCubit];
676
      float refX = computeAvg(refpos,0);
677
      float refY = computeAvg(refpos,1);
678
      float refZ = computeAvg(refpos,2);
679
      float curX = computeAvg(curpos,0);
680
      float curY = computeAvg(curpos,1);
681
      float curZ = computeAvg(curpos,2);
682

    
683
      for(int quat=0; quat<mNumQuats; quat++)
684
        {
685
        QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
686

    
687
        float dx = tmp[0]-curX;
688
        float dy = tmp[1]-curY;
689
        float dz = tmp[2]-curZ;
690

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

    
705
      builder.append('\n');
706
      }
707

    
708
    android.util.Log.e("D", "cubitQuats: \n"+builder );
709
    }
710

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

    
713
  public int getCubitRotationType(int cubit)
714
    {
715
    return TwistyObjectCubit.TYPE_NORMAL;
716
    }
717

    
718
///////////////////////////////////////////////////////////////////////////////////////////////////
719

    
720
  float[] getTrackingPoint(int cubitIndex, int cubitType)
721
    {
722
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
723
      {
724
      int variant = getCubitVariant(cubitIndex,mNumLayers);
725

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

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

    
750
      for(int i=0; i<faces; i++)
751
        {
752
        if( mVariantFaceIsOuter[variant][i]==1 )
753
          {
754
          outer=i;
755
          break;
756
          }
757
        }
758

    
759
      if( outer>=0 )
760
        {
761
        float[] v = mShapes[variant].getFirstVertexInFace(outer);
762
        float[] ret = new float[3];
763
        float[] curpos = mOrigPos[cubitIndex];
764
        Static4D quat = mOrigQuat[cubitIndex];
765
        QuatHelper.rotateVectorByQuat(mTmp, v[0], v[1], v[2], 1, quat);
766

    
767
        ret[0] = mTmp[0]+computeAvg(curpos,0);
768
        ret[1] = mTmp[1]+computeAvg(curpos,1);
769
        ret[2] = mTmp[2]+computeAvg(curpos,2);
770

    
771
        return ret;
772
        }
773
      else
774
        {
775
        android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
776
        }
777
      }
778

    
779
    return null;
780
    }
781

    
782
///////////////////////////////////////////////////////////////////////////////////////////////////
783

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

    
796
      for(int i=0; i<numAxis; i++)
797
        {
798
        Static3D ax = axis[i];
799
        float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
800
        if( len>mSize*dist3D[i]*MAXERR ) return i;
801
        }
802

    
803
      return -2;
804
      }
805

    
806
    return -1;
807
    }
808

    
809
///////////////////////////////////////////////////////////////////////////////////////////////////
810

    
811
  public float[] getCubitRowOffset(int cubitIndex)
812
    {
813
    return null;
814
    }
815

    
816
///////////////////////////////////////////////////////////////////////////////////////////////////
817

    
818
  void setRotationRowOffset(int puzzleFace, float[] offset)
819
    {
820
    mRowOffsets[puzzleFace][0] = offset[0];
821
    mRowOffsets[puzzleFace][1] = offset[1];
822
    mRowOffsets[puzzleFace][2] = offset[2];
823
    }
824

    
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826

    
827
  int getNumAxis()
828
    {
829
    return mNumAxis;
830
    }
831

    
832
///////////////////////////////////////////////////////////////////////////////////////////////////
833

    
834
  public int[][] getSolvedQuats()
835
    {
836
    return null;
837
    }
838

    
839
///////////////////////////////////////////////////////////////////////////////////////////////////
840

    
841
  protected int[][] getOldSolvedQuats()
842
    {
843
    return mSolved.getSolvedQuats(mNumLayers,mNumCubitFaces,mCubitFaceColors);
844
    }
845

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

    
854
  public int getSolvedFunctionIndex()
855
    {
856
    return 2;
857
    }
858

    
859
///////////////////////////////////////////////////////////////////////////////////////////////////
860

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

    
871
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
872
      {
873
      xoff = mRowOffsets[puzzleFace][0];
874
      yoff = mRowOffsets[puzzleFace][1];
875
      zoff = mRowOffsets[puzzleFace][2];
876
      }
877

    
878
    for(int i=0; i<len; i++)
879
      {
880
      casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
881
      ret |= computeSingleRow(axisIndex,casted);
882
      }
883

    
884
    return ret;
885
    }
886

    
887
///////////////////////////////////////////////////////////////////////////////////////////////////
888

    
889
  private int computeSingleRow(int axisIndex,float casted)
890
    {
891
    int num = mNumCuts[axisIndex];
892
    float[] cuts = mCuts[axisIndex];
893

    
894
    for(int i=0; i<num; i++)
895
      {
896
      if( casted<cuts[i] ) return (1<<i);
897
      }
898

    
899
    return (1<<num);
900
    }
901

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

    
904
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
905
    {
906
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
907
    }
908

    
909
///////////////////////////////////////////////////////////////////////////////////////////////////
910
// note the minus in front of the sin() - we rotate counterclockwise
911
// when looking towards the direction where the axis increases in values.
912

    
913
  private Static4D makeQuaternion(float axisX, float axisY, float axisZ, int angleInDegrees)
914
    {
915
    while( angleInDegrees<0 ) angleInDegrees += 360;
916
    angleInDegrees %= 360;
917
    
918
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
919
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
920

    
921
    return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
922
    }
923

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

    
926
  void rotateAllCubits(int axisIndex, int rowBitmap, int angle)
927
    {
928
    Static3D axis = mAxis[axisIndex];
929
    float axisX = axis.get0();
930
    float axisY = axis.get1();
931
    float axisZ = axis.get2();
932
    Static4D quat = makeQuaternion(axisX,axisY,axisZ,angle);
933

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

    
944
    recomputeFaceOffsets();
945

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

    
961
///////////////////////////////////////////////////////////////////////////////////////////////////
962

    
963
  private synchronized void setupPosition(int[][] moves)
964
    {
965
    if( moves!=null )
966
      {
967
      int axisIndex, row, rowBitmap, basic, angle;
968

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

    
981
        rotateAllCubits(axisIndex,rowBitmap,angle);
982
        }
983

    
984
      for(int i=0; i<mNumCubits; i++)
985
        {
986
        float[] pos = mCubits[i].getCurrentPos();
987
        int len = pos.length/3;
988
        for(int j=0; j<len; j++) clampPos(pos,3*j);
989
        }
990
      }
991
    }
992

    
993
///////////////////////////////////////////////////////////////////////////////////////////////////
994

    
995
  public int getScrambleType()
996
    {
997
    return ObjectScrambler.SCRAMBLING_ALGORITHMS;
998
    }
999

    
1000
///////////////////////////////////////////////////////////////////////////////////////////////////
1001

    
1002
  public int[][] getMinimalCubiesInRow()
1003
    {
1004
    return null;
1005
    }
1006

    
1007
///////////////////////////////////////////////////////////////////////////////////////////////////
1008

    
1009
  int computeBandagedBitmap(int rowBitmap, int axisIndex)
1010
    {
1011
    if( mIsBandaged )
1012
      {
1013
      int bitmap, initBitmap=0;
1014

    
1015
      while( initBitmap!=rowBitmap )
1016
        {
1017
        initBitmap = rowBitmap;
1018

    
1019
        for(int cubit=0; cubit<mNumCubits; cubit++)
1020
          {
1021
          bitmap = mCubits[cubit].getRotRow(axisIndex);
1022
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
1023
          }
1024
        }
1025
      }
1026

    
1027
    if( mMinimalCubiesInRow!=null )
1028
      {
1029
      int[] minC = mMinimalCubiesInRow[axisIndex];
1030
      int numL = minC.length;
1031
      int[] numC = new int[numL];
1032

    
1033
      for(int cubit=0; cubit<mNumCubits; cubit++)
1034
        {
1035
        int bitmap = mCubits[cubit].getRotRow(axisIndex);
1036

    
1037
        for(int i=0; i<numL; i++)
1038
          {
1039
          if( (bitmap&0x1)!=0 ) numC[i]++;
1040
          bitmap>>=1;
1041
          }
1042
        }
1043

    
1044
      int bitmap,initBitmap = 0;
1045

    
1046
      while( initBitmap!=rowBitmap )
1047
        {
1048
        initBitmap = rowBitmap;
1049
        bitmap = rowBitmap;
1050
        int last = 0;
1051

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

    
1068
          last = (bitmap&0x1);
1069
          bitmap>>=1;
1070
          }
1071
        }
1072
      }
1073

    
1074
    return rowBitmap;
1075
    }
1076

    
1077
///////////////////////////////////////////////////////////////////////////////////////////////////
1078

    
1079
  private int computeRowFromBitmap(int rowBitmap)
1080
    {
1081
    int index = 0;
1082

    
1083
    while(index<32)
1084
      {
1085
      if( (rowBitmap&0x1) != 0 ) return index;
1086
      rowBitmap>>=1;
1087
      index++;
1088
      }
1089
    return 0;
1090
    }
1091

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

    
1096
  void clampPos(float[] pos, int offset)
1097
    {
1098
    float currError, minError = Float.MAX_VALUE;
1099
    int minErrorIndex1 = -1;
1100
    int minErrorIndex2 = -1;
1101

    
1102
    float x = pos[offset  ];
1103
    float y = pos[offset+1];
1104
    float z = pos[offset+2];
1105

    
1106
    float xo,yo,zo;
1107

    
1108
    for(int i=0; i<mNumCubits; i++)
1109
      {
1110
      int len = mOrigPos[i].length / 3;
1111

    
1112
      for(int j=0; j<len; j++)
1113
        {
1114
        xo = mOrigPos[i][3*j  ];
1115
        yo = mOrigPos[i][3*j+1];
1116
        zo = mOrigPos[i][3*j+2];
1117

    
1118
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1119

    
1120
        if( currError<minError )
1121
          {
1122
          minError = currError;
1123
          minErrorIndex1 = i;
1124
          minErrorIndex2 = j;
1125
          }
1126
        }
1127
      }
1128

    
1129
    if( minError< 0.05f ) // TODO: 0.05 ?
1130
      {
1131
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1132
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1133
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1134
      }
1135
    }
1136

    
1137
///////////////////////////////////////////////////////////////////////////////////////////////////
1138

    
1139
  void setLibInterface(ObjectLibInterface inter)
1140
    {
1141
    mInterface = inter;
1142
    }
1143

    
1144
///////////////////////////////////////////////////////////////////////////////////////////////////
1145

    
1146
  void applyScrambles(int[][] moves)
1147
    {
1148
    setupPosition(moves);
1149
    }
1150

    
1151
///////////////////////////////////////////////////////////////////////////////////////////////////
1152

    
1153
  void initializeObject(int[][] moves)
1154
    {
1155
    solve();
1156
    setupPosition(moves);
1157
    }
1158

    
1159
///////////////////////////////////////////////////////////////////////////////////////////////////
1160

    
1161
  private void recomputeFaceOffsets()
1162
    {
1163
    if( mThereAreDeciders )
1164
      {
1165
      for(int i=0; i<mNumPuzzleFaces; i++)
1166
        {
1167
        mRowOffsets[i][0] =0;
1168
        mRowOffsets[i][1] =0;
1169
        mRowOffsets[i][2] =0;
1170
        }
1171

    
1172
      for(int i=0; i<mNumCubits; i++)
1173
        if( mCubits[i].getType()==TwistyObjectCubit.TYPE_DECIDER )
1174
          {
1175
          float[] offset = mCubits[i].getOffset();
1176
          int face = mCubits[i].getPuzzleFace();
1177
          mRowOffsets[face][0] = offset[0];
1178
          mRowOffsets[face][1] = offset[1];
1179
          mRowOffsets[face][2] = offset[2];
1180
          }
1181
      }
1182
    }
1183

    
1184
///////////////////////////////////////////////////////////////////////////////////////////////////
1185

    
1186
  synchronized float removeRotation()
1187
    {
1188
    return mRotation.removeRotation();
1189
    }
1190

    
1191
///////////////////////////////////////////////////////////////////////////////////////////////////
1192

    
1193
  long finishRotation(ObjectPreRender pre, int finishAx, int finishRow, float finishAngle, float avgSpeed)
1194
    {
1195
    int basicAngle = mBasicAngles[finishAx][finishRow];
1196
    return mRotation.finishRotation(pre,finishAx,finishRow,basicAngle,finishAngle,avgSpeed);
1197
    }
1198

    
1199
///////////////////////////////////////////////////////////////////////////////////////////////////
1200

    
1201
  void continueRotation(float angleInDegrees)
1202
    {
1203
    mRotation.continueRotation(angleInDegrees);
1204
    }
1205

    
1206
///////////////////////////////////////////////////////////////////////////////////////////////////
1207

    
1208
  synchronized long addRotation(EffectListener listener, int axis, int rowBitmap, int angle, long durationMillis )
1209
    {
1210
    return mRotation.addRotation(listener,axis,rowBitmap,angle,durationMillis);
1211
    }
1212

    
1213
///////////////////////////////////////////////////////////////////////////////////////////////////
1214

    
1215
  void enableGhostAxis(int axNum, boolean enable)
1216
    {
1217
    mTouchControl.enableGhostAxis(axNum,enable);
1218
    }
1219

    
1220
///////////////////////////////////////////////////////////////////////////////////////////////////
1221

    
1222
  synchronized boolean beginRotation(int axis, int row )
1223
    {
1224
    if( axis<0 || axis>=mNumAxis )
1225
      {
1226
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1227
      return false;
1228
      }
1229

    
1230
    return mRotation.beginRotation(axis,row);
1231
    }
1232

    
1233
///////////////////////////////////////////////////////////////////////////////////////////////////
1234

    
1235
  void setTextureMap(int cubit, int face, int color)
1236
    {
1237
    int variant  = getCubitVariant(cubit,mNumLayers);
1238
    int shape    = getVariantStickerShape(variant,face);
1239
    int texIndex = color<0 || shape<0 ? mNumTextures-mNumOverrides : shape*mNumFaceColors + color;
1240
    int row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1241
    int col      = texIndex%mNumTexCols;
1242

    
1243
    final float ratioW = 1.0f/mNumTexCols;
1244
    final float ratioH = 1.0f/mNumTexRows;
1245
    final Static4D[] maps = new Static4D[1];
1246
    maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1247
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1248
    }
1249

    
1250
///////////////////////////////////////////////////////////////////////////////////////////////////
1251
// figure out the whole face to which (cubit,face) belongs, repaint all (cubit,face) pairs to
1252
// the new color.
1253

    
1254
  void repaintPuzzleFace(int cubit, int face, int newColor, float borders, float corners)
1255
    {
1256
    int oldColorIndex = getCubitFaceColor(cubit,face);
1257
    int oldColor = mColorTable[oldColorIndex];
1258

    
1259
    if( oldColor!=newColor )
1260
      {
1261
      changeColorInTexture(borders,corners,oldColor,newColor);
1262
      mColorTable[oldColorIndex] = newColor;
1263
      setTexture();
1264
      }
1265
    }
1266

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

    
1269
  int getCubitFaceColor(int cubit, int face)
1270
    {
1271
    int puzzleFace = getCubitFaceMap(cubit,face);
1272
    if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
1273
    return puzzleFace;
1274
    }
1275

    
1276
///////////////////////////////////////////////////////////////////////////////////////////////////
1277

    
1278
  public int getCubitFaceMap(int cubit, int face)
1279
    {
1280
    int numFaces = mCubitFaceColors[cubit].length;
1281
    int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
1282
    return puzzleFace<0 ? -1 : puzzleFace;
1283
    }
1284

    
1285
///////////////////////////////////////////////////////////////////////////////////////////////////
1286
// this would return the REAL value of outer; the 'getCubitFaceColor()>=0' returns if the face is
1287
// colored, which for example in case of Container (which has colored internal faces) is not the
1288
// same thing
1289

    
1290
  boolean faceIsOuter(int cubit, int face)
1291
    {
1292
    if( mCubitFaceColors==null )
1293
      {
1294
      if( mShapes==null )
1295
        {
1296
        mShapes = new ObjectShape[mNumCubitVariants];
1297
        }
1298

    
1299
      if( mShapes[0]==null )
1300
        {
1301
        for(int v=0; v<mNumCubitVariants; v++) mShapes[v] = getObjectShape(v);
1302
        }
1303

    
1304
      if( mOrigQuat==null )
1305
        {
1306
        mOrigQuat = new Static4D[mNumCubits];
1307
        }
1308

    
1309
      if( mOrigQuat[0]==null )
1310
        {
1311
        for(int c=0; c<mNumCubits; c++) mOrigQuat[c] = getCubitQuats(c,mNumLayers);
1312
        }
1313

    
1314
      mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
1315
      }
1316

    
1317
    return mCubitFaceColors[cubit][face]>=0;
1318
    }
1319

    
1320
///////////////////////////////////////////////////////////////////////////////////////////////////
1321

    
1322
  void resetAllTextureMaps()
1323
    {
1324
    final float ratioW = 1.0f/mNumTexCols;
1325
    final float ratioH = 1.0f/mNumTexRows;
1326
    int cubColor, stiShape, texIndex, variant, row, col;
1327

    
1328
    for(int cubit=0; cubit<mNumCubits; cubit++)
1329
      {
1330
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1331
      variant = getCubitVariant(cubit,mNumLayers);
1332

    
1333
      for(int face=0; face<mNumCubitFaces; face++)
1334
        {
1335
        cubColor = getCubitFaceColor(cubit,face);
1336
        stiShape = getVariantStickerShape(variant,face);
1337
        texIndex = cubColor<0 || stiShape<0 ? mNumTextures-mNumOverrides : stiShape*mNumFaceColors + cubColor;
1338
        row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1339
        col      = texIndex%mNumTexCols;
1340

    
1341
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1342
        }
1343

    
1344
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1345
      }
1346

    
1347
    overrideCubitFaceColor();
1348
    }
1349

    
1350
///////////////////////////////////////////////////////////////////////////////////////////////////
1351

    
1352
  private void overrideCubitFaceColor()
1353
    {
1354
    final float ratioW = 1.0f/mNumTexCols;
1355
    final float ratioH = 1.0f/mNumTexRows;
1356

    
1357
    for(int i=0; i<mNumOverrides; i++)
1358
      {
1359
      int[] cubitFaces = mStickerOverrides[i].getCubitFaces();
1360
      int length = cubitFaces.length/2;
1361

    
1362
      for(int j=0; j<length; j++)
1363
        {
1364
        final Static4D[] maps = new Static4D[1];
1365
        int color = mNumTextures-mNumOverrides+1+i;
1366
        int row   = (mNumTexRows-1) - color/mNumTexCols;
1367
        int col   = color%mNumTexCols;
1368
        int cubit = cubitFaces[2*j];
1369
        int face  = cubitFaces[2*j+1];
1370
        maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1371
        mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1372
        }
1373
      }
1374
    }
1375

    
1376
///////////////////////////////////////////////////////////////////////////////////////////////////
1377

    
1378
  void releaseResources()
1379
    {
1380
    mTexture.markForDeletion();
1381
    mMesh.markForDeletion();
1382
    mEffects.markForDeletion();
1383

    
1384
    if( mBitmap!=null )
1385
      {
1386
      mBitmap.recycle();
1387
      mBitmap = null;
1388
      }
1389

    
1390
    for(int j=0; j<mNumCubits; j++)
1391
      {
1392
      mCubits[j].releaseResources();
1393
      }
1394
    }
1395

    
1396
///////////////////////////////////////////////////////////////////////////////////////////////////
1397

    
1398
  private void setCubitQuat(int cubit, int equAssociation)
1399
    {
1400
    int andAssociation = mCubits[cubit].computeAssociation();
1401

    
1402
    if( !mIsInMixupMode )
1403
      {
1404
      mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
1405
      }
1406
    else
1407
      {
1408
      mMesh.setEffectAssociation(cubit,andAssociation,cubit);
1409
      Static4D tmp = mObjectQuats[equAssociation];
1410
      mMixupModeQuats[cubit].set(tmp);
1411
      }
1412
    }
1413

    
1414
///////////////////////////////////////////////////////////////////////////////////////////////////
1415

    
1416
  synchronized void restorePreferences(OperatingSystemInterface os)
1417
    {
1418
    boolean error = false;
1419
    String key = getShortName();
1420

    
1421
    for(int i=0; i<mNumCubits; i++)
1422
      {
1423
      mQuatDebug[i] = mCubits[i].restorePreferences(key,os);
1424

    
1425
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1426
        {
1427
        boolean result = mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]],true);
1428
        if( !result ) debugQuat(mObjectQuats[mQuatDebug[i]],i,0,0,0,mQuatDebug[i],3);
1429
        }
1430
      else { error = true; break; }
1431
      }
1432

    
1433
    if( !error )
1434
      {
1435
      recomputeFaceOffsets();
1436

    
1437
      for(int i=0; i<mNumCubits; i++)
1438
        {
1439
        if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1440
          {
1441
          mCubits[i].computeRotationRow();
1442
          setCubitQuat(i,mQuatDebug[i]);
1443
          }
1444
        else { error = true; break; }
1445
        }
1446
      }
1447

    
1448
    if( error )
1449
      {
1450
      for(int i=0; i<mNumCubits; i++)
1451
        {
1452
        mCubits[i].solve();
1453
        setCubitQuat(i,0);
1454
        }
1455
      }
1456
    }
1457

    
1458
///////////////////////////////////////////////////////////////////////////////////////////////////
1459

    
1460
  void savePreferences(OperatingSystemInterface os)
1461
    {
1462
    String key = getShortName();
1463
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,os);
1464
    }
1465

    
1466
///////////////////////////////////////////////////////////////////////////////////////////////////
1467

    
1468
  public void removePreferences(OperatingSystemInterface os)
1469
    {
1470
    String key = getShortName();
1471
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,os);
1472
    }
1473

    
1474
///////////////////////////////////////////////////////////////////////////////////////////////////
1475

    
1476
  private float computeRadiusCorrection(float[][] sticker, int curr, int len)
1477
    {
1478
    final float A = 0.8f;  // 0<A<1
1479

    
1480
    int prev = curr>0 ? curr-1 : len-1;
1481
    int next = curr<len-1 ? curr+1 : 0;
1482

    
1483
    float v1x = sticker[prev][0]-sticker[curr][0];
1484
    float v1y = sticker[prev][1]-sticker[curr][1];
1485
    float v2x = sticker[next][0]-sticker[curr][0];
1486
    float v2y = sticker[next][1]-sticker[curr][1];
1487

    
1488
    float len1= v1x*v1x+v1y*v1y;
1489
    float len2= v2x*v2x+v2y*v2y;
1490

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

    
1493
    return 1-A*cos;
1494
    }
1495

    
1496
///////////////////////////////////////////////////////////////////////////////////////////////////
1497
// Radius of the sphere circumscribed on the puzzle. Needed for pillowing.
1498
//
1499
// This won't work correctly for pillowing off-center puzzles (e.g. mirrors) - for those we'd need
1500
// to introduce the concept of a 'sink center' as well.
1501
//
1502
// public because needed in TouchControlShapemod
1503

    
1504
  public float getCircumscribedRadius()
1505
    {
1506
    switch(mNumPuzzleFaces)
1507
      {
1508
      case  4: return (SQ6/4)*mSize;
1509
      case  6: return (SQ3/2)*mSize;
1510
      case  8: return (SQ2/2)*mSize;
1511
      case 12: return (SQ3/2)*((SQ5+1)/2)*mSize;
1512
      case 16: return 0.50f*mSize;
1513
      }
1514

    
1515
    return 0.0f;
1516
    }
1517

    
1518
///////////////////////////////////////////////////////////////////////////////////////////////////
1519
// might be overridden in subclasses which want per-edge radii
1520

    
1521
  protected float[][][] getStickerRadii()
1522
    {
1523
    float radius = getStickerRadius();
1524
    int numStickers = mStickerCoords.length;
1525
    float[][][] ret = new float[numStickers][][];
1526

    
1527
    for(int s=0; s<numStickers; s++)
1528
      {
1529
      int numLoops = mStickerCoords[s].length;
1530
      ret[s] = new float[numLoops][];
1531

    
1532
      for(int l=0; l<numLoops; l++)
1533
        {
1534
        int numVertices = mStickerCoords[s][l].length;
1535
        ret[s][l] = new float[numVertices];
1536
        for(int v=0; v<numVertices; v++) ret[s][l][v] = radius;
1537
        }
1538
      }
1539

    
1540
    return ret;
1541
    }
1542

    
1543
///////////////////////////////////////////////////////////////////////////////////////////////////
1544
// might be overridden in subclasses which want per-edge strokes
1545

    
1546
  protected float[][][] getStickerStrokes()
1547
    {
1548
    float stroke = getStickerStroke();
1549
    int numStickers = mStickerCoords.length;
1550
    float[][][] ret = new float[numStickers][][];
1551

    
1552
    for(int s=0; s<numStickers; s++)
1553
      {
1554
      int numLoops = mStickerCoords[s].length;
1555
      ret[s] = new float[numLoops][];
1556

    
1557
      for(int l=0; l<numLoops; l++)
1558
        {
1559
        int numVertices = mStickerCoords[s][l].length;
1560
        ret[s][l] = new float[numVertices];
1561
        for(int v=0; v<numVertices; v++) ret[s][l][v] = stroke;
1562
        }
1563
      }
1564

    
1565
    return ret;
1566
    }
1567

    
1568
///////////////////////////////////////////////////////////////////////////////////////////////////
1569

    
1570
  public ObjectSticker retSticker(int sticker)
1571
    {
1572
    if( mStickers==null )
1573
      {
1574
      float[][][] radii   = getStickerRadii();
1575
      float[][][] strokes = getStickerStrokes();
1576
      float[][][] angles  = getStickerAngles();
1577
      int numStickers = mStickerCoords.length;
1578
      mStickers = new ObjectSticker[numStickers];
1579

    
1580
      for(int s=0; s<numStickers; s++)
1581
        {
1582
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1583
        int numLoops = mStickerCoords[s].length;
1584
        float[][] rad = new float[numLoops][];
1585
        float[][] str = new float[numLoops][];
1586

    
1587
        for(int l=0; l<numLoops; l++)
1588
          {
1589
          int numVerts = mStickerCoords[s][l].length;
1590
          rad[l] = new float[numVerts];
1591
          str[l] = new float[numVerts];
1592
          float[] st = strokes[s][l];
1593
          float[] ra = radii[s][l];
1594

    
1595
          for(int v=0; v<numVerts; v++)
1596
            {
1597
            rad[l][v] = ra[v] * computeRadiusCorrection(mStickerCoords[s][l],v,numVerts) / scale;
1598
            str[l][v] = st[v] / scale;
1599
            }
1600
          }
1601

    
1602
        mStickers[s] = new ObjectSticker(mStickerCoords[s], (angles==null ? null : angles[s]) ,rad, str);
1603
        }
1604
      }
1605

    
1606
    return mStickers[sticker];
1607
    }
1608

    
1609
///////////////////////////////////////////////////////////////////////////////////////////////////
1610
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1611

    
1612
  public void adjustStickerCoords()
1613
    {
1614

    
1615
    }
1616

    
1617
///////////////////////////////////////////////////////////////////////////////////////////////////
1618

    
1619
  public Static4D[] getQuats()
1620
    {
1621
    if( mObjectQuats==null )
1622
      {
1623
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1624
      }
1625

    
1626
    return mObjectQuats;
1627
    }
1628

    
1629
///////////////////////////////////////////////////////////////////////////////////////////////////
1630

    
1631
  public int[][] getVariantFaceIsOuter()
1632
    {
1633
    return mVariantFaceIsOuter;
1634
    }
1635

    
1636
///////////////////////////////////////////////////////////////////////////////////////////////////
1637

    
1638
  public int getInternalColor()
1639
    {
1640
    return COLOR_INTERNAL;
1641
    }
1642

    
1643
///////////////////////////////////////////////////////////////////////////////////////////////////
1644
// the getFaceColors + final INTERNAL_COLOR in a grid (so that we do not exceed the maximum texture size)
1645

    
1646
  void createTexture(float borders, float corners)
1647
    {
1648
    Paint paint = new Paint();
1649
    Canvas canvas = new Canvas(mBitmap);
1650

    
1651
    paint.setAntiAlias(true);
1652
    paint.setTextAlign(Paint.Align.CENTER);
1653
    paint.setStyle(Paint.Style.FILL);
1654
    paint.setColor(getInternalColor());
1655
    canvas.drawRect(0, 0, mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, paint);
1656

    
1657
    int texture = 0;
1658
    FactorySticker factory = FactorySticker.getInstance();
1659

    
1660
    for(int row=0; row<mNumTexRows; row++)
1661
      for(int col=0; col<mNumTexCols; col++)
1662
        {
1663
        if( texture<mNumTextures-mNumOverrides )
1664
          {
1665
          ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1666
          int color = mColorTable[texture%mNumFaceColors];
1667
          factory.drawRoundedPolygons(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color, mTexHeight, sticker,borders,corners);
1668
          }
1669
        else if( texture>mNumTextures-mNumOverrides && texture<=mNumTextures )
1670
          {
1671
          int color = mStickerOverrides[mNumTextures-texture].getColor();
1672
          factory.drawSolidColor(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color, mTexHeight);
1673
          }
1674

    
1675
        texture++;
1676
        }
1677
    }
1678

    
1679
///////////////////////////////////////////////////////////////////////////////////////////////////
1680

    
1681
  void changeColorInTexture(float borders, float corners, int oldColor, int newColor)
1682
    {
1683
    Paint paint = new Paint();
1684
    Canvas canvas = new Canvas(mBitmap);
1685
    paint.setAntiAlias(true);
1686
    int texture = 0;
1687
    FactorySticker factory = FactorySticker.getInstance();
1688

    
1689
    for(int row=0; row<mNumTexRows; row++)
1690
      for(int col=0; col<mNumTexCols; col++)
1691
        {
1692
        if( texture<mNumTextures-mNumOverrides )
1693
          {
1694
          int color = mColorTable[texture%mNumFaceColors];
1695

    
1696
          if( color==oldColor )
1697
            {
1698
            ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1699
            factory.drawRoundedPolygons(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, newColor, mTexHeight, sticker,borders,corners);
1700
            }
1701
          }
1702

    
1703
        texture++;
1704
        }
1705
    }
1706

    
1707
///////////////////////////////////////////////////////////////////////////////////////////////////
1708

    
1709
  void setTexture()
1710
    {
1711
    if( mBitmap==null )
1712
      {
1713
      mBitmap = Bitmap.createBitmap( mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, Bitmap.Config.ARGB_4444);
1714
      createTexture(1.0f,1.0f);
1715
      }
1716

    
1717
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1718
      {
1719
      int max = DistortedLibrary.getMaxTextureSize();
1720
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1721
      }
1722
    }
1723

    
1724
///////////////////////////////////////////////////////////////////////////////////////////////////
1725

    
1726
  void setObjectRatioNow(float sc, int nodeSize)
1727
    {
1728
    mObjectScreenRatio = sc;
1729
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1730
    mObjectScale.set(scale,scale,scale);
1731

    
1732
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1733
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1734
    }
1735

    
1736
///////////////////////////////////////////////////////////////////////////////////////////////////
1737

    
1738
  void setObjectRatio(float sizeChange, int nodeSize)
1739
    {
1740
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1741

    
1742
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1743
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1744

    
1745
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1746
    }
1747

    
1748
///////////////////////////////////////////////////////////////////////////////////////////////////
1749

    
1750
  void setNodeSize(int nodeSize)
1751
    {
1752
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1753
    }
1754

    
1755
///////////////////////////////////////////////////////////////////////////////////////////////////
1756

    
1757
  public float getRatio()
1758
    {
1759
    return mObjectScreenRatio;
1760
    }
1761

    
1762
///////////////////////////////////////////////////////////////////////////////////////////////////
1763

    
1764
  public float getObjectRatio()
1765
    {
1766
    return mObjectScreenRatio*mInitScreenRatio;
1767
    }
1768

    
1769
///////////////////////////////////////////////////////////////////////////////////////////////////
1770

    
1771
  boolean isSolved()
1772
    {
1773
    boolean solved = mSolved.isSolved(mCubits);
1774
    return mRotation.isSolved(solved);
1775
    }
1776

    
1777
///////////////////////////////////////////////////////////////////////////////////////////////////
1778
// INTERNAL API - those are called from 'effects' package
1779
///////////////////////////////////////////////////////////////////////////////////////////////////
1780

    
1781
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1782
    {
1783
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1784
    }
1785

    
1786
///////////////////////////////////////////////////////////////////////////////////////////////////
1787

    
1788
  public Static4D getRotationQuat()
1789
    {
1790
    return mQuat;
1791
    }
1792

    
1793
///////////////////////////////////////////////////////////////////////////////////////////////////
1794

    
1795
  public float getSize()
1796
    {
1797
    return mSize;
1798
    }
1799

    
1800
///////////////////////////////////////////////////////////////////////////////////////////////////
1801

    
1802
  public void applyEffect(Effect effect, int position)
1803
    {
1804
    mEffects.apply(effect, position);
1805
    }
1806

    
1807
///////////////////////////////////////////////////////////////////////////////////////////////////
1808

    
1809
  public void removeEffect(long effectID)
1810
    {
1811
    mEffects.abortById(effectID);
1812
    }
1813

    
1814
///////////////////////////////////////////////////////////////////////////////////////////////////
1815

    
1816
  public MeshBase getObjectMesh()
1817
    {
1818
    return mMesh;
1819
    }
1820

    
1821
///////////////////////////////////////////////////////////////////////////////////////////////////
1822

    
1823
  public DistortedEffects getObjectEffects()
1824
    {
1825
    return mEffects;
1826
    }
1827

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

    
1830
  public int getCubitType(int cubit)
1831
    {
1832
    return mCubits[cubit].getType();
1833
    }
1834

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

    
1837
  public float[] getCubitOffset(int cubit)
1838
    {
1839
    return mCubits[cubit].getOffset();
1840
    }
1841

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

    
1844
  public ObjectStickerOverride[] getStickerOverrides()
1845
    {
1846
    return null;
1847
    }
1848

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

    
1851
  public boolean getError()
1852
    {
1853
    return mError;
1854
    }
1855

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

    
1858
  public String getErrorString()
1859
    {
1860
    return mErrorString;
1861
    }
1862

    
1863
///////////////////////////////////////////////////////////////////////////////////////////////////
1864
// PUBLIC API
1865
///////////////////////////////////////////////////////////////////////////////////////////////////
1866

    
1867
  public int getCubitFaceStickerIndex(int cubit, int face)
1868
    {
1869
    Static4D texMap = mMesh.getTextureMap(mNumCubitFaces*cubit + face);
1870

    
1871
    int x = (int)(texMap.get0()/texMap.get2());
1872
    int y = (int)(texMap.get1()/texMap.get3());
1873

    
1874
    return (mNumTexRows-1-y)*mNumTexCols + x;
1875
    }
1876

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

    
1879
  public int[] getNumLayers()
1880
    {
1881
    return mNumLayers;
1882
    }
1883

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

    
1886
  public synchronized void solve()
1887
    {
1888
    for(int i=0; i<mNumCubits; i++) mCubits[i].solve();
1889

    
1890
    recomputeFaceOffsets();
1891

    
1892
    for(int i=0; i<mNumCubits; i++)
1893
      {
1894
      mCubits[i].computeRotationRow();
1895
      setCubitQuat(i,0);
1896
      }
1897
    }
1898

    
1899
///////////////////////////////////////////////////////////////////////////////////////////////////
1900

    
1901
  public int getCubitQuatIndex(int cubit)
1902
    {
1903
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1904
    }
1905

    
1906
///////////////////////////////////////////////////////////////////////////////////////////////////
1907

    
1908
  public int getCubitRotRow(int cubit, int axis)
1909
    {
1910
    return mCubits[cubit].getRotRow(axis);
1911
    }
1912

    
1913
///////////////////////////////////////////////////////////////////////////////////////////////////
1914

    
1915
  public Bitmap getStickerBitmap()
1916
    {
1917
    return mBitmap;
1918
    }
1919

    
1920
///////////////////////////////////////////////////////////////////////////////////////////////////
1921

    
1922
  public DistortedNode getNode()
1923
    {
1924
    return mNode;
1925
    }
1926

    
1927
///////////////////////////////////////////////////////////////////////////////////////////////////
1928

    
1929
  public int getNumStickerTypes()
1930
    {
1931
    return mNumStickerTypes;
1932
    }
1933

    
1934
///////////////////////////////////////////////////////////////////////////////////////////////////
1935

    
1936
  public String reportState()
1937
    {
1938
    StringBuilder builder = new StringBuilder();
1939

    
1940
    for(int i=0; i<mNumCubits; i++ )
1941
      {
1942
      if( i>0 ) builder.append('.');
1943
      builder.append(mCubits[i].mQuatIndex);
1944
      }
1945

    
1946
    return builder.toString();
1947
    }
1948

    
1949
///////////////////////////////////////////////////////////////////////////////////////////////////
1950
// this is here only so it can be overridden in TwistyJSON so that we can get this from JSON.
1951

    
1952
  public int getNumCubitFaces()
1953
    {
1954
    return 0;
1955
    }
1956

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

    
1962
  public float getPillowCoeff()
1963
    {
1964
    return 1.0f;
1965
    }
1966

    
1967
///////////////////////////////////////////////////////////////////////////////////////////////////
1968

    
1969
  public TouchControl getTouchControl()
1970
    {
1971
    if( mTouchControl==null )
1972
      {
1973
      switch(getTouchControlType())
1974
        {
1975
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
1976
                                   break;
1977
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
1978
                                   break;
1979
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
1980
                                   break;
1981
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
1982
                                   break;
1983
        case TC_ICOSAHEDRON      : mTouchControl = new TouchControlIcosahedron(this);
1984
                                   break;
1985
        case TC_CUBOID           : int[] numLayers = getNumLayers();
1986
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1987
                                   break;
1988
        case TC_BALL             : mTouchControl = new TouchControlBall(this);
1989
                                   break;
1990
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
1991
                                   break;
1992
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
1993
                                   break;
1994
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
1995
                                   break;
1996
        }
1997
      }
1998
    return mTouchControl;
1999
    }
2000

    
2001
///////////////////////////////////////////////////////////////////////////////////////////////////
2002

    
2003
  public float[][] returnRotationFactor()
2004
    {
2005
    float[][] factor = new float[mNumAxis][];
2006

    
2007
    for(int ax=0; ax<mNumAxis; ax++)
2008
      {
2009
      int numL = mNumLayers[ax];
2010
      factor[ax] = new float[numL];
2011
      for(int la=0; la<numL; la++) factor[ax][la] = 1.0f;
2012
      }
2013

    
2014
    return factor;
2015
    }
2016

    
2017
///////////////////////////////////////////////////////////////////////////////////////////////////
2018

    
2019
  protected void setReader(JsonReader reader)
2020
    {
2021
    // empty
2022
    }
2023

    
2024
///////////////////////////////////////////////////////////////////////////////////////////////////
2025

    
2026
  public String getInventor()
2027
    {
2028
    return mMetadata.getAuthor();
2029
    }
2030

    
2031
///////////////////////////////////////////////////////////////////////////////////////////////////
2032

    
2033
  public int getYearOfInvention()
2034
    {
2035
    return mMetadata.getYearOfInvention();
2036
    }
2037

    
2038
///////////////////////////////////////////////////////////////////////////////////////////////////
2039

    
2040
  public float getComplexity()
2041
    {
2042
    return mMetadata.getDifficulty();
2043
    }
2044

    
2045
///////////////////////////////////////////////////////////////////////////////////////////////////
2046

    
2047
  public String getObjectName()
2048
    {
2049
    return mMetadata.getObjectName();
2050
    }
2051

    
2052
///////////////////////////////////////////////////////////////////////////////////////////////////
2053

    
2054
  public ObjectSignature getSignature()
2055
    {
2056
    return mMetadata.getSignature();
2057
    }
2058

    
2059
///////////////////////////////////////////////////////////////////////////////////////////////////
2060
  // for JSON only
2061
  public abstract int getTouchControlType();
2062
  public abstract int getTouchControlSplit();
2063
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
2064
  public abstract int[][][] getEnabled();
2065
  public abstract float[] getDist3D(int[] numLayers);
2066
  public abstract Static3D[] getFaceAxis();
2067
  public abstract int[][] getScrambleEdges();
2068
  public abstract float[][] getCuts(int[] numLayers);
2069
  public abstract float getStickerRadius();
2070
  public abstract float getStickerStroke();
2071
  public abstract float[][][] getStickerAngles();
2072
  public abstract int getCubitVariant(int cubit, int[] numLayers);
2073
  public abstract ObjectShape getObjectShape(int variant);
2074
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
2075
  public abstract ObjectVertexEffects getVertexEffects(int variant);
2076
  public abstract int getNumCubitVariants(int[] numLayers);
2077
  public abstract float[][] getCubitPositions(int[] numLayers);
2078
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
2079
  public abstract float getScreenRatio();
2080
  public abstract int[] getColorTable();
2081
  public abstract String getShortName();
2082

    
2083
  // not only for JSON
2084
  public abstract Static3D[] getRotationAxis();
2085
  public abstract int[][] getBasicAngles();
2086
  public abstract int getNumFaces();
2087
  public abstract int getFOV();
2088
  public abstract String[][] getTutorials();
2089
  }
(6-6/9)