Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 7e8750c9

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

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

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

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

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

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

    
229
    mMinimalCubiesInRow = getMinimalCubiesInRow();
230
    mNumCubits = mOrigPos.length;
231
    mNumFaceColors = getNumFaceColors();
232
    mBelongs = new boolean[mNumCubits];
233

    
234
    int scramblingType = getScrambleType();
235
    int[][] edges = getScrambleEdges();
236
    int[][] algorithms = getScrambleAlgorithms();
237

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

    
242
    boolean bandaged=false;
243

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

    
255
    mObjectScale = new Static3D(scale,scale,scale);
256
    setObjectRatioNow(scale,720);
257

    
258
    mEffects = new DistortedEffects();
259
    createQuaternionEffects();
260

    
261
    mRotation = new TwistyLayerRotations(this,mAxis,mNumLayers,getGhostAngle(),mEffects);
262

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

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

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

    
277
    mEffects.apply(quatEffect);
278
    mEffects.apply(scaleEffect);
279
    mEffects.apply(moveEffect);
280

    
281
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285
// in degrees so that everything can be treated modulo 360
286

    
287
  public int getGhostAngle()
288
    {
289
    return 0;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  private TablebasesAbstract getTablebase(OperatingSystemInterface os)
295
    {
296
    String shortName = getShortName();
297
    return ImplementedTablebasesList.createPacked(os,shortName);
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  private void createQuaternionEffects()
303
    {
304
    if( mNumQuats<=ObjectControl.MAX_QUATS )
305
      {
306
      mIsInMixupMode = false;
307

    
308
      for( int q=0; q<mNumQuats; q++)
309
        {
310
        VertexEffectQuaternion vq = new VertexEffectQuaternion(mObjectQuats[q],CENTER);
311
        vq.setMeshAssociation(0,q);
312
        mEffects.apply(vq);
313
        }
314
      }
315
    else if( mNumCubits<=ObjectControl.MAX_QUATS )
316
      {
317
      mIsInMixupMode = true;
318
      mMixupModeQuats = new Static4D[mNumCubits];
319

    
320
      for( int q=0; q<mNumCubits; q++)
321
        {
322
        mMixupModeQuats[q] = new Static4D(mObjectQuats[0]);
323
        VertexEffectQuaternion vq = new VertexEffectQuaternion(mMixupModeQuats[q],CENTER);
324
        vq.setMeshAssociation(0,q);
325
        mEffects.apply(vq);
326
        }
327
      }
328
    else
329
      {
330
      android.util.Log.e("D", "object has too many quaternions ("+mNumQuats+") or too many cubits ("+mNumCubits+")");
331
      }
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  private Static3D getPos(float[] origPos)
337
    {
338
    int len = origPos.length/3;
339
    float sumX = 0.0f;
340
    float sumY = 0.0f;
341
    float sumZ = 0.0f;
342

    
343
    for(int i=0; i<len; i++)
344
      {
345
      sumX += origPos[3*i  ];
346
      sumY += origPos[3*i+1];
347
      sumZ += origPos[3*i+2];
348
      }
349

    
350
    sumX /= len;
351
    sumY /= len;
352
    sumZ /= len;
353

    
354
    return new Static3D(sumX,sumY,sumZ);
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  private void createOuterFaces()
360
    {
361
    for(int v=0; v<mNumCubitVariants; v++)
362
      {
363
      int numFaces = mShapes[v].getNumFaces();
364
      mVariantFaceIsOuter[v] = new int[numFaces];
365
      }
366

    
367
    for( int cubit=0; cubit<mNumCubits; cubit++)
368
      {
369
      int variant = getCubitVariant(cubit,mNumLayers);
370
      int numFaces = mShapes[variant].getNumFaces();
371

    
372
      for(int face=0; face<numFaces; face++)
373
        if( getCubitFaceColor(cubit,face)>=0 )
374
          {
375
          mVariantFaceIsOuter[variant][face] = 1;
376
          }
377
      }
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
  private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
383
    {
384
    mNumCubitVariants = getNumCubitVariants(mNumLayers);
385

    
386
    if( !fromDMESH || !fromJSON )
387
      {
388
      FactoryCubit factory = FactoryCubit.getInstance();
389
      factory.clear();
390

    
391
      mShapes = new ObjectShape[mNumCubitVariants];
392
      for(int v=0; v<mNumCubitVariants; v++) mShapes[v] = getObjectShape(v);
393
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
394
      mVariantFaceIsOuter = new int[mNumCubitVariants][];
395

    
396
      mOrigQuat = new Static4D[mNumCubits];
397
      for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
398

    
399
      if( !fromJSON )
400
        {
401
        mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
402
        createOuterFaces();
403
        }
404

    
405
      if( fromDMESH )
406
        {
407
        for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i], mVariantFaceIsOuter[i]);
408
        }
409
      }
410
    }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
  private void createMeshAndCubits(InputStream stream, boolean fromDMESH)
415
    {
416
    mCubits = new TwistyObjectCubit[mNumCubits];
417

    
418
    if( fromDMESH )
419
      {
420
      DataInputStream dos = new DataInputStream(stream);
421
      mMesh = new MeshFile(dos);
422

    
423
      try
424
        {
425
        stream.close();
426
        }
427
      catch(IOException e)
428
        {
429
        android.util.Log.e("meshFile", "Error closing InputStream: "+e);
430
        }
431
      }
432
    else
433
      {
434
      MeshBase[] cubitMesh = new MeshBase[mNumCubits];
435

    
436
      for(int i=0; i<mNumCubits; i++)
437
        {
438
        cubitMesh[i] = createCubitMesh(i,mNumLayers,mNumCubitFaces);
439
        Static3D pos = getPos(mOrigPos[i]);
440
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
441
        }
442

    
443
      mMesh = new MeshJoined(cubitMesh);
444

    
445
      float pillowCoeff = getPillowCoeff();
446

    
447
      if( pillowCoeff!=1.0f )
448
        {
449
        float radius = getCircumscribedRadius();
450
        Static1D coeff = new Static1D(pillowCoeff);
451
        Static4D region= new Static4D(0,0,0,radius);
452
        VertexEffectSink sink = new VertexEffectSink(coeff,CENTER,region);
453
        mMesh.apply(sink);
454
        }
455
      }
456

    
457
    for(int i=0; i<mNumCubits; i++)
458
      {
459
      mCubits[i] = new TwistyObjectCubit(this,mOrigPos[i],mNumAxis,mMaxNumLayers,i);
460
      setCubitQuat(i,0);
461

    
462
      if( !mThereAreDeciders && mCubits[i].getType()==TwistyObjectCubit.TYPE_DECIDER )
463
        {
464
        mThereAreDeciders = true;
465
        }
466
      }
467
    }
468

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

    
471
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int numComponents)
472
    {
473
    int variant = getCubitVariant(cubit,numLayers);
474

    
475
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
476

    
477
    if( mMeshes[variant]==null )
478
      {
479
      ObjectFaceShape faceShape = getObjectFaceShape(variant);
480
      ObjectVertexEffects effects = getVertexEffects(variant);
481
      FactoryCubit factory = FactoryCubit.getInstance();
482
      factory.createNewFaceTransform(mShapes[variant],mVariantFaceIsOuter[variant]);
483
      mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,effects,numComponents);
484
      }
485

    
486
    MeshBase mesh = mMeshes[variant].copy(true);
487
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
488
    mesh.apply(quat,0xffffffff,0);
489

    
490
    return mesh;
491
    }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
  private void figureOutBitmapDimensions(int numTextures)
496
    {
497
    int maxSize = DistortedLibrary.getMaxTextureSize();
498

    
499
    mTexHeight  = DEFAULT_TEXTURE_HEIGHT;
500

    
501
    while(true)
502
      {
503
      mNumTexCols = DEFAULT_TEXTURE_ROWS;
504
      mNumTexRows = numTextures/mNumTexCols + 1;
505

    
506
      if( mNumTexRows*mTexHeight <= maxSize &&
507
          mNumTexCols*mTexHeight <= maxSize  ) break;
508

    
509
      mTexHeight/=2;
510
      }
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
516
    {
517
    mTexture = new DistortedTexture();
518

    
519
    if( fromJSON )
520
      {
521
      mNumStickerTypes = getNumStickerTypes();
522
      mNumCubitFaces = getNumCubitFaces();
523
      }
524
    else
525
      {
526
      FactoryCubit factory = FactoryCubit.getInstance();
527
      mStickerCoords   = factory.getStickerCoords();
528
      mStickerVariants = factory.getStickerVariants();
529
      mStickerScales   = factory.getStickerScales();
530
      adjustStickerCoords();
531
      mNumStickerTypes = (mStickerCoords==null ? 0 : mStickerCoords.length);
532
      }
533

    
534
    mStickerOverrides = getStickerOverrides();
535
    mNumOverrides = mStickerOverrides==null ? 0 : mStickerOverrides.length;
536

    
537
    mNumTextures= mNumFaceColors*mNumStickerTypes + mNumOverrides;
538
    figureOutBitmapDimensions(mNumTextures);
539
    if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
540

    
541
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
542
    else overrideCubitFaceColor();
543

    
544
    setTexture();
545
    }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
  public Metadata getMetadata()
550
    {
551
    return mMetadata;
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
  public boolean isInIconMode()
557
    {
558
    return mIconMode==MODE_ICON;
559
    }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
  public int getVariantStickerShape(int variant, int face)
564
    {
565
    if( variant <mStickerVariants.length )
566
      {
567
      int[] var = mStickerVariants[variant];
568
      return face>=var.length ? -1 : var[face];
569
      }
570
    return -1;
571
    }
572

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

    
575
  public boolean shouldResetTextureMaps()
576
    {
577
    return false;
578
    }
579

    
580
///////////////////////////////////////////////////////////////////////////////////////////////////
581

    
582
  public int[][] getScrambleAlgorithms()
583
    {
584
    return ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngles);
585
    }
586

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588

    
589
  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
590
    {
591
    final float MAXERR = 0.05f;
592
    int numAxis = dist.length;
593

    
594
    for(int i=0; i<numAxis; i++)
595
      {
596
      Static3D ax = faceAxis[i];
597
      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
598
      if( len>mSize*dist[i]+MAXERR ) return true;
599
      }
600

    
601
    return false;
602
    }
603

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

    
606
  private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
607
    {
608
    float[][] vertices = mShapes[variant].getVertices();
609
    Static3D[] axis = getFaceAxis();
610
    float[] dist3D = getDist3D(mNumLayers);
611

    
612
    for( float[] vertex : vertices)
613
      {
614
      float x = vertex[0];
615
      float y = vertex[1];
616
      float z = vertex[2];
617

    
618
      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
619

    
620
      float mx = tmp[0] + px;
621
      float my = tmp[1] + py;
622
      float mz = tmp[2] + pz;
623

    
624
      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
625
      }
626

    
627
    return true;
628
    }
629

    
630
///////////////////////////////////////////////////////////////////////////////////////////////////
631

    
632
  private float computeAvg(float[] pos, int offset)
633
    {
634
    int len = pos.length/3;
635
    float ret=0.0f;
636
    for(int i=0; i<len; i++) ret += pos[3*i+offset];
637
    ret /= len;
638

    
639
    return ret;
640
    }
641

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

    
644
  protected void displayCubitQuats()
645
    {
646
    StringBuilder builder = new StringBuilder();
647
    float[] tmp = new float[4];
648
    float ERR = 0.01f;
649

    
650
    for(int cubit=0; cubit<mNumCubits; cubit++)
651
      {
652
      builder.append(cubit);
653
      builder.append(" : ");
654

    
655
      int refCubit,variant = getCubitVariant(cubit,mNumLayers);
656

    
657
      for(refCubit=0; refCubit<mNumCubits; refCubit++)
658
        {
659
        if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
660
        }
661

    
662
      float[] curpos = mOrigPos[cubit];
663
      float[] refpos = mOrigPos[refCubit];
664
      float refX = computeAvg(refpos,0);
665
      float refY = computeAvg(refpos,1);
666
      float refZ = computeAvg(refpos,2);
667
      float curX = computeAvg(curpos,0);
668
      float curY = computeAvg(curpos,1);
669
      float curZ = computeAvg(curpos,2);
670

    
671
      for(int quat=0; quat<mNumQuats; quat++)
672
        {
673
        QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
674

    
675
        float dx = tmp[0]-curX;
676
        float dy = tmp[1]-curY;
677
        float dz = tmp[2]-curZ;
678

    
679
        if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
680
          {
681
          if( doesNotStickOut(variant,curX,curY,curZ,tmp,mObjectQuats[quat]) )
682
            {
683
            builder.append(quat);
684
            builder.append(',');
685
            }
686
          else
687
            {
688
            android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
689
            }
690
          }
691
        }
692

    
693
      builder.append('\n');
694
      }
695

    
696
    android.util.Log.e("D", "cubitQuats: \n"+builder );
697
    }
698

    
699
///////////////////////////////////////////////////////////////////////////////////////////////////
700

    
701
  public int getCubitRotationType(int cubit)
702
    {
703
    return TwistyObjectCubit.TYPE_NORMAL;
704
    }
705

    
706
///////////////////////////////////////////////////////////////////////////////////////////////////
707

    
708
  float[] getTrackingPoint(int cubitIndex, int cubitType)
709
    {
710
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
711
      {
712
      int variant = getCubitVariant(cubitIndex,mNumLayers);
713

    
714
      // object must have been created from JSON
715
      if( mVariantFaceIsOuter==null || mVariantFaceIsOuter[variant]==null )
716
        {
717
        mVariantFaceIsOuter = getVariantFaceIsOuter();
718
        }
719
      if( mShapes==null )
720
        {
721
        mShapes = new ObjectShape[mNumCubitVariants];
722
        }
723
      if( mShapes[variant]==null )
724
        {
725
        mShapes[variant] = getObjectShape(variant);
726
        }
727
      if( mOrigQuat==null )
728
        {
729
        mOrigQuat = new Static4D[mNumCubits];
730
        }
731
      if( mOrigQuat[cubitIndex]==null )
732
        {
733
        mOrigQuat[cubitIndex] = getCubitQuats(cubitIndex,mNumLayers);
734
        }
735

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

    
738
      for(int i=0; i<faces; i++)
739
        {
740
        if( mVariantFaceIsOuter[variant][i]==1 )
741
          {
742
          outer=i;
743
          break;
744
          }
745
        }
746

    
747
      if( outer>=0 )
748
        {
749
        float[] v = mShapes[variant].getFirstVertexInFace(outer);
750
        float[] ret = new float[3];
751
        float[] curpos = mOrigPos[cubitIndex];
752
        Static4D quat = mOrigQuat[cubitIndex];
753
        QuatHelper.rotateVectorByQuat(mTmp, v[0], v[1], v[2], 1, quat);
754

    
755
        ret[0] = mTmp[0]+computeAvg(curpos,0);
756
        ret[1] = mTmp[1]+computeAvg(curpos,1);
757
        ret[2] = mTmp[2]+computeAvg(curpos,2);
758

    
759
        return ret;
760
        }
761
      else
762
        {
763
        android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
764
        }
765
      }
766

    
767
    return null;
768
    }
769

    
770
///////////////////////////////////////////////////////////////////////////////////////////////////
771

    
772
  public int computeCurrentPuzzleFace(int type, float[] vertex)
773
    {
774
    if( type!=TwistyObjectCubit.TYPE_NORMAL )
775
      {
776
      Static3D[] axis = getFaceAxis();
777
      float[] dist3D = getDist3D(mNumLayers);
778
      final float MAXERR = 0.98f;
779
      int numAxis = axis.length;
780
      float x = vertex[0];
781
      float y = vertex[1];
782
      float z = vertex[2];
783

    
784
      for(int i=0; i<numAxis; i++)
785
        {
786
        Static3D ax = axis[i];
787
        float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
788
        if( len>mSize*dist3D[i]*MAXERR ) return i;
789
        }
790

    
791
      return -2;
792
      }
793

    
794
    return -1;
795
    }
796

    
797
///////////////////////////////////////////////////////////////////////////////////////////////////
798

    
799
  public float[] getCubitRowOffset(int cubitIndex)
800
    {
801
    return null;
802
    }
803

    
804
///////////////////////////////////////////////////////////////////////////////////////////////////
805

    
806
  void setRotationRowOffset(int puzzleFace, float[] offset)
807
    {
808
    mRowOffsets[puzzleFace][0] = offset[0];
809
    mRowOffsets[puzzleFace][1] = offset[1];
810
    mRowOffsets[puzzleFace][2] = offset[2];
811
    }
812

    
813
///////////////////////////////////////////////////////////////////////////////////////////////////
814

    
815
  int getNumAxis()
816
    {
817
    return mNumAxis;
818
    }
819

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////
821

    
822
  public int[][] getSolvedQuats()
823
    {
824
    return null;
825
    }
826

    
827
///////////////////////////////////////////////////////////////////////////////////////////////////
828

    
829
  protected int[][] getOldSolvedQuats()
830
    {
831
    return mSolved.getSolvedQuats(mNumLayers,mNumCubitFaces,mCubitFaceColors);
832
    }
833

    
834
///////////////////////////////////////////////////////////////////////////////////////////////////
835
// 0: old, groups-of-quaternions based function.
836
// Still used by: Masterball, Mirror Sqewb, Mirror Jing, Mirror Pyraminx & the Penrose Cubes.
837
//
838
// 1: specific function only for the Dino4.
839
//
840
// 2: the new default; automatic way based on automatic detection of monochromatic puzzle surfaces.
841

    
842
  public int getSolvedFunctionIndex()
843
    {
844
    return 2;
845
    }
846

    
847
///////////////////////////////////////////////////////////////////////////////////////////////////
848

    
849
  int computeRow(float[] pos, int axisIndex, int cubitType, int puzzleFace)
850
    {
851
    int ret=0;
852
    int len = pos.length / 3;
853
    Static3D axis = mAxis[axisIndex];
854
    float axisX = axis.get0();
855
    float axisY = axis.get1();
856
    float axisZ = axis.get2();
857
    float casted, xoff=0, yoff=0, zoff=0;
858

    
859
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
860
      {
861
      xoff = mRowOffsets[puzzleFace][0];
862
      yoff = mRowOffsets[puzzleFace][1];
863
      zoff = mRowOffsets[puzzleFace][2];
864
      }
865

    
866
    for(int i=0; i<len; i++)
867
      {
868
      casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
869
      ret |= computeSingleRow(axisIndex,casted);
870
      }
871

    
872
    return ret;
873
    }
874

    
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876

    
877
  private int computeSingleRow(int axisIndex,float casted)
878
    {
879
    int num = mNumCuts[axisIndex];
880
    float[] cuts = mCuts[axisIndex];
881

    
882
    for(int i=0; i<num; i++)
883
      {
884
      if( casted<cuts[i] ) return (1<<i);
885
      }
886

    
887
    return (1<<num);
888
    }
889

    
890
///////////////////////////////////////////////////////////////////////////////////////////////////
891

    
892
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
893
    {
894
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
895
    }
896

    
897
///////////////////////////////////////////////////////////////////////////////////////////////////
898
// note the minus in front of the sin() - we rotate counterclockwise
899
// when looking towards the direction where the axis increases in values.
900

    
901
  private Static4D makeQuaternion(float axisX, float axisY, float axisZ, int angleInDegrees)
902
    {
903
    while( angleInDegrees<0 ) angleInDegrees += 360;
904
    angleInDegrees %= 360;
905
    
906
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
907
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
908

    
909
    return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
910
    }
911

    
912
///////////////////////////////////////////////////////////////////////////////////////////////////
913

    
914
  void rotateAllCubits(int axisIndex, int rowBitmap, int angle)
915
    {
916
    Static3D axis = mAxis[axisIndex];
917
    float axisX = axis.get0();
918
    float axisY = axis.get1();
919
    float axisZ = axis.get2();
920
    Static4D quat = makeQuaternion(axisX,axisY,axisZ,angle);
921

    
922
    for(int i=0; i<mNumCubits; i++)
923
      {
924
      mBelongs[i] = belongsToRotation(i,axisIndex,rowBitmap);
925
      if( mBelongs[i] )
926
        {
927
        boolean result = mCubits[i].rotateCubit(quat,false);
928
        if( !result ) debugQuat(quat,i,axisX,axisY,axisZ,angle,1);
929
        }
930
      }
931

    
932
    recomputeFaceOffsets();
933

    
934
    for(int i=0; i<mNumCubits; i++)
935
      {
936
      if( mBelongs[i] )
937
        {
938
        int index = mCubits[i].postRotateCubit(quat);
939
        setCubitQuat(i,index);
940
        }
941
      else if( mCubits[i].getType()==TwistyObjectCubit.TYPE_FOLLOWER )
942
        {
943
        mCubits[i].computeRotationRow();
944
        setCubitQuat(i,mCubits[i].mQuatIndex);
945
        }
946
      }
947
    }
948

    
949
///////////////////////////////////////////////////////////////////////////////////////////////////
950

    
951
  private synchronized void setupPosition(int[][] moves)
952
    {
953
    if( moves!=null )
954
      {
955
      int axisIndex, row, rowBitmap, basic, angle;
956

    
957
      for(int[] move: moves)
958
        {
959
        axisIndex= move[0];
960
        rowBitmap= computeBandagedBitmap( move[1],axisIndex);
961
        row      = computeRowFromBitmap( move[1] );
962
        basic    = mBasicAngles[axisIndex][row];
963
        angle    = move[2]*(360/basic);   // this assumes that all layers from
964
                                          // the bitmap have the same BasicAngle.
965
                                          // at the moment this is always true as
966
                                          // there are no bandaged objects with
967
                                          // different per-layer BasicAngles.
968

    
969
        rotateAllCubits(axisIndex,rowBitmap,angle);
970
        }
971

    
972
      for(int i=0; i<mNumCubits; i++)
973
        {
974
        float[] pos = mCubits[i].getCurrentPos();
975
        int len = pos.length/3;
976
        for(int j=0; j<len; j++) clampPos(pos,3*j);
977
        }
978
      }
979
    }
980

    
981
///////////////////////////////////////////////////////////////////////////////////////////////////
982

    
983
  public int getScrambleType()
984
    {
985
    return ObjectScrambler.SCRAMBLING_ALGORITHMS;
986
    }
987

    
988
///////////////////////////////////////////////////////////////////////////////////////////////////
989

    
990
  public int[][] getMinimalCubiesInRow()
991
    {
992
    return null;
993
    }
994

    
995
///////////////////////////////////////////////////////////////////////////////////////////////////
996

    
997
  int computeBandagedBitmap(int rowBitmap, int axisIndex)
998
    {
999
    if( mIsBandaged )
1000
      {
1001
      int bitmap, initBitmap=0;
1002

    
1003
      while( initBitmap!=rowBitmap )
1004
        {
1005
        initBitmap = rowBitmap;
1006

    
1007
        for(int cubit=0; cubit<mNumCubits; cubit++)
1008
          {
1009
          bitmap = mCubits[cubit].getRotRow(axisIndex);
1010
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
1011
          }
1012
        }
1013
      }
1014

    
1015
    if( mMinimalCubiesInRow!=null )
1016
      {
1017
      int[] minC = mMinimalCubiesInRow[axisIndex];
1018
      int numL = minC.length;
1019
      int[] numC = new int[numL];
1020

    
1021
      for(int cubit=0; cubit<mNumCubits; cubit++)
1022
        {
1023
        int bitmap = mCubits[cubit].getRotRow(axisIndex);
1024

    
1025
        for(int i=0; i<numL; i++)
1026
          {
1027
          if( (bitmap&0x1)!=0 ) numC[i]++;
1028
          bitmap>>=1;
1029
          }
1030
        }
1031

    
1032
      int bitmap,initBitmap = 0;
1033

    
1034
      while( initBitmap!=rowBitmap )
1035
        {
1036
        initBitmap = rowBitmap;
1037
        bitmap = rowBitmap;
1038
        int last = 0;
1039

    
1040
        for(int i=0; i<numL; i++)
1041
          {
1042
          if( numC[i]<minC[i] && numC[i]>0 )
1043
            {
1044
            if( (bitmap&0x1)!=0 )
1045
              {
1046
              if( i>0     ) rowBitmap |= (1<<(i-1));
1047
              if( i<numL-1) rowBitmap |= (1<<(i+1));
1048
              }
1049
            else if( (bitmap&0x2)!=0 || last>0 ) // we are not rotating this row, but
1050
                                                 // we are rotating the next or the prev
1051
              {
1052
              rowBitmap |= (1<<i);
1053
              }
1054
            }
1055

    
1056
          last = (bitmap&0x1);
1057
          bitmap>>=1;
1058
          }
1059
        }
1060
      }
1061

    
1062
    return rowBitmap;
1063
    }
1064

    
1065
///////////////////////////////////////////////////////////////////////////////////////////////////
1066

    
1067
  private int computeRowFromBitmap(int rowBitmap)
1068
    {
1069
    int index = 0;
1070

    
1071
    while(index<32)
1072
      {
1073
      if( (rowBitmap&0x1) != 0 ) return index;
1074
      rowBitmap>>=1;
1075
      index++;
1076
      }
1077
    return 0;
1078
    }
1079

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

    
1084
  void clampPos(float[] pos, int offset)
1085
    {
1086
    float currError, minError = Float.MAX_VALUE;
1087
    int minErrorIndex1 = -1;
1088
    int minErrorIndex2 = -1;
1089

    
1090
    float x = pos[offset  ];
1091
    float y = pos[offset+1];
1092
    float z = pos[offset+2];
1093

    
1094
    float xo,yo,zo;
1095

    
1096
    for(int i=0; i<mNumCubits; i++)
1097
      {
1098
      int len = mOrigPos[i].length / 3;
1099

    
1100
      for(int j=0; j<len; j++)
1101
        {
1102
        xo = mOrigPos[i][3*j  ];
1103
        yo = mOrigPos[i][3*j+1];
1104
        zo = mOrigPos[i][3*j+2];
1105

    
1106
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1107

    
1108
        if( currError<minError )
1109
          {
1110
          minError = currError;
1111
          minErrorIndex1 = i;
1112
          minErrorIndex2 = j;
1113
          }
1114
        }
1115
      }
1116

    
1117
    if( minError< 0.05f ) // TODO: 0.05 ?
1118
      {
1119
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1120
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1121
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1122
      }
1123
    }
1124

    
1125
///////////////////////////////////////////////////////////////////////////////////////////////////
1126

    
1127
  void setLibInterface(ObjectLibInterface inter)
1128
    {
1129
    mInterface = inter;
1130
    }
1131

    
1132
///////////////////////////////////////////////////////////////////////////////////////////////////
1133

    
1134
  void applyScrambles(int[][] moves)
1135
    {
1136
    setupPosition(moves);
1137
    }
1138

    
1139
///////////////////////////////////////////////////////////////////////////////////////////////////
1140

    
1141
  void initializeObject(int[][] moves)
1142
    {
1143
    solve();
1144
    setupPosition(moves);
1145
    }
1146

    
1147
///////////////////////////////////////////////////////////////////////////////////////////////////
1148

    
1149
  private void recomputeFaceOffsets()
1150
    {
1151
    if( mThereAreDeciders )
1152
      {
1153
      for(int i=0; i<mNumPuzzleFaces; i++)
1154
        {
1155
        mRowOffsets[i][0] =0;
1156
        mRowOffsets[i][1] =0;
1157
        mRowOffsets[i][2] =0;
1158
        }
1159

    
1160
      for(int i=0; i<mNumCubits; i++)
1161
        if( mCubits[i].getType()==TwistyObjectCubit.TYPE_DECIDER )
1162
          {
1163
          float[] offset = mCubits[i].getOffset();
1164
          int face = mCubits[i].getPuzzleFace();
1165
          mRowOffsets[face][0] = offset[0];
1166
          mRowOffsets[face][1] = offset[1];
1167
          mRowOffsets[face][2] = offset[2];
1168
          }
1169
      }
1170
    }
1171

    
1172
///////////////////////////////////////////////////////////////////////////////////////////////////
1173

    
1174
  synchronized float removeRotation()
1175
    {
1176
    return mRotation.removeRotation();
1177
    }
1178

    
1179
///////////////////////////////////////////////////////////////////////////////////////////////////
1180

    
1181
  long finishRotation(ObjectPreRender pre, int finishAx, int finishRow, float finishAngle, float avgSpeed)
1182
    {
1183
    int basicAngle = mBasicAngles[finishAx][finishRow];
1184
    return mRotation.finishRotation(pre,finishAx,finishRow,basicAngle,finishAngle,avgSpeed);
1185
    }
1186

    
1187
///////////////////////////////////////////////////////////////////////////////////////////////////
1188

    
1189
  void continueRotation(float angleInDegrees)
1190
    {
1191
    mRotation.continueRotation(angleInDegrees);
1192
    }
1193

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

    
1196
  synchronized long addRotation(EffectListener listener, int axis, int rowBitmap, int angle, long durationMillis )
1197
    {
1198
    return mRotation.addRotation(listener,axis,rowBitmap,angle,durationMillis);
1199
    }
1200

    
1201
///////////////////////////////////////////////////////////////////////////////////////////////////
1202

    
1203
  void enableGhostAxis(int axNum, boolean enable)
1204
    {
1205
    mTouchControl.enableGhostAxis(axNum,enable);
1206
    }
1207

    
1208
///////////////////////////////////////////////////////////////////////////////////////////////////
1209

    
1210
  synchronized boolean beginRotation(int axis, int row )
1211
    {
1212
    if( axis<0 || axis>=mNumAxis )
1213
      {
1214
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1215
      return false;
1216
      }
1217

    
1218
    return mRotation.beginRotation(axis,row);
1219
    }
1220

    
1221
///////////////////////////////////////////////////////////////////////////////////////////////////
1222

    
1223
  void setTextureMap(int cubit, int face, int color)
1224
    {
1225
    int variant  = getCubitVariant(cubit,mNumLayers);
1226
    int shape    = getVariantStickerShape(variant,face);
1227
    int texIndex = color<0 || shape<0 ? mNumTextures-mNumOverrides : shape*mNumFaceColors + color;
1228
    int row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1229
    int col      = texIndex%mNumTexCols;
1230

    
1231
    final float ratioW = 1.0f/mNumTexCols;
1232
    final float ratioH = 1.0f/mNumTexRows;
1233
    final Static4D[] maps = new Static4D[1];
1234
    maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1235
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1236
    }
1237

    
1238
///////////////////////////////////////////////////////////////////////////////////////////////////
1239
// figure out the whole face to which (cubit,face) belongs, repaint all (cubit,face) pairs to
1240
// the new color.
1241

    
1242
  void repaintPuzzleFace(int cubit, int face, int color, float borders, float corners)
1243
    {
1244
    int oldColorIndex = getCubitFaceColor(cubit,face);
1245
    int oldColor = getColor(oldColorIndex);
1246

    
1247
    if( oldColor!=color )
1248
      {
1249
      changeColorInTexture(borders,corners,oldColor,color);
1250
      setTexture();
1251
      }
1252
    }
1253

    
1254
///////////////////////////////////////////////////////////////////////////////////////////////////
1255

    
1256
  int getCubitFaceColor(int cubit, int face)
1257
    {
1258
    int puzzleFace = getCubitFaceMap(cubit,face);
1259
    if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
1260
    return puzzleFace;
1261
    }
1262

    
1263
///////////////////////////////////////////////////////////////////////////////////////////////////
1264

    
1265
  public int getCubitFaceMap(int cubit, int face)
1266
    {
1267
    int numFaces = mCubitFaceColors[cubit].length;
1268
    int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
1269
    return puzzleFace<0 ? -1 : puzzleFace;
1270
    }
1271

    
1272
///////////////////////////////////////////////////////////////////////////////////////////////////
1273
// this would return the REAL value of outer; the 'getCubitFaceColor()>=0' returns if the face is
1274
// colored, which for example in case of Container (which has colored internal faces) is not the
1275
// same thing
1276

    
1277
  boolean faceIsOuter(int cubit, int face)
1278
    {
1279
    if( mCubitFaceColors==null )
1280
      {
1281
      if( mShapes==null )
1282
        {
1283
        mShapes = new ObjectShape[mNumCubitVariants];
1284
        }
1285

    
1286
      if( mShapes[0]==null )
1287
        {
1288
        for(int v=0; v<mNumCubitVariants; v++) mShapes[v] = getObjectShape(v);
1289
        }
1290

    
1291
      if( mOrigQuat==null )
1292
        {
1293
        mOrigQuat = new Static4D[mNumCubits];
1294
        }
1295

    
1296
      if( mOrigQuat[0]==null )
1297
        {
1298
        for(int c=0; c<mNumCubits; c++) mOrigQuat[c] = getCubitQuats(c,mNumLayers);
1299
        }
1300

    
1301
      mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
1302
      }
1303

    
1304
    return mCubitFaceColors[cubit][face]>=0;
1305
    }
1306

    
1307
///////////////////////////////////////////////////////////////////////////////////////////////////
1308

    
1309
  void resetAllTextureMaps()
1310
    {
1311
    final float ratioW = 1.0f/mNumTexCols;
1312
    final float ratioH = 1.0f/mNumTexRows;
1313
    int cubColor, stiShape, texIndex, variant, row, col;
1314

    
1315
    for(int cubit=0; cubit<mNumCubits; cubit++)
1316
      {
1317
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1318
      variant = getCubitVariant(cubit,mNumLayers);
1319

    
1320
      for(int face=0; face<mNumCubitFaces; face++)
1321
        {
1322
        cubColor = getCubitFaceColor(cubit,face);
1323
        stiShape = getVariantStickerShape(variant,face);
1324
        texIndex = cubColor<0 || stiShape<0 ? mNumTextures-mNumOverrides : stiShape*mNumFaceColors + cubColor;
1325
        row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1326
        col      = texIndex%mNumTexCols;
1327

    
1328
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1329
        }
1330

    
1331
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1332
      }
1333

    
1334
    overrideCubitFaceColor();
1335
    }
1336

    
1337
///////////////////////////////////////////////////////////////////////////////////////////////////
1338

    
1339
  private void overrideCubitFaceColor()
1340
    {
1341
    final float ratioW = 1.0f/mNumTexCols;
1342
    final float ratioH = 1.0f/mNumTexRows;
1343

    
1344
    for(int i=0; i<mNumOverrides; i++)
1345
      {
1346
      int[] cubitFaces = mStickerOverrides[i].getCubitFaces();
1347
      int length = cubitFaces.length/2;
1348

    
1349
      for(int j=0; j<length; j++)
1350
        {
1351
        final Static4D[] maps = new Static4D[1];
1352
        int color = mNumTextures-mNumOverrides+1+i;
1353
        int row   = (mNumTexRows-1) - color/mNumTexCols;
1354
        int col   = color%mNumTexCols;
1355
        int cubit = cubitFaces[2*j];
1356
        int face  = cubitFaces[2*j+1];
1357
        maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1358
        mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1359
        }
1360
      }
1361
    }
1362

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

    
1365
  void releaseResources()
1366
    {
1367
    mTexture.markForDeletion();
1368
    mMesh.markForDeletion();
1369
    mEffects.markForDeletion();
1370

    
1371
    if( mBitmap!=null )
1372
      {
1373
      mBitmap.recycle();
1374
      mBitmap = null;
1375
      }
1376

    
1377
    for(int j=0; j<mNumCubits; j++)
1378
      {
1379
      mCubits[j].releaseResources();
1380
      }
1381
    }
1382

    
1383
///////////////////////////////////////////////////////////////////////////////////////////////////
1384

    
1385
  private void setCubitQuat(int cubit, int equAssociation)
1386
    {
1387
    int andAssociation = mCubits[cubit].computeAssociation();
1388

    
1389
    if( !mIsInMixupMode )
1390
      {
1391
      mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
1392
      }
1393
    else
1394
      {
1395
      mMesh.setEffectAssociation(cubit,andAssociation,cubit);
1396
      Static4D tmp = mObjectQuats[equAssociation];
1397
      mMixupModeQuats[cubit].set(tmp);
1398
      }
1399
    }
1400

    
1401
///////////////////////////////////////////////////////////////////////////////////////////////////
1402

    
1403
  synchronized void restorePreferences(OperatingSystemInterface os)
1404
    {
1405
    boolean error = false;
1406
    String key = getShortName();
1407

    
1408
    for(int i=0; i<mNumCubits; i++)
1409
      {
1410
      mQuatDebug[i] = mCubits[i].restorePreferences(key,os);
1411

    
1412
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1413
        {
1414
        boolean result = mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]],true);
1415
        if( !result ) debugQuat(mObjectQuats[mQuatDebug[i]],i,0,0,0,mQuatDebug[i],3);
1416
        }
1417
      else { error = true; break; }
1418
      }
1419

    
1420
    if( !error )
1421
      {
1422
      recomputeFaceOffsets();
1423

    
1424
      for(int i=0; i<mNumCubits; i++)
1425
        {
1426
        if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1427
          {
1428
          mCubits[i].computeRotationRow();
1429
          setCubitQuat(i,mQuatDebug[i]);
1430
          }
1431
        else { error = true; break; }
1432
        }
1433
      }
1434

    
1435
    if( error )
1436
      {
1437
      for(int i=0; i<mNumCubits; i++)
1438
        {
1439
        mCubits[i].solve();
1440
        setCubitQuat(i,0);
1441
        }
1442
      }
1443
    }
1444

    
1445
///////////////////////////////////////////////////////////////////////////////////////////////////
1446

    
1447
  void savePreferences(OperatingSystemInterface os)
1448
    {
1449
    String key = getShortName();
1450
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,os);
1451
    }
1452

    
1453
///////////////////////////////////////////////////////////////////////////////////////////////////
1454

    
1455
  public void removePreferences(OperatingSystemInterface os)
1456
    {
1457
    String key = getShortName();
1458
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,os);
1459
    }
1460

    
1461
///////////////////////////////////////////////////////////////////////////////////////////////////
1462

    
1463
  private float computeRadiusCorrection(float[][] sticker, int curr, int len)
1464
    {
1465
    final float A = 0.8f;  // 0<A<1
1466

    
1467
    int prev = curr>0 ? curr-1 : len-1;
1468
    int next = curr<len-1 ? curr+1 : 0;
1469

    
1470
    float v1x = sticker[prev][0]-sticker[curr][0];
1471
    float v1y = sticker[prev][1]-sticker[curr][1];
1472
    float v2x = sticker[next][0]-sticker[curr][0];
1473
    float v2y = sticker[next][1]-sticker[curr][1];
1474

    
1475
    float len1= v1x*v1x+v1y*v1y;
1476
    float len2= v2x*v2x+v2y*v2y;
1477

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

    
1480
    return 1-A*cos;
1481
    }
1482

    
1483
///////////////////////////////////////////////////////////////////////////////////////////////////
1484
// Radius of the sphere circumscribed on the puzzle. Needed for pillowing.
1485
//
1486
// This won't work correctly for pillowing off-center puzzles (e.g. mirrors) - for those we'd need
1487
// to introduce the concept of a 'sink center' as well.
1488
//
1489
// public because needed in TouchControlShapemod
1490

    
1491
  public float getCircumscribedRadius()
1492
    {
1493
    switch(mNumPuzzleFaces)
1494
      {
1495
      case  4: return (SQ6/4)*mSize;
1496
      case  6: return (SQ3/2)*mSize;
1497
      case  8: return (SQ2/2)*mSize;
1498
      case 12: return (SQ3/2)*((SQ5+1)/2)*mSize;
1499
      case 16: return 0.50f*mSize;
1500
      }
1501

    
1502
    return 0.0f;
1503
    }
1504

    
1505
///////////////////////////////////////////////////////////////////////////////////////////////////
1506
// might be overridden in subclasses which want per-edge radii
1507

    
1508
  protected float[][][] getStickerRadii()
1509
    {
1510
    float radius = getStickerRadius();
1511
    int numStickers = mStickerCoords.length;
1512
    float[][][] ret = new float[numStickers][][];
1513

    
1514
    for(int s=0; s<numStickers; s++)
1515
      {
1516
      int numLoops = mStickerCoords[s].length;
1517
      ret[s] = new float[numLoops][];
1518

    
1519
      for(int l=0; l<numLoops; l++)
1520
        {
1521
        int numVertices = mStickerCoords[s][l].length;
1522
        ret[s][l] = new float[numVertices];
1523
        for(int v=0; v<numVertices; v++) ret[s][l][v] = radius;
1524
        }
1525
      }
1526

    
1527
    return ret;
1528
    }
1529

    
1530
///////////////////////////////////////////////////////////////////////////////////////////////////
1531
// might be overridden in subclasses which want per-edge strokes
1532

    
1533
  protected float[][][] getStickerStrokes()
1534
    {
1535
    float stroke = getStickerStroke();
1536
    int numStickers = mStickerCoords.length;
1537
    float[][][] ret = new float[numStickers][][];
1538

    
1539
    for(int s=0; s<numStickers; s++)
1540
      {
1541
      int numLoops = mStickerCoords[s].length;
1542
      ret[s] = new float[numLoops][];
1543

    
1544
      for(int l=0; l<numLoops; l++)
1545
        {
1546
        int numVertices = mStickerCoords[s][l].length;
1547
        ret[s][l] = new float[numVertices];
1548
        for(int v=0; v<numVertices; v++) ret[s][l][v] = stroke;
1549
        }
1550
      }
1551

    
1552
    return ret;
1553
    }
1554

    
1555
///////////////////////////////////////////////////////////////////////////////////////////////////
1556

    
1557
  public ObjectSticker retSticker(int sticker)
1558
    {
1559
    if( mStickers==null )
1560
      {
1561
      float[][][] radii   = getStickerRadii();
1562
      float[][][] strokes = getStickerStrokes();
1563
      float[][][] angles  = getStickerAngles();
1564
      int numStickers = mStickerCoords.length;
1565
      mStickers = new ObjectSticker[numStickers];
1566

    
1567
      for(int s=0; s<numStickers; s++)
1568
        {
1569
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1570
        int numLoops = mStickerCoords[s].length;
1571
        float[][] rad = new float[numLoops][];
1572
        float[][] str = new float[numLoops][];
1573

    
1574
        for(int l=0; l<numLoops; l++)
1575
          {
1576
          int numVerts = mStickerCoords[s][l].length;
1577
          rad[l] = new float[numVerts];
1578
          str[l] = new float[numVerts];
1579
          float[] st = strokes[s][l];
1580
          float[] ra = radii[s][l];
1581

    
1582
          for(int v=0; v<numVerts; v++)
1583
            {
1584
            rad[l][v] = ra[v] * computeRadiusCorrection(mStickerCoords[s][l],v,numVerts) / scale;
1585
            str[l][v] = st[v] / scale;
1586
            }
1587
          }
1588

    
1589
        mStickers[s] = new ObjectSticker(mStickerCoords[s], (angles==null ? null : angles[s]) ,rad, str);
1590
        }
1591
      }
1592

    
1593
    return mStickers[sticker];
1594
    }
1595

    
1596
///////////////////////////////////////////////////////////////////////////////////////////////////
1597
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1598

    
1599
  public void adjustStickerCoords()
1600
    {
1601

    
1602
    }
1603

    
1604
///////////////////////////////////////////////////////////////////////////////////////////////////
1605

    
1606
  public Static4D[] getQuats()
1607
    {
1608
    if( mObjectQuats==null )
1609
      {
1610
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1611
      }
1612

    
1613
    return mObjectQuats;
1614
    }
1615

    
1616
///////////////////////////////////////////////////////////////////////////////////////////////////
1617

    
1618
  public int[][] getVariantFaceIsOuter()
1619
    {
1620
    return mVariantFaceIsOuter;
1621
    }
1622

    
1623
///////////////////////////////////////////////////////////////////////////////////////////////////
1624

    
1625
  public int getInternalColor()
1626
    {
1627
    return COLOR_INTERNAL;
1628
    }
1629

    
1630
///////////////////////////////////////////////////////////////////////////////////////////////////
1631
// the getFaceColors + final INTERNAL_COLOR in a grid (so that we do not exceed the maximum texture size)
1632

    
1633
  void createTexture(float borders, float corners)
1634
    {
1635
    Paint paint = new Paint();
1636
    Canvas canvas = new Canvas(mBitmap);
1637

    
1638
    paint.setAntiAlias(true);
1639
    paint.setTextAlign(Paint.Align.CENTER);
1640
    paint.setStyle(Paint.Style.FILL);
1641
    paint.setColor(getInternalColor());
1642
    canvas.drawRect(0, 0, mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, paint);
1643

    
1644
    int texture = 0;
1645
    FactorySticker factory = FactorySticker.getInstance();
1646

    
1647
    for(int row=0; row<mNumTexRows; row++)
1648
      for(int col=0; col<mNumTexCols; col++)
1649
        {
1650
        if( texture<mNumTextures-mNumOverrides )
1651
          {
1652
          ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1653
          int color = getColor(texture%mNumFaceColors);
1654
          factory.drawRoundedPolygons(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color, mTexHeight, sticker,borders,corners);
1655
          }
1656
        else if( texture>mNumTextures-mNumOverrides && texture<=mNumTextures )
1657
          {
1658
          int color = mStickerOverrides[mNumTextures-texture].getColor();
1659
          factory.drawSolidColor(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, color, mTexHeight);
1660
          }
1661

    
1662
        texture++;
1663
        }
1664
    }
1665

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

    
1668
  void changeColorInTexture(float borders, float corners, int oldColor, int newColor)
1669
    {
1670
    Paint paint = new Paint();
1671
    Canvas canvas = new Canvas(mBitmap);
1672
    paint.setAntiAlias(true);
1673
    int texture = 0;
1674
    FactorySticker factory = FactorySticker.getInstance();
1675

    
1676
    for(int row=0; row<mNumTexRows; row++)
1677
      for(int col=0; col<mNumTexCols; col++)
1678
        {
1679
        if( texture<mNumTextures-mNumOverrides )
1680
          {
1681
          int color = getColor(texture%mNumFaceColors);
1682

    
1683
          if( color==oldColor )
1684
            {
1685
            ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1686
            factory.drawRoundedPolygons(canvas, paint, col*mTexHeight, (mNumTexRows-row)*mTexHeight, newColor, mTexHeight, sticker,borders,corners);
1687
            }
1688
          }
1689

    
1690
        texture++;
1691
        }
1692
    }
1693

    
1694
///////////////////////////////////////////////////////////////////////////////////////////////////
1695

    
1696
  void setTexture()
1697
    {
1698
    if( mBitmap==null )
1699
      {
1700
      mBitmap = Bitmap.createBitmap( mNumTexCols*mTexHeight, mNumTexRows*mTexHeight, Bitmap.Config.ARGB_4444);
1701
      createTexture(1.0f,1.0f);
1702
      }
1703

    
1704
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1705
      {
1706
      int max = DistortedLibrary.getMaxTextureSize();
1707
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1708
      }
1709
    }
1710

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

    
1713
  void setObjectRatioNow(float sc, int nodeSize)
1714
    {
1715
    mObjectScreenRatio = sc;
1716
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1717
    mObjectScale.set(scale,scale,scale);
1718

    
1719
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1720
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1721
    }
1722

    
1723
///////////////////////////////////////////////////////////////////////////////////////////////////
1724

    
1725
  void setObjectRatio(float sizeChange, int nodeSize)
1726
    {
1727
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1728

    
1729
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1730
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1731

    
1732
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1733
    }
1734

    
1735
///////////////////////////////////////////////////////////////////////////////////////////////////
1736

    
1737
  void setNodeSize(int nodeSize)
1738
    {
1739
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1740
    }
1741

    
1742
///////////////////////////////////////////////////////////////////////////////////////////////////
1743

    
1744
  public float getRatio()
1745
    {
1746
    return mObjectScreenRatio;
1747
    }
1748

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

    
1751
  public float getObjectRatio()
1752
    {
1753
    return mObjectScreenRatio*mInitScreenRatio;
1754
    }
1755

    
1756
///////////////////////////////////////////////////////////////////////////////////////////////////
1757

    
1758
  boolean isSolved()
1759
    {
1760
    boolean solved = mSolved.isSolved(mCubits);
1761
    return mRotation.isSolved(solved);
1762
    }
1763

    
1764
///////////////////////////////////////////////////////////////////////////////////////////////////
1765
// INTERNAL API - those are called from 'effects' package
1766
///////////////////////////////////////////////////////////////////////////////////////////////////
1767

    
1768
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1769
    {
1770
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1771
    }
1772

    
1773
///////////////////////////////////////////////////////////////////////////////////////////////////
1774

    
1775
  public Static4D getRotationQuat()
1776
    {
1777
    return mQuat;
1778
    }
1779

    
1780
///////////////////////////////////////////////////////////////////////////////////////////////////
1781

    
1782
  public float getSize()
1783
    {
1784
    return mSize;
1785
    }
1786

    
1787
///////////////////////////////////////////////////////////////////////////////////////////////////
1788

    
1789
  public void applyEffect(Effect effect, int position)
1790
    {
1791
    mEffects.apply(effect, position);
1792
    }
1793

    
1794
///////////////////////////////////////////////////////////////////////////////////////////////////
1795

    
1796
  public void removeEffect(long effectID)
1797
    {
1798
    mEffects.abortById(effectID);
1799
    }
1800

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

    
1803
  public MeshBase getObjectMesh()
1804
    {
1805
    return mMesh;
1806
    }
1807

    
1808
///////////////////////////////////////////////////////////////////////////////////////////////////
1809

    
1810
  public DistortedEffects getObjectEffects()
1811
    {
1812
    return mEffects;
1813
    }
1814

    
1815
///////////////////////////////////////////////////////////////////////////////////////////////////
1816

    
1817
  public int getCubitType(int cubit)
1818
    {
1819
    return mCubits[cubit].getType();
1820
    }
1821

    
1822
///////////////////////////////////////////////////////////////////////////////////////////////////
1823

    
1824
  public float[] getCubitOffset(int cubit)
1825
    {
1826
    return mCubits[cubit].getOffset();
1827
    }
1828

    
1829
///////////////////////////////////////////////////////////////////////////////////////////////////
1830

    
1831
  public ObjectStickerOverride[] getStickerOverrides()
1832
    {
1833
    return null;
1834
    }
1835

    
1836
///////////////////////////////////////////////////////////////////////////////////////////////////
1837

    
1838
  public boolean getError()
1839
    {
1840
    return mError;
1841
    }
1842

    
1843
///////////////////////////////////////////////////////////////////////////////////////////////////
1844

    
1845
  public String getErrorString()
1846
    {
1847
    return mErrorString;
1848
    }
1849

    
1850
///////////////////////////////////////////////////////////////////////////////////////////////////
1851
// PUBLIC API
1852
///////////////////////////////////////////////////////////////////////////////////////////////////
1853

    
1854
  public int getCubitFaceStickerIndex(int cubit, int face)
1855
    {
1856
    Static4D texMap = mMesh.getTextureMap(mNumCubitFaces*cubit + face);
1857

    
1858
    int x = (int)(texMap.get0()/texMap.get2());
1859
    int y = (int)(texMap.get1()/texMap.get3());
1860

    
1861
    return (mNumTexRows-1-y)*mNumTexCols + x;
1862
    }
1863

    
1864
///////////////////////////////////////////////////////////////////////////////////////////////////
1865

    
1866
  public int[] getNumLayers()
1867
    {
1868
    return mNumLayers;
1869
    }
1870

    
1871
///////////////////////////////////////////////////////////////////////////////////////////////////
1872

    
1873
  public synchronized void solve()
1874
    {
1875
    for(int i=0; i<mNumCubits; i++) mCubits[i].solve();
1876

    
1877
    recomputeFaceOffsets();
1878

    
1879
    for(int i=0; i<mNumCubits; i++)
1880
      {
1881
      mCubits[i].computeRotationRow();
1882
      setCubitQuat(i,0);
1883
      }
1884
    }
1885

    
1886
///////////////////////////////////////////////////////////////////////////////////////////////////
1887

    
1888
  public int getCubitQuatIndex(int cubit)
1889
    {
1890
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1891
    }
1892

    
1893
///////////////////////////////////////////////////////////////////////////////////////////////////
1894

    
1895
  public int getCubitRotRow(int cubit, int axis)
1896
    {
1897
    return mCubits[cubit].getRotRow(axis);
1898
    }
1899

    
1900
///////////////////////////////////////////////////////////////////////////////////////////////////
1901

    
1902
  public Bitmap getStickerBitmap()
1903
    {
1904
    return mBitmap;
1905
    }
1906

    
1907
///////////////////////////////////////////////////////////////////////////////////////////////////
1908

    
1909
  public DistortedNode getNode()
1910
    {
1911
    return mNode;
1912
    }
1913

    
1914
///////////////////////////////////////////////////////////////////////////////////////////////////
1915

    
1916
  public int getNumStickerTypes()
1917
    {
1918
    return mNumStickerTypes;
1919
    }
1920

    
1921
///////////////////////////////////////////////////////////////////////////////////////////////////
1922

    
1923
  public String reportState()
1924
    {
1925
    StringBuilder builder = new StringBuilder();
1926

    
1927
    for(int i=0; i<mNumCubits; i++ )
1928
      {
1929
      if( i>0 ) builder.append('.');
1930
      builder.append(mCubits[i].mQuatIndex);
1931
      }
1932

    
1933
    return builder.toString();
1934
    }
1935

    
1936
///////////////////////////////////////////////////////////////////////////////////////////////////
1937
// this is here only so it can be overridden in TwistyJSON so that we can get this from JSON.
1938

    
1939
  public int getNumCubitFaces()
1940
    {
1941
    return 0;
1942
    }
1943

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

    
1949
  public float getPillowCoeff()
1950
    {
1951
    return 1.0f;
1952
    }
1953

    
1954
///////////////////////////////////////////////////////////////////////////////////////////////////
1955

    
1956
  public TouchControl getTouchControl()
1957
    {
1958
    if( mTouchControl==null )
1959
      {
1960
      switch(getTouchControlType())
1961
        {
1962
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
1963
                                   break;
1964
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
1965
                                   break;
1966
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
1967
                                   break;
1968
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
1969
                                   break;
1970
        case TC_ICOSAHEDRON      : mTouchControl = new TouchControlIcosahedron(this);
1971
                                   break;
1972
        case TC_CUBOID           : int[] numLayers = getNumLayers();
1973
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1974
                                   break;
1975
        case TC_BALL             : mTouchControl = new TouchControlBall(this);
1976
                                   break;
1977
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
1978
                                   break;
1979
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
1980
                                   break;
1981
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
1982
                                   break;
1983
        }
1984
      }
1985
    return mTouchControl;
1986
    }
1987

    
1988
///////////////////////////////////////////////////////////////////////////////////////////////////
1989

    
1990
  public float[][] returnRotationFactor()
1991
    {
1992
    float[][] factor = new float[mNumAxis][];
1993

    
1994
    for(int ax=0; ax<mNumAxis; ax++)
1995
      {
1996
      int numL = mNumLayers[ax];
1997
      factor[ax] = new float[numL];
1998
      for(int la=0; la<numL; la++) factor[ax][la] = 1.0f;
1999
      }
2000

    
2001
    return factor;
2002
    }
2003

    
2004
///////////////////////////////////////////////////////////////////////////////////////////////////
2005

    
2006
  protected void setReader(JsonReader reader)
2007
    {
2008
    // empty
2009
    }
2010

    
2011
///////////////////////////////////////////////////////////////////////////////////////////////////
2012

    
2013
  public String getInventor()
2014
    {
2015
    return mMetadata.getAuthor();
2016
    }
2017

    
2018
///////////////////////////////////////////////////////////////////////////////////////////////////
2019

    
2020
  public int getYearOfInvention()
2021
    {
2022
    return mMetadata.getYearOfInvention();
2023
    }
2024

    
2025
///////////////////////////////////////////////////////////////////////////////////////////////////
2026

    
2027
  public float getComplexity()
2028
    {
2029
    return mMetadata.getDifficulty();
2030
    }
2031

    
2032
///////////////////////////////////////////////////////////////////////////////////////////////////
2033

    
2034
  public String getObjectName()
2035
    {
2036
    return mMetadata.getObjectName();
2037
    }
2038

    
2039
///////////////////////////////////////////////////////////////////////////////////////////////////
2040

    
2041
  public ObjectSignature getSignature()
2042
    {
2043
    return mMetadata.getSignature();
2044
    }
2045

    
2046
///////////////////////////////////////////////////////////////////////////////////////////////////
2047
  // for JSON only
2048
  public abstract int getTouchControlType();
2049
  public abstract int getTouchControlSplit();
2050
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
2051
  public abstract int[][][] getEnabled();
2052
  public abstract float[] getDist3D(int[] numLayers);
2053
  public abstract Static3D[] getFaceAxis();
2054
  public abstract int[][] getScrambleEdges();
2055
  public abstract float[][] getCuts(int[] numLayers);
2056
  public abstract float getStickerRadius();
2057
  public abstract float getStickerStroke();
2058
  public abstract float[][][] getStickerAngles();
2059
  public abstract int getCubitVariant(int cubit, int[] numLayers);
2060
  public abstract ObjectShape getObjectShape(int variant);
2061
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
2062
  public abstract ObjectVertexEffects getVertexEffects(int variant);
2063
  public abstract int getNumCubitVariants(int[] numLayers);
2064
  public abstract float[][] getCubitPositions(int[] numLayers);
2065
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
2066
  public abstract int getNumFaceColors();
2067
  public abstract float getScreenRatio();
2068
  public abstract int getColor(int face);
2069
  public abstract String getShortName();
2070

    
2071
  // not only for JSON
2072
  public abstract Static3D[] getRotationAxis();
2073
  public abstract int[][] getBasicAngles();
2074
  public abstract int getNumFaces();
2075
  public abstract int getFOV();
2076
  public abstract String[][] getTutorials();
2077
  }
(6-6/9)