1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2019 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.library.effect.VertexEffectDeform;
|
27
|
import org.distorted.library.effect.VertexEffectMove;
|
28
|
import org.distorted.library.effect.VertexEffectRotate;
|
29
|
import org.distorted.library.effect.VertexEffectScale;
|
30
|
import org.distorted.library.effect.VertexEffectSink;
|
31
|
import org.distorted.library.main.DistortedEffects;
|
32
|
import org.distorted.library.main.DistortedTexture;
|
33
|
import org.distorted.library.mesh.MeshBase;
|
34
|
import org.distorted.library.mesh.MeshJoined;
|
35
|
import org.distorted.library.mesh.MeshRectangles;
|
36
|
import org.distorted.library.mesh.MeshTriangles;
|
37
|
import org.distorted.library.type.Static1D;
|
38
|
import org.distorted.library.type.Static3D;
|
39
|
import org.distorted.library.type.Static4D;
|
40
|
|
41
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42
|
|
43
|
public class RubikPyraminx extends RubikObject
|
44
|
{
|
45
|
private static final float SQ2 = (float)Math.sqrt(2);
|
46
|
private static final float SQ3 = (float)Math.sqrt(3);
|
47
|
private static final float SQ6 = (float)Math.sqrt(6);
|
48
|
|
49
|
static final Static3D[] AXIS = new Static3D[]
|
50
|
{
|
51
|
new Static3D( 0, 1, 0 ),
|
52
|
new Static3D( 0, -1.0f/3, 2*SQ2/3 ),
|
53
|
new Static3D(-SQ2*SQ3/3, -1.0f/3, -SQ2/3 ),
|
54
|
new Static3D( SQ2*SQ3/3, -1.0f/3, -SQ2/3 )
|
55
|
};
|
56
|
|
57
|
private static final int[] FACE_COLORS = new int[]
|
58
|
{
|
59
|
0xff00ff00, 0xffffff00, // AXIS[0]right (GREEN ) AXIS[1]right (YELLOW )
|
60
|
0xff0000ff, 0xffff0000 // AXIS[2]right (BLUE ) AXIS[3]right (RED )
|
61
|
};
|
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, SQ3/2, 0.0f, 0.5f),
|
68
|
new Static4D( SQ2/2, -SQ3/6, -SQ6/6, 0.5f),
|
69
|
new Static4D(-SQ2/2, -SQ3/6, -SQ6/6, 0.5f),
|
70
|
new Static4D( 0.0f, -SQ3/6, SQ6/3, 0.5f),
|
71
|
new Static4D( 0.0f, SQ3/2, 0.0f, -0.5f),
|
72
|
new Static4D( SQ2/2, -SQ3/6, -SQ6/6, -0.5f),
|
73
|
new Static4D(-SQ2/2, -SQ3/6, -SQ6/6, -0.5f),
|
74
|
new Static4D( 0.0f, -SQ3/6, SQ6/3, -0.5f),
|
75
|
new Static4D( SQ2/2, -SQ3/3, SQ6/6, 0.0f),
|
76
|
new Static4D( 0.0f, -SQ3/3, -SQ6/3, 0.0f),
|
77
|
new Static4D(-SQ2/2, -SQ3/3, SQ6/6, 0.0f)
|
78
|
};
|
79
|
|
80
|
private int[] mRotArray;
|
81
|
private static VertexEffectRotate[] ROTATION;
|
82
|
|
83
|
private static MeshBase mMesh =null;
|
84
|
private static MeshBase[] mMeshRotated = new MeshBase[AXIS.length];
|
85
|
|
86
|
static
|
87
|
{
|
88
|
Static3D center = new Static3D(0,0,0);
|
89
|
Static1D angle = new Static1D(180.0f);
|
90
|
|
91
|
ROTATION = new VertexEffectRotate[AXIS.length];
|
92
|
|
93
|
for(int i=0; i<AXIS.length; i++)
|
94
|
{
|
95
|
ROTATION[i] = new VertexEffectRotate( angle, AXIS[i], center);
|
96
|
mMeshRotated[i] = null;
|
97
|
}
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
|
102
|
RubikPyraminx(int size, Static4D quat, DistortedTexture texture,
|
103
|
MeshRectangles mesh, DistortedEffects effects, int[][] moves, Resources res)
|
104
|
{
|
105
|
super(size, 30, quat, texture, mesh, effects, moves, RubikObjectList.PYRA, res);
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
private void emitRow(float x, float y, float z, float dx, float dy, float dz, int n, int rot, Static3D[] array, int index)
|
111
|
{
|
112
|
for(int i=0; i<n; i++)
|
113
|
{
|
114
|
mRotArray[i+index] = rot;
|
115
|
array[i+index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
|
116
|
x += dx;
|
117
|
y += dy;
|
118
|
z += dz;
|
119
|
}
|
120
|
}
|
121
|
|
122
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123
|
|
124
|
private int emitLowermost(float x, float y, float z, int n, Static3D[] array)
|
125
|
{
|
126
|
int added = 0;
|
127
|
|
128
|
emitRow( x +0.5f, y+SQ3*SQ2/9, z+ SQ3/18, 1.0f, 0, 0, n-1, 1, array, added);
|
129
|
added += (n-1);
|
130
|
emitRow( x +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, 0.5f, 0, SQ3/2, n-1, 3, array, added);
|
131
|
added += (n-1);
|
132
|
emitRow( x+n-1-1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, -0.5f, 0, SQ3/2, n-1, 2, array, added);
|
133
|
added += (n-1);
|
134
|
|
135
|
for(int i=n; i>=1; i--)
|
136
|
{
|
137
|
emitRow(x , y, z , 1,0,0, i , -1, array, added);
|
138
|
added += i;
|
139
|
emitRow(x+0.5f, y, z+SQ3/6, 1,0,0, i-1, 0, array, added);
|
140
|
added += (i-1);
|
141
|
x += 0.5f;
|
142
|
y += 0.0f;
|
143
|
z += SQ3/2;
|
144
|
}
|
145
|
|
146
|
return added;
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
private int emitUpper(float x, float y, float z, int n, Static3D[] array, int index)
|
152
|
{
|
153
|
if( n>1 )
|
154
|
{
|
155
|
emitRow( x , y , z , 1.0f, 0, 0, n-1, -1, array, index);
|
156
|
index += (n-1);
|
157
|
emitRow( x+0.5f , y+SQ3*SQ2/9, z+SQ3/18 , 1.0f, 0, 0, n-1, 1, array, index);
|
158
|
index += (n-1);
|
159
|
emitRow( x+0.5f , y , z+SQ3/2 , 0.5f, 0, SQ3/2, n-1, -1, array, index);
|
160
|
index += (n-1);
|
161
|
emitRow( x +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, 0.5f, 0, SQ3/2, n-1, 3, array, index);
|
162
|
index += (n-1);
|
163
|
emitRow( x+n-1 , y , z , -0.5f, 0, SQ3/2, n-1, -1, array, index);
|
164
|
index += (n-1);
|
165
|
emitRow( x+n-1-1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, -0.5f, 0, SQ3/2, n-1, 2, array, index);
|
166
|
index += (n-1);
|
167
|
}
|
168
|
else
|
169
|
{
|
170
|
mRotArray[index] = -1;
|
171
|
array[index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
|
172
|
index++;
|
173
|
}
|
174
|
|
175
|
return index;
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
// size^2 + 3*(size-1) in the lowermost layer, then 6*(size-2) in the next, 6*(size-3) in the next,
|
180
|
// ... 6 in the forelast, 1 in the last = 4size^2 - 6size +4 (if size>1)
|
181
|
|
182
|
Static3D[] getCubitPositions(int size)
|
183
|
{
|
184
|
int numCubits = size>1 ? 4*size*size - 6*size +4 : 1;
|
185
|
Static3D[] tmp = new Static3D[numCubits];
|
186
|
mRotArray = new int[numCubits];
|
187
|
|
188
|
int currentIndex = emitLowermost( -0.5f*size, -(SQ2*SQ3/12)*size, -(SQ3/6)*size, size, tmp);
|
189
|
|
190
|
for(int i=size-1; i>=1; i--)
|
191
|
{
|
192
|
currentIndex = emitUpper( -0.5f*i, ((SQ2*SQ3)/12)*(3*size-4*i), -(SQ3/6)*i, i, tmp, currentIndex);
|
193
|
}
|
194
|
|
195
|
return tmp;
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
|
200
|
Static4D[] getQuats()
|
201
|
{
|
202
|
return QUATS;
|
203
|
}
|
204
|
|
205
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206
|
|
207
|
int getNumFaces()
|
208
|
{
|
209
|
return FACE_COLORS.length;
|
210
|
}
|
211
|
|
212
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
213
|
|
214
|
float getScreenRatio()
|
215
|
{
|
216
|
return 0.82f;
|
217
|
}
|
218
|
|
219
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
220
|
|
221
|
private MeshBase createStaticMesh(int cubit)
|
222
|
{
|
223
|
final float SQ2 = (float)Math.sqrt(2);
|
224
|
final float SQ3 = (float)Math.sqrt(3);
|
225
|
final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
|
226
|
final int MESHES=4;
|
227
|
|
228
|
int association = 1;
|
229
|
MeshBase[] meshes = new MeshTriangles[MESHES];
|
230
|
|
231
|
meshes[0] = new MeshTriangles(5);
|
232
|
meshes[0].setEffectAssociation(0,association,0);
|
233
|
|
234
|
for(int i=1; i<MESHES; i++)
|
235
|
{
|
236
|
association <<= 1;
|
237
|
meshes[i] = meshes[0].copy(true);
|
238
|
meshes[i].setEffectAssociation(0,association,0);
|
239
|
}
|
240
|
|
241
|
MeshBase result = new MeshJoined(meshes);
|
242
|
|
243
|
Static3D a0 = new Static3D( 0, 1, 0 );
|
244
|
Static3D a1 = new Static3D( 0, -1.0f/3, 2*SQ2/3 );
|
245
|
Static3D a2 = new Static3D(-SQ2*SQ3/3, -1.0f/3, -SQ2/3 );
|
246
|
Static3D a3 = new Static3D( SQ2*SQ3/3, -1.0f/3, -SQ2/3 );
|
247
|
|
248
|
float tetraHeight = SQ2*SQ3/3;
|
249
|
float d1 = 0.75f*tetraHeight;
|
250
|
float d2 =-0.10f*tetraHeight;
|
251
|
float d3 = 0.20f*tetraHeight;
|
252
|
|
253
|
Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
|
254
|
Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
|
255
|
Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
|
256
|
Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
|
257
|
|
258
|
Static3D dVec0 = new Static3D( d2*a0.get0(), d2*a0.get1(), d2*a0.get2() );
|
259
|
Static3D dVec1 = new Static3D( d2*a1.get0(), d2*a1.get1(), d2*a1.get2() );
|
260
|
Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
|
261
|
Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
|
262
|
|
263
|
Static4D dReg = new Static4D(0,0,0,d3);
|
264
|
Static1D dRad = new Static1D(1);
|
265
|
|
266
|
Static1D angle = new Static1D(angleFaces);
|
267
|
Static3D axis1 = new Static3D( -1, 0, 0);
|
268
|
Static3D axis2 = new Static3D(0.5f, 0, -SQ3/2);
|
269
|
Static3D axis3 = new Static3D(0.5f, 0, +SQ3/2);
|
270
|
Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
|
271
|
Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
|
272
|
|
273
|
Static3D center = new Static3D(0,0,0);
|
274
|
Static4D region = new Static4D(0,0,0,0.6f);
|
275
|
|
276
|
VertexEffectScale effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
|
277
|
VertexEffectRotate effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
|
278
|
VertexEffectMove effect3 = new VertexEffectMove ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
|
279
|
VertexEffectRotate effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
|
280
|
VertexEffectRotate effect5 = new VertexEffectRotate( angle, axis1, center1 );
|
281
|
VertexEffectRotate effect6 = new VertexEffectRotate( angle, axis2, center2 );
|
282
|
VertexEffectRotate effect7 = new VertexEffectRotate( angle, axis3, center2 );
|
283
|
|
284
|
VertexEffectDeform effect8 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
|
285
|
VertexEffectDeform effect9 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
|
286
|
VertexEffectDeform effect10= new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
|
287
|
VertexEffectDeform effect11= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
|
288
|
|
289
|
VertexEffectSink effect12= new VertexEffectSink( new Static1D(1.3f), center, region );
|
290
|
|
291
|
effect4.setMeshAssociation(14,-1); // apply to mesh[1], [2] and [3]
|
292
|
effect5.setMeshAssociation( 2,-1); // apply only to mesh[1]
|
293
|
effect6.setMeshAssociation( 4,-1); // apply only to mesh[2]
|
294
|
effect7.setMeshAssociation( 8,-1); // apply only to mesh[3]
|
295
|
|
296
|
result.apply(effect1);
|
297
|
result.apply(effect2);
|
298
|
result.apply(effect3);
|
299
|
result.apply(effect4);
|
300
|
result.apply(effect5);
|
301
|
result.apply(effect6);
|
302
|
result.apply(effect7);
|
303
|
result.apply(effect8);
|
304
|
result.apply(effect9);
|
305
|
result.apply(effect10);
|
306
|
result.apply(effect11);
|
307
|
result.apply(effect12);
|
308
|
|
309
|
if( mRotArray[cubit]>=0 )
|
310
|
{
|
311
|
result.apply( ROTATION[mRotArray[cubit]] );
|
312
|
}
|
313
|
|
314
|
result.mergeEffComponents();
|
315
|
|
316
|
return result;
|
317
|
}
|
318
|
|
319
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
320
|
|
321
|
MeshBase createCubitMesh(int cubit)
|
322
|
{
|
323
|
int kind = mRotArray[cubit];
|
324
|
|
325
|
if( kind>=0 )
|
326
|
{
|
327
|
if( mMeshRotated[kind]==null ) mMeshRotated[kind] = createStaticMesh(cubit);
|
328
|
return mMeshRotated[kind].copy(true);
|
329
|
}
|
330
|
else
|
331
|
{
|
332
|
if( mMesh==null ) mMesh = createStaticMesh(cubit);
|
333
|
return mMesh.copy(true);
|
334
|
}
|
335
|
}
|
336
|
|
337
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
338
|
|
339
|
void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
|
340
|
{
|
341
|
float STROKE = 0.06f*side;
|
342
|
float OFF = STROKE/2 -1;
|
343
|
float OFF2 = 0.5f*side + OFF;
|
344
|
float HEIGHT = side - OFF;
|
345
|
float RADIUS = side/12.0f;
|
346
|
float ARC1_H = 0.2f*side;
|
347
|
float ARC1_W = side*0.5f;
|
348
|
float ARC2_W = 0.153f*side;
|
349
|
float ARC2_H = 0.905f*side;
|
350
|
float ARC3_W = side-ARC2_W;
|
351
|
|
352
|
paint.setAntiAlias(true);
|
353
|
paint.setStrokeWidth(STROKE);
|
354
|
paint.setColor(FACE_COLORS[face]);
|
355
|
paint.setStyle(Paint.Style.FILL);
|
356
|
|
357
|
canvas.drawRect(left,top,left+side,top+side,paint);
|
358
|
|
359
|
paint.setColor(INTERIOR_COLOR);
|
360
|
paint.setStyle(Paint.Style.STROKE);
|
361
|
|
362
|
canvas.drawLine( left, HEIGHT, side +left, HEIGHT, paint);
|
363
|
canvas.drawLine( OFF +left, side , OFF2 +left, 0, paint);
|
364
|
canvas.drawLine((side-OFF)+left, side , (side-OFF2) +left, 0, paint);
|
365
|
|
366
|
canvas.drawArc( ARC1_W-RADIUS+left, ARC1_H-RADIUS, ARC1_W+RADIUS+left, ARC1_H+RADIUS, 225, 90, false, paint);
|
367
|
canvas.drawArc( ARC2_W-RADIUS+left, ARC2_H-RADIUS, ARC2_W+RADIUS+left, ARC2_H+RADIUS, 105, 90, false, paint);
|
368
|
canvas.drawArc( ARC3_W-RADIUS+left, ARC2_H-RADIUS, ARC3_W+RADIUS+left, ARC2_H+RADIUS, 345, 90, false, paint);
|
369
|
}
|
370
|
|
371
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
372
|
// PUBLIC API
|
373
|
|
374
|
public Static3D[] getRotationAxis()
|
375
|
{
|
376
|
return AXIS;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
public int getBasicAngle()
|
382
|
{
|
383
|
return 3;
|
384
|
}
|
385
|
|
386
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
387
|
// I don't quite understand it, but 0.82 works better than the theoretically correct SQ3/2 ( 0.866 )
|
388
|
|
389
|
public float returnMultiplier()
|
390
|
{
|
391
|
return getSize()/0.82f;//(SQ3/2);
|
392
|
}
|
393
|
|
394
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
395
|
|
396
|
public float[] getRowChances()
|
397
|
{
|
398
|
int size = getSize();
|
399
|
int total = size*(size+1)/2;
|
400
|
float running=0.0f;
|
401
|
float[] chances = new float[size];
|
402
|
|
403
|
for(int i=0; i<size; i++)
|
404
|
{
|
405
|
running += (size-i);
|
406
|
chances[i] = running / total;
|
407
|
}
|
408
|
|
409
|
return chances;
|
410
|
}
|
411
|
|
412
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
413
|
|
414
|
public float returnRotationFactor(float offset)
|
415
|
{
|
416
|
int size = getSize();
|
417
|
int row = (int)(size*offset/(SQ3/2));
|
418
|
|
419
|
return ((float)size)/(size-row);
|
420
|
}
|
421
|
|
422
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
423
|
// TODO
|
424
|
|
425
|
public String retObjectString()
|
426
|
{
|
427
|
return "";
|
428
|
}
|
429
|
}
|