Project

General

Profile

« Previous | Next » 

Revision 23afe4c4

Added by Leszek Koltunski over 2 years ago

Move the Movement to its own package; abstract out some stuff.

View differences:

src/main/java/org/distorted/objectlib/json/JsonReader.java
25 25
import java.io.InputStreamReader;
26 26
import java.nio.charset.StandardCharsets;
27 27

  
28
import org.distorted.objectlib.main.Movement;
28
import org.distorted.objectlib.movement.Movement;
29 29
import org.json.JSONArray;
30 30
import org.json.JSONException;
31 31
import org.json.JSONObject;
src/main/java/org/distorted/objectlib/main/Movement.java
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   = 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

  
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
  }
src/main/java/org/distorted/objectlib/main/MovementCuboids.java
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.Static3D;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
// Cuboids
26

  
27
public class MovementCuboids extends Movement
28
{
29
  private final float[] mDist3D;
30

  
31
  public static final Static3D[] FACE_AXIS = new Static3D[]
32
         {
33
           new Static3D(1,0,0), new Static3D(-1,0,0),
34
           new Static3D(0,1,0), new Static3D(0,-1,0),
35
           new Static3D(0,0,1), new Static3D(0,0,-1)
36
         };
37

  
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

  
40
  public MovementCuboids(Static3D[] rotAxis, float[][] cuts, boolean[][] rotatable, float size, int type,
41
                         int[][][] enabled, float[] dist3D)
42
    {
43
    super(rotAxis, FACE_AXIS, cuts, rotatable, dist3D, size, type, enabled);
44

  
45
    mDist3D = dist3D;
46
    }
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

  
50
  int returnPart(int type, int face, float[] touchPoint)
51
    {
52
    return 0;
53
    }
54

  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

  
57
  public float returnRotationFactor(int[] numLayers, int row)
58
    {
59
    return 1.0f;
60
    }
61

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

  
64
  boolean isInsideFace(int face, float[] p)
65
    {
66
    switch(face/2)
67
      {
68
      case 0: return ( p[0]<=mDist3D[4] && p[0]>=-mDist3D[4] && p[1]<=mDist3D[2] && p[1]>=-mDist3D[2] );
69
      case 1: return ( p[0]<=mDist3D[0] && p[0]>=-mDist3D[0] && p[1]<=mDist3D[4] && p[1]>=-mDist3D[4] );
70
      case 2: return ( p[0]<=mDist3D[0] && p[0]>=-mDist3D[0] && p[1]<=mDist3D[2] && p[1]>=-mDist3D[2] );
71
      }
72
    return false;
73
    }
74
}
src/main/java/org/distorted/objectlib/main/MovementDodecahedron.java
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 static org.distorted.objectlib.main.TwistyObject.SQ5;
23

  
24
import org.distorted.library.type.Static3D;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27
// Dodecahedral objects: map the 2D swipes of user's fingers to 3D rotations
28

  
29
public class MovementDodecahedron extends Movement
30
{
31
  public static final float C2       = (SQ5+3)/4;
32
  public static final float LEN      = (float)(Math.sqrt(1.25f+0.5f*SQ5));
33
  public static final float SIN54    = (SQ5+1)/4;
34
  public static final float COS54    = (float)(Math.sqrt(10-2*SQ5)/4);
35

  
36
  public  static final float DIST3D = (float)Math.sqrt(0.625f+0.275f*SQ5);
37
  private static final float DIST2D = (SIN54/COS54)/2;
38
  private static final float[] D3D  = { DIST3D,DIST3D,DIST3D,DIST3D,DIST3D,DIST3D,
39
                                        DIST3D,DIST3D,DIST3D,DIST3D,DIST3D,DIST3D };
40

  
41
  public static final Static3D[] FACE_AXIS = new Static3D[]
42
         {
43
           new Static3D(    C2/LEN, SIN54/LEN,    0      ),
44
           new Static3D(    C2/LEN,-SIN54/LEN,    0      ),
45
           new Static3D(   -C2/LEN, SIN54/LEN,    0      ),
46
           new Static3D(   -C2/LEN,-SIN54/LEN,    0      ),
47
           new Static3D( 0        ,    C2/LEN, SIN54/LEN ),
48
           new Static3D( 0        ,    C2/LEN,-SIN54/LEN ),
49
           new Static3D( 0        ,   -C2/LEN, SIN54/LEN ),
50
           new Static3D( 0        ,   -C2/LEN,-SIN54/LEN ),
51
           new Static3D( SIN54/LEN,    0     ,    C2/LEN ),
52
           new Static3D( SIN54/LEN,    0     ,   -C2/LEN ),
53
           new Static3D(-SIN54/LEN,    0     ,    C2/LEN ),
54
           new Static3D(-SIN54/LEN,    0     ,   -C2/LEN )
55
         };
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
  public MovementDodecahedron(Static3D[] rotAxis, float[][] cuts, boolean[][] rotatable, float size, int type, int[][][] enabled)
60
    {
61
    super(rotAxis, FACE_AXIS, cuts,rotatable,D3D, size, type, enabled);
62
    }
63

  
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
  public float returnRotationFactor(int[] numLayers, int row)
67
    {
68
    return 1.0f;
69
    }
70

  
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
// return angle (in radians) that the line connecting the center C of the pentagonal face and the
73
// first vertex of the pentagon makes with a vertical line coming upwards from the center C.
74

  
75
  private float returnAngle(int face)
76
    {
77
    switch(face)
78
      {
79
      case  0:
80
      case  2:
81
      case  6:
82
      case  7: return 0.0f;
83
      case  1:
84
      case  3:
85
      case  4:
86
      case  5: return (float)(36*Math.PI/180);
87
      case  9:
88
      case 10: return (float)(54*Math.PI/180);
89
      case  8:
90
      case 11: return (float)(18*Math.PI/180);
91
      }
92

  
93
    return 0.0f;
94
    }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// The pair (distance,angle) defines a point P in R^2 in polar coordinate system. Let V be the vector
98
// from the center of the coordinate system to P.
99
// Let P' be the point defined by polar (distance,angle+PI/2). Let Lh be the half-line starting at
100
// P' and going in the direction of V.
101
// Return true iff point 'point' lies on the left of Lh, i.e. when we rotate (using the center of
102
// the coordinate system as the center of rotation) 'point' and Lh in such a way that Lh points
103
// directly upwards, is 'point' on the left or the right of it?
104

  
105
  private boolean isOnTheLeft(float[] point, float distance, float angle)
106
    {
107
    float sin = (float)Math.sin(angle);
108
    float cos = (float)Math.cos(angle);
109

  
110
    float vx = point[0] + sin*distance;
111
    float vy = point[1] - cos*distance;
112

  
113
    return vx*sin < vy*cos;
114
    }
115

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

  
118
  int returnPart(int type, int face, float[] point)
119
    {
120
    switch(type)
121
      {
122
      case TYPE_SPLIT_EDGE  : return partEdge(point,face);
123
      case TYPE_SPLIT_CORNER: return partCorner(point,face);
124
      default               : return 0;
125
      }
126
    }
127

  
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
// Return 0,1,2,3,4 - the vertex of the pentagon to which point 'point' is the closest, if the
130
// 'point' is inside the pentagon - or -1 otherwise.
131
// The 'first' vertex is the one we meet the first when we rotate clockwise starting from 12:00.
132
// This vertex makes angle 'returnAngle()' with the line coming out upwards from the center of the
133
// pentagon.
134
// Distance from the center to a vertex of the pentagon = 1/(6*COS54)
135

  
136
  int partEdge(float[] point, int face)
137
    {
138
    float angle = returnAngle(face);
139
    float A = (float)(Math.PI/5);
140

  
141
    for(int i=0; i<5; i++)
142
      {
143
      if( isOnTheLeft(point, DIST2D, (9-2*i)*A-angle) ) return -1;
144
      }
145

  
146
    if( isOnTheLeft(point, 0, 2.5f*A-angle) )
147
      {
148
      if( isOnTheLeft(point, 0, 3.5f*A-angle) )
149
        {
150
        return isOnTheLeft(point, 0, 5.5f*A-angle) ? 3 : 4;
151
        }
152
      else return 0;
153
      }
154
    else
155
      {
156
      if( isOnTheLeft(point, 0, 4.5f*A-angle) )
157
        {
158
        return 2;
159
        }
160
      else
161
        {
162
        return isOnTheLeft(point, 0, 6.5f*A-angle) ? 1 : 0;
163
        }
164
      }
165
    }
166

  
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// TODO - no such object yet
169

  
170
  int partCorner(float[] point, int face)
171
    {
172
    return 0;
173
    }
174

  
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

  
177
  boolean isInsideFace(int face, float[] p)
178
    {
179
    float angle = returnAngle(face);
180
    float A = (float)(Math.PI/5);
181

  
182
    for(int i=0; i<5; i++)
183
      {
184
      if( isOnTheLeft(p, DIST2D, (9-2*i)*A-angle) ) return false;
185
      }
186

  
187
    return true;
188
    }
189
}
src/main/java/org/distorted/objectlib/main/MovementHexahedron.java
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.Static3D;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
// Hexahedral objects: map the 2D swipes of user's fingers to 3D rotations
26

  
27
public class MovementHexahedron extends Movement
28
{
29
  private static final float DIST3D = 0.5f;
30
  private static final float DIST2D = 0.5f;
31

  
32
  private static final float[] D3D  = { DIST3D,DIST3D,DIST3D,DIST3D,DIST3D,DIST3D };
33

  
34
  public static final Static3D[] FACE_AXIS = new Static3D[]
35
         {
36
           new Static3D(1,0,0), new Static3D(-1,0,0),
37
           new Static3D(0,1,0), new Static3D(0,-1,0),
38
           new Static3D(0,0,1), new Static3D(0,0,-1)
39
         };
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
  public MovementHexahedron(Static3D[] rotAxis, float[][] cuts, boolean[][] rotatable, float size, int type, int[][][] enabled)
44
    {
45
    super(rotAxis, FACE_AXIS, cuts, rotatable, D3D, size, type, enabled);
46
    }
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
//  corner    edge
50
//  \ 0 /     3 | 0
51
// 3 \ / 1  ___ | ___
52
//   / \        |
53
//  / 2 \     2 | 1
54

  
55
  int returnPart(int type, int face, float[] touchPoint)
56
    {
57
    switch(type)
58
      {
59
      case TYPE_NOT_SPLIT   : return 0;
60
      case TYPE_SPLIT_EDGE  : boolean e0 = touchPoint[0] > 0;
61
                              boolean e1 = touchPoint[1] > 0;
62
                              return e0 ? (e1 ? 0:1) : (e1 ? 3:2);
63
      case TYPE_SPLIT_CORNER: boolean c0 = touchPoint[1] >= touchPoint[0];
64
                              boolean c1 = touchPoint[1] >=-touchPoint[0];
65
                              return c0 ? (c1 ? 0:3) : (c1 ? 1:2);
66
      }
67

  
68
    return 0;
69
    }
70

  
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

  
73
  public float returnRotationFactor(int[] numLayers, int row)
74
    {
75
    return 1.0f;
76
    }
77

  
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

  
80
  boolean isInsideFace(int face, float[] p)
81
    {
82
    return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
83
    }
84
}
src/main/java/org/distorted/objectlib/main/MovementOctahedron.java
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.Static3D;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
// Octahedral objects: map the 2D swipes of user's fingers to 3D rotations
26

  
27
public class MovementOctahedron extends Movement
28
{
29
  private static final float DIST3D = SQ6/6;
30
  private static final float DIST2D = SQ3/6;
31

  
32
  private static final float[] D3D  = { DIST3D,DIST3D,DIST3D,DIST3D,DIST3D,DIST3D,DIST3D,DIST3D };
33

  
34
  public static final Static3D[] FACE_AXIS = new Static3D[]
35
         {
36
           new Static3D(+SQ6/3,+SQ3/3,     0), new Static3D(-SQ6/3,-SQ3/3,     0),
37
           new Static3D(-SQ6/3,+SQ3/3,     0), new Static3D(+SQ6/3,-SQ3/3,     0),
38
           new Static3D(     0,+SQ3/3,+SQ6/3), new Static3D(     0,-SQ3/3,-SQ6/3),
39
           new Static3D(     0,+SQ3/3,-SQ6/3), new Static3D(     0,-SQ3/3,+SQ6/3)
40
         };
41

  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

  
44
  public MovementOctahedron(Static3D[] rotAxis, float[][] cuts, boolean[][] rotatable, float size, int type, int[][][] enabled)
45
    {
46
    super(rotAxis, FACE_AXIS, cuts, rotatable, D3D, size, type, enabled);
47
    }
48

  
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
// corner    edge
51
//   |       \ 0 /
52
// 2 | 0      \ /
53
//  / \      2 | 1
54
// / 1 \       |
55

  
56
  int returnPart(int type, int face, float[] touchPoint)
57
    {
58
    switch(type)
59
      {
60
      case TYPE_NOT_SPLIT   : return 0;
61

  
62
      case TYPE_SPLIT_EDGE  : float y1 = (face%2 == 0 ? touchPoint[1] : -touchPoint[1]);
63
                              float x1 = touchPoint[0];
64

  
65
                              boolean e0 = x1>0;
66
                              boolean e1 = y1>(+SQ3/3)*x1;
67
                              boolean e2 = y1>(-SQ3/3)*x1;
68

  
69
                              if(  e1 && e2 ) return 0;
70
                              if( !e1 && e0 ) return 1;
71
                              if( !e0 &&!e2 ) return 2;
72

  
73
      case TYPE_SPLIT_CORNER: float y2 = (face%2 == 0 ? touchPoint[1] : -touchPoint[1]);
74
                              float x2 = touchPoint[0];
75

  
76
                              boolean c0 = x2>0;
77
                              boolean c1 = y2>(+SQ3/3)*x2;
78
                              boolean c2 = y2>(-SQ3/3)*x2;
79

  
80
                              if(  c0 && c2 ) return 0;
81
                              if( !c1 &&!c2 ) return 1;
82
                              if( !c0 && c1 ) return 2;
83
      }
84

  
85
    return 0;
86
    }
87

  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

  
90
  public float returnRotationFactor(int[] numLayers, int row)
91
    {
92
    return 1.0f;
93
    }
94

  
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

  
97
  boolean isInsideFace(int face, float[] p)
98
    {
99
    float y = (face%2 == 0 ? p[1] : -p[1]);
100
    float x = p[0];
101
    return (y >= -DIST2D) && (y <= DIST2D*(2-6*x)) && (y <= DIST2D*(2+6*x));
102
    }
103
}
src/main/java/org/distorted/objectlib/main/MovementTetrahedron.java
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.Static3D;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
// Tetrahedral objects: map the 2D swipes of user's fingers to 3D rotations
26

  
27
public class MovementTetrahedron extends Movement
28
{
29
  private static final float DIST3D = SQ6/12;
30
  private static final float DIST2D = SQ3/6;
31

  
32
  private static final float[] D3D  = { DIST3D,DIST3D,DIST3D,DIST3D };
33

  
34
  public static final Static3D[] FACE_AXIS = new Static3D[]
35
         {
36
           new Static3D(     0,+SQ3/3,+SQ6/3),
37
           new Static3D(     0,+SQ3/3,-SQ6/3),
38
           new Static3D(-SQ6/3,-SQ3/3,     0),
39
           new Static3D(+SQ6/3,-SQ3/3,     0),
40
         };
41

  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

  
44
  public MovementTetrahedron(Static3D[] rotAxis, float[][] cuts, boolean[][] rotatable, float size, int type, int[][][] enabled)
45
    {
46
    super(rotAxis, FACE_AXIS, cuts, rotatable, D3D, size, type, enabled);
47
    }
48

  
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
// corner    edge
51
//   |       \ 0 /
52
// 2 | 0      \ /
53
//  / \      2 | 1
54
// / 1 \       |
55

  
56
  int returnPart(int type, int face, float[] touchPoint)
57
    {
58
    switch(type)
59
      {
60
      case TYPE_NOT_SPLIT   : return 0;
61

  
62
      case TYPE_SPLIT_EDGE  : float y1 = (face > 1 ? touchPoint[1] : -touchPoint[1]);
63
                              float x1 = touchPoint[0];
64

  
65
                              boolean e0 = x1>0;
66
                              boolean e1 = y1>(+SQ3/3)*x1;
67
                              boolean e2 = y1>(-SQ3/3)*x1;
68

  
69
                              if(  e1 && e2 ) return 0;
70
                              if( !e1 && e0 ) return 1;
71
                              if( !e0 &&!e2 ) return 2;
72

  
73
      case TYPE_SPLIT_CORNER: float y2 = (face > 1 ? touchPoint[1] : -touchPoint[1]);
74
                              float x2 = touchPoint[0];
75

  
76
                              boolean c0 = x2>0;
77
                              boolean c1 = y2>(+SQ3/3)*x2;
78
                              boolean c2 = y2>(-SQ3/3)*x2;
79

  
80
                              if(  c0 && c2 ) return 0;
81
                              if( !c1 &&!c2 ) return 1;
82
                              if( !c0 && c1 ) return 2;
83
      }
84

  
85
    return 0;
86
    }
87

  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
// Jing has nL=2
90

  
91
  public float returnRotationFactor(int[] numLayers, int row)
92
    {
93
    int numL = numLayers[0];
94

  
95
    return numL==2 ? 1.0f : ((float)numL)/(numL-row);
96
    }
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

  
100
  boolean isInsideFace(int face, float[] p)
101
    {
102
    float y = (face > 1 ? p[1] : -p[1]);
103
    float x = p[0];
104
    return (y >= -DIST2D) && (y <= DIST2D*(2-6*x)) && (y <= DIST2D*(2+6*x));
105
    }
106
}
src/main/java/org/distorted/objectlib/main/ObjectControl.java
33 33
import org.distorted.objectlib.helpers.BlockController;
34 34
import org.distorted.objectlib.helpers.MovesFinished;
35 35
import org.distorted.objectlib.helpers.ObjectLibInterface;
36
import org.distorted.objectlib.movement.Movement;
36 37

  
37 38
///////////////////////////////////////////////////////////////////////////////////////////////////
38 39

  
......
219 220
        Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
220 221
        Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, mQuat);
221 222

  
222
        if( object!=null && mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera,object.getObjectRatio() ) )
223
        if( object!=null && mMovement!=null && mMovement.faceTouched(rotatedTouchPoint,rotatedCamera) )
223 224
          {
224 225
          mDragging           = false;
225 226
          mContinuingRotation = false;
......
338 339

  
339 340
      Static4D touchPoint = new Static4D(x, y, 0, 0);
340 341
      Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, mQuat);
341
      Static2D res = mMovement.newRotation(rotatedTouchPoint,object.getObjectRatio());
342
      Static2D res = mMovement.newRotation(rotatedTouchPoint);
342 343

  
343 344
      mCurrentAxis = (int)res.get0();
344 345
      mCurrentRow  = (int)res.get1();
src/main/java/org/distorted/objectlib/main/TwistyObject.java
51 51
import org.distorted.objectlib.helpers.ObjectSticker;
52 52
import org.distorted.objectlib.helpers.ScrambleState;
53 53
import org.distorted.objectlib.json.JsonReader;
54
import org.distorted.objectlib.movement.Movement;
55
import org.distorted.objectlib.movement.MovementCuboids;
56
import org.distorted.objectlib.movement.MovementDodecahedron;
57
import org.distorted.objectlib.movement.MovementHexahedron;
58
import org.distorted.objectlib.movement.MovementOctahedron;
59
import org.distorted.objectlib.movement.MovementTetrahedron;
54 60

  
55 61
import java.io.DataInputStream;
56 62
import java.io.IOException;
57 63
import java.io.InputStream;
58 64
import java.util.Random;
59 65

  
60
import static org.distorted.objectlib.main.Movement.MOVEMENT_TETRAHEDRON;
61
import static org.distorted.objectlib.main.Movement.MOVEMENT_HEXAHEDRON;
62
import static org.distorted.objectlib.main.Movement.MOVEMENT_OCTAHEDRON;
63
import static org.distorted.objectlib.main.Movement.MOVEMENT_DODECAHEDRON;
64
import static org.distorted.objectlib.main.Movement.MOVEMENT_SHAPECHANGE;
66
import static org.distorted.objectlib.movement.Movement.MOVEMENT_TETRAHEDRON;
67
import static org.distorted.objectlib.movement.Movement.MOVEMENT_HEXAHEDRON;
68
import static org.distorted.objectlib.movement.Movement.MOVEMENT_OCTAHEDRON;
69
import static org.distorted.objectlib.movement.Movement.MOVEMENT_DODECAHEDRON;
70
import static org.distorted.objectlib.movement.Movement.MOVEMENT_SHAPECHANGE;
65 71

  
66 72
///////////////////////////////////////////////////////////////////////////////////////////////////
67 73

  
......
1129 1135
    mObjectScreenRatio = sc;
1130 1136
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1131 1137
    mObjectScale.set(scale,scale,scale);
1138

  
1139
    if( mMovement==null ) mMovement = getMovement();
1140
    mMovement.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1132 1141
    }
1133 1142

  
1134 1143
///////////////////////////////////////////////////////////////////////////////////////////////////
......
1150 1159
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1151 1160
    }
1152 1161

  
1153
///////////////////////////////////////////////////////////////////////////////////////////////////
1154

  
1155
  float getObjectRatio()
1156
    {
1157
    return mObjectScreenRatio*mInitScreenRatio;
1158
    }
1159

  
1160 1162
///////////////////////////////////////////////////////////////////////////////////////////////////
1161 1163

  
1162 1164
  public float getRatio()
src/main/java/org/distorted/objectlib/movement/Movement.java
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
              }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff