Project

General

Profile

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

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

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.type.Static2D;
23
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

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

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

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

    
46
  private final int mType;
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(Static3D[] rotAxis, Static3D[] faceAxis, float[][] cuts, boolean[][] rotatable,
57
                            float[] distance3D, float size, int type, int[][][] enabled)
58
    {
59
    mPoint = new float[3];
60
    mCamera= new float[3];
61
    mTouch = new float[3];
62

    
63
    mPoint2D = new float[2];
64
    mMove2D  = new float[2];
65

    
66
    mType       = type;
67
    mEnabled    = enabled;
68
    mObjectRatio= 1.0f;
69
    mFaceAxis   = faceAxis;
70
    mNumFaceAxis= mFaceAxis.length;
71

    
72
    mEnabledRotAxis = new int[rotAxis.length+1];
73

    
74
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
75

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

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

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

    
90
    float fx,fy,fz,f;
91

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

    
99
      for( int face=0; face<mNumFaceAxis; face++)
100
        {
101
        convertTo2Dcoords( mPoint, face, mCastedRotAxis[face][casted]);
102
        normalize2D(mCastedRotAxis[face][casted]);
103

    
104
        fx = mFaceAxis[face].get0();
105
        fy = mFaceAxis[face].get1();
106
        fz = mFaceAxis[face].get2();
107
        f  = mPoint[0]*fx + mPoint[1]*fy + mPoint[2]*fz;
108
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*fx, mPoint[1]-f*fy, mPoint[2]-f*fz, 0);
109
        }
110
      }
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  private void normalize2D(float[] vect)
116
    {
117
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
118
    vect[0] /= len;
119
    vect[1] /= len;
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
124

    
125
  private int computeRotationIndex(int faceAxis, float[] move2D, int[] enabled)
126
    {
127
    float cosAngle, minCosAngle = Float.MAX_VALUE;
128
    int minIndex=0, index;
129
    float m0 = move2D[0];
130
    float m1 = move2D[1];
131
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
132

    
133
    if( len!=0.0f )
134
      {
135
      m0 /= len;
136
      m1 /= len;
137
      }
138
    else
139
      {
140
      m0 = 1.0f;  // arbitrarily
141
      m1 = 0.0f;  //
142
      }
143

    
144
    int numAxis = enabled[0];
145

    
146
    for(int axis=1; axis<=numAxis; axis++)
147
      {
148
      index = enabled[axis];
149
      cosAngle = m0*mCastedRotAxis[faceAxis][index][0] + m1*mCastedRotAxis[faceAxis][index][1];
150
      if( cosAngle<0 ) cosAngle = -cosAngle;
151

    
152
      if( cosAngle<minCosAngle )
153
        {
154
        minCosAngle=cosAngle;
155
        minIndex = index;
156
        }
157
      }
158

    
159
    return minIndex;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// in the center of the face offset is always 0 regardless of the axis
164

    
165
  private float computeOffset(float[] point, float[] axis)
166
    {
167
    return point[0]*axis[0] + point[1]*axis[1];
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private boolean faceIsVisible(int index)
173
    {
174
    Static3D faceAxis = mFaceAxis[index];
175
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
176
    return castCameraOnAxis > mDistanceCenterFace3D[index];
177
    }
178

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

    
187
  private void castTouchPointOntoFace(int index, float[] output)
188
    {
189
    Static3D faceAxis = mFaceAxis[index];
190

    
191
    float d0 = mPoint[0]-mCamera[0];
192
    float d1 = mPoint[1]-mCamera[1];
193
    float d2 = mPoint[2]-mCamera[2];
194
    float a0 = faceAxis.get0();
195
    float a1 = faceAxis.get1();
196
    float a2 = faceAxis.get2();
197

    
198
    float denom = a0*d0 + a1*d1 + a2*d2;
199

    
200
    if( denom != 0.0f )
201
      {
202
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
203
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
204

    
205
      output[0] = mCamera[0] + d0*alpha;
206
      output[1] = mCamera[1] + d1*alpha;
207
      output[2] = mCamera[2] + d2*alpha;
208
      }
209
    }
210

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

    
221
  private void convertTo2Dcoords(float[] point3D, int index , float[] output)
222
    {
223
    Static3D faceAxis = mFaceAxis[index];
224

    
225
    float y0,y1,y2; // base Y vector of the 2D coord system
226
    float a0 = faceAxis.get0();
227
    float a1 = faceAxis.get1();
228
    float a2 = faceAxis.get2();
229

    
230
    if( a0==0.0f && a2==0.0f )
231
      {
232
      y0=0; y1=0; y2=-a1;
233
      }
234
    else if( a1==0.0f )
235
      {
236
      y0=0; y1=1; y2=0;
237
      }
238
    else
239
      {
240
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
241
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
242
      }
243

    
244
    float x0 = y1*a2 - y2*a1;  //
245
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
246
    float x2 = y0*a1 - y1*a0;  //
247

    
248
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
249

    
250
    float origin0 = originAlpha*a0; // coords of the point where axis
251
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
252
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
253

    
254
    float v0 = point3D[0] - origin0;
255
    float v1 = point3D[1] - origin1;
256
    float v2 = point3D[2] - origin2;
257

    
258
    output[0] = v0*x0 + v1*x1 + v2*x2;
259
    output[1] = v0*y0 + v1*y1 + v2*y2;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
265
    {
266
    if( cuts==null ) return null;
267

    
268
    int len = cuts.length;
269
    float[] border = new float[len];
270

    
271
    for(int i=0; i<len; i++)
272
      {
273
      if( !rotatable[i] )
274
        {
275
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
276
        }
277
      else
278
        {
279
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
280
        else
281
          {
282
          int found = -1;
283

    
284
          for(int j=i+2; j<=len; j++)
285
            {
286
            if( rotatable[j] )
287
              {
288
              found=j;
289
              break;
290
              }
291
            }
292

    
293
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
294
          }
295
        }
296
      }
297

    
298
    return border;
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
303

    
304
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
305
    {
306
    int numCuts = cuts.length;
307
    mTouchBorders = new float[numCuts][];
308

    
309
    for(int axis=0; axis<numCuts; axis++)
310
      {
311
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  private int computeSign(Static3D a, Static3D b)
318
    {
319
    float a1 = a.get0();
320
    float a2 = a.get1();
321
    float a3 = a.get2();
322
    float b1 = b.get0();
323
    float b2 = b.get1();
324
    float b3 = b.get2();
325

    
326
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330

    
331
  private float crossProductLen(Static3D a, Static3D b)
332
    {
333
    float a1 = a.get0();
334
    float a2 = a.get1();
335
    float a3 = a.get2();
336
    float b1 = b.get0();
337
    float b2 = b.get1();
338
    float b3 = b.get2();
339

    
340
    float x1 = a2*b3-a3*b2;
341
    float x2 = a3*b1-a1*b3;
342
    float x3 = a1*b2-a2*b1;
343

    
344
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
345
    }
346

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

    
352
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
353
    {
354
    int numFaces = faceAxis.length;
355
    int numRot   = rotAxis.length;
356

    
357
    mA = new float[numFaces][numRot];
358
    mB = new float[numFaces][numRot];
359

    
360
    for(int i=0; i<numFaces; i++)
361
      for(int j=0; j<numRot; j++)
362
        {
363
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
364

    
365
        if( mA[i][j]!=0.0f )
366
          {
367
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
368
          int sign = computeSign(faceAxis[i],rotAxis[j]);
369
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
370
          }
371
        else mB[i][j] = 0.0f;
372
        }
373
    }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
  private int computeRowFromOffset(int face, int axisIndex, float offset)
378
    {
379
    float[] borders = mTouchBorders[axisIndex];
380

    
381
    if( borders==null ) return 0;
382

    
383
    int len = borders.length;
384
    float A = mA[face][axisIndex];
385

    
386
    if( A!=0.0f )
387
      {
388
      float B = mB[face][axisIndex];
389

    
390
      for(int i=0; i<len; i++)
391
        {
392
        float translated = B + borders[i]/A;
393
        if( offset<translated ) return i;
394
        }
395
      }
396

    
397
    return len;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401

    
402
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
403
    {
404
    int part = returnPart(mType,face,touchPoint);
405

    
406
    int num = mEnabled[face][0].length;
407
    enabled[0] = num;
408
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
// PUBLIC API
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
416
    {
417
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
418
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
419
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
420

    
421
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
422
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
423
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
424

    
425
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
426
      {
427
      if( faceIsVisible(mLastTouchedFace) )
428
        {
429
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
430
        convertTo2Dcoords(mTouch, mLastTouchedFace, mPoint2D);
431
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
432
        }
433
      }
434

    
435
    return false;
436
    }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

    
440
  public Static2D newRotation(Static4D rotatedTouchPoint)
441
    {
442
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
443
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
444
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
445

    
446
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
447
    convertTo2Dcoords(mTouch, mLastTouchedFace, mMove2D);
448

    
449
    mMove2D[0] -= mPoint2D[0];
450
    mMove2D[1] -= mPoint2D[1];
451

    
452
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
453
    int rotIndex = computeRotationIndex(mLastTouchedFace, mMove2D, mEnabledRotAxis);
454
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
455
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
456

    
457
    return new Static2D(rotIndex,row);
458
    }
459

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

    
462
  public Static4D getCastedRotAxis(int rotIndex)
463
    {
464
    return mCastedRotAxis4D[mLastTouchedFace][rotIndex];
465
    }
466

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

    
469
  public int getTouchedFace()
470
    {
471
    return mLastTouchedFace;
472
    }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475

    
476
  public float[] getTouchedPoint3D()
477
    {
478
    return mTouch;
479
    }
480

    
481
///////////////////////////////////////////////////////////////////////////////////////////////////
482

    
483
  public void setObjectRatio(float ratio)
484
    {
485
    mObjectRatio = ratio;
486
    }
487
  }
(6-6/7)