1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2023 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.bandaged;
|
11
|
|
12
|
import org.distorted.library.main.DistortedNode;
|
13
|
import org.distorted.library.main.DistortedScreen;
|
14
|
import org.distorted.library.mesh.MeshBase;
|
15
|
import org.distorted.library.type.Static3D;
|
16
|
import org.distorted.library.type.Static4D;
|
17
|
import org.distorted.objectlib.main.TwistyObject;
|
18
|
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
public abstract class BandagedObject
|
22
|
{
|
23
|
private final DistortedScreen mScreen;
|
24
|
private final float[][] mFaceAxis;
|
25
|
private final float[] mTmp;
|
26
|
private BandagedCubit[] mCubits;
|
27
|
|
28
|
final int[] mSize;
|
29
|
final float mDist2D;
|
30
|
|
31
|
int mMax;
|
32
|
int mNumCubits;
|
33
|
|
34
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
35
|
|
36
|
BandagedObject(DistortedScreen screen)
|
37
|
{
|
38
|
mScreen = screen;
|
39
|
mSize = new int[3];
|
40
|
mDist2D = getDist2D();
|
41
|
mTmp = new float[3];
|
42
|
Static3D[] axis = getFaceAxis();
|
43
|
int numAxis = axis.length;
|
44
|
mFaceAxis = new float[numAxis][];
|
45
|
for(int i=0; i<numAxis; i++) mFaceAxis[i] = new float[] { axis[i].get0(), axis[i].get1(), axis[1].get2() };
|
46
|
}
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
abstract float getDist2D();
|
51
|
abstract float[] getDist3D();
|
52
|
abstract int[] getColors();
|
53
|
abstract Static3D[] getFaceAxis();
|
54
|
abstract float[][] getPositions();
|
55
|
abstract boolean isAdjacent(float dx, float dy, float dz);
|
56
|
abstract int computeProjectionAngle();
|
57
|
abstract boolean tryChangeObject(int x, int y, int z);
|
58
|
abstract void getTouchedPosition(float[] output, int face, float pointX, float pointY);
|
59
|
abstract boolean isInsideFace(int face, float[] p);
|
60
|
abstract TwistyObject createObject(int mode, float scale );
|
61
|
abstract MeshBase createMesh(float[] pos, boolean round);
|
62
|
|
63
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
64
|
|
65
|
int[] getSize()
|
66
|
{
|
67
|
return mSize;
|
68
|
}
|
69
|
|
70
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
71
|
|
72
|
void createCubits(Static4D quatT, Static4D quatA, Static3D scale)
|
73
|
{
|
74
|
mCubits = new BandagedCubit[mNumCubits];
|
75
|
int c=0;
|
76
|
for(float[] p : getPositions() ) mCubits[c++] = new BandagedCubit(this,p,quatT,quatA,scale,false);
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
int computeMapsIndex(float[] faceAx)
|
82
|
{
|
83
|
int min=-1, numAxis = mFaceAxis.length;
|
84
|
float error = Float.MAX_VALUE;
|
85
|
float x = faceAx[0];
|
86
|
float y = faceAx[1];
|
87
|
float z = faceAx[2];
|
88
|
|
89
|
for(int i=0; i<numAxis; i++)
|
90
|
{
|
91
|
float[] ax = mFaceAxis[i];
|
92
|
float dx = x-ax[0];
|
93
|
float dy = y-ax[1];
|
94
|
float dz = z-ax[2];
|
95
|
|
96
|
float diff = dx*dx + dy*dy + dz*dz;
|
97
|
|
98
|
if( error>diff )
|
99
|
{
|
100
|
error = diff;
|
101
|
min = i;
|
102
|
}
|
103
|
}
|
104
|
|
105
|
return min;
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
void resetObject(float scale)
|
111
|
{
|
112
|
for(int c=0; c<mNumCubits; c++)
|
113
|
{
|
114
|
if( !mCubits[c].isAttached() )
|
115
|
{
|
116
|
mCubits[c].attach();
|
117
|
mScreen.attach(mCubits[c].getNode());
|
118
|
}
|
119
|
if( mCubits[c].getPosition().length>3 )
|
120
|
{
|
121
|
mCubits[c].reset(scale);
|
122
|
}
|
123
|
mCubits[c].setUnmarked();
|
124
|
}
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
float getMaxSize()
|
130
|
{
|
131
|
return mMax;
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
float[][] getCubitPositions()
|
137
|
{
|
138
|
int numAttached = 0;
|
139
|
|
140
|
for(int i=0; i<mNumCubits; i++)
|
141
|
if( mCubits[i].isAttached() ) numAttached++;
|
142
|
|
143
|
float[][] pos = new float[numAttached][];
|
144
|
int attached=0;
|
145
|
|
146
|
for(int i=0; i<mNumCubits; i++)
|
147
|
if( mCubits[i].isAttached() )
|
148
|
{
|
149
|
pos[attached++] = mCubits[i].getPosition();
|
150
|
}
|
151
|
|
152
|
return pos;
|
153
|
}
|
154
|
|
155
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
156
|
|
157
|
void tryConnectingCubits(int index1, int index2, float scale)
|
158
|
{
|
159
|
if( index1!=index2 )
|
160
|
{
|
161
|
float[] pos1 = mCubits[index1].getPosition();
|
162
|
float[] pos2 = mCubits[index2].getPosition();
|
163
|
|
164
|
if( isAdjacent(pos1,pos2) )
|
165
|
{
|
166
|
mCubits[index2].join(pos1,scale);
|
167
|
mCubits[index1].detach();
|
168
|
mScreen.detach(mCubits[index1].getNode());
|
169
|
}
|
170
|
}
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
void attachCubits(float scale)
|
176
|
{
|
177
|
for(int i=0; i<mNumCubits; i++)
|
178
|
{
|
179
|
mCubits[i].scaleMove(scale);
|
180
|
DistortedNode node = mCubits[i].getNode();
|
181
|
mScreen.attach(node);
|
182
|
}
|
183
|
}
|
184
|
|
185
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
186
|
|
187
|
void attachAndMarkCubits(float scale, int touched)
|
188
|
{
|
189
|
for(int i=0; i<mNumCubits; i++)
|
190
|
{
|
191
|
if( mCubits[i].isAttached() )
|
192
|
{
|
193
|
mCubits[i].scaleMove(scale);
|
194
|
if( touched==i ) mCubits[i].setMarked();
|
195
|
else mCubits[i].setUnmarked();
|
196
|
DistortedNode node = mCubits[i].getNode();
|
197
|
mScreen.attach(node);
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
void scaleCubits(float scale)
|
205
|
{
|
206
|
for(int i=0; i<mNumCubits; i++) mCubits[i].scaleMove(scale);
|
207
|
}
|
208
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
210
|
|
211
|
void recreateCubits(Static4D quatT, Static4D quatA, Static3D scale)
|
212
|
{
|
213
|
if( mCubits==null )
|
214
|
{
|
215
|
createCubits(quatT,quatA,scale);
|
216
|
}
|
217
|
else
|
218
|
{
|
219
|
for(int i=0; i<mNumCubits; i++) mCubits[i].recreateBitmap();
|
220
|
}
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
void touchCubit(int index)
|
226
|
{
|
227
|
if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setMarked();
|
228
|
}
|
229
|
|
230
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231
|
|
232
|
void untouchCubit(int index)
|
233
|
{
|
234
|
if( index>=0 && index<mNumCubits && mCubits[index]!=null ) mCubits[index].setUnmarked();
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
int whichCubitTouched(int face, float pointX, float pointY)
|
240
|
{
|
241
|
getTouchedPosition(mTmp, face, pointX, pointY);
|
242
|
|
243
|
for(int c=0; c<mNumCubits; c++)
|
244
|
if( mCubits[c].isAttached() )
|
245
|
{
|
246
|
float[] pos = mCubits[c].getPosition();
|
247
|
int len = pos.length/3;
|
248
|
|
249
|
for(int p=0; p<len; p++)
|
250
|
if( pos[3*p]==mTmp[0] && pos[3*p+1]==mTmp[1] && pos[3*p+2]==mTmp[2] ) return c;
|
251
|
}
|
252
|
|
253
|
android.util.Log.e("D", "whichCubitTouched: IMPOSSIBLE!!");
|
254
|
return -1;
|
255
|
}
|
256
|
|
257
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258
|
|
259
|
boolean isAdjacent(float[] pos1, float[] pos2)
|
260
|
{
|
261
|
int len1 = pos1.length/3;
|
262
|
int len2 = pos2.length/3;
|
263
|
|
264
|
for(int i=0; i<len1; i++)
|
265
|
for(int j=0; j<len2; j++)
|
266
|
{
|
267
|
float dx = pos1[3*i ] - pos2[3*j ];
|
268
|
float dy = pos1[3*i+1] - pos2[3*j+1];
|
269
|
float dz = pos1[3*i+2] - pos2[3*j+2];
|
270
|
|
271
|
if( isAdjacent(dx,dy,dz) ) return true;
|
272
|
}
|
273
|
|
274
|
return false;
|
275
|
}
|
276
|
}
|