Project

General

Profile

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

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

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
  private static float[] mRowChances;
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  private void addTetrahedralLattice(int size, int index, float[][] pos)
147
    {
148
    final float DX = 1.0f;
149
    final float DY = SQ2/2;
150
    final float DZ = 1.0f;
151

    
152
    float startX = 0.0f;
153
    float startY =-DY*(size-1)/2;
154
    float startZ = DZ*(size-1)/2;
155

    
156
    for(int layer=0; layer<size; layer++)
157
      {
158
      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
          pos[index] = new float[] {currX,currY,currZ};
168
          index++;
169
          currZ -= DZ;
170
          }
171

    
172
        currX += DX;
173
        }
174

    
175
      startX-=DX/2;
176
      startY+=DY;
177
      startZ-=DZ/2;
178
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
183

    
184
  float[][] getCubitPositions(int size)
185
    {
186
    int numOcta = (size-1)*size*(size+1)/6;
187
    int numTetra= size*(size+1)*(size+2)/6;
188
    float[][] ret = new float[numOcta+numTetra][];
189

    
190
    addTetrahedralLattice(size-1,      0,ret);
191
    addTetrahedralLattice(size  ,numOcta,ret);
192

    
193
    return ret;
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  Static4D[] getQuats()
199
    {
200
    return QUATS;
201
    }
202

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

    
205
  int getNumFaces()
206
    {
207
    return FACE_COLORS.length;
208
    }
209

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

    
212
  int getNumStickerTypes(int numLayers)
213
    {
214
    return STICKERS.length;
215
    }
216

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

    
219
  float[] getCuts(int size)
220
    {
221
    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
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  int getNumCubitFaces()
234
    {
235
    return 8;
236
    }
237

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

    
240
  float getScreenRatio()
241
    {
242
    return 0.82f;
243
    }
244

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

    
247
  boolean shouldResetTextureMaps()
248
    {
249
    return false;
250
    }
251

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

    
254
  private int getNumOctahedrons(int numLayers)
255
    {
256
    return (numLayers-1)*numLayers*(numLayers+1)/6;
257
    }
258

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

    
261
  private int faceColor(int cubit, int axis)
262
    {
263
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_FACES;
264
    }
265

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

    
268
  int getFaceColor(int cubit, int cubitface, int size)
269
    {
270
    if( cubit< (size-1)*size*(size+1)/6 )
271
      {
272
      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
      }
281
    else
282
      {
283
      return cubitface<NUM_FACES ? faceColor(cubit,cubitface) : NUM_FACES;
284
      }
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  MeshBase createCubitMesh(int cubit, int numLayers)
290
    {
291
    if( mMeshes==null )
292
      {
293
      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
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
310
        int[] centerIndexes = new int[] { 0,0,0,0,0,0 };
311

    
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
                                                centers, centerIndexes,
319
                                                getNumCubitFaces() );
320
        }
321
      mesh = mMeshes[0].copy(true);
322
      }
323
    else
324
      {
325
      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
        float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
332
        int[] centerIndexes = new int[] { 0,0,0,0 };
333

    
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
                                                centers, centerIndexes,
341
                                                getNumCubitFaces() );
342

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

    
348
    return mesh;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352

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

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

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

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

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371
// PUBLIC API
372

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

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

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

    
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386

    
387
  public void randomizeNewScramble(int[][] scramble, Random rnd, int num)
388
    {
389
    int numLayers = getNumLayers();
390

    
391
    if( mRowChances==null ) mRowChances = getRowChances(numLayers);
392

    
393
    if( num==0 )
394
      {
395
      scramble[num][0] = rnd.nextInt(NUM_AXIS);
396
      }
397
    else
398
      {
399
      int newVector = rnd.nextInt(NUM_AXIS -1);
400
      scramble[num][0] = (newVector>=scramble[num-1][0] ? newVector+1 : newVector);
401
      }
402

    
403
    float rowFloat = rnd.nextFloat();
404

    
405
    for(int row=0; row<numLayers; row++)
406
      {
407
      if( rowFloat<=mRowChances[row] )
408
        {
409
        scramble[num][1] = row;
410
        break;
411
        }
412
      }
413

    
414
    switch( rnd.nextInt(2) )
415
      {
416
      case 0: scramble[num][2] = -1; break;
417
      case 1: scramble[num][2] =  1; break;
418
      }
419
    }
420

    
421
///////////////////////////////////////////////////////////////////////////////////////////////////
422

    
423
  public boolean isSolved()
424
    {
425
    int index = CUBITS[0].mQuatIndex;
426

    
427
    for(int i=1; i<NUM_CUBITS; i++)
428
      {
429
      if( thereIsVisibleDifference(CUBITS[i], index) ) return false;
430
      }
431

    
432
    return true;
433
    }
434

    
435
////////////////////////////////////////////////////////////////////////
436
// only needed for solvers - there are no Pyraminx solvers ATM)
437

    
438
  public String retObjectString()
439
    {
440
    return "";
441
    }
442

    
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
}
(32-32/37)