Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyPyraminx.java @ 31cd7256

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.main.DistortedEffects;
29
import org.distorted.library.main.DistortedTexture;
30
import org.distorted.library.mesh.MeshBase;
31
import org.distorted.library.mesh.MeshSquare;
32
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34
import org.distorted.main.R;
35

    
36
import java.util.Random;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class TwistyPyraminx extends TwistyObject
41
{
42
  static final Static3D[] ROT_AXIS = new Static3D[]
43
         {
44
           new Static3D(     0,-SQ3/3,-SQ6/3),
45
           new Static3D(     0,-SQ3/3,+SQ6/3),
46
           new Static3D(+SQ6/3,+SQ3/3,     0),
47
           new Static3D(-SQ6/3,+SQ3/3,     0),
48
         };
49

    
50
  private static final int[] FACE_COLORS = new int[]
51
         {
52
           COLOR_GREEN , COLOR_YELLOW,
53
           COLOR_BLUE  , COLOR_RED
54
         };
55

    
56
  // computed with res/raw/compute_quats.c
57
  private static final Static4D[] QUATS = new Static4D[]
58
         {
59
           new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
60
           new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
61
           new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
62
           new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
63
           new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
64
           new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
65
           new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
66
           new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
67
           new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
68
           new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
69
           new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
70
           new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
71
         };
72

    
73
  private static final double[][] VERTICES_TETRA = new double[][]
74
          {
75
             {-0.5, SQ2/4, 0.0},
76
             { 0.5, SQ2/4, 0.0},
77
             { 0.0,-SQ2/4, 0.5},
78
             { 0.0,-SQ2/4,-0.5}
79
          };
80

    
81
  private static final int[][] VERT_INDEXES_TETRA = new int[][]
82
          {
83
             {2,1,0},   // counterclockwise!
84
             {3,0,1},
85
             {3,2,0},
86
             {2,3,1}
87
          };
88

    
89
  private static final double[][] VERTICES_OCTA = new double[][]
90
          {
91
             { 0.5,   0.0, 0.5},
92
             { 0.5,   0.0,-0.5},
93
             {-0.5,   0.0,-0.5},
94
             {-0.5,   0.0, 0.5},
95
             { 0.0, SQ2/2, 0.0},
96
             { 0.0,-SQ2/2, 0.0}
97
          };
98

    
99
  private static final int[][] VERT_INDEXES_OCTA = new int[][]
100
          {
101
             {3,0,4},   // counterclockwise!
102
             {0,1,4},
103
             {1,2,4},
104
             {2,3,4},
105
             {5,0,3},
106
             {5,1,0},
107
             {5,2,1},
108
             {5,3,2}
109
          };
110

    
111
  private static final float[][] STICKERS = new float[][]
112
          {
113
             { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f }
114
          };
115

    
116
  private static MeshBase[] mMeshes;
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  TwistyPyraminx(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
121
                 DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
122
    {
123
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.PYRA, res, scrWidth);
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  double[][] getVertices(int cubitType)
129
    {
130
    if( cubitType==0 ) return VERTICES_OCTA;
131
    if( cubitType==1 ) return VERTICES_TETRA;
132
    return null;
133
    }
134

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

    
137
  int[][] getVertIndexes(int cubitType)
138
    {
139
    if( cubitType==0 ) return VERT_INDEXES_OCTA;
140
    if( cubitType==1 ) return VERT_INDEXES_TETRA;
141
    return null;
142
    }
143

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

    
146
  int getNumCubitTypes(int numLayers)
147
    {
148
    return 2;
149
    }
150

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

    
153
  private void addTetrahedralLattice(int size, int index, float[][] pos)
154
    {
155
    final float DX = 1.0f;
156
    final float DY = SQ2/2;
157
    final float DZ = 1.0f;
158

    
159
    float startX = 0.0f;
160
    float startY =-DY*(size-1)/2;
161
    float startZ = DZ*(size-1)/2;
162

    
163
    for(int layer=0; layer<size; layer++)
164
      {
165
      float currX = startX;
166
      float currY = startY;
167

    
168
      for(int x=0; x<layer+1; x++)
169
        {
170
        float currZ = startZ;
171

    
172
        for(int z=0; z<size-layer; z++)
173
          {
174
          pos[index] = new float[] {currX,currY,currZ};
175
          index++;
176
          currZ -= DZ;
177
          }
178

    
179
        currX += DX;
180
        }
181

    
182
      startX-=DX/2;
183
      startY+=DY;
184
      startZ-=DZ/2;
185
      }
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
190

    
191
  float[][] getCubitPositions(int size)
192
    {
193
    int numOcta = (size-1)*size*(size+1)/6;
194
    int numTetra= size*(size+1)*(size+2)/6;
195
    float[][] ret = new float[numOcta+numTetra][];
196

    
197
    addTetrahedralLattice(size-1,      0,ret);
198
    addTetrahedralLattice(size  ,numOcta,ret);
199

    
200
    return ret;
201
    }
202

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

    
205
  Static4D[] getQuats()
206
    {
207
    return QUATS;
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  int getNumFaces()
213
    {
214
    return FACE_COLORS.length;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  int getNumStickerTypes(int numLayers)
220
    {
221
    return STICKERS.length;
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  float[] getCuts(int size)
227
    {
228
    float[] cuts = new float[size-1];
229

    
230
    for(int i=0; i<size-1; i++)
231
      {
232
      cuts[i] = (1.0f-0.25f*size+i)*(SQ6/3);
233
      }
234

    
235
    return cuts;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  int getNumCubitFaces()
241
    {
242
    return 8;
243
    }
244

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

    
247
  float getScreenRatio()
248
    {
249
    return 0.82f;
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  boolean shouldResetTextureMaps()
255
    {
256
    return false;
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  private int getNumOctahedrons(int numLayers)
262
    {
263
    return (numLayers-1)*numLayers*(numLayers+1)/6;
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  private int faceColor(int cubit, int axis)
269
    {
270
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_FACES;
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  int getFaceColor(int cubit, int cubitface, int size)
276
    {
277
    if( cubit< (size-1)*size*(size+1)/6 )
278
      {
279
      switch( cubitface )
280
        {
281
        case 0: return faceColor(cubit,0);
282
        case 2: return faceColor(cubit,1);
283
        case 5: return faceColor(cubit,3);
284
        case 7: return faceColor(cubit,2);
285
        default:return NUM_FACES;
286
        }
287
      }
288
    else
289
      {
290
      return cubitface<NUM_FACES ? faceColor(cubit,cubitface) : NUM_FACES;
291
      }
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  MeshBase createCubitMesh(int cubit, int numLayers)
297
    {
298
    if( mMeshes==null )
299
      {
300
      FactoryCubit factory = FactoryCubit.getInstance();
301
      factory.clear();
302
      mMeshes = new MeshBase[2];
303
      }
304

    
305
    MeshBase mesh;
306
    int numO = getNumOctahedrons(numLayers);
307

    
308
    if( cubit<numO )
309
      {
310
      if( mMeshes[0]==null )
311
        {
312
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
313
        int[] bandIndexes   = new int[] { 0,0,0,0,0,0,0,0 };
314
        float[][] corners   = new float[][] { {0.04f,0.20f} };
315
        int[] cornerIndexes = new int[] { 0,0,0,0,0,0 };
316

    
317
        FactoryCubit factory = FactoryCubit.getInstance();
318

    
319
        factory.createNewFaceTransform(VERTICES_OCTA,VERT_INDEXES_OCTA);
320
        mMeshes[0] = factory.createRoundedSolid(VERTICES_OCTA, VERT_INDEXES_OCTA,
321
                                                bands, bandIndexes,
322
                                                corners, cornerIndexes,
323
                                                getNumCubitFaces() );
324
        }
325
      mesh = mMeshes[0].copy(true);
326
      }
327
    else
328
      {
329
      if( mMeshes[1]==null )
330
        {
331
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
332
        int[] bandIndexes   = new int[] { 0,0,0,0 };
333
        float[][] corners   = new float[][] { {0.06f,0.15f} };
334
        int[] cornerIndexes = new int[] { 0,0,0,0 };
335

    
336
        FactoryCubit factory = FactoryCubit.getInstance();
337

    
338
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
339
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
340
                                                bands, bandIndexes,
341
                                                corners, cornerIndexes,
342
                                                getNumCubitFaces() );
343

    
344
        factory.printStickerCoords();
345
        }
346
      mesh = mMeshes[1].copy(true);
347
      }
348

    
349
    return mesh;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
355
    {
356
    float R = 0.06f;
357
    float S = 0.08f;
358

    
359
    FactorySticker factory = FactorySticker.getInstance();
360
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364
// SQ6/3 = height of the tetrahedron
365

    
366
  float returnMultiplier()
367
    {
368
    return getNumLayers()/(SQ6/3);
369
    }
370

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

    
373
  float[] getRowChances(int numLayers)
374
    {
375
    int total = numLayers*(numLayers+1)/2;
376
    float running=0.0f;
377
    float[] chances = new float[numLayers];
378

    
379
    for(int i=0; i<numLayers; i++)
380
      {
381
      running += (numLayers-i);
382
      chances[i] = running / total;
383
      }
384

    
385
    return chances;
386
    }
387

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389
// PUBLIC API
390

    
391
  public Static3D[] getRotationAxis()
392
    {
393
    return ROT_AXIS;
394
    }
395

    
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

    
398
  public int getBasicAngle()
399
    {
400
    return 3;
401
    }
402

    
403
///////////////////////////////////////////////////////////////////////////////////////////////////
404

    
405
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
406
    {
407
    if( num==0 )
408
      {
409
      scramble[num][0] = rnd.nextInt(ROTATION_AXIS.length);
410
      }
411
    else
412
      {
413
      int newVector = rnd.nextInt(ROTATION_AXIS.length-1);
414
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
415
      }
416

    
417
    float rowFloat = rnd.nextFloat();
418

    
419
    for(int row=0; row<mRowChances.length; row++)
420
      {
421
      if( rowFloat<=mRowChances[row] )
422
        {
423
        scramble[num][1] = row;
424
        break;
425
        }
426
      }
427

    
428
    switch( rnd.nextInt(2) )
429
      {
430
      case 0: scramble[num][2] = -1; break;
431
      case 1: scramble[num][2] =  1; break;
432
      }
433
    }
434

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

    
437
  public boolean isSolved()
438
    {
439
    int index = CUBITS[0].mQuatIndex;
440

    
441
    for(int i=1; i<NUM_CUBITS; i++)
442
      {
443
      if( thereIsVisibleDifference(CUBITS[i], index) ) return false;
444
      }
445

    
446
    return true;
447
    }
448

    
449
////////////////////////////////////////////////////////////////////////
450
// only needed for solvers - there are no Pyraminx solvers ATM)
451

    
452
  public String retObjectString()
453
    {
454
    return "";
455
    }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
  public int getObjectName(int numLayers)
460
    {
461
    switch(numLayers)
462
      {
463
      case 3: return R.string.pyra3;
464
      case 4: return R.string.pyra4;
465
      case 5: return R.string.pyra5;
466
      }
467
    return R.string.pyra3;
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  public int getInventor(int numLayers)
473
    {
474
    switch(numLayers)
475
      {
476
      case 3: return R.string.pyra3_inventor;
477
      case 4: return R.string.pyra4_inventor;
478
      case 5: return R.string.pyra5_inventor;
479
      }
480
    return R.string.pyra3_inventor;
481
    }
482

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

    
485
  public int getComplexity(int numLayers)
486
    {
487
    switch(numLayers)
488
      {
489
      case 3: return 4;
490
      case 4: return 6;
491
      case 5: return 8;
492
      }
493
    return 4;
494
    }
495
}
(30-30/33)