Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ ad7907b0

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.res.Resources;
23

    
24
import org.distorted.helpers.FactoryCubit;
25
import org.distorted.helpers.ObjectSticker;
26
import org.distorted.library.effect.MatrixEffectQuaternion;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.mesh.MeshBase;
30
import org.distorted.library.mesh.MeshSquare;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.main.R;
34

    
35
import java.util.Random;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class TwistyDiamond extends TwistyObject
40
{
41
  private static final int FACES_PER_CUBIT =8;
42

    
43
  // the four rotation axis of a Diamond. Must be normalized.
44
  static final Static3D[] ROT_AXIS = new Static3D[]
45
         {
46
           new Static3D(+SQ6/3,+SQ3/3,     0),
47
           new Static3D(-SQ6/3,+SQ3/3,     0),
48
           new Static3D(     0,-SQ3/3,-SQ6/3),
49
           new Static3D(     0,-SQ3/3,+SQ6/3)
50
         };
51

    
52
  private static final int[] BASIC_ANGLE = new int[] { 3,3,3,3 };
53

    
54
  private static final int[] FACE_COLORS = new int[]
55
         {
56
           COLOR_ORANGE, COLOR_VIOLET,
57
           COLOR_WHITE , COLOR_BLUE  ,
58
           COLOR_YELLOW, COLOR_RED   ,
59
           COLOR_GREEN , COLOR_GREY
60
         };
61

    
62
  // All legal rotation quats of a Diamond: unit + three 180 deg turns + 8 generators
63
  private static final Static4D[] QUATS = new Static4D[]
64
         {
65
           new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
66
           new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
67
           new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
68
           new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
69

    
70
           new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
71
           new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
72
           new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
73
           new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
74
           new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
75
           new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
76
           new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
77
           new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
78
         };
79

    
80
  private static final float DIST = 0.50f;
81

    
82
  private static final int[][] mFaceNeutralQuatIndex = new int[][]
83
         {
84
             {6,10},
85
             {4, 8},
86
             {7,11},
87
             {5, 9},
88
             {7,11},
89
             {5, 9},
90
             {6,10},
91
             {4, 8}
92
         };
93

    
94
  private static final int[] mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
95

    
96
  private static final double[][] VERTICES_TETRA = new double[][]
97
          {
98
             {-0.5, SQ2/4, 0.0},
99
             { 0.5, SQ2/4, 0.0},
100
             { 0.0,-SQ2/4, 0.5},
101
             { 0.0,-SQ2/4,-0.5}
102
          };
103

    
104
  private static final int[][] VERT_INDEXES_TETRA = new int[][]
105
          {
106
             {2,1,0},   // counterclockwise!
107
             {2,3,1},
108
             {3,2,0},
109
             {3,0,1}
110
          };
111

    
112
  private static final double[][] VERTICES_OCTA = new double[][]
113
          {
114
             { 0.5,   0.0, 0.5},
115
             { 0.5,   0.0,-0.5},
116
             {-0.5,   0.0,-0.5},
117
             {-0.5,   0.0, 0.5},
118
             { 0.0, SQ2/2, 0.0},
119
             { 0.0,-SQ2/2, 0.0}
120
          };
121

    
122
  private static final int[][] VERT_INDEXES_OCTA = new int[][]
123
          {
124
             {3,0,4},   // counterclockwise!
125
             {0,1,4},
126
             {1,2,4},
127
             {2,3,4},
128
             {5,0,3},
129
             {5,1,0},
130
             {5,2,1},
131
             {5,3,2}
132
          };
133

    
134
  private static final float[][] STICKERS = new float[][]
135
          {
136
             { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f }
137
          };
138

    
139
  private static final ObjectSticker[] mStickers;
140

    
141
  static
142
    {
143
    float radius = 0.06f;
144
    float stroke = 0.07f;
145
    float[] radii = new float[] {radius,radius,radius};
146
    mStickers = new ObjectSticker[STICKERS.length];
147
    mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke);
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
153
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
154
    {
155
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  float getScreenRatio()
161
    {
162
    return 0.65f;
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  Static4D[] getQuats()
168
    {
169
    return QUATS;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  int getNumFaces()
175
    {
176
    return FACE_COLORS.length;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  boolean shouldResetTextureMaps()
182
    {
183
    return false;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  int getNumStickerTypes(int numLayers)
189
    {
190
    return STICKERS.length;
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  float[][] getCuts(int numLayers)
196
    {
197
    if( numLayers<2 )
198
      {
199
      return null;
200
      }
201
    else
202
      {
203
      float[][] cuts = new float[4][numLayers-1];
204
      float dist = SQ6*0.666f*DIST;
205
      float cut  = 0.5f*dist*(2-numLayers);
206

    
207
      for(int i=0; i<numLayers-1; i++)
208
        {
209
        cuts[0][i] = cut;
210
        cuts[1][i] = cut;
211
        cuts[2][i] = cut;
212
        cuts[3][i] = cut;
213
        cut += dist;
214
        }
215

    
216
      return cuts;
217
      }
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  int getNumCubitFaces()
223
    {
224
    return FACES_PER_CUBIT;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  private int getNumOctahedrons(int layers)
230
    {
231
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  private int getNumTetrahedrons(int layers)
237
    {
238
    return 4*layers*(layers-1);
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
244
    {
245
    float x = DIST*(layers-1);
246
    float z = DIST*(layers+1);
247

    
248
    for(int i=0; i<layers; i++, index++)
249
      {
250
      z -= 2*DIST;
251
      centers[index][0] = x;
252
      centers[index][1] = height;
253
      centers[index][2] = z;
254
      }
255

    
256
    for(int i=0; i<layers-1; i++, index++)
257
      {
258
      x -= 2*DIST;
259
      centers[index][0] = x;
260
      centers[index][1] = height;
261
      centers[index][2] = z;
262
      }
263

    
264
    for(int i=0; i<layers-1; i++, index++)
265
      {
266
      z += 2*DIST;
267
      centers[index][0] = x;
268
      centers[index][1] = height;
269
      centers[index][2] = z;
270
      }
271

    
272
    for(int i=0; i<layers-2; i++, index++)
273
      {
274
      x += 2*DIST;
275
      centers[index][0] = x;
276
      centers[index][1] = height;
277
      centers[index][2] = z;
278
      }
279

    
280
    return index;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
286
    {
287
    float x = DIST*(layers-1);
288
    float z = DIST*layers;
289

    
290
    for(int i=0; i<layers-1; i++, index++)
291
      {
292
      z -= 2*DIST;
293
      centers[index][0] = x;
294
      centers[index][1] = height;
295
      centers[index][2] = z;
296
      }
297

    
298
    x += DIST;
299
    z -= DIST;
300

    
301
    for(int i=0; i<layers-1; i++, index++)
302
      {
303
      x -= 2*DIST;
304
      centers[index][0] = x;
305
      centers[index][1] = height;
306
      centers[index][2] = z;
307
      }
308

    
309
    x -= DIST;
310
    z -= DIST;
311

    
312
    for(int i=0; i<layers-1; i++, index++)
313
      {
314
      z += 2*DIST;
315
      centers[index][0] = x;
316
      centers[index][1] = height;
317
      centers[index][2] = z;
318
      }
319

    
320
    x -= DIST;
321
    z += DIST;
322

    
323
    for(int i=0; i<layers-1; i++, index++)
324
      {
325
      x += 2*DIST;
326
      centers[index][0] = x;
327
      centers[index][1] = height;
328
      centers[index][2] = z;
329
      }
330

    
331
    return index;
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  float[][] getCubitPositions(int layers)
337
    {
338
    int numO = getNumOctahedrons(layers);
339
    int numT = getNumTetrahedrons(layers);
340
    int index = 0;
341
    float height = 0.0f;
342

    
343
    float[][] CENTERS = new float[numO+numT][3];
344

    
345
    index = createOctaPositions(CENTERS,index,layers,height);
346

    
347
    for(int i=layers-1; i>0; i--)
348
      {
349
      height += SQ2*DIST;
350
      index = createOctaPositions(CENTERS,index,i,+height);
351
      index = createOctaPositions(CENTERS,index,i,-height);
352
      }
353

    
354
    height = DIST*SQ2/2;
355

    
356
    for(int i=layers; i>1; i--)
357
      {
358
      index = createTetraPositions(CENTERS,index,i,+height);
359
      index = createTetraPositions(CENTERS,index,i,-height);
360
      height += SQ2*DIST;
361
      }
362

    
363
    return CENTERS;
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367

    
368
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
369
    {
370
    for(int i=numLayers-1; i>0; i--)
371
      {
372
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
373
      tetra -= 8*i;
374
      }
375

    
376
    return -1;
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  private Static4D getQuat(int cubit, int numLayers, int numO)
382
    {
383
    if( cubit<numO ) return QUATS[0];
384

    
385
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
386
      {
387
      case 0: return QUATS[0];                          // unit quat
388
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
389
      case 2: return QUATS[1];                          // 180 along Y
390
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
391
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
392
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
393
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
394
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
395
      }
396

    
397
    return null;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  MeshBase createCubitMesh(int cubit, int numLayers)
403
    {
404
    if( mMeshes==null )
405
      {
406
      FactoryCubit factory = FactoryCubit.getInstance();
407
      factory.clear();
408
      mMeshes = new MeshBase[2];
409
      }
410

    
411
    MeshBase mesh;
412
    int numO = getNumOctahedrons(numLayers);
413
    int N = numLayers>3 ? 5:6;
414
    int E = numLayers>3 ? 1:2;
415

    
416
    if( cubit<numO )
417
      {
418
      if( mMeshes[0]==null )
419
        {
420
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
421
        int[] bandIndexes   = new int[] { 0,0,0,0,0,0,0,0 };
422
        float[][] corners   = new float[][] { {0.04f,0.20f} };
423
        int[] cornerIndexes = new int[] { 0,0,0,0,0,0 };
424
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
425
        int[] centerIndexes = new int[] { 0,0,0,0,0,0 };
426

    
427
        FactoryCubit factory = FactoryCubit.getInstance();
428

    
429
        factory.createNewFaceTransform(VERTICES_OCTA,VERT_INDEXES_OCTA);
430
        mMeshes[0] = factory.createRoundedSolid(VERTICES_OCTA, VERT_INDEXES_OCTA,
431
                                                bands, bandIndexes,
432
                                                corners, cornerIndexes,
433
                                                centers, centerIndexes,
434
                                                getNumCubitFaces(), null );
435
        }
436
      mesh = mMeshes[0].copy(true);
437
      }
438
    else
439
      {
440
      if( mMeshes[1]==null )
441
        {
442
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
443
        int[] bandIndexes   = new int[] { 0,0,0,0 };
444
        float[][] corners   = new float[][] { {0.08f,0.15f} };
445
        int[] cornerIndexes = new int[] { 0,0,0,0 };
446
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
447
        int[] centerIndexes = new int[] { 0,0,0,0 };
448

    
449
        FactoryCubit factory = FactoryCubit.getInstance();
450

    
451
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
452
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
453
                                                bands, bandIndexes,
454
                                                corners, cornerIndexes,
455
                                                centers, centerIndexes,
456
                                                getNumCubitFaces(), null );
457
        }
458
      mesh = mMeshes[1].copy(true);
459
      }
460

    
461
    Static4D sQ = getQuat(cubit,numLayers,numO);
462
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( sQ, new Static3D(0,0,0) );
463
    mesh.apply(quat,0xffffffff,0);
464

    
465
    return mesh;
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  int getFaceColor(int cubit, int cubitface, int size)
471
    {
472
    int numO = getNumOctahedrons(size);
473

    
474
    if( cubit<numO )
475
      {
476
      int axis = 0;
477
      int layer= 1;
478

    
479
      switch(cubitface)
480
        {
481
        case 0: axis = 2; layer =             1; break;
482
        case 1: axis = 0; layer = (1<<(size-1)); break;
483
        case 2: axis = 3; layer =             1; break;
484
        case 3: axis = 1; layer = (1<<(size-1)); break;
485
        case 4: axis = 3; layer = (1<<(size-1)); break;
486
        case 5: axis = 1; layer =             1; break;
487
        case 6: axis = 2; layer = (1<<(size-1)); break;
488
        case 7: axis = 0; layer =             1; break;
489
        }
490

    
491
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
492
      }
493
    else
494
      {
495
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
496
      }
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  int getColor(int face)
502
    {
503
    return FACE_COLORS[face];
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  ObjectSticker retSticker(int face)
509
    {
510
    return mStickers[face/NUM_FACES];
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
  float returnMultiplier()
516
    {
517
    return 1.5f;
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521
// PUBLIC API
522

    
523
  public Static3D[] getRotationAxis()
524
    {
525
    return ROT_AXIS;
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  public int[] getBasicAngle()
531
    {
532
    return BASIC_ANGLE;
533
    }
534

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
538
    {
539
    if( curr==0 )
540
      {
541
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
542
      }
543
    else
544
      {
545
      int newVector = rnd.nextInt(NUM_AXIS -1);
546
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
547
      }
548

    
549
    float rowFloat = rnd.nextFloat();
550
    int numLayers = getNumLayers();
551

    
552
    for(int row=0; row<numLayers; row++)
553
      {
554
      if( rowFloat*numLayers <= row+1 )
555
        {
556
        scramble[curr][1] = row;
557
        break;
558
        }
559
      }
560

    
561
    switch( rnd.nextInt(2) )
562
      {
563
      case 0: scramble[curr][2] = -1; break;
564
      case 1: scramble[curr][2] =  1; break;
565
      }
566
    }
567

    
568
///////////////////////////////////////////////////////////////////////////////////////////////////
569
// The Diamond is solved if and only if:
570
//
571
// 1) all octahedrons are rotated with the same quat
572
// 2) all tetrahedrons might be also optionally rotated by a 'face neutral' pair of quats
573
//    (indexes of those are kept in the 'mFaceNeutralQuattIndex' table)
574
//
575
// Note: this works for any size, because even if layers>3 - and then there are 'face-internal'
576
// octahedrons which, it would seem, can be rotated by those 'face neutral' pairs of quats - but
577
// in reality no, because if they were, the octahedrons would then not fit in the lattice...
578

    
579
  public boolean isSolved()
580
    {
581
    int q = CUBITS[0].mQuatIndex;
582
    int layers = getNumLayers();
583
    int numO = getNumOctahedrons(layers);
584

    
585
    for(int i=1; i<numO; i++)
586
      {
587
      if( CUBITS[i].mQuatIndex != q ) return false;
588
      }
589

    
590
    int qI, q1Index, q2Index, face;
591

    
592
    for(int i=numO; i<NUM_CUBITS; i++)
593
      {
594
      face    = retFaceTetraBelongsTo(i-numO,layers);
595
      q1Index = mFaceNeutralQuatIndex[face][0];
596
      q2Index = mFaceNeutralQuatIndex[face][1];
597
      qI      = CUBITS[i].mQuatIndex;
598

    
599
      if(  qI != q && qI != mulQuat(q,q1Index) && qI != mulQuat(q,q2Index) ) return false;
600
      }
601

    
602
    return true;
603
    }
604

    
605
///////////////////////////////////////////////////////////////////////////////////////////////////
606

    
607
  public int getObjectName(int numLayers)
608
    {
609
    switch(numLayers)
610
      {
611
      case 2: return R.string.diam2;
612
      case 3: return R.string.diam3;
613
      case 4: return R.string.diam4;
614
      }
615

    
616
    return 0;
617
    }
618

    
619
///////////////////////////////////////////////////////////////////////////////////////////////////
620

    
621
  public int getInventor(int numLayers)
622
    {
623
    switch(numLayers)
624
      {
625
      case 2: return R.string.diam2_inventor;
626
      case 3: return R.string.diam3_inventor;
627
      case 4: return R.string.diam4_inventor;
628
      }
629

    
630
    return 0;
631
    }
632

    
633
///////////////////////////////////////////////////////////////////////////////////////////////////
634

    
635
  public int getComplexity(int numLayers)
636
    {
637
    switch(numLayers)
638
      {
639
      case 2: return 4;
640
      case 3: return 6;
641
      case 4: return 8;
642
      }
643

    
644
    return 0;
645
    }
646
}
(23-23/41)