Project

General

Profile

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

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

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 Static4D[] mQuats;
51
  private int[] mTetraToFaceMap;
52
  private ObjectSticker[] mStickers;
53
  private Movement mMovement;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

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

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

    
85
    return mStates;
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  int getSolvedFunctionIndex()
131
    {
132
    return 0;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  int getNumStickerTypes(int numLayers)
138
    {
139
    return 1;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  float[][] getCuts(int numLayers)
145
    {
146
    if( numLayers<2 )
147
      {
148
      return null;
149
      }
150
    else
151
      {
152
      float[][] cuts = new float[4][numLayers-1];
153
      float dist = SQ6/3;
154
      float cut  = 0.5f*dist*(2-numLayers);
155

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

    
165
      return cuts;
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  int getNumCubitFaces()
172
    {
173
    return 8;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private int getNumOctahedrons(int layers)
179
    {
180
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  private int getNumTetrahedrons(int layers)
186
    {
187
    return 4*layers*(layers-1);
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  private int createOctaPositions(float[][] centers, int index, int layers, float height)
193
    {
194
    float x = (layers-1)*0.5f;
195
    float z = (layers+1)*0.5f;
196

    
197
    for(int i=0; i<layers; i++, index++)
198
      {
199
      z -= 1;
200
      centers[index][0] = x;
201
      centers[index][1] = height;
202
      centers[index][2] = z;
203
      }
204

    
205
    for(int i=0; i<layers-1; i++, index++)
206
      {
207
      x -= 1;
208
      centers[index][0] = x;
209
      centers[index][1] = height;
210
      centers[index][2] = z;
211
      }
212

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

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

    
229
    return index;
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  private int createTetraPositions(float[][] centers, int index, int layers, float height)
235
    {
236
    float x = (layers-1)*0.5f;
237
    float z =  layers*0.5f;
238

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

    
247
    x += 0.5f;
248
    z -= 0.5f;
249

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

    
258
    x -= 0.5f;
259
    z -= 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
    return index;
281
    }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
  float[][] getCubitPositions(int layers)
286
    {
287
    int numO = getNumOctahedrons(layers);
288
    int numT = getNumTetrahedrons(layers);
289
    int index = 0;
290
    float height = 0.0f;
291

    
292
    float[][] CENTERS = new float[numO+numT][3];
293

    
294
    index = createOctaPositions(CENTERS,index,layers,height);
295

    
296
    for(int i=layers-1; i>0; i--)
297
      {
298
      height += SQ2/2;
299
      index = createOctaPositions(CENTERS,index,i,+height);
300
      index = createOctaPositions(CENTERS,index,i,-height);
301
      }
302

    
303
    height = SQ2/4;
304

    
305
    for(int i=layers; i>1; i--)
306
      {
307
      index = createTetraPositions(CENTERS,index,i,+height);
308
      index = createTetraPositions(CENTERS,index,i,-height);
309
      height += SQ2/2;
310
      }
311

    
312
    return CENTERS;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
318
    {
319
    if( mTetraToFaceMap==null ) mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
320

    
321
    for(int i=numLayers-1; i>0; i--)
322
      {
323
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
324
      tetra -= 8*i;
325
      }
326

    
327
    return -1;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331

    
332
  ObjectShape getObjectShape(int cubit, int numLayers)
333
    {
334
    int variant = getCubitVariant(cubit,numLayers);
335
    int N = numLayers>3 ? 5:6;
336
    int E = numLayers>2 ? (numLayers>3 ? 0:1):2;
337

    
338
    if( variant==0 )
339
      {
340
      double[][] vertices = new double[][]
341
          {
342
             { 0.5,   0.0, 0.5},
343
             { 0.5,   0.0,-0.5},
344
             {-0.5,   0.0,-0.5},
345
             {-0.5,   0.0, 0.5},
346
             { 0.0, SQ2/2, 0.0},
347
             { 0.0,-SQ2/2, 0.0}
348
          };
349

    
350
      int[][] vert_indices = new int[][]
351
          {
352
             {3,0,4},
353
             {0,1,4},
354
             {1,2,4},
355
             {2,3,4},
356
             {5,0,3},
357
             {5,1,0},
358
             {5,2,1},
359
             {5,3,2}
360
          };
361

    
362
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
363
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
364
      float[][] corners   = new float[][] { {0.04f,0.20f} };
365
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
366
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
367
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
368
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
369
      }
370
    else
371
      {
372
      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} };
373
      int[][] vert_indices = new int[][]  { {2,1,0}, {2,3,1}, {3,2,0}, {3,0,1} };
374
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
375
      int[] bandIndices   = new int[] { 0,0,0,0 };
376
      float[][] corners   = new float[][] { {0.08f,0.15f} };
377
      int[] cornerIndices = new int[] { 0,0,0,0 };
378
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
379
      int[] centerIndices = new int[] { 0,0,0,0 };
380
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
381
      }
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  Static4D getQuat(int cubit, int numLayers)
387
    {
388
    if( mQuats==null ) initializeQuats();
389
    int numO = getNumOctahedrons(numLayers);
390

    
391
    if( cubit<numO ) return mQuats[0];
392

    
393
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
394
      {
395
      case 0: return mQuats[0];                         // unit quat
396
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
397
      case 2: return mQuats[1];                         // 180 along Y
398
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
399
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
400
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
401
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
402
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
403
      }
404

    
405
    return null;
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409

    
410
  int getNumCubitVariants(int numLayers)
411
    {
412
    return 2;
413
    }
414

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

    
417
  int getCubitVariant(int cubit, int numLayers)
418
    {
419
    return cubit<getNumOctahedrons(numLayers) ? 0 : 1;
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  int getFaceColor(int cubit, int cubitface, int size)
425
    {
426
    int numO = getNumOctahedrons(size);
427

    
428
    if( cubit<numO )
429
      {
430
      int axis = 0;
431
      int layer= 1;
432

    
433
      switch(cubitface)
434
        {
435
        case 0: axis = 2; layer =             1; break;
436
        case 1: axis = 0; layer = (1<<(size-1)); break;
437
        case 2: axis = 3; layer =             1; break;
438
        case 3: axis = 1; layer = (1<<(size-1)); break;
439
        case 4: axis = 3; layer = (1<<(size-1)); break;
440
        case 5: axis = 1; layer =             1; break;
441
        case 6: axis = 2; layer = (1<<(size-1)); break;
442
        case 7: axis = 0; layer =             1; break;
443
        }
444

    
445
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
446
      }
447
    else
448
      {
449
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
450
      }
451
    }
452

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

    
455
  ObjectSticker retSticker(int face)
456
    {
457
    if( mStickers==null )
458
      {
459
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
460
      float radius = 0.06f;
461
      float stroke = 0.07f;
462
      float[] radii = new float[] {radius,radius,radius};
463
      mStickers     = new ObjectSticker[STICKERS.length];
464
      mStickers[0]  = new ObjectSticker(STICKERS[0],null,radii,stroke);
465
      }
466

    
467
    return mStickers[face/NUM_FACE_COLORS];
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471
// PUBLIC API
472

    
473
  public Static3D[] getRotationAxis()
474
    {
475
    return ROT_AXIS;
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479

    
480
  public Movement getMovement()
481
    {
482
    if( mMovement==null ) mMovement = new MovementDiamond();
483
    return mMovement;
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487

    
488
  public int[] getBasicAngle()
489
    {
490
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
491
    return mBasicAngle;
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
  public int getObjectName(int numLayers)
497
    {
498
    switch(numLayers)
499
      {
500
      case 2: return R.string.diam2;
501
      case 3: return R.string.diam3;
502
      case 4: return R.string.diam4;
503
      }
504

    
505
    return 0;
506
    }
507

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

    
510
  public int getInventor(int numLayers)
511
    {
512
    switch(numLayers)
513
      {
514
      case 2: return R.string.diam2_inventor;
515
      case 3: return R.string.diam3_inventor;
516
      case 4: return R.string.diam4_inventor;
517
      }
518

    
519
    return 0;
520
    }
521

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

    
524
  public int getComplexity(int numLayers)
525
    {
526
    switch(numLayers)
527
      {
528
      case 2: return 4;
529
      case 3: return 6;
530
      case 4: return 8;
531
      }
532

    
533
    return 0;
534
    }
535
}
(28-28/48)