Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 8db55f55

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 getSolvedFunctionIndex()
185
    {
186
    return 0;
187
    }
188

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

    
191
  int getNumStickerTypes(int numLayers)
192
    {
193
    return STICKERS.length;
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

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

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

    
219
      return cuts;
220
      }
221
    }
222

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

    
225
  int getNumCubitFaces()
226
    {
227
    return FACES_PER_CUBIT;
228
    }
229

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

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

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

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

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

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

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

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

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

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

    
283
    return index;
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

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

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

    
301
    x += DIST;
302
    z -= DIST;
303

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

    
312
    x -= DIST;
313
    z -= DIST;
314

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

    
323
    x -= DIST;
324
    z += DIST;
325

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

    
334
    return index;
335
    }
336

    
337
///////////////////////////////////////////////////////////////////////////////////////////////////
338

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

    
346
    float[][] CENTERS = new float[numO+numT][3];
347

    
348
    index = createOctaPositions(CENTERS,index,layers,height);
349

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

    
357
    height = DIST*SQ2/2;
358

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

    
366
    return CENTERS;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

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

    
379
    return -1;
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  ObjectShape getObjectShape(int cubit, int numLayers)
385
    {
386
    int variant = getCubitVariant(cubit,numLayers);
387
    int N = numLayers>3 ? 5:6;
388
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
389

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

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413

    
414
  Static4D getQuat(int cubit, int numLayers)
415
    {
416
    int numO = getNumOctahedrons(numLayers);
417

    
418
    if( cubit<numO ) return QUATS[0];
419

    
420
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
421
      {
422
      case 0: return QUATS[0];                          // unit quat
423
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
424
      case 2: return QUATS[1];                          // 180 along Y
425
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
426
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
427
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
428
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
429
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
430
      }
431

    
432
    return null;
433
    }
434

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

    
437
  int getNumCubitVariants(int numLayers)
438
    {
439
    return 2;
440
    }
441

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

    
444
  int getCubitVariant(int cubit, int numLayers)
445
    {
446
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  int getFaceColor(int cubit, int cubitface, int size)
452
    {
453
    int numO = getNumOctahedrons(size);
454

    
455
    if( cubit<numO )
456
      {
457
      int axis = 0;
458
      int layer= 1;
459

    
460
      switch(cubitface)
461
        {
462
        case 0: axis = 2; layer =             1; break;
463
        case 1: axis = 0; layer = (1<<(size-1)); break;
464
        case 2: axis = 3; layer =             1; break;
465
        case 3: axis = 1; layer = (1<<(size-1)); break;
466
        case 4: axis = 3; layer = (1<<(size-1)); break;
467
        case 5: axis = 1; layer =             1; break;
468
        case 6: axis = 2; layer = (1<<(size-1)); break;
469
        case 7: axis = 0; layer =             1; break;
470
        }
471

    
472
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
473
      }
474
    else
475
      {
476
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
477
      }
478
    }
479

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

    
482
  int getColor(int face)
483
    {
484
    return FACE_COLORS[face];
485
    }
486

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

    
489
  ObjectSticker retSticker(int face)
490
    {
491
    return mStickers[face/NUM_FACES];
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
  float returnMultiplier()
497
    {
498
    return 1.5f;
499
    }
500

    
501
///////////////////////////////////////////////////////////////////////////////////////////////////
502
// PUBLIC API
503

    
504
  public Static3D[] getRotationAxis()
505
    {
506
    return ROT_AXIS;
507
    }
508

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

    
511
  public int[] getBasicAngle()
512
    {
513
    return BASIC_ANGLE;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517

    
518
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
519
    {
520
    if( curr==0 )
521
      {
522
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
523
      }
524
    else
525
      {
526
      int newVector = rnd.nextInt(NUM_AXIS -1);
527
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
528
      }
529

    
530
    float rowFloat = rnd.nextFloat();
531
    int numLayers = getNumLayers();
532

    
533
    for(int row=0; row<numLayers; row++)
534
      {
535
      if( rowFloat*numLayers <= row+1 )
536
        {
537
        scramble[curr][1] = row;
538
        break;
539
        }
540
      }
541

    
542
    switch( rnd.nextInt(2) )
543
      {
544
      case 0: scramble[curr][2] = -1; break;
545
      case 1: scramble[curr][2] =  1; break;
546
      }
547
    }
548

    
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550

    
551
  public int getObjectName(int numLayers)
552
    {
553
    switch(numLayers)
554
      {
555
      case 2: return R.string.diam2;
556
      case 3: return R.string.diam3;
557
      case 4: return R.string.diam4;
558
      }
559

    
560
    return 0;
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

    
565
  public int getInventor(int numLayers)
566
    {
567
    switch(numLayers)
568
      {
569
      case 2: return R.string.diam2_inventor;
570
      case 3: return R.string.diam3_inventor;
571
      case 4: return R.string.diam4_inventor;
572
      }
573

    
574
    return 0;
575
    }
576

    
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578

    
579
  public int getComplexity(int numLayers)
580
    {
581
    switch(numLayers)
582
      {
583
      case 2: return 4;
584
      case 3: return 6;
585
      case 4: return 8;
586
      }
587

    
588
    return 0;
589
    }
590
}
(23-23/41)