Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 7d8cc029

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
  float getScreenRatio()
152
    {
153
    return 0.65f;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  Static4D[] getQuats()
159
    {
160
    return QUATS;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  int getNumFaces()
166
    {
167
    return FACE_COLORS.length;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  boolean shouldResetTextureMaps()
173
    {
174
    return false;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  int getNumStickerTypes(int numLayers)
180
    {
181
    return STICKERS.length;
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

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

    
198
      for(int i=0; i<numLayers-1; i++)
199
        {
200
        cuts[i] = cut;
201
        cut += dist;
202
        }
203

    
204
      return cuts;
205
      }
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  int getNumCubitFaces()
211
    {
212
    return FACES_PER_CUBIT;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  private int getNumOctahedrons(int layers)
218
    {
219
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private int getNumTetrahedrons(int layers)
225
    {
226
    return 4*layers*(layers-1);
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
232
    {
233
    float x = DIST*(layers-1);
234
    float z = DIST*(layers+1);
235

    
236
    for(int i=0; i<layers; i++, index++)
237
      {
238
      z -= 2*DIST;
239
      centers[index][0] = x;
240
      centers[index][1] = height;
241
      centers[index][2] = z;
242
      }
243

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

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

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

    
268
    return index;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
274
    {
275
    float x = DIST*(layers-1);
276
    float z = DIST*layers;
277

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

    
286
    x += DIST;
287
    z -= DIST;
288

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

    
297
    x -= DIST;
298
    z -= DIST;
299

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

    
308
    x -= DIST;
309
    z += DIST;
310

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

    
319
    return index;
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  float[][] getCubitPositions(int layers)
325
    {
326
    int numO = getNumOctahedrons(layers);
327
    int numT = getNumTetrahedrons(layers);
328
    int index = 0;
329
    float height = 0.0f;
330

    
331
    float[][] CENTERS = new float[numO+numT][3];
332

    
333
    index = createOctaPositions(CENTERS,index,layers,height);
334

    
335
    for(int i=layers-1; i>0; i--)
336
      {
337
      height += SQ2*DIST;
338
      index = createOctaPositions(CENTERS,index,i,+height);
339
      index = createOctaPositions(CENTERS,index,i,-height);
340
      }
341

    
342
    height = DIST*SQ2/2;
343

    
344
    for(int i=layers; i>1; i--)
345
      {
346
      index = createTetraPositions(CENTERS,index,i,+height);
347
      index = createTetraPositions(CENTERS,index,i,-height);
348
      height += SQ2*DIST;
349
      }
350

    
351
    return CENTERS;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

    
356
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
357
    {
358
    for(int i=numLayers-1; i>0; i--)
359
      {
360
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
361
      tetra -= 8*i;
362
      }
363

    
364
    return -1;
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  private Static4D getQuat(int cubit, int numLayers, int numO)
370
    {
371
    if( cubit<numO ) return QUATS[0];
372

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

    
385
    return null;
386
    }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
  MeshBase createCubitMesh(int cubit, int numLayers)
391
    {
392
    if( mMeshes==null )
393
      {
394
      FactoryCubit factory = FactoryCubit.getInstance();
395
      factory.clear();
396
      mMeshes = new MeshBase[2];
397
      }
398

    
399
    MeshBase mesh;
400
    int numO = getNumOctahedrons(numLayers);
401

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

    
413
        FactoryCubit factory = FactoryCubit.getInstance();
414

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

    
435
        FactoryCubit factory = FactoryCubit.getInstance();
436

    
437
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
438
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
439
                                                bands, bandIndexes,
440
                                                corners, cornerIndexes,
441
                                                centers, centerIndexes,
442
                                                getNumCubitFaces() );
443
        }
444
      mesh = mMeshes[1].copy(true);
445
      }
446

    
447
    Static4D sQ = getQuat(cubit,numLayers,numO);
448
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( sQ, new Static3D(0,0,0) );
449
    mesh.apply(quat,0xffffffff,0);
450

    
451
    return mesh;
452
    }
453

    
454
///////////////////////////////////////////////////////////////////////////////////////////////////
455

    
456
  int getFaceColor(int cubit, int cubitface, int size)
457
    {
458
    int numO = getNumOctahedrons(size);
459

    
460
    if( cubit<numO )
461
      {
462
      int axis = 0;
463
      int layer= 1;
464

    
465
      switch(cubitface)
466
        {
467
        case 0: axis = 2; layer =             1; break;
468
        case 1: axis = 0; layer = (1<<(size-1)); break;
469
        case 2: axis = 3; layer =             1; break;
470
        case 3: axis = 1; layer = (1<<(size-1)); break;
471
        case 4: axis = 3; layer = (1<<(size-1)); break;
472
        case 5: axis = 1; layer =             1; break;
473
        case 6: axis = 2; layer = (1<<(size-1)); break;
474
        case 7: axis = 0; layer =             1; break;
475
        }
476

    
477
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
478
      }
479
    else
480
      {
481
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
482
      }
483
    }
484

    
485
///////////////////////////////////////////////////////////////////////////////////////////////////
486

    
487
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
488
    {
489
    float R = 0.06f;
490
    float S = 0.07f;
491

    
492
    FactorySticker factory = FactorySticker.getInstance();
493
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  float returnMultiplier()
499
    {
500
    return 1.5f;
501
    }
502

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

    
505
  float[] getRowChances(int numLayers)
506
    {
507
    float[] chances = new float[numLayers];
508

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

    
511
    return chances;
512
    }
513

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515
// PUBLIC API
516

    
517
  public Static3D[] getRotationAxis()
518
    {
519
    return ROT_AXIS;
520
    }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

    
524
  public int getBasicAngle()
525
    {
526
    return 3;
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
532
    {
533
    if( num==0 )
534
      {
535
      scramble[num][0] = rnd.nextInt(ROTATION_AXIS.length);
536
      }
537
    else
538
      {
539
      int newVector = rnd.nextInt(ROTATION_AXIS.length-1);
540
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
541
      }
542

    
543
    float rowFloat = rnd.nextFloat();
544

    
545
    for(int row=0; row<mRowChances.length; row++)
546
      {
547
      if( rowFloat<=mRowChances[row] )
548
        {
549
        scramble[num][1] = row;
550
        break;
551
        }
552
      }
553

    
554
    switch( rnd.nextInt(2) )
555
      {
556
      case 0: scramble[num][2] = -1; break;
557
      case 1: scramble[num][2] =  1; break;
558
      }
559
    }
560

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

    
572
  public boolean isSolved()
573
    {
574
    int q = CUBITS[0].mQuatIndex;
575
    int layers = getNumLayers();
576
    int numO = getNumOctahedrons(layers);
577

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

    
583
    int qI, q1Index, q2Index, face;
584

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

    
592
      if(  qI != q && qI != mulQuat(q,q1Index) && qI != mulQuat(q,q2Index) ) return false;
593
      }
594

    
595
    return true;
596
    }
597

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599
// only needed for solvers - there are no Diamond solvers ATM
600

    
601
  public String retObjectString()
602
    {
603
    return "";
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////
607

    
608
  public int getObjectName(int numLayers)
609
    {
610
    switch(numLayers)
611
      {
612
      case 2: return R.string.diam2;
613
      case 3: return R.string.diam3;
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
      }
628

    
629
    return 0;
630
    }
631

    
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633

    
634
  public int getComplexity(int numLayers)
635
    {
636
    switch(numLayers)
637
      {
638
      case 2: return 5;
639
      case 3: return 7;
640
      }
641

    
642
    return 0;
643
    }
644
}
(20-20/33)