Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / Movement.java @ 59c20632

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.main;
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   = 0;
38
  public static final int MOVEMENT_TETRAHEDRON  = 1;
39
  public static final int MOVEMENT_OCTAHEDRON   = 2;
40
  public static final int MOVEMENT_DODECAHEDRON = 3;
41
  public static final int MOVEMENT_SHAPECHANGE  = 4;
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

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

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

    
79
    mType = type;
80
    mEnabled = enabled;
81

    
82
    mFaceAxis   = faceAxis;
83
    mNumFaceAxis= mFaceAxis.length;
84

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

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

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

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

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

    
103
    float fx,fy,fz,f;
104

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

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

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

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

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

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

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

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

    
157
    int numAxis = enabled[0];
158

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

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

    
172
    return minIndex;
173
    }
174

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

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

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

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

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

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

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

    
311
    return border;
312
    }
313

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

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

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

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

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

    
339
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

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

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

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

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

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

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

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

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

    
388
///////////////////////////////////////////////////////////////////////////////////////////////////
389

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

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

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

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

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

    
410
    return len;
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

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

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

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
// PUBLIC API
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

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

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

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

    
448
    return false;
449
    }
450

    
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452

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

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

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

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

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

    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474

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

    
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481

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

    
487
///////////////////////////////////////////////////////////////////////////////////////////////////
488

    
489
  public float[] getTouchedPoint3D()
490
    {
491
    return mTouch;
492
    }
493
  }
(2-2/16)