Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyRex.java @ 0b8ffe1a

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.library.effect.MatrixEffectQuaternion;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.mesh.MeshBase;
30
import org.distorted.library.mesh.MeshSquare;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.main.R;
34
import org.distorted.main.RubikSurfaceView;
35

    
36
import java.util.Random;
37

    
38
import static org.distorted.effects.scramble.ScrambleEffect.START_AXIS;
39
import static org.distorted.objects.FactoryCubit.REX_D;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class TwistyRex extends TwistyObject
44
{
45
  private static final int FACES_PER_CUBIT =4;
46

    
47
  // the four rotation axis of a RubikRex. Must be normalized.
48
  static final Static3D[] ROT_AXIS = new Static3D[]
49
         {
50
           new Static3D(+SQ3/3,+SQ3/3,+SQ3/3),
51
           new Static3D(+SQ3/3,+SQ3/3,-SQ3/3),
52
           new Static3D(+SQ3/3,-SQ3/3,+SQ3/3),
53
           new Static3D(+SQ3/3,-SQ3/3,-SQ3/3)
54
         };
55

    
56
  private static final int[] FACE_COLORS = new int[]
57
         {
58
           COLOR_YELLOW, COLOR_WHITE,
59
           COLOR_BLUE  , COLOR_GREEN,
60
           COLOR_RED   , COLOR_BROWN
61
         };
62

    
63
  // All legal rotation quats of a RubikRex
64
  private static final Static4D[] QUATS = new Static4D[]
65
         {
66
           new Static4D(  0.0f,  0.0f,  0.0f,  1.0f ),
67
           new Static4D(  1.0f,  0.0f,  0.0f,  0.0f ),
68
           new Static4D(  0.0f,  1.0f,  0.0f,  0.0f ),
69
           new Static4D(  0.0f,  0.0f,  1.0f,  0.0f ),
70

    
71
           new Static4D(  0.5f,  0.5f,  0.5f,  0.5f ),
72
           new Static4D(  0.5f,  0.5f,  0.5f, -0.5f ),
73
           new Static4D(  0.5f,  0.5f, -0.5f,  0.5f ),
74
           new Static4D(  0.5f,  0.5f, -0.5f, -0.5f ),
75
           new Static4D(  0.5f, -0.5f,  0.5f,  0.5f ),
76
           new Static4D(  0.5f, -0.5f,  0.5f, -0.5f ),
77
           new Static4D(  0.5f, -0.5f, -0.5f,  0.5f ),
78
           new Static4D(  0.5f, -0.5f, -0.5f, -0.5f )
79
         };
80

    
81
  private static final int[][] mFaceMap =
82
         {
83
           {  0, 18,18,18 },
84
           {  0, 18,18,18 },
85
           {  0, 18,18,18 },
86
           {  0, 18,18,18 },
87
           {  1, 18,18,18 },
88
           {  1, 18,18,18 },
89
           {  1, 18,18,18 },
90
           {  1, 18,18,18 },
91
           {  2, 18,18,18 },
92
           {  2, 18,18,18 },
93
           {  2, 18,18,18 },
94
           {  2, 18,18,18 },
95
           {  3, 18,18,18 },
96
           {  3, 18,18,18 },
97
           {  3, 18,18,18 },
98
           {  3, 18,18,18 },
99
           {  4, 18,18,18 },
100
           {  4, 18,18,18 },
101
           {  4, 18,18,18 },
102
           {  4, 18,18,18 },
103
           {  5, 18,18,18 },
104
           {  5, 18,18,18 },
105
           {  5, 18,18,18 },
106
           {  5, 18,18,18 },
107

    
108
           {  6, 18,18,18 },
109
           {  7, 18,18,18 },
110
           {  8, 18,18,18 },
111
           {  9, 18,18,18 },
112
           { 10, 18,18,18 },
113
           { 11, 18,18,18 },
114

    
115
           { 16,14, 18,18 },
116
           { 16,12, 18,18 },
117
           { 16,15, 18,18 },
118
           { 16,13, 18,18 },
119
           { 12,14, 18,18 },
120
           { 15,12, 18,18 },
121
           { 15,13, 18,18 },
122
           { 13,14, 18,18 },
123
           { 14,17, 18,18 },
124
           { 12,17, 18,18 },
125
           { 17,15, 18,18 },
126
           { 13,17, 18,18 },
127
         };
128

    
129
  private static MeshBase mCornerMesh, mFaceMesh, mEdgeMesh;
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  TwistyRex(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
134
            DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
135
    {
136
    super(size, size, 60, quat, texture, mesh, effects, moves, ObjectList.REX, res, scrWidth);
137
    }
138

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

    
141
  float getScreenRatio()
142
    {
143
    return 1.5f;
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  Static4D[] getQuats()
149
    {
150
    return QUATS;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  int getNumFaces()
156
    {
157
    return FACE_COLORS.length;
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  boolean shouldResetTextureMaps()
163
    {
164
    return false;
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  int getNumStickerTypes()
170
    {
171
    return 3;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  float[] getCuts(int numLayers)
177
    {
178
    float C = SQ3*0.15f;   // bit less than 1/6 of the length of the main diagonal
179

    
180
    return new float[] {-C,+C};
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  int getNumCubitFaces()
186
    {
187
    return FACES_PER_CUBIT;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  Static3D[] getCubitPositions(int numLayers)
193
    {
194
    final float DIST = 0.50f;
195
    final float DIST2= SQ3/6*(1.0f-2*REX_D);
196

    
197
    final Static3D[] CENTERS = new Static3D[42];
198

    
199
    CENTERS[ 0] = new Static3D( +DIST , +DIST2, +DIST2);
200
    CENTERS[ 1] = new Static3D( +DIST , +DIST2, -DIST2);
201
    CENTERS[ 2] = new Static3D( +DIST , -DIST2, -DIST2);
202
    CENTERS[ 3] = new Static3D( +DIST , -DIST2, +DIST2);
203
    CENTERS[ 4] = new Static3D( -DIST , +DIST2, +DIST2);
204
    CENTERS[ 5] = new Static3D( -DIST , +DIST2, -DIST2);
205
    CENTERS[ 6] = new Static3D( -DIST , -DIST2, -DIST2);
206
    CENTERS[ 7] = new Static3D( -DIST , -DIST2, +DIST2);
207
    CENTERS[ 8] = new Static3D( +DIST2, +DIST , +DIST2);
208
    CENTERS[ 9] = new Static3D( +DIST2, +DIST , -DIST2);
209
    CENTERS[10] = new Static3D( -DIST2, +DIST , -DIST2);
210
    CENTERS[11] = new Static3D( -DIST2, +DIST , +DIST2);
211
    CENTERS[12] = new Static3D( +DIST2, -DIST , +DIST2);
212
    CENTERS[13] = new Static3D( +DIST2, -DIST , -DIST2);
213
    CENTERS[14] = new Static3D( -DIST2, -DIST , -DIST2);
214
    CENTERS[15] = new Static3D( -DIST2, -DIST , +DIST2);
215
    CENTERS[16] = new Static3D( +DIST2, +DIST2, +DIST );
216
    CENTERS[17] = new Static3D( +DIST2, -DIST2, +DIST );
217
    CENTERS[18] = new Static3D( -DIST2, -DIST2, +DIST );
218
    CENTERS[19] = new Static3D( -DIST2, +DIST2, +DIST );
219
    CENTERS[20] = new Static3D( +DIST2, +DIST2, -DIST );
220
    CENTERS[21] = new Static3D( +DIST2, -DIST2, -DIST );
221
    CENTERS[22] = new Static3D( -DIST2, -DIST2, -DIST );
222
    CENTERS[23] = new Static3D( -DIST2, +DIST2, -DIST );
223

    
224
    CENTERS[24] = new Static3D( +DIST , +0.00f, +0.00f);
225
    CENTERS[25] = new Static3D( -DIST , +0.00f, +0.00f);
226
    CENTERS[26] = new Static3D( +0.00f, +DIST , +0.00f);
227
    CENTERS[27] = new Static3D( +0.00f, -DIST , +0.00f);
228
    CENTERS[28] = new Static3D( +0.00f, +0.00f, +DIST );
229
    CENTERS[29] = new Static3D( +0.00f, +0.00f, -DIST );
230

    
231
    CENTERS[30] = new Static3D( +0.00f, +DIST , +DIST );
232
    CENTERS[31] = new Static3D( +DIST , +0.00f, +DIST );
233
    CENTERS[32] = new Static3D( +0.00f, -DIST , +DIST );
234
    CENTERS[33] = new Static3D( -DIST , +0.00f, +DIST );
235
    CENTERS[34] = new Static3D( +DIST , +DIST , +0.00f);
236
    CENTERS[35] = new Static3D( +DIST , -DIST , +0.00f);
237
    CENTERS[36] = new Static3D( -DIST , -DIST , +0.00f);
238
    CENTERS[37] = new Static3D( -DIST , +DIST , +0.00f);
239
    CENTERS[38] = new Static3D( +0.00f, +DIST , -DIST );
240
    CENTERS[39] = new Static3D( +DIST , +0.00f, -DIST );
241
    CENTERS[40] = new Static3D( +0.00f, -DIST , -DIST );
242
    CENTERS[41] = new Static3D( -DIST , +0.00f, -DIST );
243

    
244
    return CENTERS;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
// 1  +90 ( 1, 0, 0) = Static4D(+SQ2/2,     0,     0, SQ2/2);
249
// 2  -90 ( 1, 0, 0) = Static4D(-SQ2/2,     0,     0, SQ2/2);
250
// 3  +90 ( 0, 1, 0) = Static4D(     0,+SQ2/2,     0, SQ2/2);
251
// 4  -90 ( 0, 1, 0) = Static4D(     0,-SQ2/2,     0, SQ2/2);
252
// 5  +90 ( 0, 0, 1) = Static4D(     0,     0,+SQ2/2, SQ2/2);
253
// 6  -90 ( 0, 0, 1) = Static4D(     0,     0,-SQ2/2, SQ2/2);
254
// 7  180 ( 1, 0, 1) = Static4D(+SQ2/2,     0,+SQ2/2,     0);
255
// 8  180 (-1, 0, 1) = Static4D(-SQ2/2,     0,+SQ2/2,     0);
256
// 9  180 ( 0, 1, 1) = Static4D(     0,+SQ2/2,+SQ2/2,     0);
257
//10  180 ( 0,-1, 1) = Static4D(     0,-SQ2/2,+SQ2/2,     0);
258
//11  180 ( 1, 1, 0) = Static4D(+SQ2/2,+SQ2/2,     0,     0);
259
//12  180 ( 1,-1, 0) = Static4D(+SQ2/2,-SQ2/2,     0,     0);
260

    
261
  private Static4D getQuat(int cubit)
262
    {
263
    switch(cubit)
264
      {
265
      case  0: return new Static4D(+SQ2/2,     0,+SQ2/2,     0);
266
      case  1: return QUATS[5];
267
      case  2: return new Static4D(     0,-SQ2/2,     0, SQ2/2);
268
      case  3: return QUATS[8];
269
      case  4: return QUATS[6];
270
      case  5: return new Static4D(-SQ2/2,     0,+SQ2/2,     0);
271
      case  6: return QUATS[11];
272
      case  7: return new Static4D(     0,+SQ2/2,     0, SQ2/2);
273
      case  8: return new Static4D(+SQ2/2,     0,     0, SQ2/2);
274
      case  9: return QUATS[10];
275
      case 10: return new Static4D(     0,+SQ2/2,+SQ2/2,     0);
276
      case 11: return QUATS[4];
277
      case 12: return QUATS[9];
278
      case 13: return new Static4D(-SQ2/2,     0,     0, SQ2/2);
279
      case 14: return QUATS[7];
280
      case 15: return new Static4D(     0,-SQ2/2,+SQ2/2,     0);
281
      case 16: return new Static4D(     0,     0,-SQ2/2, SQ2/2);
282
      case 17: return QUATS[0];
283
      case 18: return new Static4D(     0,     0,+SQ2/2, SQ2/2);
284
      case 19: return QUATS[3];
285
      case 20: return QUATS[1];
286
      case 21: return new Static4D(+SQ2/2,-SQ2/2,     0,     0);
287
      case 22: return QUATS[2];
288
      case 23: return new Static4D(+SQ2/2,+SQ2/2,     0,     0);
289

    
290
      case 24: return new Static4D(     0,-SQ2/2,     0, SQ2/2);
291
      case 25: return new Static4D(     0,+SQ2/2,     0, SQ2/2);
292
      case 26: return new Static4D(+SQ2/2,     0,     0, SQ2/2);
293
      case 27: return new Static4D(-SQ2/2,     0,     0, SQ2/2);
294
      case 28: return QUATS[0];
295
      case 29: return QUATS[1];
296

    
297
      case 30: return QUATS[0];
298
      case 31: return new Static4D(     0,     0,+SQ2/2, SQ2/2);
299
      case 32: return QUATS[3];
300
      case 33: return new Static4D(     0,     0,-SQ2/2, SQ2/2);
301
      case 34: return new Static4D(     0,-SQ2/2,     0, SQ2/2);
302
      case 35: return QUATS[7];
303
      case 36: return QUATS[9];
304
      case 37: return new Static4D(     0,+SQ2/2,     0, SQ2/2);
305
      case 38: return new Static4D(+SQ2/2,     0,     0, SQ2/2);
306
      case 39: return QUATS[8];
307
      case 40: return QUATS[1];
308
      case 41: return QUATS[6];
309
      }
310

    
311
    return QUATS[0];
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  MeshBase createCubitMesh(int cubit)
317
    {
318
    MeshBase mesh;
319

    
320
    if( cubit<24 )
321
      {
322
      if( mCornerMesh==null ) mCornerMesh = FactoryCubit.getInstance().createRexCornerMesh();
323
      mesh = mCornerMesh.copy(true);
324
      }
325
    else if( cubit<30 )
326
      {
327
      if( mFaceMesh==null ) mFaceMesh = FactoryCubit.getInstance().createRexFaceMesh();
328
      mesh = mFaceMesh.copy(true);
329
      }
330
    else
331
      {
332
      if( mEdgeMesh==null ) mEdgeMesh = FactoryCubit.getInstance().createRexEdgeMesh();
333
      mesh = mEdgeMesh.copy(true);
334
      }
335

    
336
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit), new Static3D(0,0,0) );
337
    mesh.apply(quat,0xffffffff,0);
338

    
339
    return mesh;
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

    
344
  int getFaceColor(int cubit, int cubitface, int numLayers)
345
    {
346
    return mFaceMap[cubit][cubitface];
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
352
    {
353
    int COLORS = FACE_COLORS.length;
354
    FactorySticker factory = FactorySticker.getInstance();
355
    float S  = 0.04f;
356
    float R1 = 0.12f;
357
    float R2 = 0.06f;
358

    
359
    if( face<COLORS )
360
      {
361
      factory.drawRexCornerSticker(canvas, paint, left, top, FACE_COLORS[face%COLORS], S, R2);
362
      }
363
    else if( face<2*COLORS )
364
      {
365
      factory.drawRexFaceSticker(canvas, paint, left, top, FACE_COLORS[face%COLORS], S, R1);
366
      }
367
    else
368
      {
369
      factory.drawRexEdgeSticker(canvas, paint, left, top, FACE_COLORS[face%COLORS], S, R1);
370
      }
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  float returnMultiplier()
376
    {
377
    return 2.0f;
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381

    
382
  float[] getRowChances()
383
    {
384
    return new float[] { 0.5f, 0.5f, 1.0f };
385
    }
386

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388
// PUBLIC API
389

    
390
  public Static3D[] getRotationAxis()
391
    {
392
    return ROT_AXIS;
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
  public int getBasicAngle()
398
    {
399
    return 3;
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  public int randomizeNewRotAxis(Random rnd, int oldRotAxis)
405
    {
406
    int numAxis = ROTATION_AXIS.length;
407

    
408
    if( oldRotAxis == START_AXIS )
409
      {
410
      return rnd.nextInt(numAxis);
411
      }
412
    else
413
      {
414
      int newVector = rnd.nextInt(numAxis-1);
415
      return (newVector>=oldRotAxis ? newVector+1 : newVector);
416
      }
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  public int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis)
422
    {
423
    float rowFloat = rnd.nextFloat();
424

    
425
    for(int row=0; row<mRowChances.length; row++)
426
      {
427
      if( rowFloat<=mRowChances[row] ) return row;
428
      }
429

    
430
    return 0;
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434
// remember about the double cover or unit quaternions!
435

    
436
  private int mulQuat(int q1, int q2)
437
    {
438
    Static4D result = RubikSurfaceView.quatMultiply(QUATS[q1],QUATS[q2]);
439

    
440
    float rX = result.get0();
441
    float rY = result.get1();
442
    float rZ = result.get2();
443
    float rW = result.get3();
444

    
445
    final float MAX_ERROR = 0.1f;
446
    float dX,dY,dZ,dW;
447

    
448
    for(int i=0; i<QUATS.length; i++)
449
      {
450
      dX = QUATS[i].get0() - rX;
451
      dY = QUATS[i].get1() - rY;
452
      dZ = QUATS[i].get2() - rZ;
453
      dW = QUATS[i].get3() - rW;
454

    
455
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
456
          dY<MAX_ERROR && dY>-MAX_ERROR &&
457
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
458
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
459

    
460
      dX = QUATS[i].get0() + rX;
461
      dY = QUATS[i].get1() + rY;
462
      dZ = QUATS[i].get2() + rZ;
463
      dW = QUATS[i].get3() + rW;
464

    
465
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
466
          dY<MAX_ERROR && dY>-MAX_ERROR &&
467
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
468
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
469
      }
470

    
471
    return -1;
472
    }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475
// The Rex is solved if and only if:
476
//
477
// TODO
478

    
479
  public boolean isSolved()
480
    {
481
    int q = CUBITS[0].mQuatIndex;
482

    
483
    for(int i=1; i<42; i++)
484
      {
485
      if( CUBITS[i].mQuatIndex != q) return false;
486
      }
487

    
488
    return true;
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492
// only needed for solvers - there are no Rex solvers ATM)
493

    
494
  public String retObjectString()
495
    {
496
    return "";
497
    }
498

    
499
///////////////////////////////////////////////////////////////////////////////////////////////////
500

    
501
  public int getObjectName(int numLayers)
502
    {
503
    return R.string.rex3;
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  public int getInventor(int numLayers)
509
    {
510
    return R.string.rex3_inventor;
511
    }
512

    
513
///////////////////////////////////////////////////////////////////////////////////////////////////
514

    
515
  public int getComplexity(int numLayers)
516
    {
517
    return 3;
518
    }
519
}
(25-25/26)