Project

General

Profile

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

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

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.helpers.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, mRotAxis;
30

    
31
  private int mLastTouchedFace;
32
  private float[][][] mCastedRotAxis;
33
  private Static4D[][] mCastedRotAxis4D;
34
  private float[][] 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);
49

    
50
    int[] numLayers       = object.getNumLayers();
51
    float[][] cuts        = object.getCuts(numLayers);
52
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
53
    float size            = object.getSize();
54
    mRotAxis              = 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[mRotAxis.length+1];
69

    
70
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
71

    
72
    mGhostAxisEnabled = -1;
73

    
74
    computeCastedAxis(mRotAxis);
75
    computeBorders(cuts,rotatable,size);
76
    computeLinear(mRotAxis,faceAxis);
77
    }
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
        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
        normalize2D(mCastedRotAxis[face][casted]);
103

    
104
        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
        }
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

    
116
    if( len!=0.0f )
117
      {
118
      vect[0] /= len;
119
      vect[1] /= len;
120
      }
121
    }
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
  private boolean faceIsVisible(int index)
134
    {
135
    Static3D faceAxis = mFaceAxis[index];
136
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
137
    return castCameraOnAxis > mDistanceCenterFace3D[index];
138
    }
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
  private void castTouchPointOntoFace(int index, float[] output)
149
    {
150
    Static3D faceAxis = mFaceAxis[index];
151

    
152
    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
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
165

    
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
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
210
    {
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
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
227
          }
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

    
238
    if( borders==null ) return 0;
239

    
240
    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
    else
254
      {
255
      // 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
      }
264

    
265
    return len;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  void computeEnabledAxis(int face, float[] touchPoint)
271
    {
272
    if( mGhostAxisEnabled<0 )
273
      {
274
      int part = returnPart(mSplit, face, touchPoint);
275

    
276
      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
      mEnabledRotAxis[1] = mGhostAxisEnabled;
284
      }
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
// PUBLIC API
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
292
    {
293
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
294
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
295
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
296

    
297
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
298
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
299
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
300

    
301
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
302
      {
303
      if( faceIsVisible(mLastTouchedFace) )
304
        {
305
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
306

    
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
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
313
        }
314
      }
315

    
316
    return false;
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
322
    {
323
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
324
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
325
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
326

    
327
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
328

    
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

    
335
    mMove2D[0] -= mPoint2D[0];
336
    mMove2D[1] -= mPoint2D[1];
337

    
338
    computeEnabledAxis(mLastTouchedFace, mPoint2D);
339
    int rotIndex = computeRotationIndex( mCastedRotAxis[mLastTouchedFace], mMove2D, mEnabledRotAxis);
340
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
341
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
342

    
343
    output[0] = rotIndex;
344
    output[1] = row;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348
// 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

    
351
  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
352
    {
353
    Static4D a = mCastedRotAxis4D[mLastTouchedFace][axisIndex];
354
    getCastedRotAxis(output,quat,a.get0(),a.get1(),a.get2(),a.get3());
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
389

    
390
  public int getTouchedCubitFace()
391
    {
392
    return 0;
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396

    
397
  public int getTouchedCubit()
398
    {
399
    return 0;
400
    }
401
  }
(11-11/14)