Project

General

Profile

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

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

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[][] mTouchBorders, mA, mB;
35

    
36
  private final int mSplit;
37
  private final int[][][] mEnabled;
38
  private boolean mGhostAxisEnabled;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
  abstract int returnPart(int type, int face, float[] touchPoint);
43
  abstract boolean isInsideFace(int face, float[] point);
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  TouchControlShapeConstant(TwistyObject object, float[] distance3D, Static3D[] faceAxis)
48
    {
49
    super(object);
50

    
51
    int[] numLayers       = object.getNumLayers();
52
    float[][] cuts        = object.getCuts(numLayers);
53
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
54
    float size            = object.getSize();
55
    mRotAxis              = object.getRotationAxis();
56

    
57
    mPoint = new float[3];
58
    mCamera= new float[3];
59
    mTouch = new float[3];
60

    
61
    mPoint2D = new float[2];
62
    mMove2D  = new float[2];
63

    
64
    mSplit      = object.getTouchControlSplit();
65
    mEnabled    = object.getEnabled();
66
    mFaceAxis   = faceAxis;
67
    mNumFaceAxis= mFaceAxis.length;
68

    
69
    mEnabledRotAxis = new int[mRotAxis.length+1];
70

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

    
73
    mGhostAxisEnabled = true;
74

    
75
    computeCastedAxis(mRotAxis);
76
    computeBorders(cuts,rotatable,size);
77
    computeLinear(mRotAxis,faceAxis);
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
82
// 1st faceAxis.
83

    
84
  private void computeCastedAxis(Static3D[] rotAxis)
85
    {
86
    mCastedRotAxis   = new float[mNumFaceAxis][rotAxis.length][2];
87
    mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
88

    
89
    for( int casted=0; casted<rotAxis.length; casted++)
90
      {
91
      Static3D a = rotAxis[casted];
92
      mPoint[0]= a.get0();
93
      mPoint[1]= a.get1();
94
      mPoint[2]= a.get2();
95

    
96
      for( int face=0; face<mNumFaceAxis; face++)
97
        {
98
        float ax = mFaceAxis[face].get0();
99
        float ay = mFaceAxis[face].get1();
100
        float az = mFaceAxis[face].get2();
101

    
102
        convertTo2Dcoords( mPoint, ax,ay,az, mCastedRotAxis[face][casted]);
103
        normalize2D(mCastedRotAxis[face][casted]);
104

    
105
        float f = mPoint[0]*ax + mPoint[1]*ay + mPoint[2]*az;
106
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*ax, mPoint[1]-f*ay, mPoint[2]-f*az, 0);
107
        }
108
      }
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void normalize2D(float[] vect)
114
    {
115
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
116

    
117
    if( len!=0.0f )
118
      {
119
      vect[0] /= len;
120
      vect[1] /= len;
121
      }
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
// in the center of the face offset is always 0 regardless of the axis
126

    
127
  private float computeOffset(float[] point, float[] axis)
128
    {
129
    return point[0]*axis[0] + point[1]*axis[1];
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private boolean faceIsVisible(int index)
135
    {
136
    Static3D faceAxis = mFaceAxis[index];
137
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
138
    return castCameraOnAxis > mDistanceCenterFace3D[index];
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
143
// compute point 'output[]' which:
144
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
145
// 2) is co-linear with mCamera and mPoint
146
//
147
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
148

    
149
  private void castTouchPointOntoFace(int index, float[] output)
150
    {
151
    Static3D faceAxis = mFaceAxis[index];
152

    
153
    float d0 = mPoint[0]-mCamera[0];
154
    float d1 = mPoint[1]-mCamera[1];
155
    float d2 = mPoint[2]-mCamera[2];
156
    float a0 = faceAxis.get0();
157
    float a1 = faceAxis.get1();
158
    float a2 = faceAxis.get2();
159

    
160
    float denom = a0*d0 + a1*d1 + a2*d2;
161

    
162
    if( denom != 0.0f )
163
      {
164
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
165
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
166

    
167
      output[0] = mCamera[0] + d0*alpha;
168
      output[1] = mCamera[1] + d1*alpha;
169
      output[2] = mCamera[2] + d2*alpha;
170
      }
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
176
    {
177
    if( cuts==null ) return null;
178

    
179
    int len = cuts.length;
180
    float[] border = new float[len];
181

    
182
    for(int i=0; i<len; i++)
183
      {
184
      if( !rotatable[i] )
185
        {
186
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
187
        }
188
      else
189
        {
190
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
191
        else
192
          {
193
          int found = -1;
194

    
195
          for(int j=i+2; j<=len; j++)
196
            {
197
            if( rotatable[j] )
198
              {
199
              found=j;
200
              break;
201
              }
202
            }
203

    
204
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
205
          }
206
        }
207
      }
208

    
209
    return border;
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
214

    
215
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
216
    {
217
    int numCuts = cuts.length;
218
    mTouchBorders = new float[numCuts][];
219

    
220
    for(int axis=0; axis<numCuts; axis++)
221
      {
222
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
223
      }
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  private int computeSign(Static3D a, Static3D b)
229
    {
230
    float a1 = a.get0();
231
    float a2 = a.get1();
232
    float a3 = a.get2();
233
    float b1 = b.get0();
234
    float b2 = b.get1();
235
    float b3 = b.get2();
236

    
237
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  private float crossProductLen(Static3D a, Static3D b)
243
    {
244
    float a1 = a.get0();
245
    float a2 = a.get1();
246
    float a3 = a.get2();
247
    float b1 = b.get0();
248
    float b2 = b.get1();
249
    float b3 = b.get2();
250

    
251
    float x1 = a2*b3-a3*b2;
252
    float x2 = a3*b1-a1*b3;
253
    float x3 = a1*b2-a2*b1;
254

    
255
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
// compute the array of 'A' and 'B' coeffs of the Ax+B linear function by which we need to multiply
260
// the 3D 'cuts' to translate it from 3D (i.e. with respect to the rotAxis) to 2D in-face (i.e. with
261
// respect to the 2D rotAxis cast into a particular face)
262

    
263
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
264
    {
265
    int numFaces = faceAxis.length;
266
    int numRot   = rotAxis.length;
267

    
268
    mA = new float[numFaces][numRot];
269
    mB = new float[numFaces][numRot];
270

    
271
    for(int i=0; i<numFaces; i++)
272
      for(int j=0; j<numRot; j++)
273
        {
274
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
275

    
276
        if( mA[i][j]!=0.0f )
277
          {
278
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
279
          int sign = computeSign(faceAxis[i],rotAxis[j]);
280
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
281
          }
282
        else mB[i][j] = 0.0f;
283
        }
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  private int computeRowFromOffset(int face, int axisIndex, float offset)
289
    {
290
    float[] borders = mTouchBorders[axisIndex];
291

    
292
    if( borders==null ) return 0;
293

    
294
    int len = borders.length;
295
    float A = mA[face][axisIndex];
296

    
297
    if( A!=0.0f )
298
      {
299
      float B = mB[face][axisIndex];
300

    
301
      for(int i=0; i<len; i++)
302
        {
303
        float translated = B + borders[i]/A;
304
        if( offset<translated ) return i;
305
        }
306
      }
307
    else
308
      {
309
      // this must mean that we are rotating along an axis that is normal to the currently
310
      // touched face. So the offset passed here as param is incorrect (and equal to 0).
311
      // recompute it and return the row (no need to translate!)
312
      Static3D ax = mRotAxis[axisIndex];
313
      offset = mTouch[0]*ax.get0() + mTouch[1]*ax.get1() + mTouch[2]*ax.get2();
314

    
315
      for(int i=0; i<len; i++)
316
        if( offset<borders[i] ) return i;
317
      }
318

    
319
    return len;
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  void computeEnabledAxis(int face, float[] touchPoint)
325
    {
326
    if( mGhostAxisEnabled )
327
      {
328
      int part = returnPart(mSplit, face, touchPoint);
329

    
330
      int num = mEnabled[face][0].length;
331
      mEnabledRotAxis[0] = num;
332
      System.arraycopy(mEnabled[face][part], 0, mEnabledRotAxis, 1, num);
333
      }
334
    else
335
      {
336
      mEnabledRotAxis[0] = 1;  // if in 'ghost' mode, only the 0th axis is enabled.
337
      mEnabledRotAxis[1] = 0;
338
      }
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
// PUBLIC API
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
346
    {
347
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
348
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
349
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
350

    
351
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
352
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
353
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
354

    
355
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
356
      {
357
      if( faceIsVisible(mLastTouchedFace) )
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, mPoint2D);
366
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
367
        }
368
      }
369

    
370
    return false;
371
    }
372

    
373
///////////////////////////////////////////////////////////////////////////////////////////////////
374

    
375
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
376
    {
377
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
378
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
379
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
380

    
381
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
382

    
383
    float ax = mFaceAxis[mLastTouchedFace].get0();
384
    float ay = mFaceAxis[mLastTouchedFace].get1();
385
    float az = mFaceAxis[mLastTouchedFace].get2();
386

    
387
    convertTo2Dcoords(mTouch, ax,ay,az, mMove2D);
388

    
389
    mMove2D[0] -= mPoint2D[0];
390
    mMove2D[1] -= mPoint2D[1];
391

    
392
    computeEnabledAxis(mLastTouchedFace, mPoint2D);
393
    int rotIndex = computeRotationIndex( mCastedRotAxis[mLastTouchedFace], mMove2D, mEnabledRotAxis);
394
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
395
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
396

    
397
    output[0] = rotIndex;
398
    output[1] = row;
399
    }
400

    
401
///////////////////////////////////////////////////////////////////////////////////////////////////
402
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
403
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
404

    
405
  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
406
    {
407
    Static4D axis = mCastedRotAxis4D[mLastTouchedFace][axisIndex];
408
    Static4D result = QuatHelper.rotateVectorByQuat(axis, quat);
409

    
410
    output[0] =result.get0();
411
    output[1] =result.get1();
412

    
413
    float len = (float)Math.sqrt(output[0]*output[0] + output[1]*output[1]);
414

    
415
    if( len!=0 )
416
      {
417
      output[0] /= len;
418
      output[1] /= len;
419
      }
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423

    
424
  public boolean axisAndFaceAgree(int axisIndex)
425
    {
426
    Static3D rotAxis = mRotAxis[axisIndex];
427
    Static3D faceAxis= mFaceAxis[mLastTouchedFace];
428

    
429
    float rx = rotAxis.get0();
430
    float ry = rotAxis.get1();
431
    float rz = rotAxis.get2();
432

    
433
    float fx = faceAxis.get0();
434
    float fy = faceAxis.get1();
435
    float fz = faceAxis.get2();
436

    
437
    float dx = rx-fx;
438
    float dy = ry-fy;
439
    float dz = rz-fz;
440

    
441
    return (dx*dx + dy*dy + dz*dz) == 0;
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445

    
446
  public float[] getTouchedPuzzleCenter()
447
    {
448
    Static3D faceAxis = mFaceAxis[mLastTouchedFace];
449
    float d = mDistanceCenterFace3D[mLastTouchedFace];
450
    return new float[] { d*faceAxis.get0(), d*faceAxis.get1(), d*faceAxis.get2(), 1 };
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  public int getTouchedCubitFace()
456
    {
457
    return 0;
458
    }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
  public int getTouchedCubit()
463
    {
464
    return 0;
465
    }
466

    
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468

    
469
  public void enableGhostAxis(boolean enable)
470
    {
471
    mGhostAxisEnabled = enable;
472
    }
473
  }
(10-10/13)