Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ eaee1ddc

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 RubikObject extends DistortedNode
57
  {
58
  private static final float NODE_RATIO = 1.32f;
59
  private static final float MAX_SIZE_CHANGE = 1.3f;
60
  private static final float MIN_SIZE_CHANGE = 0.8f;
61

    
62
  private static boolean mCreateFromDMesh = true;
63

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

    
69
  final Static3D[] ROTATION_AXIS;
70
  final Static4D[] QUATS;
71
  final Cubit[] CUBITS;
72
  final int NUM_FACES;
73
  final int NUM_TEXTURES;
74
  final int NUM_CUBIT_FACES;
75
  final int NUM_AXIS;
76
  final int NUM_CUBITS;
77
  final float BASIC_STEP;
78

    
79
  private static float mInitScreenRatio;
80
  private static float mObjectScreenRatio = 1.0f;
81

    
82
  private final int mNodeSize;
83
  private int mRotRowBitmap;
84
  private int mRotAxis;
85
  private Static3D[] mOrigPos;
86
  private Static3D mNodeScale;
87
  private Static4D mQuat;
88
  private int mSize;
89
  private RubikObjectList mList;
90
  private MeshBase mMesh;
91
  private DistortedEffects mEffects;
92
  private VertexEffectRotate mRotateEffect;
93
  private Dynamic1D mRotationAngle;
94
  private Static3D mRotationAxis;
95
  private Static3D mObjectScale;
96

    
97
  float mStart, mStep;
98
  float[] mRowChances;
99

    
100
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
101
  DistortedTexture mTexture;
102

    
103
  MatrixEffectScale mScaleEffect;
104
  MatrixEffectQuaternion mQuatEffect;
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  RubikObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshSquare nodeMesh,
109
              DistortedEffects nodeEffects, int[][] moves, RubikObjectList list, Resources res, int screenWidth)
110
    {
111
    super(nodeTexture,nodeEffects,nodeMesh);
112

    
113
    mNodeSize = screenWidth;
114

    
115
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
116

    
117
    mList = list;
118
    mOrigPos = getCubitPositions(size);
119

    
120
    QUATS = getQuats();
121
    NUM_CUBITS  = mOrigPos.length;
122
    ROTATION_AXIS = getRotationAxis();
123
    NUM_AXIS = ROTATION_AXIS.length;
124
    mInitScreenRatio = getScreenRatio();
125
    NUM_FACES = getNumFaces();
126
    NUM_CUBIT_FACES = getNumCubitFaces();
127
    NUM_TEXTURES = getNumStickerTypes()*NUM_FACES;
128
    BASIC_STEP = getBasicStep();
129

    
130
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
131
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
132

    
133
    mSize = size;
134
    computeStartAndStep(mOrigPos);
135
    mNodeScale= new Static3D(1,NODE_RATIO,1);
136
    mQuat = quat;
137

    
138
    mRowChances = getRowChances();
139

    
140
    mRotationAngle= new Dynamic1D();
141
    mRotationAxis = new Static3D(1,0,0);
142
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
143

    
144
    mRotationAngleStatic = new Static1D(0);
145
    mRotationAngleMiddle = new Static1D(0);
146
    mRotationAngleFinal  = new Static1D(0);
147

    
148
    float scale  = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
149
    mObjectScale = new Static3D(scale,scale,scale);
150
    mScaleEffect = new MatrixEffectScale(mObjectScale);
151
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
152

    
153
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
154
    nodeEffects.apply(nodeScaleEffect);
155

    
156
    CUBITS = new Cubit[NUM_CUBITS];
157
    createMeshAndCubits(list,res);
158

    
159
    mTexture = new DistortedTexture();
160
    mEffects = new DistortedEffects();
161

    
162
    int num_quats = QUATS.length;
163
    for(int q=0; q<num_quats; q++)
164
      {
165
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
166
      vq.setMeshAssociation(0,q);
167
      mEffects.apply(vq);
168
      }
169

    
170
    mEffects.apply(mRotateEffect);
171
    mEffects.apply(mQuatEffect);
172
    mEffects.apply(mScaleEffect);
173

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

    
178
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
179

    
180
    setupPosition(moves);
181

    
182
    setProjection(fov, 0.1f);
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  private void createMeshAndCubits(RubikObjectList list, Resources res)
188
    {
189
    if( mCreateFromDMesh )
190
      {
191
      int sizeIndex = RubikObjectList.getSizeIndex(list.ordinal(),mSize);
192
      int resourceID= list.getResourceIDs()[sizeIndex];
193

    
194
      InputStream is = res.openRawResource(resourceID);
195
      DataInputStream dos = new DataInputStream(is);
196
      mMesh = new MeshFile(dos);
197

    
198
      try
199
        {
200
        is.close();
201
        }
202
      catch(IOException e)
203
        {
204
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
205
        }
206

    
207
      for(int i=0; i<NUM_CUBITS; i++)
208
        {
209
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
210
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
211
        }
212

    
213
      if( shouldResetTextureMaps() ) resetAllTextureMaps();
214
      }
215
    else
216
      {
217
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
218

    
219
      for(int i=0; i<NUM_CUBITS; i++)
220
        {
221
        CUBITS[i] = new Cubit(this,mOrigPos[i]);
222
        cubitMesh[i] = createCubitMesh(i);
223
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
224
        cubitMesh[i].setEffectAssociation(0, CUBITS[i].computeAssociation(), 0);
225
        }
226

    
227
      mMesh = new MeshJoined(cubitMesh);
228
      resetAllTextureMaps();
229
      }
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public void setObjectRatio(float sizeChange)
235
    {
236
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
237

    
238
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
239
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
240

    
241
    float scale = mObjectScreenRatio*mInitScreenRatio*mNodeSize/mSize;
242
    mObjectScale.set(scale,scale,scale);
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  static float getObjectRatio()
248
    {
249
    return mObjectScreenRatio*mInitScreenRatio;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
254
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
255
// consecutive).
256
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
257
// the Cube and the Pyraminx.
258
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
259
// given Cubit belongs to.
260

    
261
  private void computeStartAndStep(Static3D[] pos)
262
    {
263
    float min = Float.MAX_VALUE;
264
    float max = Float.MIN_VALUE;
265
    float axisX = ROTATION_AXIS[0].get0();
266
    float axisY = ROTATION_AXIS[0].get1();
267
    float axisZ = ROTATION_AXIS[0].get2();
268
    float tmp;
269

    
270
    for(int i=0; i<NUM_CUBITS; i++)
271
      {
272
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
273
      if( tmp<min ) min=tmp;
274
      if( tmp>max ) max=tmp;
275
      }
276

    
277
    mStart = min;
278
    mStep  = (max-min+BASIC_STEP)/mSize;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

    
283
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
284
    {
285
    int cubitRow = (int)(CUBITS[cubit].mRotationRow[axis]+0.5f);
286
    return ((1<<cubitRow)&rowBitmap)!=0;
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 = CUBITS[j].removeRotationNow(quat);
327
            mMesh.setEffectAssociation(j, CUBITS[j].computeAssociation(),index);
328
            }
329
        }
330
      }
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
335
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
336
// away from the face of the Pyraminx shouldn't be textured.
337

    
338
  boolean isOnFace( int cubit, int axis, int row)
339
    {
340
    final float MAX_ERROR = 0.0001f;
341
    float diff = CUBITS[cubit].mRotationRow[axis] - row;
342
    return diff*diff < MAX_ERROR;
343
    }
344

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

    
347
  int getCubitFaceColorIndex(int cubit, int face)
348
    {
349
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
350
    return (int)(texMap.get0() / texMap.get2());
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
355

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

    
365
    for(int i=0; i<NUM_CUBITS; i++)
366
      {
367
      xo = mOrigPos[i].get0();
368
      yo = mOrigPos[i].get1();
369
      zo = mOrigPos[i].get2();
370

    
371
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
372

    
373
      if( currError<minError )
374
        {
375
        minError = currError;
376
        minErrorIndex = i;
377
        }
378
      }
379

    
380
    pos.set( mOrigPos[minErrorIndex] );
381
    }
382

    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384
// the getFaceColors + final black in a horizontal strip.
385

    
386
  public void createTexture()
387
    {
388
    Bitmap bitmap;
389

    
390
    Paint paint = new Paint();
391
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
392
    Canvas canvas = new Canvas(bitmap);
393

    
394
    paint.setAntiAlias(true);
395
    paint.setTextAlign(Paint.Align.CENTER);
396
    paint.setStyle(Paint.Style.FILL);
397

    
398
    paint.setColor(INTERIOR_COLOR);
399
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
400

    
401
    for(int i=0; i<NUM_TEXTURES; i++)
402
      {
403
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
404
      }
405

    
406
    mTexture.setTexture(bitmap);
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public int getSize()
412
    {
413
    return mSize;
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public void continueRotation(float angleInDegrees)
419
    {
420
    mRotationAngleStatic.set0(angleInDegrees);
421
    }
422

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

    
425
  public Static4D getRotationQuat()
426
      {
427
      return mQuat;
428
      }
429

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

    
432
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
433
    {
434
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
435
    }
436

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

    
439
  public void savePreferences(SharedPreferences.Editor editor)
440
    {
441
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
442
    }
443

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

    
446
  public synchronized void restorePreferences(SharedPreferences preferences)
447
    {
448
    for(int i=0; i<NUM_CUBITS; i++)
449
      {
450
      int index = CUBITS[i].restorePreferences(preferences);
451
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
452
      }
453
    }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456

    
457
  public void releaseResources()
458
    {
459
    mTexture.markForDeletion();
460
    }
461

    
462
///////////////////////////////////////////////////////////////////////////////////////////////////
463

    
464
  public void apply(Effect effect, int position)
465
    {
466
    mEffects.apply(effect, position);
467
    }
468

    
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470

    
471
  public void remove(long effectID)
472
    {
473
    mEffects.abortById(effectID);
474
    }
475

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

    
478
  public synchronized void solve()
479
    {
480
    for(int i=0; i<NUM_CUBITS; i++)
481
      {
482
      CUBITS[i].solve();
483
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
484
      }
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
  public void resetAllTextureMaps()
490
    {
491
    final float ratio = 1.0f/(NUM_TEXTURES+1);
492
    int color;
493

    
494
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
495
      {
496
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
497

    
498
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
499
        {
500
        color = getFaceColor(cubit,cubitface,mSize);
501
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
502
        }
503

    
504
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
505
      }
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

    
510
  public void setTextureMap(int cubit, int face, int newColor)
511
    {
512
    final float ratio = 1.0f/(NUM_TEXTURES+1);
513
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
514

    
515
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
516
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
517
    }
518

    
519
///////////////////////////////////////////////////////////////////////////////////////////////////
520

    
521
  public synchronized void beginNewRotation(int axis, int row )
522
    {
523
    if( axis<0 || axis>=ROTATION_AXIS.length )
524
      {
525
      android.util.Log.e("object", "invalid rotation axis: "+axis);
526
      return;
527
      }
528
    if( row<0 || row>=mSize )
529
      {
530
      android.util.Log.e("object", "invalid rotation row: "+row);
531
      return;
532
      }
533

    
534
    mRotAxis     = axis;
535
    mRotRowBitmap= (1<<row);
536
    mRotationAngleStatic.set0(0.0f);
537
    mRotationAxis.set( ROTATION_AXIS[axis] );
538
    mRotationAngle.add(mRotationAngleStatic);
539
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543

    
544
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
545
    {
546
    mRotAxis     = axis;
547
    mRotRowBitmap= rowBitmap;
548

    
549
    mRotationAngleStatic.set0(0.0f);
550
    mRotationAxis.set( ROTATION_AXIS[axis] );
551
    mRotationAngle.setDuration(durationMillis);
552
    mRotationAngle.resetToBeginning();
553
    mRotationAngle.add(new Static1D(0));
554
    mRotationAngle.add(new Static1D(angle));
555
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
556
    mRotateEffect.notifyWhenFinished(listener);
557

    
558
    return mRotateEffect.getID();
559
    }
560

    
561
///////////////////////////////////////////////////////////////////////////////////////////////////
562

    
563
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
564
    {
565
    float angle = getAngle();
566
    mRotationAngleStatic.set0(angle);
567
    mRotationAngleFinal.set0(nearestAngleInDegrees);
568
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
569

    
570
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
571
    mRotationAngle.resetToBeginning();
572
    mRotationAngle.removeAll();
573
    mRotationAngle.add(mRotationAngleStatic);
574
    mRotationAngle.add(mRotationAngleMiddle);
575
    mRotationAngle.add(mRotationAngleFinal);
576
    mRotateEffect.notifyWhenFinished(listener);
577

    
578
    return mRotateEffect.getID();
579
    }
580

    
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583

    
584
  private float getAngle()
585
    {
586
    int pointNum = mRotationAngle.getNumPoints();
587

    
588
    if( pointNum>=1 )
589
      {
590
      return mRotationAngle.getPoint(pointNum-1).get0();
591
      }
592
    else
593
      {
594
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
595
      crashlytics.log("points in RotationAngle: "+pointNum);
596
      return 0;
597
      }
598
    }
599

    
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601

    
602
  public synchronized void removeRotationNow()
603
    {
604
    float angle = getAngle();
605
    double nearestAngleInRadians = angle*Math.PI/180;
606
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
607
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
608
    float axisX = ROTATION_AXIS[mRotAxis].get0();
609
    float axisY = ROTATION_AXIS[mRotAxis].get1();
610
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
611
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
612

    
613
    mRotationAngle.removeAll();
614
    mRotationAngleStatic.set0(0);
615

    
616
    for(int i=0; i<NUM_CUBITS; i++)
617
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
618
        {
619
        int index = CUBITS[i].removeRotationNow(quat);
620
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
621
        }
622
    }
623

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

    
626
  public void initializeObject(int[][] moves)
627
    {
628
    solve();
629
    setupPosition(moves);
630
    }
631

    
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633

    
634
  public int getCubit(float[] point3D)
635
    {
636
    float dist, minDist = Float.MAX_VALUE;
637
    int currentBest=-1;
638
    float multiplier = returnMultiplier();
639

    
640
    point3D[0] *= multiplier;
641
    point3D[1] *= multiplier;
642
    point3D[2] *= multiplier;
643

    
644
    for(int i=0; i<NUM_CUBITS; i++)
645
      {
646
      dist = CUBITS[i].getDistSquared(point3D);
647
      if( dist<minDist )
648
        {
649
        minDist = dist;
650
        currentBest = i;
651
        }
652
      }
653

    
654
    return currentBest;
655
    }
656

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

    
659
  public int computeNearestAngle(float angle, float speed)
660
    {
661
    final int NEAREST = 360/getBasicAngle();
662

    
663
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
664
    if( angle< -(NEAREST*0.5) ) tmp-=1;
665

    
666
    if( tmp!=0 ) return NEAREST*tmp;
667

    
668
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
669
    }
670

    
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672

    
673
  public int getNodeSize()
674
    {
675
    return mNodeSize;
676
    }
677

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

    
680
  public RubikObjectList getObjectList()
681
    {
682
    return mList;
683
    }
684

    
685
///////////////////////////////////////////////////////////////////////////////////////////////////
686

    
687
  abstract float getScreenRatio();
688
  abstract Static3D[] getCubitPositions(int size);
689
  abstract Static4D[] getQuats();
690
  abstract int getNumFaces();
691
  abstract int getNumStickerTypes();
692
  abstract int getNumCubitFaces();
693
  abstract MeshBase createCubitMesh(int cubit);
694
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
695
  abstract int getFaceColor(int cubit, int cubitface, int size);
696
  abstract float returnMultiplier();
697
  abstract float[] getRowChances();
698
  abstract float getBasicStep();
699
  abstract boolean shouldResetTextureMaps();
700

    
701
  public abstract boolean isSolved();
702
  public abstract Static3D[] getRotationAxis();
703
  public abstract int getBasicAngle();
704
  public abstract int computeRowFromOffset(float offset);
705
  public abstract float returnRotationFactor(float offset);
706
  public abstract String retObjectString();
707
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
708
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
709
  }
(13-13/16)