Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 1e6fb034

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
import org.distorted.main.BuildConfig;
50

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
public abstract class TwistyObject extends DistortedNode
59
  {
60
  static final int COLOR_YELLOW = 0xffffff00;
61
  static final int COLOR_WHITE  = 0xffffffff;
62
  static final int COLOR_BLUE   = 0xff0000ff;
63
  static final int COLOR_GREEN  = 0xff00bb00;
64
  static final int COLOR_RED    = 0xff990000;
65
  static final int COLOR_ORANGE = 0xffff6200;
66
  static final int COLOR_GREY   = 0xff727c7b;
67
  static final int COLOR_VIOLET = 0xff7700bb;
68
  static final int COLOR_BLACK  = 0xff000000;
69

    
70
  static final int TEXTURE_HEIGHT = 256;
71
  static final int NUM_STICKERS_IN_ROW = 4;
72

    
73
  static final float SQ2 = (float)Math.sqrt(2);
74
  static final float SQ3 = (float)Math.sqrt(3);
75
  static final float SQ5 = (float)Math.sqrt(5);
76
  static final float SQ6 = (float)Math.sqrt(6);
77

    
78
  private static final float NODE_RATIO = 1.40f;
79
  private static final float MAX_SIZE_CHANGE = 1.35f;
80
  private static final float MIN_SIZE_CHANGE = 0.75f;
81

    
82
  private static final boolean mCreateFromDMesh = true;
83

    
84
  private static final Static3D CENTER = new Static3D(0,0,0);
85
  private static final int POST_ROTATION_MILLISEC = 500;
86

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

    
98
  private static float mInitScreenRatio;
99
  private static float mObjectScreenRatio = 1.0f;
100

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

    
118
  float[] mRowChances;
119
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
120
  DistortedTexture mTexture;
121
  MatrixEffectScale mScaleEffect;
122
  MatrixEffectQuaternion mQuatEffect;
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  TwistyObject(int numLayers, int realSize, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
127
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
128
    {
129
    super(nodeTexture,nodeEffects,nodeMesh);
130

    
131
    mNodeSize = screenWidth;
132

    
133
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
134

    
135
    mNumLayers = numLayers;
136
    mRealSize = realSize;
137
    mList = list;
138
    mOrigPos = getCubitPositions(mNumLayers);
139

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

    
151
    mQuatDebug = new int[NUM_CUBITS];
152

    
153
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
154
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
155

    
156
    mNodeScale= new Static3D(1,NODE_RATIO,1);
157
    mQuat = quat;
158

    
159
    mRowChances = getRowChances(mNumLayers);
160

    
161
    mRotationAngle= new Dynamic1D();
162
    mRotationAxis = new Static3D(1,0,0);
163
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
164

    
165
    mRotationAngleStatic = new Static1D(0);
166
    mRotationAngleMiddle = new Static1D(0);
167
    mRotationAngleFinal  = new Static1D(0);
168

    
169
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
170
    mObjectScale = new Static3D(scale,scale,scale);
171
    mScaleEffect = new MatrixEffectScale(mObjectScale);
172
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
173

    
174
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
175
    nodeEffects.apply(nodeScaleEffect);
176

    
177
    mNumTexCols = NUM_STICKERS_IN_ROW;
178
    mNumTexRows = (NUM_TEXTURES+1)/NUM_STICKERS_IN_ROW;
179

    
180
    if( mNumTexCols*mNumTexRows < NUM_TEXTURES+1 ) mNumTexRows++;
181

    
182
    CUBITS = new Cubit[NUM_CUBITS];
183
    createMeshAndCubits(list,res);
184

    
185
    mTexture = new DistortedTexture();
186
    mEffects = new DistortedEffects();
187

    
188
    int num_quats = QUATS.length;
189
    for(int q=0; q<num_quats; q++)
190
      {
191
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
192
      vq.setMeshAssociation(0,q);
193
      mEffects.apply(vq);
194
      }
195

    
196
    mEffects.apply(mRotateEffect);
197
    mEffects.apply(mQuatEffect);
198
    mEffects.apply(mScaleEffect);
199

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

    
204
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
205

    
206
    setupPosition(moves);
207

    
208
    setProjection(fov, 0.1f);
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  private void createMeshAndCubits(ObjectList list, Resources res)
214
    {
215
    if( mCreateFromDMesh )
216
      {
217
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mNumLayers);
218
      int resourceID= list.getResourceIDs()[sizeIndex];
219

    
220
      InputStream is = res.openRawResource(resourceID);
221
      DataInputStream dos = new DataInputStream(is);
222
      mMesh = new MeshFile(dos);
223

    
224
      try
225
        {
226
        is.close();
227
        }
228
      catch(IOException e)
229
        {
230
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
231
        }
232

    
233
      for(int i=0; i<NUM_CUBITS; i++)
234
        {
235
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
236
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
237
        }
238

    
239
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
240
      }
241
    else
242
      {
243
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
244

    
245
      for(int i=0; i<NUM_CUBITS; i++)
246
        {
247
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
248
        cubitMesh[i] = createCubitMesh(i,mNumLayers);
249
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
250
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
251
        }
252

    
253
      mMesh = new MeshJoined(cubitMesh);
254
      resetAllTextureMaps();
255
      }
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public void setObjectRatio(float sizeChange)
261
    {
262
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
263

    
264
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
265
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
266

    
267
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mRealSize;
268
    mObjectScale.set(scale,scale,scale);
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  static float getObjectRatio()
274
    {
275
    return mObjectScreenRatio*mInitScreenRatio;
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  int computeRow(float x, float y, float z, int rotIndex)
281
    {
282
    Static3D axis = ROTATION_AXIS[rotIndex];
283
    float tmp = x*axis.get0() + y*axis.get1() + z*axis.get2();
284

    
285
    for(int i=0; i<NUM_CUTS; i++)
286
      {
287
      if( tmp<CUTS[i] ) return i;
288
      }
289

    
290
    return NUM_CUTS;
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
296
    {
297
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
298
    return ((1<<cubitRow)&rowBitmap)!=0;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
// note the minus in front of the sin() - we rotate counterclockwise
303
// when looking towards the direction where the axis increases in values.
304

    
305
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
306
    {
307
    Static3D axis = ROTATION_AXIS[axisIndex];
308

    
309
    while( angleInDegrees<0 ) angleInDegrees += 360;
310
    angleInDegrees %= 360;
311
    
312
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
313
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
314

    
315
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  private synchronized void setupPosition(int[][] moves)
321
    {
322
    if( moves!=null )
323
      {
324
      Static4D quat;
325
      int index, axis, rowBitmap, angle;
326
      int corr = (360/getBasicAngle());
327

    
328
      for(int[] move: moves)
329
        {
330
        axis     = move[0];
331
        rowBitmap= move[1];
332
        angle    = move[2]*corr;
333
        quat     = makeQuaternion(axis,angle);
334

    
335
        for(int j=0; j<NUM_CUBITS; j++)
336
          if( belongsToRotation(j,axis,rowBitmap) )
337
            {
338
            index = CUBITS[j].removeRotationNow(quat);
339
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
340
            }
341
        }
342
      }
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  int getCubitFaceColorIndex(int cubit, int face)
348
    {
349
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
350

    
351
    int x = (int)(texMap.get0()/texMap.get2());
352
    int y = (int)(texMap.get1()/texMap.get3());
353

    
354
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
359

    
360
  void clampPos(Static3D pos)
361
    {
362
    float currError, minError = Float.MAX_VALUE;
363
    int minErrorIndex= -1;
364
    float x = pos.get0();
365
    float y = pos.get1();
366
    float z = pos.get2();
367
    float xo,yo,zo;
368

    
369
    for(int i=0; i<NUM_CUBITS; i++)
370
      {
371
      xo = mOrigPos[i].get0();
372
      yo = mOrigPos[i].get1();
373
      zo = mOrigPos[i].get2();
374

    
375
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
376

    
377
      if( currError<minError )
378
        {
379
        minError = currError;
380
        minErrorIndex = i;
381
        }
382
      }
383

    
384
    pos.set( mOrigPos[minErrorIndex] );
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
389

    
390
  public void createTexture()
391
    {
392
    Bitmap bitmap;
393

    
394
    Paint paint = new Paint();
395
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
396
    Canvas canvas = new Canvas(bitmap);
397

    
398
    paint.setAntiAlias(true);
399
    paint.setTextAlign(Paint.Align.CENTER);
400
    paint.setStyle(Paint.Style.FILL);
401

    
402
    paint.setColor(COLOR_BLACK);
403
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
404

    
405
    int tex = 0;
406

    
407
    for(int row=0; row<mNumTexRows; row++)
408
      for(int col=0; col<mNumTexCols; col++)
409
        {
410
        if( tex>=NUM_TEXTURES ) break;
411
        createFaceTexture(canvas, paint, tex, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT);
412
        tex++;
413
        }
414

    
415
    if( !mTexture.setTexture(bitmap) )
416
      {
417
      int max = DistortedLibrary.getMaxTextureSize();
418
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
419
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
420
      }
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public int getNumLayers()
426
    {
427
    return mNumLayers;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  public void continueRotation(float angleInDegrees)
433
    {
434
    mRotationAngleStatic.set0(angleInDegrees);
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  public Static4D getRotationQuat()
440
      {
441
      return mQuat;
442
      }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  public void recomputeScaleFactor(int scrWidth)
447
    {
448
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public void savePreferences(SharedPreferences.Editor editor)
454
    {
455
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459

    
460
  public synchronized void restorePreferences(SharedPreferences preferences)
461
    {
462
    boolean error = false;
463

    
464
    for(int i=0; i<NUM_CUBITS; i++)
465
      {
466
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
467

    
468
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<QUATS.length)
469
        {
470
        CUBITS[i].modifyCurrentPosition(QUATS[mQuatDebug[i]]);
471
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
472
        }
473
      else
474
        {
475
        error = true;
476
        }
477
      }
478

    
479
    if( error )
480
      {
481
      for(int i=0; i<NUM_CUBITS; i++)
482
        {
483
        CUBITS[i].solve();
484
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
485
        }
486
      recordQuatsState("Failed to restorePreferences");
487
      }
488
    }
489

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

    
492
  public void recordQuatsState(String message)
493
    {
494
    StringBuilder quats = new StringBuilder();
495

    
496
    for(int j=0; j<NUM_CUBITS; j++)
497
      {
498
      quats.append(mQuatDebug[j]);
499
      quats.append(" ");
500
      }
501

    
502
    if( BuildConfig.DEBUG )
503
      {
504
      android.util.Log.e("quats" , quats.toString());
505
      android.util.Log.e("object", mList.name()+"_"+mNumLayers);
506
      }
507
    else
508
      {
509
      Exception ex = new Exception(message);
510
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
511
      crashlytics.setCustomKey("quats" , quats.toString());
512
      crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
513
      crashlytics.recordException(ex);
514
      }
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
  public void releaseResources()
520
    {
521
    mTexture.markForDeletion();
522
    mMesh.markForDeletion();
523
    mEffects.markForDeletion();
524

    
525
    for(int j=0; j<NUM_CUBITS; j++)
526
      {
527
      CUBITS[j].releaseResources();
528
      }
529
    }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
  public void apply(Effect effect, int position)
534
    {
535
    mEffects.apply(effect, position);
536
    }
537

    
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539

    
540
  public void remove(long effectID)
541
    {
542
    mEffects.abortById(effectID);
543
    }
544

    
545
///////////////////////////////////////////////////////////////////////////////////////////////////
546

    
547
  public synchronized void solve()
548
    {
549
    for(int i=0; i<NUM_CUBITS; i++)
550
      {
551
      CUBITS[i].solve();
552
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
553
      }
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
  public void resetAllTextureMaps()
559
    {
560
    final float ratioW = 1.0f/mNumTexCols;
561
    final float ratioH = 1.0f/mNumTexRows;
562
    int color, row, col;
563

    
564
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
565
      {
566
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
567

    
568
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
569
        {
570
        color = getFaceColor(cubit,cubitface,mNumLayers);
571
        row = (mNumTexRows-1) - color/mNumTexCols;
572
        col = color%mNumTexCols;
573
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
574
        }
575

    
576
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
577
      }
578
    }
579

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

    
582
  public void setTextureMap(int cubit, int face, int newColor)
583
    {
584
    final float ratioW = 1.0f/mNumTexCols;
585
    final float ratioH = 1.0f/mNumTexRows;
586
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
587
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
588
    int col = newColor%mNumTexCols;
589

    
590
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
591
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
592
    }
593

    
594
///////////////////////////////////////////////////////////////////////////////////////////////////
595

    
596
  public synchronized void beginNewRotation(int axis, int row )
597
    {
598
    if( axis<0 || axis>=ROTATION_AXIS.length )
599
      {
600
      android.util.Log.e("object", "invalid rotation axis: "+axis);
601
      return;
602
      }
603
    if( row<0 || row>=mNumLayers )
604
      {
605
      android.util.Log.e("object", "invalid rotation row: "+row);
606
      return;
607
      }
608

    
609
    mRotAxis     = axis;
610
    mRotRowBitmap= (1<<row);
611
    mRotationAngleStatic.set0(0.0f);
612
    mRotationAxis.set( ROTATION_AXIS[axis] );
613
    mRotationAngle.add(mRotationAngleStatic);
614
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
615
    }
616

    
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618

    
619
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
620
    {
621
    mRotAxis     = axis;
622
    mRotRowBitmap= rowBitmap;
623

    
624
    mRotationAngleStatic.set0(0.0f);
625
    mRotationAxis.set( ROTATION_AXIS[axis] );
626
    mRotationAngle.setDuration(durationMillis);
627
    mRotationAngle.resetToBeginning();
628
    mRotationAngle.add(new Static1D(0));
629
    mRotationAngle.add(new Static1D(angle));
630
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
631
    mRotateEffect.notifyWhenFinished(listener);
632

    
633
    return mRotateEffect.getID();
634
    }
635

    
636
///////////////////////////////////////////////////////////////////////////////////////////////////
637

    
638
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
639
    {
640
    float angle = getAngle();
641
    mRotationAngleStatic.set0(angle);
642
    mRotationAngleFinal.set0(nearestAngleInDegrees);
643
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
644

    
645
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
646
    mRotationAngle.resetToBeginning();
647
    mRotationAngle.removeAll();
648
    mRotationAngle.add(mRotationAngleStatic);
649
    mRotationAngle.add(mRotationAngleMiddle);
650
    mRotationAngle.add(mRotationAngleFinal);
651
    mRotateEffect.notifyWhenFinished(listener);
652

    
653
    return mRotateEffect.getID();
654
    }
655

    
656

    
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658

    
659
  private float getAngle()
660
    {
661
    int pointNum = mRotationAngle.getNumPoints();
662

    
663
    if( pointNum>=1 )
664
      {
665
      return mRotationAngle.getPoint(pointNum-1).get0();
666
      }
667
    else
668
      {
669
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
670
      crashlytics.log("points in RotationAngle: "+pointNum);
671
      return 0;
672
      }
673
    }
674

    
675
///////////////////////////////////////////////////////////////////////////////////////////////////
676

    
677
  public synchronized void removeRotationNow()
678
    {
679
    float angle = getAngle();
680
    double nearestAngleInRadians = angle*Math.PI/180;
681
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
682
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
683
    float axisX = ROTATION_AXIS[mRotAxis].get0();
684
    float axisY = ROTATION_AXIS[mRotAxis].get1();
685
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
686
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
687

    
688
    mRotationAngle.removeAll();
689
    mRotationAngleStatic.set0(0);
690

    
691
    for(int i=0; i<NUM_CUBITS; i++)
692
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
693
        {
694
        int index = CUBITS[i].removeRotationNow(quat);
695
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
696
        }
697
    }
698

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

    
701
  public void initializeObject(int[][] moves)
702
    {
703
    solve();
704
    setupPosition(moves);
705
    }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708

    
709
  public int getCubit(float[] point3D)
710
    {
711
    float dist, minDist = Float.MAX_VALUE;
712
    int currentBest=-1;
713
    float multiplier = returnMultiplier();
714

    
715
    point3D[0] *= multiplier;
716
    point3D[1] *= multiplier;
717
    point3D[2] *= multiplier;
718

    
719
    for(int i=0; i<NUM_CUBITS; i++)
720
      {
721
      dist = CUBITS[i].getDistSquared(point3D);
722
      if( dist<minDist )
723
        {
724
        minDist = dist;
725
        currentBest = i;
726
        }
727
      }
728

    
729
    return currentBest;
730
    }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733

    
734
  public int computeNearestAngle(float angle, float speed)
735
    {
736
    final int NEAREST = 360/getBasicAngle();
737

    
738
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
739
    if( angle< -(NEAREST*0.5) ) tmp-=1;
740

    
741
    if( tmp!=0 ) return NEAREST*tmp;
742

    
743
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
744
    }
745

    
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747

    
748
  public int getNodeSize()
749
    {
750
    return mNodeSize;
751
    }
752

    
753
///////////////////////////////////////////////////////////////////////////////////////////////////
754

    
755
  public ObjectList getObjectList()
756
    {
757
    return mList;
758
    }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761

    
762
  abstract float getScreenRatio();
763
  abstract Static3D[] getCubitPositions(int numLayers);
764
  abstract Static4D[] getQuats();
765
  abstract int getNumFaces();
766
  abstract int getNumStickerTypes(int numLayers);
767
  abstract int getNumCubitFaces();
768
  abstract MeshBase createCubitMesh(int cubit, int numLayers);
769
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top);
770
  abstract int getFaceColor(int cubit, int cubitface, int numLayers);
771
  abstract float returnMultiplier();
772
  abstract float[] getRowChances(int numLayers);
773
  abstract float[] getCuts(int numLayers);
774
  abstract boolean shouldResetTextureMaps();
775

    
776
  public abstract boolean isSolved();
777
  public abstract Static3D[] getRotationAxis();
778
  public abstract int getBasicAngle();
779
  public abstract String retObjectString();
780
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
781
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
782
  public abstract int getObjectName(int numLayers);
783
  public abstract int getInventor(int numLayers);
784
  public abstract int getComplexity(int numLayers);
785
  }
(26-26/30)