Project

General

Profile

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

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

1 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c9c71c3f Leszek Koltunski
package org.distorted.objectlib.touchcontrol;
21 29b82486 Leszek Koltunski
22 57ef6378 Leszek Koltunski
import org.distorted.library.main.QuatHelper;
23 29b82486 Leszek Koltunski
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25 57ef6378 Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
26 29b82486 Leszek Koltunski
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
29 c9c71c3f Leszek Koltunski
public abstract class TouchControlShapeConstant extends TouchControl
30 29b82486 Leszek Koltunski
  {
31
  static final float SQ3 = (float)Math.sqrt(3);
32
  static final float SQ6 = (float)Math.sqrt(6);
33
34
  private final int mNumFaceAxis;
35
  private final float[] mPoint, mCamera, mTouch;
36
  private final float[] mPoint2D, mMove2D;
37
  private final int[] mEnabledRotAxis;
38 ab31cf6f Leszek Koltunski
  private final float[] mDistanceCenterFace3D;
39 29b82486 Leszek Koltunski
  private final Static3D[] mFaceAxis;
40
41
  private int mLastTouchedFace;
42
  private float[][][] mCastedRotAxis;
43
  private Static4D[][] mCastedRotAxis4D;
44
  private float[][] mTouchBorders, mA, mB;
45
46 57ef6378 Leszek Koltunski
  private final int mSplit;
47 29b82486 Leszek Koltunski
  private final int[][][] mEnabled;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
  abstract int returnPart(int type, int face, float[] touchPoint);
52
  abstract boolean isInsideFace(int face, float[] point);
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 57ef6378 Leszek Koltunski
  TouchControlShapeConstant(TwistyObject object, float[] distance3D, Static3D[] faceAxis)
57 29b82486 Leszek Koltunski
    {
58 11fa413d Leszek Koltunski
    super(object.getObjectRatio());
59
60 57ef6378 Leszek Koltunski
    int[] numLayers       = object.getNumLayers();
61
    float[][] cuts        = object.getCuts(numLayers);
62
    boolean[][] rotatable = object.getLayerRotatable(numLayers);
63
    float size            = object.getSize();
64
    Static3D[] rotAxis    = object.getRotationAxis();
65
66 29b82486 Leszek Koltunski
    mPoint = new float[3];
67
    mCamera= new float[3];
68
    mTouch = new float[3];
69
70
    mPoint2D = new float[2];
71
    mMove2D  = new float[2];
72
73 11fa413d Leszek Koltunski
    mSplit      = object.getTouchControlSplit();
74 57ef6378 Leszek Koltunski
    mEnabled    = object.getEnabled();
75 29b82486 Leszek Koltunski
    mFaceAxis   = faceAxis;
76
    mNumFaceAxis= mFaceAxis.length;
77
78
    mEnabledRotAxis = new int[rotAxis.length+1];
79
80
    mDistanceCenterFace3D = distance3D; // distance from the center of the object to each of its faces
81
82
    computeCastedAxis(rotAxis);
83
    computeBorders(cuts,rotatable,size);
84 ab31cf6f Leszek Koltunski
    computeLinear(rotAxis,faceAxis);
85 29b82486 Leszek Koltunski
    }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// mCastedRotAxis[1][2]{0,1} are the 2D coords of the 2nd rotAxis cast onto the face defined by the
89
// 1st faceAxis.
90
91
  private void computeCastedAxis(Static3D[] rotAxis)
92
    {
93
    mCastedRotAxis   = new float[mNumFaceAxis][rotAxis.length][2];
94
    mCastedRotAxis4D = new Static4D[mNumFaceAxis][rotAxis.length];
95
96
    for( int casted=0; casted<rotAxis.length; casted++)
97
      {
98
      Static3D a = rotAxis[casted];
99
      mPoint[0]= a.get0();
100
      mPoint[1]= a.get1();
101
      mPoint[2]= a.get2();
102
103
      for( int face=0; face<mNumFaceAxis; face++)
104
        {
105 0c5d8bf7 Leszek Koltunski
        float ax = mFaceAxis[face].get0();
106
        float ay = mFaceAxis[face].get1();
107
        float az = mFaceAxis[face].get2();
108
109
        convertTo2Dcoords( mPoint, ax,ay,az, mCastedRotAxis[face][casted]);
110 29b82486 Leszek Koltunski
        normalize2D(mCastedRotAxis[face][casted]);
111
112 0c5d8bf7 Leszek Koltunski
        float f = mPoint[0]*ax + mPoint[1]*ay + mPoint[2]*az;
113
        mCastedRotAxis4D[face][casted] = new Static4D( mPoint[0]-f*ax, mPoint[1]-f*ay, mPoint[2]-f*az, 0);
114 29b82486 Leszek Koltunski
        }
115
      }
116
    }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
  private void normalize2D(float[] vect)
121
    {
122
    float len = (float)Math.sqrt(vect[0]*vect[0] + vect[1]*vect[1]);
123
    vect[0] /= len;
124
    vect[1] /= len;
125
    }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
// in the center of the face offset is always 0 regardless of the axis
129
130
  private float computeOffset(float[] point, float[] axis)
131
    {
132
    return point[0]*axis[0] + point[1]*axis[1];
133
    }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
137 8da6b1c9 Leszek Koltunski
  private boolean faceIsVisible(int index)
138 29b82486 Leszek Koltunski
    {
139 8da6b1c9 Leszek Koltunski
    Static3D faceAxis = mFaceAxis[index];
140 29b82486 Leszek Koltunski
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
141 ab31cf6f Leszek Koltunski
    return castCameraOnAxis > mDistanceCenterFace3D[index];
142 29b82486 Leszek Koltunski
    }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
146
// compute point 'output[]' which:
147
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
148
// 2) is co-linear with mCamera and mPoint
149
//
150
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
151
152 8da6b1c9 Leszek Koltunski
  private void castTouchPointOntoFace(int index, float[] output)
153 29b82486 Leszek Koltunski
    {
154 8da6b1c9 Leszek Koltunski
    Static3D faceAxis = mFaceAxis[index];
155
156 29b82486 Leszek Koltunski
    float d0 = mPoint[0]-mCamera[0];
157
    float d1 = mPoint[1]-mCamera[1];
158
    float d2 = mPoint[2]-mCamera[2];
159
    float a0 = faceAxis.get0();
160
    float a1 = faceAxis.get1();
161
    float a2 = faceAxis.get2();
162
163
    float denom = a0*d0 + a1*d1 + a2*d2;
164
165
    if( denom != 0.0f )
166
      {
167
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
168 ab31cf6f Leszek Koltunski
      float alpha = (mDistanceCenterFace3D[index]-axisCam)/denom;
169 29b82486 Leszek Koltunski
170
      output[0] = mCamera[0] + d0*alpha;
171
      output[1] = mCamera[1] + d1*alpha;
172
      output[2] = mCamera[2] + d2*alpha;
173
      }
174
    }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178 ba6d9ee9 Leszek Koltunski
  private float[] computeBorder(float[] cuts, boolean[] rotatable, float size)
179 29b82486 Leszek Koltunski
    {
180 a135652b Leszek Koltunski
    if( cuts==null ) return null;
181
182 29b82486 Leszek Koltunski
    int len = cuts.length;
183
    float[] border = new float[len];
184
185
    for(int i=0; i<len; i++)
186
      {
187
      if( !rotatable[i] )
188
        {
189
        border[i] = i>0 ? border[i-1] : -Float.MAX_VALUE;
190
        }
191
      else
192
        {
193
        if( rotatable[i+1] ) border[i] = cuts[i]/size;
194
        else
195
          {
196
          int found = -1;
197
198
          for(int j=i+2; j<=len; j++)
199
            {
200
            if( rotatable[j] )
201
              {
202
              found=j;
203
              break;
204
              }
205
            }
206
207
          border[i] = found>0 ? (cuts[i]+cuts[found-1])/(2*size) : Float.MAX_VALUE;
208
          }
209
        }
210
      }
211
212
    return border;
213
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216 ba6d9ee9 Leszek Koltunski
// size, not numLayers (see Master Skewb where size!=numLayers) - also cuboids.
217 29b82486 Leszek Koltunski
218 ba6d9ee9 Leszek Koltunski
  void computeBorders(float[][] cuts, boolean[][] rotatable, float size)
219 29b82486 Leszek Koltunski
    {
220
    int numCuts = cuts.length;
221
    mTouchBorders = new float[numCuts][];
222
223 ba6d9ee9 Leszek Koltunski
    for(int axis=0; axis<numCuts; axis++)
224 29b82486 Leszek Koltunski
      {
225 ba6d9ee9 Leszek Koltunski
      mTouchBorders[axis] = computeBorder(cuts[axis],rotatable[axis],size);
226 29b82486 Leszek Koltunski
      }
227
    }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  private int computeSign(Static3D a, Static3D b)
232
    {
233
    float a1 = a.get0();
234
    float a2 = a.get1();
235
    float a3 = a.get2();
236
    float b1 = b.get0();
237
    float b2 = b.get1();
238
    float b3 = b.get2();
239
240
    return a1*b1+a2*b2+a3*b3 < 0 ? 1:-1;
241
    }
242
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
  private float crossProductLen(Static3D a, Static3D b)
246
    {
247
    float a1 = a.get0();
248
    float a2 = a.get1();
249
    float a3 = a.get2();
250
    float b1 = b.get0();
251
    float b2 = b.get1();
252
    float b3 = b.get2();
253
254
    float x1 = a2*b3-a3*b2;
255
    float x2 = a3*b1-a1*b3;
256
    float x3 = a1*b2-a2*b1;
257
258
    return (float)Math.sqrt(x1*x1 + x2*x2 + x3*x3);
259
    }
260
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
// compute the array of 'A' and 'B' coeffs of the Ax+B linear function by which we need to multiply
263
// the 3D 'cuts' to translate it from 3D (i.e. with respect to the rotAxis) to 2D in-face (i.e. with
264
// respect to the 2D rotAxis cast into a particular face)
265
266 ab31cf6f Leszek Koltunski
  private void computeLinear(Static3D[] rotAxis, Static3D[] faceAxis)
267 29b82486 Leszek Koltunski
    {
268
    int numFaces = faceAxis.length;
269
    int numRot   = rotAxis.length;
270
271
    mA = new float[numFaces][numRot];
272
    mB = new float[numFaces][numRot];
273
274
    for(int i=0; i<numFaces; i++)
275
      for(int j=0; j<numRot; j++)
276
        {
277
        mA[i][j] = crossProductLen(faceAxis[i],rotAxis[j]);
278
279
        if( mA[i][j]!=0.0f )
280
          {
281
          float coeff = (float)Math.sqrt(1/(mA[i][j]*mA[i][j]) -1);
282
          int sign = computeSign(faceAxis[i],rotAxis[j]);
283 ab31cf6f Leszek Koltunski
          mB[i][j] = sign*coeff*mDistanceCenterFace3D[i];
284 29b82486 Leszek Koltunski
          }
285
        else mB[i][j] = 0.0f;
286
        }
287
    }
288
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291
  private int computeRowFromOffset(int face, int axisIndex, float offset)
292
    {
293
    float[] borders = mTouchBorders[axisIndex];
294 a135652b Leszek Koltunski
295
    if( borders==null ) return 0;
296
297 29b82486 Leszek Koltunski
    int len = borders.length;
298
    float A = mA[face][axisIndex];
299
300
    if( A!=0.0f )
301
      {
302
      float B = mB[face][axisIndex];
303
304
      for(int i=0; i<len; i++)
305
        {
306
        float translated = B + borders[i]/A;
307
        if( offset<translated ) return i;
308
        }
309
      }
310
311
    return len;
312
    }
313
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
316
  void computeEnabledAxis(int face, float[] touchPoint, int[] enabled)
317
    {
318 57ef6378 Leszek Koltunski
    int part = returnPart(mSplit,face,touchPoint);
319 29b82486 Leszek Koltunski
320
    int num = mEnabled[face][0].length;
321
    enabled[0] = num;
322
    System.arraycopy(mEnabled[face][part], 0, enabled, 1, num);
323
    }
324
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
// PUBLIC API
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
329 57ef6378 Leszek Koltunski
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
330 29b82486 Leszek Koltunski
    {
331 23afe4c4 Leszek Koltunski
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
332
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
333
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
334 29b82486 Leszek Koltunski
335 23afe4c4 Leszek Koltunski
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
336
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
337
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
338 29b82486 Leszek Koltunski
339
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaceAxis; mLastTouchedFace++)
340
      {
341 8da6b1c9 Leszek Koltunski
      if( faceIsVisible(mLastTouchedFace) )
342 29b82486 Leszek Koltunski
        {
343 8da6b1c9 Leszek Koltunski
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
344 0c5d8bf7 Leszek Koltunski
345
        float ax = mFaceAxis[mLastTouchedFace].get0();
346
        float ay = mFaceAxis[mLastTouchedFace].get1();
347
        float az = mFaceAxis[mLastTouchedFace].get2();
348
349
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
350 29b82486 Leszek Koltunski
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
351
        }
352
      }
353
354
    return false;
355
    }
356
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
359 cd2e8d4c Leszek Koltunski
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
360 29b82486 Leszek Koltunski
    {
361 23afe4c4 Leszek Koltunski
    mPoint[0] = rotatedTouchPoint.get0()/mObjectRatio;
362
    mPoint[1] = rotatedTouchPoint.get1()/mObjectRatio;
363
    mPoint[2] = rotatedTouchPoint.get2()/mObjectRatio;
364 29b82486 Leszek Koltunski
365 8da6b1c9 Leszek Koltunski
    castTouchPointOntoFace(mLastTouchedFace, mTouch);
366 0c5d8bf7 Leszek Koltunski
367
    float ax = mFaceAxis[mLastTouchedFace].get0();
368
    float ay = mFaceAxis[mLastTouchedFace].get1();
369
    float az = mFaceAxis[mLastTouchedFace].get2();
370
371
    convertTo2Dcoords(mTouch, ax,ay,az, mMove2D);
372 29b82486 Leszek Koltunski
373
    mMove2D[0] -= mPoint2D[0];
374
    mMove2D[1] -= mPoint2D[1];
375
376
    computeEnabledAxis(mLastTouchedFace, mPoint2D, mEnabledRotAxis);
377 92a6fc8b Leszek Koltunski
    int rotIndex = computeRotationIndex( mCastedRotAxis[mLastTouchedFace], mMove2D, mEnabledRotAxis);
378 29b82486 Leszek Koltunski
    float offset = computeOffset(mPoint2D, mCastedRotAxis[mLastTouchedFace][rotIndex]);
379
    int row      = computeRowFromOffset(mLastTouchedFace,rotIndex,offset);
380
381 57ef6378 Leszek Koltunski
    output[0] = rotIndex;
382
    output[1] = row;
383 29b82486 Leszek Koltunski
    }
384
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386 57ef6378 Leszek Koltunski
// cast the 3D axis we are currently rotating along (which is already casted to the surface of the
387
// currently touched face AND converted into a 4D vector - fourth 0) to a 2D in-screen-surface axis
388 29b82486 Leszek Koltunski
389 57ef6378 Leszek Koltunski
  public void getCastedRotAxis(float[] output, Static4D quat, int rotIndex)
390 29b82486 Leszek Koltunski
    {
391 57ef6378 Leszek Koltunski
    Static4D axis = mCastedRotAxis4D[mLastTouchedFace][rotIndex];
392
    Static4D result = QuatHelper.rotateVectorByQuat(axis, quat);
393
394
    output[0] =result.get0();
395
    output[1] =result.get1();
396
397
    float len = (float)Math.sqrt(output[0]*output[0] + output[1]*output[1]);
398
    output[0] /= len;
399
    output[1] /= len;
400 29b82486 Leszek Koltunski
    }
401
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403
404 11fa413d Leszek Koltunski
  public int getTouchedCubitFace()
405 29b82486 Leszek Koltunski
    {
406 11fa413d Leszek Koltunski
    return 0;
407 29b82486 Leszek Koltunski
    }
408
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410
411 11fa413d Leszek Koltunski
  public int getTouchedCubit()
412 29b82486 Leszek Koltunski
    {
413 11fa413d Leszek Koltunski
    return 0;
414 29b82486 Leszek Koltunski
    }
415
  }