Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ aacf5e27

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.VertexEffectRotate;
27
import org.distorted.library.effect.VertexEffectSink;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedLibrary;
30
import org.distorted.library.main.DistortedNode;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.helpers.QuatHelper;
33
import org.distorted.library.mesh.MeshBase;
34
import org.distorted.library.mesh.MeshFile;
35
import org.distorted.library.mesh.MeshJoined;
36
import org.distorted.library.message.EffectListener;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

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

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
public abstract class TwistyObject
65
  {
66
  public static final int MESH_NICE = 0;
67
  public static final int MESH_FAST = 1;
68

    
69
  public static final int MODE_ICON = 0;
70
  public static final int MODE_NORM = 1;
71

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

    
83
  private static final int STATE_NOTHING = 0;
84
  private static final int STATE_ROTATE  = 1;
85
  private static final int STATE_FINISH  = 2;
86

    
87
  public static final int TEXTURE_HEIGHT = 256;
88
  static final int NUM_STICKERS_IN_ROW = 4;
89

    
90
  public static final float SQ2 = (float)Math.sqrt(2);
91
  public static final float SQ3 = (float)Math.sqrt(3);
92
  public static final float SQ5 = (float)Math.sqrt(5);
93
  public static final float SQ6 = (float)Math.sqrt(6);
94

    
95
  private static final float MAX_SIZE_CHANGE = 1.35f;
96
  private static final float MIN_SIZE_CHANGE = 0.75f;
97

    
98
  private static final Static3D CENTER = new Static3D(0,0,0);
99
  private static final int POST_ROTATION_MILLISEC = 500;
100

    
101
  protected float[][] mStickerCoords;
102
  protected Static4D[] mObjectQuats;
103

    
104
  private int[][] mStickerVariants;
105
  private float[] mStickerScales;
106
  private TwistyObjectCubit[] mCubits;
107
  private TwistyObjectSolved mSolved;
108
  private MeshBase[] mMeshes;
109
  private int mNumCubits, mNumQuats, mNumFaceColors, mNumTextures, mNumOverrides;
110
  private int mNumCubitFaces, mNumStickerTypes;
111
  private Static3D[] mAxis;
112
  private float[][] mCuts;
113
  private int[] mNumCuts;
114
  private float[][] mOrigPos;
115
  private Static4D[] mOrigQuat;
116
  private Static4D[] mMixupModeQuats;
117
  private boolean mIsInMixupMode;
118
  private Static4D mQuat;
119
  private int[] mNumLayers;
120
  private float mSize;
121
  private DistortedEffects mEffects;
122
  private VertexEffectRotate mRotateEffect;
123
  private Dynamic1D mRotationAngle;
124
  private Static3D mRotationAxis;
125
  private Static3D mObjectScale;
126
  private int[] mQuatDebug;
127
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
128
  private DistortedTexture mTexture;
129
  private float mInitScreenRatio;
130
  private boolean mIsBandaged;
131
  private float mObjectScreenRatio;
132
  private int mNumTexRows, mNumTexCols;
133
  private int mRotRowBitmap;
134
  private int mCurrentRotAxis;
135
  private MeshBase mMesh;
136
  private ObjectScrambler mScrambler;
137
  private TouchControl mTouchControl;
138
  private DistortedNode mNode;
139
  private ObjectLibInterface mInterface;
140
  private Bitmap mBitmap;
141
  private ObjectSticker[] mStickers;
142
  private ObjectShape[] mShapes;
143
  private int mNumCubitVariants;
144
  private int[][] mCubitFaceColors;
145
  private int[][] mVariantFaceIsOuter;
146
  private int[][] mBasicAngles;
147
  private int mIconMode;
148
  private InitData mInitData;
149
  private float[][] mRowOffsets;
150
  private boolean[] mBelongs;
151
  private float[] mTmp;
152
  private int mNumPuzzleFaces;
153
  private ObjectStickerOverride[] mStickerOverrides;
154
  private boolean mError;
155
  private String mErrorString;
156
  private int mMaxNumLayers;
157
  private int mNumAxis;
158
  private int mPointNum;
159
  private int mRotationState;
160
  private boolean mThereAreDeciders;
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  TwistyObject(int meshState, int iconMode, Static4D quat, Static3D move, float scale, InitAssets asset)
165
    {
166
    try
167
      {
168
      InputStream jsonStream = asset!=null ? asset.getJsonStream(): null;
169
      JsonReader reader = new JsonReader();
170
      reader.parseJsonFile(jsonStream);
171
      setReader(reader);
172
      mNumLayers = reader.getNumLayers();
173
      mSize      = reader.getSize();
174
      mInitData  = null;
175
      initialize(meshState,iconMode,quat,move,scale,asset,true);
176
      if( asset!=null ) asset.close();
177
      mError = false;
178
      mErrorString=null;
179
      }
180
    catch(Exception ex)
181
      {
182
      mError = true;
183
      mErrorString = ex.getMessage();
184
      }
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public TwistyObject(int meshState, int iconMode, float size, Static4D quat, Static3D move, float scale, InitData data, InitAssets asset)
190
    {
191
    mNumLayers = data.getNumLayers();
192
    mSize      = size;
193
    mInitData  = data;
194
    initialize(meshState,iconMode,quat,move,scale,asset,false);
195
    if( asset!=null ) asset.close();
196
    mError = false;
197
    mErrorString = null;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  private void debugQuat(Static4D quat, int cubitIndex, float axisX, float axisY, float axisZ, float angle, int place)
203
    {
204
    float[] tracking = mCubits[cubitIndex].getTrackingPoint();
205

    
206
    String problem = (getShortName()+" "+cubitIndex+" "+quat.get0()+" "+quat.get1()+" "+quat.get2()+" "+quat.get3());
207
    problem += (" "+angle+" "+place+" "+tracking[0]+" "+tracking[1]+" "+tracking[2]);
208
    problem += (" "+axisX+" "+axisY+" "+axisZ)+" "+mPointNum;
209

    
210
    mInterface.reportProblem(problem,true);
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  private void initialize(int meshState, int iconMode, Static4D quat, Static3D move, float scale, InitAssets asset, boolean fromJSON)
216
    {
217
    mRotationState = STATE_NOTHING;
218
    mIconMode = iconMode;
219
    mQuat = quat;
220
    mAxis = getRotationAxis();
221
    mInitScreenRatio = getScreenRatio();
222
    mBasicAngles = getBasicAngles();
223
    mObjectQuats = getQuats();
224
    mNumQuats = mObjectQuats.length;
225
    mOrigPos = getCubitPositions(mNumLayers);
226
    mNumPuzzleFaces = getNumFaces();
227
    mRowOffsets = new float[mNumPuzzleFaces][3];
228
    mTmp = new float[4];
229

    
230
    mNumAxis = mAxis.length;
231
    mCuts = getCuts(mNumLayers);
232
    mNumCuts = new int[mNumAxis];
233
    mMaxNumLayers = -1;
234
    for(int i=0; i<mNumAxis; i++)
235
      {
236
      if( mMaxNumLayers<mNumLayers[i] ) mMaxNumLayers = mNumLayers[i];
237
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
238
      }
239

    
240
    mNumCubits = mOrigPos.length;
241
    mNumFaceColors = getNumFaceColors();
242
    mBelongs = new boolean[mNumCubits];
243

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

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

    
252
    boolean bandaged=false;
253

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

    
265
    mRotationAngle= new Dynamic1D();
266
    mRotationAxis = new Static3D(1,0,0);
267
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
268

    
269
    mRotationAngleStatic = new Static1D(0);
270
    mRotationAngleMiddle = new Static1D(0);
271
    mRotationAngleFinal  = new Static1D(0);
272

    
273
    mObjectScale = new Static3D(scale,scale,scale);
274
    setObjectRatioNow(scale,720);
275

    
276
    mEffects = new DistortedEffects();
277
    createQuaternionEffects();
278

    
279
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
280
    MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
281
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
282

    
283
    InputStream meshStream = asset!=null ? asset.getMeshStream(): null;
284
    boolean fromDMESH = (meshStream!=null && meshState==MESH_NICE);
285
    getQuatsAndShapes(fromDMESH,fromJSON);
286
    createMeshAndCubits(meshStream,meshState,fromDMESH);
287
    setUpTextures(fromDMESH,fromJSON);
288

    
289
    int index = getSolvedFunctionIndex();
290
    mSolved = new TwistyObjectSolved(this,mOrigPos,index);
291
    mSolved.setupSolvedQuats(getSolvedQuats());
292

    
293
    mEffects.apply(mRotateEffect);
294
    mEffects.apply(quatEffect);
295
    mEffects.apply(scaleEffect);
296
    mEffects.apply(moveEffect);
297

    
298
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302

    
303
  private TablebasesAbstract getTablebase(OperatingSystemInterface os)
304
    {
305
    ObjectSignature signature = getSignature();
306
    long[] array = signature.getArray();
307
    int len = array.length;
308
    int sig = (int)array[len-1];
309

    
310
    return ImplementedTablebasesList.createPacked(os,sig);
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

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

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

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

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

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

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

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

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
  private void createOuterFaces()
373
    {
374
    for(int v=0; v<mNumCubitVariants; v++)
375
      {
376
      int[][] indices = mShapes[v].getVertIndices();
377
      int faces = indices.length;
378
      mVariantFaceIsOuter[v] = new int[faces];
379
      }
380

    
381
    for( int cubit=0; cubit<mNumCubits; cubit++)
382
      {
383
      int variant = getCubitVariant(cubit,mNumLayers);
384
      int[][] indices = mShapes[variant].getVertIndices();
385
      int numFaces = indices.length;
386

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

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

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

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

    
406
      mShapes = new ObjectShape[mNumCubitVariants];
407
      for(int i=0; i<mNumCubitVariants; i++) mShapes[i] = getObjectShape(i);
408
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
409
      mVariantFaceIsOuter = new int[mNumCubitVariants][];
410

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

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

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

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  private void createMeshAndCubits(InputStream stream, int meshState, boolean fromDMESH)
430
    {
431
    mCubits = new TwistyObjectCubit[mNumCubits];
432

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

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

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

    
458
      mMesh = new MeshJoined(cubitMesh);
459

    
460
      float pillowCoeff = getPillowCoeff();
461

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

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

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

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int meshState, int numComponents)
487
    {
488
    int variant = getCubitVariant(cubit,numLayers);
489

    
490
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
491

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

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

    
505
    return mesh;
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

    
510
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
511
    {
512
    mTexture = new DistortedTexture();
513

    
514
    if( fromJSON )
515
      {
516
      mNumStickerTypes = getNumStickerTypes();
517
      mNumCubitFaces = getNumCubitFaces();
518
      }
519
    else
520
      {
521
      FactoryCubit factory = FactoryCubit.getInstance();
522
      mStickerCoords   = factory.getStickerCoords();
523
      mStickerVariants = factory.getStickerVariants();
524
      mStickerScales   = factory.getStickerScales();
525
      adjustStickerCoords();
526
      mNumStickerTypes = (mStickerCoords==null ? 0 : mStickerCoords.length);
527
      }
528

    
529
    mStickerOverrides = getStickerOverrides();
530
    mNumOverrides = mStickerOverrides==null ? 0 : mStickerOverrides.length;
531

    
532
    mNumTextures= mNumFaceColors*mNumStickerTypes + mNumOverrides;
533
    mNumTexCols = NUM_STICKERS_IN_ROW;
534
    mNumTexRows = (mNumTextures+1)/NUM_STICKERS_IN_ROW;
535
    if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
536

    
537
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
538
    else overrideCubitFaceColor();
539

    
540
    setTexture();
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544

    
545
  public InitData getInitData()
546
    {
547
    return mInitData;
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
  public boolean isInIconMode()
553
    {
554
    return mIconMode==MODE_ICON;
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

    
559
  public int getVariantStickerShape(int variant, int face)
560
    {
561
    return face>=mStickerVariants[variant].length ? -1 : mStickerVariants[variant][face];
562
    }
563

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

    
566
  public boolean shouldResetTextureMaps()
567
    {
568
    return false;
569
    }
570

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

    
573
  public int[][] getScrambleAlgorithms()
574
    {
575
    return ScrambleEdgeGenerator.getScramblingAlgorithms(mBasicAngles);
576
    }
577

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

    
580
  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
581
    {
582
    final float MAXERR = 0.05f;
583
    int numAxis = dist.length;
584

    
585
    for(int i=0; i<numAxis; i++)
586
      {
587
      Static3D ax = faceAxis[i];
588
      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
589
      if( len>mSize*dist[i]+MAXERR ) return true;
590
      }
591

    
592
    return false;
593
    }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

    
597
  private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
598
    {
599
    float[][] vertices = mShapes[variant].getVertices();
600
    Static3D[] axis = getFaceAxis();
601
    float[] dist3D = getDist3D(mNumLayers);
602

    
603
    for( float[] vertex : vertices)
604
      {
605
      float x = vertex[0];
606
      float y = vertex[1];
607
      float z = vertex[2];
608

    
609
      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
610

    
611
      float mx = tmp[0] + px;
612
      float my = tmp[1] + py;
613
      float mz = tmp[2] + pz;
614

    
615
      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
616
      }
617

    
618
    return true;
619
    }
620

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

    
623
  private float computeAvg(float[] pos, int offset)
624
    {
625
    int len = pos.length/3;
626
    float ret=0.0f;
627
    for(int i=0; i<len; i++) ret += pos[3*i+offset];
628
    ret /= len;
629

    
630
    return ret;
631
    }
632

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

    
635
  protected void displayCubitQuats()
636
    {
637
    StringBuilder builder = new StringBuilder();
638
    float[] tmp = new float[4];
639
    float ERR = 0.01f;
640

    
641
    for(int cubit=0; cubit<mNumCubits; cubit++)
642
      {
643
      builder.append(cubit);
644
      builder.append(" : ");
645

    
646
      int refCubit,variant = getCubitVariant(cubit,mNumLayers);
647

    
648
      for(refCubit=0; refCubit<mNumCubits; refCubit++)
649
        {
650
        if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
651
        }
652

    
653
      float[] curpos = mOrigPos[cubit];
654
      float[] refpos = mOrigPos[refCubit];
655
      float refX = computeAvg(refpos,0);
656
      float refY = computeAvg(refpos,1);
657
      float refZ = computeAvg(refpos,2);
658
      float curX = computeAvg(curpos,0);
659
      float curY = computeAvg(curpos,1);
660
      float curZ = computeAvg(curpos,2);
661

    
662
      for(int quat=0; quat<mNumQuats; quat++)
663
        {
664
        QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
665

    
666
        float dx = tmp[0]-curX;
667
        float dy = tmp[1]-curY;
668
        float dz = tmp[2]-curZ;
669

    
670
        if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
671
          {
672
          if( doesNotStickOut(variant,curX,curY,curZ,tmp,mObjectQuats[quat]) )
673
            {
674
            builder.append(quat);
675
            builder.append(',');
676
            }
677
          else
678
            {
679
            android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
680
            }
681
          }
682
        }
683

    
684
      builder.append('\n');
685
      }
686

    
687
    android.util.Log.e("D", "cubitQuats: \n"+builder );
688
    }
689

    
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691

    
692
  public int getCubitRotationType(int cubit)
693
    {
694
    return TwistyObjectCubit.TYPE_NORMAL;
695
    }
696

    
697
///////////////////////////////////////////////////////////////////////////////////////////////////
698

    
699
  float[] getTrackingPoint(int cubitIndex, int cubitType)
700
    {
701
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
702
      {
703
      int variant = getCubitVariant(cubitIndex,mNumLayers);
704

    
705
      // object must have been created from JSON
706
      if( mVariantFaceIsOuter==null || mVariantFaceIsOuter[variant]==null )
707
        {
708
        mVariantFaceIsOuter = getVariantFaceIsOuter();
709
        }
710
      if( mShapes==null )
711
        {
712
        mShapes = new ObjectShape[mNumCubitVariants];
713
        }
714
      if( mShapes[variant]==null )
715
        {
716
        mShapes[variant] = getObjectShape(variant);
717
        }
718
      if( mOrigQuat==null )
719
        {
720
        mOrigQuat = new Static4D[mNumCubits];
721
        }
722
      if( mOrigQuat[cubitIndex]==null )
723
        {
724
        mOrigQuat[cubitIndex] = getCubitQuats(cubitIndex,mNumLayers);
725
        }
726

    
727
      int[][] indices = mShapes[variant].getVertIndices();
728
      int outer=-1, faces = indices.length;
729

    
730
      for(int i=0; i<faces; i++)
731
        {
732
        if( mVariantFaceIsOuter[variant][i]==1 )
733
          {
734
          outer=i;
735
          break;
736
          }
737
        }
738

    
739
      if( outer>=0 )
740
        {
741
        int vertIndex = indices[outer][0];
742
        float[] vertices = mShapes[variant].getVertices()[vertIndex];
743
        float[] ret = new float[3];
744
        float[] curpos = mOrigPos[cubitIndex];
745
        Static4D quat = mOrigQuat[cubitIndex];
746
        QuatHelper.rotateVectorByQuat(mTmp, vertices[0], vertices[1], vertices[2], 1, quat);
747

    
748
        ret[0] = mTmp[0]+computeAvg(curpos,0);
749
        ret[1] = mTmp[1]+computeAvg(curpos,1);
750
        ret[2] = mTmp[2]+computeAvg(curpos,2);
751

    
752
        return ret;
753
        }
754
      else
755
        {
756
        android.util.Log.e("D", "Error in getTrackingPoint: no outer face??");
757
        }
758
      }
759

    
760
    return null;
761
    }
762

    
763
///////////////////////////////////////////////////////////////////////////////////////////////////
764

    
765
  public int computeCurrentPuzzleFace(int type, float[] vertex)
766
    {
767
    if( type!=TwistyObjectCubit.TYPE_NORMAL )
768
      {
769
      Static3D[] axis = getFaceAxis();
770
      float[] dist3D = getDist3D(mNumLayers);
771
      final float MAXERR = 0.98f;
772
      int numAxis = axis.length;
773
      float x = vertex[0];
774
      float y = vertex[1];
775
      float z = vertex[2];
776

    
777
      for(int i=0; i<numAxis; i++)
778
        {
779
        Static3D ax = axis[i];
780
        float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
781
        if( len>mSize*dist3D[i]*MAXERR ) return i;
782
        }
783

    
784
      return -2;
785
      }
786

    
787
    return -1;
788
    }
789

    
790
///////////////////////////////////////////////////////////////////////////////////////////////////
791

    
792
  public float[] getCubitRowOffset(int cubitIndex)
793
    {
794
    return null;
795
    }
796

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

    
799
  void setRotationRowOffset(int puzzleFace, float[] offset)
800
    {
801
    mRowOffsets[puzzleFace][0] = offset[0];
802
    mRowOffsets[puzzleFace][1] = offset[1];
803
    mRowOffsets[puzzleFace][2] = offset[2];
804
    }
805

    
806
///////////////////////////////////////////////////////////////////////////////////////////////////
807

    
808
  int getNumAxis()
809
    {
810
    return mNumAxis;
811
    }
812

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

    
815
  public int[][] getSolvedQuats()
816
    {
817
    return mSolved.getSolvedQuats(mNumLayers,mNumCubitFaces,mCubitFaceColors);
818
    }
819

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

    
822
  public int getSolvedFunctionIndex()
823
    {
824
    return 0;
825
    }
826

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

    
829
  int computeRow(float[] pos, int axisIndex, int cubitType, int puzzleFace)
830
    {
831
    int ret=0;
832
    int len = pos.length / 3;
833
    Static3D axis = mAxis[axisIndex];
834
    float axisX = axis.get0();
835
    float axisY = axis.get1();
836
    float axisZ = axis.get2();
837
    float casted, xoff=0, yoff=0, zoff=0;
838

    
839
    if( cubitType!=TwistyObjectCubit.TYPE_NORMAL )
840
      {
841
      xoff = mRowOffsets[puzzleFace][0];
842
      yoff = mRowOffsets[puzzleFace][1];
843
      zoff = mRowOffsets[puzzleFace][2];
844
      }
845

    
846
    for(int i=0; i<len; i++)
847
      {
848
      casted = (pos[3*i]+xoff)*axisX + (pos[3*i+1]+yoff)*axisY + (pos[3*i+2]+zoff)*axisZ;
849
      ret |= computeSingleRow(axisIndex,casted);
850
      }
851

    
852
    return ret;
853
    }
854

    
855
///////////////////////////////////////////////////////////////////////////////////////////////////
856

    
857
  private int computeSingleRow(int axisIndex,float casted)
858
    {
859
    int num = mNumCuts[axisIndex];
860

    
861
    for(int i=0; i<num; i++)
862
      {
863
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
864
      }
865

    
866
    return (1<<num);
867
    }
868

    
869
///////////////////////////////////////////////////////////////////////////////////////////////////
870

    
871
  private boolean wasRotateApplied()
872
    {
873
    return mEffects.exists(mRotateEffect.getID());
874
    }
875

    
876
///////////////////////////////////////////////////////////////////////////////////////////////////
877

    
878
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
879
    {
880
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
881
    }
882

    
883
///////////////////////////////////////////////////////////////////////////////////////////////////
884
// note the minus in front of the sin() - we rotate counterclockwise
885
// when looking towards the direction where the axis increases in values.
886

    
887
  private Static4D makeQuaternion(float axisX, float axisY, float axisZ, int angleInDegrees)
888
    {
889
    while( angleInDegrees<0 ) angleInDegrees += 360;
890
    angleInDegrees %= 360;
891
    
892
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
893
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
894

    
895
    return new Static4D(axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
896
    }
897

    
898
///////////////////////////////////////////////////////////////////////////////////////////////////
899

    
900
  private synchronized void setupPosition(int[][] moves)
901
    {
902
    if( moves!=null )
903
      {
904
      Static4D quat;
905
      int index, axisIndex, row, banBitmap, basic, angle;
906

    
907
      for(int[] move: moves)
908
        {
909
        axisIndex= move[0];
910
        banBitmap= computeBandagedBitmap( move[1],axisIndex);
911
        row      = computeRowFromBitmap( move[1] );
912

    
913
        // Firebase says that it crashes here in case of the Pyraminx Diamond. No idea why.
914
        if( axisIndex >= mBasicAngles.length || row >= mBasicAngles[axisIndex].length )
915
          {
916
          String problem = getShortName()+" "+axisIndex+" "+mBasicAngles.length+" "+row+" "+mBasicAngles[0].length;
917
          mInterface.reportProblem(problem,true);
918
          axisIndex=0;
919
          row = 0;
920
          move[2] = 0;
921
          }
922

    
923
        basic    = mBasicAngles[axisIndex][row];
924
        angle    = move[2]*(360/basic);   // this assumes that all layers from
925
                                          // the bitmap have the same BasicAngle.
926
                                          // at the moment this is always true as
927
                                          // there are no bandaged objects with
928
                                          // different per-layer BasicAngles.
929
        Static3D axis = mAxis[axisIndex];
930
        float axisX = axis.get0();
931
        float axisY = axis.get1();
932
        float axisZ = axis.get2();
933
        quat = makeQuaternion(axisX,axisY,axisZ,angle);
934

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

    
945
        recomputeFaceOffsets();
946

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

    
962
      for(int i=0; i<mNumCubits; i++)
963
        {
964
        float[] pos = mCubits[i].getCurrentPos();
965
        int len = pos.length/3;
966
        for(int j=0; j<len; j++) clampPos(pos,3*j);
967
        }
968
      }
969
    }
970

    
971
///////////////////////////////////////////////////////////////////////////////////////////////////
972

    
973
  public int getScrambleType()
974
    {
975
    return ObjectScrambler.SCRAMBLING_ALGORITHMS;
976
    }
977

    
978
///////////////////////////////////////////////////////////////////////////////////////////////////
979

    
980
  int computeBandagedBitmap(int rowBitmap, int axis)
981
    {
982
    if( mIsBandaged )
983
      {
984
      int bitmap, initBitmap=0;
985

    
986
      while( initBitmap!=rowBitmap )
987
        {
988
        initBitmap = rowBitmap;
989

    
990
        for(int cubit=0; cubit<mNumCubits; cubit++)
991
          {
992
          bitmap = mCubits[cubit].getRotRow(axis);
993
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
994
          }
995
        }
996
      }
997

    
998
    return rowBitmap;
999
    }
1000

    
1001
///////////////////////////////////////////////////////////////////////////////////////////////////
1002

    
1003
  private int computeRowFromBitmap(int rowBitmap)
1004
    {
1005
    int index = 0;
1006

    
1007
    while(index<32)
1008
      {
1009
      if( (rowBitmap&0x1) != 0 ) return index;
1010
      rowBitmap>>=1;
1011
      index++;
1012
      }
1013
    return 0;
1014
    }
1015

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

    
1020
  void clampPos(float[] pos, int offset)
1021
    {
1022
    float currError, minError = Float.MAX_VALUE;
1023
    int minErrorIndex1 = -1;
1024
    int minErrorIndex2 = -1;
1025

    
1026
    float x = pos[offset  ];
1027
    float y = pos[offset+1];
1028
    float z = pos[offset+2];
1029

    
1030
    float xo,yo,zo;
1031

    
1032
    for(int i=0; i<mNumCubits; i++)
1033
      {
1034
      int len = mOrigPos[i].length / 3;
1035

    
1036
      for(int j=0; j<len; j++)
1037
        {
1038
        xo = mOrigPos[i][3*j  ];
1039
        yo = mOrigPos[i][3*j+1];
1040
        zo = mOrigPos[i][3*j+2];
1041

    
1042
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1043

    
1044
        if( currError<minError )
1045
          {
1046
          minError = currError;
1047
          minErrorIndex1 = i;
1048
          minErrorIndex2 = j;
1049
          }
1050
        }
1051
      }
1052

    
1053
    if( minError< 0.05f ) // TODO: 0.05 ?
1054
      {
1055
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1056
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1057
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1058
      }
1059
    }
1060

    
1061
///////////////////////////////////////////////////////////////////////////////////////////////////
1062

    
1063
  private float getAngle()
1064
    {
1065
    mPointNum = mRotationAngle.getNumPoints();
1066
    return mPointNum>=1 ? mRotationAngle.getPoint(mPointNum-1).get0() : 0;
1067
    }
1068

    
1069
///////////////////////////////////////////////////////////////////////////////////////////////////
1070

    
1071
  void setLibInterface(ObjectLibInterface inter)
1072
    {
1073
    mInterface = inter;
1074
    }
1075

    
1076
///////////////////////////////////////////////////////////////////////////////////////////////////
1077

    
1078
  void applyScrambles(int[][] moves)
1079
    {
1080
    setupPosition(moves);
1081
    }
1082

    
1083
///////////////////////////////////////////////////////////////////////////////////////////////////
1084

    
1085
  void initializeObject(int[][] moves)
1086
    {
1087
    solve();
1088
    setupPosition(moves);
1089
    }
1090

    
1091
///////////////////////////////////////////////////////////////////////////////////////////////////
1092

    
1093
  synchronized void removeRotationNow()
1094
    {
1095
    float angle = getAngle();
1096
    double nearestAngleInRadians = angle*Math.PI/180;
1097
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1098
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1099
    float axisX = mAxis[mCurrentRotAxis].get0();
1100
    float axisY = mAxis[mCurrentRotAxis].get1();
1101
    float axisZ = mAxis[mCurrentRotAxis].get2();
1102
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1103

    
1104
    mRotationAngleStatic.set0(0);
1105
    mRotationAngle.removeAll();
1106

    
1107
    for(int i=0; i<mNumCubits; i++)
1108
      {
1109
      mBelongs[i] = belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap);
1110
      if( mBelongs[i] )
1111
        {
1112
        boolean result = mCubits[i].rotateCubit(quat,true);
1113
        if( !result ) debugQuat(quat,i,axisX,axisY,axisZ,angle,2);
1114
        }
1115
      }
1116

    
1117
    recomputeFaceOffsets();
1118

    
1119
    for(int i=0; i<mNumCubits; i++)
1120
      {
1121
      if( mBelongs[i] )
1122
        {
1123
        int index = mCubits[i].postRotateCubit(quat);
1124
        setCubitQuat(i,mCubits[i].computeAssociation(),index);
1125
        }
1126
      else if( mCubits[i].getType()==TwistyObjectCubit.TYPE_FOLLOWER )
1127
        {
1128
        mCubits[i].computeRotationRow();
1129
        setCubitQuat(i,mCubits[i].computeAssociation(),mCubits[i].mQuatIndex);
1130
        }
1131
      }
1132

    
1133
    mRotationState = STATE_NOTHING;
1134
    }
1135

    
1136
///////////////////////////////////////////////////////////////////////////////////////////////////
1137

    
1138
  private void recomputeFaceOffsets()
1139
    {
1140
    if( mThereAreDeciders )
1141
      {
1142
      for(int i=0; i<mNumPuzzleFaces; i++)
1143
        {
1144
        mRowOffsets[i][0] =0;
1145
        mRowOffsets[i][1] =0;
1146
        mRowOffsets[i][2] =0;
1147
        }
1148

    
1149
      for(int i=0; i<mNumCubits; i++)
1150
        if( mCubits[i].getType()==TwistyObjectCubit.TYPE_DECIDER )
1151
          {
1152
          float[] offset = mCubits[i].getOffset();
1153
          int face = mCubits[i].getPuzzleFace();
1154
          mRowOffsets[face][0] = offset[0];
1155
          mRowOffsets[face][1] = offset[1];
1156
          mRowOffsets[face][2] = offset[2];
1157
          }
1158
      }
1159
    }
1160

    
1161
///////////////////////////////////////////////////////////////////////////////////////////////////
1162

    
1163
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1164
    {
1165
    if( wasRotateApplied() )
1166
      {
1167
      mRotationState = STATE_FINISH;
1168
      float angle = getAngle();
1169
      mRotationAngleStatic.set0(angle);
1170
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1171
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1172

    
1173
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1174
      mRotationAngle.resetToBeginning();
1175
      mRotationAngle.removeAll();
1176
      mRotationAngle.add(mRotationAngleStatic);
1177
      mRotationAngle.add(mRotationAngleMiddle);
1178
      mRotationAngle.add(mRotationAngleFinal);
1179
      mRotateEffect.notifyWhenFinished(listener);
1180

    
1181
      return mRotateEffect.getID();
1182
      }
1183

    
1184
    return 0;
1185
    }
1186

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

    
1189
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1190
    {
1191
    int mult = 1;
1192

    
1193
    if( wasRotateApplied() )
1194
      {
1195
      if( mRotationState==STATE_ROTATE )
1196
        {
1197
        return 0;
1198
        }
1199
      if( mRotationState==STATE_FINISH )
1200
        {
1201
        removeRotationNow();
1202
        mult = -1;
1203
        }
1204

    
1205
      mRotationState = STATE_ROTATE;
1206
      mCurrentRotAxis = axis;
1207
      mRotRowBitmap= computeBandagedBitmap( rowBitmap,axis );
1208
      mRotationAngleStatic.set0(0.0f);
1209
      mRotationAxis.set( mAxis[axis] );
1210
      mRotationAngle.setDuration(durationMillis);
1211
      mRotationAngle.resetToBeginning();
1212
      mRotationAngle.add(new Static1D(0));
1213
      mRotationAngle.add(new Static1D(angle));
1214
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1215
      mRotateEffect.notifyWhenFinished(listener);
1216
      return mult*mRotateEffect.getID();
1217
      }
1218

    
1219
    return 0;
1220
    }
1221

    
1222
///////////////////////////////////////////////////////////////////////////////////////////////////
1223

    
1224
  void continueRotation(float angleInDegrees)
1225
    {
1226
    mRotationAngleStatic.set0(angleInDegrees);
1227
    }
1228

    
1229
///////////////////////////////////////////////////////////////////////////////////////////////////
1230

    
1231
  synchronized boolean beginNewRotation(int axis, int row )
1232
    {
1233
    if( mRotationState==STATE_ROTATE )
1234
      {
1235
      return false;
1236
      }
1237
    if( mRotationState==STATE_FINISH )
1238
      {
1239
      removeRotationNow();
1240
      }
1241

    
1242
    if( axis<0 || axis>=mNumAxis )
1243
      {
1244
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1245
      return false;
1246
      }
1247
    if( row<0 || row>=mNumLayers[axis] )
1248
      {
1249
      android.util.Log.e("object", "invalid rotation row: "+row);
1250
      return false;
1251
      }
1252

    
1253
    mRotationState = STATE_ROTATE;
1254
    mCurrentRotAxis = axis;
1255
    mRotRowBitmap= computeBandagedBitmap( (1<<row),axis );
1256
    mRotationAngleStatic.set0(0.0f);
1257
    mRotationAxis.set( mAxis[axis] );
1258
    mRotationAngle.add(mRotationAngleStatic);
1259
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1260

    
1261
    return true;
1262
    }
1263

    
1264
///////////////////////////////////////////////////////////////////////////////////////////////////
1265

    
1266
  void setTextureMap(int cubit, int face, int color)
1267
    {
1268
    int variant  = getCubitVariant(cubit,mNumLayers);
1269
    int shape    = getVariantStickerShape(variant,face);
1270
    int texIndex = color<0 || shape<0 ? mNumTextures-mNumOverrides : shape*mNumFaceColors + color;
1271
    int row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1272
    int col      = texIndex%mNumTexCols;
1273

    
1274
    final float ratioW = 1.0f/mNumTexCols;
1275
    final float ratioH = 1.0f/mNumTexRows;
1276
    final Static4D[] maps = new Static4D[1];
1277
    maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1278
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1279
    }
1280

    
1281
///////////////////////////////////////////////////////////////////////////////////////////////////
1282

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

    
1290
///////////////////////////////////////////////////////////////////////////////////////////////////
1291

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

    
1299
///////////////////////////////////////////////////////////////////////////////////////////////////
1300

    
1301
  void resetAllTextureMaps()
1302
    {
1303
    final float ratioW = 1.0f/mNumTexCols;
1304
    final float ratioH = 1.0f/mNumTexRows;
1305
    int cubColor, stiShape, texIndex, variant, row, col;
1306

    
1307
    for(int cubit=0; cubit<mNumCubits; cubit++)
1308
      {
1309
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1310
      variant = getCubitVariant(cubit,mNumLayers);
1311

    
1312
      for(int face=0; face<mNumCubitFaces; face++)
1313
        {
1314
        cubColor = getCubitFaceColor(cubit,face);
1315
        stiShape = getVariantStickerShape(variant,face);
1316
        texIndex = cubColor<0 || stiShape<0 ? mNumTextures-mNumOverrides : stiShape*mNumFaceColors + cubColor;
1317
        row      = (mNumTexRows-1) - texIndex/mNumTexCols;
1318
        col      = texIndex%mNumTexCols;
1319

    
1320
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1321
        }
1322

    
1323
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1324
      }
1325

    
1326
    overrideCubitFaceColor();
1327
    }
1328

    
1329
///////////////////////////////////////////////////////////////////////////////////////////////////
1330

    
1331
  private void overrideCubitFaceColor()
1332
    {
1333
    final float ratioW = 1.0f/mNumTexCols;
1334
    final float ratioH = 1.0f/mNumTexRows;
1335

    
1336
    for(int i=0; i<mNumOverrides; i++)
1337
      {
1338
      int[] cubitFaces = mStickerOverrides[i].getCubitFaces();
1339
      int length = cubitFaces.length/2;
1340

    
1341
      for(int j=0; j<length; j++)
1342
        {
1343
        final Static4D[] maps = new Static4D[1];
1344
        int color = mNumTextures-mNumOverrides+1+i;
1345
        int row   = (mNumTexRows-1) - color/mNumTexCols;
1346
        int col   = color%mNumTexCols;
1347
        int cubit = cubitFaces[2*j];
1348
        int face  = cubitFaces[2*j+1];
1349
        maps[0] = new Static4D(col*ratioW, row*ratioH, ratioW, ratioH);
1350
        mMesh.setTextureMap(maps,mNumCubitFaces*cubit+face);
1351
        }
1352
      }
1353
    }
1354

    
1355
///////////////////////////////////////////////////////////////////////////////////////////////////
1356

    
1357
  void releaseResources()
1358
    {
1359
    mTexture.markForDeletion();
1360
    mMesh.markForDeletion();
1361
    mEffects.markForDeletion();
1362

    
1363
    for(int j=0; j<mNumCubits; j++)
1364
      {
1365
      mCubits[j].releaseResources();
1366
      }
1367
    }
1368

    
1369
///////////////////////////////////////////////////////////////////////////////////////////////////
1370

    
1371
  private void setCubitQuat(int cubit, int andAssociation, int equAssociation)
1372
    {
1373
    if( !mIsInMixupMode )
1374
      {
1375
      mMesh.setEffectAssociation(cubit,andAssociation,equAssociation);
1376
      }
1377
    else
1378
      {
1379
      mMesh.setEffectAssociation(cubit,andAssociation,cubit);
1380
      Static4D tmp = mObjectQuats[equAssociation];
1381
      mMixupModeQuats[cubit].set(tmp);
1382
      }
1383
    }
1384

    
1385
///////////////////////////////////////////////////////////////////////////////////////////////////
1386

    
1387
  synchronized void restorePreferences(OperatingSystemInterface os)
1388
    {
1389
    boolean error = false;
1390
    String key = getShortName();
1391

    
1392
    for(int i=0; i<mNumCubits; i++)
1393
      {
1394
      mQuatDebug[i] = mCubits[i].restorePreferences(key,os);
1395

    
1396
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1397
        {
1398
        boolean result = mCubits[i].rotateCubit(mObjectQuats[mQuatDebug[i]],true);
1399
        if( !result ) debugQuat(mObjectQuats[mQuatDebug[i]],i,0,0,0,mQuatDebug[i],3);
1400
        }
1401
      else { error = true; break; }
1402
      }
1403

    
1404
    if( !error )
1405
      {
1406
      recomputeFaceOffsets();
1407

    
1408
      for(int i=0; i<mNumCubits; i++)
1409
        {
1410
        if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1411
          {
1412
          mCubits[i].computeRotationRow();
1413
          setCubitQuat(i,mCubits[i].computeAssociation(),mQuatDebug[i]);
1414
          }
1415
        else { error = true; break; }
1416
        }
1417
      }
1418

    
1419
    if( error )
1420
      {
1421
      for(int i=0; i<mNumCubits; i++)
1422
        {
1423
        mCubits[i].solve();
1424
        setCubitQuat(i,mCubits[i].computeAssociation(),0);
1425
        }
1426
      }
1427
    }
1428

    
1429
///////////////////////////////////////////////////////////////////////////////////////////////////
1430

    
1431
  void savePreferences(OperatingSystemInterface os)
1432
    {
1433
    String key = getShortName();
1434
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,os);
1435
    }
1436

    
1437
///////////////////////////////////////////////////////////////////////////////////////////////////
1438

    
1439
  public void removePreferences(OperatingSystemInterface os)
1440
    {
1441
    String key = getShortName();
1442
    for(int i=0; i<mNumCubits; i++) mCubits[i].removePreferences(key,os);
1443
    }
1444

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

    
1447
  private float computeRadiusCorrection(float[] sticker, int curr, int len)
1448
    {
1449
    final float A = 0.8f;  // 0<A<1
1450

    
1451
    int prev = curr>0 ? curr-1 : len-1;
1452
    int next = curr<len-1 ? curr+1 : 0;
1453

    
1454
    float v1x = sticker[2*prev  ]-sticker[2*curr  ];
1455
    float v1y = sticker[2*prev+1]-sticker[2*curr+1];
1456
    float v2x = sticker[2*next  ]-sticker[2*curr  ];
1457
    float v2y = sticker[2*next+1]-sticker[2*curr+1];
1458

    
1459
    float len1= v1x*v1x+v1y*v1y;
1460
    float len2= v2x*v2x+v2y*v2y;
1461

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

    
1464
    return 1-A*cos;
1465
    }
1466

    
1467
///////////////////////////////////////////////////////////////////////////////////////////////////
1468
// Radius of the sphere circumscribed on the puzzle. Needed for pillowing.
1469
//
1470
// This won't work correctly for pillowing off-center puzzles (e.g. mirrors) - for those we'd need
1471
// to introduce the concept of a 'sink center' as well.
1472
//
1473
// public because needed in TouchControlShapemod
1474

    
1475
  public float getCircumscribedRadius()
1476
    {
1477
    switch(mNumPuzzleFaces)
1478
      {
1479
      case  4: return (SQ6/4)*mSize;
1480
      case  6: return (SQ3/2)*mSize;
1481
      case  8: return (SQ2/2)*mSize;
1482
      case 12: return (SQ3/2)*((SQ5+1)/2)*mSize;
1483
      case 16: return 0.50f*mSize;
1484
      }
1485

    
1486
    return 0.0f;
1487
    }
1488

    
1489
///////////////////////////////////////////////////////////////////////////////////////////////////
1490

    
1491
  public ObjectSticker retSticker(int sticker)
1492
    {
1493
    if( mStickers==null )
1494
      {
1495
      float rad = getStickerRadius();
1496
      float str = getStickerStroke();
1497
      float[][] angles = getStickerAngles();
1498
      int numStickers = mStickerCoords.length;
1499
      mStickers = new ObjectSticker[numStickers];
1500

    
1501
      for(int s=0; s<numStickers; s++)
1502
        {
1503
        float scale = mStickerScales.length>s ? mStickerScales[s] : 1.0f;
1504
        float radius = rad / scale;
1505
        float stroke = str / scale;
1506
        int len = mStickerCoords[s].length/2;
1507
        float[] radii = new float[len];
1508
        for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
1509
        mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
1510
        }
1511
      }
1512

    
1513
    return mStickers[sticker];
1514
    }
1515

    
1516
///////////////////////////////////////////////////////////////////////////////////////////////////
1517
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1518

    
1519
  public void adjustStickerCoords()
1520
    {
1521

    
1522
    }
1523

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

    
1526
  public Static4D[] getQuats()
1527
    {
1528
    if( mObjectQuats==null )
1529
      {
1530
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1531
      }
1532

    
1533
    return mObjectQuats;
1534
    }
1535

    
1536
///////////////////////////////////////////////////////////////////////////////////////////////////
1537

    
1538
  public int[][] getVariantFaceIsOuter()
1539
    {
1540
    return mVariantFaceIsOuter;
1541
    }
1542

    
1543
///////////////////////////////////////////////////////////////////////////////////////////////////
1544

    
1545
  public int getInternalColor()
1546
    {
1547
    return COLOR_INTERNAL;
1548
    }
1549

    
1550
///////////////////////////////////////////////////////////////////////////////////////////////////
1551
// the getFaceColors + final INTERNAL_COLOR in a grid (so that we do not exceed the maximum texture size)
1552

    
1553
  private void createTexture()
1554
    {
1555
    Paint paint = new Paint();
1556
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_4444);
1557
    Canvas canvas = new Canvas(mBitmap);
1558

    
1559
    paint.setAntiAlias(true);
1560
    paint.setTextAlign(Paint.Align.CENTER);
1561
    paint.setStyle(Paint.Style.FILL);
1562
    paint.setColor(getInternalColor());
1563
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1564

    
1565
    int texture = 0;
1566
    FactorySticker factory = FactorySticker.getInstance();
1567

    
1568
    for(int row=0; row<mNumTexRows; row++)
1569
      for(int col=0; col<mNumTexCols; col++)
1570
        {
1571
        if( texture<mNumTextures-mNumOverrides )
1572
          {
1573
          ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1574
          int color = getColor(texture%mNumFaceColors);
1575
          factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color, sticker);
1576
          }
1577
        else if( texture>mNumTextures-mNumOverrides && texture<=mNumTextures )
1578
          {
1579
          int color = mStickerOverrides[mNumTextures-texture].getColor();
1580
          factory.drawSolidColor(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color);
1581
          }
1582

    
1583
        texture++;
1584
        }
1585
    }
1586

    
1587
///////////////////////////////////////////////////////////////////////////////////////////////////
1588

    
1589
  void setTexture()
1590
    {
1591
    if( mBitmap==null ) createTexture();
1592

    
1593
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1594
      {
1595
      int max = DistortedLibrary.getMaxTextureSize();
1596
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1597
      }
1598
    }
1599

    
1600
///////////////////////////////////////////////////////////////////////////////////////////////////
1601

    
1602
  void setObjectRatioNow(float sc, int nodeSize)
1603
    {
1604
    mObjectScreenRatio = sc;
1605
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1606
    mObjectScale.set(scale,scale,scale);
1607

    
1608
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1609
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1610
    }
1611

    
1612
///////////////////////////////////////////////////////////////////////////////////////////////////
1613

    
1614
  void setObjectRatio(float sizeChange, int nodeSize)
1615
    {
1616
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1617

    
1618
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1619
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1620

    
1621
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1622
    }
1623

    
1624
///////////////////////////////////////////////////////////////////////////////////////////////////
1625

    
1626
  void setNodeSize(int nodeSize)
1627
    {
1628
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1629
    }
1630

    
1631
///////////////////////////////////////////////////////////////////////////////////////////////////
1632

    
1633
  public float getRatio()
1634
    {
1635
    return mObjectScreenRatio;
1636
    }
1637

    
1638
///////////////////////////////////////////////////////////////////////////////////////////////////
1639

    
1640
  public float getObjectRatio()
1641
    {
1642
    return mObjectScreenRatio*mInitScreenRatio;
1643
    }
1644

    
1645
///////////////////////////////////////////////////////////////////////////////////////////////////
1646

    
1647
  boolean isSolved()
1648
    {
1649
    return mSolved.isSolved(mCubits);
1650
    }
1651

    
1652
///////////////////////////////////////////////////////////////////////////////////////////////////
1653

    
1654
  int computeNearestAngle(int basicAngle, float angle, float speed)
1655
    {
1656
    int nearestAngle = 360/basicAngle;
1657
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1658
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1659

    
1660
    if( tmp!=0 ) return nearestAngle*tmp;
1661

    
1662
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1663
    }
1664

    
1665
///////////////////////////////////////////////////////////////////////////////////////////////////
1666
// INTERNAL API - those are called from 'effects' package
1667
///////////////////////////////////////////////////////////////////////////////////////////////////
1668

    
1669
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1670
    {
1671
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total, getSignature() );
1672
    }
1673

    
1674
///////////////////////////////////////////////////////////////////////////////////////////////////
1675

    
1676
  public Static4D getRotationQuat()
1677
    {
1678
    return mQuat;
1679
    }
1680

    
1681
///////////////////////////////////////////////////////////////////////////////////////////////////
1682

    
1683
  public float getSize()
1684
    {
1685
    return mSize;
1686
    }
1687

    
1688
///////////////////////////////////////////////////////////////////////////////////////////////////
1689

    
1690
  public void applyEffect(Effect effect, int position)
1691
    {
1692
    mEffects.apply(effect, position);
1693
    }
1694

    
1695
///////////////////////////////////////////////////////////////////////////////////////////////////
1696

    
1697
  public void removeEffect(long effectID)
1698
    {
1699
    mEffects.abortById(effectID);
1700
    }
1701

    
1702
///////////////////////////////////////////////////////////////////////////////////////////////////
1703

    
1704
  public MeshBase getObjectMesh()
1705
    {
1706
    return mMesh;
1707
    }
1708

    
1709
///////////////////////////////////////////////////////////////////////////////////////////////////
1710

    
1711
  public DistortedEffects getObjectEffects()
1712
    {
1713
    return mEffects;
1714
    }
1715

    
1716
///////////////////////////////////////////////////////////////////////////////////////////////////
1717

    
1718
  public int getCubitType(int cubit)
1719
    {
1720
    return mCubits[cubit].getType();
1721
    }
1722

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

    
1725
  public float[] getCubitOffset(int cubit)
1726
    {
1727
    return mCubits[cubit].getOffset();
1728
    }
1729

    
1730
///////////////////////////////////////////////////////////////////////////////////////////////////
1731

    
1732
  public ObjectStickerOverride[] getStickerOverrides()
1733
    {
1734
    return null;
1735
    }
1736

    
1737
///////////////////////////////////////////////////////////////////////////////////////////////////
1738

    
1739
  public boolean getError()
1740
    {
1741
    return mError;
1742
    }
1743

    
1744
///////////////////////////////////////////////////////////////////////////////////////////////////
1745

    
1746
  public String getErrorString()
1747
    {
1748
    return mErrorString;
1749
    }
1750

    
1751
///////////////////////////////////////////////////////////////////////////////////////////////////
1752
// PUBLIC API
1753
///////////////////////////////////////////////////////////////////////////////////////////////////
1754

    
1755
  public int getCubitFaceStickerIndex(int cubit, int face)
1756
    {
1757
    Static4D texMap = mMesh.getTextureMap(mNumCubitFaces*cubit + face);
1758

    
1759
    int x = (int)(texMap.get0()/texMap.get2());
1760
    int y = (int)(texMap.get1()/texMap.get3());
1761

    
1762
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1763
    }
1764

    
1765
///////////////////////////////////////////////////////////////////////////////////////////////////
1766

    
1767
  public int[] getNumLayers()
1768
    {
1769
    return mNumLayers;
1770
    }
1771

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

    
1774
  public synchronized void solve()
1775
    {
1776
    for(int i=0; i<mNumCubits; i++)
1777
      {
1778
      mCubits[i].solve();
1779
      }
1780

    
1781
    recomputeFaceOffsets();
1782

    
1783
    for(int i=0; i<mNumCubits; i++)
1784
      {
1785
      mCubits[i].computeRotationRow();
1786
      setCubitQuat(i,mCubits[i].computeAssociation(),0);
1787
      }
1788
    }
1789

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

    
1792
  public int getCubitQuatIndex(int cubit)
1793
    {
1794
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1795
    }
1796

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

    
1799
  public int getCubitRotRow(int cubit, int axis)
1800
    {
1801
    return mCubits[cubit].getRotRow(axis);
1802
    }
1803

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

    
1806
  public Bitmap getStickerBitmap()
1807
    {
1808
    return mBitmap;
1809
    }
1810

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

    
1813
  public DistortedNode getNode()
1814
    {
1815
    return mNode;
1816
    }
1817

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

    
1820
  public int getNumStickerTypes()
1821
    {
1822
    return mNumStickerTypes;
1823
    }
1824

    
1825
///////////////////////////////////////////////////////////////////////////////////////////////////
1826

    
1827
  public String reportState()
1828
    {
1829
    StringBuilder builder = new StringBuilder();
1830

    
1831
    for(int i=0; i<mNumCubits; i++ )
1832
      {
1833
      if( i>0 ) builder.append('.');
1834
      builder.append(mCubits[i].mQuatIndex);
1835
      }
1836

    
1837
    return builder.toString();
1838
    }
1839

    
1840
///////////////////////////////////////////////////////////////////////////////////////////////////
1841
// this is here only so it can be overridden in TwistyJSON so that we can get this from JSON.
1842

    
1843
  public int getNumCubitFaces()
1844
    {
1845
    return 0;
1846
    }
1847

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

    
1853
  public float getPillowCoeff()
1854
    {
1855
    return 1.0f;
1856
    }
1857

    
1858
///////////////////////////////////////////////////////////////////////////////////////////////////
1859

    
1860
  public TouchControl getTouchControl()
1861
    {
1862
    if( mTouchControl==null )
1863
      {
1864
      switch(getTouchControlType())
1865
        {
1866
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
1867
                                   break;
1868
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
1869
                                   break;
1870
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
1871
                                   break;
1872
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
1873
                                   break;
1874
        case TC_ICOSAHEDRON      : mTouchControl = new TouchControlIcosahedron(this);
1875
                                   break;
1876
        case TC_CUBOID           : int[] numLayers = getNumLayers();
1877
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1878
                                   break;
1879
        case TC_BALL             : mTouchControl = new TouchControlBall(this);
1880
                                   break;
1881
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
1882
                                   break;
1883
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
1884
                                   break;
1885
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
1886
                                   break;
1887
        }
1888
      }
1889
    return mTouchControl;
1890
    }
1891

    
1892
///////////////////////////////////////////////////////////////////////////////////////////////////
1893

    
1894
  protected void setReader(JsonReader reader)
1895
    {
1896
    // empty
1897
    }
1898

    
1899
///////////////////////////////////////////////////////////////////////////////////////////////////
1900
  // for JSON only
1901
  public abstract int getTouchControlType();
1902
  public abstract int getTouchControlSplit();
1903
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1904
  public abstract int[][][] getEnabled();
1905
  public abstract float[] getDist3D(int[] numLayers);
1906
  public abstract Static3D[] getFaceAxis();
1907
  public abstract int[][] getScrambleEdges();
1908
  public abstract float[][] getCuts(int[] numLayers);
1909
  public abstract float getStickerRadius();
1910
  public abstract float getStickerStroke();
1911
  public abstract float[][] getStickerAngles();
1912
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1913
  public abstract ObjectShape getObjectShape(int variant);
1914
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
1915
  public abstract ObjectVertexEffects getVertexEffects(int variant);
1916
  public abstract int getNumCubitVariants(int[] numLayers);
1917
  public abstract float[][] getCubitPositions(int[] numLayers);
1918
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
1919
  public abstract int getNumFaceColors();
1920
  public abstract float getScreenRatio();
1921
  public abstract int getColor(int face);
1922
  public abstract String getShortName();
1923
  public abstract ObjectSignature getSignature();
1924

    
1925
  // not only for JSON
1926
  public abstract Static3D[] getRotationAxis();
1927
  public abstract int[][] getBasicAngles();
1928
  public abstract int getNumFaces();
1929
  public abstract String getObjectName();
1930
  public abstract String getInventor();
1931
  public abstract int getYearOfInvention();
1932
  public abstract int getComplexity();
1933
  public abstract int getFOV();
1934
  public abstract String[][] getTutorials();
1935
  }
(8-8/11)