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.ObjectShape;
|
26
|
import org.distorted.helpers.ObjectSticker;
|
27
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
28
|
import org.distorted.library.main.DistortedEffects;
|
29
|
import org.distorted.library.main.DistortedTexture;
|
30
|
import org.distorted.library.mesh.MeshBase;
|
31
|
import org.distorted.library.mesh.MeshSquare;
|
32
|
import org.distorted.library.type.Static3D;
|
33
|
import org.distorted.library.type.Static4D;
|
34
|
import org.distorted.main.R;
|
35
|
|
36
|
import java.util.Random;
|
37
|
|
38
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
|
40
|
public class TwistyJing extends TwistyObject
|
41
|
{
|
42
|
static final float F = 0.24f; // length of the edge of the corner cubit divided by
|
43
|
// the length of the edge of the whole tetrahedron.
|
44
|
// keep < 0.25.
|
45
|
|
46
|
static final Static3D[] ROT_AXIS = new Static3D[]
|
47
|
{
|
48
|
new Static3D( 0,-SQ3/3,-SQ6/3),
|
49
|
new Static3D( 0,-SQ3/3,+SQ6/3),
|
50
|
new Static3D(+SQ6/3,+SQ3/3, 0),
|
51
|
new Static3D(-SQ6/3,+SQ3/3, 0),
|
52
|
};
|
53
|
|
54
|
private static final int[] BASIC_ANGLE = new int[] { 3,3,3,3 };
|
55
|
|
56
|
private static final int[] FACE_COLORS = new int[]
|
57
|
{
|
58
|
COLOR_GREEN , COLOR_YELLOW,
|
59
|
COLOR_BLUE , COLOR_RED
|
60
|
};
|
61
|
|
62
|
private static final int[] ROT_QUAT = {0,1,2,7,0,2,7,6,3,10,0,1,3,5};
|
63
|
|
64
|
// computed with res/raw/compute_quats.c
|
65
|
private static final Static4D[] QUATS = new Static4D[]
|
66
|
{
|
67
|
new Static4D( 0.0f, 0.0f, 0.0f, 1.0f),
|
68
|
new Static4D( 0.0f, 1.0f, 0.0f, 0.0f),
|
69
|
new Static4D( SQ2/2, 0.5f, 0.0f, 0.5f),
|
70
|
new Static4D(-SQ2/2, 0.5f, 0.0f, 0.5f),
|
71
|
new Static4D( 0.0f, -0.5f, -SQ2/2, 0.5f),
|
72
|
new Static4D( 0.0f, -0.5f, SQ2/2, 0.5f),
|
73
|
new Static4D( SQ2/2, 0.5f, 0.0f, -0.5f),
|
74
|
new Static4D(-SQ2/2, 0.5f, 0.0f, -0.5f),
|
75
|
new Static4D( 0.0f, -0.5f, -SQ2/2, -0.5f),
|
76
|
new Static4D( 0.0f, -0.5f, SQ2/2, -0.5f),
|
77
|
new Static4D( SQ2/2, 0.0f, SQ2/2, 0.0f),
|
78
|
new Static4D(-SQ2/2, 0.0f, SQ2/2, 0.0f)
|
79
|
};
|
80
|
|
81
|
static final float[][] CENTERS = new float[][]
|
82
|
{
|
83
|
{ 0.000f, -SQ2/4, 0.500f },
|
84
|
{ 0.000f, -SQ2/4,-0.500f },
|
85
|
{-0.500f, SQ2/4, 0.000f },
|
86
|
{ 0.500f, SQ2/4, 0.000f },
|
87
|
|
88
|
{ 0.000f, -SQ2/4, 0.000f },
|
89
|
{-0.250f, 0.000f, 0.250f },
|
90
|
{ 0.250f, 0.000f, 0.250f },
|
91
|
{-0.250f, 0.000f,-0.250f },
|
92
|
{ 0.250f, 0.000f,-0.250f },
|
93
|
{ 0.000f, SQ2/4, 0.000f },
|
94
|
|
95
|
{ 0.000f, SQ2/12, 1.0f/6 },
|
96
|
{ 0.000f, SQ2/12,-1.0f/6 },
|
97
|
{-1.0f/6,-SQ2/12, 0.000f },
|
98
|
{ 1.0f/6,-SQ2/12, 0.000f },
|
99
|
};
|
100
|
|
101
|
// Colors of the faces of cubits.
|
102
|
// GREEN 0 YELLOW 1 BLUE 2 RED 3
|
103
|
// GREEN 4 YELLOW 5 BLUE 6 RED 7
|
104
|
// GREEN 8 YELLOW 9 BLUE 10 RED 11
|
105
|
private static final int[][] mFaceMap = new int[][]
|
106
|
{
|
107
|
{ 0, 3, 2, 12, 12, 12 },
|
108
|
{ 1, 2, 3, 12, 12, 12 },
|
109
|
{ 1, 0, 2, 12, 12, 12 },
|
110
|
{ 1, 3, 0, 12, 12, 12 },
|
111
|
|
112
|
{ 7, 6, 12, 12, 12, 12 },
|
113
|
{ 4, 6, 12, 12, 12, 12 },
|
114
|
{ 7, 4, 12, 12, 12, 12 },
|
115
|
{ 5, 6, 12, 12, 12, 12 },
|
116
|
{ 7, 5, 12, 12, 12, 12 },
|
117
|
{ 4, 5, 12, 12, 12, 12 },
|
118
|
|
119
|
{ 8, 12, 12, 12, 12, 12 },
|
120
|
{ 9, 12, 12, 12, 12, 12 },
|
121
|
{ 10, 12, 12, 12, 12, 12 },
|
122
|
{ 11, 12, 12, 12, 12, 12 },
|
123
|
};
|
124
|
|
125
|
private static final float X = F/2;
|
126
|
private static final float Y = F*SQ2/2;
|
127
|
private static final float Z =-F/2;
|
128
|
private static final float L = (1-3*F);
|
129
|
private static final float X2= L/2;
|
130
|
private static final float Y2= L*SQ2/2;
|
131
|
private static final float Z2=-L/2;
|
132
|
private static final float D = F/L;
|
133
|
|
134
|
private static final double[][] VERTICES_CORNER = new double[][]
|
135
|
{
|
136
|
{ 0.0, 0.0, 0.0 },
|
137
|
{ X, Y, Z },
|
138
|
{ 0.0, 2*Y, 2*Z },
|
139
|
{ -X, Y, Z },
|
140
|
{ 0.0, 0.0, -F },
|
141
|
{ X, Y, Z-F },
|
142
|
{ 0.0, 2*Y, 2*Z-F },
|
143
|
{ -X, Y, Z-F },
|
144
|
};
|
145
|
|
146
|
private static final int[][] VERT_INDEXES_CORNER = new int[][]
|
147
|
{
|
148
|
{0,1,2,3},
|
149
|
{1,0,4,5},
|
150
|
{7,4,0,3},
|
151
|
{1,5,6,2},
|
152
|
{7,3,2,6},
|
153
|
{4,7,6,5}
|
154
|
};
|
155
|
|
156
|
private static final double[][] VERTICES_EDGE = new double[][]
|
157
|
{
|
158
|
{ 0.0, 0.0, 0.5-F },
|
159
|
{ X, Y, Z+0.5-F },
|
160
|
{ 0.0, 2*Y, 2*Z+0.5-F },
|
161
|
{ -X, Y, Z+0.5-F },
|
162
|
{ 0.0, 0.0, -0.5+F },
|
163
|
{ X, Y, -Z-0.5+F },
|
164
|
{ 0.0, 2*Y,-2*Z-0.5+F },
|
165
|
{ -X, Y, -Z-0.5+F },
|
166
|
};
|
167
|
|
168
|
private static final int[][] VERT_INDEXES_EDGE = new int[][]
|
169
|
{
|
170
|
{0,4,5,1},
|
171
|
{3,7,4,0},
|
172
|
{0,1,2,3},
|
173
|
{4,7,6,5},
|
174
|
{1,5,6,2},
|
175
|
{2,6,7,3}
|
176
|
};
|
177
|
|
178
|
private static final double[][] VERTICES_FACE = new double[][]
|
179
|
{
|
180
|
{ 0.0, -2*Y2/3, -2*Z2/3 },
|
181
|
{ X2, Y2/3, Z2/3 },
|
182
|
{ -X2, Y2/3, Z2/3 },
|
183
|
{ 0.0, -2*Y2/3,-2*Z2/3+2*D*Z2 },
|
184
|
{ X2-D*X2, Y2/3-D*Y2, Z2/3+D*Z2 },
|
185
|
{ -X2+D*X2, Y2/3-D*Y2, Z2/3+D*Z2 },
|
186
|
};
|
187
|
|
188
|
private static final int[][] VERT_INDEXES_FACE = new int[][]
|
189
|
{
|
190
|
{0,1,2},
|
191
|
{3,5,4},
|
192
|
{0,3,4,1},
|
193
|
{5,3,0,2},
|
194
|
{4,5,2,1}
|
195
|
};
|
196
|
|
197
|
private static final float[][] STICKERS = new float[][]
|
198
|
{
|
199
|
{ 0.0f, -0.5f, 0.28867516f, 0.0f, 0.0f, 0.5f, -0.28867516f, 0.0f },
|
200
|
{ -0.5f, 0.11983269f, 0.27964598f, -0.43146032f, 0.3200817f, 0.007388768f, -0.09972769f, 0.30423886f },
|
201
|
{ 0.0f, -0.5f, 0.43301272f, 0.25f, -0.43301272f, 0.25f },
|
202
|
};
|
203
|
|
204
|
private static final ObjectSticker[] mStickers;
|
205
|
|
206
|
static
|
207
|
{
|
208
|
mStickers = new ObjectSticker[STICKERS.length];
|
209
|
final float R1 = 0.05f;
|
210
|
final float R2 = 0.10f;
|
211
|
final float R3 = 0.03f;
|
212
|
final float[][] radii = { { R1,R1,R1,R1 },{ R3,R3,R2,R2 },{ R1,R1,R1 } };
|
213
|
final float[] strokes = { 0.06f, 0.03f, 0.05f };
|
214
|
|
215
|
for(int s=0; s<STICKERS.length; s++)
|
216
|
{
|
217
|
mStickers[s] = new ObjectSticker(STICKERS[s],null,radii[s],strokes[s]);
|
218
|
}
|
219
|
}
|
220
|
|
221
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222
|
|
223
|
TwistyJing(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
|
224
|
DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
|
225
|
{
|
226
|
super(size, size, quat, texture, mesh, effects, moves, ObjectList.JING, res, scrWidth);
|
227
|
}
|
228
|
|
229
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
230
|
|
231
|
float[][] getCubitPositions(int size)
|
232
|
{
|
233
|
return CENTERS;
|
234
|
}
|
235
|
|
236
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
237
|
|
238
|
Static4D[] getQuats()
|
239
|
{
|
240
|
return QUATS;
|
241
|
}
|
242
|
|
243
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
244
|
|
245
|
int getNumFaces()
|
246
|
{
|
247
|
return FACE_COLORS.length;
|
248
|
}
|
249
|
|
250
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
251
|
|
252
|
int getNumStickerTypes(int numLayers)
|
253
|
{
|
254
|
return STICKERS.length;
|
255
|
}
|
256
|
|
257
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258
|
|
259
|
float[][] getCuts(int size)
|
260
|
{
|
261
|
float[] cut = { (F-0.25f)*(SQ6/3) };
|
262
|
return new float[][] { cut,cut,cut,cut };
|
263
|
}
|
264
|
|
265
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
266
|
|
267
|
int getNumCubitFaces()
|
268
|
{
|
269
|
return 6;
|
270
|
}
|
271
|
|
272
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
273
|
|
274
|
float getScreenRatio()
|
275
|
{
|
276
|
return 2*TwistyPyraminx.SCREEN_RATIO;
|
277
|
}
|
278
|
|
279
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
280
|
|
281
|
boolean shouldResetTextureMaps()
|
282
|
{
|
283
|
return false;
|
284
|
}
|
285
|
|
286
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
287
|
|
288
|
int getFaceColor(int cubit, int cubitface, int size)
|
289
|
{
|
290
|
return mFaceMap[cubit][cubitface];
|
291
|
}
|
292
|
|
293
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
294
|
|
295
|
ObjectShape getObjectShape(int cubit, int numLayers)
|
296
|
{
|
297
|
int variant = getCubitVariant(cubit,numLayers);
|
298
|
|
299
|
if( variant==0 )
|
300
|
{
|
301
|
float[][] bands = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
|
302
|
int[] bandIndices = new int[] { 0,0,0,1,1,1 };
|
303
|
float[][] corners = new float[][] { {0.08f,0.20f*F},{0.07f,0.20f*F} };
|
304
|
int[] cornerIndices = new int[] { 0,1,1,-1,1,-1,-1,-1 };
|
305
|
float[][] centers = new float[][] { { 0.0f, F*SQ2/2, -F} };
|
306
|
int[] centerIndices = new int[] { 0,0,0,-1,0,-1,-1,-1 };
|
307
|
return new ObjectShape(VERTICES_CORNER,VERT_INDEXES_CORNER,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
|
308
|
}
|
309
|
else if( variant==1 )
|
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[] bandIndices = new int[] { 0,0,1,1,1,1 };
|
313
|
float[][] corners = new float[][] { {0.07f,0.20f*F} };
|
314
|
int[] cornerIndices = new int[] { 0,0,-1,0,0,0,-1,0 };
|
315
|
float[][] centers = new float[][] { { 0, F*SQ2/2, 0 } };
|
316
|
int[] centerIndices = new int[] { 0,0,-1,0,0,0,-1,0 };
|
317
|
return new ObjectShape(VERTICES_EDGE,VERT_INDEXES_EDGE,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
|
318
|
}
|
319
|
else
|
320
|
{
|
321
|
float[][] bands = new float[][] { {0.020f,35,0.20f*(1-3*F),0.6f*(1-3*F),5,1,1},
|
322
|
{0.001f,35,0.05f*(1-3*F),0.1f*(1-3*F),5,1,1} };
|
323
|
int[] bandIndices = new int[] { 0,1,1,1,1,1 };
|
324
|
float[][] corners = new float[][] { {0.04f,0.15f} };
|
325
|
int[] cornerIndices = new int[] { 0,0,0,-1,-1,-1 };
|
326
|
float[][] centers = new float[][] { { 0, -2*Y/3, 4*Z/3 } };
|
327
|
int[] centerIndices = new int[] { 0,0,0,-1,-1,-1 };
|
328
|
return new ObjectShape(VERTICES_FACE,VERT_INDEXES_FACE,bands,bandIndices,corners,cornerIndices,centers,centerIndices,getNumCubitFaces(), null);
|
329
|
}
|
330
|
}
|
331
|
|
332
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
333
|
|
334
|
private Static4D getQuat(int cubit, int numLayers)
|
335
|
{
|
336
|
return QUATS[ROT_QUAT[cubit]];
|
337
|
}
|
338
|
|
339
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
340
|
|
341
|
private int getNumCubitVariants(int numLayers)
|
342
|
{
|
343
|
return 3;
|
344
|
}
|
345
|
|
346
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
347
|
|
348
|
int getCubitVariant(int cubit, int numLayers)
|
349
|
{
|
350
|
return cubit<4 ? 0 : (cubit<10?1:2);
|
351
|
}
|
352
|
|
353
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
354
|
|
355
|
MeshBase createCubitMesh(int cubit, int numLayers)
|
356
|
{
|
357
|
int variant = getCubitVariant(cubit,numLayers);
|
358
|
|
359
|
if( mMeshes==null )
|
360
|
{
|
361
|
FactoryCubit factory = FactoryCubit.getInstance();
|
362
|
factory.clear();
|
363
|
mMeshes = new MeshBase[getNumCubitVariants(numLayers)];
|
364
|
}
|
365
|
|
366
|
if( mMeshes[variant]==null )
|
367
|
{
|
368
|
ObjectShape shape = getObjectShape(cubit,numLayers);
|
369
|
FactoryCubit factory = FactoryCubit.getInstance();
|
370
|
factory.createNewFaceTransform(shape);
|
371
|
mMeshes[variant] = factory.createRoundedSolid(shape);
|
372
|
}
|
373
|
|
374
|
MeshBase mesh = mMeshes[variant].copy(true);
|
375
|
MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit,numLayers), new Static3D(0,0,0) );
|
376
|
mesh.apply(quat,0xffffffff,0);
|
377
|
|
378
|
return mesh;
|
379
|
}
|
380
|
|
381
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
382
|
|
383
|
int getColor(int face)
|
384
|
{
|
385
|
return FACE_COLORS[face];
|
386
|
}
|
387
|
|
388
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
389
|
|
390
|
ObjectSticker retSticker(int face)
|
391
|
{
|
392
|
return mStickers[face/NUM_FACES];
|
393
|
}
|
394
|
|
395
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
396
|
// SQ6/3 = height of the tetrahedron
|
397
|
|
398
|
float returnMultiplier()
|
399
|
{
|
400
|
return getNumLayers()/(SQ6/3);
|
401
|
}
|
402
|
|
403
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
404
|
// PUBLIC API
|
405
|
|
406
|
public Static3D[] getRotationAxis()
|
407
|
{
|
408
|
return ROT_AXIS;
|
409
|
}
|
410
|
|
411
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
412
|
|
413
|
public int[] getBasicAngle()
|
414
|
{
|
415
|
return BASIC_ANGLE;
|
416
|
}
|
417
|
|
418
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
419
|
|
420
|
public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
|
421
|
{
|
422
|
if( curr==0 )
|
423
|
{
|
424
|
scramble[curr][0] = rnd.nextInt(NUM_AXIS);
|
425
|
}
|
426
|
else
|
427
|
{
|
428
|
int newVector = rnd.nextInt(NUM_AXIS-1);
|
429
|
scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
|
430
|
}
|
431
|
|
432
|
scramble[curr][1] = rnd.nextInt(2);
|
433
|
|
434
|
switch( rnd.nextInt(2) )
|
435
|
{
|
436
|
case 0: scramble[curr][2] = -1; break;
|
437
|
case 1: scramble[curr][2] = 1; break;
|
438
|
}
|
439
|
}
|
440
|
|
441
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
442
|
// JingPyraminx is solved iff
|
443
|
// a) all of its corner and edge cubits are rotated with the same quat
|
444
|
// b) its 4 face cubits might also be rotated along the axis perpendicular to the face.
|
445
|
//
|
446
|
// So:
|
447
|
// [10] might be extra QUAT[4] or QUAT[8]
|
448
|
// [11] might be extra QUAT[5] or QUAT[9]
|
449
|
// [12] might be extra QUAT[2] or QUAT[6]
|
450
|
// [13] might be extra QUAT[3] or QUAT[7]
|
451
|
|
452
|
public boolean isSolved()
|
453
|
{
|
454
|
int q1, q = CUBITS[0].mQuatIndex;
|
455
|
|
456
|
if( CUBITS[1].mQuatIndex != q ) return false;
|
457
|
if( CUBITS[2].mQuatIndex != q ) return false;
|
458
|
if( CUBITS[3].mQuatIndex != q ) return false;
|
459
|
if( CUBITS[4].mQuatIndex != q ) return false;
|
460
|
if( CUBITS[5].mQuatIndex != q ) return false;
|
461
|
if( CUBITS[6].mQuatIndex != q ) return false;
|
462
|
if( CUBITS[7].mQuatIndex != q ) return false;
|
463
|
if( CUBITS[8].mQuatIndex != q ) return false;
|
464
|
if( CUBITS[9].mQuatIndex != q ) return false;
|
465
|
|
466
|
q1 = CUBITS[10].mQuatIndex;
|
467
|
if( q1!=q && q1!=mulQuat(q,4) && q1!=mulQuat(q,8) ) return false;
|
468
|
q1 = CUBITS[11].mQuatIndex;
|
469
|
if( q1!=q && q1!=mulQuat(q,5) && q1!=mulQuat(q,9) ) return false;
|
470
|
q1 = CUBITS[12].mQuatIndex;
|
471
|
if( q1!=q && q1!=mulQuat(q,2) && q1!=mulQuat(q,6) ) return false;
|
472
|
q1 = CUBITS[13].mQuatIndex;
|
473
|
if( q1!=q && q1!=mulQuat(q,3) && q1!=mulQuat(q,7) ) return false;
|
474
|
|
475
|
return true;
|
476
|
}
|
477
|
|
478
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
479
|
|
480
|
public int getObjectName(int numLayers)
|
481
|
{
|
482
|
return R.string.jing;
|
483
|
}
|
484
|
|
485
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
486
|
|
487
|
public int getInventor(int numLayers)
|
488
|
{
|
489
|
return R.string.jing_inventor;
|
490
|
}
|
491
|
|
492
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
493
|
|
494
|
public int getComplexity(int numLayers)
|
495
|
{
|
496
|
return 4;
|
497
|
}
|
498
|
}
|