Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyObject.java @ e06e1b7e

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   = 0xffff90ff;
65
  static final int COLOR_VIOLET = 0xff7700bb;
66

    
67
  private static final float NODE_RATIO = 1.42f;
68
  private static final float MAX_SIZE_CHANGE = 1.4f;
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 = 0xff171717;
75
  private static final int POST_ROTATION_MILLISEC = 500;
76
  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

    
344
  private float computeAngle(float dx, float dy)
345
    {
346
    float PI = (float)Math.PI;
347
    double angle = Math.atan2(dy,dx);
348
    float ret = (float)(3*PI/2-angle);
349

    
350
    if( ret>2*PI ) ret-= 2*PI;
351

    
352
    return ret;
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  private void drawCurrVertex(Canvas canvas, Paint paint, float left, float r, float stroke, float pX,float pY, float cX, float cY, float nX, float nY)
358
    {
359
    pX = (0.5f+pX)*TEXTURE_HEIGHT;
360
    pY = (0.5f-pY)*TEXTURE_HEIGHT;
361
    cX = (0.5f+cX)*TEXTURE_HEIGHT;
362
    cY = (0.5f-cY)*TEXTURE_HEIGHT;
363
    nX = (0.5f+nX)*TEXTURE_HEIGHT;
364
    nY = (0.5f-nY)*TEXTURE_HEIGHT;
365

    
366
    canvas.drawLine(left+pX,pY,left+cX,cY,paint);
367

    
368
    float aX = pX-cX;
369
    float aY = pY-cY;
370
    float bX = cX-nX;
371
    float bY = cY-nY;
372

    
373
    float aLen = (float)Math.sqrt(aX*aX+aY*aY);
374
    float bLen = (float)Math.sqrt(bX*bX+bY*bY);
375

    
376
    aX /= aLen;
377
    aY /= aLen;
378
    bX /= bLen;
379
    bY /= bLen;
380

    
381
    float sX = (aX-bX)/2;
382
    float sY = (aY-bY)/2;
383
    float sLen = (float)Math.sqrt(sX*sX+sY*sY);
384
    sX /= sLen;
385
    sY /= sLen;
386

    
387
    float startAngle = computeAngle(bX,-bY);
388
    float endAngle   = computeAngle(aX,-aY);
389
    float sweepAngle = endAngle-startAngle;
390
    if( sweepAngle<0 ) sweepAngle += 2*Math.PI;
391

    
392
    float R = r*TEXTURE_HEIGHT+stroke/2;
393

    
394
    float A = (float)(R/(Math.cos(sweepAngle/2)));
395

    
396
    float rX = cX + A*sX;
397
    float rY = cY + A*sY;
398

    
399
    startAngle *= 180/(Math.PI);
400
    sweepAngle *= 180/(Math.PI);
401

    
402
    canvas.drawArc( left+rX-R, rY-R, left+rX+R, rY+R, startAngle, sweepAngle, false, paint);
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  void drawRoundedPolygon(Canvas canvas, Paint paint, int left, float[] vertices, float stroke, int color, float r)
408
    {
409
    stroke *= TEXTURE_HEIGHT;
410

    
411
    paint.setAntiAlias(true);
412
    paint.setStrokeWidth(stroke);
413
    paint.setColor(color);
414
    paint.setStyle(Paint.Style.FILL);
415

    
416
    canvas.drawRect(left,0,left+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
417

    
418
    paint.setColor(INTERIOR_COLOR);
419
    paint.setStyle(Paint.Style.STROKE);
420

    
421
    int length = vertices.length;
422
    int numVertices = length/2;
423

    
424
    float prevX = vertices[length-2];
425
    float prevY = vertices[length-1];
426
    float currX = vertices[0];
427
    float currY = vertices[1];
428
    float nextX = vertices[2];
429
    float nextY = vertices[3];
430

    
431
    for(int vert=0; vert<numVertices; vert++)
432
      {
433
      drawCurrVertex(canvas, paint, left, r, stroke, prevX,prevY,currX,currY,nextX,nextY);
434

    
435
      prevX = currX;
436
      prevY = currY;
437
      currX = nextX;
438
      currY = nextY;
439

    
440
      if( 2*(vert+2)+1 < length )
441
        {
442
        nextX = vertices[2*(vert+2)  ];
443
        nextY = vertices[2*(vert+2)+1];
444
        }
445
      else
446
        {
447
        nextX = vertices[0];
448
        nextY = vertices[1];
449
        }
450
      }
451
    }
452

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

    
455
  int getCubitFaceColorIndex(int cubit, int face)
456
    {
457
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
458
    return (int)(texMap.get0() / texMap.get2());
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
463

    
464
  void clampPos(Static3D pos)
465
    {
466
    float currError, minError = Float.MAX_VALUE;
467
    int minErrorIndex= -1;
468
    float x = pos.get0();
469
    float y = pos.get1();
470
    float z = pos.get2();
471
    float xo,yo,zo;
472

    
473
    for(int i=0; i<NUM_CUBITS; i++)
474
      {
475
      xo = mOrigPos[i].get0();
476
      yo = mOrigPos[i].get1();
477
      zo = mOrigPos[i].get2();
478

    
479
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
480

    
481
      if( currError<minError )
482
        {
483
        minError = currError;
484
        minErrorIndex = i;
485
        }
486
      }
487

    
488
    pos.set( mOrigPos[minErrorIndex] );
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492
// the getFaceColors + final black in a horizontal strip.
493

    
494
  public void createTexture()
495
    {
496
    Bitmap bitmap;
497

    
498
    Paint paint = new Paint();
499
    bitmap = Bitmap.createBitmap( (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
500
    Canvas canvas = new Canvas(bitmap);
501

    
502
    paint.setAntiAlias(true);
503
    paint.setTextAlign(Paint.Align.CENTER);
504
    paint.setStyle(Paint.Style.FILL);
505

    
506
    paint.setColor(INTERIOR_COLOR);
507
    canvas.drawRect(0, 0, (NUM_TEXTURES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
508

    
509
    for(int i=0; i<NUM_TEXTURES; i++)
510
      {
511
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT);
512
      }
513

    
514
    mTexture.setTexture(bitmap);
515
    }
516

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

    
519
  public int getSize()
520
    {
521
    return mSize;
522
    }
523

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

    
526
  public void continueRotation(float angleInDegrees)
527
    {
528
    mRotationAngleStatic.set0(angleInDegrees);
529
    }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
  public Static4D getRotationQuat()
534
      {
535
      return mQuat;
536
      }
537

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

    
540
  public void recomputeScaleFactor(int scrWidth)
541
    {
542
    mNodeScale.set(scrWidth,NODE_RATIO*scrWidth,scrWidth);
543
    }
544

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

    
547
  public void savePreferences(SharedPreferences.Editor editor)
548
    {
549
    for(int i=0; i<NUM_CUBITS; i++) CUBITS[i].savePreferences(editor);
550
    }
551

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

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

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

    
565
  public void releaseResources()
566
    {
567
    mTexture.markForDeletion();
568
    }
569

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

    
572
  public void apply(Effect effect, int position)
573
    {
574
    mEffects.apply(effect, position);
575
    }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
  public void remove(long effectID)
580
    {
581
    mEffects.abortById(effectID);
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585

    
586
  public synchronized void solve()
587
    {
588
    for(int i=0; i<NUM_CUBITS; i++)
589
      {
590
      CUBITS[i].solve();
591
      mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(), 0);
592
      }
593
    }
594

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

    
597
  public void resetAllTextureMaps()
598
    {
599
    final float ratio = 1.0f/(NUM_TEXTURES+1);
600
    int color;
601

    
602
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
603
      {
604
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
605

    
606
      for(int cubitface=0; cubitface<NUM_CUBIT_FACES; cubitface++)
607
        {
608
        color = getFaceColor(cubit,cubitface,mSize);
609
        maps[cubitface] = new Static4D( color*ratio, 0.0f, ratio, 1.0f);
610
        }
611

    
612
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
613
      }
614
    }
615

    
616
///////////////////////////////////////////////////////////////////////////////////////////////////
617

    
618
  public void setTextureMap(int cubit, int face, int newColor)
619
    {
620
    final float ratio = 1.0f/(NUM_TEXTURES+1);
621
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
622

    
623
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
624
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
625
    }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628

    
629
  public synchronized void beginNewRotation(int axis, int row )
630
    {
631
    if( axis<0 || axis>=ROTATION_AXIS.length )
632
      {
633
      android.util.Log.e("object", "invalid rotation axis: "+axis);
634
      return;
635
      }
636
    if( row<0 || row>=mSize )
637
      {
638
      android.util.Log.e("object", "invalid rotation row: "+row);
639
      return;
640
      }
641

    
642
    mRotAxis     = axis;
643
    mRotRowBitmap= (1<<row);
644
    mRotationAngleStatic.set0(0.0f);
645
    mRotationAxis.set( ROTATION_AXIS[axis] );
646
    mRotationAngle.add(mRotationAngleStatic);
647
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
648
    }
649

    
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651

    
652
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
653
    {
654
    mRotAxis     = axis;
655
    mRotRowBitmap= rowBitmap;
656

    
657
    mRotationAngleStatic.set0(0.0f);
658
    mRotationAxis.set( ROTATION_AXIS[axis] );
659
    mRotationAngle.setDuration(durationMillis);
660
    mRotationAngle.resetToBeginning();
661
    mRotationAngle.add(new Static1D(0));
662
    mRotationAngle.add(new Static1D(angle));
663
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis* ObjectList.MAX_OBJECT_SIZE) , -1);
664
    mRotateEffect.notifyWhenFinished(listener);
665

    
666
    return mRotateEffect.getID();
667
    }
668

    
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670

    
671
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
672
    {
673
    float angle = getAngle();
674
    mRotationAngleStatic.set0(angle);
675
    mRotationAngleFinal.set0(nearestAngleInDegrees);
676
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
677

    
678
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
679
    mRotationAngle.resetToBeginning();
680
    mRotationAngle.removeAll();
681
    mRotationAngle.add(mRotationAngleStatic);
682
    mRotationAngle.add(mRotationAngleMiddle);
683
    mRotationAngle.add(mRotationAngleFinal);
684
    mRotateEffect.notifyWhenFinished(listener);
685

    
686
    return mRotateEffect.getID();
687
    }
688

    
689

    
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691

    
692
  private float getAngle()
693
    {
694
    int pointNum = mRotationAngle.getNumPoints();
695

    
696
    if( pointNum>=1 )
697
      {
698
      return mRotationAngle.getPoint(pointNum-1).get0();
699
      }
700
    else
701
      {
702
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
703
      crashlytics.log("points in RotationAngle: "+pointNum);
704
      return 0;
705
      }
706
    }
707

    
708
///////////////////////////////////////////////////////////////////////////////////////////////////
709

    
710
  public synchronized void removeRotationNow()
711
    {
712
    float angle = getAngle();
713
    double nearestAngleInRadians = angle*Math.PI/180;
714
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
715
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
716
    float axisX = ROTATION_AXIS[mRotAxis].get0();
717
    float axisY = ROTATION_AXIS[mRotAxis].get1();
718
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
719
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
720

    
721
    mRotationAngle.removeAll();
722
    mRotationAngleStatic.set0(0);
723

    
724
    for(int i=0; i<NUM_CUBITS; i++)
725
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
726
        {
727
        int index = CUBITS[i].removeRotationNow(quat);
728
        mMesh.setEffectAssociation(i, CUBITS[i].computeAssociation(),index);
729
        }
730
    }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733

    
734
  public void initializeObject(int[][] moves)
735
    {
736
    solve();
737
    setupPosition(moves);
738
    }
739

    
740
///////////////////////////////////////////////////////////////////////////////////////////////////
741

    
742
  public int getCubit(float[] point3D)
743
    {
744
    float dist, minDist = Float.MAX_VALUE;
745
    int currentBest=-1;
746
    float multiplier = returnMultiplier();
747

    
748
    point3D[0] *= multiplier;
749
    point3D[1] *= multiplier;
750
    point3D[2] *= multiplier;
751

    
752
    for(int i=0; i<NUM_CUBITS; i++)
753
      {
754
      dist = CUBITS[i].getDistSquared(point3D);
755
      if( dist<minDist )
756
        {
757
        minDist = dist;
758
        currentBest = i;
759
        }
760
      }
761

    
762
    return currentBest;
763
    }
764

    
765
///////////////////////////////////////////////////////////////////////////////////////////////////
766

    
767
  public int computeNearestAngle(float angle, float speed)
768
    {
769
    final int NEAREST = 360/getBasicAngle();
770

    
771
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
772
    if( angle< -(NEAREST*0.5) ) tmp-=1;
773

    
774
    if( tmp!=0 ) return NEAREST*tmp;
775

    
776
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
777
    }
778

    
779
///////////////////////////////////////////////////////////////////////////////////////////////////
780

    
781
  public int getNodeSize()
782
    {
783
    return mNodeSize;
784
    }
785

    
786
///////////////////////////////////////////////////////////////////////////////////////////////////
787

    
788
  public ObjectList getObjectList()
789
    {
790
    return mList;
791
    }
792

    
793
///////////////////////////////////////////////////////////////////////////////////////////////////
794

    
795
  abstract float getScreenRatio();
796
  abstract Static3D[] getCubitPositions(int size);
797
  abstract Static4D[] getQuats();
798
  abstract int getNumFaces();
799
  abstract int getNumStickerTypes();
800
  abstract int getNumCubitFaces();
801
  abstract MeshBase createCubitMesh(int cubit);
802
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left);
803
  abstract int getFaceColor(int cubit, int cubitface, int size);
804
  abstract float returnMultiplier();
805
  abstract float[] getRowChances();
806
  abstract float getBasicStep();
807
  abstract boolean shouldResetTextureMaps();
808

    
809
  public abstract boolean isSolved();
810
  public abstract Static3D[] getRotationAxis();
811
  public abstract int getBasicAngle();
812
  public abstract String retObjectString();
813
  public abstract int randomizeNewRotAxis(Random rnd, int oldRotAxis);
814
  public abstract int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis);
815
  }
(17-17/19)