Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyJing.java @ 9c06394a

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

    
24
import org.distorted.helpers.FactoryCubit;
25
import org.distorted.helpers.ObjectSticker;
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

    
35
import java.util.Random;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

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

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

    
53
  private static final int[] BASIC_ANGLE = new int[] { 3,3,3,3 };
54

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

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

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

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

    
87
           { 0.000f, -SQ2/4, 0.000f },
88
           {-0.250f, 0.000f, 0.250f },
89
           { 0.250f, 0.000f, 0.250f },
90
           {-0.250f, 0.000f,-0.250f },
91
           { 0.250f, 0.000f,-0.250f },
92
           { 0.000f,  SQ2/4, 0.000f },
93

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

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

    
111
           {  7,  6, 12, 12, 12, 12 },
112
           {  4,  6, 12, 12, 12, 12 },
113
           {  7,  4, 12, 12, 12, 12 },
114
           {  5,  6, 12, 12, 12, 12 },
115
           {  7,  5, 12, 12, 12, 12 },
116
           {  4,  5, 12, 12, 12, 12 },
117

    
118
           {  8, 12, 12, 12, 12, 12 },
119
           {  9, 12, 12, 12, 12, 12 },
120
           { 10, 12, 12, 12, 12, 12 },
121
           { 11, 12, 12, 12, 12, 12 },
122
         };
123

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

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

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

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

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

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

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

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

    
203
  private static MeshBase[] mMeshes;
204

    
205
  private static final ObjectSticker[] mStickers;
206

    
207
  static
208
    {
209
    mStickers = new ObjectSticker[STICKERS.length];
210
    final float R1 = 0.05f;
211
    final float R2 = 0.10f;
212
    final float R3 = 0.03f;
213
    final float[][] radii  = { { R1,R1,R1,R1 },{ R3,R3,R2,R2 },{ R1,R1,R1 } };
214
    final float[] strokes = { 0.06f, 0.03f, 0.05f };
215

    
216
    for(int s=0; s<STICKERS.length; s++)
217
      {
218
      mStickers[s] = new ObjectSticker(STICKERS[s],null,radii[s],strokes[s]);
219
      }
220
    }
221

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

    
224
  TwistyJing(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
225
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
226
    {
227
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.JING, res, scrWidth);
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  float[][] getCubitPositions(int size)
233
    {
234
    return CENTERS;
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  Static4D[] getQuats()
240
    {
241
    return QUATS;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  int getNumFaces()
247
    {
248
    return FACE_COLORS.length;
249
    }
250

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

    
253
  int getNumStickerTypes(int numLayers)
254
    {
255
    return STICKERS.length;
256
    }
257

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

    
260
  float[][] getCuts(int size)
261
    {
262
    float[] cut = { (F-0.25f)*(SQ6/3) };
263
    return new float[][] { cut,cut,cut,cut };
264
    }
265

    
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  int getNumCubitFaces()
269
    {
270
    return 6;
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  float getScreenRatio()
276
    {
277
    return 2*TwistyPyraminx.SCREEN_RATIO;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  boolean shouldResetTextureMaps()
283
    {
284
    return false;
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  int getFaceColor(int cubit, int cubitface, int size)
290
    {
291
    return mFaceMap[cubit][cubitface];
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295

    
296
  MeshBase createCubitMesh(int cubit, int numLayers)
297
    {
298
    if( mMeshes==null )
299
      {
300
      FactoryCubit factory = FactoryCubit.getInstance();
301
      factory.clear();
302
      mMeshes = new MeshBase[3];
303
      }
304

    
305
    MeshBase mesh;
306

    
307
    if( cubit<4 )
308
      {
309
      if( mMeshes[0]==null )
310
        {
311
        float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
312
        int[] bandIndexes   = new int[] { 0,0,0,1,1,1 };
313
        float[][] corners   = new float[][] { {0.08f,0.20f*F},{0.07f,0.20f*F} };
314
        int[] cornerIndexes = new int[] { 0,1,1,-1,1,-1,-1,-1 };
315
        float[][] centers   = new float[][] { { 0.0f, F*SQ2/2, -F} };
316
        int[] centerIndexes = new int[] { 0,0,0,-1,0,-1,-1,-1 };
317

    
318
        FactoryCubit factory = FactoryCubit.getInstance();
319

    
320
        factory.createNewFaceTransform(VERTICES_CORNER,VERT_INDEXES_CORNER);
321
        mMeshes[0] = factory.createRoundedSolid(VERTICES_CORNER, VERT_INDEXES_CORNER,
322
                                                bands, bandIndexes,
323
                                                corners, cornerIndexes,
324
                                                centers, centerIndexes,
325
                                                getNumCubitFaces() );
326
        }
327
      mesh = mMeshes[0].copy(true);
328
      }
329
    else if( cubit<10 )
330
      {
331
      if( mMeshes[1]==null )
332
        {
333
        float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
334
        int[] bandIndexes   = new int[] { 0,0,1,1,1,1 };
335
        float[][] corners   = new float[][] { {0.07f,0.20f*F} };
336
        int[] cornerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
337
        float[][] centers   = new float[][] { { 0, F*SQ2/2, 0 } };
338
        int[] centerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
339

    
340
        FactoryCubit factory = FactoryCubit.getInstance();
341

    
342
        factory.createNewFaceTransform(VERTICES_EDGE,VERT_INDEXES_EDGE);
343
        mMeshes[1] = factory.createRoundedSolid(VERTICES_EDGE, VERT_INDEXES_EDGE,
344
                                                bands, bandIndexes,
345
                                                corners, cornerIndexes,
346
                                                centers, centerIndexes,
347
                                                getNumCubitFaces() );
348

    
349
        factory.printStickerCoords();
350
        }
351
      mesh = mMeshes[1].copy(true);
352
      }
353
    else
354
      {
355
      if( mMeshes[2]==null )
356
        {
357
        float[][] bands     = new float[][] { {0.020f,35,0.20f*(1-3*F),0.6f*(1-3*F),5,1,1},
358
                                              {0.001f,35,0.05f*(1-3*F),0.1f*(1-3*F),5,1,1} };
359
        int[] bandIndexes   = new int[] { 0,1,1,1,1,1 };
360
        float[][] corners   = new float[][] { {0.04f,0.15f} };
361
        int[] cornerIndexes = new int[] { 0,0,0,-1,-1,-1 };
362
        float[][] centers   = new float[][] { { 0, -2*Y/3, 4*Z/3 } };
363
        int[] centerIndexes = new int[] { 0,0,0,-1,-1,-1 };
364

    
365
        FactoryCubit factory = FactoryCubit.getInstance();
366

    
367
        factory.createNewFaceTransform(VERTICES_FACE,VERT_INDEXES_FACE);
368
        mMeshes[2] = factory.createRoundedSolid(VERTICES_FACE, VERT_INDEXES_FACE,
369
                                                bands, bandIndexes,
370
                                                corners, cornerIndexes,
371
                                                centers, centerIndexes,
372
                                                getNumCubitFaces() );
373

    
374
        factory.printStickerCoords();
375
        }
376
      mesh = mMeshes[2].copy(true);
377
      }
378

    
379
    int q = ROT_QUAT[cubit];
380
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( QUATS[q], new Static3D(0,0,0) );
381
    mesh.apply(quat,0xffffffff,0);
382

    
383
    return mesh;
384
    }
385

    
386
///////////////////////////////////////////////////////////////////////////////////////////////////
387

    
388
  int getColor(int face)
389
    {
390
    return FACE_COLORS[face];
391
    }
392

    
393
///////////////////////////////////////////////////////////////////////////////////////////////////
394

    
395
  ObjectSticker retSticker(int face)
396
    {
397
    return mStickers[face/NUM_FACES];
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
// SQ6/3 = height of the tetrahedron
402

    
403
  float returnMultiplier()
404
    {
405
    return getNumLayers()/(SQ6/3);
406
    }
407

    
408
///////////////////////////////////////////////////////////////////////////////////////////////////
409
// PUBLIC API
410

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

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public int[] getBasicAngle()
419
    {
420
    return BASIC_ANGLE;
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
426
    {
427
    if( curr==0 )
428
      {
429
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
430
      }
431
    else
432
      {
433
      int newVector = rnd.nextInt(NUM_AXIS-1);
434
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
435
      }
436

    
437
    scramble[curr][1] = rnd.nextInt(2);
438

    
439
    switch( rnd.nextInt(2) )
440
      {
441
      case 0: scramble[curr][2] = -1; break;
442
      case 1: scramble[curr][2] =  1; break;
443
      }
444
    }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
// JingPyraminx is solved iff
448
// a) all of its corner and edge cubits are rotated with the same quat
449
// b) its 4 face cubits might also be rotated along the axis perpendicular to the face.
450
//
451
// So:
452
// [10] might be extra QUAT[4] or QUAT[8]
453
// [11] might be extra QUAT[5] or QUAT[9]
454
// [12] might be extra QUAT[2] or QUAT[6]
455
// [13] might be extra QUAT[3] or QUAT[7]
456

    
457
  public boolean isSolved()
458
    {
459
    int q1, q = CUBITS[0].mQuatIndex;
460

    
461
    if( CUBITS[1].mQuatIndex != q ) return false;
462
    if( CUBITS[2].mQuatIndex != q ) return false;
463
    if( CUBITS[3].mQuatIndex != q ) return false;
464
    if( CUBITS[4].mQuatIndex != q ) return false;
465
    if( CUBITS[5].mQuatIndex != q ) return false;
466
    if( CUBITS[6].mQuatIndex != q ) return false;
467
    if( CUBITS[7].mQuatIndex != q ) return false;
468
    if( CUBITS[8].mQuatIndex != q ) return false;
469
    if( CUBITS[9].mQuatIndex != q ) return false;
470

    
471
    q1 = CUBITS[10].mQuatIndex;
472
    if( q1!=q && q1!=mulQuat(q,4) && q1!=mulQuat(q,8) ) return false;
473
    q1 = CUBITS[11].mQuatIndex;
474
    if( q1!=q && q1!=mulQuat(q,5) && q1!=mulQuat(q,9) ) return false;
475
    q1 = CUBITS[12].mQuatIndex;
476
    if( q1!=q && q1!=mulQuat(q,2) && q1!=mulQuat(q,6) ) return false;
477
    q1 = CUBITS[13].mQuatIndex;
478
    if( q1!=q && q1!=mulQuat(q,3) && q1!=mulQuat(q,7) ) return false;
479

    
480
    return true;
481
    }
482

    
483
////////////////////////////////////////////////////////////////////////
484
// only needed for solvers - there are no JingPyraminx solvers ATM)
485

    
486
  public String retObjectString()
487
    {
488
    return "";
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  public int getObjectName(int numLayers)
494
    {
495
    return R.string.jing;
496
    }
497

    
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499

    
500
  public int getInventor(int numLayers)
501
    {
502
    return R.string.jing_inventor;
503
    }
504

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

    
507
  public int getComplexity(int numLayers)
508
    {
509
    return 4;
510
    }
511
}
(29-29/41)