Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikObject.java @ 940100d1

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