Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 985f3dfa

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 wasRotateApplied()
296
    {
297
    return mEffects.exists(mRotateEffect.getID());
298
    }
299

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

    
302
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
303
    {
304
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
305
    return ((1<<cubitRow)&rowBitmap)!=0;
306
    }
307

    
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
// note the minus in front of the sin() - we rotate counterclockwise
310
// when looking towards the direction where the axis increases in values.
311

    
312
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
313
    {
314
    Static3D axis = ROTATION_AXIS[axisIndex];
315

    
316
    while( angleInDegrees<0 ) angleInDegrees += 360;
317
    angleInDegrees %= 360;
318
    
319
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
320
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
321

    
322
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  private synchronized void setupPosition(int[][] moves)
328
    {
329
    if( moves!=null )
330
      {
331
      Static4D quat;
332
      int index, axis, rowBitmap, angle;
333
      int corr = (360/getBasicAngle());
334

    
335
      for(int[] move: moves)
336
        {
337
        axis     = move[0];
338
        rowBitmap= move[1];
339
        angle    = move[2]*corr;
340
        quat     = makeQuaternion(axis,angle);
341

    
342
        for(int j=0; j<NUM_CUBITS; j++)
343
          if( belongsToRotation(j,axis,rowBitmap) )
344
            {
345
            index = CUBITS[j].removeRotationNow(quat);
346
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
347
            }
348
        }
349
      }
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  int getCubitFaceColorIndex(int cubit, int face)
355
    {
356
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
357

    
358
    int x = (int)(texMap.get0()/texMap.get2());
359
    int y = (int)(texMap.get1()/texMap.get3());
360

    
361
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
366

    
367
  void clampPos(Static3D pos)
368
    {
369
    float currError, minError = Float.MAX_VALUE;
370
    int minErrorIndex= -1;
371
    float x = pos.get0();
372
    float y = pos.get1();
373
    float z = pos.get2();
374
    float xo,yo,zo;
375

    
376
    for(int i=0; i<NUM_CUBITS; i++)
377
      {
378
      xo = mOrigPos[i].get0();
379
      yo = mOrigPos[i].get1();
380
      zo = mOrigPos[i].get2();
381

    
382
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
383

    
384
      if( currError<minError )
385
        {
386
        minError = currError;
387
        minErrorIndex = i;
388
        }
389
      }
390

    
391
    pos.set( mOrigPos[minErrorIndex] );
392
    }
393

    
394
///////////////////////////////////////////////////////////////////////////////////////////////////
395
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
396

    
397
  public void createTexture()
398
    {
399
    Bitmap bitmap;
400

    
401
    Paint paint = new Paint();
402
    bitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
403
    Canvas canvas = new Canvas(bitmap);
404

    
405
    paint.setAntiAlias(true);
406
    paint.setTextAlign(Paint.Align.CENTER);
407
    paint.setStyle(Paint.Style.FILL);
408

    
409
    paint.setColor(COLOR_BLACK);
410
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
411

    
412
    int tex = 0;
413

    
414
    for(int row=0; row<mNumTexRows; row++)
415
      for(int col=0; col<mNumTexCols; col++)
416
        {
417
        if( tex>=NUM_TEXTURES ) break;
418
        createFaceTexture(canvas, paint, tex, col*TEXTURE_HEIGHT, row*TEXTURE_HEIGHT);
419
        tex++;
420
        }
421

    
422
    if( !mTexture.setTexture(bitmap) )
423
      {
424
      int max = DistortedLibrary.getMaxTextureSize();
425
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
426
      crashlytics.log("failed to set texture of size "+bitmap.getWidth()+"x"+bitmap.getHeight()+" max is "+max);
427
      }
428
    }
429

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

    
432
  public int getNumLayers()
433
    {
434
    return mNumLayers;
435
    }
436

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

    
439
  public void continueRotation(float angleInDegrees)
440
    {
441
    mRotationAngleStatic.set0(angleInDegrees);
442
    }
443

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

    
446
  public Static4D getRotationQuat()
447
      {
448
      return mQuat;
449
      }
450

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

    
453
  public void recomputeScaleFactor(int scrWidth)
454
    {
455
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
456
    }
457

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

    
460
  public void savePreferences(SharedPreferences.Editor editor)
461
    {
462
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466

    
467
  public synchronized void restorePreferences(SharedPreferences preferences)
468
    {
469
    boolean error = false;
470

    
471
    for(int i=0; i<NUM_CUBITS; i++)
472
      {
473
      mQuatDebug[i] = CUBITS[i].restorePreferences(preferences);
474

    
475
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<QUATS.length)
476
        {
477
        CUBITS[i].modifyCurrentPosition(QUATS[mQuatDebug[i]]);
478
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),mQuatDebug[i]);
479
        }
480
      else
481
        {
482
        error = true;
483
        }
484
      }
485

    
486
    if( error )
487
      {
488
      for(int i=0; i<NUM_CUBITS; i++)
489
        {
490
        CUBITS[i].solve();
491
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),0);
492
        }
493
      recordQuatsState("Failed to restorePreferences");
494
      }
495
    }
496

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

    
499
  public void recordQuatsState(String message)
500
    {
501
    StringBuilder quats = new StringBuilder();
502

    
503
    for(int j=0; j<NUM_CUBITS; j++)
504
      {
505
      quats.append(mQuatDebug[j]);
506
      quats.append(" ");
507
      }
508

    
509
    if( BuildConfig.DEBUG )
510
      {
511
      android.util.Log.e("quats" , quats.toString());
512
      android.util.Log.e("object", mList.name()+"_"+mNumLayers);
513
      }
514
    else
515
      {
516
      Exception ex = new Exception(message);
517
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
518
      crashlytics.setCustomKey("quats" , quats.toString());
519
      crashlytics.setCustomKey("object", mList.name()+"_"+mNumLayers );
520
      crashlytics.recordException(ex);
521
      }
522
    }
523

    
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525

    
526
  public void releaseResources()
527
    {
528
    mTexture.markForDeletion();
529
    mMesh.markForDeletion();
530
    mEffects.markForDeletion();
531

    
532
    for(int j=0; j<NUM_CUBITS; j++)
533
      {
534
      CUBITS[j].releaseResources();
535
      }
536
    }
537

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

    
540
  public void apply(Effect effect, int position)
541
    {
542
    mEffects.apply(effect, position);
543
    }
544

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

    
547
  public void remove(long effectID)
548
    {
549
    mEffects.abortById(effectID);
550
    }
551

    
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553

    
554
  public synchronized void solve()
555
    {
556
    for(int i=0; i<NUM_CUBITS; i++)
557
      {
558
      CUBITS[i].solve();
559
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
560
      }
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

    
565
  public void resetAllTextureMaps()
566
    {
567
    final float ratioW = 1.0f/mNumTexCols;
568
    final float ratioH = 1.0f/mNumTexRows;
569
    int color, row, col;
570

    
571
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
572
      {
573
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
574

    
575
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
576
        {
577
        color = getFaceColor(cubit,cubitface,mNumLayers);
578
        row = (mNumTexRows-1) - color/mNumTexCols;
579
        col = color%mNumTexCols;
580
        maps[cubitface] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
581
        }
582

    
583
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
584
      }
585
    }
586

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

    
589
  public void setTextureMap(int cubit, int face, int newColor)
590
    {
591
    final float ratioW = 1.0f/mNumTexCols;
592
    final float ratioH = 1.0f/mNumTexRows;
593
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
594
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
595
    int col = newColor%mNumTexCols;
596

    
597
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
598
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
599
    }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

    
603
  public synchronized void beginNewRotation(int axis, int row )
604
    {
605
    if( axis<0 || axis>=ROTATION_AXIS.length )
606
      {
607
      android.util.Log.e("object", "invalid rotation axis: "+axis);
608
      return;
609
      }
610
    if( row<0 || row>=mNumLayers )
611
      {
612
      android.util.Log.e("object", "invalid rotation row: "+row);
613
      return;
614
      }
615

    
616
    mRotAxis     = axis;
617
    mRotRowBitmap= (1<<row);
618
    mRotationAngleStatic.set0(0.0f);
619
    mRotationAxis.set( ROTATION_AXIS[axis] );
620
    mRotationAngle.add(mRotationAngleStatic);
621
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
622
    }
623

    
624
///////////////////////////////////////////////////////////////////////////////////////////////////
625

    
626
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
627
    {
628
    if( wasRotateApplied() )
629
      {
630
      mRotAxis     = axis;
631
      mRotRowBitmap= rowBitmap;
632

    
633
      mRotationAngleStatic.set0(0.0f);
634
      mRotationAxis.set( ROTATION_AXIS[axis] );
635
      mRotationAngle.setDuration(durationMillis);
636
      mRotationAngle.resetToBeginning();
637
      mRotationAngle.add(new Static1D(0));
638
      mRotationAngle.add(new Static1D(angle));
639
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
640
      mRotateEffect.notifyWhenFinished(listener);
641

    
642
      return mRotateEffect.getID();
643
      }
644

    
645
    return 0;
646
    }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649

    
650
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
651
    {
652
    if( wasRotateApplied() )
653
      {
654
      float angle = getAngle();
655
      mRotationAngleStatic.set0(angle);
656
      mRotationAngleFinal.set0(nearestAngleInDegrees);
657
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
658

    
659
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
660
      mRotationAngle.resetToBeginning();
661
      mRotationAngle.removeAll();
662
      mRotationAngle.add(mRotationAngleStatic);
663
      mRotationAngle.add(mRotationAngleMiddle);
664
      mRotationAngle.add(mRotationAngleFinal);
665
      mRotateEffect.notifyWhenFinished(listener);
666

    
667
      return mRotateEffect.getID();
668
      }
669

    
670
    return 0;
671
    }
672

    
673
///////////////////////////////////////////////////////////////////////////////////////////////////
674

    
675
  private float getAngle()
676
    {
677
    int pointNum = mRotationAngle.getNumPoints();
678

    
679
    if( pointNum>=1 )
680
      {
681
      return mRotationAngle.getPoint(pointNum-1).get0();
682
      }
683
    else
684
      {
685
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
686
      crashlytics.log("points in RotationAngle: "+pointNum);
687
      return 0;
688
      }
689
    }
690

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692

    
693
  public synchronized void removeRotationNow()
694
    {
695
    float angle = getAngle();
696
    double nearestAngleInRadians = angle*Math.PI/180;
697
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
698
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
699
    float axisX = ROTATION_AXIS[mRotAxis].get0();
700
    float axisY = ROTATION_AXIS[mRotAxis].get1();
701
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
702
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
703

    
704
    mRotationAngle.removeAll();
705
    mRotationAngleStatic.set0(0);
706

    
707
    for(int i=0; i<NUM_CUBITS; i++)
708
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
709
        {
710
        int index = CUBITS[i].removeRotationNow(quat);
711
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
712
        }
713
    }
714

    
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716

    
717
  public void initializeObject(int[][] moves)
718
    {
719
    solve();
720
    setupPosition(moves);
721
    }
722

    
723
///////////////////////////////////////////////////////////////////////////////////////////////////
724

    
725
  public int getCubit(float[] point3D)
726
    {
727
    float dist, minDist = Float.MAX_VALUE;
728
    int currentBest=-1;
729
    float multiplier = returnMultiplier();
730

    
731
    point3D[0] *= multiplier;
732
    point3D[1] *= multiplier;
733
    point3D[2] *= multiplier;
734

    
735
    for(int i=0; i<NUM_CUBITS; i++)
736
      {
737
      dist = CUBITS[i].getDistSquared(point3D);
738
      if( dist<minDist )
739
        {
740
        minDist = dist;
741
        currentBest = i;
742
        }
743
      }
744

    
745
    return currentBest;
746
    }
747

    
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749

    
750
  public int computeNearestAngle(float angle, float speed)
751
    {
752
    final int NEAREST = 360/getBasicAngle();
753

    
754
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
755
    if( angle< -(NEAREST*0.5) ) tmp-=1;
756

    
757
    if( tmp!=0 ) return NEAREST*tmp;
758

    
759
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
760
    }
761

    
762
///////////////////////////////////////////////////////////////////////////////////////////////////
763

    
764
  public int getNodeSize()
765
    {
766
    return mNodeSize;
767
    }
768

    
769
///////////////////////////////////////////////////////////////////////////////////////////////////
770

    
771
  public ObjectList getObjectList()
772
    {
773
    return mList;
774
    }
775

    
776
///////////////////////////////////////////////////////////////////////////////////////////////////
777

    
778
  abstract float getScreenRatio();
779
  abstract Static3D[] getCubitPositions(int numLayers);
780
  abstract Static4D[] getQuats();
781
  abstract int getNumFaces();
782
  abstract int getNumStickerTypes(int numLayers);
783
  abstract int getNumCubitFaces();
784
  abstract MeshBase createCubitMesh(int cubit, int numLayers);
785
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top);
786
  abstract int getFaceColor(int cubit, int cubitface, int numLayers);
787
  abstract float returnMultiplier();
788
  abstract float[] getRowChances(int numLayers);
789
  abstract float[] getCuts(int numLayers);
790
  abstract boolean shouldResetTextureMaps();
791

    
792
  public abstract boolean isSolved();
793
  public abstract Static3D[] getRotationAxis();
794
  public abstract int getBasicAngle();
795
  public abstract String retObjectString();
796
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
797
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
798
  public abstract int getObjectName(int numLayers);
799
  public abstract int getInventor(int numLayers);
800
  public abstract int getComplexity(int numLayers);
801
  }
(26-26/30)