Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorTouchControl.java @ 39176a1f

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.bandaged;
21

    
22
import org.distorted.library.main.QuatHelper;
23
import org.distorted.library.type.Static3D;
24
import org.distorted.library.type.Static4D;
25
import org.distorted.objectlib.touchcontrol.TouchControlHexahedron;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
public class BandagedCreatorTouchControl
30
{
31
  private static final float DIST2D = 0.5f;
32

    
33
  private final Static4D CAMERA_POINT;
34
  private final float[] mPoint, mCamera, mTouch, mPos;
35
  private final float[] mPoint2D;
36
  private float mObjectRatio;
37
  private final Static3D[] mFaceAxis;
38
  private final float[] mDist3D;
39

    
40
  private BandagedCubit[] mCubits;
41
  private int mNumCubits;
42
  private int mLastTouchedFace;
43
  private float mX, mY, mZ, mMax;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  private boolean isInsideFace(int face, float[] p)
48
    {
49
    return ( p[0]<=DIST2D && p[0]>=-DIST2D && p[1]<=DIST2D && p[1]>=-DIST2D );
50
    }
51

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

    
63
  void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
64
    {
65
    float y0,y1,y2; // base Y vector of the 2D coord system
66

    
67
    if( ax==0.0f && az==0.0f )
68
      {
69
      y0=0; y1=0; y2=-ay;
70
      }
71
    else if( ay==0.0f )
72
      {
73
      y0=0; y1=1; y2=0;
74
      }
75
    else
76
      {
77
      float norm = (float)(-ay/Math.sqrt(1-ay*ay));
78
      y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
79
      }
80

    
81
    float x0 = y1*az - y2*ay;  //
82
    float x1 = y2*ax - y0*az;  // (2D coord baseY) x (axis) = 2D coord baseX
83
    float x2 = y0*ay - y1*ax;  //
84

    
85
    float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
86

    
87
    float origin0 = originAlpha*ax; // coords of the point where axis
88
    float origin1 = originAlpha*ay; // intersects surface plane i.e.
89
    float origin2 = originAlpha*az; // the origin of our 2D coord system
90

    
91
    float v0 = point3D[0] - origin0;
92
    float v1 = point3D[1] - origin1;
93
    float v2 = point3D[2] - origin2;
94

    
95
    output[0] = v0*x0 + v1*x1 + v2*x2;
96
    output[1] = v0*y0 + v1*y1 + v2*y2;
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
101
// compute point 'output[]' which:
102
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
103
// 2) is co-linear with mCamera and mPoint
104
//
105
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
106

    
107
  private void castTouchPointOntoFace(int face, float[] output)
108
    {
109
    Static3D faceAxis = mFaceAxis[face];
110

    
111
    float d0 = mPoint[0]-mCamera[0];
112
    float d1 = mPoint[1]-mCamera[1];
113
    float d2 = mPoint[2]-mCamera[2];
114
    float a0 = faceAxis.get0();
115
    float a1 = faceAxis.get1();
116
    float a2 = faceAxis.get2();
117

    
118
    float denom = a0*d0 + a1*d1 + a2*d2;
119

    
120
    if( denom != 0.0f )
121
      {
122
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
123
      float alpha = (mDist3D[face]-axisCam)/denom;
124

    
125
      output[0] = mCamera[0] + d0*alpha;
126
      output[1] = mCamera[1] + d1*alpha;
127
      output[2] = mCamera[2] + d2*alpha;
128
      }
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  private void stretchPoint(int face, float[] output)
134
    {
135
    switch(face/2)
136
      {
137
      case 0: output[0] *= (mMax/mZ); output[1] *= (mMax/mY); break;
138
      case 1: output[0] *= (mMax/mX); output[1] *= (mMax/mZ); break;
139
      case 2: output[0] *= (mMax/mX); output[1] *= (mMax/mY); break;
140
      }
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private boolean faceIsVisible(int face)
146
    {
147
    Static3D faceAxis = mFaceAxis[face];
148
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
149
    return castCameraOnAxis > mDist3D[face];
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
155
    {
156
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
157
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
158
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
159

    
160
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
161
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
162
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
163

    
164
    for( mLastTouchedFace=0; mLastTouchedFace<6; mLastTouchedFace++)
165
      {
166
      if( faceIsVisible(mLastTouchedFace) )
167
        {
168
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
169

    
170
        float ax = mFaceAxis[mLastTouchedFace].get0();
171
        float ay = mFaceAxis[mLastTouchedFace].get1();
172
        float az = mFaceAxis[mLastTouchedFace].get2();
173

    
174
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
175
        stretchPoint(mLastTouchedFace,mPoint2D);
176
        if( isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
177
        }
178
      }
179

    
180
    return false;
181
    }
182

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

    
185
  private void computePosition(int face, float pointX, float pointY)
186
    {
187
    switch(face)
188
      {
189
      case 0: mPos[0] = (mX-1)/2;
190
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
191
              mPos[2] = (int)(-mZ*pointX-mZ/2)+(mZ-1)/2;
192
              break;
193
      case 1: mPos[0] =-(mX-1)/2;
194
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
195
              mPos[2] = (int)(+mZ*pointX+mZ/2)-(mZ-1)/2;
196
              break;
197
      case 2: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
198
              mPos[1] = (mY-1)/2;
199
              mPos[2] = (int)(-mZ*pointY-mZ/2)+(mZ-1)/2;
200
              break;
201
      case 3: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
202
              mPos[1] =-(mY-1)/2;
203
              mPos[2] = (int)(+mZ*pointY+mZ/2)-(mZ-1)/2;
204
              break;
205
      case 4: mPos[0] = (int)(+mX*pointX+mX/2)-(mX-1)/2;
206
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
207
              mPos[2] = (mZ-1)/2;
208
              break;
209
      case 5: mPos[0] = (int)(-mX*pointX-mX/2)+(mX-1)/2;
210
              mPos[1] = (int)(+mY*pointY+mY/2)-(mY-1)/2;
211
              mPos[2] =-(mZ-1)/2;
212
              break;
213
      }
214
    }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
  private int whichCubitTouched(int face, float pointX, float pointY)
219
    {
220
    computePosition(face,pointX,pointY);
221

    
222
    for(int c=0; c<mNumCubits; c++)
223
      if( mCubits[c].isAttached() )
224
        {
225
        float[] pos = mCubits[c].getPosition();
226
        int len = pos.length/3;
227

    
228
        for(int p=0; p<len; p++)
229
          if( pos[3*p]==mPos[0] && pos[3*p+1]==mPos[1] && pos[3*p+2]==mPos[2] ) return c;
230
        }
231

    
232
    android.util.Log.e("D", "whichCubitTouched: IMPOSSIBLE!!");
233
    return -1;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// PUBLIC API
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  public BandagedCreatorTouchControl(float ratio, float fov)
241
    {
242
    mPoint = new float[3];
243
    mCamera= new float[3];
244
    mTouch = new float[3];
245
    mPos   = new float[3];
246
    mPoint2D = new float[2];
247
    mDist3D  = new float[6];
248
    mFaceAxis = TouchControlHexahedron.FACE_AXIS;
249
    mObjectRatio = ratio;
250

    
251
    double halfFOV = fov * (Math.PI/360);
252
    float tanHalf = (float)Math.tan(halfFOV);
253
    float dist = 0.5f/tanHalf;
254

    
255
    CAMERA_POINT = new Static4D(0,0,dist,0);
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public void setCubits(BandagedCubit[] cubits, int x, int y, int z)
261
    {
262
    mCubits = cubits;
263
    mNumCubits = cubits.length;
264

    
265
    mX = x;
266
    mY = y;
267
    mZ = z;
268
    mMax = mX>mY ? Math.max(mX,mZ) : Math.max(mY,mZ);
269

    
270
    mDist3D[0] = mDist3D[1] = 0.5f*(mX/mMax);
271
    mDist3D[2] = mDist3D[3] = 0.5f*(mY/mMax);
272
    mDist3D[4] = mDist3D[5] = 0.5f*(mZ/mMax);
273
    }
274

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

    
277
  public void setObjectRatio(float ratio)
278
    {
279
    mObjectRatio = ratio;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
// return the index of the cubit touched; if none, return -1.
284

    
285
  public int cubitTouched(float x, float y, Static4D quat)
286
    {
287
    Static4D touchPoint = new Static4D(x, y, 0, 0);
288
    Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, quat);
289
    Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, quat);
290

    
291
    boolean touched = objectTouched(rotatedTouchPoint,rotatedCamera);
292

    
293
    if( !touched ) return -1;
294

    
295
    return whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]);
296
    }
297
}
298

    
(5-5/13)