Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikCube.java @ 5bfc1b49

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.object;
21

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class RubikCube extends DistortedNode
48
{
49
            static final float CUBE_SCREEN_RATIO = 0.5f;
50
    private static final int POST_ROTATION_MILLISEC = 500;
51
    private static final int TEXTURE_SIZE = 100;
52

    
53
    private static final Static3D VectX = new Static3D(1,0,0);
54
    private static final Static3D VectY = new Static3D(0,1,0);
55
    private static final Static3D VectZ = new Static3D(0,0,1);
56

    
57
    public static final int VECTX = 0;  //
58
    public static final int VECTY = 1;  // don't change this
59
    public static final int VECTZ = 2;  //
60

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

    
74
    private int mRotAxis, mRotRow;
75
    private int mSize;
76

    
77
    private DistortedTexture mNodeTexture;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
    private int computeNearestAngle(float angle)
82
      {
83
      final int NEAREST = 90;
84

    
85
      int tmp = (int)((angle+NEAREST/2)/NEAREST);
86
      if( angle< -(NEAREST*0.5) ) tmp-=1;
87

    
88
      return NEAREST*tmp;
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
// All legal rotation quats must have all four of their components equal to either
93
// 0, 1, -1, 0.5, -0.5 or +-sqrt(2)/2.
94
//
95
// Because of quatMultiplication, errors can accumulate - so to avoid this, we
96
// correct the value of the 'scramble' quat to what it should be.
97
//
98
// We also have to remember that the group of unit quaternions is a double-cover of rotations
99
// in 3D ( q represents the same rotation as -q ) - so invert if needed.
100

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

    
104
    private void normalizeScrambleQuat(int i, int j, int k)
105
      {
106
      Static4D quat = mQuatScramble[i][j][k];
107

    
108
      float x = quat.get0();
109
      float y = quat.get1();
110
      float z = quat.get2();
111
      float w = quat.get3();
112
      float diff;
113

    
114
      for(float legal: LEGAL)
115
        {
116
        diff = x-legal;
117
        if( diff*diff<0.01f ) x = legal;
118
        diff = y-legal;
119
        if( diff*diff<0.01f ) y = legal;
120
        diff = z-legal;
121
        if( diff*diff<0.01f ) z = legal;
122
        diff = w-legal;
123
        if( diff*diff<0.01f ) w = legal;
124
        }
125

    
126
      if( w<0 )
127
        {
128
        w = -w;
129
        z = -z;
130
        y = -y;
131
        x = -x;
132
        }
133
      else if( w==0 )
134
        {
135
        if( z<0 )
136
          {
137
          z = -z;
138
          y = -y;
139
          x = -x;
140
          }
141
        else if( z==0 )
142
          {
143
          if( y<0 )
144
            {
145
            y = -y;
146
            x = -x;
147
            }
148
          else if( y==0 )
149
            {
150
            if( x<0 )
151
              {
152
              x = -x;
153
              }
154
            }
155
          }
156
        }
157

    
158
      mQuatScramble[i][j][k].set(x,y,z,w);
159
      }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    private float getSinkStrength()
164
      {
165
      switch(mSize)
166
        {
167
        case 1 : return 1.1f;
168
        case 2 : return 1.5f;
169
        case 3 : return 1.8f;
170
        case 4 : return 2.0f;
171
        default: return 3.0f - 4.0f/mSize;
172
        }
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
    private boolean belongsToRotation(int x, int y, int z, int vector, int row)
178
      {
179
      switch(vector)
180
        {
181
        case VECTX: return mCurrentPosition[x][y][z].get0()==row;
182
        case VECTY: return mCurrentPosition[x][y][z].get1()==row;
183
        case VECTZ: return mCurrentPosition[x][y][z].get2()==row;
184
        }
185

    
186
      return false;
187
      }
188

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

    
191
    private void modifyCurrentPosition(int x, int y, int z, Static4D quat)
192
      {
193
      Static3D current = mCurrentPosition[x][y][z];
194
      float diff = 0.5f*(mSize-1);
195
      float cubitCenterX = current.get0() - diff;
196
      float cubitCenterY = current.get1() - diff;
197
      float cubitCenterZ = current.get2() - diff;
198

    
199
      Static4D cubitCenter =  new Static4D(cubitCenterX, cubitCenterY, cubitCenterZ, 0);
200
      Static4D rotatedCenter = RubikSurfaceView.rotateVectorByQuat( cubitCenter, quat);
201

    
202
      float rotatedX = rotatedCenter.get0() + diff;
203
      float rotatedY = rotatedCenter.get1() + diff;
204
      float rotatedZ = rotatedCenter.get2() + diff;
205

    
206
      int roundedX = (int)(rotatedX+0.1f);
207
      int roundedY = (int)(rotatedY+0.1f);
208
      int roundedZ = (int)(rotatedZ+0.1f);
209

    
210
      mCurrentPosition[x][y][z].set0(roundedX);
211
      mCurrentPosition[x][y][z].set1(roundedY);
212
      mCurrentPosition[x][y][z].set2(roundedZ);
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
// PUBLIC API
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    public RubikCube(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshFlat mesh, DistortedEffects effects)
220
      {
221
      super(texture,effects,mesh);
222

    
223
      mNodeTexture = texture;
224

    
225
      mSize = size;
226

    
227
      mRotationAngleStatic = new Static1D(0);
228
      mRotationAngleMiddle = new Static1D(0);
229
      mRotationAngleFinal  = new Static1D(0);
230

    
231
      mMove     = new Static3D(0,0,0);
232
      mScale    = new Static3D(1,1,1);
233
      mNodeMove = new Static3D(0,0,0);
234
      mNodeScale= new Static3D(1,1,1);
235

    
236
      mQuatAccumulated = quatAcc;
237

    
238
      mRotAxis = VECTX;
239
      mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
240

    
241
      mNodes          = new DistortedNode[mSize][mSize][mSize];
242
      mCubes          = new MeshCubes[mSize][mSize][mSize];
243
      mEffects        = new DistortedEffects[mSize][mSize][mSize];
244
      mQuatScramble   = new Static4D[mSize][mSize][mSize];
245
      mRotationAxis   = new Static3D[mSize][mSize][mSize];
246
      mRotationAngle  = new Dynamic1D[mSize][mSize][mSize];
247
      mCurrentPosition= new Static3D[mSize][mSize][mSize];
248
      mRotateEffect   = new MatrixEffectRotate[mSize][mSize][mSize];
249

    
250
      Static3D[][][] cubeVectors = new Static3D[mSize][mSize][mSize];
251

    
252
      Static3D sinkCenter = new Static3D(TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f, TEXTURE_SIZE*0.5f);
253
      Static3D matrCenter = new Static3D(0,0,0);
254
      Static4D region = new Static4D(0,0,0, TEXTURE_SIZE*0.72f);
255

    
256
      VertexEffectSink        sinkEffect = new VertexEffectSink( new Static1D(getSinkStrength()), sinkCenter, region );
257
      MatrixEffectMove        moveEffect = new MatrixEffectMove(mMove);
258
      MatrixEffectScale      scaleEffect = new MatrixEffectScale(mScale);
259
      MatrixEffectQuaternion quatCEffect = new MatrixEffectQuaternion(quatCur, matrCenter);
260
      MatrixEffectQuaternion quatAEffect = new MatrixEffectQuaternion(quatAcc, matrCenter);
261

    
262
      MatrixEffectMove       nodeMoveEffect  = new MatrixEffectMove(mNodeMove);
263
      MatrixEffectScale      nodeScaleEffect = new MatrixEffectScale(mNodeScale);
264

    
265
      effects.apply(nodeScaleEffect);
266
      effects.apply(nodeMoveEffect);
267

    
268
      // 3x2 bitmap = 6 squares:
269
      //
270
      // RED     GREEN   BLUE
271
      // YELLOW  WHITE   BROWN
272

    
273
      final float ze = 0.0f;
274
      final float ot = 1.0f/3.0f;
275
      final float tt = 2.0f/3.0f;
276
      final float oh = 1.0f/2.0f;
277
      final float of = 1.0f/40.0f;
278

    
279
      final Static4D mapFront = new Static4D(ze,oh, ze+ot,oh+oh);
280
      final Static4D mapBack  = new Static4D(tt,ze, tt+ot,ze+oh);
281
      final Static4D mapLeft  = new Static4D(ot,ze, ot+ot,ze+oh);
282
      final Static4D mapRight = new Static4D(ze,ze, ze+ot,ze+oh);
283
      final Static4D mapTop   = new Static4D(tt,oh, tt+ot,oh+oh);
284
      final Static4D mapBottom= new Static4D(ot,oh, ot+ot,oh+oh);
285

    
286
      final Static4D mapBlack = new Static4D(ze,ze, ze+of,ze+of);
287

    
288
      Static4D tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom;
289
      float nc = 0.5f*mSize;
290
      int vertices = (int)(24.0f/mSize + 2.0f);
291

    
292
      for(int x = 0; x< mSize; x++)
293
        for(int y = 0; y< mSize; y++)
294
          for(int z = 0; z< mSize; z++)
295
            {
296
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 ) // only the external walls
297
              {
298
              tmpLeft  = (x==       0 ? mapLeft  :mapBlack);
299
              tmpRight = (x== mSize-1 ? mapRight :mapBlack);
300
              tmpFront = (z== mSize-1 ? mapFront :mapBlack);
301
              tmpBack  = (z==       0 ? mapBack  :mapBlack);
302
              tmpTop   = (y== mSize-1 ? mapTop   :mapBlack);
303
              tmpBottom= (y==       0 ? mapBottom:mapBlack);
304

    
305
              mCubes[x][y][z]           = new MeshCubes(vertices,vertices,vertices, tmpFront, tmpBack, tmpLeft, tmpRight, tmpTop, tmpBottom);
306
              cubeVectors[x][y][z]      = new Static3D( TEXTURE_SIZE*(x-nc), TEXTURE_SIZE*(y-nc), TEXTURE_SIZE*(z-nc) );
307
              mQuatScramble[x][y][z]    = new Static4D(0,0,0,1);
308
              mRotationAngle[x][y][z]   = new Dynamic1D();
309
              mRotationAxis[x][y][z]    = new Static3D(1,0,0);
310
              mCurrentPosition[x][y][z] = new Static3D(x,y,z);
311
              mRotateEffect[x][y][z]    = new MatrixEffectRotate(mRotationAngle[x][y][z], mRotationAxis[x][y][z], matrCenter);
312

    
313
              mEffects[x][y][z] = new DistortedEffects();
314
              mEffects[x][y][z].apply(sinkEffect);
315
              mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
316
              mEffects[x][y][z].apply( new MatrixEffectQuaternion(mQuatScramble[x][y][z], matrCenter));
317
              mEffects[x][y][z].apply(mRotateEffect[x][y][z]);
318
              mEffects[x][y][z].apply(quatAEffect);
319
              mEffects[x][y][z].apply(quatCEffect);
320
              mEffects[x][y][z].apply(scaleEffect);
321
              mEffects[x][y][z].apply(moveEffect);
322

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

    
325
              attach(mNodes[x][y][z]);
326
              }
327
            }
328
      }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
    public void addNewRotation(int vector, int row )
333
      {
334
      Static3D axis = VectX;
335

    
336
      switch(vector)
337
        {
338
        case VECTX: axis = VectX; break;
339
        case VECTY: axis = VectY; break;
340
        case VECTZ: axis = VectZ; break;
341
        }
342

    
343
      mRotAxis = vector;
344
      mRotRow  = row;
345

    
346
      mRotationAngleStatic.set0(0.0f);
347

    
348
      for(int x=0; x<mSize; x++)
349
        for(int y=0; y<mSize; y++)
350
          for(int z=0; z<mSize; z++)
351
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
352
              {
353
              if( belongsToRotation(x,y,z,vector,mRotRow) )
354
                {
355
                mRotationAxis[x][y][z].set(axis);
356
                mRotationAngle[x][y][z].add(mRotationAngleStatic);
357
                }
358
              }
359
      }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
    public void continueRotation(float angleInDegrees)
364
      {
365
      mRotationAngleStatic.set0(angleInDegrees);
366
      }
367

    
368
///////////////////////////////////////////////////////////////////////////////////////////////////
369

    
370
    public Static4D getRotationQuat()
371
      {
372
      return mQuatAccumulated;
373
      }
374

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

    
377
   public void savePreferences(SharedPreferences.Editor editor)
378
     {
379
     android.util.Log.e("cube", "saving Preferences");
380
     }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
   public void restorePreferences(SharedPreferences preferences)
385
     {
386
     android.util.Log.e("cube", "restoring Preferences");
387
     }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
    public long finishRotationNow(EffectListener listener)
392
      {
393
      boolean first = true;
394
      long effectID=0;
395

    
396
      for(int x=0; x<mSize; x++)
397
        for(int y=0; y<mSize; y++)
398
          for(int z=0; z<mSize; z++)
399
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
400
              {
401
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
402
                {
403
                if( first )
404
                  {
405
                  first = false;
406
                  mRotateEffect[x][y][z].notifyWhenFinished(listener);
407
                  effectID = mRotateEffect[x][y][z].getID();
408
                  int pointNum = mRotationAngle[x][y][z].getNumPoints();
409

    
410
                  if( pointNum>=1 )
411
                    {
412
                    float startingAngle = mRotationAngle[x][y][z].getPoint(pointNum-1).get0();
413
                    int nearestAngleInDegrees = computeNearestAngle(startingAngle);
414
                    mRotationAngleStatic.set0(startingAngle);
415
                    mRotationAngleFinal.set0(nearestAngleInDegrees);
416
                    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-startingAngle)*0.2f );
417
                    }
418
                  else
419
                    {
420
                    android.util.Log.e("cube", "ERROR finishing rotation!");
421
                    return 0;
422
                    }
423
                  }
424

    
425
                mRotationAngle[x][y][z].setDuration(POST_ROTATION_MILLISEC);
426
                mRotationAngle[x][y][z].resetToBeginning();
427
                mRotationAngle[x][y][z].removeAll();
428
                mRotationAngle[x][y][z].add(mRotationAngleStatic);
429
                mRotationAngle[x][y][z].add(mRotationAngleMiddle);
430
                mRotationAngle[x][y][z].add(mRotationAngleFinal);
431
                }
432
              }
433

    
434
      return effectID;
435
      }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438
// all DistortedTextures, DistortedNodes, DistortedFramebuffers, DistortedScreens and all types of
439
// Meshes HAVE TO be markedForDeletion when they are no longer needed- otherwise we have a major
440
// memory leak.
441

    
442
    public void releaseResources()
443
      {
444
      mTexture.markForDeletion();
445

    
446
      for(int x=0; x<mSize; x++)
447
        for(int y=0; y<mSize; y++)
448
          for(int z=0; z<mSize; z++)
449
            {
450
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
451
              {
452
              mCubes[x][y][z].markForDeletion();
453
              mNodes[x][y][z].markForDeletion();
454
              }
455
            }
456
      }
457

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

    
460
    public void createTexture()
461
      {
462
      Bitmap bitmap;
463

    
464
      final int S = 128;
465
      final int W = 3*S;
466
      final int H = 2*S;
467
      final int R = S/10;
468
      final int M = S/20;
469

    
470
      Paint paint = new Paint();
471
      bitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
472
      Canvas canvas = new Canvas(bitmap);
473

    
474
      paint.setAntiAlias(true);
475
      paint.setTextAlign(Paint.Align.CENTER);
476
      paint.setStyle(Paint.Style.FILL);
477

    
478
      // 3x2 bitmap = 6 squares:
479
      //
480
      // RED     GREEN   BLUE
481
      // YELLOW  WHITE   BROWN
482

    
483
      paint.setColor(0xff000000);                                  // BLACK BACKGROUND
484
      canvas.drawRect(0, 0, W, H, paint);                          //
485

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

    
499
      mTexture.setTexture(bitmap);
500
      }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
    public void recomputeScaleFactor(int screenWidth, int screenHeight)
505
      {
506
      int texW = mNodeTexture.getWidth();
507
      int texH = mNodeTexture.getHeight();
508

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

    
524
      float scaleFactor = (CUBE_SCREEN_RATIO*texW/(TEXTURE_SIZE*mSize));
525

    
526
      mMove.set( texW*0.5f , texH*0.5f , 0.0f );
527
      mScale.set(scaleFactor,scaleFactor,scaleFactor);
528
      }
529

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

    
532
    public void apply(Effect effect, int position)
533
      {
534
      for(int x=0; x<mSize; x++)
535
        for(int y=0; y<mSize; y++)
536
          for(int z=0; z<mSize; z++)
537
            {
538
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
539
              {
540
              mEffects[x][y][z].apply(effect, position);
541
              }
542
            }
543
      }
544

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

    
547
    public void remove(long effectID)
548
      {
549
      for(int x=0; x<mSize; x++)
550
        for(int y=0; y<mSize; y++)
551
          for(int z=0; z<mSize; z++)
552
            {
553
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
554
              {
555
              mEffects[x][y][z].abortById(effectID);
556
              }
557
            }
558
      }
559

    
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561

    
562
    public void solve()
563
      {
564
      for(int x=0; x<mSize; x++)
565
        for(int y=0; y<mSize; y++)
566
          for(int z=0; z<mSize; z++)
567
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
568
              {
569
              mQuatScramble[x][y][z].set(0,0,0,1);
570
              mCurrentPosition[x][y][z].set(x,y,z);
571
              }
572
      }
573

    
574
///////////////////////////////////////////////////////////////////////////////////////////////////
575

    
576
    public boolean isSolved()
577
      {
578
      Static4D q = mQuatScramble[0][0][0];
579

    
580
      float x = q.get0();
581
      float y = q.get1();
582
      float z = q.get2();
583
      float w = q.get3();
584

    
585
      for(int i = 0; i< mSize; i++)
586
        for(int j = 0; j< mSize; j++)
587
          for(int k = 0; k< mSize; k++)
588
            {
589
            if( i==0 || i==mSize-1 || j==0 || j==mSize-1 || k==0 || k==mSize-1 )
590
              {
591
              q = mQuatScramble[i][j][k];
592

    
593
              if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w )
594
                {
595
                return false;
596
                }
597
              }
598
            }
599

    
600
      return true;
601
      }
602

    
603
///////////////////////////////////////////////////////////////////////////////////////////////////
604

    
605
    public int getSize()
606
      {
607
      return mSize;
608
      }
609

    
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611

    
612
    public long addNewRotation(int vector, int row, int angle, long durationMillis, EffectListener listener )
613
      {
614
      Static3D axis = VectX;
615
      long effectID=0;
616
      boolean first = true;
617

    
618
      switch(vector)
619
        {
620
        case VECTX: axis = VectX; break;
621
        case VECTY: axis = VectY; break;
622
        case VECTZ: axis = VectZ; break;
623
        }
624

    
625
      mRotAxis = vector;
626
      mRotRow  = row;
627

    
628
      mRotationAngleStatic.set0(0.0f);
629

    
630
      for(int x=0; x<mSize; x++)
631
        for(int y=0; y<mSize; y++)
632
          for(int z=0; z<mSize; z++)
633
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
634
              {
635
              if( belongsToRotation(x,y,z,vector,mRotRow) )
636
                {
637
                mRotationAxis[x][y][z].set(axis);
638
                mRotationAngle[x][y][z].setDuration(durationMillis);
639
                mRotationAngle[x][y][z].resetToBeginning();
640
                mRotationAngle[x][y][z].add(new Static1D(0));
641
                mRotationAngle[x][y][z].add(new Static1D(angle));
642

    
643
                if( first )
644
                  {
645
                  first = false;
646
                  effectID = mRotateEffect[x][y][z].getID();
647
                  mRotateEffect[x][y][z].notifyWhenFinished(listener);
648
                  }
649
                }
650
              }
651

    
652
      return effectID;
653
      }
654

    
655
///////////////////////////////////////////////////////////////////////////////////////////////////
656

    
657
    public void removeRotationNow()
658
      {
659
      float qx=0,qy=0,qz=0;
660
      boolean first = true;
661
      Static4D quat = null;
662

    
663
      switch(mRotAxis)
664
        {
665
        case VECTX: qx=1; break;
666
        case VECTY: qy=1; break;
667
        case VECTZ: qz=1; break;
668
        }
669

    
670
      for(int x=0; x<mSize; x++)
671
        for(int y=0; y<mSize; y++)
672
          for(int z=0; z<mSize; z++)
673
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
674
              {
675
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
676
                {
677
                if( first )
678
                  {
679
                  first = false;
680
                  int pointNum = mRotationAngle[x][y][z].getNumPoints();
681

    
682
                  if( pointNum>=1 )
683
                    {
684
                    float startingAngle = mRotationAngle[x][y][z].getPoint(pointNum-1).get0();
685
                    int nearestAngleInDegrees = computeNearestAngle(startingAngle);
686
                    double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
687
                    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
688
                    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
689
                    quat = new Static4D(qx*sinA, qy*sinA, qz*sinA, cosA);
690
                    }
691
                  else
692
                    {
693
                    android.util.Log.e("cube", "ERROR removing rotation!");
694
                    return;
695
                    }
696
                  }
697

    
698
                mRotationAngle[x][y][z].removeAll();
699
                mQuatScramble[x][y][z].set(RubikSurfaceView.quatMultiply(quat,mQuatScramble[x][y][z]));
700
                normalizeScrambleQuat(x,y,z);
701
                modifyCurrentPosition(x,y,z,quat);
702
                }
703
              }
704

    
705
      mRotationAngleStatic.set0(0);
706
      }
707
}
(1-1/3)