Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 380162cb

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
///////////////////////////////////////////////////////////////////////////////////////////////////
278
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
279
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
280
// away from the face of the Pyraminx shouldn't be textured.
281
282
  private boolean isOnFace( int cubit, int axis, int row)
283
    {
284
    final float MAX_ERROR = 0.0001f;
285
    float diff = mCubits[cubit].mRotationRow[axis] - row;
286
    return diff*diff < MAX_ERROR;
287 efef689c Leszek Koltunski
    }
288
289 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
290 a31d25de Leszek Koltunski
// note the minus in front of the sin() - we rotate counterclockwise
291
// when looking towards the direction where the axis increases in values.
292 aa171dee Leszek Koltunski
293 a31d25de Leszek Koltunski
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
294 aa171dee Leszek Koltunski
    {
295 a31d25de Leszek Koltunski
    Static3D axis = ROTATION_AXIS[axisIndex];
296
297
    while( angleInDegrees<0 ) angleInDegrees += 360;
298
    angleInDegrees %= 360;
299
    
300
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
301
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
302
303
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
304
    }
305
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307
308 8bbac3c2 Leszek Koltunski
  private synchronized void setupPosition(int[][] moves)
309 a31d25de Leszek Koltunski
    {
310
    if( moves!=null )
311
      {
312
      Static4D quat;
313 818431ed Leszek Koltunski
      int index, axis, rowBitmap, angle;
314 a31d25de Leszek Koltunski
      int corr = (360/getBasicAngle());
315
316
      for(int[] move: moves)
317
        {
318
        axis     = move[0];
319
        rowBitmap= move[1];
320
        angle    = move[2]*corr;
321
        quat     = makeQuaternion(axis,angle);
322
323
        for(int j=0; j<NUM_CUBITS; j++)
324
          if( belongsToRotation(j,axis,rowBitmap) )
325
            {
326 98904e45 Leszek Koltunski
            index = mCubits[j].removeRotationNow(quat);
327
            mMesh.setEffectAssociation(j,mCubits[j].computeAssociation(),index);
328 a31d25de Leszek Koltunski
            }
329
        }
330
      }
331 aa171dee Leszek Koltunski
    }
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
    boolean belongs;
495
    final float ratio = 1.0f/(NUM_FACES+1);
496
497 380162cb Leszek Koltunski
    if( 3*ROTATION_AXIS.length == 2*NUM_FACES ) // Dino
498
      {
499
      final Static4D[] maps = new Static4D[NUM_CUBITS*NUM_CUBIT_FACES];
500
501
      final int[] map = {2,4, 4,0, 3,4, 4,1,
502
                         0,2, 0,3, 1,3, 1,2,
503
                         2,5, 5,0, 3,5, 5,1 };
504
505
      for(int i=0; i<NUM_CUBITS; i++)
506
        {
507
        maps[NUM_CUBIT_FACES*i    ] = new Static4D( map[2*i  ]*ratio, 0.0f, ratio, 1.0f);
508
        maps[NUM_CUBIT_FACES*i + 1] = new Static4D( map[2*i+1]*ratio, 0.0f, ratio, 1.0f);
509
        maps[NUM_CUBIT_FACES*i + 2] = new Static4D( NUM_FACES *ratio, 0.0f, ratio, 1.0f);
510
        maps[NUM_CUBIT_FACES*i + 3] = new Static4D( NUM_FACES *ratio, 0.0f, ratio, 1.0f);
511
        }
512
513
      mMesh.setTextureMap(maps,0);
514
      return;
515
      }
516
517
518 ad73edd5 Leszek Koltunski
    for(int cubit=0; cubit<NUM_CUBITS; cubit++)
519 1f9772f3 Leszek Koltunski
      {
520 8f53e513 Leszek Koltunski
      final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
521 ad73edd5 Leszek Koltunski
522
      if( 2*ROTATION_AXIS.length == NUM_FACES )  // i.e. there are faces on both ends of the axis (cube)
523
        {
524 8f53e513 Leszek Koltunski
        for(int i=0; i<NUM_CUBIT_FACES; i++)
525 ad73edd5 Leszek Koltunski
          {
526
          belongs = isOnFace(cubit, i/2, i%2==0 ? mSize-1:0 );
527
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
528
          }
529
        }
530
      else if( ROTATION_AXIS.length == NUM_FACES )  // just a single face on the right end of an axis (pyraminx)
531
        {
532 8f53e513 Leszek Koltunski
        for(int i=0; i<NUM_CUBIT_FACES; i++)
533 ad73edd5 Leszek Koltunski
          {
534
          belongs = isOnFace(cubit, i, 0 );
535
          maps[i] = new Static4D( (belongs?i:NUM_FACES)*ratio, 0.0f, ratio, 1.0f);
536
          }
537
        }
538
539 8f53e513 Leszek Koltunski
      mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
540 1f9772f3 Leszek Koltunski
      }
541
    }
542
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544
545
  public void setTextureMap(int cubit, int face, int newColor)
546
    {
547 470820a7 Leszek Koltunski
    final float ratio = 1.0f/(NUM_FACES+1);
548 8f53e513 Leszek Koltunski
    final Static4D[] maps = new Static4D[NUM_CUBIT_FACES];
549 1f9772f3 Leszek Koltunski
550 e03e0352 Leszek Koltunski
    maps[face] = new Static4D( newColor*ratio, 0.0f, ratio, 1.0f);
551 8f53e513 Leszek Koltunski
    mMesh.setTextureMap(maps,NUM_CUBIT_FACES*cubit);
552 1f9772f3 Leszek Koltunski
    }
553
554 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
555
556 8bbac3c2 Leszek Koltunski
  public synchronized void beginNewRotation(int axis, int row )
557 a10ada2a Leszek Koltunski
    {
558 9cd7695f Leszek Koltunski
    if( axis<0 || axis>=ROTATION_AXIS.length )
559
      {
560
      android.util.Log.e("object", "invalid rotation axis: "+axis);
561
      return;
562
      }
563
    if( row<0 || row>=mSize )
564
      {
565
      android.util.Log.e("object", "invalid rotation row: "+row);
566
      return;
567
      }
568
569 27e6c301 Leszek Koltunski
    mRotAxis     = axis;
570
    mRotRowBitmap= (1<<row);
571 a10ada2a Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
572 27e6c301 Leszek Koltunski
    mRotationAxis.set( ROTATION_AXIS[axis] );
573
    mRotationAngle.add(mRotationAngleStatic);
574 8cccfb10 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
575 27e6c301 Leszek Koltunski
    }
576 a10ada2a Leszek Koltunski
577
///////////////////////////////////////////////////////////////////////////////////////////////////
578
579 8bbac3c2 Leszek Koltunski
  public synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
580 27e6c301 Leszek Koltunski
    {
581
    mRotAxis     = axis;
582
    mRotRowBitmap= rowBitmap;
583 a10ada2a Leszek Koltunski
584 27e6c301 Leszek Koltunski
    mRotationAngleStatic.set0(0.0f);
585
    mRotationAxis.set( ROTATION_AXIS[axis] );
586
    mRotationAngle.setDuration(durationMillis);
587
    mRotationAngle.resetToBeginning();
588
    mRotationAngle.add(new Static1D(0));
589
    mRotationAngle.add(new Static1D(angle));
590 8cccfb10 Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*RubikObjectList.MAX_OBJECT_SIZE) , -1);
591 27e6c301 Leszek Koltunski
    mRotateEffect.notifyWhenFinished(listener);
592
593
    return mRotateEffect.getID();
594
    }
595 a10ada2a Leszek Koltunski
596 27e6c301 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
597 a10ada2a Leszek Koltunski
598 168b6b56 Leszek Koltunski
  public long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
599 27e6c301 Leszek Koltunski
    {
600
    float angle = getAngle();
601
    mRotationAngleStatic.set0(angle);
602
    mRotationAngleFinal.set0(nearestAngleInDegrees);
603
    mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
604
605
    mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
606
    mRotationAngle.resetToBeginning();
607
    mRotationAngle.removeAll();
608
    mRotationAngle.add(mRotationAngleStatic);
609
    mRotationAngle.add(mRotationAngleMiddle);
610
    mRotationAngle.add(mRotationAngleFinal);
611
    mRotateEffect.notifyWhenFinished(listener);
612
613
    return mRotateEffect.getID();
614
    }
615 001cc0e4 Leszek Koltunski
616 a10ada2a Leszek Koltunski
617 001cc0e4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
618
619 27e6c301 Leszek Koltunski
  private float getAngle()
620 001cc0e4 Leszek Koltunski
    {
621 27e6c301 Leszek Koltunski
    int pointNum = mRotationAngle.getNumPoints();
622 001cc0e4 Leszek Koltunski
623 27e6c301 Leszek Koltunski
    if( pointNum>=1 )
624 001cc0e4 Leszek Koltunski
      {
625 27e6c301 Leszek Koltunski
      return mRotationAngle.getPoint(pointNum-1).get0();
626
      }
627
    else
628
      {
629
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
630
      crashlytics.log("points in RotationAngle: "+pointNum);
631
      return 0;
632 001cc0e4 Leszek Koltunski
      }
633
    }
634
635 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
636
637 8bbac3c2 Leszek Koltunski
  public synchronized void removeRotationNow()
638 168b6b56 Leszek Koltunski
    {
639
    float angle = getAngle();
640
    double nearestAngleInRadians = angle*Math.PI/180;
641
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
642
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
643
    float axisX = ROTATION_AXIS[mRotAxis].get0();
644
    float axisY = ROTATION_AXIS[mRotAxis].get1();
645
    float axisZ = ROTATION_AXIS[mRotAxis].get2();
646
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
647
648
    mRotationAngle.removeAll();
649
    mRotationAngleStatic.set0(0);
650
651
    for(int i=0; i<NUM_CUBITS; i++)
652
      if( belongsToRotation(i,mRotAxis,mRotRowBitmap) )
653
        {
654
        int index = mCubits[i].removeRotationNow(quat);
655
        mMesh.setEffectAssociation(i,mCubits[i].computeAssociation(),index);
656
        }
657
    }
658 a10ada2a Leszek Koltunski
659 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
660
661 a31d25de Leszek Koltunski
  public void initializeObject(int[][] moves)
662 aa171dee Leszek Koltunski
    {
663
    solve();
664
    setupPosition(moves);
665
    }
666
667 9621255f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
668
669
  public int getCubit(float[] point3D)
670
    {
671 418aa554 Leszek Koltunski
    float dist, minDist = Float.MAX_VALUE;
672 9621255f Leszek Koltunski
    int currentBest=-1;
673
    float multiplier = returnMultiplier();
674
675
    point3D[0] *= multiplier;
676
    point3D[1] *= multiplier;
677
    point3D[2] *= multiplier;
678
679
    for(int i=0; i<NUM_CUBITS; i++)
680
      {
681
      dist = mCubits[i].getDistSquared(point3D);
682
      if( dist<minDist )
683
        {
684
        minDist = dist;
685
        currentBest = i;
686
        }
687
      }
688
689
    return currentBest;
690
    }
691
692 0e5ad27c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
693
694 168b6b56 Leszek Koltunski
  public int computeNearestAngle(float angle, float speed)
695 0e5ad27c Leszek Koltunski
    {
696
    final int NEAREST = 360/getBasicAngle();
697
698 4c864c68 Leszek Koltunski
    int tmp = (int)((angle+NEAREST/2)/NEAREST);
699
    if( angle< -(NEAREST*0.5) ) tmp-=1;
700 168b6b56 Leszek Koltunski
701 4c864c68 Leszek Koltunski
    if( tmp!=0 ) return NEAREST*tmp;
702 168b6b56 Leszek Koltunski
703 c7b00dfb Leszek Koltunski
    return speed> 1.2f ? NEAREST*(angle>0 ? 1:-1) : 0;
704 0e5ad27c Leszek Koltunski
    }
705
706 5b893eee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
707
708
  public int getNodeSize()
709
    {
710
    return mNodeSize;
711
    }
712
713 aa171dee Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
714
715
  public RubikObjectList getObjectList()
716
    {
717
    return mList;
718
    }
719
720 10a2e360 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
721
722 f0fa83ae Leszek Koltunski
  abstract float getScreenRatio();
723 10a2e360 Leszek Koltunski
  abstract Static3D[] getCubitPositions(int size);
724 10585385 Leszek Koltunski
  abstract Static4D[] getQuats();
725 411c6285 Leszek Koltunski
  abstract int getNumFaces();
726 8f53e513 Leszek Koltunski
  abstract int getNumCubitFaces();
727 14bd7976 Leszek Koltunski
  abstract MeshBase createCubitMesh(int cubit);
728 7289fd6c Leszek Koltunski
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side);
729 12ad3fca Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
730 e844c116 Leszek Koltunski
  public abstract int getBasicAngle();
731 9621255f Leszek Koltunski
  public abstract float returnMultiplier();
732 e46e17fb Leszek Koltunski
  public abstract float returnRotationFactor(float offset);
733 20931cf6 Leszek Koltunski
  public abstract String retObjectString();
734 5cf34c5f Leszek Koltunski
  public abstract float[] getRowChances();
735 fdec60a3 Leszek Koltunski
  }