Project

General

Profile

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

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

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

    
33
import java.util.Random;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

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

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

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

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

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

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

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

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

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

    
127
  private static final ObjectSticker[] mStickers;
128

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
141
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
142
    {
143
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  int[] getSolvedQuats(int cubit, int numLayers)
149
    {
150
    int status = retCubitSolvedStatus(cubit,numLayers);
151
    return status<0 ? null : buildSolvedQuats(MovementDiamond.FACE_AXIS[mFaceMap[status]],QUATS);
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  float getScreenRatio()
157
    {
158
    return 0.65f;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  Static4D[] getQuats()
164
    {
165
    return QUATS;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  int getNumFaces()
171
    {
172
    return FACE_COLORS.length;
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  boolean shouldResetTextureMaps()
178
    {
179
    return false;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  int getNumStickerTypes(int numLayers)
185
    {
186
    return STICKERS.length;
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

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

    
212
      return cuts;
213
      }
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  int getNumCubitFaces()
219
    {
220
    return FACES_PER_CUBIT;
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  private int getNumOctahedrons(int layers)
226
    {
227
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  private int getNumTetrahedrons(int layers)
233
    {
234
    return 4*layers*(layers-1);
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

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

    
244
    for(int i=0; i<layers; i++, index++)
245
      {
246
      z -= 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
      x -= 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-1; i++, index++)
261
      {
262
      z += 2*DIST;
263
      centers[index][0] = x;
264
      centers[index][1] = height;
265
      centers[index][2] = z;
266
      }
267

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

    
276
    return index;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
282
    {
283
    float x = DIST*(layers-1);
284
    float z = DIST*layers;
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
    x += DIST;
295
    z -= DIST;
296

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

    
305
    x -= DIST;
306
    z -= DIST;
307

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

    
316
    x -= DIST;
317
    z += DIST;
318

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

    
327
    return index;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

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

    
339
    float[][] CENTERS = new float[numO+numT][3];
340

    
341
    index = createOctaPositions(CENTERS,index,layers,height);
342

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

    
350
    height = DIST*SQ2/2;
351

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

    
359
    return CENTERS;
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

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

    
372
    return -1;
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  ObjectShape getObjectShape(int cubit, int numLayers)
378
    {
379
    int variant = getCubitVariant(cubit,numLayers);
380
    int N = numLayers>3 ? 5:6;
381
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
382

    
383
    if( variant==0 )
384
      {
385
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
386
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
387
      float[][] corners   = new float[][] { {0.04f,0.20f} };
388
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
389
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
390
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
391
      return new ObjectShape(VERTICES_OCTA,VERT_INDEXES_OCTA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
392
      }
393
    else
394
      {
395
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
396
      int[] bandIndices   = new int[] { 0,0,0,0 };
397
      float[][] corners   = new float[][] { {0.08f,0.15f} };
398
      int[] cornerIndices = new int[] { 0,0,0,0 };
399
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
400
      int[] centerIndices = new int[] { 0,0,0,0 };
401
      return new ObjectShape(VERTICES_TETRA,VERT_INDEXES_TETRA,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
402
      }
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406

    
407
  Static4D getQuat(int cubit, int numLayers)
408
    {
409
    int numO = getNumOctahedrons(numLayers);
410

    
411
    if( cubit<numO ) return QUATS[0];
412

    
413
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
414
      {
415
      case 0: return QUATS[0];                          // unit quat
416
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
417
      case 2: return QUATS[1];                          // 180 along Y
418
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
419
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
420
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
421
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
422
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
423
      }
424

    
425
    return null;
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
  int getNumCubitVariants(int numLayers)
431
    {
432
    return 2;
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  int getCubitVariant(int cubit, int numLayers)
438
    {
439
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  int getFaceColor(int cubit, int cubitface, int size)
445
    {
446
    int numO = getNumOctahedrons(size);
447

    
448
    if( cubit<numO )
449
      {
450
      int axis = 0;
451
      int layer= 1;
452

    
453
      switch(cubitface)
454
        {
455
        case 0: axis = 2; layer =             1; break;
456
        case 1: axis = 0; layer = (1<<(size-1)); break;
457
        case 2: axis = 3; layer =             1; break;
458
        case 3: axis = 1; layer = (1<<(size-1)); break;
459
        case 4: axis = 3; layer = (1<<(size-1)); break;
460
        case 5: axis = 1; layer =             1; break;
461
        case 6: axis = 2; layer = (1<<(size-1)); break;
462
        case 7: axis = 0; layer =             1; break;
463
        }
464

    
465
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
466
      }
467
    else
468
      {
469
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
470
      }
471
    }
472

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

    
475
  int getColor(int face)
476
    {
477
    return FACE_COLORS[face];
478
    }
479

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

    
482
  ObjectSticker retSticker(int face)
483
    {
484
    return mStickers[face/NUM_FACES];
485
    }
486

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
  float returnMultiplier()
490
    {
491
    return 1.5f;
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
// PUBLIC API
496

    
497
  public Static3D[] getRotationAxis()
498
    {
499
    return ROT_AXIS;
500
    }
501

    
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

    
504
  public int[] getBasicAngle()
505
    {
506
    return BASIC_ANGLE;
507
    }
508

    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510

    
511
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
512
    {
513
    if( curr==0 )
514
      {
515
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
516
      }
517
    else
518
      {
519
      int newVector = rnd.nextInt(NUM_AXIS -1);
520
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
521
      }
522

    
523
    float rowFloat = rnd.nextFloat();
524
    int numLayers = getNumLayers();
525

    
526
    for(int row=0; row<numLayers; row++)
527
      {
528
      if( rowFloat*numLayers <= row+1 )
529
        {
530
        scramble[curr][1] = row;
531
        break;
532
        }
533
      }
534

    
535
    switch( rnd.nextInt(2) )
536
      {
537
      case 0: scramble[curr][2] = -1; break;
538
      case 1: scramble[curr][2] =  1; break;
539
      }
540
    }
541

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

    
544
  public int getObjectName(int numLayers)
545
    {
546
    switch(numLayers)
547
      {
548
      case 2: return R.string.diam2;
549
      case 3: return R.string.diam3;
550
      case 4: return R.string.diam4;
551
      }
552

    
553
    return 0;
554
    }
555

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557

    
558
  public int getInventor(int numLayers)
559
    {
560
    switch(numLayers)
561
      {
562
      case 2: return R.string.diam2_inventor;
563
      case 3: return R.string.diam3_inventor;
564
      case 4: return R.string.diam4_inventor;
565
      }
566

    
567
    return 0;
568
    }
569

    
570
///////////////////////////////////////////////////////////////////////////////////////////////////
571

    
572
  public int getComplexity(int numLayers)
573
    {
574
    switch(numLayers)
575
      {
576
      case 2: return 4;
577
      case 3: return 6;
578
      case 4: return 8;
579
      }
580

    
581
    return 0;
582
    }
583
}
(23-23/41)