Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 31cd7256

1 ece1b58d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.res.Resources;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
26 749ef882 Leszek Koltunski
import org.distorted.helpers.FactoryCubit;
27
import org.distorted.helpers.FactorySticker;
28 ece1b58d Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshBase;
32
import org.distorted.library.mesh.MeshSquare;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35 6fd4a72c Leszek Koltunski
import org.distorted.main.R;
36 ad6f6f03 Leszek Koltunski
import org.distorted.main.RubikSurfaceView;
37 ece1b58d Leszek Koltunski
38
import java.util.Random;
39
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 9c2f0c91 Leszek Koltunski
public class TwistyDiamond extends TwistyObject
43 ece1b58d Leszek Koltunski
{
44
  private static final int FACES_PER_CUBIT =8;
45
46
  // the four rotation axis of a Diamond. Must be normalized.
47
  static final Static3D[] ROT_AXIS = new Static3D[]
48
         {
49
           new Static3D(+SQ6/3,+SQ3/3,     0),
50
           new Static3D(-SQ6/3,+SQ3/3,     0),
51 cc99cf91 Leszek Koltunski
           new Static3D(     0,-SQ3/3,-SQ6/3),
52
           new Static3D(     0,-SQ3/3,+SQ6/3)
53 ece1b58d Leszek Koltunski
         };
54
55
  private static final int[] FACE_COLORS = new int[]
56
         {
57 2ef489e2 Leszek Koltunski
           COLOR_ORANGE, COLOR_VIOLET,
58
           COLOR_WHITE , COLOR_BLUE  ,
59
           COLOR_YELLOW, COLOR_RED   ,
60
           COLOR_GREEN , COLOR_GREY
61 ece1b58d Leszek Koltunski
         };
62
63 ab0c28f0 Leszek Koltunski
  // All legal rotation quats of a Diamond: unit + three 180 deg turns + 8 generators
64 ece1b58d Leszek Koltunski
  private static final Static4D[] QUATS = new Static4D[]
65
         {
66
           new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
67
           new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
68 ab0c28f0 Leszek Koltunski
           new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
69
           new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
70
71 ece1b58d Leszek Koltunski
           new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
72
           new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
73
           new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
74
           new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
75
           new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
76
           new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
77
           new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
78 ab0c28f0 Leszek Koltunski
           new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
79 ece1b58d Leszek Koltunski
         };
80
81
  private static final float DIST = 0.50f;
82
83 2ef489e2 Leszek Koltunski
  private static final int[][] mFaceNeutralQuatIndex = new int[][]
84 ece1b58d Leszek Koltunski
         {
85 2ef489e2 Leszek Koltunski
             {6,10},
86
             {4, 8},
87
             {7,11},
88
             {5, 9},
89
             {7,11},
90
             {5, 9},
91
             {6,10},
92
             {4, 8}
93 ece1b58d Leszek Koltunski
         };
94
95 68b5f9c5 Leszek Koltunski
  private static final int[] mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
96
97 31cd7256 Leszek Koltunski
  private static final double[][] VERTICES_TETRA = new double[][]
98 b1f2ccf5 Leszek Koltunski
          {
99
             {-0.5, SQ2/4, 0.0},
100
             { 0.5, SQ2/4, 0.0},
101
             { 0.0,-SQ2/4, 0.5},
102
             { 0.0,-SQ2/4,-0.5}
103
          };
104
105 31cd7256 Leszek Koltunski
  private static final int[][] VERT_INDEXES_TETRA = new int[][]
106 b1f2ccf5 Leszek Koltunski
          {
107
             {2,1,0},   // counterclockwise!
108
             {2,3,1},
109
             {3,2,0},
110
             {3,0,1}
111
          };
112
113 31cd7256 Leszek Koltunski
  private static final double[][] VERTICES_OCTA = new double[][]
114 b1f2ccf5 Leszek Koltunski
          {
115
             { 0.5,   0.0, 0.5},
116
             { 0.5,   0.0,-0.5},
117
             {-0.5,   0.0,-0.5},
118
             {-0.5,   0.0, 0.5},
119
             { 0.0, SQ2/2, 0.0},
120
             { 0.0,-SQ2/2, 0.0}
121
          };
122
123 31cd7256 Leszek Koltunski
  private static final int[][] VERT_INDEXES_OCTA = new int[][]
124 b1f2ccf5 Leszek Koltunski
          {
125
             {3,0,4},   // counterclockwise!
126
             {0,1,4},
127
             {1,2,4},
128
             {2,3,4},
129
             {5,0,3},
130
             {5,1,0},
131
             {5,2,1},
132
             {5,3,2}
133
          };
134
135 31cd7256 Leszek Koltunski
  private static final float[][] STICKERS = new float[][]
136
          {
137
             { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f }
138
          };
139
140
  private static MeshBase[] mMeshes;
141 ece1b58d Leszek Koltunski
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144 9c2f0c91 Leszek Koltunski
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
145
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
146 ece1b58d Leszek Koltunski
    {
147 db875721 Leszek Koltunski
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
148 ece1b58d Leszek Koltunski
    }
149
150 b1f2ccf5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152
  double[][] getVertices(int cubitType)
153
    {
154 31cd7256 Leszek Koltunski
    if( cubitType==0 ) return VERTICES_OCTA;
155
    if( cubitType==1 ) return VERTICES_TETRA;
156 b1f2ccf5 Leszek Koltunski
    return null;
157
    }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
  int[][] getVertIndexes(int cubitType)
162
    {
163 31cd7256 Leszek Koltunski
    if( cubitType==0 ) return VERT_INDEXES_OCTA;
164
    if( cubitType==1 ) return VERT_INDEXES_TETRA;
165 b1f2ccf5 Leszek Koltunski
    return null;
166
    }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170
  int getNumCubitTypes(int numLayers)
171
    {
172
    return 2;
173
    }
174
175 ece1b58d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
  float getScreenRatio()
178
    {
179 ab0c28f0 Leszek Koltunski
    return 0.65f;
180 ece1b58d Leszek Koltunski
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
  Static4D[] getQuats()
185
    {
186
    return QUATS;
187
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
  int getNumFaces()
192
    {
193
    return FACE_COLORS.length;
194
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
  boolean shouldResetTextureMaps()
199
    {
200
    return false;
201
    }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205 a64e07d0 Leszek Koltunski
  int getNumStickerTypes(int numLayers)
206 ece1b58d Leszek Koltunski
    {
207 31cd7256 Leszek Koltunski
    return STICKERS.length;
208 ece1b58d Leszek Koltunski
    }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 680f921e Leszek Koltunski
  float[] getCuts(int numLayers)
213 ece1b58d Leszek Koltunski
    {
214 680f921e Leszek Koltunski
    if( numLayers<2 )
215
      {
216
      return null;
217
      }
218
    else
219
      {
220
      float[] cuts = new float[numLayers-1];
221 68b5f9c5 Leszek Koltunski
      float dist = SQ6*0.666f*DIST;
222 680f921e Leszek Koltunski
      float cut  = 0.5f*dist*(2-numLayers);
223
224
      for(int i=0; i<numLayers-1; i++)
225
        {
226
        cuts[i] = cut;
227
        cut += dist;
228
        }
229
230
      return cuts;
231
      }
232 ece1b58d Leszek Koltunski
    }
233
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236
  int getNumCubitFaces()
237
    {
238
    return FACES_PER_CUBIT;
239
    }
240
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
243 680f921e Leszek Koltunski
  private int getNumOctahedrons(int layers)
244
    {
245
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
246
    }
247
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250
  private int getNumTetrahedrons(int layers)
251
    {
252
    return 4*layers*(layers-1);
253
    }
254
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257 31cd7256 Leszek Koltunski
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
258 680f921e Leszek Koltunski
    {
259
    float x = DIST*(layers-1);
260
    float z = DIST*(layers+1);
261
262
    for(int i=0; i<layers; i++, index++)
263
      {
264
      z -= 2*DIST;
265
      centers[index][0] = x;
266
      centers[index][1] = height;
267
      centers[index][2] = z;
268
      }
269
270
    for(int i=0; i<layers-1; i++, index++)
271
      {
272
      x -= 2*DIST;
273
      centers[index][0] = x;
274
      centers[index][1] = height;
275
      centers[index][2] = z;
276
      }
277
278
    for(int i=0; i<layers-1; i++, index++)
279
      {
280
      z += 2*DIST;
281
      centers[index][0] = x;
282
      centers[index][1] = height;
283
      centers[index][2] = z;
284
      }
285
286
    for(int i=0; i<layers-2; i++, index++)
287
      {
288
      x += 2*DIST;
289
      centers[index][0] = x;
290
      centers[index][1] = height;
291
      centers[index][2] = z;
292
      }
293
294
    return index;
295
    }
296
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
299 31cd7256 Leszek Koltunski
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
300 ece1b58d Leszek Koltunski
    {
301 680f921e Leszek Koltunski
    float x = DIST*(layers-1);
302
    float z = DIST*layers;
303
304
    for(int i=0; i<layers-1; i++, index++)
305
      {
306
      z -= 2*DIST;
307
      centers[index][0] = x;
308
      centers[index][1] = height;
309
      centers[index][2] = z;
310
      }
311
312
    x += DIST;
313
    z -= DIST;
314
315
    for(int i=0; i<layers-1; i++, index++)
316
      {
317
      x -= 2*DIST;
318
      centers[index][0] = x;
319
      centers[index][1] = height;
320
      centers[index][2] = z;
321
      }
322
323
    x -= DIST;
324
    z -= DIST;
325
326
    for(int i=0; i<layers-1; i++, index++)
327
      {
328
      z += 2*DIST;
329
      centers[index][0] = x;
330
      centers[index][1] = height;
331
      centers[index][2] = z;
332
      }
333
334
    x -= DIST;
335
    z += DIST;
336
337
    for(int i=0; i<layers-1; i++, index++)
338
      {
339
      x += 2*DIST;
340
      centers[index][0] = x;
341
      centers[index][1] = height;
342
      centers[index][2] = z;
343
      }
344
345
    return index;
346
    }
347
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
350
  float[][] getCubitPositions(int layers)
351
    {
352
    int numO = getNumOctahedrons(layers);
353
    int numT = getNumTetrahedrons(layers);
354
    int index = 0;
355
    float height = 0.0f;
356
357
    float[][] CENTERS = new float[numO+numT][3];
358
359 31cd7256 Leszek Koltunski
    index = createOctaPositions(CENTERS,index,layers,height);
360 680f921e Leszek Koltunski
361
    for(int i=layers-1; i>0; i--)
362
      {
363
      height += SQ2*DIST;
364 31cd7256 Leszek Koltunski
      index = createOctaPositions(CENTERS,index,i,+height);
365
      index = createOctaPositions(CENTERS,index,i,-height);
366 680f921e Leszek Koltunski
      }
367
368
    height = DIST*SQ2/2;
369
370
    for(int i=layers; i>1; i--)
371
      {
372 31cd7256 Leszek Koltunski
      index = createTetraPositions(CENTERS,index,i,+height);
373
      index = createTetraPositions(CENTERS,index,i,-height);
374 680f921e Leszek Koltunski
      height += SQ2*DIST;
375
      }
376
377 ece1b58d Leszek Koltunski
    return CENTERS;
378
    }
379
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
382 680f921e Leszek Koltunski
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
383 ece1b58d Leszek Koltunski
    {
384 68b5f9c5 Leszek Koltunski
    for(int i=numLayers-1; i>0; i--)
385 ece1b58d Leszek Koltunski
      {
386 68b5f9c5 Leszek Koltunski
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
387
      tetra -= 8*i;
388 680f921e Leszek Koltunski
      }
389 68b5f9c5 Leszek Koltunski
390
    return -1;
391 680f921e Leszek Koltunski
    }
392
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
395
  private Static4D getQuat(int cubit, int numLayers, int numO)
396
    {
397
    if( cubit<numO ) return QUATS[0];
398
399
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
400
      {
401 2ef489e2 Leszek Koltunski
      case 0: return QUATS[0];                          // unit quat
402
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
403
      case 2: return QUATS[1];                          // 180 along Y
404
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
405
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
406
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
407
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
408
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
409 ece1b58d Leszek Koltunski
      }
410
411
    return null;
412
    }
413
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415
416 a64e07d0 Leszek Koltunski
  MeshBase createCubitMesh(int cubit, int numLayers)
417 ece1b58d Leszek Koltunski
    {
418 31cd7256 Leszek Koltunski
    if( mMeshes==null )
419
      {
420
      FactoryCubit factory = FactoryCubit.getInstance();
421
      factory.clear();
422
      mMeshes = new MeshBase[2];
423
      }
424
425 ece1b58d Leszek Koltunski
    MeshBase mesh;
426 680f921e Leszek Koltunski
    int numO = getNumOctahedrons(numLayers);
427 ece1b58d Leszek Koltunski
428 680f921e Leszek Koltunski
    if( cubit<numO )
429 ece1b58d Leszek Koltunski
      {
430 31cd7256 Leszek Koltunski
      if( mMeshes[0]==null )
431
        {
432
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
433
        int[] bandIndexes   = new int[] { 0,0,0,0,0,0,0,0 };
434
        float[][] corners   = new float[][] { {0.04f,0.20f} };
435
        int[] cornerIndexes = new int[] { 0,0,0,0,0,0 };
436
437
        FactoryCubit factory = FactoryCubit.getInstance();
438
439
        factory.createNewFaceTransform(VERTICES_OCTA,VERT_INDEXES_OCTA);
440
        mMeshes[0] = factory.createRoundedSolid(VERTICES_OCTA, VERT_INDEXES_OCTA,
441
                                                bands, bandIndexes,
442
                                                corners, cornerIndexes,
443
                                                getNumCubitFaces() );
444
        }
445
      mesh = mMeshes[0].copy(true);
446 ece1b58d Leszek Koltunski
      }
447
    else
448
      {
449 31cd7256 Leszek Koltunski
      if( mMeshes[1]==null )
450
        {
451
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
452
        int[] bandIndexes   = new int[] { 0,0,0,0 };
453
        float[][] corners   = new float[][] { {0.08f,0.15f} };
454
        int[] cornerIndexes = new int[] { 0,0,0,0 };
455
456
        FactoryCubit factory = FactoryCubit.getInstance();
457
458
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
459
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
460
                                                bands, bandIndexes,
461
                                                corners, cornerIndexes,
462
                                                getNumCubitFaces() );
463
        }
464
      mesh = mMeshes[1].copy(true);
465 ece1b58d Leszek Koltunski
      }
466
467 680f921e Leszek Koltunski
    Static4D sQ = getQuat(cubit,numLayers,numO);
468
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( sQ, new Static3D(0,0,0) );
469 ece1b58d Leszek Koltunski
    mesh.apply(quat,0xffffffff,0);
470
471
    return mesh;
472
    }
473
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475
476 2ef489e2 Leszek Koltunski
  int getFaceColor(int cubit, int cubitface, int size)
477 ece1b58d Leszek Koltunski
    {
478 2ef489e2 Leszek Koltunski
    int numO = getNumOctahedrons(size);
479 680f921e Leszek Koltunski
480
    if( cubit<numO )
481
      {
482 2ef489e2 Leszek Koltunski
      int axis = 0;
483
      int layer= 1;
484
485
      switch(cubitface)
486
        {
487
        case 0: axis = 2; layer =             1; break;
488
        case 1: axis = 0; layer = (1<<(size-1)); break;
489
        case 2: axis = 3; layer =             1; break;
490
        case 3: axis = 1; layer = (1<<(size-1)); break;
491
        case 4: axis = 3; layer = (1<<(size-1)); break;
492
        case 5: axis = 1; layer =             1; break;
493
        case 6: axis = 2; layer = (1<<(size-1)); break;
494
        case 7: axis = 0; layer =             1; break;
495
        }
496
497 a4962b9c Leszek Koltunski
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
498 680f921e Leszek Koltunski
      }
499
    else
500
      {
501 a4962b9c Leszek Koltunski
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
502 680f921e Leszek Koltunski
      }
503 ece1b58d Leszek Koltunski
    }
504
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506
507 ae755eda Leszek Koltunski
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
508 ece1b58d Leszek Koltunski
    {
509 76c2bd07 Leszek Koltunski
    float R = 0.06f;
510
    float S = 0.07f;
511
512 b89898c5 Leszek Koltunski
    FactorySticker factory = FactorySticker.getInstance();
513 31cd7256 Leszek Koltunski
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
514 ece1b58d Leszek Koltunski
    }
515
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
518
  float returnMultiplier()
519
    {
520 cc99cf91 Leszek Koltunski
    return 1.5f;
521 ece1b58d Leszek Koltunski
    }
522
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524
525 a64e07d0 Leszek Koltunski
  float[] getRowChances(int numLayers)
526 ece1b58d Leszek Koltunski
    {
527 680f921e Leszek Koltunski
    float[] chances = new float[numLayers];
528 ece1b58d Leszek Koltunski
529 680f921e Leszek Koltunski
    for(int i=0; i<numLayers; i++) chances[i] = ((float)(i+1))/numLayers;
530 ece1b58d Leszek Koltunski
531
    return chances;
532
    }
533
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535
// PUBLIC API
536
537
  public Static3D[] getRotationAxis()
538
    {
539
    return ROT_AXIS;
540
    }
541
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543
544
  public int getBasicAngle()
545
    {
546
    return 3;
547
    }
548
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
551 5043d5d0 Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
552 ece1b58d Leszek Koltunski
    {
553 5043d5d0 Leszek Koltunski
    if( num==0 )
554 ece1b58d Leszek Koltunski
      {
555 5043d5d0 Leszek Koltunski
      scramble[num][0] = rnd.nextInt(ROTATION_AXIS.length);
556 ece1b58d Leszek Koltunski
      }
557
    else
558
      {
559 bbc6471c Leszek Koltunski
      int newVector = rnd.nextInt(ROTATION_AXIS.length-1);
560 5043d5d0 Leszek Koltunski
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
561 ece1b58d Leszek Koltunski
      }
562
563
    float rowFloat = rnd.nextFloat();
564
565
    for(int row=0; row<mRowChances.length; row++)
566
      {
567 bbc6471c Leszek Koltunski
      if( rowFloat<=mRowChances[row] )
568
        {
569 5043d5d0 Leszek Koltunski
        scramble[num][1] = row;
570 bbc6471c Leszek Koltunski
        break;
571
        }
572 ece1b58d Leszek Koltunski
      }
573
574 5043d5d0 Leszek Koltunski
    switch( rnd.nextInt(2) )
575
      {
576
      case 0: scramble[num][2] = -1; break;
577
      case 1: scramble[num][2] =  1; break;
578
      }
579 ece1b58d Leszek Koltunski
    }
580
581 ad6f6f03 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
582
583
  int mulQuat(int q1, int q2)
584
    {
585
    Static4D result = RubikSurfaceView.quatMultiply(QUATS[q1],QUATS[q2]);
586
587
    float rX = result.get0();
588
    float rY = result.get1();
589
    float rZ = result.get2();
590
    float rW = result.get3();
591
592
    final float MAX_ERROR = 0.1f;
593
    float dX,dY,dZ,dW;
594
595
    for(int i=0; i<QUATS.length; i++)
596
      {
597
      dX = QUATS[i].get0() - rX;
598
      dY = QUATS[i].get1() - rY;
599
      dZ = QUATS[i].get2() - rZ;
600
      dW = QUATS[i].get3() - rW;
601
602
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
603
          dY<MAX_ERROR && dY>-MAX_ERROR &&
604
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
605
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
606
607
      dX = QUATS[i].get0() + rX;
608
      dY = QUATS[i].get1() + rY;
609
      dZ = QUATS[i].get2() + rZ;
610
      dW = QUATS[i].get3() + rW;
611
612
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
613
          dY<MAX_ERROR && dY>-MAX_ERROR &&
614
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
615
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
616
      }
617
618
    return -1;
619
    }
620
621 ece1b58d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
622 75a21cd6 Leszek Koltunski
// The Diamond is solved if and only if:
623 ece1b58d Leszek Koltunski
//
624 2ef489e2 Leszek Koltunski
// 1) all octahedrons are rotated with the same quat
625
// 2) all tetrahedrons might be also optionally rotated by a 'face neutral' pair of quats
626
//    (indexes of those are kept in the 'mFaceNeutralQuattIndex' table)
627 ad6f6f03 Leszek Koltunski
//
628 75a21cd6 Leszek Koltunski
// Note: this works for any size, because even if layers>3 - and then there are 'face-internal'
629
// octahedrons which, it would seem, can be rotated by those 'face neutral' pairs of quats - but
630
// in reality no, because if they were, the octahedrons would then not fit in the lattice...
631 ece1b58d Leszek Koltunski
632
  public boolean isSolved()
633
    {
634 ab0c28f0 Leszek Koltunski
    int q = CUBITS[0].mQuatIndex;
635 2ef489e2 Leszek Koltunski
    int layers = getNumLayers();
636
    int numO = getNumOctahedrons(layers);
637 ab0c28f0 Leszek Koltunski
638 2ef489e2 Leszek Koltunski
    for(int i=1; i<numO; i++)
639 ad6f6f03 Leszek Koltunski
      {
640 2ef489e2 Leszek Koltunski
      if( CUBITS[i].mQuatIndex != q ) return false;
641
      }
642 ad6f6f03 Leszek Koltunski
643 2ef489e2 Leszek Koltunski
    int qI, q1Index, q2Index, face;
644 ad6f6f03 Leszek Koltunski
645 2ef489e2 Leszek Koltunski
    for(int i=numO; i<NUM_CUBITS; i++)
646
      {
647
      face    = retFaceTetraBelongsTo(i-numO,layers);
648
      q1Index = mFaceNeutralQuatIndex[face][0];
649
      q2Index = mFaceNeutralQuatIndex[face][1];
650
      qI      = CUBITS[i].mQuatIndex;
651 ad6f6f03 Leszek Koltunski
652 2ef489e2 Leszek Koltunski
      if(  qI != q && qI != mulQuat(q,q1Index) && qI != mulQuat(q,q2Index) ) return false;
653 ad6f6f03 Leszek Koltunski
      }
654
655 2ef489e2 Leszek Koltunski
    return true;
656 ece1b58d Leszek Koltunski
    }
657
658
///////////////////////////////////////////////////////////////////////////////////////////////////
659 ab0c28f0 Leszek Koltunski
// only needed for solvers - there are no Diamond solvers ATM
660 ece1b58d Leszek Koltunski
661
  public String retObjectString()
662
    {
663
    return "";
664
    }
665
666 6fd4a72c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
667
668
  public int getObjectName(int numLayers)
669
    {
670 2ef489e2 Leszek Koltunski
    switch(numLayers)
671
      {
672
      case 2: return R.string.diam2;
673
      case 3: return R.string.diam3;
674
      }
675
676
    return 0;
677 6fd4a72c Leszek Koltunski
    }
678
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680
681
  public int getInventor(int numLayers)
682
    {
683 2ef489e2 Leszek Koltunski
    switch(numLayers)
684
      {
685
      case 2: return R.string.diam2_inventor;
686
      case 3: return R.string.diam3_inventor;
687
      }
688
689
    return 0;
690 6fd4a72c Leszek Koltunski
    }
691
692
///////////////////////////////////////////////////////////////////////////////////////////////////
693
694
  public int getComplexity(int numLayers)
695
    {
696 2ef489e2 Leszek Koltunski
    switch(numLayers)
697
      {
698
      case 2: return 5;
699
      case 3: return 7;
700
      }
701
702
    return 0;
703 6fd4a72c Leszek Koltunski
    }
704 ece1b58d Leszek Koltunski
}