Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 2adf1263

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
    int N = numLayers>3 ? 5:6;
407
    int E = numLayers>3 ? 1:2;
408

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

    
420
        FactoryCubit factory = FactoryCubit.getInstance();
421

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

    
442
        FactoryCubit factory = FactoryCubit.getInstance();
443

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

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

    
458
    return mesh;
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462

    
463
  int getFaceColor(int cubit, int cubitface, int size)
464
    {
465
    int numO = getNumOctahedrons(size);
466

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

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

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

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

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

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

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
  float returnMultiplier()
506
    {
507
    return 1.5f;
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511
// PUBLIC API
512

    
513
  public Static3D[] getRotationAxis()
514
    {
515
    return ROT_AXIS;
516
    }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

    
520
  public int[] getBasicAngle()
521
    {
522
    return BASIC_ANGLE;
523
    }
524

    
525
///////////////////////////////////////////////////////////////////////////////////////////////////
526

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

    
539
    float rowFloat = rnd.nextFloat();
540
    int numLayers = getNumLayers();
541

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

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

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

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

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

    
580
    int qI, q1Index, q2Index, face;
581

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

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

    
592
    return true;
593
    }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596
// only needed for solvers - there are no Diamond solvers ATM
597

    
598
  public String retObjectString()
599
    {
600
    return "";
601
    }
602

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

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

    
614
    return 0;
615
    }
616

    
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618

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

    
628
    return 0;
629
    }
630

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

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

    
642
    return 0;
643
    }
644
}
(23-23/41)