Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 967c1d17

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 static org.distorted.objects.Movement.TYPE_NOT_SPLIT;
23

    
24
import android.content.res.Resources;
25

    
26
import org.distorted.helpers.ObjectShape;
27
import org.distorted.helpers.ObjectSticker;
28
import org.distorted.helpers.ScrambleState;
29
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshSquare;
32
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34
import org.distorted.main.R;
35

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

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

    
49
  private static final int[] NUM_ENABLED = {3,3,3,3,3,3,3,3};
50

    
51
  private static final int[][][] ENABLED = new int[][][]
52
      {
53
          {{1,2,3}},{{1,2,3}},{{0,2,3}},{{0,2,3}},{{0,1,3}},{{0,1,3}},{{0,1,2}},{{0,1,2}}
54
      };
55

    
56
  private ScrambleState[] mStates;
57
  private int[] mBasicAngle;
58
  private int[] mFaceMap;
59
  private float[][] mCuts;
60
  private boolean[][] mLayerRotatable;
61
  private Static4D[] mQuats;
62
  private int[] mTetraToFaceMap;
63
  private ObjectSticker[] mStickers;
64
  private Movement mMovement;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
69
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
70
    {
71
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  ScrambleState[] getScrambleStates()
77
    {
78
    if( mStates==null )
79
      {
80
      int size = getNumLayers();
81
      int[] tmp = new int[3*2*size];
82

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

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

    
96
    return mStates;
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  private void initializeQuats()
102
    {
103
    mQuats = new Static4D[]
104
         {
105
          new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
106
          new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
107
          new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
108
          new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
109

    
110
          new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
111
          new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
112
          new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
113
          new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
114
          new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
115
          new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
116
          new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
117
          new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
118
         };
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  int[] getSolvedQuats(int cubit, int numLayers)
124
    {
125
    if( mQuats==null ) initializeQuats();
126
    if( mFaceMap==null ) mFaceMap = new int[] {4,0,6,2,7,3,5,1};
127
    int status = retCubitSolvedStatus(cubit,numLayers);
128
    return status<0 ? null : buildSolvedQuats(Movement8.FACE_AXIS[mFaceMap[status]],mQuats);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  Static4D[] getQuats()
134
    {
135
    if( mQuats==null ) initializeQuats();
136
    return mQuats;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  int getSolvedFunctionIndex()
142
    {
143
    return 0;
144
    }
145

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

    
148
  int getNumStickerTypes(int numLayers)
149
    {
150
    return 1;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  float[][] getCuts(int numLayers)
156
    {
157
    if( numLayers<2 ) return null;
158

    
159
    if( mCuts==null )
160
      {
161
      mCuts = new float[4][numLayers-1];
162
      float cut = (SQ6/6)*(2-numLayers);
163

    
164
      for(int i=0; i<numLayers-1; i++)
165
        {
166
        mCuts[0][i] = cut;
167
        mCuts[1][i] = cut;
168
        mCuts[2][i] = cut;
169
        mCuts[3][i] = cut;
170
        cut += SQ6/3;
171
        }
172
      }
173

    
174
    return mCuts;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void getLayerRotatable(int numLayers)
180
    {
181
    if( mLayerRotatable==null )
182
      {
183
      int numAxis = ROT_AXIS.length;
184
      boolean[] tmp = new boolean[numLayers];
185
      for(int i=0; i<numLayers; i++) tmp[i] = true;
186
      mLayerRotatable = new boolean[numAxis][];
187
      for(int i=0; i<numAxis; i++) mLayerRotatable[i] = tmp;
188
      }
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  int getNumCubitFaces()
194
    {
195
    return 8;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private int getNumOctahedrons(int layers)
201
    {
202
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private int getNumTetrahedrons(int layers)
208
    {
209
    return 4*layers*(layers-1);
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
215
    {
216
    float x = (layers-1)*0.5f;
217
    float z = (layers+1)*0.5f;
218

    
219
    for(int i=0; i<layers; i++, index++)
220
      {
221
      z -= 1;
222
      centers[index][0] = x;
223
      centers[index][1] = height;
224
      centers[index][2] = z;
225
      }
226

    
227
    for(int i=0; i<layers-1; i++, index++)
228
      {
229
      x -= 1;
230
      centers[index][0] = x;
231
      centers[index][1] = height;
232
      centers[index][2] = z;
233
      }
234

    
235
    for(int i=0; i<layers-1; i++, index++)
236
      {
237
      z += 1;
238
      centers[index][0] = x;
239
      centers[index][1] = height;
240
      centers[index][2] = z;
241
      }
242

    
243
    for(int i=0; i<layers-2; i++, index++)
244
      {
245
      x += 1;
246
      centers[index][0] = x;
247
      centers[index][1] = height;
248
      centers[index][2] = z;
249
      }
250

    
251
    return index;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
257
    {
258
    float x = (layers-1)*0.5f;
259
    float z =  layers*0.5f;
260

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

    
269
    x += 0.5f;
270
    z -= 0.5f;
271

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

    
280
    x -= 0.5f;
281
    z -= 0.5f;
282

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

    
291
    x -= 0.5f;
292
    z += 0.5f;
293

    
294
    for(int i=0; i<layers-1; i++, index++)
295
      {
296
      x += 1;
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
  float[][] getCubitPositions(int layers)
308
    {
309
    int numO = getNumOctahedrons(layers);
310
    int numT = getNumTetrahedrons(layers);
311
    int index = 0;
312
    float height = 0.0f;
313

    
314
    float[][] CENTERS = new float[numO+numT][3];
315

    
316
    index = createOctaPositions(CENTERS,index,layers,height);
317

    
318
    for(int i=layers-1; i>0; i--)
319
      {
320
      height += SQ2/2;
321
      index = createOctaPositions(CENTERS,index,i,+height);
322
      index = createOctaPositions(CENTERS,index,i,-height);
323
      }
324

    
325
    height = SQ2/4;
326

    
327
    for(int i=layers; i>1; i--)
328
      {
329
      index = createTetraPositions(CENTERS,index,i,+height);
330
      index = createTetraPositions(CENTERS,index,i,-height);
331
      height += SQ2/2;
332
      }
333

    
334
    return CENTERS;
335
    }
336

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

    
339
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
340
    {
341
    if( mTetraToFaceMap==null ) mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
342

    
343
    for(int i=numLayers-1; i>0; i--)
344
      {
345
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
346
      tetra -= 8*i;
347
      }
348

    
349
    return -1;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  ObjectShape getObjectShape(int cubit, int numLayers)
355
    {
356
    int variant = getCubitVariant(cubit,numLayers);
357
    int N = numLayers>3 ? 5:6;
358
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
359

    
360
    if( variant==0 )
361
      {
362
      double[][] vertices = new double[][]
363
          {
364
             { 0.5,   0.0, 0.5},
365
             { 0.5,   0.0,-0.5},
366
             {-0.5,   0.0,-0.5},
367
             {-0.5,   0.0, 0.5},
368
             { 0.0, SQ2/2, 0.0},
369
             { 0.0,-SQ2/2, 0.0}
370
          };
371

    
372
      int[][] vert_indices = new int[][]
373
          {
374
             {3,0,4},
375
             {0,1,4},
376
             {1,2,4},
377
             {2,3,4},
378
             {5,0,3},
379
             {5,1,0},
380
             {5,2,1},
381
             {5,3,2}
382
          };
383

    
384
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
385
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
386
      float[][] corners   = new float[][] { {0.04f,0.20f} };
387
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
388
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
389
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
390
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
391
      }
392
    else
393
      {
394
      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} };
395
      int[][] vert_indices = new int[][]  { {2,1,0}, {2,3,1}, {3,2,0}, {3,0,1} };
396
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
397
      int[] bandIndices   = new int[] { 0,0,0,0 };
398
      float[][] corners   = new float[][] { {0.08f,0.15f} };
399
      int[] cornerIndices = new int[] { 0,0,0,0 };
400
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
401
      int[] centerIndices = new int[] { 0,0,0,0 };
402
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
403
      }
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  Static4D getQuat(int cubit, int numLayers)
409
    {
410
    if( mQuats==null ) initializeQuats();
411
    int numO = getNumOctahedrons(numLayers);
412

    
413
    if( cubit<numO ) return mQuats[0];
414

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

    
427
    return null;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  int getNumCubitVariants(int numLayers)
433
    {
434
    return 2;
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

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

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  int getFaceColor(int cubit, int cubitface, int size)
447
    {
448
    int numO = getNumOctahedrons(size);
449

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

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

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

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  ObjectSticker retSticker(int face)
478
    {
479
    if( mStickers==null )
480
      {
481
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
482
      float radius = 0.06f;
483
      float stroke = 0.07f;
484
      float[] radii = new float[] {radius,radius,radius};
485
      mStickers     = new ObjectSticker[STICKERS.length];
486
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
487
      }
488

    
489
    return mStickers[face/NUM_FACE_COLORS];
490
    }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493
// PUBLIC API
494

    
495
  public Static3D[] getRotationAxis()
496
    {
497
    return ROT_AXIS;
498
    }
499

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

    
502
  public Movement getMovement()
503
    {
504
    if( mMovement==null )
505
      {
506
      int numLayers = getNumLayers();
507
      if( mCuts==null ) getCuts(numLayers);
508
      getLayerRotatable(numLayers);
509
      mMovement = new Movement8(ROT_AXIS,mCuts,mLayerRotatable,numLayers,TYPE_NOT_SPLIT,NUM_ENABLED,ENABLED);
510
      }
511
    return mMovement;
512
    }
513

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515

    
516
  public int[] getBasicAngle()
517
    {
518
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
519
    return mBasicAngle;
520
    }
521

    
522
///////////////////////////////////////////////////////////////////////////////////////////////////
523

    
524
  public int getObjectName(int numLayers)
525
    {
526
    switch(numLayers)
527
      {
528
      case 2: return R.string.diam2;
529
      case 3: return R.string.diam3;
530
      case 4: return R.string.diam4;
531
      }
532

    
533
    return 0;
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
  public int getInventor(int numLayers)
539
    {
540
    switch(numLayers)
541
      {
542
      case 2: return R.string.diam2_inventor;
543
      case 3: return R.string.diam3_inventor;
544
      case 4: return R.string.diam4_inventor;
545
      }
546

    
547
    return 0;
548
    }
549

    
550
///////////////////////////////////////////////////////////////////////////////////////////////////
551

    
552
  public int getComplexity(int numLayers)
553
    {
554
    switch(numLayers)
555
      {
556
      case 2: return 4;
557
      case 3: return 6;
558
      case 4: return 8;
559
      }
560

    
561
    return 0;
562
    }
563
}
(18-18/38)