Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / touchcontrol / TouchControlShapeChanging.java @ 52375039

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.objectlib.touchcontrol;
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.helpers.FactoryCubit;
16
import org.distorted.objectlib.helpers.ObjectShape;
17
import org.distorted.objectlib.main.TwistyObject;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public class TouchControlShapeChanging extends TouchControl
22
  {
23
  private static final float NOT_TOUCHED = 1000000.0f;
24
  static final float[] mTmp = new float[4];
25

    
26
  static class FaceInfo
27
    {
28
    private final float[] normal;      // vector normal to the surface of the face, pointing outside.
29
    private final float distance;      // distance from (0,0,0) to the surface of the face
30
    private final float[][] vertices;  // vertices of the face. Already rotated by the initQuat and
31
                                       // moved by 'position' (arithmetic average of all positions)
32
    private final float[][] rotated;   // temp array to store vertices times rotation quaternion.
33

    
34
    //////////////////////////////////////////////////////////
35

    
36
    FaceInfo(float[][] verts, float size)
37
      {
38
      int numV = verts.length;
39

    
40
      vertices = new float[numV][];
41
      rotated  = new float[numV][];
42

    
43
      for(int i=0; i<numV; i++)
44
        {
45
        int len = verts[i].length;
46
        vertices[i]= new float[len];
47
        rotated[i] = new float[len];
48

    
49
        for(int j=0; j<len; j++) vertices[i][j] = verts[i][j]/size;
50
        }
51

    
52
      // assuming the first three vertices are linearly independent
53
      float[] v0 = vertices[0];
54
      float[] v1 = vertices[1];
55
      float[] v2 = vertices[2];
56

    
57
      float a1 = v0[0] - v1[0];
58
      float a2 = v0[1] - v1[1];
59
      float a3 = v0[2] - v1[2];
60
      float b1 = v1[0] - v2[0];
61
      float b2 = v1[1] - v2[1];
62
      float b3 = v1[2] - v2[2];
63

    
64
      float vx = a2*b3-a3*b2;
65
      float vy = a3*b1-a1*b3;
66
      float vz = a1*b2-a2*b1;
67

    
68
      float len = (float)Math.sqrt(vx*vx+vy*vy+vz*vz);
69

    
70
      vx/=len;
71
      vy/=len;
72
      vz/=len;
73

    
74
      normal = new float[4];
75
      normal[0] = vx;
76
      normal[1] = vy;
77
      normal[2] = vz;
78
      normal[3] = 0.0f;
79

    
80
      if( FactoryCubit.totalAngle(vertices,normal)<0 )
81
        {
82
        normal[0] *= -1;
83
        normal[1] *= -1;
84
        normal[2] *= -1;
85
        }
86

    
87
      distance = normal[0]*v0[0] + normal[1]*v0[1] + normal[2]*v0[2];
88
      }
89

    
90
    //////////////////////////////////////////////////////////
91
    public float[] getNormal()
92
      {
93
      return normal;
94
      }
95
    }
96
  ////////////////////////////////////////////////////////////
97
  // end FaceInfo
98

    
99
  private final float[] mTouch, mLastT;
100
  private int mNumCubits;
101
  private int[] mNumFaces;
102
  private boolean mPreparationDone;
103
  private boolean[][] mRotatable;
104
  private float[][] mCuts;
105

    
106
  final float[] mCamera, mPoint;
107
  final Static3D[] mRotAxis;
108
  final TwistyObject mObject;
109
  int mTouchedCubit, mTouchedCubitFace, mNumAxis;
110
  FaceInfo[][] mInfos;
111
  float[][] mQuats;
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public TouchControlShapeChanging(TwistyObject object)
116
    {
117
    super(object);
118

    
119
    mPoint = new float[3];
120
    mCamera= new float[3];
121
    mTouch = new float[3];
122
    mLastT = new float[3];
123
    mObject= object;
124
    mPreparationDone = false;
125
    mGhostAxisEnabled = -1;
126

    
127
    if( object!=null )
128
      {
129
      int[] numL  = object.getNumLayers();
130
      mRotAxis    = object.getRotationAxis() ;
131
      mNumAxis    = mRotAxis.length;
132
      mRotatable  = object.getLayerRotatable(numL);
133
      mCuts       = object.getCuts(numL);
134
      float size  = object.getSize();
135
      computeBorders(mCuts,mRotatable,size);
136
      }
137
    else
138
      {
139
      mRotAxis = null;
140
      mNumAxis = 0;
141
      }
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// mesh multigon
146

    
147
  private FaceInfo[] computeInfos(float[][] vertices, int[][][] indices, float[] position, Static4D quat, float size)
148
    {
149
    int len = position.length/3;
150
    float avgX = 0.0f;
151
    float avgY = 0.0f;
152
    float avgZ = 0.0f;
153

    
154
    for(int i=0; i<len; i++)
155
      {
156
      avgX += position[3*i  ];
157
      avgY += position[3*i+1];
158
      avgZ += position[3*i+2];
159
      }
160

    
161
    avgX /= len;
162
    avgY /= len;
163
    avgZ /= len;
164

    
165
    int numFaces = indices.length;
166
    FaceInfo[] infos = new FaceInfo[numFaces];
167
    Static4D tmp;
168

    
169
    for(int f=0; f<numFaces; f++)
170
      {
171
      int[][] inds = indices[f];
172
      int numSegments = inds.length;
173
      int numVerts = 0;
174
      for(int[] ind : inds) numVerts += ind.length;
175

    
176
      float[][] verts = new float[numVerts][4];
177
      int pointer = 0;
178

    
179
      for(int s=0; s<numSegments; s++)
180
        {
181
        int numV = inds[s].length;
182

    
183
        for(int v=0; v<numV; v++)
184
          {
185
          int index = indices[f][s][v];
186
          float x = vertices[index][0];
187
          float y = vertices[index][1];
188
          float z = vertices[index][2];
189
          float w = 1.0f;
190

    
191
          tmp = QuatHelper.rotateVectorByQuat(x,y,z,w,quat);
192

    
193
          verts[pointer][0] = tmp.get0() + avgX;
194
          verts[pointer][1] = tmp.get1() + avgY;
195
          verts[pointer][2] = tmp.get2() + avgZ;
196
          verts[pointer][3] = 1.0f;
197
          pointer++;
198
          }
199
        }
200

    
201
      infos[f] = new FaceInfo(verts,size);
202
      }
203

    
204
    return infos;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// mesh polygon
209

    
210
  private FaceInfo[] computeInfos(float[][] vertices, int[][] indices, float[] position, Static4D quat, float size)
211
    {
212
    int len = position.length/3;
213
    float avgX = 0.0f;
214
    float avgY = 0.0f;
215
    float avgZ = 0.0f;
216

    
217
    for(int i=0; i<len; i++)
218
      {
219
      avgX += position[3*i  ];
220
      avgY += position[3*i+1];
221
      avgZ += position[3*i+2];
222
      }
223

    
224
    avgX /= len;
225
    avgY /= len;
226
    avgZ /= len;
227

    
228
    int numFaces = indices.length;
229
    FaceInfo[] infos = new FaceInfo[numFaces];
230
    Static4D tmp;
231

    
232
    for(int f=0; f<numFaces; f++)
233
      {
234
      int numVerts = indices[f].length;
235
      float[][] verts = new float[numVerts][4];
236

    
237
      for(int v=0; v<numVerts; v++)
238
        {
239
        int index = indices[f][v];
240
        float x = vertices[index][0];
241
        float y = vertices[index][1];
242
        float z = vertices[index][2];
243
        float w = 1.0f;
244

    
245
        tmp = QuatHelper.rotateVectorByQuat(x,y,z,w,quat);
246

    
247
        verts[v][0] = tmp.get0() + avgX;
248
        verts[v][1] = tmp.get1() + avgY;
249
        verts[v][2] = tmp.get2() + avgZ;
250
        verts[v][3] = 1.0f;
251
        }
252

    
253
      infos[f] = new FaceInfo(verts,size);
254
      }
255

    
256
    return infos;
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260
// software implementation of DistortedLibrary.mainVertexShader.degree() function.
261
// (limited to regions centered at [0,0,0])
262

    
263
  private float computeVertexDegree(float radius, float[] vert)
264
    {
265
    float x = vert[0];
266
    float y = vert[1];
267
    float z = vert[2];
268

    
269
    float len = (float)Math.sqrt(x*x + y*y + z*z);
270
    return len>radius ? 0.0f : 1.0f-len/radius;
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
// software implementation of DistortedLibrary.VertexEffectSink
275

    
276
  private float[] adjustVert(float pillow, float radius, float[] vert)
277
    {
278
    float[] output = new float[3];
279
    float deg = computeVertexDegree(radius,vert);
280
    float t = 1.0f - deg*(1.0f-pillow)/pillow;
281
    output[0] = t*vert[0];
282
    output[1] = t*vert[1];
283
    output[2] = t*vert[2];
284

    
285
    return output;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  private float[][] adjustVerticesForPillow(float pillow, float radius, float[][] verts)
291
    {
292
    int num = verts.length;
293
    float[][] output = new float[num][3];
294
    for(int i=0; i<num; i++) output[i] = adjustVert(pillow,radius,verts[i]);
295
    return output;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  private void prepare()
301
    {
302
    int[] numLayers = mObject.getNumLayers();
303
    float[][] positions = mObject.getCubitPositions(numLayers);
304
    float size = mObject.getSize();
305
    mNumCubits = positions.length;
306
    mNumFaces  = new int[mNumCubits];
307

    
308
    mInfos = new FaceInfo[mNumCubits][];
309
    float pillow = mObject.getPillowCoeff();
310
    float radius = mObject.getCircumscribedRadius();
311

    
312
    for(int i=0; i<mNumCubits; i++)
313
      {
314
      int variant = mObject.getCubitVariant(i,numLayers);
315
      ObjectShape shape = mObject.getObjectShape(variant);
316
      Static4D quat = mObject.getCubitQuats(i,numLayers);
317
      float[][] vertices = shape.getVertices();
318
      if( pillow!=1.0f ) vertices = adjustVerticesForPillow(pillow,radius,vertices);
319
      mNumFaces[i] =shape.getNumFaces();
320

    
321
      int[][] indices = shape.getVertIndices();
322

    
323
      if( indices!=null )
324
        {
325
        mInfos[i] = computeInfos(vertices, indices, positions[i], quat, size);
326
        }
327
      else
328
        {
329
        int[][][] ind = shape.getMultigonIndices();
330
        mInfos[i] = computeInfos(vertices, ind, positions[i], quat, size);
331
        }
332
      }
333

    
334
    Static4D[] quats = mObject.getQuats();
335
    int numQuats = quats.length;
336

    
337
    mQuats = new float[numQuats][4];
338

    
339
    for(int i=0; i<numQuats; i++)
340
      {
341
      Static4D q = quats[i];
342
      mQuats[i][0] = q.get0();
343
      mQuats[i][1] = q.get1();
344
      mQuats[i][2] = q.get2();
345
      mQuats[i][3] = q.get3();
346
      }
347

    
348
    mPreparationDone = true;
349
    }
350

    
351
///////////////////////////////////////////////////////////////////////////////////////////////////
352
// points A, B, C are co-linear. Return true iff B is between A and C on this line.
353
// Compute D1 = A-B, D2=C-B. Then D1 and D2 are parallel vectors.
354
// They disagree in direction iff |D1+D2|<|D1-D2|
355

    
356
  private boolean isBetween(float ax, float ay, float az,
357
                            float bx, float by, float bz,
358
                            float cx, float cy, float cz)
359
    {
360
    float d1x = ax-bx;
361
    float d1y = ay-by;
362
    float d1z = az-bz;
363

    
364
    float d2x = cx-bx;
365
    float d2y = cy-by;
366
    float d2z = cz-bz;
367

    
368
    float sx = d1x+d2x;
369
    float sy = d1y+d2y;
370
    float sz = d1z+d2z;
371

    
372
    float dx = d1x-d2x;
373
    float dy = d1y-d2y;
374
    float dz = d1z-d2z;
375

    
376
    return sx*sx+sy*sy+sz*sz < dx*dx+dy*dy+dz*dz;
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
// General algorithm: shoot a half-line from the 'point' and count how many
381
// sides of the polygon it intersects with. The point is inside iff this number
382
// is odd. Note that this works also in case of concave polygons.
383
//
384
// Arbitrarily take point P on the plane ( we have decided on P=(vert[0]+vert[1])/2 )
385
// as the other point defining the half-line.
386
// 'point' and 'P' define a line L1 in 3D. Then for each side the pair of its vertices
387
// defines a line L2. If L1||L2 return false. Otherwise, the lines are skew so it's
388
// possible to compute points C1 and C2 on lines L1 and L2 which are closest to the
389
// other line and check if
390
//
391
// a) C1 and P are on the same side of 'point'
392
//    (which happens iff 'point' is not in between of C1 and P)
393
// b) C2 is between the two vertices.
394
//
395
// Both a) and b) together mean that the half-line intersects with side defined by (p2,d2)
396
//
397
// C1 and C2 can be computed in the following way:
398
// Let n = d1 x d2 - then vector n is perpendicular to both d1 and d2 --> (c1-c2) is
399
// parallel to n.
400
// There exist real numbers A,B,C such that
401
// c1 = p1 + A*d1
402
// c2 = p2 + B*d2 and
403
// c2 = c1 + C*n so that
404
// p1 + A*d1 + C*n = p2 + B*d2  --> (p1-p2) + A*d1 = B*d2 - C*n (*)
405
// Let n2 = n x d2. Let's multiply both sides of (*) by n2. Then
406
// (p1-p2)*n2 + A*(d1*n2) = 0 (0 because d1*n2 = n*n2 = 0)
407
// and from that A = [(p1-p2)*n2]/[d1*n2]
408
// Similarly     B = [(p2-p1)*n1]/[d2*n1]  where n1 = n x d1.
409

    
410
  private boolean isInside(float[] point, float[][] vertices)
411
    {
412
    float e1x = (vertices[0][0]+vertices[1][0])/2;
413
    float e1y = (vertices[0][1]+vertices[1][1])/2;
414
    float e1z = (vertices[0][2]+vertices[1][2])/2;
415

    
416
    float d1x = e1x - point[0];
417
    float d1y = e1y - point[1];
418
    float d1z = e1z - point[2];
419

    
420
    float ax = vertices[0][0] - vertices[1][0];
421
    float ay = vertices[0][1] - vertices[1][1];
422
    float az = vertices[0][2] - vertices[1][2];
423

    
424
    float normX = d1y*az - d1z*ay;
425
    float normY = d1z*ax - d1x*az;
426
    float normZ = d1x*ay - d1y*ax;
427

    
428
    float n1x = d1y*normZ - d1z*normY;
429
    float n1y = d1z*normX - d1x*normZ;
430
    float n1z = d1x*normY - d1y*normX;
431

    
432
    float p1x = point[0];
433
    float p1y = point[1];
434
    float p1z = point[2];
435

    
436
    int len = vertices.length;
437
    int numCrossings = 0;
438

    
439
    for(int side=0; side<len; side++)
440
      {
441
      float p2x = vertices[side][0];
442
      float p2y = vertices[side][1];
443
      float p2z = vertices[side][2];
444

    
445
      int next = side==len-1 ? 0 : side+1;
446

    
447
      float e2x = vertices[next][0];
448
      float e2y = vertices[next][1];
449
      float e2z = vertices[next][2];
450

    
451
      float d2x = e2x-p2x;
452
      float d2y = e2y-p2y;
453
      float d2z = e2z-p2z;
454

    
455
      float nx = d2y*d1z - d2z*d1y;
456
      float ny = d2z*d1x - d2x*d1z;
457
      float nz = d2x*d1y - d2y*d1x;
458

    
459
      float n2x = d2y*nz - d2z*ny;
460
      float n2y = d2z*nx - d2x*nz;
461
      float n2z = d2x*ny - d2y*nx;
462

    
463
      float dpx = p1x-p2x;
464
      float dpy = p1y-p2y;
465
      float dpz = p1z-p2z;
466

    
467
      float A1 =-dpx*n2x-dpy*n2y-dpz*n2z;
468
      float B1 = d1x*n2x+d1y*n2y+d1z*n2z;
469

    
470
      float A2 = dpx*n1x+dpy*n1y+dpz*n1z;
471
      float B2 = d2x*n1x+d2y*n1y+d2z*n1z;
472

    
473
      if( B1==0 || B2==0 ) continue;
474

    
475
      float C1 = A1/B1;
476
      float C2 = A2/B2;
477

    
478
      float c1x = p1x + C1*d1x;
479
      float c1y = p1y + C1*d1y;
480
      float c1z = p1z + C1*d1z;
481

    
482
      float c2x = p2x + C2*d2x;
483
      float c2y = p2y + C2*d2y;
484
      float c2z = p2z + C2*d2z;
485

    
486
      if( !isBetween(c1x,c1y,c1z, p1x,p1y,p1z, e1x,e1y,e1z ) &&
487
           isBetween(p2x,p2y,p2z, c2x,c2y,c2z, e2x,e2y,e2z )  )
488
        {
489
        numCrossings++;
490
        }
491
      }
492

    
493
    return (numCrossings%2)==1;
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  private void rotateVertices(float[][] points, float[][] rotated, float[] quat)
499
    {
500
    int numPoints = points.length;
501

    
502
    for(int i=0; i<numPoints; i++)
503
      {
504
      QuatHelper.rotateVectorByQuat(rotated[i],points[i],quat);
505
      }
506
    }
507

    
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
510
// a normalVec (nx,ny,nz) and distance (which together define a plane) compute point 'output[]' which:
511
// 1) lies on this plane
512
// 2) is co-linear with mCamera and mPoint
513
//
514
// output = camera + alpha*(point-camera), where alpha = [dist-normalVec*camera] / [normalVec*(point-camera)]
515

    
516
  void castTouchPointOntoFace(float nx, float ny, float nz, float distance, float[] output)
517
    {
518
    float d0 = mPoint[0]-mCamera[0];
519
    float d1 = mPoint[1]-mCamera[1];
520
    float d2 = mPoint[2]-mCamera[2];
521

    
522
    float denom = nx*d0 + ny*d1 + nz*d2;
523

    
524
    if( denom != 0.0f )
525
      {
526
      float axisCam = nx*mCamera[0] + ny*mCamera[1] + nz*mCamera[2];
527
      float alpha = (distance-axisCam)/denom;
528

    
529
      output[0] = mCamera[0] + d0*alpha;
530
      output[1] = mCamera[1] + d1*alpha;
531
      output[2] = mCamera[2] + d2*alpha;
532
      }
533
    }
534

    
535
///////////////////////////////////////////////////////////////////////////////////////////////////
536

    
537
  private boolean cubitFaceIsVisible(float nx, float ny, float nz, float distance)
538
    {
539
    return mCamera[0]*nx + mCamera[1]*ny + mCamera[2]*nz > distance;
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543
// FaceInfo defines a 3D plane (by means of a unit normal vector 'vector' and distance from the origin
544
// 'distance') and a list of points on the plane ('vertices').
545
//
546
// 0) rotate the face normal vector by quat
547
// 1) see if the face is visible. If not, return NOT_TOUCHED
548
// 2) else, cast the line passing through mPoint and mCamera onto this plane
549
// 3) if Z of this point is further from us than the already computed closestSoFar, return NOT_TOUCHED
550
// 4) else, rotate 'vertices' by quat and see if the casted point lies inside the polygon defined by them
551
// 5) if yes, return the distance from this point to the camera; otherwise, return NOT_TOUCHED
552

    
553
  private float cubitFaceTouched(FaceInfo info, float[] quat, float closestSoFar)
554
    {
555
    QuatHelper.rotateVectorByQuat(mTmp,info.normal,quat);
556
    float nx = mTmp[0];
557
    float ny = mTmp[1];
558
    float nz = mTmp[2];
559

    
560
    if( cubitFaceIsVisible(nx,ny,nz,info.distance) )
561
      {
562
      castTouchPointOntoFace(nx,ny,nz,info.distance,mTouch);
563

    
564
      float dx = mTouch[0]-mCamera[0];
565
      float dy = mTouch[1]-mCamera[1];
566
      float dz = mTouch[2]-mCamera[2];
567
      float dist = dx*dx + dy*dy + dz*dz;
568

    
569
      if( dist<closestSoFar )
570
        {
571
        rotateVertices(info.vertices,info.rotated,quat);
572
        if( isInside(mTouch,info.rotated) ) return dist;
573
        }
574
      }
575

    
576
    return NOT_TOUCHED;
577
    }
578

    
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580
// This is in order to support non-rotatable rows.
581
// If mTouchedCubit ( as computed by objectTouched() ) turns out to be not rotatable with respect to
582
// the rotAxis, we need to figure out which of the neighbouring 'rotatable' cubits is the closest.
583
//
584
// Note that we cannot do it in objectTouched(), because then we do not yet know which axis we are
585
// going to be rotating along.
586

    
587
  private float distanceToRow(int rotIndex, int row)
588
    {
589
    float closestSoFar = NOT_TOUCHED;
590
    int numQuats = mQuats.length;
591
    int bmp = (1<<row);
592

    
593
    for(int cubit=0; cubit<mNumCubits; cubit++)
594
      {
595
      int rowBitmap = mObject.getCubitRotRow(cubit,rotIndex);
596

    
597
      if( (rowBitmap&bmp) != 0 )
598
        {
599
        int quatIndex = mObject.getCubitQuatIndex(cubit);
600

    
601
        if( quatIndex<numQuats )
602
          {
603
          float[] quat = mQuats[quatIndex];
604

    
605
          for(int face=0; face<mNumFaces[cubit]; face++)
606
            {
607
            float dist = cubitFaceTouched(mInfos[cubit][face],quat,closestSoFar);
608
            if( dist!=NOT_TOUCHED ) closestSoFar = dist;
609
            }
610
          }
611
        }
612
      }
613

    
614
    return closestSoFar;
615
    }
616

    
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618

    
619
  private int notRotatable(int axis)
620
    {
621
    Static3D a = mRotAxis[axis];
622
    float x = mLastT[0]*a.get0() + mLastT[1]*a.get1() + mLastT[2]*a.get2();
623
    x *= mObject.getSize();
624
    float[] cuts = mCuts[axis];
625

    
626
    if( cuts==null ) return -1;
627
    int l = cuts.length;
628

    
629
    if( l>0 && x<=cuts[0]   ) return (l>=2 && x>=(3*cuts[  0]-cuts[  1])/2) ?   1 : -1;
630
    if( l>0 && x>=cuts[l-1] ) return (l>=2 && x<=(3*cuts[l-2]-cuts[l-1])/2) ? l-1 : -1;
631

    
632
    for(int i=1; i<l; i++)
633
      if( x<=cuts[i] )
634
        return x<((cuts[i-1]+cuts[i])/2) ? i-1 : i+1;
635

    
636
    return -1;
637
    }
638

    
639
///////////////////////////////////////////////////////////////////////////////////////////////////
640

    
641
  int computeRow(int cubit, int axis)
642
    {
643
    int row = mObject.getCubitRotRow(cubit,axis);
644

    
645
    for(int r=0; r<32; r++)
646
      {
647
      if( (row&1)==1 )
648
        return mRotatable[axis][r] ? r : notRotatable(axis);
649

    
650
      row>>=1;
651
      }
652

    
653
    return -1;
654
    }
655

    
656
///////////////////////////////////////////////////////////////////////////////////////////////////
657
// PUBLIC API
658
///////////////////////////////////////////////////////////////////////////////////////////////////
659

    
660
  public boolean objectTouched(Static4D rotatedTouchPoint, Static4D rotatedCamera)
661
    {
662
    if( !mPreparationDone ) prepare();
663

    
664
    mPoint[0]  = rotatedTouchPoint.get0()/mObjectRatio;
665
    mPoint[1]  = rotatedTouchPoint.get1()/mObjectRatio;
666
    mPoint[2]  = rotatedTouchPoint.get2()/mObjectRatio;
667

    
668
    mCamera[0] = rotatedCamera.get0()/mObjectRatio;
669
    mCamera[1] = rotatedCamera.get1()/mObjectRatio;
670
    mCamera[2] = rotatedCamera.get2()/mObjectRatio;
671

    
672
    float closestSoFar = NOT_TOUCHED;
673
    mTouchedCubit = -1;
674
    mTouchedCubitFace = -1;
675
    int numQuats = mQuats.length;
676

    
677
    for(int cubit=0; cubit<mNumCubits; cubit++)
678
      {
679
      int quatIndex = mObject.getCubitQuatIndex(cubit);
680

    
681
      if( quatIndex<numQuats )
682
        {
683
        float[] quat = mQuats[quatIndex];
684

    
685
        for(int face=0; face<mNumFaces[cubit]; face++)
686
          {
687
          float dist = cubitFaceTouched(mInfos[cubit][face],quat,closestSoFar);
688

    
689
          if( dist!=NOT_TOUCHED )
690
            {
691
            mTouchedCubit= cubit;
692
            mTouchedCubitFace = face;
693
            closestSoFar = dist;
694
            mLastT[0] = mTouch[0];
695
            mLastT[1] = mTouch[1];
696
            mLastT[2] = mTouch[2];
697
            }
698
          }
699
        }
700
      }
701

    
702
    return closestSoFar!=NOT_TOUCHED;
703
    }
704

    
705
///////////////////////////////////////////////////////////////////////////////////////////////////
706
// really implemented in derived classes; here present only because we need to be able to
707
// instantiate an object of this class for MODE_REPLACE.
708

    
709
  public void newRotation(int[] output, Static4D rotatedTouchPoint, Static4D quat)
710
    {
711

    
712
    }
713

    
714
///////////////////////////////////////////////////////////////////////////////////////////////////
715

    
716
  public void getCastedRotAxis(float[] output, Static4D quat, int axisIndex)
717
    {
718
    Static3D a = mRotAxis[axisIndex];
719
    getCastedRotAxis(output,quat,a.get0(),a.get1(),a.get2(),0);
720
    }
721

    
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723

    
724
  public boolean axisAndFaceAgree(int axisIndex)
725
    {
726
    return false;
727
    }
728

    
729
///////////////////////////////////////////////////////////////////////////////////////////////////
730

    
731
  public float[] getTouchedPuzzleCenter()
732
    {
733
    return null;
734
    }
735

    
736
///////////////////////////////////////////////////////////////////////////////////////////////////
737

    
738
  public int getTouchedCubitFace()
739
    {
740
    return mTouchedCubitFace;
741
    }
742

    
743
///////////////////////////////////////////////////////////////////////////////////////////////////
744

    
745
  public int getTouchedCubit()
746
    {
747
    return mTouchedCubit;
748
    }
749
  }
(10-10/14)