Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / objects / TwistyDiamond.java @ 55fa6993

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
  public 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.diam_2;
104
      case 3: return R.raw.diam_3;
105
      case 4: return R.raw.diam_4;
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
  public 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
  public 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
  public int getNumStickerTypes(int[] numLayers)
161
    {
162
    return 1;
163
    }
164

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

    
167
  public 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
  public 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
  public 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
  public ObjectShape getObjectShape(int variant)
402
    {
403
    int numL = getNumLayers()[0];
404
    int N = numL>3 ? 5:6;
405
    int E = numL>2 ? (numL>3 ? 0:1):2;
406

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

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

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

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

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

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

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

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

    
476
    return null;
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

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

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

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

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494

    
495
  public int getVariantFaceColor(int variant, int face, int[] numLayers)
496
    {
497
    return 0;
498
    }
499

    
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501

    
502
  public int getCubitFaceColor(int cubit, int face, int[] numLayers)
503
    {
504
    int numL = numLayers[0];
505
    int numO = getNumOctahedrons(numL);
506

    
507
    if( cubit<numO )
508
      {
509
      int axis = 0;
510
      int layer= 1;
511

    
512
      switch(face)
513
        {
514
        case 0: axis = 2; layer =             1; break;
515
        case 1: axis = 0; layer = (1<<(numL-1)); break;
516
        case 2: axis = 3; layer =             1; break;
517
        case 3: axis = 1; layer = (1<<(numL-1)); break;
518
        case 4: axis = 3; layer = (1<<(numL-1)); break;
519
        case 5: axis = 1; layer =             1; break;
520
        case 6: axis = 2; layer = (1<<(numL-1)); break;
521
        case 7: axis = 0; layer =             1; break;
522
        }
523

    
524
      return CUBITS[cubit].getRotRow(axis) == layer ? face : -1;
525
      }
526
    else
527
      {
528
      return face>0 ? -1 : retFaceTetraBelongsTo(cubit-numO, numL);
529
      }
530
    }
531

    
532
///////////////////////////////////////////////////////////////////////////////////////////////////
533

    
534
  public ObjectSticker retSticker(int sticker)
535
    {
536
    if( mStickers==null )
537
      {
538
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
539
      float radius = 0.06f;
540
      float stroke = 0.07f;
541
      float[] radii = new float[] {radius,radius,radius};
542

    
543
      if( ObjectControl.isInIconMode() )
544
        {
545
        int[] numLayers = getNumLayers();
546

    
547
        switch(numLayers[0])
548
          {
549
          case 2: stroke*=1.4f; break;
550
          case 3: stroke*=2.0f; break;
551
          case 4: stroke*=2.1f; break;
552
          default:stroke*=2.2f; break;
553
          }
554
        }
555

    
556
      mStickers     = new ObjectSticker[STICKERS.length];
557
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
558
      }
559

    
560
    return mStickers[sticker];
561
    }
562

    
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564
// PUBLIC API
565

    
566
  public Static3D[] getRotationAxis()
567
    {
568
    return ROT_AXIS;
569
    }
570

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

    
573
  public int[] getBasicAngle()
574
    {
575
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
576
    return mBasicAngle;
577
    }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580

    
581
  public ObjectType intGetObjectType(int[] numLayers)
582
    {
583
    switch(numLayers[0])
584
      {
585
      case 2: return ObjectType.DIAM_2;
586
      case 3: return ObjectType.DIAM_3;
587
      case 4: return ObjectType.DIAM_4;
588
      }
589

    
590
    return ObjectType.DIAM_2;
591
    }
592

    
593
///////////////////////////////////////////////////////////////////////////////////////////////////
594

    
595
  public int getObjectName(int[] numLayers)
596
    {
597
    switch(numLayers[0])
598
      {
599
      case 2: return R.string.diam2;
600
      case 3: return R.string.diam3;
601
      case 4: return R.string.diam4;
602
      }
603

    
604
    return 0;
605
    }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608

    
609
  public int getInventor(int[] numLayers)
610
    {
611
    switch(numLayers[0])
612
      {
613
      case 2: return R.string.diam2_inventor;
614
      case 3: return R.string.diam3_inventor;
615
      case 4: return R.string.diam4_inventor;
616
      }
617

    
618
    return 0;
619
    }
620

    
621
///////////////////////////////////////////////////////////////////////////////////////////////////
622

    
623
  public int getYearOfInvention(int[] numLayers)
624
    {
625
    switch(numLayers[0])
626
      {
627
      case 2: return 1984;
628
      case 3: return 2003;
629
      case 4: return 2011;
630
      }
631
    return 1984;
632
    }
633

    
634
///////////////////////////////////////////////////////////////////////////////////////////////////
635

    
636
  public int getComplexity(int[] numLayers)
637
    {
638
    switch(numLayers[0])
639
      {
640
      case 2: return 4;
641
      case 3: return 6;
642
      case 4: return 8;
643
      }
644

    
645
    return 0;
646
    }
647
}
(7-7/25)