Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedCreatorTouchControl.java @ 6c295be1

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

    
16
///////////////////////////////////////////////////////////////////////////////////////////////////
17

    
18
public class BandagedCreatorTouchControl
19
{
20
  private final BandagedObject mObject;
21
  private final Static4D CAMERA_POINT;
22
  private final float[] mPoint, mCamera, mTouch;
23
  private final float[] mPoint2D;
24
  private float mObjectRatio;
25
  private final Static3D[] mFaceAxis;
26
  private final int mNumFaces;
27

    
28
  private float[] mDist3D;
29
  private int mLastTouchedFace;
30

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

    
42
  void convertTo2Dcoords(float[] point3D, float ax, float ay, float az , float[] output)
43
    {
44
    float y0,y1,y2; // base Y vector of the 2D coord system
45

    
46
    if( ax==0.0f && az==0.0f )
47
      {
48
      y0=0; y1=0; y2=-ay;
49
      }
50
    else if( ay==0.0f )
51
      {
52
      y0=0; y1=1; y2=0;
53
      }
54
    else
55
      {
56
      float norm = (float)(-ay/Math.sqrt(1-ay*ay));
57
      y0 = norm*ax; y1= norm*(ay-1/ay); y2=norm*az;
58
      }
59

    
60
    float x0 = y1*az - y2*ay;  //
61
    float x1 = y2*ax - y0*az;  // (2D coord baseY) x (axis) = 2D coord baseX
62
    float x2 = y0*ay - y1*ax;  //
63

    
64
    float originAlpha = point3D[0]*ax + point3D[1]*ay + point3D[2]*az;
65

    
66
    float origin0 = originAlpha*ax; // coords of the point where axis
67
    float origin1 = originAlpha*ay; // intersects surface plane i.e.
68
    float origin2 = originAlpha*az; // the origin of our 2D coord system
69

    
70
    float v0 = point3D[0] - origin0;
71
    float v1 = point3D[1] - origin1;
72
    float v2 = point3D[2] - origin2;
73

    
74
    output[0] = v0*x0 + v1*x1 + v2*x2;
75
    output[1] = v0*y0 + v1*y1 + v2*y2;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
80
// compute point 'output[]' which:
81
// 1) lies on a face of the Object, i.e. surface defined by (axis, distance from (0,0,0))
82
// 2) is co-linear with mCamera and mPoint
83
//
84
// output = camera + alpha*(point-camera), where alpha = [dist-axis*camera] / [axis*(point-camera)]
85

    
86
  private void castTouchPointOntoFace(int face, float[] output)
87
    {
88
    Static3D faceAxis = mFaceAxis[face];
89

    
90
    float d0 = mPoint[0]-mCamera[0];
91
    float d1 = mPoint[1]-mCamera[1];
92
    float d2 = mPoint[2]-mCamera[2];
93
    float a0 = faceAxis.get0();
94
    float a1 = faceAxis.get1();
95
    float a2 = faceAxis.get2();
96

    
97
    float denom = a0*d0 + a1*d1 + a2*d2;
98

    
99
    if( denom != 0.0f )
100
      {
101
      float axisCam = a0*mCamera[0] + a1*mCamera[1] + a2*mCamera[2];
102
      float alpha = (mDist3D[face]-axisCam)/denom;
103

    
104
      output[0] = mCamera[0] + d0*alpha;
105
      output[1] = mCamera[1] + d1*alpha;
106
      output[2] = mCamera[2] + d2*alpha;
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  private boolean faceIsVisible(int face)
113
    {
114
    Static3D faceAxis = mFaceAxis[face];
115
    float castCameraOnAxis = mCamera[0]*faceAxis.get0() + mCamera[1]*faceAxis.get1() + mCamera[2]*faceAxis.get2();
116
    return castCameraOnAxis > mDist3D[face];
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  private boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
122
    {
123
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
124
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
125
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
126

    
127
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
128
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
129
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
130

    
131
    for( mLastTouchedFace=0; mLastTouchedFace<mNumFaces; mLastTouchedFace++)
132
      {
133
      if( faceIsVisible(mLastTouchedFace) )
134
        {
135
        castTouchPointOntoFace(mLastTouchedFace, mTouch);
136

    
137
        float ax = mFaceAxis[mLastTouchedFace].get0();
138
        float ay = mFaceAxis[mLastTouchedFace].get1();
139
        float az = mFaceAxis[mLastTouchedFace].get2();
140

    
141
        convertTo2Dcoords(mTouch, ax,ay,az, mPoint2D);
142

    
143
        if( mObject.isInsideFace(mLastTouchedFace,mPoint2D) ) return true;
144
        }
145
      }
146

    
147
    return false;
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// PUBLIC API
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  public BandagedCreatorTouchControl(float ratio, float fov, BandagedObject object)
155
    {
156
    mObject = object;
157
    mPoint = new float[3];
158
    mCamera= new float[3];
159
    mTouch = new float[3];
160
    mPoint2D = new float[2];
161
    mDist3D  = new float[6];
162
    mFaceAxis = mObject.getFaceAxis();
163
    mObjectRatio = ratio;
164
    mNumFaces = mFaceAxis.length;
165

    
166
    double halfFOV = fov * (Math.PI/360);
167
    float tanHalf = (float)Math.tan(halfFOV);
168
    float dist = 0.5f/tanHalf;
169

    
170
    CAMERA_POINT = new Static4D(0,0,dist,0);
171
    }
172

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

    
175
  public void setDist3D(float[] dist3d)
176
    {
177
    mDist3D = dist3d;
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  public void setObjectRatio(float ratio)
183
    {
184
    mObjectRatio = ratio;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// return the index of the cubit touched; if none, return -1.
189

    
190
  public int cubitTouched(float x, float y, Static4D quat)
191
    {
192
    Static4D touchPoint = new Static4D(x, y, 0, 0);
193
    Static4D rotatedTouchPoint= QuatHelper.rotateVectorByInvertedQuat(touchPoint, quat);
194
    Static4D rotatedCamera= QuatHelper.rotateVectorByInvertedQuat(CAMERA_POINT, quat);
195

    
196
    boolean touched = objectTouched(rotatedTouchPoint,rotatedCamera);
197

    
198
    if( !touched ) return -1;
199

    
200
    return mObject.whichCubitTouched(mLastTouchedFace,mPoint2D[0],mPoint2D[1]);
201
    }
202
}
203

    
(5-5/16)