Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 7ee89540

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[][][] ENABLED = new int[][][]
50
      {
51
          {{1,2,3}},{{1,2,3}},{{0,2,3}},{{0,2,3}},{{0,1,3}},{{0,1,3}},{{0,1,2}},{{0,1,2}}
52
      };
53

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
81
      for(int i=0; i<2*size; i++)
82
        {
83
        tmp[3*i  ] = (i<size) ?  i:i-size;
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
  private void initializeQuats()
100
    {
101
    mQuats = new Static4D[]
102
         {
103
          new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
104
          new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
105
          new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
106
          new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
107

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  int getSolvedFunctionIndex()
140
    {
141
    return 0;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  int getNumStickerTypes(int numLayers)
147
    {
148
    return 1;
149
    }
150

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

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

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

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

    
172
    return mCuts;
173
    }
174

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

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

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

    
191
  int getNumCubitFaces()
192
    {
193
    return 8;
194
    }
195

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

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

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

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

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

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

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

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

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

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

    
249
    return index;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

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

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

    
267
    x += 0.5f;
268
    z -= 0.5f;
269

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

    
278
    x -= 0.5f;
279
    z -= 0.5f;
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
    x -= 0.5f;
290
    z += 0.5f;
291

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

    
300
    return index;
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  float[][] getCubitPositions(int layers)
306
    {
307
    int numO = getNumOctahedrons(layers);
308
    int numT = getNumTetrahedrons(layers);
309
    int index = 0;
310
    float height = 0.0f;
311

    
312
    float[][] CENTERS = new float[numO+numT][3];
313

    
314
    index = createOctaPositions(CENTERS,index,layers,height);
315

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

    
323
    height = SQ2/4;
324

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

    
332
    return CENTERS;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

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

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

    
347
    return -1;
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

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

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

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

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

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

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

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

    
413
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
414
      {
415
      case 0: return mQuats[0];                         // unit quat
416
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
417
      case 2: return mQuats[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
  ObjectSticker retSticker(int face)
476
    {
477
    if( mStickers==null )
478
      {
479
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
480
      float radius = 0.06f;
481
      float stroke = 0.07f;
482
      float[] radii = new float[] {radius,radius,radius};
483
      mStickers     = new ObjectSticker[STICKERS.length];
484
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
485
      }
486

    
487
    return mStickers[face/NUM_FACE_COLORS];
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491
// PUBLIC API
492

    
493
  public Static3D[] getRotationAxis()
494
    {
495
    return ROT_AXIS;
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

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

    
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513

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

    
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521

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

    
531
    return 0;
532
    }
533

    
534
///////////////////////////////////////////////////////////////////////////////////////////////////
535

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

    
545
    return 0;
546
    }
547

    
548
///////////////////////////////////////////////////////////////////////////////////////////////////
549

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

    
559
    return 0;
560
    }
561
}
(18-18/38)