Project

General

Profile

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

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

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
    vect[0] /= len;
114
    vect[1] /= len;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// in the center of the face offset is always 0 regardless of the axis
119

    
120
  private float computeOffset(float[] point, float[] axis)
121
    {
122
    return point[0]*axis[0] + point[1]*axis[1];
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private boolean faceIsVisible(int index)
128
    {
129
    Static3D faceAxis = mFaceAxis[index];
130
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
131
    return castCameraOnAxis > mDistanceCenterFace3D[index];
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
136
// compute point 'output[]' which:
137
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
138
// 2) is co-linear with mCamera and mPoint
139
//
140
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
141

    
142
  private void castTouchPointOntoFace(int index, float[] output)
143
    {
144
    Static3D faceAxis = mFaceAxis[index];
145

    
146
    float d0 = mPoint[0]-mCamera[0];
147
    float d1 = mPoint[1]-mCamera[1];
148
    float d2 = mPoint[2]-mCamera[2];
149
    float a0 = faceAxis.get0();
150
    float a1 = faceAxis.get1();
151
    float a2 = faceAxis.get2();
152

    
153
    float denom = a0*d0 + a1*d1 + a2*d2;
154

    
155
    if( denom != 0.0f )
156
      {
157
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
158
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
159

    
160
      output[0] = mCamera[0] + d0*alpha;
161
      output[1] = mCamera[1] + d1*alpha;
162
      output[2] = mCamera[2] + d2*alpha;
163
      }
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
169
    {
170
    if( cuts==null ) return null;
171

    
172
    int len = cuts.length;
173
    float[] border = new float[len];
174

    
175
    for(int i=0; i<len; i++)
176
      {
177
      if( !rotatable[i] )
178
        {
179
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
180
        }
181
      else
182
        {
183
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
184
        else
185
          {
186
          int found = -1;
187

    
188
          for(int j=i+2; j<=len; j++)
189
            {
190
            if( rotatable[j] )
191
              {
192
              found=j;
193
              break;
194
              }
195
            }
196

    
197
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
198
          }
199
        }
200
      }
201

    
202
    return border;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
207

    
208
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
209
    {
210
    int numCuts = cuts.length;
211
    mTouchBorders = new float[numCuts][];
212

    
213
    for(int axis=0; axis<numCuts; axis++)
214
      {
215
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
216
      }
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  private int computeSign(Static3D a, Static3D b)
222
    {
223
    float a1 = a.get0();
224
    float a2 = a.get1();
225
    float a3 = a.get2();
226
    float b1 = b.get0();
227
    float b2 = b.get1();
228
    float b3 = b.get2();
229

    
230
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  private float crossProductLen(Static3D a, Static3D b)
236
    {
237
    float a1 = a.get0();
238
    float a2 = a.get1();
239
    float a3 = a.get2();
240
    float b1 = b.get0();
241
    float b2 = b.get1();
242
    float b3 = b.get2();
243

    
244
    float x1 = a2*b3-a3*b2;
245
    float x2 = a3*b1-a1*b3;
246
    float x3 = a1*b2-a2*b1;
247

    
248
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
249
    }
250

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

    
256
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
257
    {
258
    int numFaces = faceAxis.length;
259
    int numRot   = rotAxis.length;
260

    
261
    mA = new float[numFaces][numRot];
262
    mB = new float[numFaces][numRot];
263

    
264
    for(int i=0; i<numFaces; i++)
265
      for(int j=0; j<numRot; j++)
266
        {
267
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
268

    
269
        if( mA[i][j]!=0.0f )
270
          {
271
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
272
          int sign = computeSign(faceAxis[i],rotAxis[j]);
273
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
274
          }
275
        else mB[i][j] = 0.0f;
276
        }
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  private int computeRowFromOffset(int face, int axisIndex, float offset)
282
    {
283
    float[] borders = mTouchBorders[axisIndex];
284

    
285
    if( borders==null ) return 0;
286

    
287
    int len = borders.length;
288
    float A = mA[face][axisIndex];
289

    
290
    if( A!=0.0f )
291
      {
292
      float B = mB[face][axisIndex];
293

    
294
      for(int i=0; i<len; i++)
295
        {
296
        float translated = B + borders[i]/A;
297
        if( offset<translated ) return i;
298
        }
299
      }
300

    
301
    return len;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
307
    {
308
    int part = returnPart(mSplit,face,touchPoint);
309

    
310
    int num = mEnabled[face][0].length;
311
    enabled[0] = num;
312
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
// PUBLIC API
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
320
    {
321
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
322
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
323
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
324

    
325
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
326
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
327
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
328

    
329
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
330
      {
331
      if( faceIsVisible(mLastTouchedFace) )
332
        {
333
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
334

    
335
        float ax = mFaceAxis[mLastTouchedFace].get0();
336
        float ay = mFaceAxis[mLastTouchedFace].get1();
337
        float az = mFaceAxis[mLastTouchedFace].get2();
338

    
339
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
340
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
341
        }
342
      }
343

    
344
    return false;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348

    
349
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
350
    {
351
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
352
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
353
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
354

    
355
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
356

    
357
    float ax = mFaceAxis[mLastTouchedFace].get0();
358
    float ay = mFaceAxis[mLastTouchedFace].get1();
359
    float az = mFaceAxis[mLastTouchedFace].get2();
360

    
361
    convertTo2Dcoords(mTouch, ax,ay,az, mMove2D);
362

    
363
    mMove2D[0] -= mPoint2D[0];
364
    mMove2D[1] -= mPoint2D[1];
365

    
366
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
367
    int rotIndex = computeRotationIndex( mCastedRotAxis[mLastTouchedFace], mMove2D, mEnabledRotAxis);
368
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
369
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
370

    
371
    output[0] = rotIndex;
372
    output[1] = row;
373
    }
374

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

    
379
  public void getCastedRotAxis(float[] output, Static4D quat, int rotIndex)
380
    {
381
    Static4D axis = mCastedRotAxis4D[mLastTouchedFace][rotIndex];
382
    Static4D result = QuatHelper.rotateVectorByQuat(axis, quat);
383

    
384
    output[0] =result.get0();
385
    output[1] =result.get1();
386

    
387
    float len = (float)Math.sqrt(output[0]*output[0] + output[1]*output[1]);
388
    output[0] /= len;
389
    output[1] /= len;
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  public int getTouchedCubitFace()
395
    {
396
    return 0;
397
    }
398

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
  public int getTouchedCubit()
402
    {
403
    return 0;
404
    }
405
  }
(8-8/11)