Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyPyraminx.java @ 4946b635

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
24 045d8cbd Leszek Koltunski
import org.distorted.helpers.ObjectShape;
25 9c06394a Leszek Koltunski
import org.distorted.helpers.ObjectSticker;
26 bbbfb6af Leszek Koltunski
import org.distorted.helpers.ScrambleState;
27 e844c116 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29 efa8aa48 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
30 e844c116 Leszek Koltunski
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32 6fd4a72c Leszek Koltunski
import org.distorted.main.R;
33 e844c116 Leszek Koltunski
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36 efa81f0c Leszek Koltunski
public class TwistyPyraminx extends Twisty4
37 e844c116 Leszek Koltunski
{
38 ad38d800 Leszek Koltunski
  static final Static3D[] ROT_AXIS = new Static3D[]
39 e844c116 Leszek Koltunski
         {
40 ac940e24 Leszek Koltunski
           new Static3D(     0,-SQ3/3,-SQ6/3),
41
           new Static3D(     0,-SQ3/3,+SQ6/3),
42
           new Static3D(+SQ6/3,+SQ3/3,     0),
43
           new Static3D(-SQ6/3,+SQ3/3,     0),
44 ad38d800 Leszek Koltunski
         };
45
46 bbbfb6af Leszek Koltunski
  private ScrambleState[] mStates;
47 48fec01e Leszek Koltunski
  private int[] mBasicAngle;
48
  private Static4D[] mQuats;
49 ef018c1b Leszek Koltunski
  private float[][] mCuts;
50
  private boolean[][] mLayerRotatable;
51 48fec01e Leszek Koltunski
  private ObjectSticker[] mStickers;
52 e9a87113 Leszek Koltunski
  private Movement mMovement;
53 bbbfb6af Leszek Koltunski
54 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 ac940e24 Leszek Koltunski
  TwistyPyraminx(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
57
                 DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
58 e844c116 Leszek Koltunski
    {
59 db875721 Leszek Koltunski
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.PYRA, res, scrWidth);
60 91792184 Leszek Koltunski
    }
61 bbbfb6af Leszek Koltunski
62 91792184 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
  ScrambleState[] getScrambleStates()
65
    {
66
    if( mStates==null )
67
      {
68
      int numLayers = getNumLayers();
69
      initializeScrambleStates(numLayers);
70
      }
71
72
    return mStates;
73 e844c116 Leszek Koltunski
    }
74
75 48fec01e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
  private void initializeQuats()
78
    {
79
    mQuats = new Static4D[]
80
         {
81
         new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
82
         new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
83
         new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
84
         new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
85
         new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
86
         new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
87
         new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
88
         new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
89
         new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
90
         new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
91
         new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
92
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
93
         };
94
    }
95
96 a480ee80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 bbbfb6af Leszek Koltunski
  private int[][] generateState(int start, int end)
99 a480ee80 Leszek Koltunski
    {
100 bbbfb6af Leszek Koltunski
    int len = end-start+1;
101
    int[] tmp = new int[6*len];
102
103
    for(int i=0; i<len; i++)
104
      {
105
      tmp[6*i  ] = start;
106
      tmp[6*i+1] = -1;
107
      tmp[6*i+2] = start;
108
      tmp[6*i+3] = start;
109
      tmp[6*i+4] = +1;
110
      tmp[6*i+5] = start;
111
112
      start++;
113
      }
114
115
    return new int[][] {tmp,tmp,tmp,tmp};
116 a480ee80 Leszek Koltunski
    }
117
118 0203be88 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120 bbbfb6af Leszek Koltunski
  private void initializeScrambleStates(int numLayers)
121 0203be88 Leszek Koltunski
    {
122 bbbfb6af Leszek Koltunski
    mStates = new ScrambleState[numLayers];
123 0203be88 Leszek Koltunski
124 bbbfb6af Leszek Koltunski
    for(int i=0; i<numLayers-1; i++)
125 0203be88 Leszek Koltunski
      {
126 bbbfb6af Leszek Koltunski
      mStates[i] = new ScrambleState( generateState(0,numLayers-1-i) );
127 0203be88 Leszek Koltunski
      }
128
129 bbbfb6af Leszek Koltunski
    mStates[numLayers-1] = new ScrambleState( generateState(1,numLayers-2) );
130
    }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
134
  int[] getSolvedQuats(int cubit, int numLayers)
135
    {
136 48fec01e Leszek Koltunski
    if( mQuats==null ) initializeQuats();
137 bbbfb6af Leszek Koltunski
    int status = retCubitSolvedStatus(cubit,numLayers);
138 48fec01e Leszek Koltunski
    return status<0 ? null : buildSolvedQuats(MovementPyraminx.FACE_AXIS[status],mQuats);
139 0203be88 Leszek Koltunski
    }
140
141 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143 e6cf7283 Leszek Koltunski
  private void addTetrahedralLattice(int size, int index, float[][] pos)
144 49f67f9b Leszek Koltunski
    {
145 ac940e24 Leszek Koltunski
    final float DX = 1.0f;
146
    final float DY = SQ2/2;
147
    final float DZ = 1.0f;
148 49f67f9b Leszek Koltunski
149 ac940e24 Leszek Koltunski
    float startX = 0.0f;
150
    float startY =-DY*(size-1)/2;
151
    float startZ = DZ*(size-1)/2;
152 769409d2 Leszek Koltunski
153 ac940e24 Leszek Koltunski
    for(int layer=0; layer<size; layer++)
154 49f67f9b Leszek Koltunski
      {
155 ac940e24 Leszek Koltunski
      float currX = startX;
156
      float currY = startY;
157
158
      for(int x=0; x<layer+1; x++)
159
        {
160
        float currZ = startZ;
161
162
        for(int z=0; z<size-layer; z++)
163
          {
164 e6cf7283 Leszek Koltunski
          pos[index] = new float[] {currX,currY,currZ};
165 ac940e24 Leszek Koltunski
          index++;
166
          currZ -= DZ;
167
          }
168
169
        currX += DX;
170
        }
171
172
      startX-=DX/2;
173
      startY+=DY;
174
      startZ-=DZ/2;
175 49f67f9b Leszek Koltunski
      }
176
    }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179 ac940e24 Leszek Koltunski
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
180 49f67f9b Leszek Koltunski
181 e6cf7283 Leszek Koltunski
  float[][] getCubitPositions(int size)
182 e844c116 Leszek Koltunski
    {
183 ac940e24 Leszek Koltunski
    int numOcta = (size-1)*size*(size+1)/6;
184
    int numTetra= size*(size+1)*(size+2)/6;
185 e6cf7283 Leszek Koltunski
    float[][] ret = new float[numOcta+numTetra][];
186 49f67f9b Leszek Koltunski
187 ac940e24 Leszek Koltunski
    addTetrahedralLattice(size-1,      0,ret);
188
    addTetrahedralLattice(size  ,numOcta,ret);
189 49f67f9b Leszek Koltunski
190 ac940e24 Leszek Koltunski
    return ret;
191 e844c116 Leszek Koltunski
    }
192
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
195 10585385 Leszek Koltunski
  Static4D[] getQuats()
196 e844c116 Leszek Koltunski
    {
197 48fec01e Leszek Koltunski
    if( mQuats==null ) initializeQuats();
198
    return mQuats;
199 e844c116 Leszek Koltunski
    }
200
201 169219a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
  int getSolvedFunctionIndex()
204
    {
205
    return 0;
206
    }
207
208 eab9d8f8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210 a64e07d0 Leszek Koltunski
  int getNumStickerTypes(int numLayers)
211 eab9d8f8 Leszek Koltunski
    {
212 48fec01e Leszek Koltunski
    return 1;
213 eab9d8f8 Leszek Koltunski
    }
214
215 7403cdfa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217 5da517d3 Leszek Koltunski
  float[][] getCuts(int numLayers)
218 7403cdfa Leszek Koltunski
    {
219 ef018c1b Leszek Koltunski
    if( mCuts==null )
220 a97e02b7 Leszek Koltunski
      {
221 ef018c1b Leszek Koltunski
      mCuts = new float[4][numLayers-1];
222
223
      for(int i=0; i<numLayers-1; i++)
224
        {
225 4946b635 Leszek Koltunski
        float cut = (1.0f+i-numLayers/4.0f)*(SQ6/3);
226 ef018c1b Leszek Koltunski
        mCuts[0][i] = cut;
227
        mCuts[1][i] = cut;
228
        mCuts[2][i] = cut;
229
        mCuts[3][i] = cut;
230
        }
231 a97e02b7 Leszek Koltunski
      }
232
233 ef018c1b Leszek Koltunski
    return mCuts;
234
    }
235
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238
  private void getLayerRotatable(int numLayers)
239
    {
240
    if( mLayerRotatable==null )
241
      {
242
      int numAxis = ROT_AXIS.length;
243
      boolean[] tmp = new boolean[numLayers];
244
      for(int i=0; i<numLayers; i++) tmp[i] = true;
245
      mLayerRotatable = new boolean[numAxis][];
246
      for(int i=0; i<numAxis; i++) mLayerRotatable[i] = tmp;
247
      }
248 7403cdfa Leszek Koltunski
    }
249
250 8f53e513 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
251
252
  int getNumCubitFaces()
253
    {
254 ac940e24 Leszek Koltunski
    return 8;
255 8f53e513 Leszek Koltunski
    }
256
257 31cd7256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
258
259
  private int getNumOctahedrons(int numLayers)
260
    {
261
    return (numLayers-1)*numLayers*(numLayers+1)/6;
262
    }
263
264 f6d06256 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266 ac940e24 Leszek Koltunski
  private int faceColor(int cubit, int axis)
267 f6d06256 Leszek Koltunski
    {
268 abf36986 Leszek Koltunski
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_TEXTURES;
269 f6d06256 Leszek Koltunski
    }
270
271 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
272
273 ac940e24 Leszek Koltunski
  int getFaceColor(int cubit, int cubitface, int size)
274 e844c116 Leszek Koltunski
    {
275 ac940e24 Leszek Koltunski
    if( cubit< (size-1)*size*(size+1)/6 )
276 40ab026e Leszek Koltunski
      {
277 ac940e24 Leszek Koltunski
      switch( cubitface )
278
        {
279
        case 0: return faceColor(cubit,0);
280
        case 2: return faceColor(cubit,1);
281
        case 5: return faceColor(cubit,3);
282
        case 7: return faceColor(cubit,2);
283 abf36986 Leszek Koltunski
        default:return NUM_TEXTURES;
284 ac940e24 Leszek Koltunski
        }
285 40ab026e Leszek Koltunski
      }
286 ac940e24 Leszek Koltunski
    else
287 89a11f7b Leszek Koltunski
      {
288 abf36986 Leszek Koltunski
      return cubitface<NUM_TEXTURES ? faceColor(cubit,cubitface) : NUM_TEXTURES;
289 89a11f7b Leszek Koltunski
      }
290 e844c116 Leszek Koltunski
    }
291
292 045d8cbd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
293
294
  ObjectShape getObjectShape(int cubit, int numLayers)
295
    {
296
    int variant = getCubitVariant(cubit,numLayers);
297
298
    if( variant==0 )
299
      {
300 48fec01e Leszek Koltunski
      double[][] vertices = new double[][] { { 0.5,0.0,0.5},{ 0.5,0.0,-0.5},{-0.5,0.0,-0.5},{-0.5,0.0,0.5},{ 0.0,SQ2/2,0.0},{ 0.0,-SQ2/2,0.0} };
301
      int[][] vert_indices = new int[][] { {3,0,4},{0,1,4},{1,2,4},{2,3,4},{5,0,3},{5,1,0},{5,2,1},{5,3,2} };
302 045d8cbd Leszek Koltunski
      int N = numLayers==3? 6 : 5;
303
      int E = numLayers==3? 2 : 1;
304
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
305
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
306
      float[][] corners   = new float[][] { {0.04f,0.20f} };
307
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
308
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
309
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
310 48fec01e Leszek Koltunski
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
311 045d8cbd Leszek Koltunski
      }
312
    else
313
      {
314 48fec01e Leszek Koltunski
      double[][] vertices = new double[][] { {-0.5, SQ2/4, 0.0},{ 0.5, SQ2/4, 0.0},{ 0.0,-SQ2/4, 0.5},{ 0.0,-SQ2/4,-0.5} };
315
      int[][] vert_indices = new int[][] { {2,1,0},{3,0,1},{3,2,0},{2,3,1} };
316 045d8cbd Leszek Koltunski
      int N = numLayers==3? 6 : 5;
317
      int E = numLayers==3? 2 : 1;
318
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
319
      int[] bandIndices   = new int[] { 0,0,0,0 };
320
      float[][] corners   = new float[][] { {0.06f,0.15f} };
321
      int[] cornerIndices = new int[] { 0,0,0,0 };
322
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
323
      int[] centerIndices = new int[] { 0,0,0,0 };
324 48fec01e Leszek Koltunski
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
325 045d8cbd Leszek Koltunski
      }
326
    }
327
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
330 3e605536 Leszek Koltunski
  Static4D getQuat(int cubit, int numLayers)
331 045d8cbd Leszek Koltunski
    {
332 48fec01e Leszek Koltunski
    if( mQuats==null ) initializeQuats();
333
    return mQuats[0];
334 045d8cbd Leszek Koltunski
    }
335
336
///////////////////////////////////////////////////////////////////////////////////////////////////
337
338 3e605536 Leszek Koltunski
  int getNumCubitVariants(int numLayers)
339 045d8cbd Leszek Koltunski
    {
340
    return 2;
341
    }
342
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344
345
  int getCubitVariant(int cubit, int numLayers)
346
    {
347
    return cubit<getNumOctahedrons(numLayers) ? 0:1;
348
    }
349
350 9c06394a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
351 76c2bd07 Leszek Koltunski
352 9c06394a Leszek Koltunski
  ObjectSticker retSticker(int face)
353
    {
354 48fec01e Leszek Koltunski
    if( mStickers==null )
355
      {
356
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
357
      final float stroke = 0.08f;
358
      final float radius = 0.06f;
359
      final float[] radii= {radius,radius,radius};
360
      mStickers = new ObjectSticker[STICKERS.length];
361
      mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke);
362
      }
363
364 abf36986 Leszek Koltunski
    return mStickers[face/NUM_FACE_COLORS];
365 7289fd6c Leszek Koltunski
    }
366
367 7c969a6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
368 91792184 Leszek Koltunski
// public API
369 bbbfb6af Leszek Koltunski
370
  public Static3D[] getRotationAxis()
371
    {
372
    return ROT_AXIS;
373
    }
374
375 e9a87113 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
376
377
  public Movement getMovement()
378
    {
379 ef018c1b Leszek Koltunski
    if( mMovement==null )
380
      {
381
      int numLayers = getNumLayers();
382
      if( mCuts==null ) getCuts(numLayers);
383
      getLayerRotatable(numLayers);
384
385
      mMovement = new MovementPyraminx(mCuts,mLayerRotatable,numLayers);
386
      }
387 e9a87113 Leszek Koltunski
    return mMovement;
388
    }
389
390 bbbfb6af Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
391
392
  public int[] getBasicAngle()
393
    {
394 48fec01e Leszek Koltunski
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
395
    return mBasicAngle;
396 e46e17fb Leszek Koltunski
    }
397 f0336037 Leszek Koltunski
398 6fd4a72c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
399
400
  public int getObjectName(int numLayers)
401
    {
402
    switch(numLayers)
403
      {
404
      case 3: return R.string.pyra3;
405
      case 4: return R.string.pyra4;
406
      case 5: return R.string.pyra5;
407
      }
408
    return R.string.pyra3;
409
    }
410
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
413
  public int getInventor(int numLayers)
414
    {
415
    switch(numLayers)
416
      {
417
      case 3: return R.string.pyra3_inventor;
418
      case 4: return R.string.pyra4_inventor;
419
      case 5: return R.string.pyra5_inventor;
420
      }
421
    return R.string.pyra3_inventor;
422
    }
423
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
426
  public int getComplexity(int numLayers)
427
    {
428
    switch(numLayers)
429
      {
430
      case 3: return 4;
431
      case 4: return 6;
432
      case 5: return 8;
433
      }
434
    return 4;
435
    }
436 e844c116 Leszek Koltunski
}