Project

General

Profile

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

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

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 static org.distorted.objects.Movement.TYPE_NOT_SPLIT;
23

    
24
import android.content.res.Resources;
25

    
26
import org.distorted.helpers.ObjectShape;
27
import org.distorted.helpers.ObjectSticker;
28
import org.distorted.helpers.ScrambleState;
29
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedTexture;
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
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

    
48
  private static final int[][][] ENABLED = new int[][][]
49
      {
50
          {{1,2,3}},{{0,2,3}},{{0,1,3}},{{0,1,2}}
51
      };
52

    
53
  private ScrambleState[] mStates;
54
  private int[] mBasicAngle;
55
  private Static4D[] mQuats;
56
  private float[][] mCuts;
57
  private boolean[][] mLayerRotatable;
58
  private ObjectSticker[] mStickers;
59
  private Movement mMovement;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  TwistyPyraminx(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
64
                 DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
65
    {
66
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.PYRA, res, scrWidth);
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  ScrambleState[] getScrambleStates()
72
    {
73
    if( mStates==null )
74
      {
75
      int numLayers = getNumLayers();
76
      initializeScrambleStates(numLayers);
77
      }
78

    
79
    return mStates;
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  private void initializeQuats()
85
    {
86
    mQuats = new Static4D[]
87
         {
88
         new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
89
         new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
90
         new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
91
         new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
92
         new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
93
         new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
94
         new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
95
         new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
96
         new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
97
         new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
98
         new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
99
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
100
         };
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  private int[][] generateState(int start, int end)
106
    {
107
    int len = end-start+1;
108
    int[] tmp = new int[6*len];
109

    
110
    for(int i=0; i<len; i++)
111
      {
112
      tmp[6*i  ] = start;
113
      tmp[6*i+1] = -1;
114
      tmp[6*i+2] = start;
115
      tmp[6*i+3] = start;
116
      tmp[6*i+4] = +1;
117
      tmp[6*i+5] = start;
118

    
119
      start++;
120
      }
121

    
122
    return new int[][] {tmp,tmp,tmp,tmp};
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void initializeScrambleStates(int numLayers)
128
    {
129
    mStates = new ScrambleState[numLayers];
130

    
131
    for(int i=0; i<numLayers-1; i++)
132
      {
133
      mStates[i] = new ScrambleState( generateState(0,numLayers-1-i) );
134
      }
135

    
136
    mStates[numLayers-1] = new ScrambleState( generateState(1,numLayers-2) );
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  int[] getSolvedQuats(int cubit, int numLayers)
142
    {
143
    if( mQuats==null ) initializeQuats();
144
    int status = retCubitSolvedStatus(cubit,numLayers);
145
    return status<0 ? null : buildSolvedQuats(Movement4.FACE_AXIS[status],mQuats);
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

    
156
    float startX = 0.0f;
157
    float startY =-DY*(size-1)/2;
158
    float startZ = DZ*(size-1)/2;
159

    
160
    for(int layer=0; layer<size; layer++)
161
      {
162
      float currX = startX;
163
      float currY = startY;
164

    
165
      for(int x=0; x<layer+1; x++)
166
        {
167
        float currZ = startZ;
168

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

    
176
        currX += DX;
177
        }
178

    
179
      startX-=DX/2;
180
      startY+=DY;
181
      startZ-=DZ/2;
182
      }
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
187

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

    
194
    addTetrahedralLattice(size-1,      0,ret);
195
    addTetrahedralLattice(size  ,numOcta,ret);
196

    
197
    return ret;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  Static4D[] getQuats()
203
    {
204
    if( mQuats==null ) initializeQuats();
205
    return mQuats;
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  int getSolvedFunctionIndex()
211
    {
212
    return 0;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  int getNumStickerTypes(int numLayers)
218
    {
219
    return 1;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  float[][] getCuts(int numLayers)
225
    {
226
    if( mCuts==null )
227
      {
228
      mCuts = new float[4][numLayers-1];
229

    
230
      for(int i=0; i<numLayers-1; i++)
231
        {
232
        float cut = (1.0f+i-numLayers/4.0f)*(SQ6/3);
233
        mCuts[0][i] = cut;
234
        mCuts[1][i] = cut;
235
        mCuts[2][i] = cut;
236
        mCuts[3][i] = cut;
237
        }
238
      }
239

    
240
    return mCuts;
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  private void getLayerRotatable(int numLayers)
246
    {
247
    if( mLayerRotatable==null )
248
      {
249
      int numAxis = ROT_AXIS.length;
250
      boolean[] tmp = new boolean[numLayers];
251
      for(int i=0; i<numLayers; i++) tmp[i] = true;
252
      mLayerRotatable = new boolean[numAxis][];
253
      for(int i=0; i<numAxis; i++) mLayerRotatable[i] = tmp;
254
      }
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  int getNumCubitFaces()
260
    {
261
    return 8;
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  private int getNumOctahedrons(int numLayers)
267
    {
268
    return (numLayers-1)*numLayers*(numLayers+1)/6;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  private int faceColor(int cubit, int axis)
274
    {
275
    return CUBITS[cubit].mRotationRow[axis] == 1 ? axis : NUM_TEXTURES;
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  int getFaceColor(int cubit, int cubitface, int size)
281
    {
282
    if( cubit< (size-1)*size*(size+1)/6 )
283
      {
284
      switch( cubitface )
285
        {
286
        case 0: return faceColor(cubit,0);
287
        case 2: return faceColor(cubit,1);
288
        case 5: return faceColor(cubit,3);
289
        case 7: return faceColor(cubit,2);
290
        default:return NUM_TEXTURES;
291
        }
292
      }
293
    else
294
      {
295
      return cubitface<NUM_TEXTURES ? faceColor(cubit,cubitface) : NUM_TEXTURES;
296
      }
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  ObjectShape getObjectShape(int cubit, int numLayers)
302
    {
303
    int variant = getCubitVariant(cubit,numLayers);
304

    
305
    if( variant==0 )
306
      {
307
      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} };
308
      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} };
309
      int N = numLayers==3? 6 : 5;
310
      int E = numLayers==3? 2 : 1;
311
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
312
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
313
      float[][] corners   = new float[][] { {0.04f,0.20f} };
314
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
315
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
316
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
317
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
318
      }
319
    else
320
      {
321
      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} };
322
      int[][] vert_indices = new int[][] { {2,1,0},{3,0,1},{3,2,0},{2,3,1} };
323
      int N = numLayers==3? 6 : 5;
324
      int E = numLayers==3? 2 : 1;
325
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
326
      int[] bandIndices   = new int[] { 0,0,0,0 };
327
      float[][] corners   = new float[][] { {0.06f,0.15f} };
328
      int[] cornerIndices = new int[] { 0,0,0,0 };
329
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
330
      int[] centerIndices = new int[] { 0,0,0,0 };
331
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
332
      }
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  Static4D getQuat(int cubit, int numLayers)
338
    {
339
    if( mQuats==null ) initializeQuats();
340
    return mQuats[0];
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  int getNumCubitVariants(int numLayers)
346
    {
347
    return 2;
348
    }
349

    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351

    
352
  int getCubitVariant(int cubit, int numLayers)
353
    {
354
    return cubit<getNumOctahedrons(numLayers) ? 0:1;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  ObjectSticker retSticker(int face)
360
    {
361
    if( mStickers==null )
362
      {
363
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
364
      final float stroke = 0.08f;
365
      final float radius = 0.06f;
366
      final float[] radii= {radius,radius,radius};
367
      mStickers = new ObjectSticker[STICKERS.length];
368
      mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke);
369
      }
370

    
371
    return mStickers[face/NUM_FACE_COLORS];
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
// public API
376

    
377
  public Static3D[] getRotationAxis()
378
    {
379
    return ROT_AXIS;
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  public Movement getMovement()
385
    {
386
    if( mMovement==null )
387
      {
388
      int numLayers = getNumLayers();
389
      if( mCuts==null ) getCuts(numLayers);
390
      getLayerRotatable(numLayers);
391
      mMovement = new Movement4(ROT_AXIS,mCuts,mLayerRotatable,numLayers,TYPE_NOT_SPLIT,ENABLED);
392
      }
393
    return mMovement;
394
    }
395

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

    
398
  public int[] getBasicAngle()
399
    {
400
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
401
    return mBasicAngle;
402
    }
403

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
  public int getObjectName(int numLayers)
407
    {
408
    switch(numLayers)
409
      {
410
      case 3: return R.string.pyra3;
411
      case 4: return R.string.pyra4;
412
      case 5: return R.string.pyra5;
413
      }
414
    return R.string.pyra3;
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  public int getInventor(int numLayers)
420
    {
421
    switch(numLayers)
422
      {
423
      case 3: return R.string.pyra3_inventor;
424
      case 4: return R.string.pyra4_inventor;
425
      case 5: return R.string.pyra5_inventor;
426
      }
427
    return R.string.pyra3_inventor;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  public int getComplexity(int numLayers)
433
    {
434
    switch(numLayers)
435
      {
436
      case 3: return 4;
437
      case 4: return 6;
438
      case 5: return 8;
439
      }
440
    return 4;
441
    }
442
}
(31-31/38)