Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ 9c2f0c91

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.DistortedNode;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshFile;
41
import org.distorted.library.mesh.MeshJoined;
42
import org.distorted.library.mesh.MeshSquare;
43
import org.distorted.library.message.EffectListener;
44
import org.distorted.library.type.Dynamic1D;
45
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
67
  private static final float NODE_RATIO = 1.32f;
68
  private static final float MAX_SIZE_CHANGE = 1.3f;
69
  private static final float MIN_SIZE_CHANGE = 0.8f;
70

    
71
  private static boolean mCreateFromDMesh = true;
72

    
73
  private static final Static3D CENTER = new Static3D(0,0,0);
74
  static final int INTERIOR_COLOR = 0xff000000;
75
  private static final int POST_ROTATION_MILLISEC = 500;
76
  private static final int TEXTURE_HEIGHT = 256;
77

    
78
  final Static3D[] ROTATION_AXIS;
79
  final Static4D[] QUATS;
80
  final Cubit[] CUBITS;
81
  final int NUM_FACES;
82
  final int NUM_TEXTURES;
83
  final int NUM_CUBIT_FACES;
84
  final int NUM_AXIS;
85
  final int NUM_CUBITS;
86
  final float BASIC_STEP;
87

    
88
  private static float mInitScreenRatio;
89
  private static float mObjectScreenRatio = 1.0f;
90

    
91
  private final int mNodeSize;
92
  private int mRotRowBitmap;
93
  private int mRotAxis;
94
  private Static3D[] mOrigPos;
95
  private Static3D mNodeScale;
96
  private Static4D mQuat;
97
  private int mSize;
98
  private ObjectList mList;
99
  private MeshBase mMesh;
100
  private DistortedEffects mEffects;
101
  private VertexEffectRotate mRotateEffect;
102
  private Dynamic1D mRotationAngle;
103
  private Static3D mRotationAxis;
104
  private Static3D mObjectScale;
105

    
106
  float mStart, mStep;
107
  float[] mRowChances;
108

    
109
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
110
  DistortedTexture mTexture;
111

    
112
  MatrixEffectScale mScaleEffect;
113
  MatrixEffectQuaternion mQuatEffect;
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  TwistyObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
118
               DistortedEffects nodeEffects, int[][] moves, ObjectList list, Resources res, int screenWidth)
119
    {
120
    super(nodeTexture,nodeEffects,nodeMesh);
121

    
122
    mNodeSize = screenWidth;
123

    
124
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
125

    
126
    mList = list;
127
    mOrigPos = getCubitPositions(size);
128

    
129
    QUATS = getQuats();
130
    NUM_CUBITS  = mOrigPos.length;
131
    ROTATION_AXIS = getRotationAxis();
132
    NUM_AXIS = ROTATION_AXIS.length;
133
    mInitScreenRatio = getScreenRatio();
134
    NUM_FACES = getNumFaces();
135
    NUM_CUBIT_FACES = getNumCubitFaces();
136
    NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
137
    BASIC_STEP = getBasicStep();
138

    
139
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
140
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
141

    
142
    mSize = size;
143
    computeStartAndStep(mOrigPos);
144
    mNodeScale= new Static3D(1,NODE_RATIO,1);
145
    mQuat = quat;
146

    
147
    mRowChances = getRowChances();
148

    
149
    mRotationAngle= new Dynamic1D();
150
    mRotationAxis = new Static3D(1,0,0);
151
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
152

    
153
    mRotationAngleStatic = new Static1D(0);
154
    mRotationAngleMiddle = new Static1D(0);
155
    mRotationAngleFinal  = new Static1D(0);
156

    
157
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
158
    mObjectScale = new Static3D(scale,scale,scale);
159
    mScaleEffect = new MatrixEffectScale(mObjectScale);
160
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
161

    
162
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
163
    nodeEffects.apply(nodeScaleEffect);
164

    
165
    CUBITS = new Cubit[NUM_CUBITS];
166
    createMeshAndCubits(list,res);
167

    
168
    mTexture = new DistortedTexture();
169
    mEffects = new DistortedEffects();
170

    
171
    int num_quats = QUATS.length;
172
    for(int q=0; q<num_quats; q++)
173
      {
174
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
175
      vq.setMeshAssociation(0,q);
176
      mEffects.apply(vq);
177
      }
178

    
179
    mEffects.apply(mRotateEffect);
180
    mEffects.apply(mQuatEffect);
181
    mEffects.apply(mScaleEffect);
182

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

    
187
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
188

    
189
    setupPosition(moves);
190

    
191
    setProjection(fov, 0.1f);
192
    }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
  private void createMeshAndCubits(ObjectList list, Resources res)
197
    {
198
    if( mCreateFromDMesh )
199
      {
200
      int sizeIndex = ObjectList.getSizeIndex(list.ordinal(),mSize);
201
      int resourceID= list.getResourceIDs()[sizeIndex];
202

    
203
      InputStream is = res.openRawResource(resourceID);
204
      DataInputStream dos = new DataInputStream(is);
205
      mMesh = new MeshFile(dos);
206

    
207
      try
208
        {
209
        is.close();
210
        }
211
      catch(IOException e)
212
        {
213
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
214
        }
215

    
216
      for(int i=0; i<NUM_CUBITS; i++)
217
        {
218
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
219
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
220
        }
221

    
222
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
223
      }
224
    else
225
      {
226
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
227

    
228
      for(int i=0; i<NUM_CUBITS; i++)
229
        {
230
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
231
        cubitMesh[i] = createCubitMesh(i);
232
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
233
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
234
        }
235

    
236
      mMesh = new MeshJoined(cubitMesh);
237
      resetAllTextureMaps();
238
      }
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public void setObjectRatio(float sizeChange)
244
    {
245
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
246

    
247
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
248
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
249

    
250
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
251
    mObjectScale.set(scale,scale,scale);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  static float getObjectRatio()
257
    {
258
    return mObjectScreenRatio*mInitScreenRatio;
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
263
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
264
// consecutive).
265
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
266
// the Cube and the Pyraminx.
267
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
268
// given Cubit belongs to.
269

    
270
  private void computeStartAndStep(Static3D[] pos)
271
    {
272
    float min = Float.MAX_VALUE;
273
    float max = Float.MIN_VALUE;
274
    float axisX = ROTATION_AXIS[0].get0();
275
    float axisY = ROTATION_AXIS[0].get1();
276
    float axisZ = ROTATION_AXIS[0].get2();
277
    float tmp;
278

    
279
    for(int i=0; i<NUM_CUBITS; i++)
280
      {
281
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
282
      if( tmp<min ) min=tmp;
283
      if( tmp>max ) max=tmp;
284
      }
285

    
286
    mStart = min;
287
    mStep  = (max-min+BASIC_STEP)/mSize;
288
    }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

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

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

    
302
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
303
    {
304
    Static3D axis = ROTATION_AXIS[axisIndex];
305

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

    
312
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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

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

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

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
344
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
345
// away from the face of the Pyraminx shouldn't be textured.
346

    
347
  boolean isOnFace( int cubit, int axis, int row)
348
    {
349
    final float MAX_ERROR = 0.0001f;
350
    float diff = CUBITS[cubit].mRotationRow[axis] - row;
351
    return diff*diff < MAX_ERROR;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  int getCubitFaceColorIndex(int cubit, int face)
357
    {
358
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
359
    return (int)(texMap.get0() / texMap.get2());
360
    }
361

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

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

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

    
380
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
381

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

    
389
    pos.set( mOrigPos[minErrorIndex] );
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
// the getFaceColors + final black in a horizontal strip.
394

    
395
  public void createTexture()
396
    {
397
    Bitmap bitmap;
398

    
399
    Paint paint = new Paint();
400
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
401
    Canvas canvas = new Canvas(bitmap);
402

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

    
407
    paint.setColor(INTERIOR_COLOR);
408
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
409

    
410
    for(int i=0; i<NUM_TEXTURES; i++)
411
      {
412
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
413
      }
414

    
415
    mTexture.setTexture(bitmap);
416
    }
417

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  public int getSize()
421
    {
422
    return mSize;
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426

    
427
  public void continueRotation(float angleInDegrees)
428
    {
429
    mRotationAngleStatic.set0(angleInDegrees);
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433

    
434
  public Static4D getRotationQuat()
435
      {
436
      return mQuat;
437
      }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
442
    {
443
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  public void savePreferences(SharedPreferences.Editor editor)
449
    {
450
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  public synchronized void restorePreferences(SharedPreferences preferences)
456
    {
457
    for(int i=0; i<NUM_CUBITS; i++)
458
      {
459
      int index = CUBITS[i].restorePreferences(preferences);
460
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
461
      }
462
    }
463

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

    
466
  public void releaseResources()
467
    {
468
    mTexture.markForDeletion();
469
    }
470

    
471
///////////////////////////////////////////////////////////////////////////////////////////////////
472

    
473
  public void apply(Effect effect, int position)
474
    {
475
    mEffects.apply(effect, position);
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  public void remove(long effectID)
481
    {
482
    mEffects.abortById(effectID);
483
    }
484

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

    
487
  public synchronized void solve()
488
    {
489
    for(int i=0; i<NUM_CUBITS; i++)
490
      {
491
      CUBITS[i].solve();
492
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
493
      }
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public void resetAllTextureMaps()
499
    {
500
    final float ratio = 1.0f/(NUM_TEXTURES+1);
501
    int color;
502

    
503
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
504
      {
505
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
506

    
507
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
508
        {
509
        color = getFaceColor(cubit,cubitface,mSize);
510
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
511
        }
512

    
513
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
514
      }
515
    }
516

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

    
519
  public void setTextureMap(int cubit, int face, int newColor)
520
    {
521
    final float ratio = 1.0f/(NUM_TEXTURES+1);
522
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
523

    
524
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
525
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  public synchronized void beginNewRotation(int axis, int row )
531
    {
532
    if( axis<0 || axis>=ROTATION_AXIS.length )
533
      {
534
      android.util.Log.e("object", "invalid rotation axis: "+axis);
535
      return;
536
      }
537
    if( row<0 || row>=mSize )
538
      {
539
      android.util.Log.e("object", "invalid rotation row: "+row);
540
      return;
541
      }
542

    
543
    mRotAxis     = axis;
544
    mRotRowBitmap= (1<<row);
545
    mRotationAngleStatic.set0(0.0f);
546
    mRotationAxis.set( ROTATION_AXIS[axis] );
547
    mRotationAngle.add(mRotationAngleStatic);
548
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
549
    }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552

    
553
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
554
    {
555
    mRotAxis     = axis;
556
    mRotRowBitmap= rowBitmap;
557

    
558
    mRotationAngleStatic.set0(0.0f);
559
    mRotationAxis.set( ROTATION_AXIS[axis] );
560
    mRotationAngle.setDuration(durationMillis);
561
    mRotationAngle.resetToBeginning();
562
    mRotationAngle.add(new Static1D(0));
563
    mRotationAngle.add(new Static1D(angle));
564
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
565
    mRotateEffect.notifyWhenFinished(listener);
566

    
567
    return mRotateEffect.getID();
568
    }
569

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

    
572
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
573
    {
574
    float angle = getAngle();
575
    mRotationAngleStatic.set0(angle);
576
    mRotationAngleFinal.set0(nearestAngleInDegrees);
577
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
578

    
579
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
580
    mRotationAngle.resetToBeginning();
581
    mRotationAngle.removeAll();
582
    mRotationAngle.add(mRotationAngleStatic);
583
    mRotationAngle.add(mRotationAngleMiddle);
584
    mRotationAngle.add(mRotationAngleFinal);
585
    mRotateEffect.notifyWhenFinished(listener);
586

    
587
    return mRotateEffect.getID();
588
    }
589

    
590

    
591
///////////////////////////////////////////////////////////////////////////////////////////////////
592

    
593
  private float getAngle()
594
    {
595
    int pointNum = mRotationAngle.getNumPoints();
596

    
597
    if( pointNum>=1 )
598
      {
599
      return mRotationAngle.getPoint(pointNum-1).get0();
600
      }
601
    else
602
      {
603
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
604
      crashlytics.log("points in RotationAngle: "+pointNum);
605
      return 0;
606
      }
607
    }
608

    
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610

    
611
  public synchronized void removeRotationNow()
612
    {
613
    float angle = getAngle();
614
    double nearestAngleInRadians = angle*Math.PI/180;
615
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
616
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
617
    float axisX = ROTATION_AXIS[mRotAxis].get0();
618
    float axisY = ROTATION_AXIS[mRotAxis].get1();
619
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
620
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
621

    
622
    mRotationAngle.removeAll();
623
    mRotationAngleStatic.set0(0);
624

    
625
    for(int i=0; i<NUM_CUBITS; i++)
626
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
627
        {
628
        int index = CUBITS[i].removeRotationNow(quat);
629
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
630
        }
631
    }
632

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

    
635
  public void initializeObject(int[][] moves)
636
    {
637
    solve();
638
    setupPosition(moves);
639
    }
640

    
641
///////////////////////////////////////////////////////////////////////////////////////////////////
642

    
643
  public int getCubit(float[] point3D)
644
    {
645
    float dist, minDist = Float.MAX_VALUE;
646
    int currentBest=-1;
647
    float multiplier = returnMultiplier();
648

    
649
    point3D[0] *= multiplier;
650
    point3D[1] *= multiplier;
651
    point3D[2] *= multiplier;
652

    
653
    for(int i=0; i<NUM_CUBITS; i++)
654
      {
655
      dist = CUBITS[i].getDistSquared(point3D);
656
      if( dist<minDist )
657
        {
658
        minDist = dist;
659
        currentBest = i;
660
        }
661
      }
662

    
663
    return currentBest;
664
    }
665

    
666
///////////////////////////////////////////////////////////////////////////////////////////////////
667

    
668
  public int computeNearestAngle(float angle, float speed)
669
    {
670
    final int NEAREST = 360/getBasicAngle();
671

    
672
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
673
    if( angle< -(NEAREST*0.5) ) tmp-=1;
674

    
675
    if( tmp!=0 ) return NEAREST*tmp;
676

    
677
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
678
    }
679

    
680
///////////////////////////////////////////////////////////////////////////////////////////////////
681

    
682
  public int getNodeSize()
683
    {
684
    return mNodeSize;
685
    }
686

    
687
///////////////////////////////////////////////////////////////////////////////////////////////////
688

    
689
  public ObjectList getObjectList()
690
    {
691
    return mList;
692
    }
693

    
694
///////////////////////////////////////////////////////////////////////////////////////////////////
695

    
696
  abstract float getScreenRatio();
697
  abstract Static3D[] getCubitPositions(int size);
698
  abstract Static4D[] getQuats();
699
  abstract int getNumFaces();
700
  abstract int getNumStickerTypes();
701
  abstract int getNumCubitFaces();
702
  abstract MeshBase createCubitMesh(int cubit);
703
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
704
  abstract int getFaceColor(int cubit, int cubitface, int size);
705
  abstract float returnMultiplier();
706
  abstract float[] getRowChances();
707
  abstract float getBasicStep();
708
  abstract boolean shouldResetTextureMaps();
709

    
710
  public abstract boolean isSolved();
711
  public abstract Static3D[] getRotationAxis();
712
  public abstract int getBasicAngle();
713
  public abstract int computeRowFromOffset(float offset);
714
  public abstract float returnRotationFactor(float offset);
715
  public abstract String retObjectString();
716
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
717
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
718
  }
(16-16/18)