Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyBandagedAbstract.java @ e9a87113

1 4c0a6d97 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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
24 f10a88a8 Leszek Koltunski
import org.distorted.helpers.ObjectShape;
25 9c06394a Leszek Koltunski
import org.distorted.helpers.ObjectSticker;
26 6cf89a3e Leszek Koltunski
import org.distorted.helpers.ScrambleState;
27 4c0a6d97 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.mesh.MeshSquare;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
35 538ee7a6 Leszek Koltunski
abstract class TwistyBandagedAbstract extends TwistyObject
36 4c0a6d97 Leszek Koltunski
{
37
  // the three rotation axis of a 3x3 Cube. Must be normalized.
38
  static final Static3D[] ROT_AXIS = new Static3D[]
39
         {
40
           new Static3D(1,0,0),
41
           new Static3D(0,1,0),
42
           new Static3D(0,0,1)
43
         };
44
45
  private static final int[] FACE_COLORS = new int[]
46
         {
47
           COLOR_YELLOW, COLOR_WHITE,
48
           COLOR_BLUE  , COLOR_GREEN,
49
           COLOR_RED   , COLOR_ORANGE
50
         };
51
52 538ee7a6 Leszek Koltunski
  private static final int[][] mDimensions = new int[][]
53
        {
54
         {1,1,1},  // has to be X>=Z>=Y so that all
55
         {2,1,1},  // the faces are horizontal
56
         {3,1,1},
57
         {2,1,2},
58
         {2,2,2}
59 4c0a6d97 Leszek Koltunski
        };
60
61 97eb0852 Leszek Koltunski
  private static final int NUM_STICKERS = 4;
62 9c06394a Leszek Koltunski
63 97eb0852 Leszek Koltunski
  private int[] mBasicAngle;
64
  private Static4D[] mQuats;
65
  private ObjectSticker[] mStickers;
66
  private Static4D[] mInitQuats;
67
  private int[][] mAxisMap;
68
  private int[][] mFaceMap;
69 e9a87113 Leszek Koltunski
  private Movement mMovement;
70 f2d0d23e Leszek Koltunski
  ScrambleState[] mStates;
71
  float[][] POSITIONS;
72
  int[] QUAT_INDICES;
73 38589947 Leszek Koltunski
74 4c0a6d97 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 538ee7a6 Leszek Koltunski
  TwistyBandagedAbstract(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
77
                         DistortedEffects effects, int[][] moves, ObjectList list, Resources res, int scrWidth)
78 4c0a6d97 Leszek Koltunski
    {
79
    super(size, size, quat, texture, mesh, effects, moves, list, res, scrWidth);
80
    }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84 68ce0d53 Leszek Koltunski
  abstract float[][] getPositions();
85
  abstract int[] getQuatIndices();
86
87 97eb0852 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89
  private void initializeQuats()
90
    {
91
    mQuats = new Static4D[]
92
         {
93
         new Static4D(  0.0f,   0.0f,   0.0f,   1.0f),
94
         new Static4D(  1.0f,   0.0f,   0.0f,   0.0f),
95
         new Static4D(  0.0f,   1.0f,   0.0f,   0.0f),
96
         new Static4D(  0.0f,   0.0f,   1.0f,   0.0f),
97
98
         new Static4D( SQ2/2,  SQ2/2,  0.0f ,   0.0f),
99
         new Static4D( SQ2/2, -SQ2/2,  0.0f ,   0.0f),
100
         new Static4D( SQ2/2,   0.0f,  SQ2/2,   0.0f),
101
         new Static4D(-SQ2/2,   0.0f,  SQ2/2,   0.0f),
102
         new Static4D( SQ2/2,   0.0f,   0.0f,  SQ2/2),
103
         new Static4D( SQ2/2,   0.0f,   0.0f, -SQ2/2),
104
         new Static4D(  0.0f,  SQ2/2,  SQ2/2,   0.0f),
105
         new Static4D(  0.0f,  SQ2/2, -SQ2/2,   0.0f),
106
         new Static4D(  0.0f,  SQ2/2,   0.0f,  SQ2/2),
107
         new Static4D(  0.0f,  SQ2/2,   0.0f, -SQ2/2),
108
         new Static4D(  0.0f,   0.0f,  SQ2/2,  SQ2/2),
109
         new Static4D(  0.0f,   0.0f,  SQ2/2, -SQ2/2),
110
111
         new Static4D(  0.5f,   0.5f,   0.5f,   0.5f),
112
         new Static4D(  0.5f,   0.5f,  -0.5f,   0.5f),
113
         new Static4D(  0.5f,   0.5f,  -0.5f,  -0.5f),
114
         new Static4D(  0.5f,  -0.5f,   0.5f,  -0.5f),
115
         new Static4D( -0.5f,  -0.5f,  -0.5f,   0.5f),
116
         new Static4D( -0.5f,   0.5f,  -0.5f,  -0.5f),
117
         new Static4D( -0.5f,   0.5f,   0.5f,  -0.5f),
118
         new Static4D( -0.5f,   0.5f,   0.5f,   0.5f)
119
         };
120
    }
121
122 a480ee80 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  int[] getSolvedQuats(int cubit, int numLayers)
125
    {
126 97eb0852 Leszek Koltunski
    if( mQuats==null ) initializeQuats();
127 a480ee80 Leszek Koltunski
    int status = retCubitSolvedStatus(cubit,numLayers);
128 97eb0852 Leszek Koltunski
    return status<0 ? null : buildSolvedQuats(MovementCube.FACE_AXIS[status],mQuats);
129 a480ee80 Leszek Koltunski
    }
130
131 68ce0d53 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133 f10a88a8 Leszek Koltunski
  int getNumCubits()
134
    {
135
    return getPositions().length;
136
    }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140
  private float[] getCubitPosition(int cubit)
141 68ce0d53 Leszek Koltunski
    {
142
    float[][] pos = getPositions();
143
144 f10a88a8 Leszek Koltunski
    return ( cubit>=0 && cubit< pos.length ) ? pos[cubit] : null;
145
    }
146 68ce0d53 Leszek Koltunski
147 f10a88a8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149
  private int getQuatIndex(int cubit)
150
    {
151
    int[] indices = getQuatIndices();
152
    return ( cubit>=0 && cubit< indices.length ) ? indices[cubit] : 0;
153 68ce0d53 Leszek Koltunski
    }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 f10a88a8 Leszek Koltunski
  ObjectShape getObjectShape(int cubit, int numLayers)
158 68ce0d53 Leszek Koltunski
    {
159 f10a88a8 Leszek Koltunski
    int variant = getCubitVariant(cubit,numLayers);
160 68ce0d53 Leszek Koltunski
161 f10a88a8 Leszek Koltunski
    final int[][] vert_indices =
162
      {
163
        {2,3,1,0},
164
        {7,6,4,5},
165
        {4,0,1,5},
166
        {7,3,2,6},
167
        {6,2,0,4},
168
        {3,7,5,1},
169
      };
170
171
    float defHeight = 0.048f;
172
    int[] bandIndices = new int[] { 0,0,1,1,2,2 };
173
    float[][] corners = new float[][] { {0.04f,0.15f} };
174
    int[] cornerIndices = new int[] { 0,0,0,0,0,0,0,0 };
175
    int[] centerIndices = new int[] { 0,1,2,3,4,5,6,7 };
176
177
    int X = mDimensions[variant][0];
178
    int Y = mDimensions[variant][1];
179
    int Z = mDimensions[variant][2];
180
181
    int maxXY = Math.max(X,Y);
182
    int maxXZ = Math.max(X,Z);
183
    int maxYZ = Math.max(Y,Z);
184
185
    double[][] vertices =
186
      {
187
        {+0.5f*X,+0.5f*Y,+0.5f*Z},
188
        {+0.5f*X,+0.5f*Y,-0.5f*Z},
189
        {+0.5f*X,-0.5f*Y,+0.5f*Z},
190
        {+0.5f*X,-0.5f*Y,-0.5f*Z},
191
        {-0.5f*X,+0.5f*Y,+0.5f*Z},
192
        {-0.5f*X,+0.5f*Y,-0.5f*Z},
193
        {-0.5f*X,-0.5f*Y,+0.5f*Z},
194
        {-0.5f*X,-0.5f*Y,-0.5f*Z}
195
      };
196
197
    float[][] bands= new float[][]
198
      {
199
        {defHeight/maxYZ,65,0.25f,0.5f,5,1,2},
200
        {defHeight/maxXZ,65,0.25f,0.5f,5,1,2},
201
        {defHeight/maxXY,65,0.25f,0.5f,5,1,2}
202
      };
203
204
    float[][] centers = new float[][]
205
      {
206
        {+0.5f*(X-1),+0.5f*(Y-1),+0.5f*(Z-1)},
207
        {+0.5f*(X-1),+0.5f*(Y-1),-0.5f*(Z-1)},
208
        {+0.5f*(X-1),-0.5f*(Y-1),+0.5f*(Z-1)},
209
        {+0.5f*(X-1),-0.5f*(Y-1),-0.5f*(Z-1)},
210
        {-0.5f*(X-1),+0.5f*(Y-1),+0.5f*(Z-1)},
211
        {-0.5f*(X-1),+0.5f*(Y-1),-0.5f*(Z-1)},
212
        {-0.5f*(X-1),-0.5f*(Y-1),+0.5f*(Z-1)},
213
        {-0.5f*(X-1),-0.5f*(Y-1),-0.5f*(Z-1)}
214
      };
215
216
    return new ObjectShape(vertices,vert_indices,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
217 68ce0d53 Leszek Koltunski
    }
218 4c0a6d97 Leszek Koltunski
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221 3e605536 Leszek Koltunski
  Static4D getQuat(int cubit, int numLayers)
222 4c0a6d97 Leszek Koltunski
    {
223 97eb0852 Leszek Koltunski
    if( mInitQuats ==null )
224
      {
225
      mInitQuats = new Static4D[]
226
        {
227
        new Static4D(  0.0f,   0.0f,   0.0f,   1.0f),  // NULL
228
        new Static4D( SQ2/2,   0.0f,   0.0f, -SQ2/2),  // X
229
        new Static4D(  0.0f,  SQ2/2,   0.0f, -SQ2/2),  // Y
230
        new Static4D(  0.0f,   0.0f,  SQ2/2, -SQ2/2),  // Z
231
        new Static4D( -0.5f,  +0.5f,  -0.5f,  +0.5f),  // ZX
232
        new Static4D( +0.5f,  +0.5f,  +0.5f,  -0.5f),  // YX
233
        };
234
      }
235
236
    return mInitQuats[getQuatIndex(cubit)];
237 3e605536 Leszek Koltunski
    }
238 f10a88a8 Leszek Koltunski
239 3e605536 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
240
241
  int getNumCubitVariants(int numLayers)
242
    {
243
    return mDimensions.length;
244
    }
245
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
248
  int getCubitVariant(int cubit, int numLayers)
249
    {
250
    float[][] pos = getPositions();
251 f10a88a8 Leszek Koltunski
252 3e605536 Leszek Koltunski
    if( cubit>=0 && cubit<pos.length )
253 f10a88a8 Leszek Koltunski
      {
254 3e605536 Leszek Koltunski
      int numPoints = pos[cubit].length/3;
255
      return numPoints==8 ? 4 : numPoints-1;
256 4c0a6d97 Leszek Koltunski
      }
257
258 3e605536 Leszek Koltunski
    return 1;
259 4c0a6d97 Leszek Koltunski
    }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263 9c06394a Leszek Koltunski
  int getColor(int face)
264
    {
265
    return FACE_COLORS[face];
266
    }
267
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
  ObjectSticker retSticker(int face)
271 4c0a6d97 Leszek Koltunski
    {
272 97eb0852 Leszek Koltunski
    if( mStickers==null )
273
      {
274
      mStickers = new ObjectSticker[NUM_STICKERS];
275
276
      int[][] stickerDimensions = new int[][]
277
        {
278
         {1,1},  // dimensions of the faces of
279
         {2,1},  // the cuboids defined in mDimensions
280
         {3,1},
281
         {2,2}
282
        };
283
284
      for(int s=0; s<NUM_STICKERS; s++)
285
        {
286
        float X = stickerDimensions[s][0];
287
        float Y = stickerDimensions[s][1];
288
        float MAX = Math.max(X,Y);
289
        X /= (2*MAX);
290
        Y /= (2*MAX);
291
292
        float R = 0.10f / MAX;
293
        float S = 0.08f / MAX;
294
        float[] coords = { -X,-Y, +X,-Y, +X,+Y, -X,+Y};
295
        float[] radii = new float[] {R,R,R,R};
296
        mStickers[s] = new ObjectSticker(coords,null,radii,S);
297
        }
298
      }
299
300 abf36986 Leszek Koltunski
    return mStickers[face/NUM_FACE_COLORS];
301 4c0a6d97 Leszek Koltunski
    }
302
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
305 e6cf7283 Leszek Koltunski
  float[][] getCubitPositions(int size)
306 4c0a6d97 Leszek Koltunski
    {
307
    int numCubits = getNumCubits();
308 e6cf7283 Leszek Koltunski
    float[][] tmp = new float[numCubits][];
309 4c0a6d97 Leszek Koltunski
310
    for(int cubit=0; cubit<numCubits; cubit++)
311
      {
312 e6cf7283 Leszek Koltunski
      tmp[cubit] = getCubitPosition(cubit);
313 4c0a6d97 Leszek Koltunski
      }
314
315
    return tmp;
316
    }
317
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
320
  Static4D[] getQuats()
321
    {
322 97eb0852 Leszek Koltunski
    if( mQuats==null ) initializeQuats();
323
    return mQuats;
324 4c0a6d97 Leszek Koltunski
    }
325
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
328
  boolean shouldResetTextureMaps()
329
    {
330
    return false;
331
    }
332
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
335 abf36986 Leszek Koltunski
  int getNumFaceColors()
336 4c0a6d97 Leszek Koltunski
    {
337
    return FACE_COLORS.length;
338
    }
339
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
342 e6734aa9 Leszek Koltunski
  float[][] getCuts(int numLayers)
343 4c0a6d97 Leszek Koltunski
    {
344 e6734aa9 Leszek Koltunski
    float[][] cuts = new float[3][numLayers-1];
345 4c0a6d97 Leszek Koltunski
346
    for(int i=0; i<numLayers-1; i++)
347
      {
348 e6734aa9 Leszek Koltunski
      float cut = (2-numLayers)*0.5f + i;
349
      cuts[0][i] = cut;
350
      cuts[1][i] = cut;
351
      cuts[2][i] = cut;
352 4c0a6d97 Leszek Koltunski
      }
353
354
    return cuts;
355
    }
356
357 169219a7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359
  int getSolvedFunctionIndex()
360
    {
361
    return 0;
362
    }
363
364 4c0a6d97 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
365
366
  int getNumStickerTypes(int numLayers)
367
    {
368 538ee7a6 Leszek Koltunski
    return NUM_STICKERS;
369 4c0a6d97 Leszek Koltunski
    }
370
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
373
  int getNumCubitFaces()
374
    {
375
    return FACE_COLORS.length;
376
    }
377
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379
380
  float getScreenRatio()
381
    {
382
    return 0.5f;
383
    }
384
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386
387 538ee7a6 Leszek Koltunski
  private int retStickerIndex(int horzSize, int vertSize)
388 4c0a6d97 Leszek Koltunski
    {
389 538ee7a6 Leszek Koltunski
    switch(horzSize)
390 4c0a6d97 Leszek Koltunski
      {
391 538ee7a6 Leszek Koltunski
      case 1: return 0;
392
      case 2: return vertSize==1 ? 1:3;
393
      case 3: return 2;
394 4c0a6d97 Leszek Koltunski
      }
395
396 538ee7a6 Leszek Koltunski
    return 0;
397
    }
398 4c0a6d97 Leszek Koltunski
399 538ee7a6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
400
401
  private int getStickerIndex(int cubitface, int[] dim)
402
    {
403 4c0a6d97 Leszek Koltunski
    switch(cubitface)
404
      {
405 538ee7a6 Leszek Koltunski
      case 0: case 1: return retStickerIndex(dim[2],dim[1]);
406
      case 2: case 3: return retStickerIndex(dim[0],dim[2]);
407
      case 4: case 5: return retStickerIndex(dim[0],dim[1]);
408 4c0a6d97 Leszek Koltunski
      }
409
410 538ee7a6 Leszek Koltunski
    return 0;
411
    }
412
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
415
  int getFaceColor(int cubit, int cubitface, int numLayers)
416
    {
417 97eb0852 Leszek Koltunski
    if( mFaceMap==null )
418
      {
419
      // cubitface=2 when rotated by quatIndex=1 gets moved to position mFaceMap[2][1]
420
      mFaceMap = new int[][]
421
          {
422
              {0,0,5,2,4,2},
423
              {1,1,4,3,5,3},
424
              {2,4,2,1,1,4},
425
              {3,5,3,0,0,5},
426
              {4,3,0,4,3,0},
427
              {5,2,1,5,2,1}
428
          };
429
      }
430
431
    if( mAxisMap==null )
432
      {
433
      // axis=1 when rotated by quatIndex=2 gets moved to axis mAxisMap[1][2]
434
      mAxisMap = new int[][] { {0,0,2,1,2,1}, {1,2,1,0,0,2}, {2,1,0,2,1,0} };
435
      }
436
437 f10a88a8 Leszek Koltunski
    int variant      = getCubitVariant(cubit,numLayers);
438 538ee7a6 Leszek Koltunski
    int[] dim        = mDimensions[variant];
439
    float[] pos      = getCubitPosition(cubit);
440
    int stickerIndex = getStickerIndex(cubitface,dim);
441
    int quatIndex    = getQuatIndex(cubit);
442
    int face         = mFaceMap[cubitface][quatIndex];
443
    int multiplier   = (face%2)==0 ? 1:-1;
444
    int posIndex     = face/2;
445
    int dimIndex     = mAxisMap[posIndex][quatIndex];
446 92ec91b9 Leszek Koltunski
447
    float position = 0.0f;
448
    int len = pos.length/3;
449
    for(int i=0; i<len; i++) position += pos[3*i+posIndex];
450
    position /= len;
451
452
    boolean reaches  = multiplier*position + dim[dimIndex]*0.5f > (numLayers-1)*0.5f;
453 538ee7a6 Leszek Koltunski
454 abf36986 Leszek Koltunski
    return reaches ? stickerIndex*NUM_FACE_COLORS + face : NUM_TEXTURES;
455 4c0a6d97 Leszek Koltunski
    }
456
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458
459
  float returnMultiplier()
460
    {
461
    return getNumLayers();
462
    }
463
464
///////////////////////////////////////////////////////////////////////////////////////////////////
465
// PUBLIC API
466
467
  public Static3D[] getRotationAxis()
468
    {
469
    return ROT_AXIS;
470
    }
471
472 e9a87113 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
473
474
  public Movement getMovement()
475
    {
476
    if( mMovement==null ) mMovement = new MovementCube();
477
    return mMovement;
478
    }
479
480 4c0a6d97 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
481
482 925ed78f Leszek Koltunski
  public int[] getBasicAngle()
483 4c0a6d97 Leszek Koltunski
    {
484 97eb0852 Leszek Koltunski
    if( mBasicAngle ==null ) mBasicAngle = new int[] { 4,4,4 };
485
    return mBasicAngle;
486 4c0a6d97 Leszek Koltunski
    }
487
}