1 |
29b82486
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2021 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Magic Cube. //
|
5 |
|
|
// //
|
6 |
a7a40b3c
|
Leszek Koltunski
|
// Magic Cube is proprietary software licensed under an EULA which you should have received //
|
7 |
|
|
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html //
|
8 |
29b82486
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9 |
|
|
|
10 |
198c5bf0
|
Leszek Koltunski
|
package org.distorted.objectlib.helpers;
|
11 |
29b82486
|
Leszek Koltunski
|
|
12 |
aacf5e27
|
Leszek Koltunski
|
import org.distorted.library.helpers.QuatHelper;
|
13 |
9b1fe915
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
14 |
5931ae4d
|
Leszek Koltunski
|
import org.distorted.library.type.Static4D;
|
15 |
|
|
import org.distorted.objectlib.main.TwistyObject;
|
16 |
|
|
|
17 |
29b82486
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
18 |
|
|
|
19 |
|
|
public class ObjectShape
|
20 |
|
|
{
|
21 |
9b1fe915
|
Leszek Koltunski
|
private static final float[] mTmp1 = new float[4];
|
22 |
|
|
private static final float[] mTmp2 = new float[4];
|
23 |
|
|
|
24 |
57ef6378
|
Leszek Koltunski
|
private final float[][] mVertices;
|
25 |
29b82486
|
Leszek Koltunski
|
private final int[][] mVertIndices;
|
26 |
|
|
|
27 |
fe3dec09
|
Leszek Koltunski
|
private final boolean mFacesMultigon;
|
28 |
|
|
private final int mNumFaces;
|
29 |
|
|
private final int[][][] mMultigonIndices;
|
30 |
|
|
|
31 |
29b82486
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32 |
|
|
|
33 |
59a971c1
|
Leszek Koltunski
|
public ObjectShape(float[][] vertices, int[][] vertIndices)
|
34 |
29b82486
|
Leszek Koltunski
|
{
|
35 |
fe3dec09
|
Leszek Koltunski
|
mVertices = vertices;
|
36 |
|
|
mVertIndices = vertIndices;
|
37 |
|
|
mMultigonIndices = null;
|
38 |
|
|
mFacesMultigon = false;
|
39 |
|
|
mNumFaces = vertIndices.length;
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43 |
|
|
|
44 |
|
|
public ObjectShape(float[][] vertices, int[][][] vertIndices)
|
45 |
|
|
{
|
46 |
|
|
mVertices = vertices;
|
47 |
|
|
mVertIndices = null;
|
48 |
|
|
mMultigonIndices= vertIndices;
|
49 |
|
|
mFacesMultigon = true;
|
50 |
|
|
mNumFaces = vertIndices.length;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
b76da174
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
|
|
public void debug()
|
56 |
|
|
{
|
57 |
|
|
StringBuilder str = new StringBuilder();
|
58 |
|
|
str.append("isMultigon: ");
|
59 |
|
|
str.append(mFacesMultigon);
|
60 |
|
|
str.append("\n");
|
61 |
|
|
str.append("numVertices: ");
|
62 |
|
|
int numV =mVertices.length;
|
63 |
|
|
str.append(numV);
|
64 |
3821ebef
|
leszek
|
str.append("\n");
|
65 |
b76da174
|
leszek
|
|
66 |
|
|
for(float[] v : mVertices)
|
67 |
|
|
{
|
68 |
94c50a70
|
leszek
|
str.append(" {");
|
69 |
b76da174
|
leszek
|
str.append(v[0]);
|
70 |
94c50a70
|
leszek
|
str.append("f, ");
|
71 |
b76da174
|
leszek
|
str.append(v[1]);
|
72 |
94c50a70
|
leszek
|
str.append("f, ");
|
73 |
b76da174
|
leszek
|
str.append(v[2]);
|
74 |
94c50a70
|
leszek
|
str.append("f },\n");
|
75 |
b76da174
|
leszek
|
}
|
76 |
|
|
|
77 |
|
|
if( mFacesMultigon )
|
78 |
|
|
{
|
79 |
|
|
int numFaces =mMultigonIndices.length;
|
80 |
94c50a70
|
leszek
|
str.append("numFaces: ");
|
81 |
b76da174
|
leszek
|
str.append(numFaces);
|
82 |
|
|
str.append("\n");
|
83 |
|
|
|
84 |
|
|
for(int[][] mMultigonIndex : mMultigonIndices)
|
85 |
|
|
{
|
86 |
|
|
for(int[] multigonIndex : mMultigonIndex)
|
87 |
|
|
{
|
88 |
94c50a70
|
leszek
|
int len = multigonIndex.length;
|
89 |
|
|
str.append(" {");
|
90 |
|
|
|
91 |
|
|
for(int i=0; i<len; i++)
|
92 |
b76da174
|
leszek
|
{
|
93 |
94c50a70
|
leszek
|
str.append( i==0 ? " " : ", ");
|
94 |
|
|
str.append(multigonIndex[i]);
|
95 |
b76da174
|
leszek
|
}
|
96 |
94c50a70
|
leszek
|
str.append("},");
|
97 |
b76da174
|
leszek
|
}
|
98 |
3821ebef
|
leszek
|
str.append("\n");
|
99 |
b76da174
|
leszek
|
}
|
100 |
|
|
}
|
101 |
|
|
else
|
102 |
|
|
{
|
103 |
|
|
int numFaces =mVertIndices.length;
|
104 |
|
|
str.append("\nnumFaces: ");
|
105 |
|
|
str.append(numFaces);
|
106 |
|
|
str.append("\n ");
|
107 |
|
|
|
108 |
|
|
for(int[] vertIndex : mVertIndices)
|
109 |
|
|
{
|
110 |
|
|
for(int index : vertIndex)
|
111 |
|
|
{
|
112 |
|
|
str.append(" ");
|
113 |
|
|
str.append(index);
|
114 |
|
|
}
|
115 |
|
|
str.append("\n ");
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
android.util.Log.e("D", "ObjectShape: \n"+str);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
fe3dec09
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
123 |
|
|
|
124 |
|
|
public int getNumFaces()
|
125 |
|
|
{
|
126 |
|
|
return mNumFaces;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130 |
|
|
|
131 |
|
|
public boolean isMultigon()
|
132 |
|
|
{
|
133 |
|
|
return mFacesMultigon;
|
134 |
29b82486
|
Leszek Koltunski
|
}
|
135 |
|
|
|
136 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
137 |
|
|
|
138 |
57ef6378
|
Leszek Koltunski
|
public float[][] getVertices()
|
139 |
29b82486
|
Leszek Koltunski
|
{
|
140 |
|
|
return mVertices;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144 |
|
|
|
145 |
|
|
public int[][] getVertIndices()
|
146 |
|
|
{
|
147 |
|
|
return mVertIndices;
|
148 |
|
|
}
|
149 |
|
|
|
150 |
fe3dec09
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151 |
|
|
|
152 |
|
|
public int[][][] getMultigonIndices()
|
153 |
|
|
{
|
154 |
|
|
return mMultigonIndices;
|
155 |
|
|
}
|
156 |
|
|
|
157 |
efa5bc1e
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158 |
|
|
|
159 |
|
|
public float[] getFirstVertexInFace(int face)
|
160 |
|
|
{
|
161 |
|
|
if( mFacesMultigon )
|
162 |
|
|
{
|
163 |
|
|
int[][][] indices=getMultigonIndices();
|
164 |
|
|
int vertIndex=indices[face][0][0];
|
165 |
|
|
return getVertices()[vertIndex];
|
166 |
|
|
}
|
167 |
|
|
else
|
168 |
|
|
{
|
169 |
|
|
int[][] indices=getVertIndices();
|
170 |
|
|
int vertIndex=indices[face][0];
|
171 |
|
|
return getVertices()[vertIndex];
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
|
175 |
adb5b717
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176 |
|
|
|
177 |
|
|
private void computeNormalVector(float[] v0, float[] v1, float[] v2, float[] output)
|
178 |
|
|
{
|
179 |
|
|
float x1 = v0[0];
|
180 |
|
|
float y1 = v0[1];
|
181 |
|
|
float z1 = v0[2];
|
182 |
|
|
float x2 = v1[0];
|
183 |
|
|
float y2 = v1[1];
|
184 |
|
|
float z2 = v1[2];
|
185 |
|
|
float x3 = v2[0];
|
186 |
|
|
float y3 = v2[1];
|
187 |
|
|
float z3 = v2[2];
|
188 |
|
|
|
189 |
|
|
float v1x = x2-x1;
|
190 |
|
|
float v1y = y2-y1;
|
191 |
|
|
float v1z = z2-z1;
|
192 |
|
|
float v2x = x3-x1;
|
193 |
|
|
float v2y = y3-y1;
|
194 |
|
|
float v2z = z3-z1;
|
195 |
|
|
|
196 |
|
|
output[0] = v1y*v2z - v2y*v1z;
|
197 |
|
|
output[1] = v1z*v2x - v2z*v1x;
|
198 |
|
|
output[2] = v1x*v2y - v2x*v1y;
|
199 |
|
|
|
200 |
|
|
double len = output[0]*output[0] + output[1]*output[1] + output[2]*output[2];
|
201 |
|
|
len = Math.sqrt(len);
|
202 |
|
|
output[0] /= len;
|
203 |
|
|
output[1] /= len;
|
204 |
|
|
output[2] /= len;
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208 |
|
|
// return surface defined by the face, i.e. a 4-tuple [ (nx,ny,nz), d ]
|
209 |
|
|
|
210 |
f80b9473
|
leszek
|
public void getFaceNormal(int face, float[] output)
|
211 |
adb5b717
|
leszek
|
{
|
212 |
|
|
int[] loopI = mFacesMultigon ? getMultigonIndices()[face][0] : getVertIndices()[face];
|
213 |
|
|
|
214 |
|
|
float[] v0 = mVertices[loopI[0]];
|
215 |
|
|
float[] v1 = mVertices[loopI[1]];
|
216 |
|
|
float[] v2 = mVertices[loopI[2]];
|
217 |
|
|
|
218 |
|
|
computeNormalVector(v0,v1,v2,output);
|
219 |
f80b9473
|
leszek
|
}
|
220 |
|
|
|
221 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
222 |
|
|
// return any point laying inside the face
|
223 |
|
|
|
224 |
|
|
public void getFacePoint(int face, float[] output)
|
225 |
|
|
{
|
226 |
|
|
int[] loopI = mFacesMultigon ? getMultigonIndices()[face][0] : getVertIndices()[face];
|
227 |
|
|
|
228 |
|
|
float[] v0 = mVertices[loopI[0]];
|
229 |
|
|
float[] v1 = mVertices[loopI[1]];
|
230 |
|
|
float[] v2 = mVertices[loopI[2]];
|
231 |
adb5b717
|
leszek
|
|
232 |
f80b9473
|
leszek
|
output[0] = (v0[0] + v1[0] + v2[0]) / 3;
|
233 |
|
|
output[1] = (v0[1] + v1[1] + v2[1]) / 3;
|
234 |
|
|
output[2] = (v0[2] + v1[2] + v2[2]) / 3;
|
235 |
adb5b717
|
leszek
|
}
|
236 |
|
|
|
237 |
c187cb69
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238 |
|
|
|
239 |
ac97ecc0
|
Leszek Koltunski
|
public static int computeNumComponents(ObjectShape[] shapes)
|
240 |
c187cb69
|
Leszek Koltunski
|
{
|
241 |
ac97ecc0
|
Leszek Koltunski
|
int ret = 0;
|
242 |
|
|
|
243 |
|
|
for( ObjectShape shape : shapes )
|
244 |
|
|
{
|
245 |
89c2b479
|
Leszek Koltunski
|
int numShape = shape.mNumFaces;
|
246 |
ac97ecc0
|
Leszek Koltunski
|
if( numShape>ret ) ret = numShape;
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
return ret;
|
250 |
c187cb69
|
Leszek Koltunski
|
}
|
251 |
5931ae4d
|
Leszek Koltunski
|
|
252 |
9b1fe915
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
253 |
|
|
// take vertex, apply quat, apply move, write to output
|
254 |
|
|
|
255 |
|
|
private static void computeVertex(float[] output, float[] vertex, float[] move, Static4D quat)
|
256 |
|
|
{
|
257 |
|
|
QuatHelper.rotateVectorByQuat(mTmp2,vertex[0],vertex[1],vertex[2],1.0f,quat);
|
258 |
|
|
|
259 |
|
|
int numMoves = move.length/3;
|
260 |
|
|
float moveX=0.0f, moveY=0.0f, moveZ=0.0f;
|
261 |
|
|
|
262 |
|
|
for(int m=0; m<numMoves; m++)
|
263 |
|
|
{
|
264 |
|
|
moveX += move[3*m ];
|
265 |
|
|
moveY += move[3*m+1];
|
266 |
|
|
moveZ += move[3*m+2];
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
output[0] = mTmp2[0] + moveX/numMoves;
|
270 |
|
|
output[1] = mTmp2[1] + moveY/numMoves;
|
271 |
|
|
output[2] = mTmp2[2] + moveZ/numMoves;
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
275 |
|
|
|
276 |
|
|
private static boolean vertInFace(float[] vertex, Static3D faceAxis, float dist)
|
277 |
|
|
{
|
278 |
802fe251
|
Leszek Koltunski
|
final float MAX_ERROR = 0.04f; // Rex Cube requires this
|
279 |
9b1fe915
|
Leszek Koltunski
|
|
280 |
|
|
float x= faceAxis.get0();
|
281 |
|
|
float y= faceAxis.get1();
|
282 |
|
|
float z= faceAxis.get2();
|
283 |
|
|
|
284 |
|
|
float a = vertex[0]*x + vertex[1]*y + vertex[2]*z;
|
285 |
|
|
float diff = a - dist;
|
286 |
|
|
|
287 |
|
|
return diff>-MAX_ERROR && diff<MAX_ERROR;
|
288 |
|
|
}
|
289 |
|
|
|
290 |
5931ae4d
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
291 |
|
|
|
292 |
89c2b479
|
Leszek Koltunski
|
private static boolean indicesContain(int[][] indices, int index)
|
293 |
|
|
{
|
294 |
|
|
for( int[] ind : indices )
|
295 |
|
|
for( int i : ind )
|
296 |
|
|
if( i==index ) return true;
|
297 |
|
|
|
298 |
|
|
return false;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
302 |
|
|
|
303 |
|
|
private static boolean indicesContain(int[] indices, int index)
|
304 |
5931ae4d
|
Leszek Koltunski
|
{
|
305 |
9b1fe915
|
Leszek Koltunski
|
for( int j : indices )
|
306 |
|
|
if( j==index ) return true;
|
307 |
|
|
|
308 |
|
|
return false;
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
312 |
|
|
|
313 |
|
|
private static int[] computeCubitFaceColors(ObjectShape shape, float[] move, Static4D quat, Static3D[] faceAxis, float[] dist3D, float size)
|
314 |
|
|
{
|
315 |
|
|
float[][] vertices = shape.getVertices();
|
316 |
|
|
int numVert = vertices.length;
|
317 |
89c2b479
|
Leszek Koltunski
|
int numPuzzleFaces = faceAxis.length;
|
318 |
|
|
int numCubitFaces = shape.mNumFaces;
|
319 |
|
|
int[] cubitFaceColor = new int[numCubitFaces];
|
320 |
|
|
for(int face=0; face<numCubitFaces; face++) cubitFaceColor[face] = 0xffffffff;
|
321 |
9b1fe915
|
Leszek Koltunski
|
|
322 |
|
|
for(int vert=0; vert<numVert; vert++)
|
323 |
|
|
{
|
324 |
|
|
computeVertex(mTmp1,vertices[vert],move,quat);
|
325 |
|
|
int vertBelongsBitmap = 0x00000000;
|
326 |
|
|
|
327 |
89c2b479
|
Leszek Koltunski
|
for(int face=0; face<numPuzzleFaces; face++)
|
328 |
9b1fe915
|
Leszek Koltunski
|
if( vertInFace(mTmp1,faceAxis[face],dist3D[face]*size) ) vertBelongsBitmap |= (1<<face);
|
329 |
|
|
|
330 |
89c2b479
|
Leszek Koltunski
|
if( shape.mFacesMultigon )
|
331 |
|
|
{
|
332 |
|
|
for(int index=0; index<numCubitFaces; index++)
|
333 |
|
|
if( cubitFaceColor[index]!=0 && indicesContain(shape.mMultigonIndices[index],vert) ) cubitFaceColor[index] &= vertBelongsBitmap;
|
334 |
|
|
}
|
335 |
|
|
else
|
336 |
|
|
{
|
337 |
|
|
for(int index=0; index<numCubitFaces; index++)
|
338 |
|
|
if( cubitFaceColor[index]!=0 && indicesContain(shape.mVertIndices[index],vert) ) cubitFaceColor[index] &= vertBelongsBitmap;
|
339 |
|
|
}
|
340 |
9b1fe915
|
Leszek Koltunski
|
}
|
341 |
|
|
|
342 |
|
|
return cubitFaceColor;
|
343 |
|
|
}
|
344 |
|
|
|
345 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
346 |
|
|
|
347 |
|
|
private static void translateFromBitmap(int cubit, int[] colorsBitmap)
|
348 |
|
|
{
|
349 |
|
|
int len = colorsBitmap.length;
|
350 |
|
|
|
351 |
|
|
for(int face=0; face<len; face++)
|
352 |
|
|
{
|
353 |
|
|
if( colorsBitmap[face]==0 ) colorsBitmap[face] = -1;
|
354 |
|
|
else
|
355 |
|
|
{
|
356 |
|
|
int shift=0;
|
357 |
|
|
|
358 |
|
|
while( (colorsBitmap[face]&0x1) != 1 )
|
359 |
|
|
{
|
360 |
|
|
colorsBitmap[face]>>=1;
|
361 |
|
|
shift++;
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
if( colorsBitmap[face]!=1 )
|
365 |
|
|
{
|
366 |
f80b9473
|
leszek
|
android.util.Log.e("ObjectShape", "ERROR, cubit= "+cubit+" face "+face+" seems to belong to "+shift+" and still "+colorsBitmap[face]);
|
367 |
9b1fe915
|
Leszek Koltunski
|
}
|
368 |
|
|
|
369 |
|
|
colorsBitmap[face] = shift;
|
370 |
|
|
}
|
371 |
|
|
}
|
372 |
5931ae4d
|
Leszek Koltunski
|
}
|
373 |
|
|
|
374 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
375 |
|
|
|
376 |
9b1fe915
|
Leszek Koltunski
|
public static int[][] computeColors(ObjectShape[] shapes, float[][] moves, Static4D[] quats, TwistyObject object)
|
377 |
5931ae4d
|
Leszek Koltunski
|
{
|
378 |
9b1fe915
|
Leszek Koltunski
|
int numCubits = moves.length;
|
379 |
5931ae4d
|
Leszek Koltunski
|
int[][] colors = new int[numCubits][];
|
380 |
|
|
int[] numLayers = object.getNumLayers();
|
381 |
9b1fe915
|
Leszek Koltunski
|
Static3D[] faceAxis = object.getFaceAxis();
|
382 |
|
|
float[] dist3D = object.getDist3D(numLayers);
|
383 |
|
|
float size = object.getSize();
|
384 |
5931ae4d
|
Leszek Koltunski
|
|
385 |
|
|
for(int cubit=0; cubit<numCubits; cubit++)
|
386 |
|
|
{
|
387 |
|
|
int variant = object.getCubitVariant(cubit,numLayers);
|
388 |
9b1fe915
|
Leszek Koltunski
|
colors[cubit] = computeCubitFaceColors(shapes[variant],moves[cubit],quats[cubit],faceAxis,dist3D,size);
|
389 |
|
|
translateFromBitmap(cubit,colors[cubit]);
|
390 |
5931ae4d
|
Leszek Koltunski
|
}
|
391 |
|
|
|
392 |
|
|
return colors;
|
393 |
|
|
}
|
394 |
29b82486
|
Leszek Koltunski
|
}
|