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
|
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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206
|
|
207
|
TwistyJing(int size, Static4D quat, DistortedTexture texture, MeshSquare mesh,
|
208
|
DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
|
209
|
{
|
210
|
super(size, size, quat, texture, mesh, effects, moves, ObjectList.JING, res, scrWidth);
|
211
|
}
|
212
|
|
213
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
214
|
|
215
|
float[][] getCubitPositions(int size)
|
216
|
{
|
217
|
return CENTERS;
|
218
|
}
|
219
|
|
220
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
221
|
|
222
|
Static4D[] getQuats()
|
223
|
{
|
224
|
return QUATS;
|
225
|
}
|
226
|
|
227
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228
|
|
229
|
int getNumFaces()
|
230
|
{
|
231
|
return FACE_COLORS.length;
|
232
|
}
|
233
|
|
234
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
235
|
|
236
|
int getNumStickerTypes(int numLayers)
|
237
|
{
|
238
|
return STICKERS.length;
|
239
|
}
|
240
|
|
241
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
242
|
|
243
|
float[][] getCuts(int size)
|
244
|
{
|
245
|
float[] cut = { (F-0.25f)*(SQ6/3) };
|
246
|
return new float[][] { cut,cut,cut,cut };
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
int getNumCubitFaces()
|
252
|
{
|
253
|
return 6;
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
|
258
|
float getScreenRatio()
|
259
|
{
|
260
|
return 1.64f;
|
261
|
}
|
262
|
|
263
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
264
|
|
265
|
boolean shouldResetTextureMaps()
|
266
|
{
|
267
|
return false;
|
268
|
}
|
269
|
|
270
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
271
|
|
272
|
int getFaceColor(int cubit, int cubitface, int size)
|
273
|
{
|
274
|
return mFaceMap[cubit][cubitface];
|
275
|
}
|
276
|
|
277
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
278
|
|
279
|
MeshBase createCubitMesh(int cubit, int numLayers)
|
280
|
{
|
281
|
if( mMeshes==null )
|
282
|
{
|
283
|
FactoryCubit factory = FactoryCubit.getInstance();
|
284
|
factory.clear();
|
285
|
mMeshes = new MeshBase[3];
|
286
|
}
|
287
|
|
288
|
MeshBase mesh;
|
289
|
|
290
|
if( cubit<4 )
|
291
|
{
|
292
|
if( mMeshes[0]==null )
|
293
|
{
|
294
|
float[][] bands = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
|
295
|
int[] bandIndexes = new int[] { 0,0,0,1,1,1 };
|
296
|
float[][] corners = new float[][] { {0.08f,0.20f*F},{0.07f,0.20f*F} };
|
297
|
int[] cornerIndexes = new int[] { 0,1,1,-1,1,-1,-1,-1 };
|
298
|
float[][] centers = new float[][] { { 0.0f, F*SQ2/2, -F} };
|
299
|
int[] centerIndexes = new int[] { 0,0,0,-1,0,-1,-1,-1 };
|
300
|
|
301
|
FactoryCubit factory = FactoryCubit.getInstance();
|
302
|
|
303
|
factory.createNewFaceTransform(VERTICES_CORNER,VERT_INDEXES_CORNER);
|
304
|
mMeshes[0] = factory.createRoundedSolid(VERTICES_CORNER, VERT_INDEXES_CORNER,
|
305
|
bands, bandIndexes,
|
306
|
corners, cornerIndexes,
|
307
|
centers, centerIndexes,
|
308
|
getNumCubitFaces() );
|
309
|
}
|
310
|
mesh = mMeshes[0].copy(true);
|
311
|
}
|
312
|
else if( cubit<10 )
|
313
|
{
|
314
|
if( mMeshes[1]==null )
|
315
|
{
|
316
|
float[][] bands = new float[][] { {0.015f,35,0.5f*F,F,5,1,1},{0.001f,35,0.5f*F,F,5,1,1} };
|
317
|
int[] bandIndexes = new int[] { 0,0,1,1,1,1 };
|
318
|
float[][] corners = new float[][] { {0.07f,0.20f*F} };
|
319
|
int[] cornerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
|
320
|
float[][] centers = new float[][] { { 0, F*SQ2/2, 0 } };
|
321
|
int[] centerIndexes = new int[] { 0,0,-1,0,0,0,-1,0 };
|
322
|
|
323
|
FactoryCubit factory = FactoryCubit.getInstance();
|
324
|
|
325
|
factory.createNewFaceTransform(VERTICES_EDGE,VERT_INDEXES_EDGE);
|
326
|
mMeshes[1] = factory.createRoundedSolid(VERTICES_EDGE, VERT_INDEXES_EDGE,
|
327
|
bands, bandIndexes,
|
328
|
corners, cornerIndexes,
|
329
|
centers, centerIndexes,
|
330
|
getNumCubitFaces() );
|
331
|
|
332
|
factory.printStickerCoords();
|
333
|
}
|
334
|
mesh = mMeshes[1].copy(true);
|
335
|
}
|
336
|
else
|
337
|
{
|
338
|
if( mMeshes[2]==null )
|
339
|
{
|
340
|
float[][] bands = new float[][] { {0.020f,35,0.20f*(1-3*F),0.6f*(1-3*F),5,1,1},
|
341
|
{0.001f,35,0.05f*(1-3*F),0.1f*(1-3*F),5,1,1} };
|
342
|
int[] bandIndexes = new int[] { 0,1,1,1,1,1 };
|
343
|
float[][] corners = new float[][] { {0.04f,0.15f} };
|
344
|
int[] cornerIndexes = new int[] { 0,0,0,-1,-1,-1 };
|
345
|
float[][] centers = new float[][] { { 0, -2*Y/3, 4*Z/3 } };
|
346
|
int[] centerIndexes = new int[] { 0,0,0,-1,-1,-1 };
|
347
|
|
348
|
FactoryCubit factory = FactoryCubit.getInstance();
|
349
|
|
350
|
factory.createNewFaceTransform(VERTICES_FACE,VERT_INDEXES_FACE);
|
351
|
mMeshes[2] = factory.createRoundedSolid(VERTICES_FACE, VERT_INDEXES_FACE,
|
352
|
bands, bandIndexes,
|
353
|
corners, cornerIndexes,
|
354
|
centers, centerIndexes,
|
355
|
getNumCubitFaces() );
|
356
|
|
357
|
factory.printStickerCoords();
|
358
|
}
|
359
|
mesh = mMeshes[2].copy(true);
|
360
|
}
|
361
|
|
362
|
int q = ROT_QUAT[cubit];
|
363
|
MatrixEffectQuaternion quat = new MatrixEffectQuaternion( QUATS[q], new Static3D(0,0,0) );
|
364
|
mesh.apply(quat,0xffffffff,0);
|
365
|
|
366
|
return mesh;
|
367
|
}
|
368
|
|
369
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
370
|
|
371
|
void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top)
|
372
|
{
|
373
|
int COLORS = FACE_COLORS.length;
|
374
|
int stickerType = face/COLORS;
|
375
|
float R,S;
|
376
|
|
377
|
switch(stickerType)
|
378
|
{
|
379
|
case 0: R = 0.05f; S = 0.06f; break;
|
380
|
case 1: R = 0.03f; S = 0.03f; break;
|
381
|
default: R = 0.05f; S = 0.05f; break;
|
382
|
}
|
383
|
|
384
|
FactorySticker factory = FactorySticker.getInstance();
|
385
|
factory.drawRoundedPolygon(canvas, paint, left, top, STICKERS[stickerType], S, FACE_COLORS[face%COLORS], R);
|
386
|
}
|
387
|
|
388
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
389
|
// SQ6/3 = height of the tetrahedron
|
390
|
|
391
|
float returnMultiplier()
|
392
|
{
|
393
|
return getNumLayers()/(SQ6/3);
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
// PUBLIC API
|
398
|
|
399
|
public Static3D[] getRotationAxis()
|
400
|
{
|
401
|
return ROT_AXIS;
|
402
|
}
|
403
|
|
404
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
405
|
|
406
|
public int[] getBasicAngle()
|
407
|
{
|
408
|
return BASIC_ANGLE;
|
409
|
}
|
410
|
|
411
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
412
|
|
413
|
public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
|
414
|
{
|
415
|
if( curr==0 )
|
416
|
{
|
417
|
scramble[curr][0] = rnd.nextInt(NUM_AXIS);
|
418
|
}
|
419
|
else
|
420
|
{
|
421
|
int newVector = rnd.nextInt(NUM_AXIS-1);
|
422
|
scramble[curr][0] = (newVector>=scramble[curr-1][0] ? newVector+1 : newVector);
|
423
|
}
|
424
|
|
425
|
scramble[curr][1] = rnd.nextInt(2);
|
426
|
|
427
|
switch( rnd.nextInt(2) )
|
428
|
{
|
429
|
case 0: scramble[curr][2] = -1; break;
|
430
|
case 1: scramble[curr][2] = 1; break;
|
431
|
}
|
432
|
}
|
433
|
|
434
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
435
|
// JingPyraminx is solved iff
|
436
|
// a) all of its corner and edge cubits are rotated with the same quat
|
437
|
// b) its 4 face cubits might also be rotated along the axis perpendicular to the face.
|
438
|
//
|
439
|
// So:
|
440
|
// [10] might be extra QUAT[] or QUAT[]
|
441
|
// [11] might be extra QUAT[] or QUAT[]
|
442
|
// [12] might be extra QUAT[] or QUAT[]
|
443
|
// [13] might be extra QUAT[] or QUAT[]
|
444
|
|
445
|
public boolean isSolved()
|
446
|
{
|
447
|
int index = CUBITS[0].mQuatIndex;
|
448
|
|
449
|
if( CUBITS[1].mQuatIndex != index ) return false;
|
450
|
if( CUBITS[2].mQuatIndex != index ) return false;
|
451
|
if( CUBITS[3].mQuatIndex != index ) return false;
|
452
|
if( CUBITS[4].mQuatIndex != index ) return false;
|
453
|
if( CUBITS[5].mQuatIndex != index ) return false;
|
454
|
if( CUBITS[6].mQuatIndex != index ) return false;
|
455
|
if( CUBITS[7].mQuatIndex != index ) return false;
|
456
|
if( CUBITS[8].mQuatIndex != index ) return false;
|
457
|
if( CUBITS[9].mQuatIndex != index ) return false;
|
458
|
|
459
|
// TODO: face cubits
|
460
|
|
461
|
return true;
|
462
|
}
|
463
|
|
464
|
////////////////////////////////////////////////////////////////////////
|
465
|
// only needed for solvers - there are no JingPyraminx solvers ATM)
|
466
|
|
467
|
public String retObjectString()
|
468
|
{
|
469
|
return "";
|
470
|
}
|
471
|
|
472
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
473
|
|
474
|
public int getObjectName(int numLayers)
|
475
|
{
|
476
|
return R.string.jing;
|
477
|
}
|
478
|
|
479
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
480
|
|
481
|
public int getInventor(int numLayers)
|
482
|
{
|
483
|
return R.string.jing_inventor;
|
484
|
}
|
485
|
|
486
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
487
|
|
488
|
public int getComplexity(int numLayers)
|
489
|
{
|
490
|
return 3;
|
491
|
}
|
492
|
}
|