Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / movement / Movement.java @ 23afe4c4

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.movement;
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 Movement
29
  {
30
  // it doesn't matter where we touch a face - the list of enabled rotAxis will always be the same
31
  public static final int TYPE_NOT_SPLIT    = 0;
32
  // each face is split into several parts by lines coming from its center to the midpoints of each edge
33
  public static final int TYPE_SPLIT_EDGE   = 1;
34
  // each face is split into several parts by lines coming from its center to the vertices
35
  public static final int TYPE_SPLIT_CORNER = 2;
36

    
37
  public static final int MOVEMENT_HEXAHEDRON   = 6;
38
  public static final int MOVEMENT_TETRAHEDRON  = 4;
39
  public static final int MOVEMENT_OCTAHEDRON   = 8;
40
  public static final int MOVEMENT_DODECAHEDRON =12;
41
  public static final int MOVEMENT_SHAPECHANGE  = 0;
42

    
43
  static final float SQ3 = (float)Math.sqrt(3);
44
  static final float SQ6 = (float)Math.sqrt(6);
45

    
46
  private final int mNumFaceAxis;
47
  private final float[] mPoint, mCamera, mTouch;
48
  private final float[] mPoint2D, mMove2D;
49
  private final int[] mEnabledRotAxis;
50
  private final float[] mDistanceCenterFace3D;
51
  private final Static3D[] mFaceAxis;
52

    
53
  private int mLastTouchedFace;
54
  private float[][][] mCastedRotAxis;
55
  private Static4D[][] mCastedRotAxis4D;
56
  private float[][] mTouchBorders, mA, mB;
57
  private float mObjectRatio;
58

    
59
  private final int mType;
60
  private final int[][][] mEnabled;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  abstract int returnPart(int type, int face, float[] touchPoint);
65
  abstract boolean isInsideFace(int face, float[] point);
66
  public abstract float returnRotationFactor(int[] numLayers, int row);
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  Movement(Static3D[] rotAxis, Static3D[] faceAxis, float[][] cuts, boolean[][] rotatable,
71
           float[] distance3D, float size, int type, int[][][] enabled)
72
    {
73
    mPoint = new float[3];
74
    mCamera= new float[3];
75
    mTouch = new float[3];
76

    
77
    mPoint2D = new float[2];
78
    mMove2D  = new float[2];
79

    
80
    mType       = type;
81
    mEnabled    = enabled;
82
    mObjectRatio= 1.0f;
83
    mFaceAxis   = faceAxis;
84
    mNumFaceAxis= mFaceAxis.length;
85

    
86
    mEnabledRotAxis = new int[rotAxis.length+1];
87

    
88
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
89

    
90
    computeCastedAxis(rotAxis);
91
    computeBorders(cuts,rotatable,size);
92
    computeLinear(rotAxis,faceAxis);
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
97
// 1st faceAxis.
98

    
99
  private void computeCastedAxis(Static3D[] rotAxis)
100
    {
101
    mCastedRotAxis   = new float[mNumFaceAxis][rotAxis.length][2];
102
    mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
103

    
104
    float fx,fy,fz,f;
105

    
106
    for( int casted=0; casted<rotAxis.length; casted++)
107
      {
108
      Static3D a = rotAxis[casted];
109
      mPoint[0]= a.get0();
110
      mPoint[1]= a.get1();
111
      mPoint[2]= a.get2();
112

    
113
      for( int face=0; face<mNumFaceAxis; face++)
114
        {
115
        convertTo2Dcoords( mPoint, face, mCastedRotAxis[face][casted]);
116
        normalize2D(mCastedRotAxis[face][casted]);
117

    
118
        fx = mFaceAxis[face].get0();
119
        fy = mFaceAxis[face].get1();
120
        fz = mFaceAxis[face].get2();
121
        f  = mPoint[0]*fx + mPoint[1]*fy + mPoint[2]*fz;
122
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*fx, mPoint[1]-f*fy, mPoint[2]-f*fz, 0);
123
        }
124
      }
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  private void normalize2D(float[] vect)
130
    {
131
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
132
    vect[0] /= len;
133
    vect[1] /= len;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// find the casted axis with which our move2D vector forms an angle closest to 90 deg.
138

    
139
  private int computeRotationIndex(int faceAxis, float[] move2D, int[] enabled)
140
    {
141
    float cosAngle, minCosAngle = Float.MAX_VALUE;
142
    int minIndex=0, index;
143
    float m0 = move2D[0];
144
    float m1 = move2D[1];
145
    float len = (float)Math.sqrt(m0*m0 + m1*m1);
146

    
147
    if( len!=0.0f )
148
      {
149
      m0 /= len;
150
      m1 /= len;
151
      }
152
    else
153
      {
154
      m0 = 1.0f;  // arbitrarily
155
      m1 = 0.0f;  //
156
      }
157

    
158
    int numAxis = enabled[0];
159

    
160
    for(int axis=1; axis<=numAxis; axis++)
161
      {
162
      index = enabled[axis];
163
      cosAngle = m0*mCastedRotAxis[faceAxis][index][0] + m1*mCastedRotAxis[faceAxis][index][1];
164
      if( cosAngle<0 ) cosAngle = -cosAngle;
165

    
166
      if( cosAngle<minCosAngle )
167
        {
168
        minCosAngle=cosAngle;
169
        minIndex = index;
170
        }
171
      }
172

    
173
    return minIndex;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
// in the center of the face offset is always 0 regardless of the axis
178

    
179
  private float computeOffset(float[] point, float[] axis)
180
    {
181
    return point[0]*axis[0] + point[1]*axis[1];
182
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private boolean faceIsVisible(int index)
187
    {
188
    Static3D faceAxis = mFaceAxis[index];
189
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
190
    return castCameraOnAxis > mDistanceCenterFace3D[index];
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
195
// compute point 'output[]' which:
196
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
197
// 2) is co-linear with mCamera and mPoint
198
//
199
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
200

    
201
  private void castTouchPointOntoFace(int index, float[] output)
202
    {
203
    Static3D faceAxis = mFaceAxis[index];
204

    
205
    float d0 = mPoint[0]-mCamera[0];
206
    float d1 = mPoint[1]-mCamera[1];
207
    float d2 = mPoint[2]-mCamera[2];
208
    float a0 = faceAxis.get0();
209
    float a1 = faceAxis.get1();
210
    float a2 = faceAxis.get2();
211

    
212
    float denom = a0*d0 + a1*d1 + a2*d2;
213

    
214
    if( denom != 0.0f )
215
      {
216
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
217
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
218

    
219
      output[0] = mCamera[0] + d0*alpha;
220
      output[1] = mCamera[1] + d1*alpha;
221
      output[2] = mCamera[2] + d2*alpha;
222
      }
223
    }
224

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

    
235
  private void convertTo2Dcoords(float[] point3D, int index , float[] output)
236
    {
237
    Static3D faceAxis = mFaceAxis[index];
238

    
239
    float y0,y1,y2; // base Y vector of the 2D coord system
240
    float a0 = faceAxis.get0();
241
    float a1 = faceAxis.get1();
242
    float a2 = faceAxis.get2();
243

    
244
    if( a0==0.0f && a2==0.0f )
245
      {
246
      y0=0; y1=0; y2=-a1;
247
      }
248
    else if( a1==0.0f )
249
      {
250
      y0=0; y1=1; y2=0;
251
      }
252
    else
253
      {
254
      float norm = (float)(-a1/Math.sqrt(1-a1*a1));
255
      y0 = norm*a0; y1= norm*(a1-1/a1); y2=norm*a2;
256
      }
257

    
258
    float x0 = y1*a2 - y2*a1;  //
259
    float x1 = y2*a0 - y0*a2;  // (2D coord baseY) x (axis) = 2D coord baseX
260
    float x2 = y0*a1 - y1*a0;  //
261

    
262
    float originAlpha = point3D[0]*a0 + point3D[1]*a1 + point3D[2]*a2;
263

    
264
    float origin0 = originAlpha*a0; // coords of the point where axis
265
    float origin1 = originAlpha*a1; // intersects surface plane i.e.
266
    float origin2 = originAlpha*a2; // the origin of our 2D coord system
267

    
268
    float v0 = point3D[0] - origin0;
269
    float v1 = point3D[1] - origin1;
270
    float v2 = point3D[2] - origin2;
271

    
272
    output[0] = v0*x0 + v1*x1 + v2*x2;
273
    output[1] = v0*y0 + v1*y1 + v2*y2;
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
279
    {
280
    if( cuts==null ) return null;
281

    
282
    int len = cuts.length;
283
    float[] border = new float[len];
284

    
285
    for(int i=0; i<len; i++)
286
      {
287
      if( !rotatable[i] )
288
        {
289
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
290
        }
291
      else
292
        {
293
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
294
        else
295
          {
296
          int found = -1;
297

    
298
          for(int j=i+2; j<=len; j++)
299
            {
300
            if( rotatable[j] )
301
              {
302
              found=j;
303
              break;
304
              }
305
            }
306

    
307
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
308
          }
309
        }
310
      }
311

    
312
    return border;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
317

    
318
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
319
    {
320
    int numCuts = cuts.length;
321
    mTouchBorders = new float[numCuts][];
322

    
323
    for(int axis=0; axis<numCuts; axis++)
324
      {
325
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
326
      }
327
    }
328

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

    
331
  private int computeSign(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
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
341
    }
342

    
343
///////////////////////////////////////////////////////////////////////////////////////////////////
344

    
345
  private float crossProductLen(Static3D a, Static3D b)
346
    {
347
    float a1 = a.get0();
348
    float a2 = a.get1();
349
    float a3 = a.get2();
350
    float b1 = b.get0();
351
    float b2 = b.get1();
352
    float b3 = b.get2();
353

    
354
    float x1 = a2*b3-a3*b2;
355
    float x2 = a3*b1-a1*b3;
356
    float x3 = a1*b2-a2*b1;
357

    
358
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
359
    }
360

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

    
366
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
367
    {
368
    int numFaces = faceAxis.length;
369
    int numRot   = rotAxis.length;
370

    
371
    mA = new float[numFaces][numRot];
372
    mB = new float[numFaces][numRot];
373

    
374
    for(int i=0; i<numFaces; i++)
375
      for(int j=0; j<numRot; j++)
376
        {
377
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
378

    
379
        if( mA[i][j]!=0.0f )
380
          {
381
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
382
          int sign = computeSign(faceAxis[i],rotAxis[j]);
383
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
384
          }
385
        else mB[i][j] = 0.0f;
386
        }
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  private int computeRowFromOffset(int face, int axisIndex, float offset)
392
    {
393
    float[] borders = mTouchBorders[axisIndex];
394

    
395
    if( borders==null ) return 0;
396

    
397
    int len = borders.length;
398
    float A = mA[face][axisIndex];
399

    
400
    if( A!=0.0f )
401
      {
402
      float B = mB[face][axisIndex];
403

    
404
      for(int i=0; i<len; i++)
405
        {
406
        float translated = B + borders[i]/A;
407
        if( offset<translated ) return i;
408
        }
409
      }
410

    
411
    return len;
412
    }
413

    
414
///////////////////////////////////////////////////////////////////////////////////////////////////
415

    
416
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
417
    {
418
    int part = returnPart(mType,face,touchPoint);
419

    
420
    int num = mEnabled[face][0].length;
421
    enabled[0] = num;
422
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426
// PUBLIC API
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  public boolean faceTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
430
    {
431
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
432
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
433
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
434

    
435
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
436
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
437
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
438

    
439
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
440
      {
441
      if( faceIsVisible(mLastTouchedFace) )
442
        {
443
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
444
        convertTo2Dcoords(mTouch, mLastTouchedFace, mPoint2D);
445
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
446
        }
447
      }
448

    
449
    return false;
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

    
454
  public Static2D newRotation(Static4D rotatedTouchPoint)
455
    {
456
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
457
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
458
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
459

    
460
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
461
    convertTo2Dcoords(mTouch, mLastTouchedFace, mMove2D);
462

    
463
    mMove2D[0] -= mPoint2D[0];
464
    mMove2D[1] -= mPoint2D[1];
465

    
466
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
467
    int rotIndex = computeRotationIndex(mLastTouchedFace, mMove2D, mEnabledRotAxis);
468
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
469
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
470

    
471
    return new Static2D(rotIndex,row);
472
    }
473

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

    
476
  public Static4D getCastedRotAxis(int rotIndex)
477
    {
478
    return mCastedRotAxis4D[mLastTouchedFace][rotIndex];
479
    }
480

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

    
483
  public int getTouchedFace()
484
    {
485
    return mLastTouchedFace;
486
    }
487

    
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489

    
490
  public float[] getTouchedPoint3D()
491
    {
492
    return mTouch;
493
    }
494

    
495
///////////////////////////////////////////////////////////////////////////////////////////////////
496

    
497
  public void setObjectRatio(float ratio)
498
    {
499
    mObjectRatio = ratio;
500
    }
501
  }
(1-1/6)