Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 5da517d3

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

    
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(MovementDiamond.FACE_AXIS[mFaceMap[status]],mQuats);
127
    }
128

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

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

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

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

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

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

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

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

    
195
      return cuts;
196
      }
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

    
227
    for(int i=0; i<layers; i++, index++)
228
      {
229
      z -= 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
      x -= 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-1; i++, index++)
244
      {
245
      z += 1;
246
      centers[index][0] = x;
247
      centers[index][1] = height;
248
      centers[index][2] = z;
249
      }
250

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

    
259
    return index;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

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

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

    
277
    x += 0.5f;
278
    z -= 0.5f;
279

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

    
288
    x -= 0.5f;
289
    z -= 0.5f;
290

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

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

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

    
310
    return index;
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

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

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

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

    
333
    height = SQ2/4;
334

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

    
342
    return CENTERS;
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

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

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

    
357
    return -1;
358
    }
359

    
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361

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

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

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

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

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

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

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

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

    
435
    return null;
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

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

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446

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

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

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

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

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

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

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

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

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491

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

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

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508

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

    
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515
// PUBLIC API
516

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

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

    
524
  public int[] getBasicAngle()
525
    {
526
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
527
    return mBasicAngle;
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
  public int getObjectName(int numLayers)
533
    {
534
    switch(numLayers)
535
      {
536
      case 2: return R.string.diam2;
537
      case 3: return R.string.diam3;
538
      case 4: return R.string.diam4;
539
      }
540

    
541
    return 0;
542
    }
543

    
544
///////////////////////////////////////////////////////////////////////////////////////////////////
545

    
546
  public int getInventor(int numLayers)
547
    {
548
    switch(numLayers)
549
      {
550
      case 2: return R.string.diam2_inventor;
551
      case 3: return R.string.diam3_inventor;
552
      case 4: return R.string.diam4_inventor;
553
      }
554

    
555
    return 0;
556
    }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

    
560
  public int getComplexity(int numLayers)
561
    {
562
    switch(numLayers)
563
      {
564
      case 2: return 4;
565
      case 3: return 6;
566
      case 4: return 8;
567
      }
568

    
569
    return 0;
570
    }
571
}
(27-27/47)