Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyPyraminx.java @ 7d8cc029

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
  private void addTetrahedralLattice(int size, int index, float[][] pos)
129
    {
130
    final float DX = 1.0f;
131
    final float DY = SQ2/2;
132
    final float DZ = 1.0f;
133

    
134
    float startX = 0.0f;
135
    float startY =-DY*(size-1)/2;
136
    float startZ = DZ*(size-1)/2;
137

    
138
    for(int layer=0; layer<size; layer++)
139
      {
140
      float currX = startX;
141
      float currY = startY;
142

    
143
      for(int x=0; x<layer+1; x++)
144
        {
145
        float currZ = startZ;
146

    
147
        for(int z=0; z<size-layer; z++)
148
          {
149
          pos[index] = new float[] {currX,currY,currZ};
150
          index++;
151
          currZ -= DZ;
152
          }
153

    
154
        currX += DX;
155
        }
156

    
157
      startX-=DX/2;
158
      startY+=DY;
159
      startZ-=DZ/2;
160
      }
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
165

    
166
  float[][] getCubitPositions(int size)
167
    {
168
    int numOcta = (size-1)*size*(size+1)/6;
169
    int numTetra= size*(size+1)*(size+2)/6;
170
    float[][] ret = new float[numOcta+numTetra][];
171

    
172
    addTetrahedralLattice(size-1,      0,ret);
173
    addTetrahedralLattice(size  ,numOcta,ret);
174

    
175
    return ret;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  Static4D[] getQuats()
181
    {
182
    return QUATS;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  int getNumFaces()
188
    {
189
    return FACE_COLORS.length;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  int getNumStickerTypes(int numLayers)
195
    {
196
    return STICKERS.length;
197
    }
198

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

    
201
  float[] getCuts(int size)
202
    {
203
    float[] cuts = new float[size-1];
204

    
205
    for(int i=0; i<size-1; i++)
206
      {
207
      cuts[i] = (1.0f-0.25f*size+i)*(SQ6/3);
208
      }
209

    
210
    return cuts;
211
    }
212

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

    
215
  int getNumCubitFaces()
216
    {
217
    return 8;
218
    }
219

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

    
222
  float getScreenRatio()
223
    {
224
    return 0.82f;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  boolean shouldResetTextureMaps()
230
    {
231
    return false;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  private int getNumOctahedrons(int numLayers)
237
    {
238
    return (numLayers-1)*numLayers*(numLayers+1)/6;
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  private int faceColor(int cubit, int axis)
244
    {
245
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_FACES;
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  int getFaceColor(int cubit, int cubitface, int size)
251
    {
252
    if( cubit< (size-1)*size*(size+1)/6 )
253
      {
254
      switch( cubitface )
255
        {
256
        case 0: return faceColor(cubit,0);
257
        case 2: return faceColor(cubit,1);
258
        case 5: return faceColor(cubit,3);
259
        case 7: return faceColor(cubit,2);
260
        default:return NUM_FACES;
261
        }
262
      }
263
    else
264
      {
265
      return cubitface<NUM_FACES ? faceColor(cubit,cubitface) : NUM_FACES;
266
      }
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  MeshBase createCubitMesh(int cubit, int numLayers)
272
    {
273
    if( mMeshes==null )
274
      {
275
      FactoryCubit factory = FactoryCubit.getInstance();
276
      factory.clear();
277
      mMeshes = new MeshBase[2];
278
      }
279

    
280
    MeshBase mesh;
281
    int numO = getNumOctahedrons(numLayers);
282

    
283
    if( cubit<numO )
284
      {
285
      if( mMeshes[0]==null )
286
        {
287
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
288
        int[] bandIndexes   = new int[] { 0,0,0,0,0,0,0,0 };
289
        float[][] corners   = new float[][] { {0.04f,0.20f} };
290
        int[] cornerIndexes = new int[] { 0,0,0,0,0,0 };
291
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
292
        int[] centerIndexes = new int[] { 0,0,0,0,0,0 };
293

    
294
        FactoryCubit factory = FactoryCubit.getInstance();
295

    
296
        factory.createNewFaceTransform(VERTICES_OCTA,VERT_INDEXES_OCTA);
297
        mMeshes[0] = factory.createRoundedSolid(VERTICES_OCTA, VERT_INDEXES_OCTA,
298
                                                bands, bandIndexes,
299
                                                corners, cornerIndexes,
300
                                                centers, centerIndexes,
301
                                                getNumCubitFaces() );
302
        }
303
      mesh = mMeshes[0].copy(true);
304
      }
305
    else
306
      {
307
      if( mMeshes[1]==null )
308
        {
309
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
310
        int[] bandIndexes   = new int[] { 0,0,0,0 };
311
        float[][] corners   = new float[][] { {0.06f,0.15f} };
312
        int[] cornerIndexes = new int[] { 0,0,0,0 };
313
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
314
        int[] centerIndexes = new int[] { 0,0,0,0 };
315

    
316
        FactoryCubit factory = FactoryCubit.getInstance();
317

    
318
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
319
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
320
                                                bands, bandIndexes,
321
                                                corners, cornerIndexes,
322
                                                centers, centerIndexes,
323
                                                getNumCubitFaces() );
324

    
325
        factory.printStickerCoords();
326
        }
327
      mesh = mMeshes[1].copy(true);
328
      }
329

    
330
    return mesh;
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334

    
335
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
336
    {
337
    float R = 0.06f;
338
    float S = 0.08f;
339

    
340
    FactorySticker factory = FactorySticker.getInstance();
341
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345
// SQ6/3 = height of the tetrahedron
346

    
347
  float returnMultiplier()
348
    {
349
    return getNumLayers()/(SQ6/3);
350
    }
351

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

    
354
  float[] getRowChances(int numLayers)
355
    {
356
    int total = numLayers*(numLayers+1)/2;
357
    float running=0.0f;
358
    float[] chances = new float[numLayers];
359

    
360
    for(int i=0; i<numLayers; i++)
361
      {
362
      running += (numLayers-i);
363
      chances[i] = running / total;
364
      }
365

    
366
    return chances;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// PUBLIC API
371

    
372
  public Static3D[] getRotationAxis()
373
    {
374
    return ROT_AXIS;
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  public int getBasicAngle()
380
    {
381
    return 3;
382
    }
383

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

    
386
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
387
    {
388
    if( num==0 )
389
      {
390
      scramble[num][0] = rnd.nextInt(ROTATION_AXIS.length);
391
      }
392
    else
393
      {
394
      int newVector = rnd.nextInt(ROTATION_AXIS.length-1);
395
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
396
      }
397

    
398
    float rowFloat = rnd.nextFloat();
399

    
400
    for(int row=0; row<mRowChances.length; row++)
401
      {
402
      if( rowFloat<=mRowChances[row] )
403
        {
404
        scramble[num][1] = row;
405
        break;
406
        }
407
      }
408

    
409
    switch( rnd.nextInt(2) )
410
      {
411
      case 0: scramble[num][2] = -1; break;
412
      case 1: scramble[num][2] =  1; break;
413
      }
414
    }
415

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

    
418
  public boolean isSolved()
419
    {
420
    int index = CUBITS[0].mQuatIndex;
421

    
422
    for(int i=1; i<NUM_CUBITS; i++)
423
      {
424
      if( thereIsVisibleDifference(CUBITS[i], index) ) return false;
425
      }
426

    
427
    return true;
428
    }
429

    
430
////////////////////////////////////////////////////////////////////////
431
// only needed for solvers - there are no Pyraminx solvers ATM)
432

    
433
  public String retObjectString()
434
    {
435
    return "";
436
    }
437

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

    
440
  public int getObjectName(int numLayers)
441
    {
442
    switch(numLayers)
443
      {
444
      case 3: return R.string.pyra3;
445
      case 4: return R.string.pyra4;
446
      case 5: return R.string.pyra5;
447
      }
448
    return R.string.pyra3;
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

    
453
  public int getInventor(int numLayers)
454
    {
455
    switch(numLayers)
456
      {
457
      case 3: return R.string.pyra3_inventor;
458
      case 4: return R.string.pyra4_inventor;
459
      case 5: return R.string.pyra5_inventor;
460
      }
461
    return R.string.pyra3_inventor;
462
    }
463

    
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465

    
466
  public int getComplexity(int numLayers)
467
    {
468
    switch(numLayers)
469
      {
470
      case 3: return 4;
471
      case 4: return 6;
472
      case 5: return 8;
473
      }
474
    return 4;
475
    }
476
}
(30-30/33)