Project

General

Profile

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

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

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 57ef6378 Leszek Koltunski
import org.distorted.library.main.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
  private float[][] mTouchBorders, mA, mB;
35
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 11fa413d Leszek Koltunski
    super(object.getObjectRatio());
49
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 333ecf1b Leszek Koltunski
    computeCastedAxis(mRotAxis);
73 29b82486 Leszek Koltunski
    computeBorders(cuts,rotatable,size);
74 333ecf1b Leszek Koltunski
    computeLinear(mRotAxis,faceAxis);
75 29b82486 Leszek Koltunski
    }
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 0c5d8bf7 Leszek Koltunski
        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 29b82486 Leszek Koltunski
        normalize2D(mCastedRotAxis[face][casted]);
101
102 0c5d8bf7 Leszek Koltunski
        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 29b82486 Leszek Koltunski
        }
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 bfdb9aa5 Leszek Koltunski
114
    if( len!=0.0f )
115
      {
116
      vect[0] /= len;
117
      vect[1] /= len;
118
      }
119 29b82486 Leszek Koltunski
    }
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 8da6b1c9 Leszek Koltunski
  private boolean faceIsVisible(int index)
132 29b82486 Leszek Koltunski
    {
133 8da6b1c9 Leszek Koltunski
    Static3D faceAxis = mFaceAxis[index];
134 29b82486 Leszek Koltunski
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
135 ab31cf6f Leszek Koltunski
    return castCameraOnAxis > mDistanceCenterFace3D[index];
136 29b82486 Leszek Koltunski
    }
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 8da6b1c9 Leszek Koltunski
  private void castTouchPointOntoFace(int index, float[] output)
147 29b82486 Leszek Koltunski
    {
148 8da6b1c9 Leszek Koltunski
    Static3D faceAxis = mFaceAxis[index];
149
150 29b82486 Leszek Koltunski
    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 ab31cf6f Leszek Koltunski
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
163 29b82486 Leszek Koltunski
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 ba6d9ee9 Leszek Koltunski
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
173 29b82486 Leszek Koltunski
    {
174 a135652b Leszek Koltunski
    if( cuts==null ) return null;
175
176 29b82486 Leszek Koltunski
    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 ba6d9ee9 Leszek Koltunski
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
211 29b82486 Leszek Koltunski
212 ba6d9ee9 Leszek Koltunski
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
213 29b82486 Leszek Koltunski
    {
214
    int numCuts = cuts.length;
215
    mTouchBorders = new float[numCuts][];
216
217 ba6d9ee9 Leszek Koltunski
    for(int axis=0; axis<numCuts; axis++)
218 29b82486 Leszek Koltunski
      {
219 ba6d9ee9 Leszek Koltunski
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
220 29b82486 Leszek Koltunski
      }
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 ab31cf6f Leszek Koltunski
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
261 29b82486 Leszek Koltunski
    {
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 ab31cf6f Leszek Koltunski
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
278 29b82486 Leszek Koltunski
          }
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 a135652b Leszek Koltunski
289
    if( borders==null ) return 0;
290
291 29b82486 Leszek Koltunski
    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 2241c459 Leszek Koltunski
    else
305
      {
306 333ecf1b Leszek Koltunski
      // this must mean that we are rotating along an axis that is normal to the currently
307
      // touched face. So the offset passed here as param is incorrect (and equal to 0).
308
      // recompute it and return the row (no need to translate!)
309
      Static3D ax = mRotAxis[axisIndex];
310
      offset = mTouch[0]*ax.get0() + mTouch[1]*ax.get1() + mTouch[2]*ax.get2();
311
312
      for(int i=0; i<len; i++)
313
        if( offset<borders[i] ) return i;
314 2241c459 Leszek Koltunski
      }
315 29b82486 Leszek Koltunski
316
    return len;
317
    }
318
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
321
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
322
    {
323 57ef6378 Leszek Koltunski
    int part = returnPart(mSplit,face,touchPoint);
324 29b82486 Leszek Koltunski
325
    int num = mEnabled[face][0].length;
326
    enabled[0] = num;
327
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
328
    }
329
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
// PUBLIC API
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
334 57ef6378 Leszek Koltunski
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
335 29b82486 Leszek Koltunski
    {
336 23afe4c4 Leszek Koltunski
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
337
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
338
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
339 29b82486 Leszek Koltunski
340 23afe4c4 Leszek Koltunski
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
341
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
342
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
343 29b82486 Leszek Koltunski
344
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
345
      {
346 8da6b1c9 Leszek Koltunski
      if( faceIsVisible(mLastTouchedFace) )
347 29b82486 Leszek Koltunski
        {
348 8da6b1c9 Leszek Koltunski
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
349 0c5d8bf7 Leszek Koltunski
350
        float ax = mFaceAxis[mLastTouchedFace].get0();
351
        float ay = mFaceAxis[mLastTouchedFace].get1();
352
        float az = mFaceAxis[mLastTouchedFace].get2();
353
354
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
355 29b82486 Leszek Koltunski
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
356
        }
357
      }
358
359
    return false;
360
    }
361
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363
364 cd2e8d4c Leszek Koltunski
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
365 29b82486 Leszek Koltunski
    {
366 23afe4c4 Leszek Koltunski
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
367
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
368
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
369 29b82486 Leszek Koltunski
370 8da6b1c9 Leszek Koltunski
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
371 0c5d8bf7 Leszek Koltunski
372
    float ax = mFaceAxis[mLastTouchedFace].get0();
373
    float ay = mFaceAxis[mLastTouchedFace].get1();
374
    float az = mFaceAxis[mLastTouchedFace].get2();
375
376
    convertTo2Dcoords(mTouch, ax,ay,az, mMove2D);
377 29b82486 Leszek Koltunski
378
    mMove2D[0] -= mPoint2D[0];
379
    mMove2D[1] -= mPoint2D[1];
380
381
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
382 92a6fc8b Leszek Koltunski
    int rotIndex = computeRotationIndex( mCastedRotAxis[mLastTouchedFace], mMove2D, mEnabledRotAxis);
383 29b82486 Leszek Koltunski
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
384
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
385
386 57ef6378 Leszek Koltunski
    output[0] = rotIndex;
387
    output[1] = row;
388 29b82486 Leszek Koltunski
    }
389
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391 57ef6378 Leszek Koltunski
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
392
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
393 29b82486 Leszek Koltunski
394 5caf2641 Leszek Koltunski
  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
395 29b82486 Leszek Koltunski
    {
396 5caf2641 Leszek Koltunski
    Static4D axis = mCastedRotAxis4D[mLastTouchedFace][axisIndex];
397 57ef6378 Leszek Koltunski
    Static4D result = QuatHelper.rotateVectorByQuat(axis, quat);
398
399
    output[0] =result.get0();
400
    output[1] =result.get1();
401
402
    float len = (float)Math.sqrt(output[0]*output[0] + output[1]*output[1]);
403 2241c459 Leszek Koltunski
404
    if( len!=0 )
405
      {
406
      output[0] /= len;
407
      output[1] /= len;
408
      }
409 29b82486 Leszek Koltunski
    }
410
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
413 11fa413d Leszek Koltunski
  public int getTouchedCubitFace()
414 29b82486 Leszek Koltunski
    {
415 11fa413d Leszek Koltunski
    return 0;
416 29b82486 Leszek Koltunski
    }
417
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419
420 11fa413d Leszek Koltunski
  public int getTouchedCubit()
421 29b82486 Leszek Koltunski
    {
422 11fa413d Leszek Koltunski
    return 0;
423 29b82486 Leszek Koltunski
    }
424
  }