Project

General

Profile

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

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

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 TwistyObject
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 static final int[] FACE_COLORS = new int[]
48
         {
49
           COLOR_ORANGE, COLOR_VIOLET,
50
           COLOR_WHITE , COLOR_BLUE  ,
51
           COLOR_YELLOW, COLOR_RED   ,
52
           COLOR_GREEN , COLOR_GREY
53
         };
54

    
55
  private static final int FACES_PER_CUBIT =8;
56

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

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

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

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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

    
95
    return mStates;
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

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

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

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

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

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

    
132
  float getScreenRatio()
133
    {
134
    return 0.65f;
135
    }
136

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

    
139
  Static4D[] getQuats()
140
    {
141
    if( mQuats==null ) initializeQuats();
142
    return mQuats;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  int getNumFaceColors()
148
    {
149
    return FACE_COLORS.length;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  boolean shouldResetTextureMaps()
155
    {
156
    return false;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  int getSolvedFunctionIndex()
162
    {
163
    return 0;
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  int getNumStickerTypes(int numLayers)
169
    {
170
    return 1;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  float[][] getCuts(int numLayers)
176
    {
177
    if( numLayers<2 )
178
      {
179
      return null;
180
      }
181
    else
182
      {
183
      float[][] cuts = new float[4][numLayers-1];
184
      float dist = SQ6/3;
185
      float cut  = 0.5f*dist*(2-numLayers);
186

    
187
      for(int i=0; i<numLayers-1; i++)
188
        {
189
        cuts[0][i] = cut;
190
        cuts[1][i] = cut;
191
        cuts[2][i] = cut;
192
        cuts[3][i] = cut;
193
        cut += dist;
194
        }
195

    
196
      return cuts;
197
      }
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  int getNumCubitFaces()
203
    {
204
    return FACES_PER_CUBIT;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  private int getNumOctahedrons(int layers)
210
    {
211
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  private int getNumTetrahedrons(int layers)
217
    {
218
    return 4*layers*(layers-1);
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
224
    {
225
    float x = (layers-1)*0.5f;
226
    float z = (layers+1)*0.5f;
227

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

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

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

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

    
260
    return index;
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
266
    {
267
    float x = (layers-1)*0.5f;
268
    float z =  layers*0.5f;
269

    
270
    for(int i=0; i<layers-1; i++, index++)
271
      {
272
      z -= 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
      x -= 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
      z += 1;
295
      centers[index][0] = x;
296
      centers[index][1] = height;
297
      centers[index][2] = z;
298
      }
299

    
300
    x -= 0.5f;
301
    z += 0.5f;
302

    
303
    for(int i=0; i<layers-1; i++, index++)
304
      {
305
      x += 1;
306
      centers[index][0] = x;
307
      centers[index][1] = height;
308
      centers[index][2] = z;
309
      }
310

    
311
    return index;
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  float[][] getCubitPositions(int layers)
317
    {
318
    int numO = getNumOctahedrons(layers);
319
    int numT = getNumTetrahedrons(layers);
320
    int index = 0;
321
    float height = 0.0f;
322

    
323
    float[][] CENTERS = new float[numO+numT][3];
324

    
325
    index = createOctaPositions(CENTERS,index,layers,height);
326

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

    
334
    height = SQ2/4;
335

    
336
    for(int i=layers; i>1; i--)
337
      {
338
      index = createTetraPositions(CENTERS,index,i,+height);
339
      index = createTetraPositions(CENTERS,index,i,-height);
340
      height += SQ2/2;
341
      }
342

    
343
    return CENTERS;
344
    }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
349
    {
350
    if( mTetraToFaceMap==null ) mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
351

    
352
    for(int i=numLayers-1; i>0; i--)
353
      {
354
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
355
      tetra -= 8*i;
356
      }
357

    
358
    return -1;
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362

    
363
  ObjectShape getObjectShape(int cubit, int numLayers)
364
    {
365
    int variant = getCubitVariant(cubit,numLayers);
366
    int N = numLayers>3 ? 5:6;
367
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
368

    
369
    if( variant==0 )
370
      {
371
      double[][] vertices = new double[][]
372
          {
373
             { 0.5,   0.0, 0.5},
374
             { 0.5,   0.0,-0.5},
375
             {-0.5,   0.0,-0.5},
376
             {-0.5,   0.0, 0.5},
377
             { 0.0, SQ2/2, 0.0},
378
             { 0.0,-SQ2/2, 0.0}
379
          };
380

    
381
      int[][] vert_indices = new int[][]
382
          {
383
             {3,0,4},
384
             {0,1,4},
385
             {1,2,4},
386
             {2,3,4},
387
             {5,0,3},
388
             {5,1,0},
389
             {5,2,1},
390
             {5,3,2}
391
          };
392

    
393
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
394
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
395
      float[][] corners   = new float[][] { {0.04f,0.20f} };
396
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
397
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
398
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
399
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
400
      }
401
    else
402
      {
403
      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} };
404
      int[][] vert_indices = new int[][]  { {2,1,0}, {2,3,1}, {3,2,0}, {3,0,1} };
405
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
406
      int[] bandIndices   = new int[] { 0,0,0,0 };
407
      float[][] corners   = new float[][] { {0.08f,0.15f} };
408
      int[] cornerIndices = new int[] { 0,0,0,0 };
409
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
410
      int[] centerIndices = new int[] { 0,0,0,0 };
411
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
412
      }
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  Static4D getQuat(int cubit, int numLayers)
418
    {
419
    if( mQuats==null ) initializeQuats();
420
    int numO = getNumOctahedrons(numLayers);
421

    
422
    if( cubit<numO ) return mQuats[0];
423

    
424
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
425
      {
426
      case 0: return mQuats[0];                         // unit quat
427
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
428
      case 2: return mQuats[1];                         // 180 along Y
429
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
430
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
431
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
432
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
433
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
434
      }
435

    
436
    return null;
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  int getNumCubitVariants(int numLayers)
442
    {
443
    return 2;
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  int getCubitVariant(int cubit, int numLayers)
449
    {
450
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
451
    }
452

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

    
455
  int getFaceColor(int cubit, int cubitface, int size)
456
    {
457
    int numO = getNumOctahedrons(size);
458

    
459
    if( cubit<numO )
460
      {
461
      int axis = 0;
462
      int layer= 1;
463

    
464
      switch(cubitface)
465
        {
466
        case 0: axis = 2; layer =             1; break;
467
        case 1: axis = 0; layer = (1<<(size-1)); break;
468
        case 2: axis = 3; layer =             1; break;
469
        case 3: axis = 1; layer = (1<<(size-1)); break;
470
        case 4: axis = 3; layer = (1<<(size-1)); break;
471
        case 5: axis = 1; layer =             1; break;
472
        case 6: axis = 2; layer = (1<<(size-1)); break;
473
        case 7: axis = 0; layer =             1; break;
474
        }
475

    
476
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
477
      }
478
    else
479
      {
480
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
481
      }
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
  int getColor(int face)
487
    {
488
    return FACE_COLORS[face];
489
    }
490

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

    
493
  ObjectSticker retSticker(int face)
494
    {
495
    if( mStickers==null )
496
      {
497
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
498
      float radius = 0.06f;
499
      float stroke = 0.07f;
500
      float[] radii = new float[] {radius,radius,radius};
501
      mStickers     = new ObjectSticker[STICKERS.length];
502
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
503
      }
504

    
505
    return mStickers[face/NUM_FACE_COLORS];
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

    
510
  float returnMultiplier()
511
    {
512
    return 1.5f;
513
    }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516
// PUBLIC API
517

    
518
  public Static3D[] getRotationAxis()
519
    {
520
    return ROT_AXIS;
521
    }
522

    
523
///////////////////////////////////////////////////////////////////////////////////////////////////
524

    
525
  public Movement getMovement()
526
    {
527
    if( mMovement==null ) mMovement = new MovementDiamond();
528
    return mMovement;
529
    }
530

    
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532

    
533
  public int[] getBasicAngle()
534
    {
535
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
536
    return mBasicAngle;
537
    }
538

    
539
///////////////////////////////////////////////////////////////////////////////////////////////////
540

    
541
  public int getObjectName(int numLayers)
542
    {
543
    switch(numLayers)
544
      {
545
      case 2: return R.string.diam2;
546
      case 3: return R.string.diam3;
547
      case 4: return R.string.diam4;
548
      }
549

    
550
    return 0;
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554

    
555
  public int getInventor(int numLayers)
556
    {
557
    switch(numLayers)
558
      {
559
      case 2: return R.string.diam2_inventor;
560
      case 3: return R.string.diam3_inventor;
561
      case 4: return R.string.diam4_inventor;
562
      }
563

    
564
    return 0;
565
    }
566

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568

    
569
  public int getComplexity(int numLayers)
570
    {
571
    switch(numLayers)
572
      {
573
      case 2: return 4;
574
      case 3: return 6;
575
      case 4: return 8;
576
      }
577

    
578
    return 0;
579
    }
580
}
(24-24/44)