Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ ef018c1b

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.helpers.ScrambleState;
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
import org.distorted.main.R;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

    
47
  private ScrambleState[] mStates;
48
  private int[] mBasicAngle;
49
  private int[] mFaceMap;
50
  private float[][] mCuts;
51
  private boolean[][] mLayerRotatable;
52
  private Static4D[] mQuats;
53
  private int[] mTetraToFaceMap;
54
  private ObjectSticker[] mStickers;
55
  private Movement mMovement;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
60
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
61
    {
62
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  ScrambleState[] getScrambleStates()
68
    {
69
    if( mStates==null )
70
      {
71
      int size = getNumLayers();
72
      int[] tmp = new int[3*2*size];
73

    
74
      for(int i=0; i<2*size; i++)
75
        {
76
        tmp[3*i  ] = (i<size) ?  i:i-size;
77
        tmp[3*i+1] = (i%2==0) ? -1:1;
78
        tmp[3*i+2] = 0;
79
        }
80

    
81
      mStates = new ScrambleState[]
82
        {
83
        new ScrambleState( new int[][] {tmp,tmp,tmp,tmp} )
84
        };
85
      }
86

    
87
    return mStates;
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  private void initializeQuats()
93
    {
94
    mQuats = new Static4D[]
95
         {
96
          new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
97
          new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
98
          new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
99
          new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
100

    
101
          new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
102
          new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
103
          new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
104
          new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
105
          new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
106
          new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
107
          new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
108
          new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
109
         };
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  int[] getSolvedQuats(int cubit, int numLayers)
115
    {
116
    if( mQuats==null ) initializeQuats();
117
    if( mFaceMap==null ) mFaceMap = new int[] {4,0,6,2,7,3,5,1};
118
    int status = retCubitSolvedStatus(cubit,numLayers);
119
    return status<0 ? null : buildSolvedQuats(MovementDiamond.FACE_AXIS[mFaceMap[status]],mQuats);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  Static4D[] getQuats()
125
    {
126
    if( mQuats==null ) initializeQuats();
127
    return mQuats;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  int getSolvedFunctionIndex()
133
    {
134
    return 0;
135
    }
136

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

    
139
  int getNumStickerTypes(int numLayers)
140
    {
141
    return 1;
142
    }
143

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

    
146
  float[][] getCuts(int numLayers)
147
    {
148
    if( numLayers<2 ) return null;
149

    
150
    if( mCuts==null )
151
      {
152
      mCuts = new float[4][numLayers-1];
153
      float cut = (SQ6/6)*(2-numLayers);
154

    
155
      for(int i=0; i<numLayers-1; i++)
156
        {
157
        mCuts[0][i] = cut;
158
        mCuts[1][i] = cut;
159
        mCuts[2][i] = cut;
160
        mCuts[3][i] = cut;
161
        cut += SQ6/3;
162
        }
163
      }
164

    
165
    return mCuts;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void getLayerRotatable(int numLayers)
171
    {
172
    if( mLayerRotatable==null )
173
      {
174
      int numAxis = ROT_AXIS.length;
175
      boolean[] tmp = new boolean[numLayers];
176
      for(int i=0; i<numLayers; i++) tmp[i] = true;
177
      mLayerRotatable = new boolean[numAxis][];
178
      for(int i=0; i<numAxis; i++) mLayerRotatable[i] = tmp;
179
      }
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  int getNumCubitFaces()
185
    {
186
    return 8;
187
    }
188

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

    
191
  private int getNumOctahedrons(int layers)
192
    {
193
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
194
    }
195

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

    
198
  private int getNumTetrahedrons(int layers)
199
    {
200
    return 4*layers*(layers-1);
201
    }
202

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

    
205
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
206
    {
207
    float x = (layers-1)*0.5f;
208
    float z = (layers+1)*0.5f;
209

    
210
    for(int i=0; i<layers; i++, index++)
211
      {
212
      z -= 1;
213
      centers[index][0] = x;
214
      centers[index][1] = height;
215
      centers[index][2] = z;
216
      }
217

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

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

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

    
242
    return index;
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
248
    {
249
    float x = (layers-1)*0.5f;
250
    float z =  layers*0.5f;
251

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

    
260
    x += 0.5f;
261
    z -= 0.5f;
262

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

    
271
    x -= 0.5f;
272
    z -= 0.5f;
273

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

    
282
    x -= 0.5f;
283
    z += 0.5f;
284

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

    
293
    return index;
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  float[][] getCubitPositions(int layers)
299
    {
300
    int numO = getNumOctahedrons(layers);
301
    int numT = getNumTetrahedrons(layers);
302
    int index = 0;
303
    float height = 0.0f;
304

    
305
    float[][] CENTERS = new float[numO+numT][3];
306

    
307
    index = createOctaPositions(CENTERS,index,layers,height);
308

    
309
    for(int i=layers-1; i>0; i--)
310
      {
311
      height += SQ2/2;
312
      index = createOctaPositions(CENTERS,index,i,+height);
313
      index = createOctaPositions(CENTERS,index,i,-height);
314
      }
315

    
316
    height = SQ2/4;
317

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

    
325
    return CENTERS;
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
331
    {
332
    if( mTetraToFaceMap==null ) mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
333

    
334
    for(int i=numLayers-1; i>0; i--)
335
      {
336
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
337
      tetra -= 8*i;
338
      }
339

    
340
    return -1;
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  ObjectShape getObjectShape(int cubit, int numLayers)
346
    {
347
    int variant = getCubitVariant(cubit,numLayers);
348
    int N = numLayers>3 ? 5:6;
349
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
350

    
351
    if( variant==0 )
352
      {
353
      double[][] vertices = new double[][]
354
          {
355
             { 0.5,   0.0, 0.5},
356
             { 0.5,   0.0,-0.5},
357
             {-0.5,   0.0,-0.5},
358
             {-0.5,   0.0, 0.5},
359
             { 0.0, SQ2/2, 0.0},
360
             { 0.0,-SQ2/2, 0.0}
361
          };
362

    
363
      int[][] vert_indices = new int[][]
364
          {
365
             {3,0,4},
366
             {0,1,4},
367
             {1,2,4},
368
             {2,3,4},
369
             {5,0,3},
370
             {5,1,0},
371
             {5,2,1},
372
             {5,3,2}
373
          };
374

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

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
  Static4D getQuat(int cubit, int numLayers)
400
    {
401
    if( mQuats==null ) initializeQuats();
402
    int numO = getNumOctahedrons(numLayers);
403

    
404
    if( cubit<numO ) return mQuats[0];
405

    
406
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
407
      {
408
      case 0: return mQuats[0];                         // unit quat
409
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
410
      case 2: return mQuats[1];                         // 180 along Y
411
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
412
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
413
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
414
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
415
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
416
      }
417

    
418
    return null;
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
  int getNumCubitVariants(int numLayers)
424
    {
425
    return 2;
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
  int getCubitVariant(int cubit, int numLayers)
431
    {
432
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
433
    }
434

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

    
437
  int getFaceColor(int cubit, int cubitface, int size)
438
    {
439
    int numO = getNumOctahedrons(size);
440

    
441
    if( cubit<numO )
442
      {
443
      int axis = 0;
444
      int layer= 1;
445

    
446
      switch(cubitface)
447
        {
448
        case 0: axis = 2; layer =             1; break;
449
        case 1: axis = 0; layer = (1<<(size-1)); break;
450
        case 2: axis = 3; layer =             1; break;
451
        case 3: axis = 1; layer = (1<<(size-1)); break;
452
        case 4: axis = 3; layer = (1<<(size-1)); break;
453
        case 5: axis = 1; layer =             1; break;
454
        case 6: axis = 2; layer = (1<<(size-1)); break;
455
        case 7: axis = 0; layer =             1; break;
456
        }
457

    
458
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
459
      }
460
    else
461
      {
462
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
463
      }
464
    }
465

    
466
///////////////////////////////////////////////////////////////////////////////////////////////////
467

    
468
  ObjectSticker retSticker(int face)
469
    {
470
    if( mStickers==null )
471
      {
472
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
473
      float radius = 0.06f;
474
      float stroke = 0.07f;
475
      float[] radii = new float[] {radius,radius,radius};
476
      mStickers     = new ObjectSticker[STICKERS.length];
477
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
478
      }
479

    
480
    return mStickers[face/NUM_FACE_COLORS];
481
    }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484
// PUBLIC API
485

    
486
  public Static3D[] getRotationAxis()
487
    {
488
    return ROT_AXIS;
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  public Movement getMovement()
494
    {
495
    if( mMovement==null )
496
      {
497
      int numLayers = getNumLayers();
498
      if( mCuts==null ) getCuts(numLayers);
499
      getLayerRotatable(numLayers);
500

    
501
      mMovement = new MovementDiamond(mCuts,mLayerRotatable,numLayers);
502
      }
503
    return mMovement;
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  public int[] getBasicAngle()
509
    {
510
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
511
    return mBasicAngle;
512
    }
513

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

    
516
  public int getObjectName(int numLayers)
517
    {
518
    switch(numLayers)
519
      {
520
      case 2: return R.string.diam2;
521
      case 3: return R.string.diam3;
522
      case 4: return R.string.diam4;
523
      }
524

    
525
    return 0;
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

    
530
  public int getInventor(int numLayers)
531
    {
532
    switch(numLayers)
533
      {
534
      case 2: return R.string.diam2_inventor;
535
      case 3: return R.string.diam3_inventor;
536
      case 4: return R.string.diam4_inventor;
537
      }
538

    
539
    return 0;
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543

    
544
  public int getComplexity(int numLayers)
545
    {
546
    switch(numLayers)
547
      {
548
      case 2: return 4;
549
      case 3: return 6;
550
      case 4: return 8;
551
      }
552

    
553
    return 0;
554
    }
555
}
(28-28/48)