Project

General

Profile

Download (15.7 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / objects / TwistyPyraminx.java @ 8592461c

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.objectlib.objects;
21

    
22
import static org.distorted.objectlib.main.Movement.TYPE_NOT_SPLIT;
23

    
24
import android.content.res.Resources;
25

    
26
import org.distorted.library.main.DistortedEffects;
27
import org.distorted.library.main.DistortedTexture;
28
import org.distorted.library.mesh.MeshSquare;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31

    
32
import org.distorted.objectlib.R;
33
import org.distorted.objectlib.main.Movement;
34
import org.distorted.objectlib.main.Movement4;
35
import org.distorted.objectlib.main.ObjectControl;
36
import org.distorted.objectlib.main.ObjectType;
37
import org.distorted.objectlib.helpers.ObjectShape;
38
import org.distorted.objectlib.helpers.ObjectSticker;
39
import org.distorted.objectlib.helpers.ScrambleState;
40
import org.distorted.objectlib.main.Twisty4;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
public class TwistyPyraminx extends Twisty4
45
{
46
  static final Static3D[] ROT_AXIS = new Static3D[]
47
         {
48
           new Static3D(     0,-SQ3/3,-SQ6/3),
49
           new Static3D(     0,-SQ3/3,+SQ6/3),
50
           new Static3D(+SQ6/3,+SQ3/3,     0),
51
           new Static3D(-SQ6/3,+SQ3/3,     0),
52
         };
53

    
54
  private static final int[][][] ENABLED = new int[][][]
55
      {
56
          {{1,2,3}},{{0,2,3}},{{0,1,3}},{{0,1,2}}
57
      };
58

    
59
  private ScrambleState[] mStates;
60
  private int[] mBasicAngle;
61
  private Static4D[] mQuats;
62
  private float[][] mCuts;
63
  private boolean[][] mLayerRotatable;
64
  private ObjectSticker[] mStickers;
65
  private Movement mMovement;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  public TwistyPyraminx(int size, Static4D quat, Static3D move, DistortedTexture texture,
70
                        MeshSquare mesh, DistortedEffects effects, Resources res, int scrWidth)
71
    {
72
    super(size, size, quat, move, texture, mesh, effects, res, scrWidth);
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  protected ScrambleState[] getScrambleStates()
78
    {
79
    if( mStates==null )
80
      {
81
      int numLayers = getNumLayers();
82
      initializeScrambleStates(numLayers);
83
      }
84

    
85
    return mStates;
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  protected int getResource(int numLayers)
91
    {
92
    switch(numLayers)
93
      {
94
      case 3: return R.raw.pyra3;
95
      case 4: return R.raw.pyra4;
96
      case 5: return R.raw.pyra5;
97
      }
98

    
99
    return 0;
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private void initializeQuats()
105
    {
106
    mQuats = new Static4D[]
107
         {
108
         new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
109
         new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
110
         new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
111
         new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
112
         new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
113
         new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
114
         new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
115
         new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
116
         new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
117
         new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
118
         new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
119
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
120
         };
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private int[][] generateState(int start, int end)
126
    {
127
    int len = end-start+1;
128
    int[] tmp = new int[6*len];
129

    
130
    for(int i=0; i<len; i++)
131
      {
132
      tmp[6*i  ] = start;
133
      tmp[6*i+1] = -1;
134
      tmp[6*i+2] = start;
135
      tmp[6*i+3] = start;
136
      tmp[6*i+4] = +1;
137
      tmp[6*i+5] = start;
138

    
139
      start++;
140
      }
141

    
142
    return new int[][] {tmp,tmp,tmp,tmp};
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  private void initializeScrambleStates(int numLayers)
148
    {
149
    mStates = new ScrambleState[numLayers];
150

    
151
    for(int i=0; i<numLayers-1; i++)
152
      {
153
      mStates[i] = new ScrambleState( generateState(0,numLayers-1-i) );
154
      }
155

    
156
    mStates[numLayers-1] = new ScrambleState( generateState(1,numLayers-2) );
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  protected int[] getSolvedQuats(int cubit, int numLayers)
162
    {
163
    if( mQuats==null ) initializeQuats();
164
    int status = retCubitSolvedStatus(cubit,numLayers);
165
    return status<0 ? null : buildSolvedQuats(Movement4.FACE_AXIS[status],mQuats);
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private void addTetrahedralLattice(int size, int index, float[][] pos)
171
    {
172
    final float DX = 1.0f;
173
    final float DY = SQ2/2;
174
    final float DZ = 1.0f;
175

    
176
    float startX = 0.0f;
177
    float startY =-DY*(size-1)/2;
178
    float startZ = DZ*(size-1)/2;
179

    
180
    for(int layer=0; layer<size; layer++)
181
      {
182
      float currX = startX;
183
      float currY = startY;
184

    
185
      for(int x=0; x<layer+1; x++)
186
        {
187
        float currZ = startZ;
188

    
189
        for(int z=0; z<size-layer; z++)
190
          {
191
          pos[index] = new float[] {currX,currY,currZ};
192
          index++;
193
          currZ -= DZ;
194
          }
195

    
196
        currX += DX;
197
        }
198

    
199
      startX-=DX/2;
200
      startY+=DY;
201
      startZ-=DZ/2;
202
      }
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
// there are (n^3-n)/6 octahedrons and ((n+1)^3 - (n+1))/6 tetrahedrons
207

    
208
  protected float[][] getCubitPositions(int size)
209
    {
210
    int numOcta = (size-1)*size*(size+1)/6;
211
    int numTetra= size*(size+1)*(size+2)/6;
212
    float[][] ret = new float[numOcta+numTetra][];
213

    
214
    addTetrahedralLattice(size-1,      0,ret);
215
    addTetrahedralLattice(size  ,numOcta,ret);
216

    
217
    return ret;
218
    }
219

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

    
222
  protected Static4D[] getQuats()
223
    {
224
    if( mQuats==null ) initializeQuats();
225
    return mQuats;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  protected int getSolvedFunctionIndex()
231
    {
232
    return 0;
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  protected int getNumStickerTypes(int numLayers)
238
    {
239
    return 1;
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  protected float[][] getCuts(int numLayers)
245
    {
246
    if( mCuts==null )
247
      {
248
      mCuts = new float[4][numLayers-1];
249

    
250
      for(int i=0; i<numLayers-1; i++)
251
        {
252
        float cut = (1.0f+i-numLayers/4.0f)*(SQ6/3);
253
        mCuts[0][i] = cut;
254
        mCuts[1][i] = cut;
255
        mCuts[2][i] = cut;
256
        mCuts[3][i] = cut;
257
        }
258
      }
259

    
260
    return mCuts;
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private void getLayerRotatable(int numLayers)
266
    {
267
    if( mLayerRotatable==null )
268
      {
269
      int numAxis = ROT_AXIS.length;
270
      boolean[] tmp = new boolean[numLayers];
271
      for(int i=0; i<numLayers; i++) tmp[i] = true;
272
      mLayerRotatable = new boolean[numAxis][];
273
      for(int i=0; i<numAxis; i++) mLayerRotatable[i] = tmp;
274
      }
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  protected int getNumCubitFaces()
280
    {
281
    return 8;
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  private int getNumOctahedrons(int numLayers)
287
    {
288
    return (numLayers-1)*numLayers*(numLayers+1)/6;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  private int faceColor(int cubit, int axis)
294
    {
295
    return CUBITS[cubit].getRotRow(axis) == 1 ? axis : NUM_TEXTURES;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  protected int getFaceColor(int cubit, int cubitface, int size)
301
    {
302
    if( cubit< (size-1)*size*(size+1)/6 )
303
      {
304
      switch( cubitface )
305
        {
306
        case 0: return faceColor(cubit,0);
307
        case 2: return faceColor(cubit,1);
308
        case 5: return faceColor(cubit,3);
309
        case 7: return faceColor(cubit,2);
310
        default:return NUM_TEXTURES;
311
        }
312
      }
313
    else
314
      {
315
      return cubitface<NUM_TEXTURES ? faceColor(cubit,cubitface) : NUM_TEXTURES;
316
      }
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  protected ObjectShape getObjectShape(int cubit, int numLayers)
322
    {
323
    int variant = getCubitVariant(cubit,numLayers);
324

    
325
    if( variant==0 )
326
      {
327
      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} };
328
      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} };
329
      int N = numLayers==3? 6 : 5;
330
      int E = numLayers==3? 2 : 1;
331
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
332
      int[] bandIndices   = new int[] { 0,0,0,0,0,0,0,0 };
333
      float[][] corners   = new float[][] { {0.04f,0.20f} };
334
      int[] cornerIndices = new int[] { 0,0,0,0,0,0 };
335
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
336
      int[] centerIndices = new int[] { 0,0,0,0,0,0 };
337
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
338
      }
339
    else
340
      {
341
      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} };
342
      int[][] vert_indices = new int[][] { {2,1,0},{3,0,1},{3,2,0},{2,3,1} };
343
      int N = numLayers==3? 6 : 5;
344
      int E = numLayers==3? 2 : 1;
345
      float[][] bands     = new float[][] { {0.05f,35,0.5f,0.8f,N,E,E} };
346
      int[] bandIndices   = new int[] { 0,0,0,0 };
347
      float[][] corners   = new float[][] { {0.06f,0.15f} };
348
      int[] cornerIndices = new int[] { 0,0,0,0 };
349
      float[][] centers   = new float[][] { {0.0f, 0.0f, 0.0f} };
350
      int[] centerIndices = new int[] { 0,0,0,0 };
351
      return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
352
      }
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  protected Static4D getQuat(int cubit, int numLayers)
358
    {
359
    if( mQuats==null ) initializeQuats();
360
    return mQuats[0];
361
    }
362

    
363
///////////////////////////////////////////////////////////////////////////////////////////////////
364

    
365
  protected int getNumCubitVariants(int numLayers)
366
    {
367
    return 2;
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
  protected int getCubitVariant(int cubit, int numLayers)
373
    {
374
    return cubit<getNumOctahedrons(numLayers) ? 0:1;
375
    }
376

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

    
379
  protected ObjectSticker retSticker(int face)
380
    {
381
    if( mStickers==null )
382
      {
383
      float[][] STICKERS = new float[][] { { -0.4330127f, -0.25f, 0.4330127f, -0.25f, 0.0f, 0.5f } };
384
      final float radius = 0.06f;
385
      final float[] radii= {radius,radius,radius};
386
      mStickers = new ObjectSticker[STICKERS.length];
387

    
388
      float stroke = 0.08f;
389

    
390
      if( ObjectControl.isInIconMode() )
391
        {
392
        switch(getNumLayers())
393
          {
394
          case 2: stroke*=1.0f; break;
395
          case 3: stroke*=1.4f; break;
396
          case 4: stroke*=1.7f; break;
397
          default:stroke*=1.9f; break;
398
          }
399
        }
400

    
401
      mStickers[0] = new ObjectSticker(STICKERS[0],null,radii,stroke);
402
      }
403

    
404
    return mStickers[face/NUM_FACE_COLORS];
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
// public API
409

    
410
  public Static3D[] getRotationAxis()
411
    {
412
    return ROT_AXIS;
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416

    
417
  public Movement getMovement()
418
    {
419
    if( mMovement==null )
420
      {
421
      int numLayers = getNumLayers();
422
      if( mCuts==null ) getCuts(numLayers);
423
      getLayerRotatable(numLayers);
424
      mMovement = new Movement4(ROT_AXIS,mCuts,mLayerRotatable,numLayers,TYPE_NOT_SPLIT,ENABLED);
425
      }
426
    return mMovement;
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  public int[] getBasicAngle()
432
    {
433
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 3,3,3,3 };
434
    return mBasicAngle;
435
    }
436

    
437
///////////////////////////////////////////////////////////////////////////////////////////////////
438

    
439
  public ObjectType intGetObjectType(int numLayers)
440
    {
441
    switch(numLayers)
442
      {
443
      case 3: return ObjectType.PYRA_3;
444
      case 4: return ObjectType.PYRA_4;
445
      case 5: return ObjectType.PYRA_5;
446
      }
447

    
448
    return ObjectType.PYRA_3;
449
    }
450

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

    
453
  public int getObjectName(int numLayers)
454
    {
455
    switch(numLayers)
456
      {
457
      case 3: return R.string.pyra3;
458
      case 4: return R.string.pyra4;
459
      case 5: return R.string.pyra5;
460
      }
461
    return R.string.pyra3;
462
    }
463

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

    
466
  public int getInventor(int numLayers)
467
    {
468
    switch(numLayers)
469
      {
470
      case 3: return R.string.pyra3_inventor;
471
      case 4: return R.string.pyra4_inventor;
472
      case 5: return R.string.pyra5_inventor;
473
      }
474
    return R.string.pyra3_inventor;
475
    }
476

    
477
///////////////////////////////////////////////////////////////////////////////////////////////////
478

    
479
  public int getComplexity(int numLayers)
480
    {
481
    switch(numLayers)
482
      {
483
      case 3: return 4;
484
      case 4: return 6;
485
      case 5: return 8;
486
      }
487
    return 4;
488
    }
489
}
(18-18/25)