Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ 749ef882

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
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.helpers.FactoryCubit;
27
import org.distorted.helpers.FactorySticker;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshBase;
32
import org.distorted.library.mesh.MeshSquare;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35
import org.distorted.main.R;
36
import org.distorted.main.RubikSurfaceView;
37

    
38
import java.util.Random;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class TwistyDiamond extends TwistyObject
43
{
44
  private static final int FACES_PER_CUBIT =8;
45

    
46
  // the four rotation axis of a Diamond. Must be normalized.
47
  static final Static3D[] ROT_AXIS = new Static3D[]
48
         {
49
           new Static3D(+SQ6/3,+SQ3/3,     0),
50
           new Static3D(-SQ6/3,+SQ3/3,     0),
51
           new Static3D(     0,-SQ3/3,-SQ6/3),
52
           new Static3D(     0,-SQ3/3,+SQ6/3)
53
         };
54

    
55
  private static final int[] FACE_COLORS = new int[]
56
         {
57
           COLOR_ORANGE, COLOR_VIOLET,
58
           COLOR_WHITE , COLOR_BLUE  ,
59
           COLOR_YELLOW, COLOR_RED   ,
60
           COLOR_GREEN , COLOR_GREY
61
         };
62

    
63
  // All legal rotation quats of a Diamond: unit + three 180 deg turns + 8 generators
64
  private static final Static4D[] QUATS = new Static4D[]
65
         {
66
           new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
67
           new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
68
           new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
69
           new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
70

    
71
           new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
72
           new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
73
           new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
74
           new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
75
           new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
76
           new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
77
           new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
78
           new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
79
         };
80

    
81
  private static final float DIST = 0.50f;
82

    
83
  private static final int[][] mFaceNeutralQuatIndex = new int[][]
84
         {
85
             {6,10},
86
             {4, 8},
87
             {7,11},
88
             {5, 9},
89
             {7,11},
90
             {5, 9},
91
             {6,10},
92
             {4, 8}
93
         };
94

    
95
  private static final int[] mTetraToFaceMap = new int[] {1,2,3,0,5,6,7,4};
96

    
97
  private static MeshBase mOctaMesh, mTetraMesh;
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
102
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
103
    {
104
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  float getScreenRatio()
110
    {
111
    return 0.65f;
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  Static4D[] getQuats()
117
    {
118
    return QUATS;
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  int getNumFaces()
124
    {
125
    return FACE_COLORS.length;
126
    }
127

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

    
130
  boolean shouldResetTextureMaps()
131
    {
132
    return false;
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[numLayers-1];
153
      float dist = SQ6*0.666f*DIST;
154
      float cut  = 0.5f*dist*(2-numLayers);
155

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

    
162
      return cuts;
163
      }
164
    }
165

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

    
168
  int getNumCubitFaces()
169
    {
170
    return FACES_PER_CUBIT;
171
    }
172

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

    
175
  private int getNumOctahedrons(int layers)
176
    {
177
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  private int getNumTetrahedrons(int layers)
183
    {
184
    return 4*layers*(layers-1);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private int createOctahedrons(float[][] centers, int index, int layers, float height)
190
    {
191
    float x = DIST*(layers-1);
192
    float z = DIST*(layers+1);
193

    
194
    for(int i=0; i<layers; i++, index++)
195
      {
196
      z -= 2*DIST;
197
      centers[index][0] = x;
198
      centers[index][1] = height;
199
      centers[index][2] = z;
200
      }
201

    
202
    for(int i=0; i<layers-1; i++, index++)
203
      {
204
      x -= 2*DIST;
205
      centers[index][0] = x;
206
      centers[index][1] = height;
207
      centers[index][2] = z;
208
      }
209

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

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

    
226
    return index;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  private int createTetrahedrons(float[][] centers, int index, int layers, float height)
232
    {
233
    float x = DIST*(layers-1);
234
    float z = DIST*layers;
235

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

    
244
    x += DIST;
245
    z -= DIST;
246

    
247
    for(int i=0; i<layers-1; i++, index++)
248
      {
249
      x -= 2*DIST;
250
      centers[index][0] = x;
251
      centers[index][1] = height;
252
      centers[index][2] = z;
253
      }
254

    
255
    x -= DIST;
256
    z -= DIST;
257

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

    
266
    x -= DIST;
267
    z += DIST;
268

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

    
277
    return index;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

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

    
289
    float[][] CENTERS = new float[numO+numT][3];
290

    
291
    index = createOctahedrons(CENTERS,index,layers,height);
292

    
293
    for(int i=layers-1; i>0; i--)
294
      {
295
      height += SQ2*DIST;
296
      index = createOctahedrons(CENTERS,index,i,+height);
297
      index = createOctahedrons(CENTERS,index,i,-height);
298
      }
299

    
300
    height = DIST*SQ2/2;
301

    
302
    for(int i=layers; i>1; i--)
303
      {
304
      index = createTetrahedrons(CENTERS,index,i,+height);
305
      index = createTetrahedrons(CENTERS,index,i,-height);
306
      height += SQ2*DIST;
307
      }
308

    
309
    return CENTERS;
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
315
    {
316
    for(int i=numLayers-1; i>0; i--)
317
      {
318
      if( tetra < 8*i ) return mTetraToFaceMap[tetra/i];
319
      tetra -= 8*i;
320
      }
321

    
322
    return -1;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  private Static4D getQuat(int cubit, int numLayers, int numO)
328
    {
329
    if( cubit<numO ) return QUATS[0];
330

    
331
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
332
      {
333
      case 0: return QUATS[0];                          // unit quat
334
      case 1: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
335
      case 2: return QUATS[1];                          // 180 along Y
336
      case 3: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along
337
      case 4: return new Static4D(0,     0,1,    0);    // 180 along Z
338
      case 5: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
339
      case 6: return new Static4D(     1,0,0,    0);    // 180 along X
340
      case 7: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
341
      }
342

    
343
    return null;
344
    }
345

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

    
348
  MeshBase createCubitMesh(int cubit, int numLayers)
349
    {
350
    MeshBase mesh;
351
    int numO = getNumOctahedrons(numLayers);
352

    
353
    if( cubit<numO )
354
      {
355
      if( mOctaMesh==null ) mOctaMesh = FactoryCubit.getInstance().createOctaMesh();
356
      mesh = mOctaMesh.copy(true);
357
      }
358
    else
359
      {
360
      if( mTetraMesh==null ) mTetraMesh = FactoryCubit.getInstance().createTetraMesh();
361
      mesh = mTetraMesh.copy(true);
362
      }
363

    
364
    Static4D sQ = getQuat(cubit,numLayers,numO);
365
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( sQ, new Static3D(0,0,0) );
366
    mesh.apply(quat,0xffffffff,0);
367

    
368
    return mesh;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  int getFaceColor(int cubit, int cubitface, int size)
374
    {
375
    int numO = getNumOctahedrons(size);
376

    
377
    if( cubit<numO )
378
      {
379
      int axis = 0;
380
      int layer= 1;
381

    
382
      switch(cubitface)
383
        {
384
        case 0: axis = 2; layer =             1; break;
385
        case 1: axis = 0; layer = (1<<(size-1)); break;
386
        case 2: axis = 3; layer =             1; break;
387
        case 3: axis = 1; layer = (1<<(size-1)); break;
388
        case 4: axis = 3; layer = (1<<(size-1)); break;
389
        case 5: axis = 1; layer =             1; break;
390
        case 6: axis = 2; layer = (1<<(size-1)); break;
391
        case 7: axis = 0; layer =             1; break;
392
        }
393

    
394
      return CUBITS[cubit].mRotationRow[axis] == layer ? cubitface : NUM_TEXTURES;
395
      }
396
    else
397
      {
398
      return cubitface>0 ? NUM_TEXTURES : retFaceTetraBelongsTo(cubit-numO, size);
399
      }
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
405
    {
406
    float E = 0.75f;
407
    float F = 0.50f;
408
    float R = 0.06f;
409
    float S = 0.07f;
410
    float[] vertices = { -F,-E/3, +F,-E/3, 0.0f,2*E/3};
411

    
412
    FactorySticker factory = FactorySticker.getInstance();
413
    factory.drawRoundedPolygon(canvas, paint, left, top, vertices, S, FACE_COLORS[face], R);
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  float returnMultiplier()
419
    {
420
    return 1.5f;
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  float[] getRowChances(int numLayers)
426
    {
427
    float[] chances = new float[numLayers];
428

    
429
    for(int i=0; i<numLayers; i++) chances[i] = ((float)(i+1))/numLayers;
430

    
431
    return chances;
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
// PUBLIC API
436

    
437
  public Static3D[] getRotationAxis()
438
    {
439
    return ROT_AXIS;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443

    
444
  public int getBasicAngle()
445
    {
446
    return 3;
447
    }
448

    
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450

    
451
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
452
    {
453
    if( num==0 )
454
      {
455
      scramble[num][0] = rnd.nextInt(ROTATION_AXIS.length);
456
      }
457
    else
458
      {
459
      int newVector = rnd.nextInt(ROTATION_AXIS.length-1);
460
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
461
      }
462

    
463
    float rowFloat = rnd.nextFloat();
464

    
465
    for(int row=0; row<mRowChances.length; row++)
466
      {
467
      if( rowFloat<=mRowChances[row] )
468
        {
469
        scramble[num][1] = row;
470
        break;
471
        }
472
      }
473

    
474
    switch( rnd.nextInt(2) )
475
      {
476
      case 0: scramble[num][2] = -1; break;
477
      case 1: scramble[num][2] =  1; break;
478
      }
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  int mulQuat(int q1, int q2)
484
    {
485
    Static4D result = RubikSurfaceView.quatMultiply(QUATS[q1],QUATS[q2]);
486

    
487
    float rX = result.get0();
488
    float rY = result.get1();
489
    float rZ = result.get2();
490
    float rW = result.get3();
491

    
492
    final float MAX_ERROR = 0.1f;
493
    float dX,dY,dZ,dW;
494

    
495
    for(int i=0; i<QUATS.length; i++)
496
      {
497
      dX = QUATS[i].get0() - rX;
498
      dY = QUATS[i].get1() - rY;
499
      dZ = QUATS[i].get2() - rZ;
500
      dW = QUATS[i].get3() - rW;
501

    
502
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
503
          dY<MAX_ERROR && dY>-MAX_ERROR &&
504
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
505
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
506

    
507
      dX = QUATS[i].get0() + rX;
508
      dY = QUATS[i].get1() + rY;
509
      dZ = QUATS[i].get2() + rZ;
510
      dW = QUATS[i].get3() + rW;
511

    
512
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
513
          dY<MAX_ERROR && dY>-MAX_ERROR &&
514
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
515
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
516
      }
517

    
518
    return -1;
519
    }
520

    
521
///////////////////////////////////////////////////////////////////////////////////////////////////
522
// The Diamond is solved if and only if:
523
//
524
// 1) all octahedrons are rotated with the same quat
525
// 2) all tetrahedrons might be also optionally rotated by a 'face neutral' pair of quats
526
//    (indexes of those are kept in the 'mFaceNeutralQuattIndex' table)
527
//
528
// Note: this works for any size, because even if layers>3 - and then there are 'face-internal'
529
// octahedrons which, it would seem, can be rotated by those 'face neutral' pairs of quats - but
530
// in reality no, because if they were, the octahedrons would then not fit in the lattice...
531

    
532
  public boolean isSolved()
533
    {
534
    int q = CUBITS[0].mQuatIndex;
535
    int layers = getNumLayers();
536
    int numO = getNumOctahedrons(layers);
537

    
538
    for(int i=1; i<numO; i++)
539
      {
540
      if( CUBITS[i].mQuatIndex != q ) return false;
541
      }
542

    
543
    int qI, q1Index, q2Index, face;
544

    
545
    for(int i=numO; i<NUM_CUBITS; i++)
546
      {
547
      face    = retFaceTetraBelongsTo(i-numO,layers);
548
      q1Index = mFaceNeutralQuatIndex[face][0];
549
      q2Index = mFaceNeutralQuatIndex[face][1];
550
      qI      = CUBITS[i].mQuatIndex;
551

    
552
      if(  qI != q && qI != mulQuat(q,q1Index) && qI != mulQuat(q,q2Index) ) return false;
553
      }
554

    
555
    return true;
556
    }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559
// only needed for solvers - there are no Diamond solvers ATM
560

    
561
  public String retObjectString()
562
    {
563
    return "";
564
    }
565

    
566
///////////////////////////////////////////////////////////////////////////////////////////////////
567

    
568
  public int getObjectName(int numLayers)
569
    {
570
    switch(numLayers)
571
      {
572
      case 2: return R.string.diam2;
573
      case 3: return R.string.diam3;
574
      }
575

    
576
    return 0;
577
    }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580

    
581
  public int getInventor(int numLayers)
582
    {
583
    switch(numLayers)
584
      {
585
      case 2: return R.string.diam2_inventor;
586
      case 3: return R.string.diam3_inventor;
587
      }
588

    
589
    return 0;
590
    }
591

    
592
///////////////////////////////////////////////////////////////////////////////////////////////////
593

    
594
  public int getComplexity(int numLayers)
595
    {
596
    switch(numLayers)
597
      {
598
      case 2: return 5;
599
      case 3: return 7;
600
      }
601

    
602
    return 0;
603
    }
604
}
(20-20/33)