1 |
e844c116
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
1f9772f3
|
Leszek Koltunski
|
package org.distorted.objects;
|
21 |
e844c116
|
Leszek Koltunski
|
|
22 |
|
|
import android.graphics.Canvas;
|
23 |
|
|
import android.graphics.Paint;
|
24 |
|
|
|
25 |
988f434e
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffect;
|
26 |
|
|
import org.distorted.library.effect.MatrixEffectMove;
|
27 |
|
|
import org.distorted.library.effect.MatrixEffectRotate;
|
28 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
29 |
f0fa83ae
|
Leszek Koltunski
|
import org.distorted.library.effect.VertexEffectSink;
|
30 |
e844c116
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
31 |
|
|
import org.distorted.library.main.DistortedTexture;
|
32 |
|
|
import org.distorted.library.mesh.MeshBase;
|
33 |
988f434e
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshJoined;
|
34 |
e844c116
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshRectangles;
|
35 |
988f434e
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshTriangles;
|
36 |
|
|
import org.distorted.library.type.Static1D;
|
37 |
e844c116
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
38 |
|
|
import org.distorted.library.type.Static4D;
|
39 |
|
|
|
40 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
|
42 |
|
|
public class RubikPyraminx extends RubikObject
|
43 |
|
|
{
|
44 |
9f4c44fe
|
Leszek Koltunski
|
private static final float SQ2 = (float)Math.sqrt(2);
|
45 |
|
|
private static final float SQ3 = (float)Math.sqrt(3);
|
46 |
|
|
|
47 |
37a25788
|
Leszek Koltunski
|
static final Static3D[] AXIS = new Static3D[]
|
48 |
e844c116
|
Leszek Koltunski
|
{
|
49 |
9f4c44fe
|
Leszek Koltunski
|
new Static3D( 0, 1, 0 ),
|
50 |
49f67f9b
|
Leszek Koltunski
|
new Static3D( 0, -1.0f/3, 2*SQ2/3 ),
|
51 |
9f4c44fe
|
Leszek Koltunski
|
new Static3D(-SQ2*SQ3/3, -1.0f/3, -SQ2/3 ),
|
52 |
49f67f9b
|
Leszek Koltunski
|
new Static3D( SQ2*SQ3/3, -1.0f/3, -SQ2/3 )
|
53 |
e844c116
|
Leszek Koltunski
|
};
|
54 |
|
|
|
55 |
|
|
private static final int[] FACE_COLORS = new int[]
|
56 |
|
|
{
|
57 |
906cc928
|
Leszek Koltunski
|
0xff00ff00, 0xffffff00, // AXIS[0]right (GREEN ) AXIS[1]right (YELLOW )
|
58 |
|
|
0xff0000ff, 0xffff0000 // AXIS[2]right (BLUE ) AXIS[3]right (RED )
|
59 |
e844c116
|
Leszek Koltunski
|
};
|
60 |
|
|
|
61 |
9f4c44fe
|
Leszek Koltunski
|
// computed with res/raw/compute_quats.c
|
62 |
e844c116
|
Leszek Koltunski
|
private static final float[] LEGALQUATS = new float[]
|
63 |
|
|
{
|
64 |
9f4c44fe
|
Leszek Koltunski
|
0.0f, 1.0f, -1.0f, 0.5f, -0.5f, SQ2/2, -SQ2/2, SQ3/2, -SQ3/2,
|
65 |
|
|
SQ3/3, -SQ3/3, SQ3/6, -SQ3/6, SQ2*SQ3/3, -SQ2*SQ3/3, SQ2*SQ3/6, -SQ2*SQ3/6
|
66 |
e844c116
|
Leszek Koltunski
|
};
|
67 |
|
|
|
68 |
89a11f7b
|
Leszek Koltunski
|
private int[] mRotArray;
|
69 |
|
|
private static MatrixEffectRotate[][] ROTATION;
|
70 |
|
|
|
71 |
|
|
static
|
72 |
|
|
{
|
73 |
|
|
Static3D center = new Static3D(0,0,0);
|
74 |
|
|
Static1D angle = new Static1D(180.0f);
|
75 |
|
|
|
76 |
|
|
ROTATION = new MatrixEffectRotate[AXIS.length][1];
|
77 |
|
|
|
78 |
|
|
for(int i=0; i<AXIS.length; i++)
|
79 |
|
|
{
|
80 |
|
|
ROTATION[i][0] = new MatrixEffectRotate( angle, AXIS[i], center);
|
81 |
|
|
}
|
82 |
|
|
}
|
83 |
49f67f9b
|
Leszek Koltunski
|
|
84 |
e844c116
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85 |
|
|
|
86 |
a31d25de
|
Leszek Koltunski
|
RubikPyraminx(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects, int[][] moves)
|
87 |
e844c116
|
Leszek Koltunski
|
{
|
88 |
aa171dee
|
Leszek Koltunski
|
super(size, 30, quatCur,quatAcc,texture,mesh,effects,moves, RubikObjectList.PYRA);
|
89 |
e844c116
|
Leszek Koltunski
|
}
|
90 |
|
|
|
91 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92 |
|
|
|
93 |
89a11f7b
|
Leszek Koltunski
|
private void emitRow(float x, float y, float z, float dx, float dy, float dz, int n, int rot, Static3D[] array, int index)
|
94 |
49f67f9b
|
Leszek Koltunski
|
{
|
95 |
|
|
for(int i=0; i<n; i++)
|
96 |
|
|
{
|
97 |
|
|
mRotArray[i+index] = rot;
|
98 |
|
|
array[i+index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
|
99 |
|
|
x += dx;
|
100 |
|
|
y += dy;
|
101 |
|
|
z += dz;
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
|
|
|
107 |
|
|
private int emitLowermost(float x, float y, float z, int n, Static3D[] array)
|
108 |
|
|
{
|
109 |
|
|
int added = 0;
|
110 |
|
|
|
111 |
769409d2
|
Leszek Koltunski
|
emitRow( x +0.5f, y+SQ3*SQ2/9, z+ SQ3/18, 1.0f, 0, 0, n-1, 1, array, added);
|
112 |
|
|
added += (n-1);
|
113 |
|
|
emitRow( x +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, 0.5f, 0, SQ3/2, n-1, 3, array, added);
|
114 |
|
|
added += (n-1);
|
115 |
|
|
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);
|
116 |
|
|
added += (n-1);
|
117 |
|
|
|
118 |
49f67f9b
|
Leszek Koltunski
|
for(int i=n; i>=1; i--)
|
119 |
|
|
{
|
120 |
769409d2
|
Leszek Koltunski
|
emitRow(x , y, z , 1,0,0, i , -1, array, added);
|
121 |
49f67f9b
|
Leszek Koltunski
|
added += i;
|
122 |
769409d2
|
Leszek Koltunski
|
emitRow(x+0.5f, y, z+SQ3/6, 1,0,0, i-1, 0, array, added);
|
123 |
|
|
added += (i-1);
|
124 |
49f67f9b
|
Leszek Koltunski
|
x += 0.5f;
|
125 |
|
|
y += 0.0f;
|
126 |
|
|
z += SQ3/2;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
return added;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133 |
|
|
|
134 |
|
|
private int emitUpper(float x, float y, float z, int n, Static3D[] array, int index)
|
135 |
|
|
{
|
136 |
|
|
if( n>1 )
|
137 |
|
|
{
|
138 |
769409d2
|
Leszek Koltunski
|
emitRow( x , y , z , 1.0f, 0, 0, n-1, -1, array, index);
|
139 |
|
|
index += (n-1);
|
140 |
|
|
emitRow( x+0.5f , y+SQ3*SQ2/9, z+SQ3/18 , 1.0f, 0, 0, n-1, 1, array, index);
|
141 |
|
|
index += (n-1);
|
142 |
|
|
emitRow( x+0.5f , y , z+SQ3/2 , 0.5f, 0, SQ3/2, n-1, -1, array, index);
|
143 |
|
|
index += (n-1);
|
144 |
|
|
emitRow( x +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, 0.5f, 0, SQ3/2, n-1, 3, array, index);
|
145 |
49f67f9b
|
Leszek Koltunski
|
index += (n-1);
|
146 |
769409d2
|
Leszek Koltunski
|
emitRow( x+n-1 , y , z , -0.5f, 0, SQ3/2, n-1, -1, array, index);
|
147 |
49f67f9b
|
Leszek Koltunski
|
index += (n-1);
|
148 |
769409d2
|
Leszek Koltunski
|
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);
|
149 |
49f67f9b
|
Leszek Koltunski
|
index += (n-1);
|
150 |
|
|
}
|
151 |
|
|
else
|
152 |
|
|
{
|
153 |
89a11f7b
|
Leszek Koltunski
|
mRotArray[index] = -1;
|
154 |
49f67f9b
|
Leszek Koltunski
|
array[index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
|
155 |
|
|
index++;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
return index;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162 |
|
|
// size^2 + 3*(size-1) in the lowermost layer, then 6*(size-2) in the next, 6*(size-3) in the next,
|
163 |
|
|
// ... 6 in the forelast, 1 in the last = 4size^2 - 6size +4 (if size>1)
|
164 |
|
|
|
165 |
e844c116
|
Leszek Koltunski
|
Static3D[] getCubitPositions(int size)
|
166 |
|
|
{
|
167 |
769409d2
|
Leszek Koltunski
|
int numCubits = size>1 ? 4*size*size - 6*size +4 : 1;
|
168 |
c2cb520d
|
Leszek Koltunski
|
Static3D[] tmp = new Static3D[numCubits];
|
169 |
89a11f7b
|
Leszek Koltunski
|
mRotArray = new int[numCubits];
|
170 |
49f67f9b
|
Leszek Koltunski
|
|
171 |
|
|
int currentIndex = emitLowermost( -0.5f*size, -(SQ2*SQ3/12)*size, -(SQ3/6)*size, size, tmp);
|
172 |
c2cb520d
|
Leszek Koltunski
|
|
173 |
49f67f9b
|
Leszek Koltunski
|
for(int i=size-1; i>=1; i--)
|
174 |
cc7dc72a
|
Leszek Koltunski
|
{
|
175 |
49f67f9b
|
Leszek Koltunski
|
currentIndex = emitUpper( -0.5f*i, ((SQ2*SQ3)/12)*(3*size-4*i), -(SQ3/6)*i, i, tmp, currentIndex);
|
176 |
cc7dc72a
|
Leszek Koltunski
|
}
|
177 |
49f67f9b
|
Leszek Koltunski
|
|
178 |
c2cb520d
|
Leszek Koltunski
|
return tmp;
|
179 |
e844c116
|
Leszek Koltunski
|
}
|
180 |
|
|
|
181 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182 |
|
|
|
183 |
|
|
float[] getLegalQuats()
|
184 |
|
|
{
|
185 |
|
|
return LEGALQUATS;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
189 |
|
|
|
190 |
|
|
int getNumFaces()
|
191 |
|
|
{
|
192 |
|
|
return FACE_COLORS.length;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
f0fa83ae
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
196 |
|
|
|
197 |
|
|
float getScreenRatio()
|
198 |
|
|
{
|
199 |
7381193e
|
Leszek Koltunski
|
return 0.82f;
|
200 |
f0fa83ae
|
Leszek Koltunski
|
}
|
201 |
|
|
|
202 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203 |
|
|
|
204 |
|
|
VertexEffectSink getSink(int size)
|
205 |
|
|
{
|
206 |
|
|
Static3D center = new Static3D(0,0,0);
|
207 |
|
|
Static4D region = new Static4D(0,0,0,0.6f);
|
208 |
|
|
return new VertexEffectSink( new Static1D(1.3f), center, region );
|
209 |
|
|
}
|
210 |
|
|
|
211 |
e844c116
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
212 |
|
|
|
213 |
89a11f7b
|
Leszek Koltunski
|
MeshBase createCubitMesh(int cubit, int vertices)
|
214 |
e844c116
|
Leszek Koltunski
|
{
|
215 |
988f434e
|
Leszek Koltunski
|
final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
|
216 |
|
|
final int MESHES=4;
|
217 |
|
|
|
218 |
|
|
MeshBase[] meshes = new MeshTriangles[MESHES];
|
219 |
|
|
for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(5);
|
220 |
|
|
|
221 |
|
|
MatrixEffect[] effects0 = new MatrixEffect[3];
|
222 |
|
|
effects0[0] = new MatrixEffectScale( new Static3D(1,SQ3/2,1) );
|
223 |
|
|
effects0[1] = new MatrixEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
|
224 |
769409d2
|
Leszek Koltunski
|
effects0[2] = new MatrixEffectMove( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
|
225 |
988f434e
|
Leszek Koltunski
|
|
226 |
|
|
meshes[0].apply(effects0);
|
227 |
|
|
|
228 |
|
|
Static1D angle = new Static1D(angleFaces);
|
229 |
|
|
Static3D axis = new Static3D(-1,0,0);
|
230 |
769409d2
|
Leszek Koltunski
|
Static3D center= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
|
231 |
988f434e
|
Leszek Koltunski
|
|
232 |
|
|
MatrixEffect[] effects1 = new MatrixEffect[5];
|
233 |
|
|
effects1[0] = effects0[0];
|
234 |
|
|
effects1[1] = effects0[1];
|
235 |
|
|
effects1[2] = effects0[2];
|
236 |
|
|
effects1[3] = new MatrixEffectRotate( new Static1D(180), new Static3D(0,0,1), center );
|
237 |
|
|
effects1[4] = new MatrixEffectRotate( angle, axis, center );
|
238 |
|
|
meshes[1].apply(effects1);
|
239 |
|
|
|
240 |
|
|
axis.set(0.5f,0,-SQ3/2);
|
241 |
|
|
center.set2(SQ3/3);
|
242 |
|
|
meshes[2].apply(effects1);
|
243 |
|
|
|
244 |
|
|
axis.set2(SQ3/2);
|
245 |
|
|
meshes[3].apply(effects1);
|
246 |
|
|
|
247 |
89a11f7b
|
Leszek Koltunski
|
MeshJoined result = new MeshJoined(meshes);
|
248 |
|
|
|
249 |
|
|
if( mRotArray[cubit]>=0 )
|
250 |
|
|
{
|
251 |
|
|
result.apply( ROTATION[mRotArray[cubit]] );
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
return result;
|
255 |
e844c116
|
Leszek Koltunski
|
}
|
256 |
|
|
|
257 |
7289fd6c
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258 |
|
|
|
259 |
|
|
void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
|
260 |
|
|
{
|
261 |
|
|
float STROKE = 0.06f*side;
|
262 |
|
|
float OFF = STROKE/2 -1;
|
263 |
|
|
float OFF2 = 0.5f*side + OFF;
|
264 |
|
|
float HEIGHT = side - OFF;
|
265 |
|
|
float RADIUS = side/12.0f;
|
266 |
|
|
float ARC1_H = 0.2f*side;
|
267 |
|
|
float ARC1_W = side*0.5f;
|
268 |
|
|
float ARC2_W = 0.153f*side;
|
269 |
|
|
float ARC2_H = 0.905f*side;
|
270 |
|
|
float ARC3_W = side-ARC2_W;
|
271 |
|
|
|
272 |
|
|
paint.setAntiAlias(true);
|
273 |
|
|
paint.setStrokeWidth(STROKE);
|
274 |
|
|
paint.setColor(FACE_COLORS[face]);
|
275 |
|
|
paint.setStyle(Paint.Style.FILL);
|
276 |
|
|
|
277 |
|
|
canvas.drawRect(left,top,left+side,top+side,paint);
|
278 |
|
|
|
279 |
|
|
paint.setColor(0xff000000);
|
280 |
|
|
paint.setStyle(Paint.Style.STROKE);
|
281 |
|
|
|
282 |
|
|
canvas.drawLine( left, HEIGHT, side +left, HEIGHT, paint);
|
283 |
|
|
canvas.drawLine( OFF +left, side , OFF2 +left, 0, paint);
|
284 |
|
|
canvas.drawLine((side-OFF)+left, side , (side-OFF2) +left, 0, paint);
|
285 |
|
|
|
286 |
|
|
canvas.drawArc( ARC1_W-RADIUS+left, ARC1_H-RADIUS, ARC1_W+RADIUS+left, ARC1_H+RADIUS, 225, 90, false, paint);
|
287 |
|
|
canvas.drawArc( ARC2_W-RADIUS+left, ARC2_H-RADIUS, ARC2_W+RADIUS+left, ARC2_H+RADIUS, 105, 90, false, paint);
|
288 |
|
|
canvas.drawArc( ARC3_W-RADIUS+left, ARC2_H-RADIUS, ARC3_W+RADIUS+left, ARC2_H+RADIUS, 345, 90, false, paint);
|
289 |
|
|
}
|
290 |
|
|
|
291 |
e844c116
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
292 |
|
|
// PUBLIC API
|
293 |
|
|
|
294 |
12ad3fca
|
Leszek Koltunski
|
public Static3D[] getRotationAxis()
|
295 |
|
|
{
|
296 |
|
|
return AXIS;
|
297 |
|
|
}
|
298 |
|
|
|
299 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
300 |
|
|
|
301 |
e844c116
|
Leszek Koltunski
|
public int getBasicAngle()
|
302 |
|
|
{
|
303 |
|
|
return 3;
|
304 |
|
|
}
|
305 |
39e74052
|
Leszek Koltunski
|
|
306 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
307 |
|
|
|
308 |
9621255f
|
Leszek Koltunski
|
public float returnMultiplier()
|
309 |
39e74052
|
Leszek Koltunski
|
{
|
310 |
9621255f
|
Leszek Koltunski
|
return getSize()/(SQ3/2);
|
311 |
39e74052
|
Leszek Koltunski
|
}
|
312 |
e46e17fb
|
Leszek Koltunski
|
|
313 |
5cf34c5f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
314 |
|
|
|
315 |
|
|
public float[] getRowChances()
|
316 |
|
|
{
|
317 |
|
|
int size = getSize();
|
318 |
|
|
int total = size*(size+1)/2;
|
319 |
|
|
float running=0.0f;
|
320 |
|
|
float[] chances = new float[size];
|
321 |
|
|
|
322 |
|
|
for(int i=0; i<size; i++)
|
323 |
|
|
{
|
324 |
|
|
running += (size-i);
|
325 |
|
|
chances[i] = running / total;
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
return chances;
|
329 |
|
|
}
|
330 |
|
|
|
331 |
e46e17fb
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
332 |
|
|
|
333 |
|
|
public float returnRotationFactor(float offset)
|
334 |
|
|
{
|
335 |
|
|
int size = getSize();
|
336 |
|
|
int row = (int)(size*offset/(SQ3/2));
|
337 |
|
|
|
338 |
|
|
return ((float)size)/(size-row);
|
339 |
|
|
}
|
340 |
f0336037
|
Leszek Koltunski
|
|
341 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
342 |
|
|
// TODO
|
343 |
|
|
|
344 |
20931cf6
|
Leszek Koltunski
|
public String retObjectString()
|
345 |
f0336037
|
Leszek Koltunski
|
{
|
346 |
|
|
return "";
|
347 |
|
|
}
|
348 |
e844c116
|
Leszek Koltunski
|
}
|