Project

General

Profile

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

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

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;
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
  // computed with res/raw/compute_quats.c
62
  private static final Static4D[] QUATS = new Static4D[]
63
         {
64
           new Static4D(  0.0f,   0.0f,   0.0f,  1.0f),
65
           new Static4D(  0.0f,   1.0f,   0.0f,  0.0f),
66
           new Static4D( SQ2/2,   0.5f,   0.0f,  0.5f),
67
           new Static4D(-SQ2/2,   0.5f,   0.0f,  0.5f),
68
           new Static4D(  0.0f,  -0.5f, -SQ2/2,  0.5f),
69
           new Static4D(  0.0f,  -0.5f,  SQ2/2,  0.5f),
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.0f,  SQ2/2,  0.0f),
75
           new Static4D(-SQ2/2,   0.0f,  SQ2/2,  0.0f)
76
         };
77

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

    
85
           { 0.000f, -SQ2/4, 0.000f },
86
           {-0.250f, 0.000f, 0.250f },
87
           { 0.250f, 0.000f, 0.250f },
88
           {-0.250f, 0.000f,-0.250f },
89
           { 0.250f, 0.000f,-0.250f },
90
           { 0.000f,  SQ2/4, 0.000f },
91

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

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

    
109
           {  7, 5, 12, 12, 12, 12 },
110
           {  6, 5, 12, 12, 12, 12 },
111
           {  7, 6, 12, 12, 12, 12 },
112
           {  4, 5, 12, 12, 12, 12 },
113
           {  7, 4, 12, 12, 12, 12 },
114
           {  6, 4, 12, 12, 12, 12 },
115

    
116
           { 10, 12, 12, 12, 12, 12 },
117
           {  8, 12, 12, 12, 12, 12 },
118
           {  9, 12, 12, 12, 12, 12 },
119
           { 11, 12, 12, 12, 12, 12 },
120
         };
121

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

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

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

    
153
  private static final double[][] VERTICES_EDGE = new double[][]
154
          {
155
             { 0.0, 0.0,     0.5-F },
156
             {   X,   Y,   Z+0.5-F },
157
             { 0.0, 2*Y, 2*Z+0.5-F },
158
             {  -X,   Y,   Z+0.5-F },
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
          };
164

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

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

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

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

    
201
  private static MeshBase[] mMeshes;
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  TwistyJing(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
206
             DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
207
    {
208
    super(size, size, quat, texture, mesh, effects, moves, ObjectList.JING, res, scrWidth);
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  float[][] getCubitPositions(int size)
214
    {
215
    return CENTERS;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  Static4D[] getQuats()
221
    {
222
    return QUATS;
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  int getNumFaces()
228
    {
229
    return FACE_COLORS.length;
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  int getNumStickerTypes(int numLayers)
235
    {
236
    return STICKERS.length;
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  float[][] getCuts(int size)
242
    {
243
    float[] cut = {F-0.5f};
244
    return new float[][] { cut,cut,cut,cut };
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  int getNumCubitFaces()
250
    {
251
    return 6;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  float getScreenRatio()
257
    {
258
    return 1.64f;
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  boolean shouldResetTextureMaps()
264
    {
265
    return false;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  int getFaceColor(int cubit, int cubitface, int size)
271
    {
272
    return mFaceMap[cubit][cubitface];
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private int getQuat(int cubit)
278
    {
279
    switch(cubit)
280
      {
281
      case  0: return 0;
282
      case  1: return 1;
283
      case  2: return 2;
284
      case  3: return 7;
285

    
286
      case  4: return 0;
287
      case  5: return 2;
288
      case  6: return 7;
289
      case  7: return 6;
290
      case  8: return 3;
291
      case  9: return 10;
292

    
293
      case 10: return 0;
294
      case 11: return 1;
295
      case 12: return 3;
296
      case 13: return 5;
297
      }
298

    
299
    return 0;
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  MeshBase createCubitMesh(int cubit, int numLayers)
305
    {
306
    if( mMeshes==null )
307
      {
308
      FactoryCubit factory = FactoryCubit.getInstance();
309
      factory.clear();
310
      mMeshes = new MeshBase[3];
311
      }
312

    
313
    MeshBase mesh;
314

    
315
    if( cubit<4 )
316
      {
317
      if( mMeshes[0]==null )
318
        {
319
        float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
320
        int[] bandIndexes   = new int[] { 0,0,0,1,1,1 };
321
        float[][] corners   = new float[][] { {0.10f,0.20f*F},{0.07f,0.20f*F} };
322
        int[] cornerIndexes = new int[] { 0,1,1,-1,1,-1,-1,-1 };
323
        float[][] centers   = new float[][] { { 0.0f, F*SQ2/2, -F} };
324
        int[] centerIndexes = new int[] { 0,0,0,-1,0,-1,-1,-1 };
325

    
326
        FactoryCubit factory = FactoryCubit.getInstance();
327

    
328
        factory.createNewFaceTransform(VERTICES_CORNER,VERT_INDEXES_CORNER);
329
        mMeshes[0] = factory.createRoundedSolid(VERTICES_CORNER, VERT_INDEXES_CORNER,
330
                                                bands, bandIndexes,
331
                                                corners, cornerIndexes,
332
                                                centers, centerIndexes,
333
                                                getNumCubitFaces() );
334
        }
335
      mesh = mMeshes[0].copy(true);
336
      }
337
    else if( cubit<10 )
338
      {
339
      if( mMeshes[1]==null )
340
        {
341
        float[][] bands     = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
342
        int[] bandIndexes   = new int[] { 0,0,1,1,1,1 };
343
        float[][] corners   = new float[][] { {0.07f,0.20f*F} };
344
        int[] cornerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
345
        float[][] centers   = new float[][] { { 0, F*SQ2/2, 0 } };
346
        int[] centerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
347

    
348
        FactoryCubit factory = FactoryCubit.getInstance();
349

    
350
        factory.createNewFaceTransform(VERTICES_EDGE,VERT_INDEXES_EDGE);
351
        mMeshes[1] = factory.createRoundedSolid(VERTICES_EDGE, VERT_INDEXES_EDGE,
352
                                                bands, bandIndexes,
353
                                                corners, cornerIndexes,
354
                                                centers, centerIndexes,
355
                                                getNumCubitFaces() );
356

    
357
        factory.printStickerCoords();
358
        }
359
      mesh = mMeshes[1].copy(true);
360
      }
361
    else
362
      {
363
      if( mMeshes[2]==null )
364
        {
365
        float[][] bands     = new float[][] { {0.020f,35,0.20f*(1-3*F),0.6f*(1-3*F),5,1,1},
366
                                              {0.001f,35,0.05f*(1-3*F),0.1f*(1-3*F),5,1,1} };
367
        int[] bandIndexes   = new int[] { 0,1,1,1,1,1 };
368
        float[][] corners   = new float[][] { {0.04f,0.15f} };
369
        int[] cornerIndexes = new int[] { 0,0,0,-1,-1,-1 };
370
        float[][] centers   = new float[][] { { 0, -2*Y/3, 4*Z/3 } };
371
        int[] centerIndexes = new int[] { 0,0,0,-1,-1,-1 };
372

    
373
        FactoryCubit factory = FactoryCubit.getInstance();
374

    
375
        factory.createNewFaceTransform(VERTICES_FACE,VERT_INDEXES_FACE);
376
        mMeshes[2] = factory.createRoundedSolid(VERTICES_FACE, VERT_INDEXES_FACE,
377
                                                bands, bandIndexes,
378
                                                corners, cornerIndexes,
379
                                                centers, centerIndexes,
380
                                                getNumCubitFaces() );
381

    
382
        factory.printStickerCoords();
383
        }
384
      mesh = mMeshes[2].copy(true);
385
      }
386

    
387
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( QUATS[getQuat(cubit)], new Static3D(0,0,0) );
388
    mesh.apply(quat,0xffffffff,0);
389

    
390
    return mesh;
391
    }
392

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

    
395
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
396
    {
397
    int COLORS = FACE_COLORS.length;
398
    int stickerType = face/COLORS;
399
    float R,S;
400

    
401
    switch(stickerType)
402
      {
403
      case 0:  R = 0.05f; S = 0.05f; break;
404
      case 1:  R = 0.03f; S = 0.03f; break;
405
      default: R = 0.05f; S = 0.06f; break;
406
      }
407

    
408
    FactorySticker factory = FactorySticker.getInstance();
409
    factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
410
    }
411

    
412
///////////////////////////////////////////////////////////////////////////////////////////////////
413
// SQ6/3 = height of the tetrahedron
414

    
415
  float returnMultiplier()
416
    {
417
    return getNumLayers()/(SQ6/3);
418
    }
419

    
420
///////////////////////////////////////////////////////////////////////////////////////////////////
421
// PUBLIC API
422

    
423
  public Static3D[] getRotationAxis()
424
    {
425
    return ROT_AXIS;
426
    }
427

    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429

    
430
  public int[] getBasicAngle()
431
    {
432
    return BASIC_ANGLE;
433
    }
434

    
435
///////////////////////////////////////////////////////////////////////////////////////////////////
436

    
437
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
438
    {
439
    if( curr==0 )
440
      {
441
      scramble[curr][0] = rnd.nextInt(NUM_AXIS);
442
      }
443
    else
444
      {
445
      int newVector = rnd.nextInt(NUM_AXIS-1);
446
      scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
447
      }
448

    
449
    scramble[curr][1] = rnd.nextInt(2);
450

    
451
    switch( rnd.nextInt(2) )
452
      {
453
      case 0: scramble[curr][2] = -1; break;
454
      case 1: scramble[curr][2] =  1; break;
455
      }
456
    }
457

    
458
///////////////////////////////////////////////////////////////////////////////////////////////////
459
// JingPyraminx is solved iff
460
// a) all of its corner and edge cubits are rotated with the same quat
461
// b) its 4 face cubits might also be rotated along the axis perpendicular to the face.
462
//
463
// So:
464
// [10] might be extra QUAT[] or QUAT[]
465
// [11] might be extra QUAT[] or QUAT[]
466
// [12] might be extra QUAT[] or QUAT[]
467
// [13] might be extra QUAT[] or QUAT[]
468

    
469
  public boolean isSolved()
470
    {
471
    int index = CUBITS[0].mQuatIndex;
472

    
473
    if( CUBITS[1].mQuatIndex != index ) return false;
474
    if( CUBITS[2].mQuatIndex != index ) return false;
475
    if( CUBITS[3].mQuatIndex != index ) return false;
476
    if( CUBITS[4].mQuatIndex != index ) return false;
477
    if( CUBITS[5].mQuatIndex != index ) return false;
478
    if( CUBITS[6].mQuatIndex != index ) return false;
479
    if( CUBITS[7].mQuatIndex != index ) return false;
480
    if( CUBITS[8].mQuatIndex != index ) return false;
481
    if( CUBITS[9].mQuatIndex != index ) return false;
482

    
483
    // TODO: face cubits
484

    
485
    return true;
486
    }
487

    
488
////////////////////////////////////////////////////////////////////////
489
// only needed for solvers - there are no JingPyraminx solvers ATM)
490

    
491
  public String retObjectString()
492
    {
493
    return "";
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public int getObjectName(int numLayers)
499
    {
500
    return R.string.jing;
501
    }
502

    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504

    
505
  public int getInventor(int numLayers)
506
    {
507
    return R.string.jing_inventor;
508
    }
509

    
510
///////////////////////////////////////////////////////////////////////////////////////////////////
511

    
512
  public int getComplexity(int numLayers)
513
    {
514
    return 3;
515
    }
516
}
(29-29/41)