Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 1576f41c

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