Project

General

Profile

Download (19.1 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / objects / TwistyDiamond.java @ 59c20632

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.objectlib.objects;
21

    
22
import static org.distorted.objectlib.main.Movement.MOVEMENT_OCTAHEDRON;
23
import static org.distorted.objectlib.main.Movement.TYPE_NOT_SPLIT;
24

    
25
import android.content.res.Resources;
26

    
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

    
33
import org.distorted.objectlib.R;
34
import org.distorted.objectlib.main.Movement8;
35
import org.distorted.objectlib.main.ObjectControl;
36
import org.distorted.objectlib.main.ObjectType;
37
import org.distorted.objectlib.helpers.ObjectShape;
38
import org.distorted.objectlib.helpers.ObjectSticker;
39
import org.distorted.objectlib.helpers.ScrambleState;
40
import org.distorted.objectlib.main.Twisty8;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
55
  private ScrambleState[] mStates;
56
  private int[] mBasicAngle;
57
  private int[] mFaceMap;
58
  private float[][] mCuts;
59
  private Static4D[] mQuats;
60
  private int[] mTetraToFaceMap;
61
  private ObjectSticker[] mStickers;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  public TwistyDiamond(int[] numL, Static4D quat, Static3D move, DistortedTexture texture,
66
                       MeshSquare mesh, DistortedEffects effects, Resources res, int surfaceW, int surfaceH)
67
    {
68
    super(numL, numL[0], quat, move, texture, mesh, effects, res, surfaceW, surfaceH);
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  protected ScrambleState[] getScrambleStates()
74
    {
75
    if( mStates==null )
76
      {
77
      int[] numLayers = getNumLayers();
78
      int numL = numLayers[0];
79
      int[] tmp = new int[3*2*numL];
80

    
81
      for(int i=0; i<2*numL; i++)
82
        {
83
        tmp[3*i  ] = (i<numL) ?  i:i-numL;
84
        tmp[3*i+1] = (i%2==0) ? -1:1;
85
        tmp[3*i+2] = 0;
86
        }
87

    
88
      mStates = new ScrambleState[]
89
        {
90
        new ScrambleState( new int[][] {tmp,tmp,tmp,tmp} )
91
        };
92
      }
93

    
94
    return mStates;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  protected int getResource(int[] numLayers)
100
    {
101
    switch(numLayers[0])
102
      {
103
      case 2: return R.raw.diam2;
104
      case 3: return R.raw.diam3;
105
      case 4: return R.raw.diam4;
106
      }
107

    
108
    return 0;
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void initializeQuats()
114
    {
115
    mQuats = new Static4D[]
116
         {
117
          new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
118
          new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
119
          new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
120
          new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
121

    
122
          new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
123
          new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
124
          new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
125
          new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
126
          new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
127
          new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
128
          new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
129
          new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
130
         };
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  protected int[] getSolvedQuats(int cubit, int[] numLayers)
136
    {
137
    if( mQuats==null ) initializeQuats();
138
    if( mFaceMap==null ) mFaceMap = new int[] {4,0,6,2,7,3,5,1};
139
    int status = retCubitSolvedStatus(cubit,numLayers);
140
    return status<0 ? null : buildSolvedQuats(Movement8.FACE_AXIS[mFaceMap[status]],mQuats);
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  protected Static4D[] getQuats()
146
    {
147
    if( mQuats==null ) initializeQuats();
148
    return mQuats;
149
    }
150

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

    
153
  public int getSolvedFunctionIndex()
154
    {
155
    return 0;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  protected int getNumStickerTypes(int[] numLayers)
161
    {
162
    return 1;
163
    }
164

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

    
167
  protected float[][] getCuts(int[] numLayers)
168
    {
169
    int numL = numLayers[0];
170
    if( numL<2 ) return null;
171

    
172
    if( mCuts==null )
173
      {
174
      mCuts = new float[4][numL-1];
175
      float cut = (SQ6/6)*(2-numL);
176

    
177
      for(int i=0; i<numL-1; i++)
178
        {
179
        mCuts[0][i] = cut;
180
        mCuts[1][i] = cut;
181
        mCuts[2][i] = cut;
182
        mCuts[3][i] = cut;
183
        cut += SQ6/3;
184
        }
185
      }
186

    
187
    return mCuts;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  public boolean[][] getLayerRotatable(int[] numLayers)
193
    {
194
    int numAxis = ROT_AXIS.length;
195
    boolean[][] layerRotatable = new boolean[numAxis][];
196

    
197
    for(int i=0; i<numAxis; i++)
198
      {
199
      layerRotatable[i] = new boolean[numLayers[i]];
200
      for(int j=0; j<numLayers[i]; j++) layerRotatable[i][j] = true;
201
      }
202

    
203
    return layerRotatable;
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public int getMovementType()
209
    {
210
    return MOVEMENT_OCTAHEDRON;
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
  public int getMovementSplit()
216
    {
217
    return TYPE_NOT_SPLIT;
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public int[][][] getEnabled()
223
    {
224
    return new int[][][]
225
      {
226
          {{1,2,3}},{{1,2,3}},{{0,2,3}},{{0,2,3}},{{0,1,3}},{{0,1,3}},{{0,1,2}},{{0,1,2}}
227
      };
228
    }
229

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

    
232
  public float[] getDist3D(int[] numLayers)
233
    {
234
    return null;
235
    }
236

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

    
239
  protected int getNumCubitFaces()
240
    {
241
    return 8;
242
    }
243

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

    
246
  private int getNumOctahedrons(int layers)
247
    {
248
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  private int getNumTetrahedrons(int layers)
254
    {
255
    return 4*layers*(layers-1);
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
261
    {
262
    float x = (layers-1)*0.5f;
263
    float z = (layers+1)*0.5f;
264

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

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

    
281
    for(int i=0; i<layers-1; i++, index++)
282
      {
283
      z += 1;
284
      centers[index][0] = x;
285
      centers[index][1] = height;
286
      centers[index][2] = z;
287
      }
288

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

    
297
    return index;
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
303
    {
304
    float x = (layers-1)*0.5f;
305
    float z =  layers*0.5f;
306

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

    
315
    x += 0.5f;
316
    z -= 0.5f;
317

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

    
326
    x -= 0.5f;
327
    z -= 0.5f;
328

    
329
    for(int i=0; i<layers-1; i++, index++)
330
      {
331
      z += 1;
332
      centers[index][0] = x;
333
      centers[index][1] = height;
334
      centers[index][2] = z;
335
      }
336

    
337
    x -= 0.5f;
338
    z += 0.5f;
339

    
340
    for(int i=0; i<layers-1; i++, index++)
341
      {
342
      x += 1;
343
      centers[index][0] = x;
344
      centers[index][1] = height;
345
      centers[index][2] = z;
346
      }
347

    
348
    return index;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

    
353
  protected float[][] getCubitPositions(int[] numLayers)
354
    {
355
    int layers = numLayers[0];
356
    int numO = getNumOctahedrons(layers);
357
    int numT = getNumTetrahedrons(layers);
358
    int index = 0;
359
    float height = 0.0f;
360

    
361
    float[][] CENTERS = new float[numO+numT][3];
362

    
363
    index = createOctaPositions(CENTERS,index,layers,height);
364

    
365
    for(int i=layers-1; i>0; i--)
366
      {
367
      height += SQ2/2;
368
      index = createOctaPositions(CENTERS,index,i,+height);
369
      index = createOctaPositions(CENTERS,index,i,-height);
370
      }
371

    
372
    height = SQ2/4;
373

    
374
    for(int i=layers; i>1; i--)
375
      {
376
      index = createTetraPositions(CENTERS,index,i,+height);
377
      index = createTetraPositions(CENTERS,index,i,-height);
378
      height += SQ2/2;
379
      }
380

    
381
    return CENTERS;
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
387
    {
388
    if( mTetraToFaceMap==null ) mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
389

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

    
396
    return -1;
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

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

    
408
    if( variant==0 )
409
      {
410
      double[][] vertices = new double[][]
411
          {
412
             { 0.5,   0.0, 0.5},
413
             { 0.5,   0.0,-0.5},
414
             {-0.5,   0.0,-0.5},
415
             {-0.5,   0.0, 0.5},
416
             { 0.0, SQ2/2, 0.0},
417
             { 0.0,-SQ2/2, 0.0}
418
          };
419

    
420
      int[][] vert_indices = new int[][]
421
          {
422
             {3,0,4},
423
             {0,1,4},
424
             {1,2,4},
425
             {2,3,4},
426
             {5,0,3},
427
             {5,1,0},
428
             {5,2,1},
429
             {5,3,2}
430
          };
431

    
432
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
433
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
434
      float[][] corners   = new float[][] { {0.04f,0.20f} };
435
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
436
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
437
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
438
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
439
      }
440
    else
441
      {
442
      double[][] vertices = new double[][] { {-0.5, SQ2/4, 0.0}, { 0.5, SQ2/4, 0.0}, { 0.0,-SQ2/4, 0.5}, { 0.0,-SQ2/4,-0.5} };
443
      int[][] vert_indices = new int[][]  { {2,1,0}, {2,3,1}, {3,2,0}, {3,0,1} };
444
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
445
      int[] bandIndices   = new int[] { 0,0,0,0 };
446
      float[][] corners   = new float[][] { {0.08f,0.15f} };
447
      int[] cornerIndices = new int[] { 0,0,0,0 };
448
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
449
      int[] centerIndices = new int[] { 0,0,0,0 };
450
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
451
      }
452
    }
453

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

    
456
  protected Static4D getQuat(int cubit, int[] numLayers)
457
    {
458
    if( mQuats==null ) initializeQuats();
459

    
460
    int numL = numLayers[0];
461
    int numO = getNumOctahedrons(numL);
462

    
463
    if( cubit<numO ) return mQuats[0];
464

    
465
    switch( retFaceTetraBelongsTo(cubit-numO, numL) )
466
      {
467
      case 0: return mQuats[0];                         // unit quat
468
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
469
      case 2: return mQuats[1];                         // 180 along Y
470
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
471
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
472
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
473
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
474
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
475
      }
476

    
477
    return null;
478
    }
479

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

    
482
  protected int getNumCubitVariants(int[] numLayers)
483
    {
484
    return 2;
485
    }
486

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

    
489
  protected int getCubitVariant(int cubit, int[] numLayers)
490
    {
491
    return cubit<getNumOctahedrons(numLayers[0]) ? 0 : 1;
492
    }
493

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

    
496
  protected int getFaceColor(int cubit, int cubitface, int[] numLayers)
497
    {
498
    int numL = numLayers[0];
499
    int numO = getNumOctahedrons(numL);
500

    
501
    if( cubit<numO )
502
      {
503
      int axis = 0;
504
      int layer= 1;
505

    
506
      switch(cubitface)
507
        {
508
        case 0: axis = 2; layer =             1; break;
509
        case 1: axis = 0; layer = (1<<(numL-1)); break;
510
        case 2: axis = 3; layer =             1; break;
511
        case 3: axis = 1; layer = (1<<(numL-1)); break;
512
        case 4: axis = 3; layer = (1<<(numL-1)); break;
513
        case 5: axis = 1; layer =             1; break;
514
        case 6: axis = 2; layer = (1<<(numL-1)); break;
515
        case 7: axis = 0; layer =             1; break;
516
        }
517

    
518
      return CUBITS[cubit].getRotRow(axis) == layer ? cubitface : NUM_TEXTURES;
519
      }
520
    else
521
      {
522
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, numL);
523
      }
524
    }
525

    
526
///////////////////////////////////////////////////////////////////////////////////////////////////
527

    
528
  protected ObjectSticker retSticker(int face)
529
    {
530
    if( mStickers==null )
531
      {
532
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
533
      float radius = 0.06f;
534
      float stroke = 0.07f;
535
      float[] radii = new float[] {radius,radius,radius};
536

    
537
      if( ObjectControl.isInIconMode() )
538
        {
539
        int[] numLayers = getNumLayers();
540

    
541
        switch(numLayers[0])
542
          {
543
          case 2: stroke*=1.4f; break;
544
          case 3: stroke*=2.0f; break;
545
          case 4: stroke*=2.1f; break;
546
          default:stroke*=2.2f; break;
547
          }
548
        }
549

    
550
      mStickers     = new ObjectSticker[STICKERS.length];
551
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
552
      }
553

    
554
    return mStickers[face/NUM_FACE_COLORS];
555
    }
556

    
557
///////////////////////////////////////////////////////////////////////////////////////////////////
558
// PUBLIC API
559

    
560
  public Static3D[] getRotationAxis()
561
    {
562
    return ROT_AXIS;
563
    }
564

    
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566

    
567
  public int[] getBasicAngle()
568
    {
569
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
570
    return mBasicAngle;
571
    }
572

    
573
///////////////////////////////////////////////////////////////////////////////////////////////////
574

    
575
  public ObjectType intGetObjectType(int[] numLayers)
576
    {
577
    switch(numLayers[0])
578
      {
579
      case 2: return ObjectType.DIAM_2;
580
      case 3: return ObjectType.DIAM_3;
581
      case 4: return ObjectType.DIAM_4;
582
      }
583

    
584
    return ObjectType.DIAM_2;
585
    }
586

    
587
///////////////////////////////////////////////////////////////////////////////////////////////////
588

    
589
  public int getObjectName(int[] numLayers)
590
    {
591
    switch(numLayers[0])
592
      {
593
      case 2: return R.string.diam2;
594
      case 3: return R.string.diam3;
595
      case 4: return R.string.diam4;
596
      }
597

    
598
    return 0;
599
    }
600

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602

    
603
  public int getInventor(int[] numLayers)
604
    {
605
    switch(numLayers[0])
606
      {
607
      case 2: return R.string.diam2_inventor;
608
      case 3: return R.string.diam3_inventor;
609
      case 4: return R.string.diam4_inventor;
610
      }
611

    
612
    return 0;
613
    }
614

    
615
///////////////////////////////////////////////////////////////////////////////////////////////////
616

    
617
  public int getYearOfInvention(int[] numLayers)
618
    {
619
    switch(numLayers[0])
620
      {
621
      case 2: return 1984;
622
      case 3: return 2003;
623
      case 4: return 2011;
624
      }
625
    return 1984;
626
    }
627

    
628
///////////////////////////////////////////////////////////////////////////////////////////////////
629

    
630
  public int getComplexity(int[] numLayers)
631
    {
632
    switch(numLayers[0])
633
      {
634
      case 2: return 4;
635
      case 3: return 6;
636
      case 4: return 8;
637
      }
638

    
639
    return 0;
640
    }
641
}
(7-7/25)