Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.objectlib.touchcontrol;
21

    
22
import org.distorted.library.main.QuatHelper;
23
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25
import org.distorted.objectlib.main.TwistyObject;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
public abstract class TouchControlShapeConstant extends TouchControl
30
  {
31
  static final float SQ3 = (float)Math.sqrt(3);
32
  static final float SQ6 = (float)Math.sqrt(6);
33

    
34
  private final int mNumFaceAxis;
35
  private final float[] mPoint, mCamera, mTouch;
36
  private final float[] mPoint2D, mMove2D;
37
  private final int[] mEnabledRotAxis;
38
  private final float[] mDistanceCenterFace3D;
39
  private final Static3D[] mFaceAxis;
40

    
41
  private int mLastTouchedFace;
42
  private float[][][] mCastedRotAxis;
43
  private Static4D[][] mCastedRotAxis4D;
44
  private float[][] mTouchBorders, mA, mB;
45

    
46
  private final int mSplit;
47
  private final int[][][] mEnabled;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  abstract int returnPart(int type, int face, float[] touchPoint);
52
  abstract boolean isInsideFace(int face, float[] point);
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  TouchControlShapeConstant(TwistyObject object, float[] distance3D, Static3D[] faceAxis)
57
    {
58
    super(object.getObjectRatio());
59

    
60
    int[] numLayers       = object.getNumLayers();
61
    float[][] cuts        = object.getCuts(numLayers);
62
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
63
    float size            = object.getSize();
64
    Static3D[] rotAxis    = object.getRotationAxis();
65

    
66
    mPoint = new float[3];
67
    mCamera= new float[3];
68
    mTouch = new float[3];
69

    
70
    mPoint2D = new float[2];
71
    mMove2D  = new float[2];
72

    
73
    mSplit      = object.getTouchControlSplit();
74
    mEnabled    = object.getEnabled();
75
    mFaceAxis   = faceAxis;
76
    mNumFaceAxis= mFaceAxis.length;
77

    
78
    mEnabledRotAxis = new int[rotAxis.length+1];
79

    
80
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
81

    
82
    computeCastedAxis(rotAxis);
83
    computeBorders(cuts,rotatable,size);
84
    computeLinear(rotAxis,faceAxis);
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
89
// 1st faceAxis.
90

    
91
  private void computeCastedAxis(Static3D[] rotAxis)
92
    {
93
    mCastedRotAxis   = new float[mNumFaceAxis][rotAxis.length][2];
94
    mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
95

    
96
    for( int casted=0; casted<rotAxis.length; casted++)
97
      {
98
      Static3D a = rotAxis[casted];
99
      mPoint[0]= a.get0();
100
      mPoint[1]= a.get1();
101
      mPoint[2]= a.get2();
102

    
103
      for( int face=0; face<mNumFaceAxis; face++)
104
        {
105
        float ax = mFaceAxis[face].get0();
106
        float ay = mFaceAxis[face].get1();
107
        float az = mFaceAxis[face].get2();
108

    
109
        convertTo2Dcoords( mPoint, ax,ay,az, mCastedRotAxis[face][casted]);
110
        normalize2D(mCastedRotAxis[face][casted]);
111

    
112
        float f = mPoint[0]*ax + mPoint[1]*ay + mPoint[2]*az;
113
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*ax, mPoint[1]-f*ay, mPoint[2]-f*az, 0);
114
        }
115
      }
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  private void normalize2D(float[] vect)
121
    {
122
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
123
    vect[0] /= len;
124
    vect[1] /= len;
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
// in the center of the face offset is always 0 regardless of the axis
129

    
130
  private float computeOffset(float[] point, float[] axis)
131
    {
132
    return point[0]*axis[0] + point[1]*axis[1];
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

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

    
152
  private void castTouchPointOntoFace(int index, float[] output)
153
    {
154
    Static3D faceAxis = mFaceAxis[index];
155

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

    
163
    float denom = a0*d0 + a1*d1 + a2*d2;
164

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

    
170
      output[0] = mCamera[0] + d0*alpha;
171
      output[1] = mCamera[1] + d1*alpha;
172
      output[2] = mCamera[2] + d2*alpha;
173
      }
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
179
    {
180
    if( cuts==null ) return null;
181

    
182
    int len = cuts.length;
183
    float[] border = new float[len];
184

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

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

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

    
212
    return border;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
217

    
218
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
219
    {
220
    int numCuts = cuts.length;
221
    mTouchBorders = new float[numCuts][];
222

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

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

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

    
240
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

    
254
    float x1 = a2*b3-a3*b2;
255
    float x2 = a3*b1-a1*b3;
256
    float x3 = a1*b2-a2*b1;
257

    
258
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
259
    }
260

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

    
266
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
267
    {
268
    int numFaces = faceAxis.length;
269
    int numRot   = rotAxis.length;
270

    
271
    mA = new float[numFaces][numRot];
272
    mB = new float[numFaces][numRot];
273

    
274
    for(int i=0; i<numFaces; i++)
275
      for(int j=0; j<numRot; j++)
276
        {
277
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
278

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

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  private int computeRowFromOffset(int face, int axisIndex, float offset)
292
    {
293
    float[] borders = mTouchBorders[axisIndex];
294

    
295
    if( borders==null ) return 0;
296

    
297
    int len = borders.length;
298
    float A = mA[face][axisIndex];
299

    
300
    if( A!=0.0f )
301
      {
302
      float B = mB[face][axisIndex];
303

    
304
      for(int i=0; i<len; i++)
305
        {
306
        float translated = B + borders[i]/A;
307
        if( offset<translated ) return i;
308
        }
309
      }
310

    
311
    return len;
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
317
    {
318
    int part = returnPart(mSplit,face,touchPoint);
319

    
320
    int num = mEnabled[face][0].length;
321
    enabled[0] = num;
322
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
// PUBLIC API
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
330
    {
331
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
332
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
333
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
334

    
335
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
336
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
337
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
338

    
339
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
340
      {
341
      if( faceIsVisible(mLastTouchedFace) )
342
        {
343
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
344

    
345
        float ax = mFaceAxis[mLastTouchedFace].get0();
346
        float ay = mFaceAxis[mLastTouchedFace].get1();
347
        float az = mFaceAxis[mLastTouchedFace].get2();
348

    
349
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
350
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
351
        }
352
      }
353

    
354
    return false;
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
360
    {
361
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
362
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
363
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
364

    
365
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
366

    
367
    float ax = mFaceAxis[mLastTouchedFace].get0();
368
    float ay = mFaceAxis[mLastTouchedFace].get1();
369
    float az = mFaceAxis[mLastTouchedFace].get2();
370

    
371
    convertTo2Dcoords(mTouch, ax,ay,az, mMove2D);
372

    
373
    mMove2D[0] -= mPoint2D[0];
374
    mMove2D[1] -= mPoint2D[1];
375

    
376
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
377
    int rotIndex = computeRotationIndex( mCastedRotAxis[mLastTouchedFace], mMove2D, mEnabledRotAxis);
378
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
379
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
380

    
381
    output[0] = rotIndex;
382
    output[1] = row;
383
    }
384

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

    
389
  public void getCastedRotAxis(float[] output, Static4D quat, int rotIndex)
390
    {
391
    Static4D axis = mCastedRotAxis4D[mLastTouchedFace][rotIndex];
392
    Static4D result = QuatHelper.rotateVectorByQuat(axis, quat);
393

    
394
    output[0] =result.get0();
395
    output[1] =result.get1();
396

    
397
    float len = (float)Math.sqrt(output[0]*output[0] + output[1]*output[1]);
398
    output[0] /= len;
399
    output[1] /= len;
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403

    
404
  public int getTouchedCubitFace()
405
    {
406
    return 0;
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public int getTouchedCubit()
412
    {
413
    return 0;
414
    }
415
  }
(8-8/11)