Project

General

Profile

Download (10.3 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorTouchControl.java @ f3b3c087

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.bandaged;
11

    
12
import org.distorted.library.helpers.QuatHelper;
13
import org.distorted.library.type.Static3D;
14
import org.distorted.library.type.Static4D;
15
import org.distorted.objectlib.touchcontrol.TouchControlHexahedron;
16

    
17
///////////////////////////////////////////////////////////////////////////////////////////////////
18

    
19
public class BandagedCreatorTouchControl
20
{
21
  private static final float DIST2D = 0.5f;
22

    
23
  private final Static4D CAMERA_POINT;
24
  private final float[] mPoint, mCamera, mTouch, mPos;
25
  private final float[] mPoint2D;
26
  private float mObjectRatio;
27
  private final Static3D[] mFaceAxis;
28
  private final float[] mDist3D;
29

    
30
  private BandagedCubit[] mCubits;
31
  private int mNumCubits;
32
  private int mLastTouchedFace;
33
  private float mX, mY, mZ, mMax;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
  private boolean isInsideFace(int face, float[] p)
38
    {
39
    return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
40
    }
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
// Convert the 3D point3D into a 2D point on the same face surface, but in a different
44
// coordinate system: a in-plane 2D coord where the origin is in the point where the axis intersects
45
// the surface, and whose Y axis points 'north' i.e. is in the plane given by the 3D origin, the
46
// original 3D Y axis and our 2D in-plane origin.
47
// If those 3 points constitute a degenerate triangle which does not define a plane - which can only
48
// happen if axis is vertical (or in theory when 2D origin and 3D origin meet, but that would have to
49
// mean that the distance between the center of the Object and its faces is 0) - then we arbitrarily
50
// decide that 2D Y = (0,0,-1) in the North Pole and (0,0,1) in the South Pole)
51
// (ax,ay,az) - vector normal to the face surface.
52

    
53
  void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
54
    {
55
    float y0,y1,y2; // base Y vector of the 2D coord system
56

    
57
    if( ax==0.0f && az==0.0f )
58
      {
59
      y0=0; y1=0; y2=-ay;
60
      }
61
    else if( ay==0.0f )
62
      {
63
      y0=0; y1=1; y2=0;
64
      }
65
    else
66
      {
67
      float norm = (float)(-ay/Math.sqrt(1-ay*ay));
68
      y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
69
      }
70

    
71
    float x0 = y1*az - y2*ay;  //
72
    float x1 = y2*ax - y0*az;  // (2D coord baseY) x (axis) = 2D coord baseX
73
    float x2 = y0*ay - y1*ax;  //
74

    
75
    float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
76

    
77
    float origin0 = originAlpha*ax; // coords of the point where axis
78
    float origin1 = originAlpha*ay; // intersects surface plane i.e.
79
    float origin2 = originAlpha*az; // the origin of our 2D coord system
80

    
81
    float v0 = point3D[0] - origin0;
82
    float v1 = point3D[1] - origin1;
83
    float v2 = point3D[2] - origin2;
84

    
85
    output[0] = v0*x0 + v1*x1 + v2*x2;
86
    output[1] = v0*y0 + v1*y1 + v2*y2;
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
91
// compute point 'output[]' which:
92
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
93
// 2) is co-linear with mCamera and mPoint
94
//
95
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
96

    
97
  private void castTouchPointOntoFace(int face, float[] output)
98
    {
99
    Static3D faceAxis = mFaceAxis[face];
100

    
101
    float d0 = mPoint[0]-mCamera[0];
102
    float d1 = mPoint[1]-mCamera[1];
103
    float d2 = mPoint[2]-mCamera[2];
104
    float a0 = faceAxis.get0();
105
    float a1 = faceAxis.get1();
106
    float a2 = faceAxis.get2();
107

    
108
    float denom = a0*d0 + a1*d1 + a2*d2;
109

    
110
    if( denom != 0.0f )
111
      {
112
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
113
      float alpha = (mDist3D[face]-axisCam)/denom;
114

    
115
      output[0] = mCamera[0] + d0*alpha;
116
      output[1] = mCamera[1] + d1*alpha;
117
      output[2] = mCamera[2] + d2*alpha;
118
      }
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private void stretchPoint(int face, float[] output)
124
    {
125
    switch(face/2)
126
      {
127
      case 0: output[0] *= (mMax/mZ); output[1] *= (mMax/mY); break;
128
      case 1: output[0] *= (mMax/mX); output[1] *= (mMax/mZ); break;
129
      case 2: output[0] *= (mMax/mX); output[1] *= (mMax/mY); break;
130
      }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private boolean faceIsVisible(int face)
136
    {
137
    Static3D faceAxis = mFaceAxis[face];
138
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
139
    return castCameraOnAxis > mDist3D[face];
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  private boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
145
    {
146
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
147
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
148
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
149

    
150
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
151
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
152
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
153

    
154
    for( mLastTouchedFace=0; mLastTouchedFace<6; mLastTouchedFace++)
155
      {
156
      if( faceIsVisible(mLastTouchedFace) )
157
        {
158
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
159

    
160
        float ax = mFaceAxis[mLastTouchedFace].get0();
161
        float ay = mFaceAxis[mLastTouchedFace].get1();
162
        float az = mFaceAxis[mLastTouchedFace].get2();
163

    
164
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
165
        stretchPoint(mLastTouchedFace,mPoint2D);
166
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
167
        }
168
      }
169

    
170
    return false;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  private void computePosition(int face, float pointX, float pointY)
176
    {
177
    switch(face)
178
      {
179
      case 0: mPos[0] = (mX-1)/2;
180
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
181
              mPos[2] = (int)(-mZ*pointX-mZ/2)+(mZ-1)/2;
182
              break;
183
      case 1: mPos[0] =-(mX-1)/2;
184
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
185
              mPos[2] = (int)(+mZ*pointX+mZ/2)-(mZ-1)/2;
186
              break;
187
      case 2: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
188
              mPos[1] = (mY-1)/2;
189
              mPos[2] = (int)(-mZ*pointY-mZ/2)+(mZ-1)/2;
190
              break;
191
      case 3: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
192
              mPos[1] =-(mY-1)/2;
193
              mPos[2] = (int)(+mZ*pointY+mZ/2)-(mZ-1)/2;
194
              break;
195
      case 4: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
196
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
197
              mPos[2] = (mZ-1)/2;
198
              break;
199
      case 5: mPos[0] = (int)(-mX*pointX-mX/2)+(mX-1)/2;
200
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
201
              mPos[2] =-(mZ-1)/2;
202
              break;
203
      }
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  private int whichCubitTouched(int face, float pointX, float pointY)
209
    {
210
    computePosition(face,pointX,pointY);
211

    
212
    for(int c=0; c<mNumCubits; c++)
213
      if( mCubits[c].isAttached() )
214
        {
215
        float[] pos = mCubits[c].getPosition();
216
        int len = pos.length/3;
217

    
218
        for(int p=0; p<len; p++)
219
          if( pos[3*p]==mPos[0] && pos[3*p+1]==mPos[1] && pos[3*p+2]==mPos[2] ) return c;
220
        }
221

    
222
    android.util.Log.e("D", "whichCubitTouched: IMPOSSIBLE!!");
223
    return -1;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
// PUBLIC API
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public BandagedCreatorTouchControl(float ratio, float fov)
231
    {
232
    mPoint = new float[3];
233
    mCamera= new float[3];
234
    mTouch = new float[3];
235
    mPos   = new float[3];
236
    mPoint2D = new float[2];
237
    mDist3D  = new float[6];
238
    mFaceAxis = TouchControlHexahedron.FACE_AXIS;
239
    mObjectRatio = ratio;
240

    
241
    double halfFOV = fov * (Math.PI/360);
242
    float tanHalf = (float)Math.tan(halfFOV);
243
    float dist = 0.5f/tanHalf;
244

    
245
    CAMERA_POINT = new Static4D(0,0,dist,0);
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
251
    {
252
    mCubits = cubits;
253
    mNumCubits = cubits.length;
254

    
255
    mX = x;
256
    mY = y;
257
    mZ = z;
258
    mMax = mX>mY ? Math.max(mX,mZ) : Math.max(mY,mZ);
259

    
260
    mDist3D[0] = mDist3D[1] = 0.5f*(mX/mMax);
261
    mDist3D[2] = mDist3D[3] = 0.5f*(mY/mMax);
262
    mDist3D[4] = mDist3D[5] = 0.5f*(mZ/mMax);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  public void setObjectRatio(float ratio)
268
    {
269
    mObjectRatio = ratio;
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
// return the index of the cubit touched; if none, return -1.
274

    
275
  public int cubitTouched(float x, float y, Static4D quat)
276
    {
277
    Static4D touchPoint = new Static4D(x, y, 0, 0);
278
    Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, quat);
279
    Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, quat);
280

    
281
    boolean touched = objectTouched(rotatedTouchPoint,rotatedCamera);
282

    
283
    if( !touched ) return -1;
284

    
285
    return whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]);
286
    }
287
}
288

    
(5-5/13)