Project

General

Profile

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

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

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
    float fx,fy,fz,f;
97

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

    
105
      for( int face=0; face<mNumFaceAxis; face++)
106
        {
107
        convertTo2Dcoords( mPoint, face, mCastedRotAxis[face][casted]);
108
        normalize2D(mCastedRotAxis[face][casted]);
109

    
110
        fx = mFaceAxis[face].get0();
111
        fy = mFaceAxis[face].get1();
112
        fz = mFaceAxis[face].get2();
113
        f  = mPoint[0]*fx + mPoint[1]*fy + mPoint[2]*fz;
114
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*fx, mPoint[1]-f*fy, mPoint[2]-f*fz, 0);
115
        }
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
130

    
131
  private int computeRotationIndex(int faceAxis, float[] move2D, int[] enabled)
132
    {
133
    float cosAngle, minCosAngle = Float.MAX_VALUE;
134
    int minIndex=0, index;
135
    float m0 = move2D[0];
136
    float m1 = move2D[1];
137
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
138

    
139
    if( len!=0.0f )
140
      {
141
      m0 /= len;
142
      m1 /= len;
143
      }
144
    else
145
      {
146
      m0 = 1.0f;  // arbitrarily
147
      m1 = 0.0f;  //
148
      }
149

    
150
    int numAxis = enabled[0];
151

    
152
    for(int axis=1; axis<=numAxis; axis++)
153
      {
154
      index = enabled[axis];
155
      cosAngle = m0*mCastedRotAxis[faceAxis][index][0] + m1*mCastedRotAxis[faceAxis][index][1];
156
      if( cosAngle<0 ) cosAngle = -cosAngle;
157

    
158
      if( cosAngle<minCosAngle )
159
        {
160
        minCosAngle=cosAngle;
161
        minIndex = index;
162
        }
163
      }
164

    
165
    return minIndex;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
// in the center of the face offset is always 0 regardless of the axis
170

    
171
  private float computeOffset(float[] point, float[] axis)
172
    {
173
    return point[0]*axis[0] + point[1]*axis[1];
174
    }
175

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

    
178
  private boolean faceIsVisible(int index)
179
    {
180
    Static3D faceAxis = mFaceAxis[index];
181
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
182
    return castCameraOnAxis > mDistanceCenterFace3D[index];
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
187
// compute point 'output[]' which:
188
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
189
// 2) is co-linear with mCamera and mPoint
190
//
191
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
192

    
193
  private void castTouchPointOntoFace(int index, float[] output)
194
    {
195
    Static3D faceAxis = mFaceAxis[index];
196

    
197
    float d0 = mPoint[0]-mCamera[0];
198
    float d1 = mPoint[1]-mCamera[1];
199
    float d2 = mPoint[2]-mCamera[2];
200
    float a0 = faceAxis.get0();
201
    float a1 = faceAxis.get1();
202
    float a2 = faceAxis.get2();
203

    
204
    float denom = a0*d0 + a1*d1 + a2*d2;
205

    
206
    if( denom != 0.0f )
207
      {
208
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
209
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
210

    
211
      output[0] = mCamera[0] + d0*alpha;
212
      output[1] = mCamera[1] + d1*alpha;
213
      output[2] = mCamera[2] + d2*alpha;
214
      }
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
219
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
220
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
221
// original 3D Y axis and our 2D in-plane origin.
222
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
223
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
224
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
225
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
226

    
227
  private void convertTo2Dcoords(float[] point3D, int index , float[] output)
228
    {
229
    Static3D faceAxis = mFaceAxis[index];
230

    
231
    float y0,y1,y2; // base Y vector of the 2D coord system
232
    float a0 = faceAxis.get0();
233
    float a1 = faceAxis.get1();
234
    float a2 = faceAxis.get2();
235

    
236
    if( a0==0.0f && a2==0.0f )
237
      {
238
      y0=0; y1=0; y2=-a1;
239
      }
240
    else if( a1==0.0f )
241
      {
242
      y0=0; y1=1; y2=0;
243
      }
244
    else
245
      {
246
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
247
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
248
      }
249

    
250
    float x0 = y1*a2 - y2*a1;  //
251
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
252
    float x2 = y0*a1 - y1*a0;  //
253

    
254
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
255

    
256
    float origin0 = originAlpha*a0; // coords of the point where axis
257
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
258
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
259

    
260
    float v0 = point3D[0] - origin0;
261
    float v1 = point3D[1] - origin1;
262
    float v2 = point3D[2] - origin2;
263

    
264
    output[0] = v0*x0 + v1*x1 + v2*x2;
265
    output[1] = v0*y0 + v1*y1 + v2*y2;
266
    }
267

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

    
270
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
271
    {
272
    if( cuts==null ) return null;
273

    
274
    int len = cuts.length;
275
    float[] border = new float[len];
276

    
277
    for(int i=0; i<len; i++)
278
      {
279
      if( !rotatable[i] )
280
        {
281
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
282
        }
283
      else
284
        {
285
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
286
        else
287
          {
288
          int found = -1;
289

    
290
          for(int j=i+2; j<=len; j++)
291
            {
292
            if( rotatable[j] )
293
              {
294
              found=j;
295
              break;
296
              }
297
            }
298

    
299
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
300
          }
301
        }
302
      }
303

    
304
    return border;
305
    }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
309

    
310
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
311
    {
312
    int numCuts = cuts.length;
313
    mTouchBorders = new float[numCuts][];
314

    
315
    for(int axis=0; axis<numCuts; axis++)
316
      {
317
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
318
      }
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

    
323
  private int computeSign(Static3D a, Static3D b)
324
    {
325
    float a1 = a.get0();
326
    float a2 = a.get1();
327
    float a3 = a.get2();
328
    float b1 = b.get0();
329
    float b2 = b.get1();
330
    float b3 = b.get2();
331

    
332
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  private float crossProductLen(Static3D a, Static3D b)
338
    {
339
    float a1 = a.get0();
340
    float a2 = a.get1();
341
    float a3 = a.get2();
342
    float b1 = b.get0();
343
    float b2 = b.get1();
344
    float b3 = b.get2();
345

    
346
    float x1 = a2*b3-a3*b2;
347
    float x2 = a3*b1-a1*b3;
348
    float x3 = a1*b2-a2*b1;
349

    
350
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
351
    }
352

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

    
358
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
359
    {
360
    int numFaces = faceAxis.length;
361
    int numRot   = rotAxis.length;
362

    
363
    mA = new float[numFaces][numRot];
364
    mB = new float[numFaces][numRot];
365

    
366
    for(int i=0; i<numFaces; i++)
367
      for(int j=0; j<numRot; j++)
368
        {
369
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
370

    
371
        if( mA[i][j]!=0.0f )
372
          {
373
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
374
          int sign = computeSign(faceAxis[i],rotAxis[j]);
375
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
376
          }
377
        else mB[i][j] = 0.0f;
378
        }
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
  private int computeRowFromOffset(int face, int axisIndex, float offset)
384
    {
385
    float[] borders = mTouchBorders[axisIndex];
386

    
387
    if( borders==null ) return 0;
388

    
389
    int len = borders.length;
390
    float A = mA[face][axisIndex];
391

    
392
    if( A!=0.0f )
393
      {
394
      float B = mB[face][axisIndex];
395

    
396
      for(int i=0; i<len; i++)
397
        {
398
        float translated = B + borders[i]/A;
399
        if( offset<translated ) return i;
400
        }
401
      }
402

    
403
    return len;
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
409
    {
410
    int part = returnPart(mSplit,face,touchPoint);
411

    
412
    int num = mEnabled[face][0].length;
413
    enabled[0] = num;
414
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418
// PUBLIC API
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
422
    {
423
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
424
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
425
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
426

    
427
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
428
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
429
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
430

    
431
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
432
      {
433
      if( faceIsVisible(mLastTouchedFace) )
434
        {
435
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
436
        convertTo2Dcoords(mTouch, mLastTouchedFace, mPoint2D);
437
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
438
        }
439
      }
440

    
441
    return false;
442
    }
443

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

    
446
  public void newRotation(int[] output, Static4D rotatedTouchPoint)
447
    {
448
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
449
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
450
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
451

    
452
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
453
    convertTo2Dcoords(mTouch, mLastTouchedFace, mMove2D);
454

    
455
    mMove2D[0] -= mPoint2D[0];
456
    mMove2D[1] -= mPoint2D[1];
457

    
458
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
459
    int rotIndex = computeRotationIndex(mLastTouchedFace, mMove2D, mEnabledRotAxis);
460
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
461
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
462

    
463
    output[0] = rotIndex;
464
    output[1] = row;
465
    }
466

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

    
471
  public void getCastedRotAxis(float[] output, Static4D quat, int rotIndex)
472
    {
473
    Static4D axis = mCastedRotAxis4D[mLastTouchedFace][rotIndex];
474
    Static4D result = QuatHelper.rotateVectorByQuat(axis, quat);
475

    
476
    output[0] =result.get0();
477
    output[1] =result.get1();
478

    
479
    float len = (float)Math.sqrt(output[0]*output[0] + output[1]*output[1]);
480
    output[0] /= len;
481
    output[1] /= len;
482
    }
483

    
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485

    
486
  public int getTouchedCubitFace()
487
    {
488
    return 0;
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492

    
493
  public int getTouchedCubit()
494
    {
495
    return 0;
496
    }
497
  }
(7-7/8)