Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objects;
21

    
22
import android.content.res.Resources;
23

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

    
36
import java.util.Random;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

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

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

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

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

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

    
81
  private static final float DIST = 0.50f;
82

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

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

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

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

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

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

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

    
140
  private static final ObjectSticker[] mStickers;
141

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

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

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

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

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

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

    
217
      return cuts;
218
      }
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

    
249
    for(int i=0; i<layers; i++, index++)
250
      {
251
      z -= 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
      x -= 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-1; i++, index++)
266
      {
267
      z += 2*DIST;
268
      centers[index][0] = x;
269
      centers[index][1] = height;
270
      centers[index][2] = z;
271
      }
272

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

    
281
    return index;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

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

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

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

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

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

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

    
332
    return index;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

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

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

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

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

    
355
    height = DIST*SQ2/2;
356

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

    
364
    return CENTERS;
365
    }
366

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

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

    
377
    return -1;
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
  private Static4D getQuat(int cubit, int numLayers)
383
    {
384
    int numO = getNumOctahedrons(numLayers);
385

    
386
    if( cubit<numO ) return QUATS[0];
387

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

    
400
    return null;
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  private int getNumCubitVariants(int numLayers)
406
    {
407
    return 2;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

    
412
  int getCubitVariant(int cubit, int numLayers)
413
    {
414
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  ObjectShape getObjectShape(int cubit, int numLayers)
420
    {
421
    int variant = getCubitVariant(cubit,numLayers);
422
    int N = numLayers>3 ? 5:6;
423
    int E = numLayers>3 ? 1:2;
424

    
425
    if( variant==0 )
426
      {
427
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
428
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
429
      float[][] corners   = new float[][] { {0.04f,0.20f} };
430
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
431
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
432
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
433
      return new ObjectShape(VERTICES_OCTA,VERT_INDEXES_OCTA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
434
      }
435
    else
436
      {
437
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
438
      int[] bandIndices   = new int[] { 0,0,0,0 };
439
      float[][] corners   = new float[][] { {0.08f,0.15f} };
440
      int[] cornerIndices = new int[] { 0,0,0,0 };
441
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
442
      int[] centerIndices = new int[] { 0,0,0,0 };
443
      return new ObjectShape(VERTICES_TETRA,VERT_INDEXES_TETRA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
444
      }
445
    }
446

    
447
///////////////////////////////////////////////////////////////////////////////////////////////////
448

    
449
  MeshBase createCubitMesh(int cubit, int numLayers)
450
    {
451
    int variant = getCubitVariant(cubit,numLayers);
452

    
453
    if( mMeshes==null )
454
      {
455
      FactoryCubit factory = FactoryCubit.getInstance();
456
      factory.clear();
457
      mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
458
      }
459

    
460
    if( mMeshes[variant]==null )
461
      {
462
      ObjectShape shape = getObjectShape(cubit,numLayers);
463
      FactoryCubit factory = FactoryCubit.getInstance();
464
      factory.createNewFaceTransform(shape);
465
      mMeshes[variant] = factory.createRoundedSolid(shape);
466
      }
467

    
468
    MeshBase mesh = mMeshes[variant].copy(true);
469
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
470
    mesh.apply(quat,0xffffffff,0);
471

    
472
    return mesh;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  int getFaceColor(int cubit, int cubitface, int size)
478
    {
479
    int numO = getNumOctahedrons(size);
480

    
481
    if( cubit<numO )
482
      {
483
      int axis = 0;
484
      int layer= 1;
485

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

    
498
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
499
      }
500
    else
501
      {
502
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
503
      }
504
    }
505

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

    
508
  int getColor(int face)
509
    {
510
    return FACE_COLORS[face];
511
    }
512

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

    
515
  ObjectSticker retSticker(int face)
516
    {
517
    return mStickers[face/NUM_FACES];
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

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

    
527
///////////////////////////////////////////////////////////////////////////////////////////////////
528
// PUBLIC API
529

    
530
  public Static3D[] getRotationAxis()
531
    {
532
    return ROT_AXIS;
533
    }
534

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

    
537
  public int[] getBasicAngle()
538
    {
539
    return BASIC_ANGLE;
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543

    
544
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
545
    {
546
    if( curr==0 )
547
      {
548
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
549
      }
550
    else
551
      {
552
      int newVector = rnd.nextInt(NUM_AXIS -1);
553
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
554
      }
555

    
556
    float rowFloat = rnd.nextFloat();
557
    int numLayers = getNumLayers();
558

    
559
    for(int row=0; row<numLayers; row++)
560
      {
561
      if( rowFloat*numLayers <= row+1 )
562
        {
563
        scramble[curr][1] = row;
564
        break;
565
        }
566
      }
567

    
568
    switch( rnd.nextInt(2) )
569
      {
570
      case 0: scramble[curr][2] = -1; break;
571
      case 1: scramble[curr][2] =  1; break;
572
      }
573
    }
574

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

    
586
  public boolean isSolved()
587
    {
588
    int q = CUBITS[0].mQuatIndex;
589
    int layers = getNumLayers();
590
    int numO = getNumOctahedrons(layers);
591

    
592
    for(int i=1; i<numO; i++)
593
      {
594
      if( CUBITS[i].mQuatIndex != q ) return false;
595
      }
596

    
597
    int qI, q1Index, q2Index, face;
598

    
599
    for(int i=numO; i<NUM_CUBITS; i++)
600
      {
601
      face    = retFaceTetraBelongsTo(i-numO,layers);
602
      q1Index = mFaceNeutralQuatIndex[face][0];
603
      q2Index = mFaceNeutralQuatIndex[face][1];
604
      qI      = CUBITS[i].mQuatIndex;
605

    
606
      if(  qI != q && qI != mulQuat(q,q1Index) && qI != mulQuat(q,q2Index) ) return false;
607
      }
608

    
609
    return true;
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613

    
614
  public int getObjectName(int numLayers)
615
    {
616
    switch(numLayers)
617
      {
618
      case 2: return R.string.diam2;
619
      case 3: return R.string.diam3;
620
      case 4: return R.string.diam4;
621
      }
622

    
623
    return 0;
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627

    
628
  public int getInventor(int numLayers)
629
    {
630
    switch(numLayers)
631
      {
632
      case 2: return R.string.diam2_inventor;
633
      case 3: return R.string.diam3_inventor;
634
      case 4: return R.string.diam4_inventor;
635
      }
636

    
637
    return 0;
638
    }
639

    
640
///////////////////////////////////////////////////////////////////////////////////////////////////
641

    
642
  public int getComplexity(int numLayers)
643
    {
644
    switch(numLayers)
645
      {
646
      case 2: return 4;
647
      case 3: return 6;
648
      case 4: return 8;
649
      }
650

    
651
    return 0;
652
    }
653
}
(23-23/41)