1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2020 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.touchcontrol;
|
11
|
|
12
|
import org.distorted.library.main.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 abstract class TouchControlShapeConstant extends TouchControl
|
20
|
{
|
21
|
static final float SQ3 = (float)Math.sqrt(3);
|
22
|
static final float SQ6 = (float)Math.sqrt(6);
|
23
|
|
24
|
private final int mNumFaceAxis;
|
25
|
private final float[] mPoint, mCamera, mTouch;
|
26
|
private final float[] mPoint2D, mMove2D;
|
27
|
private final int[] mEnabledRotAxis;
|
28
|
private final float[] mDistanceCenterFace3D;
|
29
|
private final Static3D[] mFaceAxis;
|
30
|
|
31
|
private int mLastTouchedFace;
|
32
|
private float[][][] mCastedRotAxis;
|
33
|
private Static4D[][] mCastedRotAxis4D;
|
34
|
private float[][] mTouchBorders, mA, mB;
|
35
|
|
36
|
private final int mSplit;
|
37
|
private final int[][][] mEnabled;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
abstract int returnPart(int type, int face, float[] touchPoint);
|
42
|
abstract boolean isInsideFace(int face, float[] point);
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
|
46
|
TouchControlShapeConstant(TwistyObject object, float[] distance3D, Static3D[] faceAxis)
|
47
|
{
|
48
|
super(object.getObjectRatio());
|
49
|
|
50
|
int[] numLayers = object.getNumLayers();
|
51
|
float[][] cuts = object.getCuts(numLayers);
|
52
|
boolean[][] rotatable = object.getLayerRotatable(numLayers);
|
53
|
float size = object.getSize();
|
54
|
Static3D[] rotAxis = object.getRotationAxis();
|
55
|
|
56
|
mPoint = new float[3];
|
57
|
mCamera= new float[3];
|
58
|
mTouch = new float[3];
|
59
|
|
60
|
mPoint2D = new float[2];
|
61
|
mMove2D = new float[2];
|
62
|
|
63
|
mSplit = object.getTouchControlSplit();
|
64
|
mEnabled = object.getEnabled();
|
65
|
mFaceAxis = faceAxis;
|
66
|
mNumFaceAxis= mFaceAxis.length;
|
67
|
|
68
|
mEnabledRotAxis = new int[rotAxis.length+1];
|
69
|
|
70
|
mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
|
71
|
|
72
|
computeCastedAxis(rotAxis);
|
73
|
computeBorders(cuts,rotatable,size);
|
74
|
computeLinear(rotAxis,faceAxis);
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
|
79
|
// 1st faceAxis.
|
80
|
|
81
|
private void computeCastedAxis(Static3D[] rotAxis)
|
82
|
{
|
83
|
mCastedRotAxis = new float[mNumFaceAxis][rotAxis.length][2];
|
84
|
mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
|
85
|
|
86
|
for( int casted=0; casted<rotAxis.length; casted++)
|
87
|
{
|
88
|
Static3D a = rotAxis[casted];
|
89
|
mPoint[0]= a.get0();
|
90
|
mPoint[1]= a.get1();
|
91
|
mPoint[2]= a.get2();
|
92
|
|
93
|
for( int face=0; face<mNumFaceAxis; face++)
|
94
|
{
|
95
|
float ax = mFaceAxis[face].get0();
|
96
|
float ay = mFaceAxis[face].get1();
|
97
|
float az = mFaceAxis[face].get2();
|
98
|
|
99
|
convertTo2Dcoords( mPoint, ax,ay,az, mCastedRotAxis[face][casted]);
|
100
|
normalize2D(mCastedRotAxis[face][casted]);
|
101
|
|
102
|
float f = mPoint[0]*ax + mPoint[1]*ay + mPoint[2]*az;
|
103
|
mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*ax, mPoint[1]-f*ay, mPoint[2]-f*az, 0);
|
104
|
}
|
105
|
}
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
|
110
|
private void normalize2D(float[] vect)
|
111
|
{
|
112
|
float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
|
113
|
|
114
|
if( len!=0.0f )
|
115
|
{
|
116
|
vect[0] /= len;
|
117
|
vect[1] /= len;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
// in the center of the face offset is always 0 regardless of the axis
|
123
|
|
124
|
private float computeOffset(float[] point, float[] axis)
|
125
|
{
|
126
|
return point[0]*axis[0] + point[1]*axis[1];
|
127
|
}
|
128
|
|
129
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
|
131
|
private boolean faceIsVisible(int index)
|
132
|
{
|
133
|
Static3D faceAxis = mFaceAxis[index];
|
134
|
float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
|
135
|
return castCameraOnAxis > mDistanceCenterFace3D[index];
|
136
|
}
|
137
|
|
138
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
139
|
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
|
140
|
// compute point 'output[]' which:
|
141
|
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
|
142
|
// 2) is co-linear with mCamera and mPoint
|
143
|
//
|
144
|
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
|
145
|
|
146
|
private void castTouchPointOntoFace(int index, float[] output)
|
147
|
{
|
148
|
Static3D faceAxis = mFaceAxis[index];
|
149
|
|
150
|
float d0 = mPoint[0]-mCamera[0];
|
151
|
float d1 = mPoint[1]-mCamera[1];
|
152
|
float d2 = mPoint[2]-mCamera[2];
|
153
|
float a0 = faceAxis.get0();
|
154
|
float a1 = faceAxis.get1();
|
155
|
float a2 = faceAxis.get2();
|
156
|
|
157
|
float denom = a0*d0 + a1*d1 + a2*d2;
|
158
|
|
159
|
if( denom != 0.0f )
|
160
|
{
|
161
|
float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
|
162
|
float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
|
163
|
|
164
|
output[0] = mCamera[0] + d0*alpha;
|
165
|
output[1] = mCamera[1] + d1*alpha;
|
166
|
output[2] = mCamera[2] + d2*alpha;
|
167
|
}
|
168
|
}
|
169
|
|
170
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171
|
|
172
|
private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
|
173
|
{
|
174
|
if( cuts==null ) return null;
|
175
|
|
176
|
int len = cuts.length;
|
177
|
float[] border = new float[len];
|
178
|
|
179
|
for(int i=0; i<len; i++)
|
180
|
{
|
181
|
if( !rotatable[i] )
|
182
|
{
|
183
|
border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
|
184
|
}
|
185
|
else
|
186
|
{
|
187
|
if( rotatable[i+1] ) border[i] = cuts[i]/size;
|
188
|
else
|
189
|
{
|
190
|
int found = -1;
|
191
|
|
192
|
for(int j=i+2; j<=len; j++)
|
193
|
{
|
194
|
if( rotatable[j] )
|
195
|
{
|
196
|
found=j;
|
197
|
break;
|
198
|
}
|
199
|
}
|
200
|
|
201
|
border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
|
202
|
}
|
203
|
}
|
204
|
}
|
205
|
|
206
|
return border;
|
207
|
}
|
208
|
|
209
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
210
|
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
|
211
|
|
212
|
void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
|
213
|
{
|
214
|
int numCuts = cuts.length;
|
215
|
mTouchBorders = new float[numCuts][];
|
216
|
|
217
|
for(int axis=0; axis<numCuts; axis++)
|
218
|
{
|
219
|
mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
|
220
|
}
|
221
|
}
|
222
|
|
223
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
224
|
|
225
|
private int computeSign(Static3D a, Static3D b)
|
226
|
{
|
227
|
float a1 = a.get0();
|
228
|
float a2 = a.get1();
|
229
|
float a3 = a.get2();
|
230
|
float b1 = b.get0();
|
231
|
float b2 = b.get1();
|
232
|
float b3 = b.get2();
|
233
|
|
234
|
return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
private float crossProductLen(Static3D a, Static3D b)
|
240
|
{
|
241
|
float a1 = a.get0();
|
242
|
float a2 = a.get1();
|
243
|
float a3 = a.get2();
|
244
|
float b1 = b.get0();
|
245
|
float b2 = b.get1();
|
246
|
float b3 = b.get2();
|
247
|
|
248
|
float x1 = a2*b3-a3*b2;
|
249
|
float x2 = a3*b1-a1*b3;
|
250
|
float x3 = a1*b2-a2*b1;
|
251
|
|
252
|
return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
|
253
|
}
|
254
|
|
255
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
256
|
// compute the array of 'A' and 'B' coeffs of the Ax+B linear function by which we need to multiply
|
257
|
// the 3D 'cuts' to translate it from 3D (i.e. with respect to the rotAxis) to 2D in-face (i.e. with
|
258
|
// respect to the 2D rotAxis cast into a particular face)
|
259
|
|
260
|
private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
|
261
|
{
|
262
|
int numFaces = faceAxis.length;
|
263
|
int numRot = rotAxis.length;
|
264
|
|
265
|
mA = new float[numFaces][numRot];
|
266
|
mB = new float[numFaces][numRot];
|
267
|
|
268
|
for(int i=0; i<numFaces; i++)
|
269
|
for(int j=0; j<numRot; j++)
|
270
|
{
|
271
|
mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
|
272
|
|
273
|
if( mA[i][j]!=0.0f )
|
274
|
{
|
275
|
float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
|
276
|
int sign = computeSign(faceAxis[i],rotAxis[j]);
|
277
|
mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
|
278
|
}
|
279
|
else mB[i][j] = 0.0f;
|
280
|
}
|
281
|
}
|
282
|
|
283
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
284
|
|
285
|
private int computeRowFromOffset(int face, int axisIndex, float offset)
|
286
|
{
|
287
|
float[] borders = mTouchBorders[axisIndex];
|
288
|
|
289
|
if( borders==null ) return 0;
|
290
|
|
291
|
int len = borders.length;
|
292
|
float A = mA[face][axisIndex];
|
293
|
|
294
|
if( A!=0.0f )
|
295
|
{
|
296
|
float B = mB[face][axisIndex];
|
297
|
|
298
|
for(int i=0; i<len; i++)
|
299
|
{
|
300
|
float translated = B + borders[i]/A;
|
301
|
if( offset<translated ) return i;
|
302
|
}
|
303
|
}
|
304
|
|
305
|
return len;
|
306
|
}
|
307
|
|
308
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
309
|
|
310
|
void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
|
311
|
{
|
312
|
int part = returnPart(mSplit,face,touchPoint);
|
313
|
|
314
|
int num = mEnabled[face][0].length;
|
315
|
enabled[0] = num;
|
316
|
System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
|
317
|
}
|
318
|
|
319
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
320
|
// PUBLIC API
|
321
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
322
|
|
323
|
public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
|
324
|
{
|
325
|
mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
|
326
|
mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
|
327
|
mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
|
328
|
|
329
|
mCamera[0] = rotatedCamera.get0()/mObjectRatio;
|
330
|
mCamera[1] = rotatedCamera.get1()/mObjectRatio;
|
331
|
mCamera[2] = rotatedCamera.get2()/mObjectRatio;
|
332
|
|
333
|
for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
|
334
|
{
|
335
|
if( faceIsVisible(mLastTouchedFace) )
|
336
|
{
|
337
|
castTouchPointOntoFace(mLastTouchedFace, mTouch);
|
338
|
|
339
|
float ax = mFaceAxis[mLastTouchedFace].get0();
|
340
|
float ay = mFaceAxis[mLastTouchedFace].get1();
|
341
|
float az = mFaceAxis[mLastTouchedFace].get2();
|
342
|
|
343
|
convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
|
344
|
if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
|
345
|
}
|
346
|
}
|
347
|
|
348
|
return false;
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
|
353
|
public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
|
354
|
{
|
355
|
mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
|
356
|
mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
|
357
|
mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
|
358
|
|
359
|
castTouchPointOntoFace(mLastTouchedFace, mTouch);
|
360
|
|
361
|
float ax = mFaceAxis[mLastTouchedFace].get0();
|
362
|
float ay = mFaceAxis[mLastTouchedFace].get1();
|
363
|
float az = mFaceAxis[mLastTouchedFace].get2();
|
364
|
|
365
|
convertTo2Dcoords(mTouch, ax,ay,az, mMove2D);
|
366
|
|
367
|
mMove2D[0] -= mPoint2D[0];
|
368
|
mMove2D[1] -= mPoint2D[1];
|
369
|
|
370
|
computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
|
371
|
int rotIndex = computeRotationIndex( mCastedRotAxis[mLastTouchedFace], mMove2D, mEnabledRotAxis);
|
372
|
float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
|
373
|
int row = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
|
374
|
|
375
|
output[0] = rotIndex;
|
376
|
output[1] = row;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
|
381
|
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
|
382
|
|
383
|
public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
|
384
|
{
|
385
|
Static4D axis = mCastedRotAxis4D[mLastTouchedFace][axisIndex];
|
386
|
Static4D result = QuatHelper.rotateVectorByQuat(axis, quat);
|
387
|
|
388
|
output[0] =result.get0();
|
389
|
output[1] =result.get1();
|
390
|
|
391
|
float len = (float)Math.sqrt(output[0]*output[0] + output[1]*output[1]);
|
392
|
output[0] /= len;
|
393
|
output[1] /= len;
|
394
|
}
|
395
|
|
396
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
397
|
|
398
|
public int getTouchedCubitFace()
|
399
|
{
|
400
|
return 0;
|
401
|
}
|
402
|
|
403
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
404
|
|
405
|
public int getTouchedCubit()
|
406
|
{
|
407
|
return 0;
|
408
|
}
|
409
|
}
|