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