Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 6cf89a3e

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.ObjectShape;
25
import org.distorted.helpers.ObjectSticker;
26
import org.distorted.helpers.ScrambleState;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.mesh.MeshSquare;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32
import org.distorted.main.R;
33

    
34
import java.util.Random;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

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

    
51
  private static final int[] BASIC_ANGLE = new int[] { 3,3,3,3 };
52

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

    
61
  private static final int[] mFaceMap = new int[] {4,0,6,2,7,3,5,1};
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[] mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
84

    
85
  private static final double[][] VERTICES_TETRA = new double[][]
86
          {
87
             {-0.5, SQ2/4, 0.0},
88
             { 0.5, SQ2/4, 0.0},
89
             { 0.0,-SQ2/4, 0.5},
90
             { 0.0,-SQ2/4,-0.5}
91
          };
92

    
93
  private static final int[][] VERT_INDEXES_TETRA = new int[][]
94
          {
95
             {2,1,0},   // counterclockwise!
96
             {2,3,1},
97
             {3,2,0},
98
             {3,0,1}
99
          };
100

    
101
  private static final double[][] VERTICES_OCTA = new double[][]
102
          {
103
             { 0.5,   0.0, 0.5},
104
             { 0.5,   0.0,-0.5},
105
             {-0.5,   0.0,-0.5},
106
             {-0.5,   0.0, 0.5},
107
             { 0.0, SQ2/2, 0.0},
108
             { 0.0,-SQ2/2, 0.0}
109
          };
110

    
111
  private static final int[][] VERT_INDEXES_OCTA = new int[][]
112
          {
113
             {3,0,4},   // counterclockwise!
114
             {0,1,4},
115
             {1,2,4},
116
             {2,3,4},
117
             {5,0,3},
118
             {5,1,0},
119
             {5,2,1},
120
             {5,3,2}
121
          };
122

    
123
  private static final float[][] STICKERS = new float[][]
124
          {
125
             { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f }
126
          };
127

    
128
  private static final ObjectSticker[] mStickers;
129

    
130
  static
131
    {
132
    float radius = 0.06f;
133
    float stroke = 0.07f;
134
    float[] radii = new float[] {radius,radius,radius};
135
    mStickers = new ObjectSticker[STICKERS.length];
136
    mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke);
137
    }
138

    
139
  private int mCurrState;
140
  private int mIndexExcluded;
141
  private final ScrambleState[] mStates;
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
    int[] tmp = new int[3*2*size];
151

    
152
    for(int i=0; i<2*size; i++)
153
      {
154
      tmp[3*i  ] = (i<size) ?  i:i-size;
155
      tmp[3*i+1] = (i%2==0) ? -1:1;
156
      tmp[3*i+2] = 0;
157
      }
158

    
159
    mStates = new ScrambleState[]
160
      {
161
      new ScrambleState( new int[][] {tmp,tmp,tmp,tmp} )
162
      };
163
    }
164

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

    
167
  int[] getSolvedQuats(int cubit, int numLayers)
168
    {
169
    int status = retCubitSolvedStatus(cubit,numLayers);
170
    return status<0 ? null : buildSolvedQuats(MovementDiamond.FACE_AXIS[mFaceMap[status]],QUATS);
171
    }
172

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

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

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

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

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

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

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

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

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  int getSolvedFunctionIndex()
204
    {
205
    return 0;
206
    }
207

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

    
210
  int getNumStickerTypes(int numLayers)
211
    {
212
    return STICKERS.length;
213
    }
214

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

    
217
  float[][] getCuts(int numLayers)
218
    {
219
    if( numLayers<2 )
220
      {
221
      return null;
222
      }
223
    else
224
      {
225
      float[][] cuts = new float[4][numLayers-1];
226
      float dist = SQ6*0.666f*DIST;
227
      float cut  = 0.5f*dist*(2-numLayers);
228

    
229
      for(int i=0; i<numLayers-1; i++)
230
        {
231
        cuts[0][i] = cut;
232
        cuts[1][i] = cut;
233
        cuts[2][i] = cut;
234
        cuts[3][i] = cut;
235
        cut += dist;
236
        }
237

    
238
      return cuts;
239
      }
240
    }
241

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

    
244
  int getNumCubitFaces()
245
    {
246
    return FACES_PER_CUBIT;
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  private int getNumOctahedrons(int layers)
252
    {
253
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  private int getNumTetrahedrons(int layers)
259
    {
260
    return 4*layers*(layers-1);
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
266
    {
267
    float x = DIST*(layers-1);
268
    float z = DIST*(layers+1);
269

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

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

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

    
294
    for(int i=0; i<layers-2; 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
    return index;
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306

    
307
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
308
    {
309
    float x = DIST*(layers-1);
310
    float z = DIST*layers;
311

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

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

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

    
331
    x -= DIST;
332
    z -= DIST;
333

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

    
342
    x -= DIST;
343
    z += DIST;
344

    
345
    for(int i=0; i<layers-1; i++, index++)
346
      {
347
      x += 2*DIST;
348
      centers[index][0] = x;
349
      centers[index][1] = height;
350
      centers[index][2] = z;
351
      }
352

    
353
    return index;
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357

    
358
  float[][] getCubitPositions(int layers)
359
    {
360
    int numO = getNumOctahedrons(layers);
361
    int numT = getNumTetrahedrons(layers);
362
    int index = 0;
363
    float height = 0.0f;
364

    
365
    float[][] CENTERS = new float[numO+numT][3];
366

    
367
    index = createOctaPositions(CENTERS,index,layers,height);
368

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

    
376
    height = DIST*SQ2/2;
377

    
378
    for(int i=layers; i>1; i--)
379
      {
380
      index = createTetraPositions(CENTERS,index,i,+height);
381
      index = createTetraPositions(CENTERS,index,i,-height);
382
      height += SQ2*DIST;
383
      }
384

    
385
    return CENTERS;
386
    }
387

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

    
390
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
391
    {
392
    for(int i=numLayers-1; i>0; i--)
393
      {
394
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
395
      tetra -= 8*i;
396
      }
397

    
398
    return -1;
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402

    
403
  ObjectShape getObjectShape(int cubit, int numLayers)
404
    {
405
    int variant = getCubitVariant(cubit,numLayers);
406
    int N = numLayers>3 ? 5:6;
407
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
408

    
409
    if( variant==0 )
410
      {
411
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
412
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
413
      float[][] corners   = new float[][] { {0.04f,0.20f} };
414
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
415
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
416
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
417
      return new ObjectShape(VERTICES_OCTA,VERT_INDEXES_OCTA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
418
      }
419
    else
420
      {
421
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
422
      int[] bandIndices   = new int[] { 0,0,0,0 };
423
      float[][] corners   = new float[][] { {0.08f,0.15f} };
424
      int[] cornerIndices = new int[] { 0,0,0,0 };
425
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
426
      int[] centerIndices = new int[] { 0,0,0,0 };
427
      return new ObjectShape(VERTICES_TETRA,VERT_INDEXES_TETRA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
428
      }
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432

    
433
  Static4D getQuat(int cubit, int numLayers)
434
    {
435
    int numO = getNumOctahedrons(numLayers);
436

    
437
    if( cubit<numO ) return QUATS[0];
438

    
439
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
440
      {
441
      case 0: return QUATS[0];                          // unit quat
442
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
443
      case 2: return QUATS[1];                          // 180 along Y
444
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
445
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
446
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
447
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
448
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
449
      }
450

    
451
    return null;
452
    }
453

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

    
456
  int getNumCubitVariants(int numLayers)
457
    {
458
    return 2;
459
    }
460

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

    
463
  int getCubitVariant(int cubit, int numLayers)
464
    {
465
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
466
    }
467

    
468
///////////////////////////////////////////////////////////////////////////////////////////////////
469

    
470
  int getFaceColor(int cubit, int cubitface, int size)
471
    {
472
    int numO = getNumOctahedrons(size);
473

    
474
    if( cubit<numO )
475
      {
476
      int axis = 0;
477
      int layer= 1;
478

    
479
      switch(cubitface)
480
        {
481
        case 0: axis = 2; layer =             1; break;
482
        case 1: axis = 0; layer = (1<<(size-1)); break;
483
        case 2: axis = 3; layer =             1; break;
484
        case 3: axis = 1; layer = (1<<(size-1)); break;
485
        case 4: axis = 3; layer = (1<<(size-1)); break;
486
        case 5: axis = 1; layer =             1; break;
487
        case 6: axis = 2; layer = (1<<(size-1)); break;
488
        case 7: axis = 0; layer =             1; break;
489
        }
490

    
491
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
492
      }
493
    else
494
      {
495
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
496
      }
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  int getColor(int face)
502
    {
503
    return FACE_COLORS[face];
504
    }
505

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

    
508
  ObjectSticker retSticker(int face)
509
    {
510
    return mStickers[face/NUM_FACES];
511
    }
512

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

    
515
  float returnMultiplier()
516
    {
517
    return 1.5f;
518
    }
519

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521
// PUBLIC API
522

    
523
  public Static3D[] getRotationAxis()
524
    {
525
    return ROT_AXIS;
526
    }
527

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

    
530
  public int[] getBasicAngle()
531
    {
532
    return BASIC_ANGLE;
533
    }
534

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

    
537
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int totalScrambles)
538
    {
539
    if( curr==0 )
540
      {
541
      mCurrState     = 0;
542
      mIndexExcluded =-1;
543
      }
544

    
545
    int total = mStates[mCurrState].getTotal(mIndexExcluded);
546
    int random= rnd.nextInt(total);
547
    int[] info= mStates[mCurrState].getInfo(random,mIndexExcluded);
548

    
549
    scramble[curr][0] = info[0];
550
    scramble[curr][1] = info[1];
551
    scramble[curr][2] = info[2];
552

    
553
    mCurrState     = info[3];
554
    mIndexExcluded = info[0];
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558

    
559
  public int getObjectName(int numLayers)
560
    {
561
    switch(numLayers)
562
      {
563
      case 2: return R.string.diam2;
564
      case 3: return R.string.diam3;
565
      case 4: return R.string.diam4;
566
      }
567

    
568
    return 0;
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
  public int getInventor(int numLayers)
574
    {
575
    switch(numLayers)
576
      {
577
      case 2: return R.string.diam2_inventor;
578
      case 3: return R.string.diam3_inventor;
579
      case 4: return R.string.diam4_inventor;
580
      }
581

    
582
    return 0;
583
    }
584

    
585
///////////////////////////////////////////////////////////////////////////////////////////////////
586

    
587
  public int getComplexity(int numLayers)
588
    {
589
    switch(numLayers)
590
      {
591
      case 2: return 4;
592
      case 3: return 6;
593
      case 4: return 8;
594
      }
595

    
596
    return 0;
597
    }
598
}
(23-23/41)