Project

General

Profile

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

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

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