Project

General

Profile

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

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

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
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.helpers.FactoryCubit;
27
import org.distorted.helpers.FactorySticker;
28
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
import org.distorted.main.R;
36

    
37
import java.util.Random;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
45
  // the four rotation axis of a Diamond. Must be normalized.
46
  static final Static3D[] ROT_AXIS = new Static3D[]
47
         {
48
           new Static3D(+SQ6/3,+SQ3/3,     0),
49
           new Static3D(-SQ6/3,+SQ3/3,     0),
50
           new Static3D(     0,-SQ3/3,-SQ6/3),
51
           new Static3D(     0,-SQ3/3,+SQ6/3)
52
         };
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 MeshBase[] mMeshes;
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
144
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
145
    {
146
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  double[][] getVertices(int cubitType)
152
    {
153
    if( cubitType==0 ) return VERTICES_OCTA;
154
    if( cubitType==1 ) return VERTICES_TETRA;
155
    return null;
156
    }
157

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

    
160
  int[][] getVertIndexes(int cubitType)
161
    {
162
    if( cubitType==0 ) return VERT_INDEXES_OCTA;
163
    if( cubitType==1 ) return VERT_INDEXES_TETRA;
164
    return null;
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  int getNumCubitTypes(int numLayers)
170
    {
171
    return 2;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  float getScreenRatio()
177
    {
178
    return 0.65f;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  Static4D[] getQuats()
184
    {
185
    return QUATS;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  int getNumFaces()
191
    {
192
    return FACE_COLORS.length;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  boolean shouldResetTextureMaps()
198
    {
199
    return false;
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  int getNumStickerTypes(int numLayers)
205
    {
206
    return STICKERS.length;
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  float[] getCuts(int numLayers)
212
    {
213
    if( numLayers<2 )
214
      {
215
      return null;
216
      }
217
    else
218
      {
219
      float[] cuts = new float[numLayers-1];
220
      float dist = SQ6*0.666f*DIST;
221
      float cut  = 0.5f*dist*(2-numLayers);
222

    
223
      for(int i=0; i<numLayers-1; i++)
224
        {
225
        cuts[i] = cut;
226
        cut += dist;
227
        }
228

    
229
      return cuts;
230
      }
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  int getNumCubitFaces()
236
    {
237
    return FACES_PER_CUBIT;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  private int getNumOctahedrons(int layers)
243
    {
244
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  private int getNumTetrahedrons(int layers)
250
    {
251
    return 4*layers*(layers-1);
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
257
    {
258
    float x = DIST*(layers-1);
259
    float z = DIST*(layers+1);
260

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

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

    
277
    for(int i=0; i<layers-1; i++, index++)
278
      {
279
      z += 2*DIST;
280
      centers[index][0] = x;
281
      centers[index][1] = height;
282
      centers[index][2] = z;
283
      }
284

    
285
    for(int i=0; i<layers-2; i++, index++)
286
      {
287
      x += 2*DIST;
288
      centers[index][0] = x;
289
      centers[index][1] = height;
290
      centers[index][2] = z;
291
      }
292

    
293
    return index;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
299
    {
300
    float x = DIST*(layers-1);
301
    float z = DIST*layers;
302

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

    
311
    x += DIST;
312
    z -= DIST;
313

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

    
322
    x -= DIST;
323
    z -= DIST;
324

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

    
333
    x -= DIST;
334
    z += DIST;
335

    
336
    for(int i=0; i<layers-1; i++, index++)
337
      {
338
      x += 2*DIST;
339
      centers[index][0] = x;
340
      centers[index][1] = height;
341
      centers[index][2] = z;
342
      }
343

    
344
    return index;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
  float[][] getCubitPositions(int layers)
350
    {
351
    int numO = getNumOctahedrons(layers);
352
    int numT = getNumTetrahedrons(layers);
353
    int index = 0;
354
    float height = 0.0f;
355

    
356
    float[][] CENTERS = new float[numO+numT][3];
357

    
358
    index = createOctaPositions(CENTERS,index,layers,height);
359

    
360
    for(int i=layers-1; i>0; i--)
361
      {
362
      height += SQ2*DIST;
363
      index = createOctaPositions(CENTERS,index,i,+height);
364
      index = createOctaPositions(CENTERS,index,i,-height);
365
      }
366

    
367
    height = DIST*SQ2/2;
368

    
369
    for(int i=layers; i>1; i--)
370
      {
371
      index = createTetraPositions(CENTERS,index,i,+height);
372
      index = createTetraPositions(CENTERS,index,i,-height);
373
      height += SQ2*DIST;
374
      }
375

    
376
    return CENTERS;
377
    }
378

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

    
381
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
382
    {
383
    for(int i=numLayers-1; i>0; i--)
384
      {
385
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
386
      tetra -= 8*i;
387
      }
388

    
389
    return -1;
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  private Static4D getQuat(int cubit, int numLayers, int numO)
395
    {
396
    if( cubit<numO ) return QUATS[0];
397

    
398
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
399
      {
400
      case 0: return QUATS[0];                          // unit quat
401
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
402
      case 2: return QUATS[1];                          // 180 along Y
403
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
404
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
405
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
406
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
407
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
408
      }
409

    
410
    return null;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  MeshBase createCubitMesh(int cubit, int numLayers)
416
    {
417
    if( mMeshes==null )
418
      {
419
      FactoryCubit factory = FactoryCubit.getInstance();
420
      factory.clear();
421
      mMeshes = new MeshBase[2];
422
      }
423

    
424
    MeshBase mesh;
425
    int numO = getNumOctahedrons(numLayers);
426

    
427
    if( cubit<numO )
428
      {
429
      if( mMeshes[0]==null )
430
        {
431
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
432
        int[] bandIndexes   = new int[] { 0,0,0,0,0,0,0,0 };
433
        float[][] corners   = new float[][] { {0.04f,0.20f} };
434
        int[] cornerIndexes = new int[] { 0,0,0,0,0,0 };
435
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
436
        int[] centerIndexes = new int[] { 0,0,0,0,0,0 };
437

    
438
        FactoryCubit factory = FactoryCubit.getInstance();
439

    
440
        factory.createNewFaceTransform(VERTICES_OCTA,VERT_INDEXES_OCTA);
441
        mMeshes[0] = factory.createRoundedSolid(VERTICES_OCTA, VERT_INDEXES_OCTA,
442
                                                bands, bandIndexes,
443
                                                corners, cornerIndexes,
444
                                                centers, centerIndexes,
445
                                                getNumCubitFaces() );
446
        }
447
      mesh = mMeshes[0].copy(true);
448
      }
449
    else
450
      {
451
      if( mMeshes[1]==null )
452
        {
453
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
454
        int[] bandIndexes   = new int[] { 0,0,0,0 };
455
        float[][] corners   = new float[][] { {0.08f,0.15f} };
456
        int[] cornerIndexes = new int[] { 0,0,0,0 };
457
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
458
        int[] centerIndexes = new int[] { 0,0,0,0 };
459

    
460
        FactoryCubit factory = FactoryCubit.getInstance();
461

    
462
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
463
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
464
                                                bands, bandIndexes,
465
                                                corners, cornerIndexes,
466
                                                centers, centerIndexes,
467
                                                getNumCubitFaces() );
468
        }
469
      mesh = mMeshes[1].copy(true);
470
      }
471

    
472
    Static4D sQ = getQuat(cubit,numLayers,numO);
473
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( sQ, new Static3D(0,0,0) );
474
    mesh.apply(quat,0xffffffff,0);
475

    
476
    return mesh;
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
  int getFaceColor(int cubit, int cubitface, int size)
482
    {
483
    int numO = getNumOctahedrons(size);
484

    
485
    if( cubit<numO )
486
      {
487
      int axis = 0;
488
      int layer= 1;
489

    
490
      switch(cubitface)
491
        {
492
        case 0: axis = 2; layer =             1; break;
493
        case 1: axis = 0; layer = (1<<(size-1)); break;
494
        case 2: axis = 3; layer =             1; break;
495
        case 3: axis = 1; layer = (1<<(size-1)); break;
496
        case 4: axis = 3; layer = (1<<(size-1)); break;
497
        case 5: axis = 1; layer =             1; break;
498
        case 6: axis = 2; layer = (1<<(size-1)); break;
499
        case 7: axis = 0; layer =             1; break;
500
        }
501

    
502
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
503
      }
504
    else
505
      {
506
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
507
      }
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
513
    {
514
    float R = 0.06f;
515
    float S = 0.07f;
516

    
517
    FactorySticker factory = FactorySticker.getInstance();
518
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
519
    }
520

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522

    
523
  float returnMultiplier()
524
    {
525
    return 1.5f;
526
    }
527

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

    
530
  float[] getRowChances(int numLayers)
531
    {
532
    float[] chances = new float[numLayers];
533

    
534
    for(int i=0; i<numLayers; i++) chances[i] = ((float)(i+1))/numLayers;
535

    
536
    return chances;
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540
// PUBLIC API
541

    
542
  public Static3D[] getRotationAxis()
543
    {
544
    return ROT_AXIS;
545
    }
546

    
547
///////////////////////////////////////////////////////////////////////////////////////////////////
548

    
549
  public int getBasicAngle()
550
    {
551
    return 3;
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555

    
556
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
557
    {
558
    if( num==0 )
559
      {
560
      scramble[num][0] = rnd.nextInt(ROTATION_AXIS.length);
561
      }
562
    else
563
      {
564
      int newVector = rnd.nextInt(ROTATION_AXIS.length-1);
565
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
566
      }
567

    
568
    float rowFloat = rnd.nextFloat();
569

    
570
    for(int row=0; row<mRowChances.length; row++)
571
      {
572
      if( rowFloat<=mRowChances[row] )
573
        {
574
        scramble[num][1] = row;
575
        break;
576
        }
577
      }
578

    
579
    switch( rnd.nextInt(2) )
580
      {
581
      case 0: scramble[num][2] = -1; break;
582
      case 1: scramble[num][2] =  1; break;
583
      }
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587
// The Diamond is solved if and only if:
588
//
589
// 1) all octahedrons are rotated with the same quat
590
// 2) all tetrahedrons might be also optionally rotated by a 'face neutral' pair of quats
591
//    (indexes of those are kept in the 'mFaceNeutralQuattIndex' table)
592
//
593
// Note: this works for any size, because even if layers>3 - and then there are 'face-internal'
594
// octahedrons which, it would seem, can be rotated by those 'face neutral' pairs of quats - but
595
// in reality no, because if they were, the octahedrons would then not fit in the lattice...
596

    
597
  public boolean isSolved()
598
    {
599
    int q = CUBITS[0].mQuatIndex;
600
    int layers = getNumLayers();
601
    int numO = getNumOctahedrons(layers);
602

    
603
    for(int i=1; i<numO; i++)
604
      {
605
      if( CUBITS[i].mQuatIndex != q ) return false;
606
      }
607

    
608
    int qI, q1Index, q2Index, face;
609

    
610
    for(int i=numO; i<NUM_CUBITS; i++)
611
      {
612
      face    = retFaceTetraBelongsTo(i-numO,layers);
613
      q1Index = mFaceNeutralQuatIndex[face][0];
614
      q2Index = mFaceNeutralQuatIndex[face][1];
615
      qI      = CUBITS[i].mQuatIndex;
616

    
617
      if(  qI != q && qI != mulQuat(q,q1Index) && qI != mulQuat(q,q2Index) ) return false;
618
      }
619

    
620
    return true;
621
    }
622

    
623
///////////////////////////////////////////////////////////////////////////////////////////////////
624
// only needed for solvers - there are no Diamond solvers ATM
625

    
626
  public String retObjectString()
627
    {
628
    return "";
629
    }
630

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632

    
633
  public int getObjectName(int numLayers)
634
    {
635
    switch(numLayers)
636
      {
637
      case 2: return R.string.diam2;
638
      case 3: return R.string.diam3;
639
      }
640

    
641
    return 0;
642
    }
643

    
644
///////////////////////////////////////////////////////////////////////////////////////////////////
645

    
646
  public int getInventor(int numLayers)
647
    {
648
    switch(numLayers)
649
      {
650
      case 2: return R.string.diam2_inventor;
651
      case 3: return R.string.diam3_inventor;
652
      }
653

    
654
    return 0;
655
    }
656

    
657
///////////////////////////////////////////////////////////////////////////////////////////////////
658

    
659
  public int getComplexity(int numLayers)
660
    {
661
    switch(numLayers)
662
      {
663
      case 2: return 5;
664
      case 3: return 7;
665
      }
666

    
667
    return 0;
668
    }
669
}
(20-20/33)