1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2021 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// 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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
9
|
|
10
|
package org.distorted.objectlib.helpers;
|
11
|
|
12
|
import org.distorted.library.helpers.QuatHelper;
|
13
|
import org.distorted.library.type.Static3D;
|
14
|
import org.distorted.library.type.Static4D;
|
15
|
import org.distorted.objectlib.main.TwistyObject;
|
16
|
|
17
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
18
|
|
19
|
public class ObjectShape
|
20
|
{
|
21
|
private static final float[] mTmp1 = new float[4];
|
22
|
private static final float[] mTmp2 = new float[4];
|
23
|
|
24
|
private final float[][] mVertices;
|
25
|
private final int[][] mVertIndices;
|
26
|
|
27
|
private final boolean mFacesMultigon;
|
28
|
private final int mNumFaces;
|
29
|
private final int[][][] mMultigonIndices;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
public ObjectShape(float[][] vertices, int[][] vertIndices)
|
34
|
{
|
35
|
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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54
|
|
55
|
public int getNumFaces()
|
56
|
{
|
57
|
return mNumFaces;
|
58
|
}
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
|
62
|
public boolean isMultigon()
|
63
|
{
|
64
|
return mFacesMultigon;
|
65
|
}
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
public float[][] getVertices()
|
70
|
{
|
71
|
return mVertices;
|
72
|
}
|
73
|
|
74
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
75
|
|
76
|
public int[][] getVertIndices()
|
77
|
{
|
78
|
return mVertIndices;
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
public int[][][] getMultigonIndices()
|
84
|
{
|
85
|
return mMultigonIndices;
|
86
|
}
|
87
|
|
88
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89
|
|
90
|
public static int computeNumComponents(ObjectShape[] shapes)
|
91
|
{
|
92
|
int ret = 0;
|
93
|
|
94
|
for( ObjectShape shape : shapes )
|
95
|
{
|
96
|
int numShape = shape.mVertIndices.length;
|
97
|
if( numShape>ret ) ret = numShape;
|
98
|
}
|
99
|
|
100
|
return ret;
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
// take vertex, apply quat, apply move, write to output
|
105
|
|
106
|
private static void computeVertex(float[] output, float[] vertex, float[] move, Static4D quat)
|
107
|
{
|
108
|
QuatHelper.rotateVectorByQuat(mTmp2,vertex[0],vertex[1],vertex[2],1.0f,quat);
|
109
|
|
110
|
int numMoves = move.length/3;
|
111
|
float moveX=0.0f, moveY=0.0f, moveZ=0.0f;
|
112
|
|
113
|
for(int m=0; m<numMoves; m++)
|
114
|
{
|
115
|
moveX += move[3*m ];
|
116
|
moveY += move[3*m+1];
|
117
|
moveZ += move[3*m+2];
|
118
|
}
|
119
|
|
120
|
output[0] = mTmp2[0] + moveX/numMoves;
|
121
|
output[1] = mTmp2[1] + moveY/numMoves;
|
122
|
output[2] = mTmp2[2] + moveZ/numMoves;
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
private static boolean vertInFace(float[] vertex, Static3D faceAxis, float dist)
|
128
|
{
|
129
|
final float MAX_ERROR = 0.04f; // Rex Cube requires this
|
130
|
|
131
|
float x= faceAxis.get0();
|
132
|
float y= faceAxis.get1();
|
133
|
float z= faceAxis.get2();
|
134
|
|
135
|
float a = vertex[0]*x + vertex[1]*y + vertex[2]*z;
|
136
|
float diff = a - dist;
|
137
|
|
138
|
return diff>-MAX_ERROR && diff<MAX_ERROR;
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
|
143
|
private static boolean indexContains(int[] indices, int index)
|
144
|
{
|
145
|
for( int j : indices )
|
146
|
if( j==index ) return true;
|
147
|
|
148
|
return false;
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
private static int[] computeCubitFaceColors(ObjectShape shape, float[] move, Static4D quat, Static3D[] faceAxis, float[] dist3D, float size)
|
154
|
{
|
155
|
float[][] vertices = shape.getVertices();
|
156
|
int[][] indices = shape.getVertIndices();
|
157
|
int numVert = vertices.length;
|
158
|
int numFaces = faceAxis.length;
|
159
|
int numIndices = indices.length;
|
160
|
int[] cubitFaceColor = new int[numIndices];
|
161
|
for(int face=0; face<numIndices; face++) cubitFaceColor[face] = 0xffffffff;
|
162
|
|
163
|
for(int vert=0; vert<numVert; vert++)
|
164
|
{
|
165
|
computeVertex(mTmp1,vertices[vert],move,quat);
|
166
|
int vertBelongsBitmap = 0x00000000;
|
167
|
|
168
|
for(int face=0; face<numFaces; face++)
|
169
|
if( vertInFace(mTmp1,faceAxis[face],dist3D[face]*size) ) vertBelongsBitmap |= (1<<face);
|
170
|
|
171
|
for(int index=0; index<numIndices; index++)
|
172
|
if( cubitFaceColor[index]!=0 && indexContains(indices[index],vert) ) cubitFaceColor[index] &= vertBelongsBitmap;
|
173
|
}
|
174
|
|
175
|
return cubitFaceColor;
|
176
|
}
|
177
|
|
178
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
179
|
|
180
|
private static void translateFromBitmap(int cubit, int[] colorsBitmap)
|
181
|
{
|
182
|
int len = colorsBitmap.length;
|
183
|
|
184
|
for(int face=0; face<len; face++)
|
185
|
{
|
186
|
if( colorsBitmap[face]==0 ) colorsBitmap[face] = -1;
|
187
|
else
|
188
|
{
|
189
|
int shift=0;
|
190
|
|
191
|
while( (colorsBitmap[face]&0x1) != 1 )
|
192
|
{
|
193
|
colorsBitmap[face]>>=1;
|
194
|
shift++;
|
195
|
}
|
196
|
|
197
|
if( colorsBitmap[face]!=1 )
|
198
|
{
|
199
|
android.util.Log.e("D", "ERROR, cubit= "+cubit+" face "+face+" seems to belong to "+shift+" and still "+colorsBitmap[face]);
|
200
|
}
|
201
|
|
202
|
colorsBitmap[face] = shift;
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
|
209
|
public static int[][] computeColors(ObjectShape[] shapes, float[][] moves, Static4D[] quats, TwistyObject object)
|
210
|
{
|
211
|
int numCubits = moves.length;
|
212
|
int[][] colors = new int[numCubits][];
|
213
|
int[] numLayers = object.getNumLayers();
|
214
|
Static3D[] faceAxis = object.getFaceAxis();
|
215
|
float[] dist3D = object.getDist3D(numLayers);
|
216
|
float size = object.getSize();
|
217
|
|
218
|
for(int cubit=0; cubit<numCubits; cubit++)
|
219
|
{
|
220
|
int variant = object.getCubitVariant(cubit,numLayers);
|
221
|
colors[cubit] = computeCubitFaceColors(shapes[variant],moves[cubit],quats[cubit],faceAxis,dist3D,size);
|
222
|
translateFromBitmap(cubit,colors[cubit]);
|
223
|
}
|
224
|
|
225
|
return colors;
|
226
|
}
|
227
|
}
|