Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 36b9ee93

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
import org.distorted.main.RubikDebug;
51

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

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

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

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

    
83
  private static final boolean mCreateFromDMesh = true;
84

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

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

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

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

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

    
132
    mNodeSize = screenWidth;
133

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

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

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

    
152
    mQuatDebug = new int[NUM_CUBITS];
153

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

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

    
160
    mRowChances = getRowChances(mNumLayers);
161

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

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

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

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

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

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

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

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

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

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

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

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

    
207
    setupPosition(moves);
208

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

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

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

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

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

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

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

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

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

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

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

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

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

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

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

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

    
291
    return NUM_CUTS;
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

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

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

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

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

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

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

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

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

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

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
406
    int tex = 0;
407

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

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

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

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

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

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

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

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

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

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

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

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

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

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

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

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

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

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

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

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

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

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

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

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

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

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

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

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

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547

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

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

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

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

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

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

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582

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

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

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

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

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

    
618
///////////////////////////////////////////////////////////////////////////////////////////////////
619

    
620
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
621
    {
622
    RubikDebug.addDebug("adding new rot: axis="+axis+" rowBmp="+rowBitmap+" angle="+angle+" dur="+durationMillis+" object="+getObjectList().name());
623

    
624
    mRotAxis     = axis;
625
    mRotRowBitmap= rowBitmap;
626

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

    
636
    return mRotateEffect.getID();
637
    }
638

    
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640

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

    
648
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
649
    mRotationAngle.resetToBeginning();
650
    mRotationAngle.removeAll();
651
    mRotationAngle.add(mRotationAngleStatic);
652
    mRotationAngle.add(mRotationAngleMiddle);
653
    mRotationAngle.add(mRotationAngleFinal);
654
    mRotateEffect.notifyWhenFinished(listener);
655

    
656
    return mRotateEffect.getID();
657
    }
658

    
659

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

    
662
  private float getAngle()
663
    {
664
    int pointNum = mRotationAngle.getNumPoints();
665

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

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679

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

    
691
    mRotationAngle.removeAll();
692
    mRotationAngleStatic.set0(0);
693

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

    
702
///////////////////////////////////////////////////////////////////////////////////////////////////
703

    
704
  public void initializeObject(int[][] moves)
705
    {
706
    solve();
707
    setupPosition(moves);
708
    }
709

    
710
///////////////////////////////////////////////////////////////////////////////////////////////////
711

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

    
718
    point3D[0] *= multiplier;
719
    point3D[1] *= multiplier;
720
    point3D[2] *= multiplier;
721

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

    
732
    return currentBest;
733
    }
734

    
735
///////////////////////////////////////////////////////////////////////////////////////////////////
736

    
737
  public int computeNearestAngle(float angle, float speed)
738
    {
739
    final int NEAREST = 360/getBasicAngle();
740

    
741
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
742
    if( angle< -(NEAREST*0.5) ) tmp-=1;
743

    
744
    if( tmp!=0 ) return NEAREST*tmp;
745

    
746
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
747
    }
748

    
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750

    
751
  public int getNodeSize()
752
    {
753
    return mNodeSize;
754
    }
755

    
756
///////////////////////////////////////////////////////////////////////////////////////////////////
757

    
758
  public ObjectList getObjectList()
759
    {
760
    return mList;
761
    }
762

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

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

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