Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ f6d06256

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