Project

General

Profile

Download (24 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ a15078bb

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objects;
21

    
22
import android.content.SharedPreferences;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27

    
28
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29

    
30
import org.distorted.library.effect.Effect;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectQuaternion;
35
import org.distorted.library.effect.VertexEffectRotate;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedNode;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshFile;
42
import org.distorted.library.mesh.MeshJoined;
43
import org.distorted.library.mesh.MeshSquare;
44
import org.distorted.library.message.EffectListener;
45
import org.distorted.library.type.Dynamic1D;
46
import org.distorted.library.type.Static1D;
47
import org.distorted.library.type.Static3D;
48
import org.distorted.library.type.Static4D;
49

    
50
import java.io.DataInputStream;
51
import java.io.IOException;
52
import java.io.InputStream;
53
import java.util.Random;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
public abstract class TwistyObject extends DistortedNode
58
  {
59
  static final int COLOR_YELLOW = 0xffffff00;
60
  static final int COLOR_WHITE  = 0xffffffff;
61
  static final int COLOR_BLUE   = 0xff0000ff;
62
  static final int COLOR_GREEN  = 0xff00ff00;
63
  static final int COLOR_RED    = 0xffff0000;
64
  static final int COLOR_BROWN  = 0xffb5651d;
65
  static final int COLOR_PINK   = 0xffff90ff;
66
  static final int COLOR_VIOLET = 0xff7700bb;
67
  static final int COLOR_BLACK  = 0xff000000;
68

    
69
  static final int TEXTURE_HEIGHT = 256;
70

    
71
  static final float SQ2 = (float)Math.sqrt(2);
72
  static final float SQ3 = (float)Math.sqrt(3);
73
  static final float SQ6 = (float)Math.sqrt(6);
74

    
75
  private static final float NODE_RATIO = 1.40f;
76
  private static final float MAX_SIZE_CHANGE = 1.35f;
77
  private static final float MIN_SIZE_CHANGE = 0.8f;
78

    
79
  private static boolean mCreateFromDMesh = false;
80

    
81
  private static final Static3D CENTER = new Static3D(0,0,0);
82
  private static final int POST_ROTATION_MILLISEC = 500;
83

    
84
  final Static3D[] ROTATION_AXIS;
85
  final Static4D[] QUATS;
86
  final Cubit[] CUBITS;
87
  final int NUM_FACES;
88
  final int NUM_TEXTURES;
89
  final int NUM_CUBIT_FACES;
90
  final int NUM_AXIS;
91
  final int NUM_CUBITS;
92
  final float[] CUTS;
93
  final int NUM_CUTS;
94

    
95
  private static float mInitScreenRatio;
96
  private static float mObjectScreenRatio = 1.0f;
97

    
98
  private final int mNodeSize;
99
  private int mRotRowBitmap;
100
  private int mRotAxis;
101
  private Static3D[] mOrigPos;
102
  private Static3D mNodeScale;
103
  private Static4D mQuat;
104
  private int mNumLayers, mRealSize;
105
  private ObjectList mList;
106
  private MeshBase mMesh;
107
  private DistortedEffects mEffects;
108
  private VertexEffectRotate mRotateEffect;
109
  private Dynamic1D mRotationAngle;
110
  private Static3D mRotationAxis;
111
  private Static3D mObjectScale;
112
  private int[] mQuatDebug;
113

    
114
  float[] mRowChances;
115
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
116
  DistortedTexture mTexture;
117
  MatrixEffectScale mScaleEffect;
118
  MatrixEffectQuaternion mQuatEffect;
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  TwistyObject(int numLayers, int realSize, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
123
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
124
    {
125
    super(nodeTexture,nodeEffects,nodeMesh);
126

    
127
    mNodeSize = screenWidth;
128

    
129
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
130

    
131
    mNumLayers = numLayers;
132
    mRealSize = realSize;
133
    mList = list;
134
    mOrigPos = getCubitPositions(mNumLayers);
135

    
136
    QUATS = getQuats();
137
    NUM_CUBITS  = mOrigPos.length;
138
    ROTATION_AXIS = getRotationAxis();
139
    NUM_AXIS = ROTATION_AXIS.length;
140
    mInitScreenRatio = getScreenRatio();
141
    NUM_FACES = getNumFaces();
142
    NUM_CUBIT_FACES = getNumCubitFaces();
143
    NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
144
    CUTS = getCuts(mNumLayers);
145
    NUM_CUTS = CUTS.length;
146

    
147
    mQuatDebug = new int[NUM_CUBITS];
148

    
149
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
150
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
151

    
152
    mNodeScale= new Static3D(1,NODE_RATIO,1);
153
    mQuat = quat;
154

    
155
    mRowChances = getRowChances();
156

    
157
    mRotationAngle= new Dynamic1D();
158
    mRotationAxis = new Static3D(1,0,0);
159
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
160

    
161
    mRotationAngleStatic = new Static1D(0);
162
    mRotationAngleMiddle = new Static1D(0);
163
    mRotationAngleFinal  = new Static1D(0);
164

    
165
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
166
    mObjectScale = new Static3D(scale,scale,scale);
167
    mScaleEffect = new MatrixEffectScale(mObjectScale);
168
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
169

    
170
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
171
    nodeEffects.apply(nodeScaleEffect);
172

    
173
    CUBITS = new Cubit[NUM_CUBITS];
174
    createMeshAndCubits(list,res);
175

    
176
    mTexture = new DistortedTexture();
177
    mEffects = new DistortedEffects();
178

    
179
    int num_quats = QUATS.length;
180
    for(int q=0; q<num_quats; q++)
181
      {
182
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
183
      vq.setMeshAssociation(0,q);
184
      mEffects.apply(vq);
185
      }
186

    
187
    mEffects.apply(mRotateEffect);
188
    mEffects.apply(mQuatEffect);
189
    mEffects.apply(mScaleEffect);
190

    
191
    // Now postprocessed effects (the glow when you solve an object) require component centers. In
192
    // order for the effect to be in front of the object, we need to set the center to be behind it.
193
    getMesh().setComponentCenter(0,0,0,-0.1f);
194

    
195
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
196

    
197
    setupPosition(moves);
198

    
199
    setProjection(fov, 0.1f);
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  private void createMeshAndCubits(ObjectList list, Resources res)
205
    {
206
    if( mCreateFromDMesh )
207
      {
208
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
209
      int resourceID= list.getResourceIDs()[sizeIndex];
210

    
211
      InputStream is = res.openRawResource(resourceID);
212
      DataInputStream dos = new DataInputStream(is);
213
      mMesh = new MeshFile(dos);
214

    
215
      try
216
        {
217
        is.close();
218
        }
219
      catch(IOException e)
220
        {
221
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
222
        }
223

    
224
      for(int i=0; i<NUM_CUBITS; i++)
225
        {
226
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
227
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
228
        }
229

    
230
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
231
      }
232
    else
233
      {
234
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
235

    
236
      for(int i=0; i<NUM_CUBITS; i++)
237
        {
238
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
239
        cubitMesh[i] = createCubitMesh(i);
240
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
241
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
242
        }
243

    
244
      mMesh = new MeshJoined(cubitMesh);
245
      resetAllTextureMaps();
246
      }
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  public void setObjectRatio(float sizeChange)
252
    {
253
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
254

    
255
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
256
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
257

    
258
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
259
    mObjectScale.set(scale,scale,scale);
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  static float getObjectRatio()
265
    {
266
    return mObjectScreenRatio*mInitScreenRatio;
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  int computeRow(float x, float y, float z, int rotIndex)
272
    {
273
    Static3D axis = ROTATION_AXIS[rotIndex];
274
    float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
275

    
276
    for(int i=0; i<NUM_CUTS; i++)
277
      {
278
      if( tmp<CUTS[i] ) return i;
279
      }
280

    
281
    return NUM_CUTS;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
287
    {
288
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
289
    return ((1<<cubitRow)&rowBitmap)!=0;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293
// note the minus in front of the sin() - we rotate counterclockwise
294
// when looking towards the direction where the axis increases in values.
295

    
296
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
297
    {
298
    Static3D axis = ROTATION_AXIS[axisIndex];
299

    
300
    while( angleInDegrees<0 ) angleInDegrees += 360;
301
    angleInDegrees %= 360;
302
    
303
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
304
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
305

    
306
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
307
    }
308

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

    
311
  private synchronized void setupPosition(int[][] moves)
312
    {
313
    if( moves!=null )
314
      {
315
      Static4D quat;
316
      int index, axis, rowBitmap, angle;
317
      int corr = (360/getBasicAngle());
318

    
319
      for(int[] move: moves)
320
        {
321
        axis     = move[0];
322
        rowBitmap= move[1];
323
        angle    = move[2]*corr;
324
        quat     = makeQuaternion(axis,angle);
325

    
326
        for(int j=0; j<NUM_CUBITS; j++)
327
          if( belongsToRotation(j,axis,rowBitmap) )
328
            {
329
            index = CUBITS[j].removeRotationNow(quat);
330
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
331
            }
332
        }
333
      }
334
    }
335

    
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337

    
338
  int getCubitFaceColorIndex(int cubit, int face)
339
    {
340
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
341
    return (int)(texMap.get0() / texMap.get2());
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
346

    
347
  void clampPos(Static3D pos)
348
    {
349
    float currError, minError = Float.MAX_VALUE;
350
    int minErrorIndex= -1;
351
    float x = pos.get0();
352
    float y = pos.get1();
353
    float z = pos.get2();
354
    float xo,yo,zo;
355

    
356
    for(int i=0; i<NUM_CUBITS; i++)
357
      {
358
      xo = mOrigPos[i].get0();
359
      yo = mOrigPos[i].get1();
360
      zo = mOrigPos[i].get2();
361

    
362
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
363

    
364
      if( currError<minError )
365
        {
366
        minError = currError;
367
        minErrorIndex = i;
368
        }
369
      }
370

    
371
    pos.set( mOrigPos[minErrorIndex] );
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
// the getFaceColors + final black in a horizontal strip.
376

    
377
  public void createTexture()
378
    {
379
    Bitmap bitmap;
380

    
381
    Paint paint = new Paint();
382
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
383
    Canvas canvas = new Canvas(bitmap);
384

    
385
    paint.setAntiAlias(true);
386
    paint.setTextAlign(Paint.Align.CENTER);
387
    paint.setStyle(Paint.Style.FILL);
388

    
389
    paint.setColor(COLOR_BLACK);
390
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
391

    
392
    for(int i=0; i<NUM_TEXTURES; i++)
393
      {
394
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT);
395
      }
396

    
397
    if( !mTexture.setTexture(bitmap) )
398
      {
399
      int max = DistortedLibrary.getMaxTextureSize();
400
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
401
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
402
      }
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  public int getNumLayers()
408
    {
409
    return mNumLayers;
410
    }
411

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

    
414
  public void continueRotation(float angleInDegrees)
415
    {
416
    mRotationAngleStatic.set0(angleInDegrees);
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  public Static4D getRotationQuat()
422
      {
423
      return mQuat;
424
      }
425

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

    
428
  public void recomputeScaleFactor(int scrWidth)
429
    {
430
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public void savePreferences(SharedPreferences.Editor editor)
436
    {
437
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
438
    }
439

    
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441

    
442
  public synchronized void restorePreferences(SharedPreferences preferences)
443
    {
444
    for(int i=0; i<NUM_CUBITS; i++)
445
      {
446
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
447

    
448
      if( mQuatDebug[i]<0 )
449
        {
450
        for(int j=0; j<NUM_CUBITS; j++)
451
          {
452
          CUBITS[j].modifyCurrentPosition(QUATS[0]);
453
          mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),0);
454
          }
455
        recordQuatsState("Failed to restorePreferences");
456

    
457
        break;
458
        }
459

    
460
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
461
      }
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  public void recordQuatsState(String message)
467
    {
468
    StringBuilder quats = new StringBuilder();
469

    
470
    for(int j=0; j<NUM_CUBITS; j++)
471
      {
472
      quats.append(mQuatDebug[j]);
473
      quats.append(" ");
474
      }
475

    
476
    Exception ex = new Exception(message);
477
    FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
478
    crashlytics.setCustomKey("quats" , quats.toString());
479
    crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
480
    crashlytics.recordException(ex);
481
    }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
  public void releaseResources()
486
    {
487
    mTexture.markForDeletion();
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
  public void apply(Effect effect, int position)
493
    {
494
    mEffects.apply(effect, position);
495
    }
496

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////
498

    
499
  public void remove(long effectID)
500
    {
501
    mEffects.abortById(effectID);
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505

    
506
  public synchronized void solve()
507
    {
508
    for(int i=0; i<NUM_CUBITS; i++)
509
      {
510
      CUBITS[i].solve();
511
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
512
      }
513
    }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516

    
517
  public void resetAllTextureMaps()
518
    {
519
    final float ratio = 1.0f/(NUM_TEXTURES+1);
520
    int color;
521

    
522
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
523
      {
524
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
525

    
526
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
527
        {
528
        color = getFaceColor(cubit,cubitface,mNumLayers);
529
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
530
        }
531

    
532
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
533
      }
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
  public void setTextureMap(int cubit, int face, int newColor)
539
    {
540
    final float ratio = 1.0f/(NUM_TEXTURES+1);
541
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
542

    
543
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
544
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
545
    }
546

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

    
549
  public synchronized void beginNewRotation(int axis, int row )
550
    {
551
    if( axis<0 || axis>=ROTATION_AXIS.length )
552
      {
553
      android.util.Log.e("object", "invalid rotation axis: "+axis);
554
      return;
555
      }
556
    if( row<0 || row>=mNumLayers )
557
      {
558
      android.util.Log.e("object", "invalid rotation row: "+row);
559
      return;
560
      }
561

    
562
    mRotAxis     = axis;
563
    mRotRowBitmap= (1<<row);
564
    mRotationAngleStatic.set0(0.0f);
565
    mRotationAxis.set( ROTATION_AXIS[axis] );
566
    mRotationAngle.add(mRotationAngleStatic);
567
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
568
    }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

    
572
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
573
    {
574
    mRotAxis     = axis;
575
    mRotRowBitmap= rowBitmap;
576

    
577
    mRotationAngleStatic.set0(0.0f);
578
    mRotationAxis.set( ROTATION_AXIS[axis] );
579
    mRotationAngle.setDuration(durationMillis);
580
    mRotationAngle.resetToBeginning();
581
    mRotationAngle.add(new Static1D(0));
582
    mRotationAngle.add(new Static1D(angle));
583
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
584
    mRotateEffect.notifyWhenFinished(listener);
585

    
586
    return mRotateEffect.getID();
587
    }
588

    
589
///////////////////////////////////////////////////////////////////////////////////////////////////
590

    
591
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
592
    {
593
    float angle = getAngle();
594
    mRotationAngleStatic.set0(angle);
595
    mRotationAngleFinal.set0(nearestAngleInDegrees);
596
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
597

    
598
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
599
    mRotationAngle.resetToBeginning();
600
    mRotationAngle.removeAll();
601
    mRotationAngle.add(mRotationAngleStatic);
602
    mRotationAngle.add(mRotationAngleMiddle);
603
    mRotationAngle.add(mRotationAngleFinal);
604
    mRotateEffect.notifyWhenFinished(listener);
605

    
606
    return mRotateEffect.getID();
607
    }
608

    
609

    
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611

    
612
  private float getAngle()
613
    {
614
    int pointNum = mRotationAngle.getNumPoints();
615

    
616
    if( pointNum>=1 )
617
      {
618
      return mRotationAngle.getPoint(pointNum-1).get0();
619
      }
620
    else
621
      {
622
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
623
      crashlytics.log("points in RotationAngle: "+pointNum);
624
      return 0;
625
      }
626
    }
627

    
628
///////////////////////////////////////////////////////////////////////////////////////////////////
629

    
630
  public synchronized void removeRotationNow()
631
    {
632
    float angle = getAngle();
633
    double nearestAngleInRadians = angle*Math.PI/180;
634
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
635
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
636
    float axisX = ROTATION_AXIS[mRotAxis].get0();
637
    float axisY = ROTATION_AXIS[mRotAxis].get1();
638
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
639
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
640

    
641
    mRotationAngle.removeAll();
642
    mRotationAngleStatic.set0(0);
643

    
644
    for(int i=0; i<NUM_CUBITS; i++)
645
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
646
        {
647
        int index = CUBITS[i].removeRotationNow(quat);
648
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
649
        }
650
    }
651

    
652
///////////////////////////////////////////////////////////////////////////////////////////////////
653

    
654
  public void initializeObject(int[][] moves)
655
    {
656
    solve();
657
    setupPosition(moves);
658
    }
659

    
660
///////////////////////////////////////////////////////////////////////////////////////////////////
661

    
662
  public int getCubit(float[] point3D)
663
    {
664
    float dist, minDist = Float.MAX_VALUE;
665
    int currentBest=-1;
666
    float multiplier = returnMultiplier();
667

    
668
    point3D[0] *= multiplier;
669
    point3D[1] *= multiplier;
670
    point3D[2] *= multiplier;
671

    
672
    for(int i=0; i<NUM_CUBITS; i++)
673
      {
674
      dist = CUBITS[i].getDistSquared(point3D);
675
      if( dist<minDist )
676
        {
677
        minDist = dist;
678
        currentBest = i;
679
        }
680
      }
681

    
682
    return currentBest;
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686

    
687
  public int computeNearestAngle(float angle, float speed)
688
    {
689
    final int NEAREST = 360/getBasicAngle();
690

    
691
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
692
    if( angle< -(NEAREST*0.5) ) tmp-=1;
693

    
694
    if( tmp!=0 ) return NEAREST*tmp;
695

    
696
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
697
    }
698

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

    
701
  public int getNodeSize()
702
    {
703
    return mNodeSize;
704
    }
705

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

    
708
  public ObjectList getObjectList()
709
    {
710
    return mList;
711
    }
712

    
713
///////////////////////////////////////////////////////////////////////////////////////////////////
714

    
715
  abstract float getScreenRatio();
716
  abstract Static3D[] getCubitPositions(int size);
717
  abstract Static4D[] getQuats();
718
  abstract int getNumFaces();
719
  abstract int getNumStickerTypes();
720
  abstract int getNumCubitFaces();
721
  abstract MeshBase createCubitMesh(int cubit);
722
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left);
723
  abstract int getFaceColor(int cubit, int cubitface, int size);
724
  abstract float returnMultiplier();
725
  abstract float[] getRowChances();
726
  abstract float[] getCuts(int size);
727
  abstract boolean shouldResetTextureMaps();
728

    
729
  public abstract boolean isSolved();
730
  public abstract Static3D[] getRotationAxis();
731
  public abstract int getBasicAngle();
732
  public abstract String retObjectString();
733
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
734
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
735
  }
(19-19/22)