Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyJing.java @ f074c272

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

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

    
37
import java.util.Random;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class TwistyJing extends TwistyObject
42
{
43
  static final float F = 0.24f;  // length of the edge of the corner cubit divided by
44
                                 // the length of the edge of the whole tetrahedron.
45
                                 // keep < 0.25.
46

    
47
  static final Static3D[] ROT_AXIS = new Static3D[]
48
         {
49
           new Static3D(     0,-SQ3/3,-SQ6/3),
50
           new Static3D(     0,-SQ3/3,+SQ6/3),
51
           new Static3D(+SQ6/3,+SQ3/3,     0),
52
           new Static3D(-SQ6/3,+SQ3/3,     0),
53
         };
54

    
55
  private static final int[] BASIC_ANGLE = new int[] { 3,3,3,3 };
56

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

    
63
  private static final int[] ROT_QUAT = {0,1,2,7,0,2,7,6,3,10,0,1,3,5};
64

    
65
  // computed with res/raw/compute_quats.c
66
  private static final Static4D[] QUATS = new Static4D[]
67
         {
68
           new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
69
           new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
70
           new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
71
           new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
72
           new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
73
           new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
74
           new Static4D( SQ2/2,   0.5f,   0.0f, -0.5f),
75
           new Static4D(-SQ2/2,   0.5f,   0.0f, -0.5f),
76
           new Static4D(  0.0f,  -0.5f, -SQ2/2, -0.5f),
77
           new Static4D(  0.0f,  -0.5f,  SQ2/2, -0.5f),
78
           new Static4D( SQ2/2,   0.0f,  SQ2/2,  0.0f),
79
           new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
80
         };
81

    
82
  static final float[][] CENTERS = new float[][]
83
         {
84
           { 0.000f, -SQ2/4, 0.500f },
85
           { 0.000f, -SQ2/4,-0.500f },
86
           {-0.500f,  SQ2/4, 0.000f },
87
           { 0.500f,  SQ2/4, 0.000f },
88

    
89
           { 0.000f, -SQ2/4, 0.000f },
90
           {-0.250f, 0.000f, 0.250f },
91
           { 0.250f, 0.000f, 0.250f },
92
           {-0.250f, 0.000f,-0.250f },
93
           { 0.250f, 0.000f,-0.250f },
94
           { 0.000f,  SQ2/4, 0.000f },
95

    
96
           { 0.000f, SQ2/12, 1.0f/6 },
97
           { 0.000f, SQ2/12,-1.0f/6 },
98
           {-1.0f/6,-SQ2/12, 0.000f },
99
           { 1.0f/6,-SQ2/12, 0.000f },
100
         };
101

    
102
  // Colors of the faces of cubits.
103
  // GREEN 0 YELLOW 1 BLUE  2 RED 3
104
  // GREEN 4 YELLOW 5 BLUE  6 RED 7
105
  // GREEN 8 YELLOW 9 BLUE 10 RED 11
106
  private static final int[][] mFaceMap = new int[][]
107
         {
108
           {  0,  3,  2, 12, 12, 12 },
109
           {  1,  2,  3, 12, 12, 12 },
110
           {  1,  0,  2, 12, 12, 12 },
111
           {  1,  3,  0, 12, 12, 12 },
112

    
113
           {  7,  6, 12, 12, 12, 12 },
114
           {  4,  6, 12, 12, 12, 12 },
115
           {  7,  4, 12, 12, 12, 12 },
116
           {  5,  6, 12, 12, 12, 12 },
117
           {  7,  5, 12, 12, 12, 12 },
118
           {  4,  5, 12, 12, 12, 12 },
119

    
120
           {  8, 12, 12, 12, 12, 12 },
121
           {  9, 12, 12, 12, 12, 12 },
122
           { 10, 12, 12, 12, 12, 12 },
123
           { 11, 12, 12, 12, 12, 12 },
124
         };
125

    
126
  private static final float X = F/2;
127
  private static final float Y = F*SQ2/2;
128
  private static final float Z =-F/2;
129
  private static final float L = (1-3*F);
130
  private static final float X2= L/2;
131
  private static final float Y2= L*SQ2/2;
132
  private static final float Z2=-L/2;
133
  private static final float D = F/L;
134

    
135
  private static final double[][] VERTICES_CORNER = new double[][]
136
          {
137
             { 0.0, 0.0, 0.0 },
138
             {   X,   Y,   Z },
139
             { 0.0, 2*Y, 2*Z },
140
             {  -X,   Y,   Z },
141
             { 0.0, 0.0,    -F },
142
             {   X,   Y,   Z-F },
143
             { 0.0, 2*Y, 2*Z-F },
144
             {  -X,   Y,   Z-F },
145
          };
146

    
147
  private static final int[][] VERT_INDEXES_CORNER = new int[][]
148
          {
149
             {0,1,2,3},
150
             {1,0,4,5},
151
             {7,4,0,3},
152
             {1,5,6,2},
153
             {7,3,2,6},
154
             {4,7,6,5}
155
          };
156

    
157
  private static final double[][] VERTICES_EDGE = new double[][]
158
          {
159
             { 0.0, 0.0,     0.5-F },
160
             {   X,   Y,   Z+0.5-F },
161
             { 0.0, 2*Y, 2*Z+0.5-F },
162
             {  -X,   Y,   Z+0.5-F },
163
             { 0.0, 0.0,    -0.5+F },
164
             {   X,   Y,  -Z-0.5+F },
165
             { 0.0, 2*Y,-2*Z-0.5+F },
166
             {  -X,   Y,  -Z-0.5+F },
167
          };
168

    
169
  private static final int[][] VERT_INDEXES_EDGE = new int[][]
170
          {
171
             {0,4,5,1},
172
             {3,7,4,0},
173
             {0,1,2,3},
174
             {4,7,6,5},
175
             {1,5,6,2},
176
             {2,6,7,3}
177
          };
178

    
179
  private static final double[][] VERTICES_FACE = new double[][]
180
          {
181
             {    0.0,     -2*Y2/3,   -2*Z2/3 },
182
             {      X2,       Y2/3,      Z2/3 },
183
             {     -X2,       Y2/3,      Z2/3 },
184
             {    0.0,     -2*Y2/3,-2*Z2/3+2*D*Z2 },
185
             {  X2-D*X2, Y2/3-D*Y2, Z2/3+D*Z2 },
186
             { -X2+D*X2, Y2/3-D*Y2, Z2/3+D*Z2 },
187
          };
188

    
189
  private static final int[][] VERT_INDEXES_FACE = new int[][]
190
          {
191
             {0,1,2},
192
             {3,5,4},
193
             {0,3,4,1},
194
             {5,3,0,2},
195
             {4,5,2,1}
196
          };
197

    
198
  private static final float[][] STICKERS = new float[][]
199
          {
200
             { 0.0f, -0.5f, 0.28867516f, 0.0f, 0.0f, 0.5f, -0.28867516f, 0.0f },
201
             { -0.5f, 0.11983269f, 0.27964598f, -0.43146032f, 0.3200817f, 0.007388768f, -0.09972769f, 0.30423886f },
202
             { 0.0f, -0.5f, 0.43301272f, 0.25f, -0.43301272f, 0.25f },
203
          };
204

    
205
  private static MeshBase[] mMeshes;
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  TwistyJing(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
210
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
211
    {
212
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.JING, res, scrWidth);
213
    }
214

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

    
217
  float[][] getCubitPositions(int size)
218
    {
219
    return CENTERS;
220
    }
221

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

    
224
  Static4D[] getQuats()
225
    {
226
    return QUATS;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  int getNumFaces()
232
    {
233
    return FACE_COLORS.length;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  int getNumStickerTypes(int numLayers)
239
    {
240
    return STICKERS.length;
241
    }
242

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

    
245
  float[][] getCuts(int size)
246
    {
247
    float[] cut = { (F-0.25f)*(SQ6/3) };
248
    return new float[][] { cut,cut,cut,cut };
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  int getNumCubitFaces()
254
    {
255
    return 6;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  float getScreenRatio()
261
    {
262
    return 2*TwistyPyraminx.SCREEN_RATIO;
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  boolean shouldResetTextureMaps()
268
    {
269
    return false;
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  int getFaceColor(int cubit, int cubitface, int size)
275
    {
276
    return mFaceMap[cubit][cubitface];
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  MeshBase createCubitMesh(int cubit, int numLayers)
282
    {
283
    if( mMeshes==null )
284
      {
285
      FactoryCubit factory = FactoryCubit.getInstance();
286
      factory.clear();
287
      mMeshes = new MeshBase[3];
288
      }
289

    
290
    MeshBase mesh;
291

    
292
    if( cubit<4 )
293
      {
294
      if( mMeshes[0]==null )
295
        {
296
        float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
297
        int[] bandIndexes   = new int[] { 0,0,0,1,1,1 };
298
        float[][] corners   = new float[][] { {0.08f,0.20f*F},{0.07f,0.20f*F} };
299
        int[] cornerIndexes = new int[] { 0,1,1,-1,1,-1,-1,-1 };
300
        float[][] centers   = new float[][] { { 0.0f, F*SQ2/2, -F} };
301
        int[] centerIndexes = new int[] { 0,0,0,-1,0,-1,-1,-1 };
302

    
303
        FactoryCubit factory = FactoryCubit.getInstance();
304

    
305
        factory.createNewFaceTransform(VERTICES_CORNER,VERT_INDEXES_CORNER);
306
        mMeshes[0] = factory.createRoundedSolid(VERTICES_CORNER, VERT_INDEXES_CORNER,
307
                                                bands, bandIndexes,
308
                                                corners, cornerIndexes,
309
                                                centers, centerIndexes,
310
                                                getNumCubitFaces() );
311
        }
312
      mesh = mMeshes[0].copy(true);
313
      }
314
    else if( cubit<10 )
315
      {
316
      if( mMeshes[1]==null )
317
        {
318
        float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
319
        int[] bandIndexes   = new int[] { 0,0,1,1,1,1 };
320
        float[][] corners   = new float[][] { {0.07f,0.20f*F} };
321
        int[] cornerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
322
        float[][] centers   = new float[][] { { 0, F*SQ2/2, 0 } };
323
        int[] centerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
324

    
325
        FactoryCubit factory = FactoryCubit.getInstance();
326

    
327
        factory.createNewFaceTransform(VERTICES_EDGE,VERT_INDEXES_EDGE);
328
        mMeshes[1] = factory.createRoundedSolid(VERTICES_EDGE, VERT_INDEXES_EDGE,
329
                                                bands, bandIndexes,
330
                                                corners, cornerIndexes,
331
                                                centers, centerIndexes,
332
                                                getNumCubitFaces() );
333

    
334
        factory.printStickerCoords();
335
        }
336
      mesh = mMeshes[1].copy(true);
337
      }
338
    else
339
      {
340
      if( mMeshes[2]==null )
341
        {
342
        float[][] bands     = new float[][] { {0.020f,35,0.20f*(1-3*F),0.6f*(1-3*F),5,1,1},
343
                                              {0.001f,35,0.05f*(1-3*F),0.1f*(1-3*F),5,1,1} };
344
        int[] bandIndexes   = new int[] { 0,1,1,1,1,1 };
345
        float[][] corners   = new float[][] { {0.04f,0.15f} };
346
        int[] cornerIndexes = new int[] { 0,0,0,-1,-1,-1 };
347
        float[][] centers   = new float[][] { { 0, -2*Y/3, 4*Z/3 } };
348
        int[] centerIndexes = new int[] { 0,0,0,-1,-1,-1 };
349

    
350
        FactoryCubit factory = FactoryCubit.getInstance();
351

    
352
        factory.createNewFaceTransform(VERTICES_FACE,VERT_INDEXES_FACE);
353
        mMeshes[2] = factory.createRoundedSolid(VERTICES_FACE, VERT_INDEXES_FACE,
354
                                                bands, bandIndexes,
355
                                                corners, cornerIndexes,
356
                                                centers, centerIndexes,
357
                                                getNumCubitFaces() );
358

    
359
        factory.printStickerCoords();
360
        }
361
      mesh = mMeshes[2].copy(true);
362
      }
363

    
364
    int q = ROT_QUAT[cubit];
365
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( QUATS[q], new Static3D(0,0,0) );
366
    mesh.apply(quat,0xffffffff,0);
367

    
368
    return mesh;
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372

    
373
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
374
    {
375
    int COLORS = FACE_COLORS.length;
376
    int stickerType = face/COLORS;
377
    float R,S;
378

    
379
    switch(stickerType)
380
      {
381
      case 0:  R = 0.05f; S = 0.06f; break;
382
      case 1:  R = 0.03f; S = 0.03f; break;
383
      default: R = 0.05f; S = 0.05f; break;
384
      }
385

    
386
    FactorySticker factory = FactorySticker.getInstance();
387
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
// SQ6/3 = height of the tetrahedron
392

    
393
  float returnMultiplier()
394
    {
395
    return getNumLayers()/(SQ6/3);
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399
// PUBLIC API
400

    
401
  public Static3D[] getRotationAxis()
402
    {
403
    return ROT_AXIS;
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  public int[] getBasicAngle()
409
    {
410
    return BASIC_ANGLE;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
416
    {
417
    if( curr==0 )
418
      {
419
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
420
      }
421
    else
422
      {
423
      int newVector = rnd.nextInt(NUM_AXIS-1);
424
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
425
      }
426

    
427
    scramble[curr][1] = rnd.nextInt(2);
428

    
429
    switch( rnd.nextInt(2) )
430
      {
431
      case 0: scramble[curr][2] = -1; break;
432
      case 1: scramble[curr][2] =  1; break;
433
      }
434
    }
435

    
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
// JingPyraminx is solved iff
438
// a) all of its corner and edge cubits are rotated with the same quat
439
// b) its 4 face cubits might also be rotated along the axis perpendicular to the face.
440
//
441
// So:
442
// [10] might be extra QUAT[4] or QUAT[8]
443
// [11] might be extra QUAT[5] or QUAT[9]
444
// [12] might be extra QUAT[2] or QUAT[6]
445
// [13] might be extra QUAT[3] or QUAT[7]
446

    
447
  public boolean isSolved()
448
    {
449
    int q1, q = CUBITS[0].mQuatIndex;
450

    
451
    if( CUBITS[1].mQuatIndex != q ) return false;
452
    if( CUBITS[2].mQuatIndex != q ) return false;
453
    if( CUBITS[3].mQuatIndex != q ) return false;
454
    if( CUBITS[4].mQuatIndex != q ) return false;
455
    if( CUBITS[5].mQuatIndex != q ) return false;
456
    if( CUBITS[6].mQuatIndex != q ) return false;
457
    if( CUBITS[7].mQuatIndex != q ) return false;
458
    if( CUBITS[8].mQuatIndex != q ) return false;
459
    if( CUBITS[9].mQuatIndex != q ) return false;
460

    
461
    q1 = CUBITS[10].mQuatIndex;
462
    if( q1!=q && q1!=mulQuat(q,4) && q1!=mulQuat(q,8) ) return false;
463
    q1 = CUBITS[11].mQuatIndex;
464
    if( q1!=q && q1!=mulQuat(q,5) && q1!=mulQuat(q,9) ) return false;
465
    q1 = CUBITS[12].mQuatIndex;
466
    if( q1!=q && q1!=mulQuat(q,2) && q1!=mulQuat(q,6) ) return false;
467
    q1 = CUBITS[13].mQuatIndex;
468
    if( q1!=q && q1!=mulQuat(q,3) && q1!=mulQuat(q,7) ) return false;
469

    
470
    return true;
471
    }
472

    
473
////////////////////////////////////////////////////////////////////////
474
// only needed for solvers - there are no JingPyraminx solvers ATM)
475

    
476
  public String retObjectString()
477
    {
478
    return "";
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  public int getObjectName(int numLayers)
484
    {
485
    return R.string.jing;
486
    }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
  public int getInventor(int numLayers)
491
    {
492
    return R.string.jing_inventor;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
  public int getComplexity(int numLayers)
498
    {
499
    return 4;
500
    }
501
}
(29-29/41)