Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 8f53e513

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.MeshRectangles;
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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
public abstract class RubikObject extends DistortedNode
56
  {
57
  private static final float NODE_RATIO = 1.32f;
58
  private static final float MAX_SIZE_CHANGE = 1.3f;
59
  private static final float MIN_SIZE_CHANGE = 0.8f;
60

    
61
  private static boolean mCreateFromDMesh = false;
62

    
63
  private static final Static3D CENTER = new Static3D(0,0,0);
64
  static final int INTERIOR_COLOR = 0xff000000;
65
  private static final int POST_ROTATION_MILLISEC = 500;
66
  private static final int TEXTURE_HEIGHT = 128;
67

    
68
  final Static3D[] ROTATION_AXIS;
69
  final Static4D[] QUATS;
70
  final int NUM_FACES;
71
  final int NUM_CUBIT_FACES;
72

    
73
  private static float mInitScreenRatio,mObjectScreenRatio;
74

    
75
  private final int NUM_CUBITS;
76
  private final int mNodeSize;
77
  private int mRotRowBitmap;
78
  private int mRotAxis;
79
  private Static3D[] mOrigPos;
80
  private Static3D mNodeScale;
81
  private Static4D mQuat;
82
  private Cubit[] mCubits;
83
  private int mSize;
84
  private RubikObjectList mList;
85
  private MeshBase mMesh;
86
  private DistortedEffects mEffects;
87
  private VertexEffectRotate mRotateEffect;
88
  private Dynamic1D mRotationAngle;
89
  private Static3D mRotationAxis;
90
  private Static3D mObjectScale;
91

    
92
  float mStart, mStep;
93

    
94
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
95
  DistortedTexture mTexture;
96

    
97
  MatrixEffectScale mScaleEffect;
98
  MatrixEffectQuaternion mQuatEffect;
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  RubikObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshRectangles nodeMesh,
103
              DistortedEffects nodeEffects, int[][] moves, RubikObjectList list, Resources res, int screenWidth)
104
    {
105
    super(nodeTexture,nodeEffects,nodeMesh);
106

    
107
    mNodeSize = screenWidth;
108

    
109
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
110

    
111
    mList = list;
112
    mOrigPos = getCubitPositions(size);
113

    
114
    QUATS = getQuats();
115
    NUM_CUBITS  = mOrigPos.length;
116
    ROTATION_AXIS = getRotationAxis();
117
    mObjectScreenRatio = getScreenRatio();
118
    mInitScreenRatio = mObjectScreenRatio;
119
    NUM_FACES = getNumFaces();
120
    NUM_CUBIT_FACES = getNumCubitFaces();
121

    
122
    mSize = size;
123
    computeStartAndStep(mOrigPos);
124
    mNodeScale= new Static3D(1,NODE_RATIO,1);
125
    mQuat = quat;
126

    
127
    mRotationAngle= new Dynamic1D();
128
    mRotationAxis = new Static3D(1,0,0);
129
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
130

    
131
    mRotationAngleStatic = new Static1D(0);
132
    mRotationAngleMiddle = new Static1D(0);
133
    mRotationAngleFinal  = new Static1D(0);
134

    
135
    float scale  = mObjectScreenRatio*mNodeSize/mSize;
136
    mObjectScale = new Static3D(scale,scale,scale);
137
    mScaleEffect = new MatrixEffectScale(mObjectScale);
138
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
139

    
140
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
141
    nodeEffects.apply(nodeScaleEffect);
142

    
143
    createMeshAndCubits(list,res);
144

    
145
    mTexture = new DistortedTexture();
146
    mEffects = new DistortedEffects();
147

    
148
    int num_quats = QUATS.length;
149
    for(int q=0; q<num_quats; q++)
150
      {
151
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
152
      vq.setMeshAssociation(0,q);
153
      mEffects.apply(vq);
154
      }
155

    
156
    mEffects.apply(mRotateEffect);
157
    mEffects.apply(mQuatEffect);
158
    mEffects.apply(mScaleEffect);
159

    
160
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
161

    
162
    setupPosition(moves);
163

    
164
    setProjection(fov, 0.1f);
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  private void createMeshAndCubits(RubikObjectList list, Resources res)
170
    {
171
    mCubits = new Cubit[NUM_CUBITS];
172

    
173
    if( mCreateFromDMesh )
174
      {
175
      int sizeIndex = RubikObjectList.getSizeIndex(list.ordinal(),mSize);
176
      int resourceID= list.getResourceIDs()[sizeIndex];
177

    
178
      InputStream is = res.openRawResource(resourceID);
179
      DataInputStream dos = new DataInputStream(is);
180
      mMesh = new MeshFile(dos);
181

    
182
      try
183
        {
184
        is.close();
185
        }
186
      catch(IOException e)
187
        {
188
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
189
        }
190

    
191
      for(int i=0; i<NUM_CUBITS; i++)
192
        {
193
        mCubits[i] = new Cubit(this,mOrigPos[i]);
194
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
195
        }
196
      }
197
    else
198
      {
199
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
200

    
201
      for(int i=0; i<NUM_CUBITS; i++)
202
        {
203
        mCubits[i] = new Cubit(this,mOrigPos[i]);
204
        cubitMesh[i] = createCubitMesh(i);
205
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
206
        cubitMesh[i].setEffectAssociation(0, mCubits[i].computeAssociation(), 0);
207
        }
208

    
209
      mMesh = new MeshJoined(cubitMesh);
210
      resetAllTextureMaps();
211
      }
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  public void setObjectRatio(float sizeChange)
217
    {
218
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
219

    
220
    if( mObjectScreenRatio > MAX_SIZE_CHANGE*mInitScreenRatio)
221
      {
222
      mObjectScreenRatio = MAX_SIZE_CHANGE*mInitScreenRatio;
223
      }
224
    if( mObjectScreenRatio < MIN_SIZE_CHANGE*mInitScreenRatio)
225
      {
226
      mObjectScreenRatio = MIN_SIZE_CHANGE*mInitScreenRatio;
227
      }
228

    
229
    float scale = mObjectScreenRatio*mNodeSize/mSize;
230
    mObjectScale.set(scale,scale,scale);
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  static float getObjectRatio()
236
    {
237
    return mObjectScreenRatio;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
242
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
243
// consecutive).
244
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
245
// the Cube and the Pyraminx.
246
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
247
// given Cubit belongs to.
248

    
249
  private void computeStartAndStep(Static3D[] pos)
250
    {
251
    float min = Float.MAX_VALUE;
252
    float max = Float.MIN_VALUE;
253
    float axisX = ROTATION_AXIS[0].get0();
254
    float axisY = ROTATION_AXIS[0].get1();
255
    float axisZ = ROTATION_AXIS[0].get2();
256
    float tmp;
257

    
258
    for(int i=0; i<NUM_CUBITS; i++)
259
      {
260
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
261
      if( tmp<min ) min=tmp;
262
      if( tmp>max ) max=tmp;
263
      }
264

    
265
    mStart = min;
266
    mStep  = (max-min+1.0f)/mSize;
267
    }
268

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

    
271
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
272
    {
273
    int cubitRow = (int)(mCubits[cubit].mRotationRow[axis]+0.5f);
274
    return ((1<<cubitRow)&rowBitmap)!=0;
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
279
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
280
// away from the face of the Pyraminx shouldn't be textured.
281

    
282
  private boolean isOnFace( int cubit, int axis, int row)
283
    {
284
    final float MAX_ERROR = 0.0001f;
285
    float diff = mCubits[cubit].mRotationRow[axis] - row;
286
    return diff*diff < MAX_ERROR;
287
    }
288

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

    
293
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
294
    {
295
    Static3D axis = ROTATION_AXIS[axisIndex];
296

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

    
303
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

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

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

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

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

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

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
343

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

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

    
359
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
360

    
361
      if( currError<minError )
362
        {
363
        minError = currError;
364
        minErrorIndex = i;
365
        }
366
      }
367

    
368
    pos.set( mOrigPos[minErrorIndex] );
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
// the getFaceColors + final black in a horizontal strip.
373

    
374
  public void createTexture()
375
    {
376
    Bitmap bitmap;
377

    
378
    Paint paint = new Paint();
379
    bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
380
    Canvas canvas = new Canvas(bitmap);
381

    
382
    paint.setAntiAlias(true);
383
    paint.setTextAlign(Paint.Align.CENTER);
384
    paint.setStyle(Paint.Style.FILL);
385

    
386
    paint.setColor(INTERIOR_COLOR);
387
    canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
388

    
389
    for(int i=0; i<NUM_FACES; i++)
390
      {
391
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
392
      }
393

    
394
    mTexture.setTexture(bitmap);
395
    }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
  public int getSize()
400
    {
401
    return mSize;
402
    }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
  public void continueRotation(float angleInDegrees)
407
    {
408
    mRotationAngleStatic.set0(angleInDegrees);
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
  public Static4D getRotationQuat()
414
      {
415
      return mQuat;
416
      }
417

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

    
420
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
421
    {
422
    float factor = Math.min(scrWidth,scrHeight);
423
    mNodeScale.set(factor,NODE_RATIO*factor,factor);
424
    }
425

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

    
428
  public void savePreferences(SharedPreferences.Editor editor)
429
    {
430
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
431
    }
432

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

    
435
  public synchronized void restorePreferences(SharedPreferences preferences)
436
    {
437
    for(int i=0; i<NUM_CUBITS; i++)
438
      {
439
      int index = mCubits[i].restorePreferences(preferences);
440
      mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
441
      }
442
    }
443

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

    
446
  public void releaseResources()
447
    {
448
    mTexture.markForDeletion();
449
    }
450

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

    
453
  public void apply(Effect effect, int position)
454
    {
455
    mEffects.apply(effect, position);
456
    }
457

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

    
460
  public void remove(long effectID)
461
    {
462
    mEffects.abortById(effectID);
463
    }
464

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

    
467
  public synchronized void solve()
468
    {
469
    for(int i=0; i<NUM_CUBITS; i++)
470
      {
471
      mCubits[i].solve();
472
      mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
473
      }
474
    }
475

    
476
///////////////////////////////////////////////////////////////////////////////////////////////////
477

    
478
  public boolean isSolved()
479
    {
480
    int index = mCubits[0].mQuatIndex;
481

    
482
    for(int i=1; i<NUM_CUBITS; i++)
483
      {
484
      if( !mCubits[i].thereIsNoVisibleDifference(index) ) return false;
485
      }
486

    
487
    return true;
488
    }
489

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

    
492
  public void resetAllTextureMaps()
493
    {
494
    boolean belongs;
495
    final float ratio = 1.0f/(NUM_FACES+1);
496

    
497
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
498
      {
499
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
500

    
501
      if( 2*ROTATION_AXIS.length == NUM_FACES )  // i.e. there are faces on both ends of the axis (cube)
502
        {
503
        for(int i=0; i<NUM_CUBIT_FACES; i++)
504
          {
505
          belongs = isOnFace(cubit, i/2, i%2==0 ? mSize-1:0 );
506
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
507
          }
508
        }
509
      else if( ROTATION_AXIS.length == NUM_FACES )  // just a single face on the right end of an axis (pyraminx)
510
        {
511
        for(int i=0; i<NUM_CUBIT_FACES; i++)
512
          {
513
          belongs = isOnFace(cubit, i, 0 );
514
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
515
          }
516
        }
517
      else if( 3*ROTATION_AXIS.length == 2*NUM_FACES ) // Dino  TODO
518
        {
519
        for(int i=0; i<NUM_CUBIT_FACES; i++)
520
          {
521
          belongs = true;//isOnFace(cubit, i, 0 );
522
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
523
          }
524
        }
525

    
526
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
527
      }
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
  public void setTextureMap(int cubit, int face, int newColor)
533
    {
534
    final float ratio = 1.0f/(NUM_FACES+1);
535
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
536

    
537
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
538
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
539
    }
540

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

    
543
  public synchronized void beginNewRotation(int axis, int row )
544
    {
545
    if( axis<0 || axis>=ROTATION_AXIS.length )
546
      {
547
      android.util.Log.e("object", "invalid rotation axis: "+axis);
548
      return;
549
      }
550
    if( row<0 || row>=mSize )
551
      {
552
      android.util.Log.e("object", "invalid rotation row: "+row);
553
      return;
554
      }
555

    
556
    mRotAxis     = axis;
557
    mRotRowBitmap= (1<<row);
558
    mRotationAngleStatic.set0(0.0f);
559
    mRotationAxis.set( ROTATION_AXIS[axis] );
560
    mRotationAngle.add(mRotationAngleStatic);
561
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
562
    }
563

    
564
///////////////////////////////////////////////////////////////////////////////////////////////////
565

    
566
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
567
    {
568
    mRotAxis     = axis;
569
    mRotRowBitmap= rowBitmap;
570

    
571
    mRotationAngleStatic.set0(0.0f);
572
    mRotationAxis.set( ROTATION_AXIS[axis] );
573
    mRotationAngle.setDuration(durationMillis);
574
    mRotationAngle.resetToBeginning();
575
    mRotationAngle.add(new Static1D(0));
576
    mRotationAngle.add(new Static1D(angle));
577
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
578
    mRotateEffect.notifyWhenFinished(listener);
579

    
580
    return mRotateEffect.getID();
581
    }
582

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

    
585
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
586
    {
587
    float angle = getAngle();
588
    mRotationAngleStatic.set0(angle);
589
    mRotationAngleFinal.set0(nearestAngleInDegrees);
590
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
591

    
592
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
593
    mRotationAngle.resetToBeginning();
594
    mRotationAngle.removeAll();
595
    mRotationAngle.add(mRotationAngleStatic);
596
    mRotationAngle.add(mRotationAngleMiddle);
597
    mRotationAngle.add(mRotationAngleFinal);
598
    mRotateEffect.notifyWhenFinished(listener);
599

    
600
    return mRotateEffect.getID();
601
    }
602

    
603

    
604
///////////////////////////////////////////////////////////////////////////////////////////////////
605

    
606
  private float getAngle()
607
    {
608
    int pointNum = mRotationAngle.getNumPoints();
609

    
610
    if( pointNum>=1 )
611
      {
612
      return mRotationAngle.getPoint(pointNum-1).get0();
613
      }
614
    else
615
      {
616
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
617
      crashlytics.log("points in RotationAngle: "+pointNum);
618
      return 0;
619
      }
620
    }
621

    
622
///////////////////////////////////////////////////////////////////////////////////////////////////
623

    
624
  public synchronized void removeRotationNow()
625
    {
626
    float angle = getAngle();
627
    double nearestAngleInRadians = angle*Math.PI/180;
628
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
629
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
630
    float axisX = ROTATION_AXIS[mRotAxis].get0();
631
    float axisY = ROTATION_AXIS[mRotAxis].get1();
632
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
633
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
634

    
635
    mRotationAngle.removeAll();
636
    mRotationAngleStatic.set0(0);
637

    
638
    for(int i=0; i<NUM_CUBITS; i++)
639
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
640
        {
641
        int index = mCubits[i].removeRotationNow(quat);
642
        mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
643
        }
644
    }
645

    
646
///////////////////////////////////////////////////////////////////////////////////////////////////
647

    
648
  public void initializeObject(int[][] moves)
649
    {
650
    solve();
651
    setupPosition(moves);
652
    }
653

    
654
///////////////////////////////////////////////////////////////////////////////////////////////////
655

    
656
  public int getCubit(float[] point3D)
657
    {
658
    float dist, minDist = Float.MAX_VALUE;
659
    int currentBest=-1;
660
    float multiplier = returnMultiplier();
661

    
662
    point3D[0] *= multiplier;
663
    point3D[1] *= multiplier;
664
    point3D[2] *= multiplier;
665

    
666
    for(int i=0; i<NUM_CUBITS; i++)
667
      {
668
      dist = mCubits[i].getDistSquared(point3D);
669
      if( dist<minDist )
670
        {
671
        minDist = dist;
672
        currentBest = i;
673
        }
674
      }
675

    
676
    return currentBest;
677
    }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680

    
681
  public int computeNearestAngle(float angle, float speed)
682
    {
683
    final int NEAREST = 360/getBasicAngle();
684

    
685
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
686
    if( angle< -(NEAREST*0.5) ) tmp-=1;
687

    
688
    if( tmp!=0 ) return NEAREST*tmp;
689

    
690
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
691
    }
692

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694

    
695
  public int getNodeSize()
696
    {
697
    return mNodeSize;
698
    }
699

    
700
///////////////////////////////////////////////////////////////////////////////////////////////////
701

    
702
  public RubikObjectList getObjectList()
703
    {
704
    return mList;
705
    }
706

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

    
709
  abstract float getScreenRatio();
710
  abstract Static3D[] getCubitPositions(int size);
711
  abstract Static4D[] getQuats();
712
  abstract int getNumFaces();
713
  abstract int getNumCubitFaces();
714
  abstract MeshBase createCubitMesh(int cubit);
715
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
716
  public abstract Static3D[] getRotationAxis();
717
  public abstract int getBasicAngle();
718
  public abstract float returnMultiplier();
719
  public abstract float returnRotationFactor(float offset);
720
  public abstract String retObjectString();
721
  public abstract float[] getRowChances();
722
  }
(6-6/10)