Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 91dff543

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[] BASIC_ANGLE = new int[] { 3,3,3,3 };
55

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

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

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

    
82
  private static final float DIST = 0.50f;
83

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

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

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

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

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

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

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

    
141
  private static MeshBase[] mMeshes;
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  float getScreenRatio()
154
    {
155
    return 0.65f;
156
    }
157

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

    
160
  Static4D[] getQuats()
161
    {
162
    return QUATS;
163
    }
164

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

    
167
  int getNumFaces()
168
    {
169
    return FACE_COLORS.length;
170
    }
171

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

    
174
  boolean shouldResetTextureMaps()
175
    {
176
    return false;
177
    }
178

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

    
181
  int getNumStickerTypes(int numLayers)
182
    {
183
    return STICKERS.length;
184
    }
185

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

    
188
  float[][] getCuts(int numLayers)
189
    {
190
    if( numLayers<2 )
191
      {
192
      return null;
193
      }
194
    else
195
      {
196
      float[][] cuts = new float[4][numLayers-1];
197
      float dist = SQ6*0.666f*DIST;
198
      float cut  = 0.5f*dist*(2-numLayers);
199

    
200
      for(int i=0; i<numLayers-1; i++)
201
        {
202
        cuts[0][i] = cut;
203
        cuts[1][i] = cut;
204
        cuts[2][i] = cut;
205
        cuts[3][i] = cut;
206
        cut += dist;
207
        }
208

    
209
      return cuts;
210
      }
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  int getNumCubitFaces()
216
    {
217
    return FACES_PER_CUBIT;
218
    }
219

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

    
222
  private int getNumOctahedrons(int layers)
223
    {
224
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
225
    }
226

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

    
229
  private int getNumTetrahedrons(int layers)
230
    {
231
    return 4*layers*(layers-1);
232
    }
233

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

    
236
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
237
    {
238
    float x = DIST*(layers-1);
239
    float z = DIST*(layers+1);
240

    
241
    for(int i=0; i<layers; i++, index++)
242
      {
243
      z -= 2*DIST;
244
      centers[index][0] = x;
245
      centers[index][1] = height;
246
      centers[index][2] = z;
247
      }
248

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

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

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

    
273
    return index;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
279
    {
280
    float x = DIST*(layers-1);
281
    float z = DIST*layers;
282

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

    
291
    x += DIST;
292
    z -= DIST;
293

    
294
    for(int i=0; i<layers-1; i++, index++)
295
      {
296
      x -= 2*DIST;
297
      centers[index][0] = x;
298
      centers[index][1] = height;
299
      centers[index][2] = z;
300
      }
301

    
302
    x -= DIST;
303
    z -= DIST;
304

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

    
313
    x -= DIST;
314
    z += DIST;
315

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

    
324
    return index;
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  float[][] getCubitPositions(int layers)
330
    {
331
    int numO = getNumOctahedrons(layers);
332
    int numT = getNumTetrahedrons(layers);
333
    int index = 0;
334
    float height = 0.0f;
335

    
336
    float[][] CENTERS = new float[numO+numT][3];
337

    
338
    index = createOctaPositions(CENTERS,index,layers,height);
339

    
340
    for(int i=layers-1; i>0; i--)
341
      {
342
      height += SQ2*DIST;
343
      index = createOctaPositions(CENTERS,index,i,+height);
344
      index = createOctaPositions(CENTERS,index,i,-height);
345
      }
346

    
347
    height = DIST*SQ2/2;
348

    
349
    for(int i=layers; i>1; i--)
350
      {
351
      index = createTetraPositions(CENTERS,index,i,+height);
352
      index = createTetraPositions(CENTERS,index,i,-height);
353
      height += SQ2*DIST;
354
      }
355

    
356
    return CENTERS;
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
362
    {
363
    for(int i=numLayers-1; i>0; i--)
364
      {
365
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
366
      tetra -= 8*i;
367
      }
368

    
369
    return -1;
370
    }
371

    
372
///////////////////////////////////////////////////////////////////////////////////////////////////
373

    
374
  private Static4D getQuat(int cubit, int numLayers, int numO)
375
    {
376
    if( cubit<numO ) return QUATS[0];
377

    
378
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
379
      {
380
      case 0: return QUATS[0];                          // unit quat
381
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
382
      case 2: return QUATS[1];                          // 180 along Y
383
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
384
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
385
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
386
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
387
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
388
      }
389

    
390
    return null;
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  MeshBase createCubitMesh(int cubit, int numLayers)
396
    {
397
    if( mMeshes==null )
398
      {
399
      FactoryCubit factory = FactoryCubit.getInstance();
400
      factory.clear();
401
      mMeshes = new MeshBase[2];
402
      }
403

    
404
    MeshBase mesh;
405
    int numO = getNumOctahedrons(numLayers);
406

    
407
    if( cubit<numO )
408
      {
409
      if( mMeshes[0]==null )
410
        {
411
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
412
        int[] bandIndexes   = new int[] { 0,0,0,0,0,0,0,0 };
413
        float[][] corners   = new float[][] { {0.04f,0.20f} };
414
        int[] cornerIndexes = new int[] { 0,0,0,0,0,0 };
415
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
416
        int[] centerIndexes = new int[] { 0,0,0,0,0,0 };
417

    
418
        FactoryCubit factory = FactoryCubit.getInstance();
419

    
420
        factory.createNewFaceTransform(VERTICES_OCTA,VERT_INDEXES_OCTA);
421
        mMeshes[0] = factory.createRoundedSolid(VERTICES_OCTA, VERT_INDEXES_OCTA,
422
                                                bands, bandIndexes,
423
                                                corners, cornerIndexes,
424
                                                centers, centerIndexes,
425
                                                getNumCubitFaces() );
426
        }
427
      mesh = mMeshes[0].copy(true);
428
      }
429
    else
430
      {
431
      if( mMeshes[1]==null )
432
        {
433
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
434
        int[] bandIndexes   = new int[] { 0,0,0,0 };
435
        float[][] corners   = new float[][] { {0.08f,0.15f} };
436
        int[] cornerIndexes = new int[] { 0,0,0,0 };
437
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
438
        int[] centerIndexes = new int[] { 0,0,0,0 };
439

    
440
        FactoryCubit factory = FactoryCubit.getInstance();
441

    
442
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
443
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
444
                                                bands, bandIndexes,
445
                                                corners, cornerIndexes,
446
                                                centers, centerIndexes,
447
                                                getNumCubitFaces() );
448
        }
449
      mesh = mMeshes[1].copy(true);
450
      }
451

    
452
    Static4D sQ = getQuat(cubit,numLayers,numO);
453
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( sQ, new Static3D(0,0,0) );
454
    mesh.apply(quat,0xffffffff,0);
455

    
456
    return mesh;
457
    }
458

    
459
///////////////////////////////////////////////////////////////////////////////////////////////////
460

    
461
  int getFaceColor(int cubit, int cubitface, int size)
462
    {
463
    int numO = getNumOctahedrons(size);
464

    
465
    if( cubit<numO )
466
      {
467
      int axis = 0;
468
      int layer= 1;
469

    
470
      switch(cubitface)
471
        {
472
        case 0: axis = 2; layer =             1; break;
473
        case 1: axis = 0; layer = (1<<(size-1)); break;
474
        case 2: axis = 3; layer =             1; break;
475
        case 3: axis = 1; layer = (1<<(size-1)); break;
476
        case 4: axis = 3; layer = (1<<(size-1)); break;
477
        case 5: axis = 1; layer =             1; break;
478
        case 6: axis = 2; layer = (1<<(size-1)); break;
479
        case 7: axis = 0; layer =             1; break;
480
        }
481

    
482
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
483
      }
484
    else
485
      {
486
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
487
      }
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

    
492
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
493
    {
494
    float R = 0.06f;
495
    float S = 0.07f;
496

    
497
    FactorySticker factory = FactorySticker.getInstance();
498
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502

    
503
  float returnMultiplier()
504
    {
505
    return 1.5f;
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509
// PUBLIC API
510

    
511
  public Static3D[] getRotationAxis()
512
    {
513
    return ROT_AXIS;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  public int[] getBasicAngle()
519
    {
520
    return BASIC_ANGLE;
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
526
    {
527
    if( curr==0 )
528
      {
529
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
530
      }
531
    else
532
      {
533
      int newVector = rnd.nextInt(NUM_AXIS -1);
534
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
535
      }
536

    
537
    float rowFloat = rnd.nextFloat();
538
    int numLayers = getNumLayers();
539

    
540
    for(int row=0; row<numLayers; row++)
541
      {
542
      if( rowFloat*numLayers <= row+1 )
543
        {
544
        scramble[curr][1] = row;
545
        break;
546
        }
547
      }
548

    
549
    switch( rnd.nextInt(2) )
550
      {
551
      case 0: scramble[curr][2] = -1; break;
552
      case 1: scramble[curr][2] =  1; break;
553
      }
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
// The Diamond is solved if and only if:
558
//
559
// 1) all octahedrons are rotated with the same quat
560
// 2) all tetrahedrons might be also optionally rotated by a 'face neutral' pair of quats
561
//    (indexes of those are kept in the 'mFaceNeutralQuattIndex' table)
562
//
563
// Note: this works for any size, because even if layers>3 - and then there are 'face-internal'
564
// octahedrons which, it would seem, can be rotated by those 'face neutral' pairs of quats - but
565
// in reality no, because if they were, the octahedrons would then not fit in the lattice...
566

    
567
  public boolean isSolved()
568
    {
569
    int q = CUBITS[0].mQuatIndex;
570
    int layers = getNumLayers();
571
    int numO = getNumOctahedrons(layers);
572

    
573
    for(int i=1; i<numO; i++)
574
      {
575
      if( CUBITS[i].mQuatIndex != q ) return false;
576
      }
577

    
578
    int qI, q1Index, q2Index, face;
579

    
580
    for(int i=numO; i<NUM_CUBITS; i++)
581
      {
582
      face    = retFaceTetraBelongsTo(i-numO,layers);
583
      q1Index = mFaceNeutralQuatIndex[face][0];
584
      q2Index = mFaceNeutralQuatIndex[face][1];
585
      qI      = CUBITS[i].mQuatIndex;
586

    
587
      if(  qI != q && qI != mulQuat(q,q1Index) && qI != mulQuat(q,q2Index) ) return false;
588
      }
589

    
590
    return true;
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594
// only needed for solvers - there are no Diamond solvers ATM
595

    
596
  public String retObjectString()
597
    {
598
    return "";
599
    }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

    
603
  public int getObjectName(int numLayers)
604
    {
605
    switch(numLayers)
606
      {
607
      case 2: return R.string.diam2;
608
      case 3: return R.string.diam3;
609
      }
610

    
611
    return 0;
612
    }
613

    
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615

    
616
  public int getInventor(int numLayers)
617
    {
618
    switch(numLayers)
619
      {
620
      case 2: return R.string.diam2_inventor;
621
      case 3: return R.string.diam3_inventor;
622
      }
623

    
624
    return 0;
625
    }
626

    
627
///////////////////////////////////////////////////////////////////////////////////////////////////
628

    
629
  public int getComplexity(int numLayers)
630
    {
631
    switch(numLayers)
632
      {
633
      case 2: return 5;
634
      case 3: return 7;
635
      }
636

    
637
    return 0;
638
    }
639
}
(23-23/41)