Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyPyraminx.java @ 582617c1

1 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 e844c116 Leszek Koltunski
22 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
23 e844c116 Leszek Koltunski
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
26 749ef882 Leszek Koltunski
import org.distorted.helpers.FactoryCubit;
27
import org.distorted.helpers.FactorySticker;
28 e844c116 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedTexture;
30
import org.distorted.library.mesh.MeshBase;
31 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
32 e844c116 Leszek Koltunski
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34 6fd4a72c Leszek Koltunski
import org.distorted.main.R;
35 e844c116 Leszek Koltunski
36 7c969a6d Leszek Koltunski
import java.util.Random;
37
38 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 9c2f0c91 Leszek Koltunski
public class TwistyPyraminx extends TwistyObject
41 e844c116 Leszek Koltunski
{
42 ad38d800 Leszek Koltunski
  static final Static3D[] ROT_AXIS = new Static3D[]
43 e844c116 Leszek Koltunski
         {
44 ac940e24 Leszek Koltunski
           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 ad38d800 Leszek Koltunski
         };
49
50 e844c116 Leszek Koltunski
  private static final int[] FACE_COLORS = new int[]
51
         {
52 ece1b58d Leszek Koltunski
           COLOR_GREEN , COLOR_YELLOW,
53
           COLOR_BLUE  , COLOR_RED
54 e844c116 Leszek Koltunski
         };
55
56 9f4c44fe Leszek Koltunski
  // computed with res/raw/compute_quats.c
57 10585385 Leszek Koltunski
  private static final Static4D[] QUATS = new Static4D[]
58 e844c116 Leszek Koltunski
         {
59 10585385 Leszek Koltunski
           new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
60 ac940e24 Leszek Koltunski
           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 e844c116 Leszek Koltunski
         };
72
73 31cd7256 Leszek Koltunski
  private static final double[][] VERTICES_TETRA = new double[][]
74 b1f2ccf5 Leszek Koltunski
          {
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 31cd7256 Leszek Koltunski
  private static final int[][] VERT_INDEXES_TETRA = new int[][]
82 b1f2ccf5 Leszek Koltunski
          {
83
             {2,1,0},   // counterclockwise!
84 31cd7256 Leszek Koltunski
             {3,0,1},
85 b1f2ccf5 Leszek Koltunski
             {3,2,0},
86 31cd7256 Leszek Koltunski
             {2,3,1}
87 b1f2ccf5 Leszek Koltunski
          };
88
89 31cd7256 Leszek Koltunski
  private static final double[][] VERTICES_OCTA = new double[][]
90 b1f2ccf5 Leszek Koltunski
          {
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 31cd7256 Leszek Koltunski
  private static final int[][] VERT_INDEXES_OCTA = new int[][]
100 b1f2ccf5 Leszek Koltunski
          {
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 31cd7256 Leszek Koltunski
  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 0203be88 Leszek Koltunski
  private static float[] mRowChances;
118 49f67f9b Leszek Koltunski
119 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 ac940e24 Leszek Koltunski
  TwistyPyraminx(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
122
                 DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
123 e844c116 Leszek Koltunski
    {
124 db875721 Leszek Koltunski
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.PYRA, res, scrWidth);
125 e844c116 Leszek Koltunski
    }
126
127 0203be88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129
  private float[] getRowChances(int numLayers)
130
    {
131
    int total = numLayers*(numLayers+1)/2;
132
    float running=0.0f;
133
    float[] chances = new float[numLayers];
134
135
    for(int i=0; i<numLayers; i++)
136
      {
137
      running += (numLayers-i);
138
      chances[i] = running / total;
139
      }
140
141
    return chances;
142
    }
143
144 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146 e6cf7283 Leszek Koltunski
  private void addTetrahedralLattice(int size, int index, float[][] pos)
147 49f67f9b Leszek Koltunski
    {
148 ac940e24 Leszek Koltunski
    final float DX = 1.0f;
149
    final float DY = SQ2/2;
150
    final float DZ = 1.0f;
151 49f67f9b Leszek Koltunski
152 ac940e24 Leszek Koltunski
    float startX = 0.0f;
153
    float startY =-DY*(size-1)/2;
154
    float startZ = DZ*(size-1)/2;
155 769409d2 Leszek Koltunski
156 ac940e24 Leszek Koltunski
    for(int layer=0; layer<size; layer++)
157 49f67f9b Leszek Koltunski
      {
158 ac940e24 Leszek Koltunski
      float currX = startX;
159
      float currY = startY;
160
161
      for(int x=0; x<layer+1; x++)
162
        {
163
        float currZ = startZ;
164
165
        for(int z=0; z<size-layer; z++)
166
          {
167 e6cf7283 Leszek Koltunski
          pos[index] = new float[] {currX,currY,currZ};
168 ac940e24 Leszek Koltunski
          index++;
169
          currZ -= DZ;
170
          }
171
172
        currX += DX;
173
        }
174
175
      startX-=DX/2;
176
      startY+=DY;
177
      startZ-=DZ/2;
178 49f67f9b Leszek Koltunski
      }
179
    }
180
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182 ac940e24 Leszek Koltunski
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
183 49f67f9b Leszek Koltunski
184 e6cf7283 Leszek Koltunski
  float[][] getCubitPositions(int size)
185 e844c116 Leszek Koltunski
    {
186 ac940e24 Leszek Koltunski
    int numOcta = (size-1)*size*(size+1)/6;
187
    int numTetra= size*(size+1)*(size+2)/6;
188 e6cf7283 Leszek Koltunski
    float[][] ret = new float[numOcta+numTetra][];
189 49f67f9b Leszek Koltunski
190 ac940e24 Leszek Koltunski
    addTetrahedralLattice(size-1,      0,ret);
191
    addTetrahedralLattice(size  ,numOcta,ret);
192 49f67f9b Leszek Koltunski
193 ac940e24 Leszek Koltunski
    return ret;
194 e844c116 Leszek Koltunski
    }
195
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198 10585385 Leszek Koltunski
  Static4D[] getQuats()
199 e844c116 Leszek Koltunski
    {
200 10585385 Leszek Koltunski
    return QUATS;
201 e844c116 Leszek Koltunski
    }
202
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
  int getNumFaces()
206
    {
207
    return FACE_COLORS.length;
208
    }
209
210 eab9d8f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 a64e07d0 Leszek Koltunski
  int getNumStickerTypes(int numLayers)
213 eab9d8f8 Leszek Koltunski
    {
214 31cd7256 Leszek Koltunski
    return STICKERS.length;
215 eab9d8f8 Leszek Koltunski
    }
216
217 7403cdfa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219 a97e02b7 Leszek Koltunski
  float[] getCuts(int size)
220 7403cdfa Leszek Koltunski
    {
221 a97e02b7 Leszek Koltunski
    float[] cuts = new float[size-1];
222
223
    for(int i=0; i<size-1; i++)
224
      {
225
      cuts[i] = (1.0f-0.25f*size+i)*(SQ6/3);
226
      }
227
228
    return cuts;
229 7403cdfa Leszek Koltunski
    }
230
231 8f53e513 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233
  int getNumCubitFaces()
234
    {
235 ac940e24 Leszek Koltunski
    return 8;
236 8f53e513 Leszek Koltunski
    }
237
238 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239
240
  float getScreenRatio()
241
    {
242 7381193e Leszek Koltunski
    return 0.82f;
243 f0fa83ae Leszek Koltunski
    }
244
245 eaee1ddc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
  boolean shouldResetTextureMaps()
248
    {
249
    return false;
250
    }
251
252 31cd7256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253
254
  private int getNumOctahedrons(int numLayers)
255
    {
256
    return (numLayers-1)*numLayers*(numLayers+1)/6;
257
    }
258
259 f6d06256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261 ac940e24 Leszek Koltunski
  private int faceColor(int cubit, int axis)
262 f6d06256 Leszek Koltunski
    {
263 f0450fcc Leszek Koltunski
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_FACES;
264 f6d06256 Leszek Koltunski
    }
265
266 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 ac940e24 Leszek Koltunski
  int getFaceColor(int cubit, int cubitface, int size)
269 e844c116 Leszek Koltunski
    {
270 ac940e24 Leszek Koltunski
    if( cubit< (size-1)*size*(size+1)/6 )
271 40ab026e Leszek Koltunski
      {
272 ac940e24 Leszek Koltunski
      switch( cubitface )
273
        {
274
        case 0: return faceColor(cubit,0);
275
        case 2: return faceColor(cubit,1);
276
        case 5: return faceColor(cubit,3);
277
        case 7: return faceColor(cubit,2);
278
        default:return NUM_FACES;
279
        }
280 40ab026e Leszek Koltunski
      }
281 ac940e24 Leszek Koltunski
    else
282 89a11f7b Leszek Koltunski
      {
283 ac940e24 Leszek Koltunski
      return cubitface<NUM_FACES ? faceColor(cubit,cubitface) : NUM_FACES;
284 89a11f7b Leszek Koltunski
      }
285 e844c116 Leszek Koltunski
    }
286
287 40ab026e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288
289 a64e07d0 Leszek Koltunski
  MeshBase createCubitMesh(int cubit, int numLayers)
290 40ab026e Leszek Koltunski
    {
291 31cd7256 Leszek Koltunski
    if( mMeshes==null )
292 40ab026e Leszek Koltunski
      {
293 31cd7256 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
294
      factory.clear();
295
      mMeshes = new MeshBase[2];
296
      }
297
298
    MeshBase mesh;
299
    int numO = getNumOctahedrons(numLayers);
300
301
    if( cubit<numO )
302
      {
303
      if( mMeshes[0]==null )
304
        {
305
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
306
        int[] bandIndexes   = new int[] { 0,0,0,0,0,0,0,0 };
307
        float[][] corners   = new float[][] { {0.04f,0.20f} };
308
        int[] cornerIndexes = new int[] { 0,0,0,0,0,0 };
309 b3c9061a Leszek Koltunski
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
310
        int[] centerIndexes = new int[] { 0,0,0,0,0,0 };
311 31cd7256 Leszek Koltunski
312
        FactoryCubit factory = FactoryCubit.getInstance();
313
314
        factory.createNewFaceTransform(VERTICES_OCTA,VERT_INDEXES_OCTA);
315
        mMeshes[0] = factory.createRoundedSolid(VERTICES_OCTA, VERT_INDEXES_OCTA,
316
                                                bands, bandIndexes,
317
                                                corners, cornerIndexes,
318 b3c9061a Leszek Koltunski
                                                centers, centerIndexes,
319 31cd7256 Leszek Koltunski
                                                getNumCubitFaces() );
320
        }
321
      mesh = mMeshes[0].copy(true);
322 40ab026e Leszek Koltunski
      }
323
    else
324
      {
325 31cd7256 Leszek Koltunski
      if( mMeshes[1]==null )
326
        {
327
        float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,6,2,2} };
328
        int[] bandIndexes   = new int[] { 0,0,0,0 };
329
        float[][] corners   = new float[][] { {0.06f,0.15f} };
330
        int[] cornerIndexes = new int[] { 0,0,0,0 };
331 b3c9061a Leszek Koltunski
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
332
        int[] centerIndexes = new int[] { 0,0,0,0 };
333 31cd7256 Leszek Koltunski
334
        FactoryCubit factory = FactoryCubit.getInstance();
335
336
        factory.createNewFaceTransform(VERTICES_TETRA,VERT_INDEXES_TETRA);
337
        mMeshes[1] = factory.createRoundedSolid(VERTICES_TETRA, VERT_INDEXES_TETRA,
338
                                                bands, bandIndexes,
339
                                                corners, cornerIndexes,
340 b3c9061a Leszek Koltunski
                                                centers, centerIndexes,
341 31cd7256 Leszek Koltunski
                                                getNumCubitFaces() );
342
343
        factory.printStickerCoords();
344
        }
345
      mesh = mMeshes[1].copy(true);
346 40ab026e Leszek Koltunski
      }
347 31cd7256 Leszek Koltunski
348
    return mesh;
349 40ab026e Leszek Koltunski
    }
350
351 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
352
353 ae755eda Leszek Koltunski
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
354 7289fd6c Leszek Koltunski
    {
355 76c2bd07 Leszek Koltunski
    float R = 0.06f;
356
    float S = 0.08f;
357
358 b89898c5 Leszek Koltunski
    FactorySticker factory = FactorySticker.getInstance();
359 31cd7256 Leszek Koltunski
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[0], S, FACE_COLORS[face], R);
360 7289fd6c Leszek Koltunski
    }
361
362 fb377dae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
363 7403cdfa Leszek Koltunski
// SQ6/3 = height of the tetrahedron
364 fb377dae Leszek Koltunski
365
  float returnMultiplier()
366
    {
367 d99f3a48 Leszek Koltunski
    return getNumLayers()/(SQ6/3);
368 fb377dae Leszek Koltunski
    }
369
370 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
371
// PUBLIC API
372
373 12ad3fca Leszek Koltunski
  public Static3D[] getRotationAxis()
374
    {
375 ad38d800 Leszek Koltunski
    return ROT_AXIS;
376 12ad3fca Leszek Koltunski
    }
377
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380 e844c116 Leszek Koltunski
  public int getBasicAngle()
381
    {
382
    return 3;
383
    }
384 39e74052 Leszek Koltunski
385 7c969a6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
386
387 5043d5d0 Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
388 7c969a6d Leszek Koltunski
    {
389 0203be88 Leszek Koltunski
    int numLayers = getNumLayers();
390
391
    if( mRowChances==null ) mRowChances = getRowChances(numLayers);
392
393 5043d5d0 Leszek Koltunski
    if( num==0 )
394 5cf34c5f Leszek Koltunski
      {
395 582617c1 Leszek Koltunski
      scramble[num][0] = rnd.nextInt(NUM_AXIS);
396 7c969a6d Leszek Koltunski
      }
397
    else
398
      {
399 582617c1 Leszek Koltunski
      int newVector = rnd.nextInt(NUM_AXIS -1);
400 5043d5d0 Leszek Koltunski
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
401 5cf34c5f Leszek Koltunski
      }
402 e46e17fb Leszek Koltunski
403 7c969a6d Leszek Koltunski
    float rowFloat = rnd.nextFloat();
404 e46e17fb Leszek Koltunski
405 0203be88 Leszek Koltunski
    for(int row=0; row<numLayers; row++)
406 7c969a6d Leszek Koltunski
      {
407 bbc6471c Leszek Koltunski
      if( rowFloat<=mRowChances[row] )
408
        {
409 5043d5d0 Leszek Koltunski
        scramble[num][1] = row;
410 bbc6471c Leszek Koltunski
        break;
411
        }
412 7c969a6d Leszek Koltunski
      }
413
414 5043d5d0 Leszek Koltunski
    switch( rnd.nextInt(2) )
415
      {
416
      case 0: scramble[num][2] = -1; break;
417
      case 1: scramble[num][2] =  1; break;
418
      }
419 e46e17fb Leszek Koltunski
    }
420 f0336037 Leszek Koltunski
421 6b6504fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
422
423
  public boolean isSolved()
424
    {
425
    int index = CUBITS[0].mQuatIndex;
426
427
    for(int i=1; i<NUM_CUBITS; i++)
428
      {
429 722b2512 Leszek Koltunski
      if( thereIsVisibleDifference(CUBITS[i], index) ) return false;
430 6b6504fe Leszek Koltunski
      }
431
432
    return true;
433
    }
434
435 221a4090 Leszek Koltunski
////////////////////////////////////////////////////////////////////////
436 ee35e63c Leszek Koltunski
// only needed for solvers - there are no Pyraminx solvers ATM)
437 f0336037 Leszek Koltunski
438 20931cf6 Leszek Koltunski
  public String retObjectString()
439 f0336037 Leszek Koltunski
    {
440
    return "";
441
    }
442 6fd4a72c Leszek Koltunski
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444
445
  public int getObjectName(int numLayers)
446
    {
447
    switch(numLayers)
448
      {
449
      case 3: return R.string.pyra3;
450
      case 4: return R.string.pyra4;
451
      case 5: return R.string.pyra5;
452
      }
453
    return R.string.pyra3;
454
    }
455
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457
458
  public int getInventor(int numLayers)
459
    {
460
    switch(numLayers)
461
      {
462
      case 3: return R.string.pyra3_inventor;
463
      case 4: return R.string.pyra4_inventor;
464
      case 5: return R.string.pyra5_inventor;
465
      }
466
    return R.string.pyra3_inventor;
467
    }
468
469
///////////////////////////////////////////////////////////////////////////////////////////////////
470
471
  public int getComplexity(int numLayers)
472
    {
473
    switch(numLayers)
474
      {
475
      case 3: return 4;
476
      case 4: return 6;
477
      case 5: return 8;
478
      }
479
    return 4;
480
    }
481 e844c116 Leszek Koltunski
}