Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 418aa554

1 fdec60a3 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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 fdec60a3 Leszek Koltunski
22 27a70eae Leszek Koltunski
import android.content.SharedPreferences;
23 ccf9fec5 Leszek Koltunski
import android.content.res.Resources;
24 411c6285 Leszek Koltunski
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27 27a70eae Leszek Koltunski
28 27e6c301 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
29
30 27a70eae Leszek Koltunski
import org.distorted.library.effect.Effect;
31 19f0f767 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32 27a70eae Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 10585385 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
35 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
36 27a70eae Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.main.DistortedTexture;
39 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
40 ccf9fec5 Leszek Koltunski
import org.distorted.library.mesh.MeshFile;
41 19f0f767 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
42 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
43 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
44 27e6c301 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
45 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48 4f9f99a2 Leszek Koltunski
49 ccf9fec5 Leszek Koltunski
import java.io.DataInputStream;
50
import java.io.IOException;
51
import java.io.InputStream;
52
53 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 27a70eae Leszek Koltunski
public abstract class RubikObject extends DistortedNode
56 fdec60a3 Leszek Koltunski
  {
57 c7b00dfb Leszek Koltunski
  private static final float NODE_RATIO = 1.32f;
58
  private static final float MAX_SIZE_CHANGE = 1.3f;
59
  private static final float MIN_SIZE_CHANGE = 0.8f;
60
61 19f0f767 Leszek Koltunski
  private static boolean mCreateFromDMesh = true;
62
63 8cccfb10 Leszek Koltunski
  private static final Static3D CENTER = new Static3D(0,0,0);
64 14bd7976 Leszek Koltunski
  static final int INTERIOR_COLOR = 0xff000000;
65 27e6c301 Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
66 7289fd6c Leszek Koltunski
  private static final int TEXTURE_HEIGHT = 128;
67 27e6c301 Leszek Koltunski
68 efef689c Leszek Koltunski
  final Static3D[] ROTATION_AXIS;
69 98904e45 Leszek Koltunski
  final Static4D[] QUATS;
70 470820a7 Leszek Koltunski
  final int NUM_FACES;
71 27a70eae Leszek Koltunski
72 c7b00dfb Leszek Koltunski
  private static float mInitScreenRatio,mObjectScreenRatio;
73 f0fa83ae Leszek Koltunski
74 a10ada2a Leszek Koltunski
  private final int NUM_CUBITS;
75 5b893eee Leszek Koltunski
  private final int mNodeSize;
76 9224ffd2 Leszek Koltunski
  private int mRotRowBitmap;
77 efef689c Leszek Koltunski
  private int mRotAxis;
78 49f67f9b Leszek Koltunski
  private Static3D[] mOrigPos;
79 fb6a40c8 Leszek Koltunski
  private Static3D mNodeScale;
80 4da7d87a Leszek Koltunski
  private Static4D mQuat;
81 b32444ee Leszek Koltunski
  private Cubit[] mCubits;
82 49f67f9b Leszek Koltunski
  private int mSize;
83 aa171dee Leszek Koltunski
  private RubikObjectList mList;
84 470820a7 Leszek Koltunski
  private MeshBase mMesh;
85
  private DistortedEffects mEffects;
86 27e6c301 Leszek Koltunski
  private VertexEffectRotate mRotateEffect;
87
  private Dynamic1D mRotationAngle;
88
  private Static3D mRotationAxis;
89 c7b00dfb Leszek Koltunski
  private Static3D mObjectScale;
90 27a70eae Leszek Koltunski
91 e844c116 Leszek Koltunski
  float mStart, mStep;
92 fdec60a3 Leszek Koltunski
93 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
94
  DistortedTexture mTexture;
95
96
  MatrixEffectScale mScaleEffect;
97 4da7d87a Leszek Koltunski
  MatrixEffectQuaternion mQuatEffect;
98 27a70eae Leszek Koltunski
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100 fdec60a3 Leszek Koltunski
101 4da7d87a Leszek Koltunski
  RubikObject(int size, int fov, Static4D quat, DistortedTexture nodeTexture, MeshRectangles nodeMesh,
102 5b893eee Leszek Koltunski
              DistortedEffects nodeEffects, int[][] moves, RubikObjectList list, Resources res, int screenWidth)
103 fdec60a3 Leszek Koltunski
    {
104 411c6285 Leszek Koltunski
    super(nodeTexture,nodeEffects,nodeMesh);
105 fdec60a3 Leszek Koltunski
106 5b893eee Leszek Koltunski
    mNodeSize = screenWidth;
107
108 c7b00dfb Leszek Koltunski
    resizeFBO(mNodeSize, (int)(NODE_RATIO*mNodeSize));
109 d41742f7 Leszek Koltunski
110 aa171dee Leszek Koltunski
    mList = list;
111 49f67f9b Leszek Koltunski
    mOrigPos = getCubitPositions(size);
112 10a2e360 Leszek Koltunski
113 98904e45 Leszek Koltunski
    QUATS = getQuats();
114 49f67f9b Leszek Koltunski
    NUM_CUBITS  = mOrigPos.length;
115 efef689c Leszek Koltunski
    ROTATION_AXIS = getRotationAxis();
116 c7b00dfb Leszek Koltunski
    mObjectScreenRatio = getScreenRatio();
117
    mInitScreenRatio = mObjectScreenRatio;
118 470820a7 Leszek Koltunski
    NUM_FACES = getNumFaces();
119 a10ada2a Leszek Koltunski
120 27a70eae Leszek Koltunski
    mSize = size;
121 49f67f9b Leszek Koltunski
    computeStartAndStep(mOrigPos);
122 c7b00dfb Leszek Koltunski
    mNodeScale= new Static3D(1,NODE_RATIO,1);
123 4da7d87a Leszek Koltunski
    mQuat = quat;
124 e844c116 Leszek Koltunski
125 27e6c301 Leszek Koltunski
    mRotationAngle= new Dynamic1D();
126
    mRotationAxis = new Static3D(1,0,0);
127 8cccfb10 Leszek Koltunski
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
128 27e6c301 Leszek Koltunski
129 27a70eae Leszek Koltunski
    mRotationAngleStatic = new Static1D(0);
130
    mRotationAngleMiddle = new Static1D(0);
131
    mRotationAngleFinal  = new Static1D(0);
132
133 19f0f767 Leszek Koltunski
    float scale  = mObjectScreenRatio*mNodeSize/mSize;
134
    mObjectScale = new Static3D(scale,scale,scale);
135 c7b00dfb Leszek Koltunski
    mScaleEffect = new MatrixEffectScale(mObjectScale);
136 4da7d87a Leszek Koltunski
    mQuatEffect  = new MatrixEffectQuaternion(quat, CENTER);
137 27a70eae Leszek Koltunski
138
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
139 411c6285 Leszek Koltunski
    nodeEffects.apply(nodeScaleEffect);
140 a10ada2a Leszek Koltunski
141 19f0f767 Leszek Koltunski
    createMeshAndCubits(list,res);
142 7381193e Leszek Koltunski
143 19f0f767 Leszek Koltunski
    mTexture = new DistortedTexture();
144 470820a7 Leszek Koltunski
    mEffects = new DistortedEffects();
145 10585385 Leszek Koltunski
146 98904e45 Leszek Koltunski
    int num_quats = QUATS.length;
147 10585385 Leszek Koltunski
    for(int q=0; q<num_quats; q++)
148
      {
149 98904e45 Leszek Koltunski
      VertexEffectQuaternion vq = new VertexEffectQuaternion(QUATS[q],CENTER);
150 10585385 Leszek Koltunski
      vq.setMeshAssociation(0,q);
151
      mEffects.apply(vq);
152
      }
153
154 27e6c301 Leszek Koltunski
    mEffects.apply(mRotateEffect);
155 4da7d87a Leszek Koltunski
    mEffects.apply(mQuatEffect);
156 470820a7 Leszek Koltunski
    mEffects.apply(mScaleEffect);
157
158
    attach( new DistortedNode(mTexture,mEffects,mMesh) );
159
160 aa171dee Leszek Koltunski
    setupPosition(moves);
161
162 4888e97c Leszek Koltunski
    setProjection(fov, 0.1f);
163 27a70eae Leszek Koltunski
    }
164
165 19f0f767 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
  private void createMeshAndCubits(RubikObjectList list, Resources res)
168
    {
169
    mCubits = new Cubit[NUM_CUBITS];
170
171
    if( mCreateFromDMesh )
172
      {
173
      int sizeIndex = RubikObjectList.getSizeIndex(list.ordinal(),mSize);
174
      int resourceID= list.getResourceIDs()[sizeIndex];
175
176
      InputStream is = res.openRawResource(resourceID);
177
      DataInputStream dos = new DataInputStream(is);
178
      mMesh = new MeshFile(dos);
179
180
      try
181
        {
182
        is.close();
183
        }
184
      catch(IOException e)
185
        {
186
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
187
        }
188
189
      for(int i=0; i<NUM_CUBITS; i++)
190
        {
191
        mCubits[i] = new Cubit(this,mOrigPos[i]);
192
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
193
        }
194
      }
195
    else
196
      {
197
      MeshBase[] cubitMesh = new MeshBase[NUM_CUBITS];
198
199
      for(int i=0; i<NUM_CUBITS; i++)
200
        {
201
        mCubits[i] = new Cubit(this,mOrigPos[i]);
202
        cubitMesh[i] = createCubitMesh(i);
203
        cubitMesh[i].apply(new MatrixEffectMove(mOrigPos[i]),1,0);
204
        cubitMesh[i].setEffectAssociation(0, mCubits[i].computeAssociation(), 0);
205
        }
206
207
      mMesh = new MeshJoined(cubitMesh);
208
      resetAllTextureMaps();
209
      }
210
    }
211
212 c7b00dfb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214
  public void setObjectRatio(float sizeChange)
215
    {
216
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
217
218
    if( mObjectScreenRatio > MAX_SIZE_CHANGE*mInitScreenRatio)
219
      {
220
      mObjectScreenRatio = MAX_SIZE_CHANGE*mInitScreenRatio;
221
      }
222
    if( mObjectScreenRatio < MIN_SIZE_CHANGE*mInitScreenRatio)
223
      {
224
      mObjectScreenRatio = MIN_SIZE_CHANGE*mInitScreenRatio;
225
      }
226
227
    float scale = mObjectScreenRatio*mNodeSize/mSize;
228
    mObjectScale.set(scale,scale,scale);
229
    }
230
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233
  static float getObjectRatio()
234
    {
235
    return mObjectScreenRatio;
236
    }
237
238 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
239 97d2f701 Leszek Koltunski
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
240
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
241
// consecutive).
242
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
243
// the Cube and the Pyraminx.
244
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
245
// given Cubit belongs to.
246 e844c116 Leszek Koltunski
247
  private void computeStartAndStep(Static3D[] pos)
248
    {
249
    float min = Float.MAX_VALUE;
250
    float max = Float.MIN_VALUE;
251
    float axisX = ROTATION_AXIS[0].get0();
252
    float axisY = ROTATION_AXIS[0].get1();
253
    float axisZ = ROTATION_AXIS[0].get2();
254
    float tmp;
255
256
    for(int i=0; i<NUM_CUBITS; i++)
257
      {
258
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
259
      if( tmp<min ) min=tmp;
260
      if( tmp>max ) max=tmp;
261
      }
262
263
    mStart = min;
264
    mStep  = (max-min+1.0f)/mSize;
265
    }
266
267 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
268
269 9224ffd2 Leszek Koltunski
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
270 efef689c Leszek Koltunski
    {
271 9224ffd2 Leszek Koltunski
    int cubitRow = (int)(mCubits[cubit].mRotationRow[axis]+0.5f);
272
    return ((1<<cubitRow)&rowBitmap)!=0;
273 66cbdd21 Leszek Koltunski
    }
274
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
277
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
278
// away from the face of the Pyraminx shouldn't be textured.
279
280
  private boolean isOnFace( int cubit, int axis, int row)
281
    {
282
    final float MAX_ERROR = 0.0001f;
283
    float diff = mCubits[cubit].mRotationRow[axis] - row;
284
    return diff*diff < MAX_ERROR;
285 efef689c Leszek Koltunski
    }
286
287 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
288 a31d25de Leszek Koltunski
// note the minus in front of the sin() - we rotate counterclockwise
289
// when looking towards the direction where the axis increases in values.
290 aa171dee Leszek Koltunski
291 a31d25de Leszek Koltunski
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
292 aa171dee Leszek Koltunski
    {
293 a31d25de Leszek Koltunski
    Static3D axis = ROTATION_AXIS[axisIndex];
294
295
    while( angleInDegrees<0 ) angleInDegrees += 360;
296
    angleInDegrees %= 360;
297
    
298
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
299
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
300
301
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
302
    }
303
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306 8bbac3c2 Leszek Koltunski
  private synchronized void setupPosition(int[][] moves)
307 a31d25de Leszek Koltunski
    {
308
    if( moves!=null )
309
      {
310
      Static4D quat;
311 818431ed Leszek Koltunski
      int index, axis, rowBitmap, angle;
312 a31d25de Leszek Koltunski
      int corr = (360/getBasicAngle());
313
314
      for(int[] move: moves)
315
        {
316
        axis     = move[0];
317
        rowBitmap= move[1];
318
        angle    = move[2]*corr;
319
        quat     = makeQuaternion(axis,angle);
320
321
        for(int j=0; j<NUM_CUBITS; j++)
322
          if( belongsToRotation(j,axis,rowBitmap) )
323
            {
324 98904e45 Leszek Koltunski
            index = mCubits[j].removeRotationNow(quat);
325
            mMesh.setEffectAssociation(j,mCubits[j].computeAssociation(),index);
326 a31d25de Leszek Koltunski
            }
327
        }
328
      }
329 aa171dee Leszek Koltunski
    }
330
331 fa0f7a56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
332
333
  int getCubitFaceColorIndex(int cubit, int face)
334
    {
335 470820a7 Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(NUM_FACES*cubit + face);
336
    return (int)(texMap.get0() / texMap.get2());
337 fa0f7a56 Leszek Koltunski
    }
338
339 49f67f9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
340
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
341
342
  void clampPos(Static3D pos)
343
    {
344
    float currError, minError = Float.MAX_VALUE;
345
    int minErrorIndex= -1;
346
    float x = pos.get0();
347
    float y = pos.get1();
348
    float z = pos.get2();
349
    float xo,yo,zo;
350
351
    for(int i=0; i<NUM_CUBITS; i++)
352
      {
353
      xo = mOrigPos[i].get0();
354
      yo = mOrigPos[i].get1();
355
      zo = mOrigPos[i].get2();
356
357
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
358
359
      if( currError<minError )
360
        {
361
        minError = currError;
362
        minErrorIndex = i;
363
        }
364
      }
365
366
    pos.set( mOrigPos[minErrorIndex] );
367
    }
368
369 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// the getFaceColors + final black in a horizontal strip.
371
372
  public void createTexture()
373
    {
374
    Bitmap bitmap;
375
376
    Paint paint = new Paint();
377 470820a7 Leszek Koltunski
    bitmap = Bitmap.createBitmap( (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
378 411c6285 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
379
380
    paint.setAntiAlias(true);
381
    paint.setTextAlign(Paint.Align.CENTER);
382
    paint.setStyle(Paint.Style.FILL);
383
384 14bd7976 Leszek Koltunski
    paint.setColor(INTERIOR_COLOR);
385 470820a7 Leszek Koltunski
    canvas.drawRect(0, 0, (NUM_FACES+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
386 411c6285 Leszek Koltunski
387 470820a7 Leszek Koltunski
    for(int i=0; i<NUM_FACES; i++)
388 411c6285 Leszek Koltunski
      {
389 ca292407 Leszek Koltunski
      createFaceTexture(canvas, paint, i, i*TEXTURE_HEIGHT, 0, TEXTURE_HEIGHT);
390 411c6285 Leszek Koltunski
      }
391
392
    mTexture.setTexture(bitmap);
393
    }
394
395 dd73fdab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
396
397 27a70eae Leszek Koltunski
  public int getSize()
398 fdec60a3 Leszek Koltunski
    {
399 27a70eae Leszek Koltunski
    return mSize;
400 fdec60a3 Leszek Koltunski
    }
401
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403
404 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
405 fdec60a3 Leszek Koltunski
    {
406 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
407 fdec60a3 Leszek Koltunski
    }
408
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410
411 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
412
      {
413 4da7d87a Leszek Koltunski
      return mQuat;
414 27a70eae Leszek Koltunski
      }
415
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
418 5ba13c05 Leszek Koltunski
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
419 fdec60a3 Leszek Koltunski
    {
420 f0fa83ae Leszek Koltunski
    float factor = Math.min(scrWidth,scrHeight);
421 c7b00dfb Leszek Koltunski
    mNodeScale.set(factor,NODE_RATIO*factor,factor);
422 fdec60a3 Leszek Koltunski
    }
423 27a70eae Leszek Koltunski
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
426 a10ada2a Leszek Koltunski
  public void savePreferences(SharedPreferences.Editor editor)
427
    {
428
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
429
    }
430 f16ff19d Leszek Koltunski
431 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
432 27a70eae Leszek Koltunski
433 8bbac3c2 Leszek Koltunski
  public synchronized void restorePreferences(SharedPreferences preferences)
434 a10ada2a Leszek Koltunski
    {
435 2fcad75d Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
436
      {
437
      int index = mCubits[i].restorePreferences(preferences);
438
      mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
439
      }
440 a10ada2a Leszek Koltunski
    }
441 27a70eae Leszek Koltunski
442 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
443
444
  public void releaseResources()
445
    {
446
    mTexture.markForDeletion();
447
    }
448
449
///////////////////////////////////////////////////////////////////////////////////////////////////
450
451
  public void apply(Effect effect, int position)
452
    {
453 8cccfb10 Leszek Koltunski
    mEffects.apply(effect, position);
454 a10ada2a Leszek Koltunski
    }
455
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457
458
  public void remove(long effectID)
459
    {
460 8cccfb10 Leszek Koltunski
    mEffects.abortById(effectID);
461 a10ada2a Leszek Koltunski
    }
462 74686c71 Leszek Koltunski
463 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
464
465 8bbac3c2 Leszek Koltunski
  public synchronized void solve()
466 a10ada2a Leszek Koltunski
    {
467 98904e45 Leszek Koltunski
    for(int i=0; i<NUM_CUBITS; i++)
468
      {
469
      mCubits[i].solve();
470
      mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
471
      }
472 a10ada2a Leszek Koltunski
    }
473
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475
476
  public boolean isSolved()
477
    {
478 2fcad75d Leszek Koltunski
    int index = mCubits[0].mQuatIndex;
479 a10ada2a Leszek Koltunski
480 94ad5a8f Leszek Koltunski
    for(int i=1; i<NUM_CUBITS; i++)
481 a10ada2a Leszek Koltunski
      {
482 2fcad75d Leszek Koltunski
      if( !mCubits[i].thereIsNoVisibleDifference(index) ) return false;
483 a10ada2a Leszek Koltunski
      }
484
485
    return true;
486
    }
487
488 1f9772f3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
489
490
  public void resetAllTextureMaps()
491
    {
492 ad73edd5 Leszek Koltunski
    boolean belongs;
493
    final float ratio = 1.0f/(NUM_FACES+1);
494
495
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
496 1f9772f3 Leszek Koltunski
      {
497 ad73edd5 Leszek Koltunski
      final Static4D[] maps = new Static4D[NUM_FACES];
498
499
      if( 2*ROTATION_AXIS.length == NUM_FACES )  // i.e. there are faces on both ends of the axis (cube)
500
        {
501
        for(int i=0; i<NUM_FACES; i++)
502
          {
503
          belongs = isOnFace(cubit, i/2, i%2==0 ? mSize-1:0 );
504
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
505
          }
506
        }
507
      else if( ROTATION_AXIS.length == NUM_FACES )  // just a single face on the right end of an axis (pyraminx)
508
        {
509
        for(int i=0; i<NUM_FACES; i++)
510
          {
511
          belongs = isOnFace(cubit, i, 0 );
512
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
513
          }
514
        }
515
516
      mMesh.setTextureMap(maps,NUM_FACES*cubit);
517 1f9772f3 Leszek Koltunski
      }
518
    }
519
520
///////////////////////////////////////////////////////////////////////////////////////////////////
521
522
  public void setTextureMap(int cubit, int face, int newColor)
523
    {
524 470820a7 Leszek Koltunski
    final float ratio = 1.0f/(NUM_FACES+1);
525
    final Static4D[] maps = new Static4D[NUM_FACES];
526 1f9772f3 Leszek Koltunski
527 e03e0352 Leszek Koltunski
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
528
    mMesh.setTextureMap(maps,NUM_FACES*cubit);
529 1f9772f3 Leszek Koltunski
    }
530
531 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
532
533 8bbac3c2 Leszek Koltunski
  public synchronized void beginNewRotation(int axis, int row )
534 a10ada2a Leszek Koltunski
    {
535 9cd7695f Leszek Koltunski
    if( axis<0 || axis>=ROTATION_AXIS.length )
536
      {
537
      android.util.Log.e("object", "invalid rotation axis: "+axis);
538
      return;
539
      }
540
    if( row<0 || row>=mSize )
541
      {
542
      android.util.Log.e("object", "invalid rotation row: "+row);
543
      return;
544
      }
545
546 27e6c301 Leszek Koltunski
    mRotAxis     = axis;
547
    mRotRowBitmap= (1<<row);
548 a10ada2a Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
549 27e6c301 Leszek Koltunski
    mRotationAxis.set( ROTATION_AXIS[axis] );
550
    mRotationAngle.add(mRotationAngleStatic);
551 8cccfb10 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
552 27e6c301 Leszek Koltunski
    }
553 a10ada2a Leszek Koltunski
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555
556 8bbac3c2 Leszek Koltunski
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
557 27e6c301 Leszek Koltunski
    {
558
    mRotAxis     = axis;
559
    mRotRowBitmap= rowBitmap;
560 a10ada2a Leszek Koltunski
561 27e6c301 Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
562
    mRotationAxis.set( ROTATION_AXIS[axis] );
563
    mRotationAngle.setDuration(durationMillis);
564
    mRotationAngle.resetToBeginning();
565
    mRotationAngle.add(new Static1D(0));
566
    mRotationAngle.add(new Static1D(angle));
567 8cccfb10 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
568 27e6c301 Leszek Koltunski
    mRotateEffect.notifyWhenFinished(listener);
569
570
    return mRotateEffect.getID();
571
    }
572 a10ada2a Leszek Koltunski
573 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
574 a10ada2a Leszek Koltunski
575 168b6b56 Leszek Koltunski
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
576 27e6c301 Leszek Koltunski
    {
577
    float angle = getAngle();
578
    mRotationAngleStatic.set0(angle);
579
    mRotationAngleFinal.set0(nearestAngleInDegrees);
580
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
581
582
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
583
    mRotationAngle.resetToBeginning();
584
    mRotationAngle.removeAll();
585
    mRotationAngle.add(mRotationAngleStatic);
586
    mRotationAngle.add(mRotationAngleMiddle);
587
    mRotationAngle.add(mRotationAngleFinal);
588
    mRotateEffect.notifyWhenFinished(listener);
589
590
    return mRotateEffect.getID();
591
    }
592 001cc0e4 Leszek Koltunski
593 a10ada2a Leszek Koltunski
594 001cc0e4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
595
596 27e6c301 Leszek Koltunski
  private float getAngle()
597 001cc0e4 Leszek Koltunski
    {
598 27e6c301 Leszek Koltunski
    int pointNum = mRotationAngle.getNumPoints();
599 001cc0e4 Leszek Koltunski
600 27e6c301 Leszek Koltunski
    if( pointNum>=1 )
601 001cc0e4 Leszek Koltunski
      {
602 27e6c301 Leszek Koltunski
      return mRotationAngle.getPoint(pointNum-1).get0();
603
      }
604
    else
605
      {
606
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
607
      crashlytics.log("points in RotationAngle: "+pointNum);
608
      return 0;
609 001cc0e4 Leszek Koltunski
      }
610
    }
611
612 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
613
614 8bbac3c2 Leszek Koltunski
  public synchronized void removeRotationNow()
615 168b6b56 Leszek Koltunski
    {
616
    float angle = getAngle();
617
    double nearestAngleInRadians = angle*Math.PI/180;
618
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
619
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
620
    float axisX = ROTATION_AXIS[mRotAxis].get0();
621
    float axisY = ROTATION_AXIS[mRotAxis].get1();
622
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
623
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
624
625
    mRotationAngle.removeAll();
626
    mRotationAngleStatic.set0(0);
627
628
    for(int i=0; i<NUM_CUBITS; i++)
629
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
630
        {
631
        int index = mCubits[i].removeRotationNow(quat);
632
        mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
633
        }
634
    }
635 a10ada2a Leszek Koltunski
636 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
637
638 a31d25de Leszek Koltunski
  public void initializeObject(int[][] moves)
639 aa171dee Leszek Koltunski
    {
640
    solve();
641
    setupPosition(moves);
642
    }
643
644 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
645
646
  public int getCubit(float[] point3D)
647
    {
648 418aa554 Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
649 9621255f Leszek Koltunski
    int currentBest=-1;
650
    float multiplier = returnMultiplier();
651
652
    point3D[0] *= multiplier;
653
    point3D[1] *= multiplier;
654
    point3D[2] *= multiplier;
655
656
    for(int i=0; i<NUM_CUBITS; i++)
657
      {
658
      dist = mCubits[i].getDistSquared(point3D);
659
      if( dist<minDist )
660
        {
661
        minDist = dist;
662
        currentBest = i;
663
        }
664
      }
665
666
    return currentBest;
667
    }
668
669 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
670
671 168b6b56 Leszek Koltunski
  public int computeNearestAngle(float angle, float speed)
672 0e5ad27c Leszek Koltunski
    {
673
    final int NEAREST = 360/getBasicAngle();
674
675 4c864c68 Leszek Koltunski
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
676
    if( angle< -(NEAREST*0.5) ) tmp-=1;
677 168b6b56 Leszek Koltunski
678 4c864c68 Leszek Koltunski
    if( tmp!=0 ) return NEAREST*tmp;
679 168b6b56 Leszek Koltunski
680 c7b00dfb Leszek Koltunski
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
681 0e5ad27c Leszek Koltunski
    }
682
683 5b893eee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
684
685
  public int getNodeSize()
686
    {
687
    return mNodeSize;
688
    }
689
690 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
691
692
  public RubikObjectList getObjectList()
693
    {
694
    return mList;
695
    }
696
697 10a2e360 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
698
699 f0fa83ae Leszek Koltunski
  abstract float getScreenRatio();
700 10a2e360 Leszek Koltunski
  abstract Static3D[] getCubitPositions(int size);
701 10585385 Leszek Koltunski
  abstract Static4D[] getQuats();
702 411c6285 Leszek Koltunski
  abstract int getNumFaces();
703 14bd7976 Leszek Koltunski
  abstract MeshBase createCubitMesh(int cubit);
704 7289fd6c Leszek Koltunski
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
705 12ad3fca Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
706 e844c116 Leszek Koltunski
  public abstract int getBasicAngle();
707 9621255f Leszek Koltunski
  public abstract float returnMultiplier();
708 e46e17fb Leszek Koltunski
  public abstract float returnRotationFactor(float offset);
709 20931cf6 Leszek Koltunski
  public abstract String retObjectString();
710 5cf34c5f Leszek Koltunski
  public abstract float[] getRowChances();
711 fdec60a3 Leszek Koltunski
  }