Project

General

Profile

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

magiccube / src / main / java / org / distorted / magic / RubikCube.java @ fdec60a3

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.magic;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.library.effect.Effect;
27
import org.distorted.library.effect.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectRotate;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectSink;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedNode;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshCubes;
36
import org.distorted.library.mesh.MeshFlat;
37
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.type.Dynamic1D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class RubikCube extends DistortedNode
46
{
47
    private static final int POST_ROTATION_MILLISEC = 500;
48
    private static final int TEXTURE_SIZE = 100;
49

    
50
    private static final Static3D VectX = new Static3D(1,0,0);
51
    private static final Static3D VectY = new Static3D(0,1,0);
52
    private static final Static3D VectZ = new Static3D(0,0,1);
53

    
54
    public static final int VECTX = 0;  //
55
    public static final int VECTY = 1;  // don't change this
56
    public static final int VECTZ = 2;  //
57

    
58
    private DistortedNode[][][] mNodes;
59
    private MeshCubes[][][] mCubes;
60
    private DistortedEffects[][][] mEffects;
61
    private Static4D[][][] mQuatScramble;
62
    private Static3D[][][] mRotationAxis;
63
    private Dynamic1D[][][] mRotationAngle;
64
    private Static3D[][][] mCurrentPosition;
65
    private MatrixEffectRotate[][][] mRotateEffect;
66
    private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
67
    private Static3D mMove, mScale, mNodeMove, mNodeScale;
68
    private Static4D mQuatAccumulated;
69
    private DistortedTexture mTexture;
70

    
71
    private int mRotAxis, mRotRow;
72
    private int mSize;
73

    
74
    private DistortedTexture mNodeTexture;
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
    RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshFlat mesh, DistortedEffects effects)
79
      {
80
      super(texture,effects,mesh);
81

    
82
      mNodeTexture = texture;
83

    
84
      mSize = size;
85

    
86
      mRotationAngleStatic = new Static1D(0);
87
      mRotationAngleMiddle = new Static1D(0);
88
      mRotationAngleFinal  = new Static1D(0);
89

    
90
      mMove     = new Static3D(0,0,0);
91
      mScale    = new Static3D(1,1,1);
92
      mNodeMove = new Static3D(0,0,0);
93
      mNodeScale= new Static3D(1,1,1);
94

    
95
      mQuatAccumulated = quatAcc;
96

    
97
      mRotAxis = VECTX;
98
      mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
99

    
100
      mNodes          = new DistortedNode[mSize][mSize][mSize];
101
      mCubes          = new MeshCubes[mSize][mSize][mSize];
102
      mEffects        = new DistortedEffects[mSize][mSize][mSize];
103
      mQuatScramble   = new Static4D[mSize][mSize][mSize];
104
      mRotationAxis   = new Static3D[mSize][mSize][mSize];
105
      mRotationAngle  = new Dynamic1D[mSize][mSize][mSize];
106
      mCurrentPosition= new Static3D[mSize][mSize][mSize];
107
      mRotateEffect   = new MatrixEffectRotate[mSize][mSize][mSize];
108

    
109
      Static3D[][][] cubeVectors = new Static3D[mSize][mSize][mSize];
110

    
111
      Static3D sinkCenter = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f);
112
      Static3D matrCenter = new Static3D(0,0,0);
113
      Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f);
114

    
115
      VertexEffectSink        sinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), sinkCenter, region );
116
      MatrixEffectMove        moveEffect = new MatrixEffectMove(mMove);
117
      MatrixEffectScale      scaleEffect = new MatrixEffectScale(mScale);
118
      MatrixEffectQuaternion quatCEffect = new MatrixEffectQuaternion(quatCur, matrCenter);
119
      MatrixEffectQuaternion quatAEffect = new MatrixEffectQuaternion(quatAcc, matrCenter);
120

    
121
      MatrixEffectMove       nodeMoveEffect  = new MatrixEffectMove(mNodeMove);
122
      MatrixEffectScale      nodeScaleEffect = new MatrixEffectScale(mNodeScale);
123

    
124
      effects.apply(nodeScaleEffect);
125
      effects.apply(nodeMoveEffect);
126

    
127
      // 3x2 bitmap = 6 squares:
128
      //
129
      // RED     GREEN   BLUE
130
      // YELLOW  WHITE   BROWN
131

    
132
      final float ze = 0.0f;
133
      final float ot = 1.0f/3.0f;
134
      final float tt = 2.0f/3.0f;
135
      final float oh = 1.0f/2.0f;
136
      final float of = 1.0f/40.0f;
137

    
138
      final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
139
      final Static4D mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
140
      final Static4D mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
141
      final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
142
      final Static4D mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
143
      final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
144

    
145
      final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
146

    
147
      Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
148
      float nc = 0.5f*mSize;
149
      int vertices = (int)(24.0f/mSize + 2.0f);
150

    
151
      for(int x = 0; x< mSize; x++)
152
        for(int y = 0; y< mSize; y++)
153
          for(int z = 0; z< mSize; z++)
154
            {
155
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) // only the external walls
156
              {
157
              tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
158
              tmpRight = (x== mSize-1 ? mapRight :mapBlack);
159
              tmpFront = (z== mSize-1 ? mapFront :mapBlack);
160
              tmpBack  = (z==       0 ? mapBack  :mapBlack);
161
              tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
162
              tmpBottom= (y==       0 ? mapBottom:mapBlack);
163

    
164
              mCubes[x][y][z]           = new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
165
              cubeVectors[x][y][z]      = new Static3D( TEXTURE_SIZE*(x-nc), TEXTURE_SIZE*(y-nc), TEXTURE_SIZE*(z-nc) );
166
              mQuatScramble[x][y][z]    = new Static4D(0,0,0,1);
167
              mRotationAngle[x][y][z]   = new Dynamic1D();
168
              mRotationAxis[x][y][z]    = new Static3D(1,0,0);
169
              mCurrentPosition[x][y][z] = new Static3D(x,y,z);
170
              mRotateEffect[x][y][z]    = new MatrixEffectRotate(mRotationAngle[x][y][z], mRotationAxis[x][y][z], matrCenter);
171

    
172
              mEffects[x][y][z] = new DistortedEffects();
173
              mEffects[x][y][z].apply(sinkEffect);
174
              mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
175
              mEffects[x][y][z].apply( new MatrixEffectQuaternion(mQuatScramble[x][y][z], matrCenter));
176
              mEffects[x][y][z].apply(mRotateEffect[x][y][z]);
177
              mEffects[x][y][z].apply(quatAEffect);
178
              mEffects[x][y][z].apply(quatCEffect);
179
              mEffects[x][y][z].apply(scaleEffect);
180
              mEffects[x][y][z].apply(moveEffect);
181

    
182
              mNodes[x][y][z] = new DistortedNode(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
183

    
184
              attach(mNodes[x][y][z]);
185
              }
186
            }
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    private int computeNearestAngle(float angle)
192
      {
193
      final int NEAREST = 90;
194

    
195
      int tmp = (int)((angle+NEAREST/2)/NEAREST);
196
      if( angle< -(NEAREST/2) ) tmp-=1;
197

    
198
      return NEAREST*tmp;
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// All legal rotation quats must have all four of their components equal to either
203
// 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
204
//
205
// Because of quatMultiplication, errors can accumulate - so to avoid this, we
206
// correct the value of the 'scramble' quat to what it should be.
207
//
208
// We also have to remember that the group of unit quaternions is a double-cover of rotations
209
// in 3D ( q represents the same rotation as -q ) - so invert if needed.
210

    
211
    private static final float SQ2 = 0.5f*((float)Math.sqrt(2));
212
    private static final float[] LEGAL = { 0.0f , 0.5f , -0.5f , 1.0f , -1.0f , SQ2 , -SQ2 };
213

    
214
    private void normalizeScrambleQuat(int i, int j, int k)
215
      {
216
      Static4D quat = mQuatScramble[i][j][k];
217

    
218
      float x = quat.get1();
219
      float y = quat.get2();
220
      float z = quat.get3();
221
      float w = quat.get4();
222
      float diff;
223

    
224
      for(float legal: LEGAL)
225
        {
226
        diff = x-legal;
227
        if( diff*diff<0.01f ) x = legal;
228
        diff = y-legal;
229
        if( diff*diff<0.01f ) y = legal;
230
        diff = z-legal;
231
        if( diff*diff<0.01f ) z = legal;
232
        diff = w-legal;
233
        if( diff*diff<0.01f ) w = legal;
234
        }
235

    
236
      if( w<0 )
237
        {
238
        w = -w;
239
        z = -z;
240
        y = -y;
241
        x = -x;
242
        }
243
      else if( w==0 )
244
        {
245
        if( z<0 )
246
          {
247
          z = -z;
248
          y = -y;
249
          x = -x;
250
          }
251
        else if( z==0 )
252
          {
253
          if( y<0 )
254
            {
255
            y = -y;
256
            x = -x;
257
            }
258
          else if( y==0 )
259
            {
260
            if( x<0 )
261
              {
262
              x = -x;
263
              }
264
            }
265
          }
266
        }
267

    
268
      mQuatScramble[i][j][k].set(x,y,z,w);
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    private float getSinkStrength()
274
      {
275
      switch(mSize)
276
        {
277
        case 1 : return 1.1f;
278
        case 2 : return 1.5f;
279
        case 3 : return 1.8f;
280
        case 4 : return 2.0f;
281
        default: return 3.0f - 4.0f/mSize;
282
        }
283
      }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
    private boolean belongsToRotation(int x, int y, int z, int vector, int row)
288
      {
289
      switch(vector)
290
        {
291
        case VECTX: return mCurrentPosition[x][y][z].get1()==row;
292
        case VECTY: return mCurrentPosition[x][y][z].get2()==row;
293
        case VECTZ: return mCurrentPosition[x][y][z].get3()==row;
294
        }
295

    
296
      return false;
297
      }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
    private void modifyCurrentPosition(int x, int y, int z, Static4D quat)
302
      {
303
      Static3D current = mCurrentPosition[x][y][z];
304
      float diff = 0.5f*(mSize-1);
305
      float cubitCenterX = current.get1() - diff;
306
      float cubitCenterY = current.get2() - diff;
307
      float cubitCenterZ = current.get3() - diff;
308

    
309
      Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
310
      Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
311

    
312
      float rotatedX = rotatedCenter.get1() + diff;
313
      float rotatedY = rotatedCenter.get2() + diff;
314
      float rotatedZ = rotatedCenter.get3() + diff;
315

    
316
      int roundedX = (int)(rotatedX+0.1f);
317
      int roundedY = (int)(rotatedY+0.1f);
318
      int roundedZ = (int)(rotatedZ+0.1f);
319

    
320
      mCurrentPosition[x][y][z].set1(roundedX);
321
      mCurrentPosition[x][y][z].set2(roundedY);
322
      mCurrentPosition[x][y][z].set3(roundedZ);
323
      }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
    boolean isSolved()
328
      {
329
      Static4D q = mQuatScramble[0][0][0];
330

    
331
      float x = q.get1();
332
      float y = q.get2();
333
      float z = q.get3();
334
      float w = q.get4();
335

    
336
      for(int i = 0; i< mSize; i++)
337
        for(int j = 0; j< mSize; j++)
338
          for(int k = 0; k< mSize; k++)
339
            {
340
            if( i==0 || i==mSize-1 || j==0 || j==mSize-1 || k==0 || k==mSize-1 )
341
              {
342
              q = mQuatScramble[i][j][k];
343

    
344
              if( q.get1()!=x || q.get2()!=y || q.get3()!=z || q.get4()!=w )
345
                {
346
                return false;
347
                }
348
              }
349
            }
350

    
351
      return true;
352
      }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
356
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
357
// memory leak.
358

    
359
    void releaseResources()
360
      {
361
      mTexture.markForDeletion();
362

    
363
      for(int x=0; x<mSize; x++)
364
        for(int y=0; y<mSize; y++)
365
          for(int z=0; z<mSize; z++)
366
            {
367
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
368
              {
369
              mCubes[x][y][z].markForDeletion();
370
              mNodes[x][y][z].markForDeletion();
371
              }
372
            }
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    void addNewRotation(int vector, int row )
378
      {
379
      Static3D axis = VectX;
380

    
381
      switch(vector)
382
        {
383
        case VECTX: axis = VectX; break;
384
        case VECTY: axis = VectY; break;
385
        case VECTZ: axis = VectZ; break;
386
        }
387

    
388
      mRotAxis = vector;
389
      mRotRow  = row;
390

    
391
      mRotationAngleStatic.set1(0.0f);
392

    
393
      for(int x=0; x<mSize; x++)
394
        for(int y=0; y<mSize; y++)
395
          for(int z=0; z<mSize; z++)
396
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
397
              {
398
              if( belongsToRotation(x,y,z,vector,mRotRow) )
399
                {
400
                mRotationAxis[x][y][z].set(axis);
401
                mRotationAngle[x][y][z].add(mRotationAngleStatic);
402
                }
403
              }
404
      }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
    long finishRotationNow(EffectListener listener)
409
      {
410
      boolean first = true;
411
      long effectID=0;
412

    
413
      for(int x=0; x<mSize; x++)
414
        for(int y=0; y<mSize; y++)
415
          for(int z=0; z<mSize; z++)
416
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
417
              {
418
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
419
                {
420
                if( first )
421
                  {
422
                  first = false;
423
                  mRotateEffect[x][y][z].notifyWhenFinished(listener);
424
                  effectID = mRotateEffect[x][y][z].getID();
425
                  int pointNum = mRotationAngle[x][y][z].getNumPoints();
426

    
427
                  if( pointNum>=1 )
428
                    {
429
                    float startingAngle = mRotationAngle[x][y][z].getPoint(pointNum-1).get1();
430
                    int nearestAngleInDegrees = computeNearestAngle(startingAngle);
431
                    mRotationAngleStatic.set1(startingAngle);
432
                    mRotationAngleFinal.set1(nearestAngleInDegrees);
433
                    mRotationAngleMiddle.set1( nearestAngleInDegrees + (nearestAngleInDegrees-startingAngle)*0.2f );
434
                    }
435
                  else
436
                    {
437
                    android.util.Log.e("cube", "ERROR finishing rotation!");
438
                    return 0;
439
                    }
440
                  }
441

    
442
                mRotationAngle[x][y][z].setDuration(POST_ROTATION_MILLISEC);
443
                mRotationAngle[x][y][z].resetToBeginning();
444
                mRotationAngle[x][y][z].removeAll();
445
                mRotationAngle[x][y][z].add(mRotationAngleStatic);
446
                mRotationAngle[x][y][z].add(mRotationAngleMiddle);
447
                mRotationAngle[x][y][z].add(mRotationAngleFinal);
448
                }
449
              }
450

    
451
      return effectID;
452
      }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
    void continueRotation(float angleInDegrees)
457
      {
458
      mRotationAngleStatic.set1(angleInDegrees);
459
      }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
    void createTexture()
464
      {
465
      Bitmap bitmap;
466

    
467
      final int S = 128;
468
      final int W = 3*S;
469
      final int H = 2*S;
470
      final int R = S/10;
471
      final int M = S/20;
472

    
473
      Paint paint = new Paint();
474
      bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
475
      Canvas canvas = new Canvas(bitmap);
476

    
477
      paint.setAntiAlias(true);
478
      paint.setTextAlign(Paint.Align.CENTER);
479
      paint.setStyle(Paint.Style.FILL);
480

    
481
      // 3x2 bitmap = 6 squares:
482
      //
483
      // RED     GREEN   BLUE
484
      // YELLOW  WHITE   BROWN
485

    
486
      paint.setColor(0xff000000);                                  // BLACK BACKGROUND
487
      canvas.drawRect(0, 0, W, H, paint);                          //
488

    
489
      paint.setColor(0xffff0000);                                  // RED
490
      canvas.drawRoundRect(    M,   M,   S-M,   S-M, R, R, paint); //
491
      paint.setColor(0xff00ff00);                                  // GREEN
492
      canvas.drawRoundRect(  S+M,   M, 2*S-M,   S-M, R, R, paint); //
493
      paint.setColor(0xff0000ff);                                  // BLUE
494
      canvas.drawRoundRect(2*S+M,   M, 3*S-M,   S-M, R, R, paint); //
495
      paint.setColor(0xffffff00);                                  // YELLOW
496
      canvas.drawRoundRect(    M, S+M,   S-M, 2*S-M, R, R, paint); //
497
      paint.setColor(0xffffffff);                                  // WHITE
498
      canvas.drawRoundRect(  S+M, S+M, 2*S-M, 2*S-M, R, R, paint); //
499
      paint.setColor(0xffb5651d);                                  // BROWN
500
      canvas.drawRoundRect(2*S+M, S+M, 3*S-M, 2*S-M, R, R, paint); //
501

    
502
      mTexture.setTexture(bitmap);
503
      }
504

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

    
507
    void recomputeScaleFactor(int screenWidth, int screenHeight, float size)
508
      {
509
      int texW = mNodeTexture.getWidth();
510
      int texH = mNodeTexture.getHeight();
511

    
512
      if( (float)texH/texW > (float)screenHeight/screenWidth )
513
        {
514
        int w = (screenHeight*texW)/texH;
515
        float factor = (float)screenHeight/texH;
516
        mNodeMove.set((screenWidth-w)*0.5f ,0, 0);
517
        mNodeScale.set(factor,factor,factor);
518
        }
519
      else
520
        {
521
        int h = (screenWidth*texH)/texW;
522
        float factor = (float)screenWidth/texW;
523
        mNodeMove.set(0,(screenHeight-h)*0.5f,0);
524
        mNodeScale.set(factor,factor,factor);
525
        }
526

    
527
      float scaleFactor = (size/(TEXTURE_SIZE*mSize)) * (float)texW/(screenWidth>screenHeight ? screenHeight:screenWidth);
528

    
529
      mMove.set( texW*0.5f , texH*0.5f , 0.0f );
530
      mScale.set(scaleFactor,scaleFactor,scaleFactor);
531
      }
532

    
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534
// PUBLIC API
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
    public Static4D getRotationQuat()
538
      {
539
      return mQuatAccumulated;
540
      }
541

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

    
544
    public void apply(Effect effect, int position)
545
      {
546
      for(int x=0; x<mSize; x++)
547
        for(int y=0; y<mSize; y++)
548
          for(int z=0; z<mSize; z++)
549
            {
550
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
551
              {
552
              mEffects[x][y][z].apply(effect, position);
553
              }
554
            }
555
      }
556

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

    
559
    public void remove(long effectID)
560
      {
561
      for(int x=0; x<mSize; x++)
562
        for(int y=0; y<mSize; y++)
563
          for(int z=0; z<mSize; z++)
564
            {
565
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
566
              {
567
              mEffects[x][y][z].abortById(effectID);
568
              }
569
            }
570
      }
571

    
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573

    
574
    public void solve()
575
      {
576
      for(int x=0; x<mSize; x++)
577
        for(int y=0; y<mSize; y++)
578
          for(int z=0; z<mSize; z++)
579
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
580
              {
581
              mQuatScramble[x][y][z].set(0,0,0,1);
582
              mCurrentPosition[x][y][z].set(x,y,z);
583
              }
584
      }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587

    
588
    public int getSize()
589
      {
590
      return mSize;
591
      }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
    public long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener )
596
      {
597
      Static3D axis = VectX;
598
      long effectID=0;
599
      boolean first = true;
600

    
601
      switch(vector)
602
        {
603
        case VECTX: axis = VectX; break;
604
        case VECTY: axis = VectY; break;
605
        case VECTZ: axis = VectZ; break;
606
        }
607

    
608
      mRotAxis = vector;
609
      mRotRow  = row;
610

    
611
      mRotationAngleStatic.set1(0.0f);
612

    
613
      for(int x=0; x<mSize; x++)
614
        for(int y=0; y<mSize; y++)
615
          for(int z=0; z<mSize; z++)
616
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
617
              {
618
              if( belongsToRotation(x,y,z,vector,mRotRow) )
619
                {
620
                mRotationAxis[x][y][z].set(axis);
621
                mRotationAngle[x][y][z].setDuration(durationMillis);
622
                mRotationAngle[x][y][z].resetToBeginning();
623
                mRotationAngle[x][y][z].add(new Static1D(0));
624
                mRotationAngle[x][y][z].add(new Static1D(angle));
625

    
626
                if( first )
627
                  {
628
                  first = false;
629
                  effectID = mRotateEffect[x][y][z].getID();
630
                  mRotateEffect[x][y][z].notifyWhenFinished(listener);
631
                  }
632
                }
633
              }
634

    
635
      return effectID;
636
      }
637

    
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639

    
640
    public void removeRotationNow()
641
      {
642
      float qx=0,qy=0,qz=0;
643
      boolean first = true;
644
      Static4D quat = null;
645

    
646
      switch(mRotAxis)
647
        {
648
        case VECTX: qx=1; break;
649
        case VECTY: qy=1; break;
650
        case VECTZ: qz=1; break;
651
        }
652

    
653
      for(int x=0; x<mSize; x++)
654
        for(int y=0; y<mSize; y++)
655
          for(int z=0; z<mSize; z++)
656
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
657
              {
658
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
659
                {
660
                if( first )
661
                  {
662
                  first = false;
663
                  int pointNum = mRotationAngle[x][y][z].getNumPoints();
664

    
665
                  if( pointNum>=1 )
666
                    {
667
                    float startingAngle = mRotationAngle[x][y][z].getPoint(pointNum-1).get1();
668
                    int nearestAngleInDegrees = computeNearestAngle(startingAngle);
669
                    double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
670
                    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
671
                    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
672
                    quat = new Static4D(qx*sinA, qy*sinA, qz*sinA, cosA);
673
                    }
674
                  else
675
                    {
676
                    android.util.Log.e("cube", "ERROR removing rotation!");
677
                    return;
678
                    }
679
                  }
680

    
681
                mRotationAngle[x][y][z].removeAll();
682
                mQuatScramble[x][y][z].set(RubikSurfaceView.quatMultiply(quat,mQuatScramble[x][y][z]));
683
                normalizeScrambleQuat(x,y,z);
684
                modifyCurrentPosition(x,y,z,quat);
685
                }
686
              }
687

    
688
      mRotationAngleStatic.set1(0);
689
      }
690
}
(3-3/10)