Project

General

Profile

Download (13.4 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControlShapeConstant.java @ 52375039

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