Project

General

Profile

Download (54.8 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ c60d98c4

1 29b82486 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
package org.distorted.objectlib.main;
21
22 3a1efb32 Leszek Koltunski
import java.io.DataInputStream;
23
import java.io.IOException;
24
import java.io.InputStream;
25
import java.util.Random;
26
27 29b82486 Leszek Koltunski
import android.content.SharedPreferences;
28
import android.graphics.Bitmap;
29
import android.graphics.Canvas;
30
import android.graphics.Paint;
31
32
import org.distorted.library.effect.Effect;
33
import org.distorted.library.effect.MatrixEffectMove;
34
import org.distorted.library.effect.MatrixEffectQuaternion;
35
import org.distorted.library.effect.MatrixEffectScale;
36
import org.distorted.library.effect.VertexEffectQuaternion;
37
import org.distorted.library.effect.VertexEffectRotate;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.library.main.DistortedNode;
41
import org.distorted.library.main.DistortedTexture;
42 a706f8e0 Leszek Koltunski
import org.distorted.library.main.QuatHelper;
43 29b82486 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
44
import org.distorted.library.mesh.MeshFile;
45
import org.distorted.library.mesh.MeshJoined;
46
import org.distorted.library.message.EffectListener;
47
import org.distorted.library.type.Dynamic1D;
48
import org.distorted.library.type.Static1D;
49
import org.distorted.library.type.Static3D;
50
import org.distorted.library.type.Static4D;
51 a57e6870 Leszek Koltunski
52 198c5bf0 Leszek Koltunski
import org.distorted.objectlib.helpers.FactoryCubit;
53
import org.distorted.objectlib.helpers.FactorySticker;
54 3ee1d662 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectFaceShape;
55 d887aa16 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectLibInterface;
56 198c5bf0 Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectShape;
57
import org.distorted.objectlib.helpers.ObjectSticker;
58 802fe251 Leszek Koltunski
import org.distorted.objectlib.helpers.QuatGroupGenerator;
59 10b7e306 Leszek Koltunski
import org.distorted.objectlib.scrambling.ScrambleState;
60
import org.distorted.objectlib.scrambling.ObjectScrambler;
61 82eb152a Leszek Koltunski
import org.distorted.objectlib.json.JsonReader;
62 3a1efb32 Leszek Koltunski
import org.distorted.objectlib.touchcontrol.*;
63 29b82486 Leszek Koltunski
64 3a1efb32 Leszek Koltunski
import static org.distorted.objectlib.touchcontrol.TouchControl.*;
65 59c20632 Leszek Koltunski
66 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 7ba38dd4 Leszek Koltunski
public abstract class TwistyObject
69 29b82486 Leszek Koltunski
  {
70 c3a033e9 Leszek Koltunski
  public static final int MESH_NICE = 0;
71
  public static final int MESH_FAST = 1;
72
73 3bf19410 Leszek Koltunski
  public static final int MODE_ICON = 0;
74
  public static final int MODE_NORM = 1;
75
76 7ec32155 Leszek Koltunski
  public static final int COLOR_YELLOW   = 0xffffff00;
77
  public static final int COLOR_WHITE    = 0xffffffff;
78
  public static final int COLOR_BLUE     = 0xff0000ff;
79
  public static final int COLOR_GREEN    = 0xff00bb00;
80
  public static final int COLOR_RED      = 0xff990000;
81
  public static final int COLOR_ORANGE   = 0xffff6200;
82
  public static final int COLOR_GREY     = 0xff727c7b;
83
  public static final int COLOR_VIOLET   = 0xff7700bb;
84
  public static final int COLOR_STROKE   = 0xff000000;
85 c60d98c4 Leszek Koltunski
  public static final int COLOR_INTERNAL = 0xff000000;
86 29b82486 Leszek Koltunski
87
  public static final int TEXTURE_HEIGHT = 256;
88
  static final int NUM_STICKERS_IN_ROW = 4;
89
90
  public static final float SQ2 = (float)Math.sqrt(2);
91
  public static final float SQ3 = (float)Math.sqrt(3);
92
  public static final float SQ5 = (float)Math.sqrt(5);
93
  public static final float SQ6 = (float)Math.sqrt(6);
94
95
  private static final float MAX_SIZE_CHANGE = 1.35f;
96
  private static final float MIN_SIZE_CHANGE = 0.75f;
97
98
  private static final Static3D CENTER = new Static3D(0,0,0);
99
  private static final int POST_ROTATION_MILLISEC = 500;
100
101 7af68038 Leszek Koltunski
  protected float[][] mStickerCoords;
102 802fe251 Leszek Koltunski
  protected Static4D[] mObjectQuats;
103 d55d2c6a Leszek Koltunski
  int mNumAxis, mMaxNumLayers;
104 82eb152a Leszek Koltunski
105 a05b6e06 Leszek Koltunski
  private int[][] mStickerVariants;
106
  private float[] mStickerScales;
107
  private Cubit[] mCubits;
108
  private MeshBase[] mMeshes;
109
  private int mNumCubits, mNumQuats, mNumFaceColors, mNumTextures;
110 7af68038 Leszek Koltunski
  private int mNumCubitFaces, mNumStickerTypes;
111 82eb152a Leszek Koltunski
  private Static3D[] mAxis;
112
  private float[][] mCuts;
113
  private int[] mNumCuts;
114
  private float[][] mOrigPos;
115 5931ae4d Leszek Koltunski
  private Static4D[] mOrigQuat;
116 82eb152a Leszek Koltunski
  private Static4D mQuat;
117 a57e6870 Leszek Koltunski
  private final int[] mNumLayers;
118 59c20632 Leszek Koltunski
  private final float mSize;
119 82eb152a Leszek Koltunski
  private DistortedEffects mEffects;
120
  private VertexEffectRotate mRotateEffect;
121
  private Dynamic1D mRotationAngle;
122
  private Static3D mRotationAxis;
123
  private Static3D mObjectScale;
124
  private int[] mQuatDebug;
125
  private Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
126
  private DistortedTexture mTexture;
127
  private float mInitScreenRatio;
128
  private int mSolvedFunctionIndex;
129
  private boolean mIsBandaged;
130 29b82486 Leszek Koltunski
  private float mObjectScreenRatio;
131
  private int[][] mSolvedQuats;
132
  private int[][] mQuatMult;
133
  private int[] mTmpQuats;
134
  private int mNumTexRows, mNumTexCols;
135
  private int mRotRowBitmap;
136 59c20632 Leszek Koltunski
  private int mCurrentRotAxis;
137 29b82486 Leszek Koltunski
  private MeshBase mMesh;
138 10b7e306 Leszek Koltunski
  private ObjectScrambler mScrambler;
139 c9c71c3f Leszek Koltunski
  private TouchControl mTouchControl;
140 7ba38dd4 Leszek Koltunski
  private DistortedNode mNode;
141 d887aa16 Leszek Koltunski
  private ObjectLibInterface mInterface;
142 d5c71d02 Leszek Koltunski
  private Bitmap mBitmap;
143 d53fb890 Leszek Koltunski
  private ObjectSticker[] mStickers;
144 b968d359 Leszek Koltunski
  private ObjectShape[] mShapes;
145
  private int mNumCubitVariants;
146 9b1fe915 Leszek Koltunski
  private int[][] mCubitFaceColors;
147 59a971c1 Leszek Koltunski
  private int[][] mVariantFaceIsOuter;
148 beee90ab Leszek Koltunski
  private int[][] mBasicAngles;
149 3bf19410 Leszek Koltunski
  private int mIconMode;
150 29b82486 Leszek Koltunski
151
  //////////////////// SOLVED1 ////////////////////////
152
153
  private int[] mFaceMap;
154
  private int[][] mScramble;
155
  private int[] mColors;
156
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159 3bf19410 Leszek Koltunski
  TwistyObject(InputStream jsonStream, int meshState, int iconMode, Static4D quat, Static3D move, float scale, InputStream meshStream)
160 82eb152a Leszek Koltunski
    {
161 a2d6c41a Leszek Koltunski
    JsonReader reader = JsonReader.getInstance();
162 82eb152a Leszek Koltunski
    reader.parseJsonFile(jsonStream);
163
    setReader(reader);
164
165
    mNumLayers = reader.getNumLayers();
166
    mSize      = reader.getSize();
167 3bf19410 Leszek Koltunski
    initialize(meshState,iconMode,quat,move,scale,meshStream,true);
168 82eb152a Leszek Koltunski
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 3bf19410 Leszek Koltunski
  TwistyObject(int[] numLayers, int meshState, int iconMode, float size, Static4D quat, Static3D move, float scale, InputStream meshStream)
173 29b82486 Leszek Koltunski
    {
174
    mNumLayers = numLayers;
175 82eb152a Leszek Koltunski
    mSize      = size;
176 3bf19410 Leszek Koltunski
    initialize(meshState,iconMode,quat,move,scale,meshStream,false);
177 82eb152a Leszek Koltunski
    }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181 3bf19410 Leszek Koltunski
  private void initialize(int meshState, int iconMode, Static4D quat, Static3D move, float scale, InputStream stream, boolean fromJSON)
182 82eb152a Leszek Koltunski
    {
183 3bf19410 Leszek Koltunski
    mIconMode = iconMode;
184 82eb152a Leszek Koltunski
    mQuat = quat;
185 29b82486 Leszek Koltunski
    mAxis = getRotationAxis();
186
    mInitScreenRatio = getScreenRatio();
187
    mSolvedFunctionIndex = getSolvedFunctionIndex();
188 802fe251 Leszek Koltunski
    mBasicAngles = getBasicAngles();
189 89a00832 Leszek Koltunski
    mObjectQuats = getQuats();
190
    mNumQuats = mObjectQuats.length;
191
    mOrigPos = getCubitPositions(mNumLayers);
192 29b82486 Leszek Koltunski
193 332e1fb0 Leszek Koltunski
    int numAxis = mAxis.length;
194 d55d2c6a Leszek Koltunski
    mMaxNumLayers = -1;
195 29b82486 Leszek Koltunski
    mCuts = getCuts(mNumLayers);
196 332e1fb0 Leszek Koltunski
    mNumCuts = new int[numAxis];
197
    for(int i=0; i<numAxis; i++)
198 dfdb26a9 Leszek Koltunski
      {
199 d55d2c6a Leszek Koltunski
      if( mMaxNumLayers<mNumLayers[i] ) mMaxNumLayers = mNumLayers[i];
200 dfdb26a9 Leszek Koltunski
      mNumCuts[i] = (mCuts==null || mCuts[i]==null ? 0 : mCuts[i].length);
201
      }
202 29b82486 Leszek Koltunski
203 a05b6e06 Leszek Koltunski
    mNumCubits = mOrigPos.length;
204
    mNumFaceColors = getNumFaceColors();
205 d55d2c6a Leszek Koltunski
    mNumAxis = mAxis.length;
206 a05b6e06 Leszek Koltunski
207 29b82486 Leszek Koltunski
    int scramblingType = getScrambleType();
208
    ScrambleState[] states = getScrambleStates();
209 10b7e306 Leszek Koltunski
    mScrambler = new ObjectScrambler(scramblingType, mNumAxis,mNumLayers,states);
210 29b82486 Leszek Koltunski
211
    boolean bandaged=false;
212
213 a05b6e06 Leszek Koltunski
    for( int c=0; c<mNumCubits; c++)
214 29b82486 Leszek Koltunski
      {
215
      if( mOrigPos[c].length>3 )
216
        {
217
        bandaged=true;
218
        break;
219
        }
220
      }
221
    mIsBandaged = bandaged;
222 a05b6e06 Leszek Koltunski
    mQuatDebug = new int[mNumCubits];
223 29b82486 Leszek Koltunski
224
    mRotationAngle= new Dynamic1D();
225
    mRotationAxis = new Static3D(1,0,0);
226
    mRotateEffect = new VertexEffectRotate(mRotationAngle, mRotationAxis, CENTER);
227
228
    mRotationAngleStatic = new Static1D(0);
229
    mRotationAngleMiddle = new Static1D(0);
230
    mRotationAngleFinal  = new Static1D(0);
231
232 64c209f5 Leszek Koltunski
    mObjectScale = new Static3D(scale,scale,scale);
233
    setObjectRatioNow(scale,720);
234 e7daa161 Leszek Koltunski
235 29b82486 Leszek Koltunski
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mObjectScale);
236 57ef6378 Leszek Koltunski
    MatrixEffectQuaternion quatEffect = new MatrixEffectQuaternion(mQuat, CENTER);
237 b80e6524 Leszek Koltunski
    MatrixEffectMove moveEffect = new MatrixEffectMove(move);
238 29b82486 Leszek Koltunski
239 e16fd960 Leszek Koltunski
    boolean fromDMESH = (stream!=null && meshState==MESH_NICE);
240 4c9ca251 Leszek Koltunski
    getQuatsAndShapes(fromDMESH,fromJSON);
241 3afd2fe4 Leszek Koltunski
    createMeshAndCubits(stream,meshState,fromDMESH);
242 e16fd960 Leszek Koltunski
    setUpTextures(fromDMESH,fromJSON);
243 19595510 Leszek Koltunski
    createDataStructuresForSolved();
244 29b82486 Leszek Koltunski
245
    mEffects = new DistortedEffects();
246
247 a05b6e06 Leszek Koltunski
    for( int q=0; q<mNumQuats; q++)
248 29b82486 Leszek Koltunski
      {
249 d55d2c6a Leszek Koltunski
      VertexEffectQuaternion vq = new VertexEffectQuaternion(mObjectQuats[q],CENTER);
250 29b82486 Leszek Koltunski
      vq.setMeshAssociation(0,q);
251
      mEffects.apply(vq);
252
      }
253
254
    mEffects.apply(mRotateEffect);
255
    mEffects.apply(quatEffect);
256
    mEffects.apply(scaleEffect);
257 b80e6524 Leszek Koltunski
    mEffects.apply(moveEffect);
258 29b82486 Leszek Koltunski
259 7ba38dd4 Leszek Koltunski
    mNode = new DistortedNode(mTexture,mEffects,mMesh);
260 29b82486 Leszek Koltunski
    }
261
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
264
  private Static3D getPos(float[] origPos)
265
    {
266
    int len = origPos.length/3;
267
    float sumX = 0.0f;
268
    float sumY = 0.0f;
269
    float sumZ = 0.0f;
270
271
    for(int i=0; i<len; i++)
272
      {
273
      sumX += origPos[3*i  ];
274
      sumY += origPos[3*i+1];
275
      sumZ += origPos[3*i+2];
276
      }
277
278
    sumX /= len;
279
    sumY /= len;
280
    sumZ /= len;
281
282
    return new Static3D(sumX,sumY,sumZ);
283
    }
284
285 59a971c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
286
287
  private void createOuterFaces()
288
    {
289
    for(int v=0; v<mNumCubitVariants; v++)
290
      {
291
      int[][] indices = mShapes[v].getVertIndices();
292
      int faces = indices.length;
293
      mVariantFaceIsOuter[v] = new int[faces];
294
      }
295
296 a05b6e06 Leszek Koltunski
    for( int cubit=0; cubit<mNumCubits; cubit++)
297 59a971c1 Leszek Koltunski
      {
298
      int variant = getCubitVariant(cubit,mNumLayers);
299
      int[][] indices = mShapes[variant].getVertIndices();
300
      int numFaces = indices.length;
301
302
      for(int face=0; face<numFaces; face++)
303 d4105efe Leszek Koltunski
        if( getCubitFaceColor(cubit,face)>=0 )
304 59a971c1 Leszek Koltunski
          {
305
          mVariantFaceIsOuter[variant][face] = 1;
306
          }
307
      }
308
    }
309
310 4c9ca251 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
311
312
  private void getQuatsAndShapes(boolean fromDMESH, boolean fromJSON)
313
    {
314
    mNumCubitVariants = getNumCubitVariants(mNumLayers);
315
316
    if( !fromDMESH || !fromJSON )
317
      {
318
      FactoryCubit factory = FactoryCubit.getInstance();
319
      factory.clear();
320
321 a05b6e06 Leszek Koltunski
      mOrigQuat = new Static4D[mNumCubits];
322
      for(int i=0; i<mNumCubits; i++) mOrigQuat[i] = getCubitQuats(i,mNumLayers);
323 4c9ca251 Leszek Koltunski
324
      mShapes = new ObjectShape[mNumCubitVariants];
325
      for(int i=0; i<mNumCubitVariants; i++) mShapes[i] = getObjectShape(i);
326
      mNumCubitFaces = ObjectShape.computeNumComponents(mShapes);
327 59a971c1 Leszek Koltunski
      mVariantFaceIsOuter = new int[mNumCubitVariants][];
328 4c9ca251 Leszek Koltunski
329 59a971c1 Leszek Koltunski
      if( !fromJSON )
330 4c9ca251 Leszek Koltunski
        {
331 59a971c1 Leszek Koltunski
        mCubitFaceColors = ObjectShape.computeColors(mShapes,mOrigPos,mOrigQuat,this);
332
        createOuterFaces();
333 4c9ca251 Leszek Koltunski
        }
334 9b1fe915 Leszek Koltunski
335 59a971c1 Leszek Koltunski
      if( fromDMESH )
336 9b1fe915 Leszek Koltunski
        {
337 59a971c1 Leszek Koltunski
        for(int i=0; i<mNumCubitVariants; i++) factory.createNewFaceTransform(mShapes[i], mVariantFaceIsOuter[i]);
338 9b1fe915 Leszek Koltunski
        }
339 4c9ca251 Leszek Koltunski
      }
340
    }
341
342 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
343
344 3afd2fe4 Leszek Koltunski
  private void createMeshAndCubits(InputStream stream, int meshState, boolean fromDMESH)
345 29b82486 Leszek Koltunski
    {
346 a05b6e06 Leszek Koltunski
    mCubits = new Cubit[mNumCubits];
347 4c9ca251 Leszek Koltunski
348 e16fd960 Leszek Koltunski
    if( fromDMESH )
349 29b82486 Leszek Koltunski
      {
350 82eb152a Leszek Koltunski
      DataInputStream dos = new DataInputStream(stream);
351 29b82486 Leszek Koltunski
      mMesh = new MeshFile(dos);
352
353
      try
354
        {
355 82eb152a Leszek Koltunski
        stream.close();
356 29b82486 Leszek Koltunski
        }
357
      catch(IOException e)
358
        {
359
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
360
        }
361
362 a05b6e06 Leszek Koltunski
      for(int i=0; i<mNumCubits; i++)
363 29b82486 Leszek Koltunski
        {
364 d55d2c6a Leszek Koltunski
        mCubits[i] = new Cubit(this,mOrigPos[i], mNumAxis);
365 a05b6e06 Leszek Koltunski
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
366 29b82486 Leszek Koltunski
        }
367
      }
368
    else
369
      {
370 a05b6e06 Leszek Koltunski
      MeshBase[] cubitMesh = new MeshBase[mNumCubits];
371 5931ae4d Leszek Koltunski
372 a05b6e06 Leszek Koltunski
      for(int i=0; i<mNumCubits; i++)
373 29b82486 Leszek Koltunski
        {
374 d55d2c6a Leszek Koltunski
        mCubits[i] = new Cubit(this,mOrigPos[i], mNumAxis);
375 ac97ecc0 Leszek Koltunski
        cubitMesh[i] = createCubitMesh(i,mNumLayers,meshState,mNumCubitFaces);
376 29b82486 Leszek Koltunski
        Static3D pos = getPos(mOrigPos[i]);
377
        cubitMesh[i].apply(new MatrixEffectMove(pos),1,0);
378 a05b6e06 Leszek Koltunski
        cubitMesh[i].setEffectAssociation(0, mCubits[i].computeAssociation(), 0);
379 29b82486 Leszek Koltunski
        }
380
381
      mMesh = new MeshJoined(cubitMesh);
382
      }
383
    }
384
385
///////////////////////////////////////////////////////////////////////////////////////////////////
386
387 ac97ecc0 Leszek Koltunski
  private MeshBase createCubitMesh(int cubit, int[] numLayers, int meshState, int numComponents)
388 29b82486 Leszek Koltunski
    {
389
    int variant = getCubitVariant(cubit,numLayers);
390
391 b968d359 Leszek Koltunski
    if( mMeshes==null ) mMeshes = new MeshBase[mNumCubitVariants];
392 29b82486 Leszek Koltunski
393
    if( mMeshes[variant]==null )
394
      {
395 3ee1d662 Leszek Koltunski
      ObjectFaceShape faceShape = getObjectFaceShape(variant);
396 29b82486 Leszek Koltunski
      FactoryCubit factory = FactoryCubit.getInstance();
397 59a971c1 Leszek Koltunski
      factory.createNewFaceTransform(mShapes[variant],mVariantFaceIsOuter[variant]);
398 ac97ecc0 Leszek Koltunski
      mMeshes[variant] = factory.createRoundedSolid(mShapes[variant],faceShape,meshState, numComponents);
399 29b82486 Leszek Koltunski
      }
400
401
    MeshBase mesh = mMeshes[variant].copy(true);
402 5931ae4d Leszek Koltunski
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( mOrigQuat[cubit], CENTER );
403 29b82486 Leszek Koltunski
    mesh.apply(quat,0xffffffff,0);
404
405
    return mesh;
406
    }
407
408 7994b456 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
409
410
  private void setUpTextures(boolean fromDMESH, boolean fromJSON)
411
    {
412
    mTexture = new DistortedTexture();
413
414
    if( fromJSON )
415
      {
416
      mNumStickerTypes = getNumStickerTypes();
417
      mNumCubitFaces = getNumCubitFaces();
418
      }
419
    else
420
      {
421
      FactoryCubit factory = FactoryCubit.getInstance();
422 3d2493ea Leszek Koltunski
      mStickerCoords   = factory.getStickerCoords();
423 7994b456 Leszek Koltunski
      mStickerVariants = factory.getStickerVariants();
424 3d2493ea Leszek Koltunski
      mStickerScales   = factory.getStickerScales();
425 7994b456 Leszek Koltunski
      adjustStickerCoords();
426 325a17e0 Leszek Koltunski
      mNumStickerTypes = (mStickerCoords==null ? 0 : mStickerCoords.length);
427 7994b456 Leszek Koltunski
      }
428
429 a05b6e06 Leszek Koltunski
    mNumTextures= mNumFaceColors *mNumStickerTypes;
430 7994b456 Leszek Koltunski
    mNumTexCols = NUM_STICKERS_IN_ROW;
431 a05b6e06 Leszek Koltunski
    mNumTexRows = (mNumTextures+1)/NUM_STICKERS_IN_ROW;
432
    if( mNumTexCols*mNumTexRows < mNumTextures+1 ) mNumTexRows++;
433 7994b456 Leszek Koltunski
434
    if( !fromDMESH || shouldResetTextureMaps() ) resetAllTextureMaps();
435
    setTexture();
436
    }
437
438 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
439
440
  private int getMultQuat(int index1, int index2)
441
    {
442
    if( mQuatMult==null )
443
      {
444 a05b6e06 Leszek Koltunski
      mQuatMult = new int[mNumQuats][mNumQuats];
445 c8bc83d9 Leszek Koltunski
446 a05b6e06 Leszek Koltunski
      for(int i=0; i<mNumQuats; i++)
447
        for(int j=0; j<mNumQuats; j++) mQuatMult[i][j] = -1;
448 c8bc83d9 Leszek Koltunski
      }
449
450
    if( mQuatMult[index1][index2]==-1 )
451
      {
452
      mQuatMult[index1][index2] = mulQuat(index1,index2);
453
      }
454
455
    return mQuatMult[index1][index2];
456
    }
457
458 3bf19410 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
459
460
  public boolean isInIconMode()
461
    {
462
    return mIconMode==MODE_ICON;
463
    }
464
465 ec42a6fe Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
466
467
  public int getVariantFaceColor(int variant, int face)
468
    {
469
    return face>=mStickerVariants[variant].length ? -1 : mStickerVariants[variant][face];
470
    }
471
472 19595510 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
473
474
  public boolean shouldResetTextureMaps()
475
    {
476
    return false;
477
    }
478
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480
481
  private void createDataStructuresForSolved()
482
    {
483
    mTmpQuats = new int[mNumQuats];
484
    mSolvedQuats = getSolvedQuats();
485
    }
486
487 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
488
// This is used to build internal data structures for the generic 'isSolved()'
489
//
490
// if this is an internal cubit (all faces black): return -1
491
// if this is a face cubit (one non-black face): return the color index of the only non-black face.
492
// Color index, i.e. the index into the 'FACE_COLORS' table.
493
// else (edge or corner cubit, more than one non-black face): return -2.
494
495 19595510 Leszek Koltunski
  protected int retCubitSolvedStatus(int cubit)
496 29b82486 Leszek Koltunski
    {
497 a75ae1ee Leszek Koltunski
    int numNonBlack=0, nonBlackIndex=-1, varColor, cubColor;
498 19595510 Leszek Koltunski
    int variant = getCubitVariant(cubit,mNumLayers);
499 29b82486 Leszek Koltunski
500
    for(int face=0; face<mNumCubitFaces; face++)
501
      {
502 ec42a6fe Leszek Koltunski
      varColor = getVariantFaceColor(variant,face);
503 0ed726d2 Leszek Koltunski
      int numFaces = mCubitFaceColors[cubit].length;
504
      cubColor = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
505 29b82486 Leszek Koltunski
506 a75ae1ee Leszek Koltunski
      if( varColor>=0 && cubColor>=0 )
507 29b82486 Leszek Koltunski
        {
508
        numNonBlack++;
509 a75ae1ee Leszek Koltunski
        nonBlackIndex = cubColor;
510 29b82486 Leszek Koltunski
        }
511
      }
512
513
    if( numNonBlack==0 ) return -1;
514
    if( numNonBlack>=2 ) return -2;
515
516
    return nonBlackIndex;
517
    }
518
519 a4af26c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
520
521
  private boolean sticksOut(Static3D[] faceAxis, float[] dist, float x, float y, float z )
522
    {
523
    final float MAXERR = 0.05f;
524
    int numAxis = dist.length;
525
526
    for(int i=0; i<numAxis; i++)
527
      {
528
      Static3D ax = faceAxis[i];
529
      float len = ax.get0()*x + ax.get1()*y + ax.get2()*z;
530 dcce7b29 Leszek Koltunski
      if( len>mSize*dist[i]+MAXERR ) return true;
531 a4af26c1 Leszek Koltunski
      }
532
533
    return false;
534
    }
535
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537
538 9554f5d4 Leszek Koltunski
  private boolean doesNotStickOut(int variant, float px, float py, float pz, float[] tmp, Static4D quat)
539 a4af26c1 Leszek Koltunski
    {
540
    ObjectShape shape = getObjectShape(variant);
541
    float[][] vertices = shape.getVertices();
542
    Static3D[] axis = getFaceAxis();
543
    float[] dist3D = getDist3D(mNumLayers);
544
545
    for( float[] vertex : vertices)
546
      {
547
      float x = vertex[0];
548
      float y = vertex[1];
549
      float z = vertex[2];
550
551
      QuatHelper.rotateVectorByQuat(tmp, x, y, z, 1, quat);
552
553 9554f5d4 Leszek Koltunski
      float mx = tmp[0] + px;
554
      float my = tmp[1] + py;
555
      float mz = tmp[2] + pz;
556 a4af26c1 Leszek Koltunski
557
      if( sticksOut(axis, dist3D, mx,my,mz) ) return false;
558
      }
559
560
    return true;
561
    }
562
563 9554f5d4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
564
565
  private float computeAvg(float[] pos, int offset)
566
    {
567
    int len = pos.length/3;
568
    float ret=0.0f;
569
    for(int i=0; i<len; i++) ret += pos[3*i+offset];
570
    ret /= len;
571
572
    return ret;
573
    }
574
575 97a6aa87 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
576
577
  protected void displayCubitQuats()
578
    {
579
    StringBuilder builder = new StringBuilder();
580
    float[] tmp = new float[4];
581 a4af26c1 Leszek Koltunski
    float ERR = 0.01f;
582 97a6aa87 Leszek Koltunski
583
    for(int cubit=0; cubit<mNumCubits; cubit++)
584
      {
585 a4af26c1 Leszek Koltunski
      builder.append(cubit);
586
      builder.append(" : ");
587
588 97a6aa87 Leszek Koltunski
      int refCubit,variant = getCubitVariant(cubit,mNumLayers);
589
590
      for(refCubit=0; refCubit<mNumCubits; refCubit++)
591
        {
592
        if( getCubitVariant(refCubit,mNumLayers)==variant ) break;
593
        }
594
595
      float[] curpos = mOrigPos[cubit];
596
      float[] refpos = mOrigPos[refCubit];
597 9554f5d4 Leszek Koltunski
      float refX = computeAvg(refpos,0);
598
      float refY = computeAvg(refpos,1);
599
      float refZ = computeAvg(refpos,2);
600
      float curX = computeAvg(curpos,0);
601
      float curY = computeAvg(curpos,1);
602
      float curZ = computeAvg(curpos,2);
603 97a6aa87 Leszek Koltunski
604
      for(int quat=0; quat<mNumQuats; quat++)
605
        {
606
        QuatHelper.rotateVectorByQuat(tmp,refX,refY,refZ,0,mObjectQuats[quat]);
607
608
        float dx = tmp[0]-curX;
609
        float dy = tmp[1]-curY;
610
        float dz = tmp[2]-curZ;
611
612 a4af26c1 Leszek Koltunski
        if( dx>-ERR && dx<ERR && dy>-ERR && dy<ERR && dz>-ERR && dz<ERR )
613 97a6aa87 Leszek Koltunski
          {
614 9554f5d4 Leszek Koltunski
          if( doesNotStickOut(variant,curX,curY,curZ,tmp,mObjectQuats[quat]) )
615 a4af26c1 Leszek Koltunski
            {
616
            builder.append(quat);
617
            builder.append(',');
618
            }
619
          else
620
            {
621
            android.util.Log.e("D", "cubit: "+cubit+" quat: "+quat+" : center correct, but shape sticks out");
622
            }
623 97a6aa87 Leszek Koltunski
          }
624
        }
625
626
      builder.append('\n');
627
      }
628
629
    android.util.Log.e("D", "cubitQuats: \n"+builder.toString() );
630
    }
631
632 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
633
634 19595510 Leszek Koltunski
  protected int[] buildSolvedQuats(Static3D faceAx)
635 29b82486 Leszek Koltunski
    {
636
    final float MAXD = 0.0001f;
637
    float x = faceAx.get0();
638
    float y = faceAx.get1();
639
    float z = faceAx.get2();
640
    float a,dx,dy,dz,qx,qy,qz;
641
    Static4D quat;
642
    int place = 0;
643
644 19595510 Leszek Koltunski
    for(int q=1; q<mNumQuats; q++)
645 29b82486 Leszek Koltunski
      {
646 19595510 Leszek Koltunski
      quat = mObjectQuats[q];
647 29b82486 Leszek Koltunski
      qx = quat.get0();
648
      qy = quat.get1();
649
      qz = quat.get2();
650
651
           if( x!=0.0f ) { a = qx/x; }
652
      else if( y!=0.0f ) { a = qy/y; }
653
      else               { a = qz/z; }
654
655
      dx = a*x-qx;
656
      dy = a*y-qy;
657
      dz = a*z-qz;
658
659
      if( dx>-MAXD && dx<MAXD && dy>-MAXD && dy<MAXD && dz>-MAXD && dz<MAXD )
660
        {
661
        mTmpQuats[place++] = q;
662
        }
663
      }
664
665
    if( place!=0 )
666
      {
667
      int[] ret = new int[place];
668
      System.arraycopy(mTmpQuats,0,ret,0,place);
669
      return ret;
670
      }
671
672
    return null;
673
    }
674
675 f5426a4c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
676
677 19595510 Leszek Koltunski
  public int[][] getSolvedQuats()
678 f5426a4c Leszek Koltunski
    {
679 ed0988c0 Leszek Koltunski
    int[] groups = new int[mNumCubits];
680
    int numGroups = 1;
681
    int numFirst  = 0;
682 f5426a4c Leszek Koltunski
683 19595510 Leszek Koltunski
    for(int cubit=0; cubit<mNumCubits; cubit++)
684 f5426a4c Leszek Koltunski
      {
685 ed0988c0 Leszek Koltunski
      groups[cubit] = retCubitSolvedStatus(cubit);
686
      if( groups[cubit]>=0 ) numGroups++;
687
      else                   numFirst++;
688
      }
689
690
    int firstIndex = 1;
691
    int groupIndex = 1;
692
    int[][] solvedQuats = new int[numGroups][];
693
    solvedQuats[0] = new int[1+numFirst];
694
    solvedQuats[0][0] = numFirst;
695
    Static3D[] axis = getFaceAxis();
696
697
    for(int cubit=0; cubit<mNumCubits; cubit++)
698
      {
699
      int group = groups[cubit];
700 19595510 Leszek Koltunski
701 ed0988c0 Leszek Koltunski
      if( group<0 )
702 19595510 Leszek Koltunski
        {
703 ed0988c0 Leszek Koltunski
        solvedQuats[0][firstIndex] = cubit;
704
        firstIndex++;
705
        }
706
      else
707
        {
708
        int[] quats = buildSolvedQuats(axis[group]);
709 82904e62 Leszek Koltunski
        int len = quats==null ? 0 : quats.length;
710 ed0988c0 Leszek Koltunski
        solvedQuats[groupIndex] = new int[2+len];
711
        solvedQuats[groupIndex][0] = 1;
712
        solvedQuats[groupIndex][1] = cubit;
713
        for(int i=0; i<len; i++) solvedQuats[groupIndex][i+2] = quats[i];
714
        groupIndex++;
715 19595510 Leszek Koltunski
        }
716 f5426a4c Leszek Koltunski
      }
717 cd2e8d4c Leszek Koltunski
/*
718 ed0988c0 Leszek Koltunski
    String dbg = "SOLVED GROUPS:\n";
719
720
    for(int g=0; g<numGroups; g++)
721
      {
722
      int len = solvedQuats[g].length;
723 82904e62 Leszek Koltunski
      for(int i=0; i<len; i++) dbg += (" "+solvedQuats[g][i]);
724 ed0988c0 Leszek Koltunski
      dbg+="\n";
725
      }
726
727
    android.util.Log.e("D", dbg);
728 cd2e8d4c Leszek Koltunski
*/
729 19595510 Leszek Koltunski
    return solvedQuats;
730 f5426a4c Leszek Koltunski
    }
731
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733
734
  public int getSolvedFunctionIndex()
735
    {
736
    return 0;
737
    }
738
739 690557d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
740
// special SolvedQuats for the case where there are no corner of edge cubits.
741
// first row {0} - means there are no corners or edges.
742
// each next defines all cubits of a singe face (numCubits, firstCubit, cubit1,..,cubitN-1, quat0,..., quatM
743
744
  private boolean isSolvedCentersOnly()
745
    {
746
    int numGroups = mSolvedQuats.length;
747
748
    for(int group=1; group<numGroups; group++)
749
      {
750
      int numEntries= mSolvedQuats[group].length;
751
      int numCubits = mSolvedQuats[group][0];
752
      int firstCubit= mSolvedQuats[group][1];
753
      int firstQuat = mCubits[firstCubit].mQuatIndex;
754
755
      for(int cubit=2; cubit<=numCubits; cubit++)
756
        {
757
        int currCubit= mSolvedQuats[group][cubit];
758
        int currQuat = mCubits[currCubit].mQuatIndex;
759
        boolean isGood= (firstQuat==currQuat);
760
761
        for(int q=numCubits+1; !isGood && q<numEntries; q++)
762
          {
763
          int quat = mSolvedQuats[group][q];
764
          if( firstQuat == getMultQuat(currQuat,quat) ) isGood = true;
765
          }
766
767
        if( !isGood ) return false;
768
        }
769
      }
770
771
    return true;
772
    }
773
774 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
775
776 c8bc83d9 Leszek Koltunski
  private boolean isSolved0()
777 29b82486 Leszek Koltunski
    {
778 690557d9 Leszek Koltunski
    if( mSolvedQuats[0][0]==0 ) return isSolvedCentersOnly();
779
780 ed0988c0 Leszek Koltunski
    for( int[] solvedQuat : mSolvedQuats )
781
      {
782
      int numCubits = solvedQuat[0];
783
      int firstCubit= solvedQuat[1];
784
      int quat = mCubits[firstCubit].mQuatIndex;
785
786
      for( int cubit=2; cubit<=numCubits; cubit++ )
787
        {
788
        int c = solvedQuat[cubit];
789
        if( quat != mCubits[c].mQuatIndex ) return false;
790
        }
791
      }
792
793
    int cubit= mSolvedQuats[0][1];
794
    int quat0= mCubits[cubit].mQuatIndex;
795
    int numGroups = mSolvedQuats.length;
796 29b82486 Leszek Koltunski
797 ed0988c0 Leszek Koltunski
    for(int group=1; group<numGroups; group++)
798 29b82486 Leszek Koltunski
      {
799 ed0988c0 Leszek Koltunski
      int firstCubit= mSolvedQuats[group][1];
800
      int currQuat  = mCubits[firstCubit].mQuatIndex;
801 29b82486 Leszek Koltunski
802 ed0988c0 Leszek Koltunski
      if( quat0==currQuat ) continue;
803 29b82486 Leszek Koltunski
804 ed0988c0 Leszek Koltunski
      boolean isGood= false;
805
      int numEntries= mSolvedQuats[group].length;
806
      int numCubits = mSolvedQuats[group][0];
807 29b82486 Leszek Koltunski
808 ed0988c0 Leszek Koltunski
      for(int q=numCubits+1; q<numEntries; q++)
809 29b82486 Leszek Koltunski
        {
810 ed0988c0 Leszek Koltunski
        int quat = mSolvedQuats[group][q];
811
812
        if( quat0 == getMultQuat(currQuat,quat) )
813 29b82486 Leszek Koltunski
          {
814 ed0988c0 Leszek Koltunski
          isGood = true;
815 29b82486 Leszek Koltunski
          break;
816
          }
817
        }
818
819 ed0988c0 Leszek Koltunski
      if( !isGood ) return false;
820 29b82486 Leszek Koltunski
      }
821
822
    return true;
823
    }
824
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826
827
  private int computeScramble(int quatNum, int centerNum)
828
    {
829
    float MAXDIFF = 0.01f;
830
    float[] center= mOrigPos[centerNum];
831
    Static4D sc = new Static4D(center[0], center[1], center[2], 1.0f);
832 d55d2c6a Leszek Koltunski
    Static4D result = QuatHelper.rotateVectorByQuat(sc,mObjectQuats[quatNum]);
833 29b82486 Leszek Koltunski
834
    float x = result.get0();
835
    float y = result.get1();
836
    float z = result.get2();
837
838 a05b6e06 Leszek Koltunski
    for(int c=0; c<mNumCubits; c++)
839 29b82486 Leszek Koltunski
      {
840
      float[] cent = mOrigPos[c];
841
842
      float qx = cent[0] - x;
843
      float qy = cent[1] - y;
844
      float qz = cent[2] - z;
845
846
      if( qx>-MAXDIFF && qx<MAXDIFF &&
847
          qy>-MAXDIFF && qy<MAXDIFF &&
848
          qz>-MAXDIFF && qz<MAXDIFF  ) return c;
849
      }
850
851
    return -1;
852
    }
853
854
///////////////////////////////////////////////////////////////////////////////////////////////////
855
// Dino4 uses this. It is solved if and only if groups of cubits
856
// (0,3,7), (1,2,5), (4,8,9), (6,10,11)
857
// or
858
// (0,1,4), (2,3,6), (5,9,10), (7,8,11)
859
// are all the same color.
860
861 c8bc83d9 Leszek Koltunski
  private boolean isSolved1()
862 29b82486 Leszek Koltunski
    {
863
    if( mScramble==null )
864
      {
865 a05b6e06 Leszek Koltunski
      mScramble = new int[mNumQuats][mNumCubits];
866
      mColors   = new int[mNumCubits];
867 29b82486 Leszek Koltunski
868 a05b6e06 Leszek Koltunski
      for(int q=0; q<mNumQuats; q++)
869
        for(int c=0; c<mNumCubits; c++) mScramble[q][c] = computeScramble(q,c);
870 29b82486 Leszek Koltunski
      }
871
872
    if( mFaceMap==null )
873
      {
874
      mFaceMap = new int[] { 4, 2, 2, 4, 0, 2, 1, 4, 0, 0, 1, 1 };
875
      }
876
877 a05b6e06 Leszek Koltunski
    for(int c=0; c<mNumCubits; c++)
878 29b82486 Leszek Koltunski
      {
879 a05b6e06 Leszek Koltunski
      int index = mScramble[mCubits[c].mQuatIndex][c];
880 29b82486 Leszek Koltunski
      mColors[index] = mFaceMap[c];
881
      }
882
883
    if( mColors[0]==mColors[3] && mColors[0]==mColors[7] &&
884
        mColors[1]==mColors[2] && mColors[1]==mColors[5] &&
885
        mColors[4]==mColors[8] && mColors[4]==mColors[9]  ) return true;
886
887
    if( mColors[0]==mColors[1] && mColors[0]==mColors[4] &&
888
        mColors[2]==mColors[3] && mColors[2]==mColors[6] &&
889
        mColors[5]==mColors[9] && mColors[5]==mColors[10] ) return true;
890
891
    return false;
892
    }
893
894
///////////////////////////////////////////////////////////////////////////////////////////////////
895
896
  int computeRow(float[] pos, int axisIndex)
897
    {
898
    int ret=0;
899
    int len = pos.length / 3;
900
    Static3D axis = mAxis[axisIndex];
901
    float axisX = axis.get0();
902
    float axisY = axis.get1();
903
    float axisZ = axis.get2();
904
    float casted;
905
906
    for(int i=0; i<len; i++)
907
      {
908
      casted = pos[3*i]*axisX + pos[3*i+1]*axisY + pos[3*i+2]*axisZ;
909
      ret |= computeSingleRow(axisIndex,casted);
910
      }
911
912
    return ret;
913
    }
914
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916
917
  private int computeSingleRow(int axisIndex,float casted)
918
    {
919
    int num = mNumCuts[axisIndex];
920
921
    for(int i=0; i<num; i++)
922
      {
923
      if( casted<mCuts[axisIndex][i] ) return (1<<i);
924
      }
925
926
    return (1<<num);
927
    }
928
929
///////////////////////////////////////////////////////////////////////////////////////////////////
930
931
  private boolean wasRotateApplied()
932
    {
933
    return mEffects.exists(mRotateEffect.getID());
934
    }
935
936
///////////////////////////////////////////////////////////////////////////////////////////////////
937
938
  private boolean belongsToRotation( int cubit, int axis, int rowBitmap)
939
    {
940 a05b6e06 Leszek Koltunski
    return (mCubits[cubit].getRotRow(axis) & rowBitmap) != 0;
941 29b82486 Leszek Koltunski
    }
942
943
///////////////////////////////////////////////////////////////////////////////////////////////////
944
// note the minus in front of the sin() - we rotate counterclockwise
945
// when looking towards the direction where the axis increases in values.
946
947
  private Static4D makeQuaternion(int axisIndex, int angleInDegrees)
948
    {
949
    Static3D axis = mAxis[axisIndex];
950
951
    while( angleInDegrees<0 ) angleInDegrees += 360;
952
    angleInDegrees %= 360;
953
    
954
    float cosA = (float)Math.cos(Math.PI*angleInDegrees/360);
955
    float sinA =-(float)Math.sqrt(1-cosA*cosA);
956
957
    return new Static4D(axis.get0()*sinA, axis.get1()*sinA, axis.get2()*sinA, cosA);
958
    }
959
960
///////////////////////////////////////////////////////////////////////////////////////////////////
961
962
  private synchronized void setupPosition(int[][] moves)
963
    {
964
    if( moves!=null )
965
      {
966
      Static4D quat;
967 c9e40dfc Leszek Koltunski
      int index, axis, row, rowBitmap, angle;
968 29b82486 Leszek Koltunski
969
      for(int[] move: moves)
970
        {
971
        axis     = move[0];
972 78cb3c28 Leszek Koltunski
        rowBitmap= computeBitmapFromRow( move[1],axis );
973 c9e40dfc Leszek Koltunski
        row      = computeRowFromBitmap( move[1] );
974
        angle    = move[2]*(360/mBasicAngles[axis][row]);   // this assumes that all layers from
975
                                                            // the bitmap have the same BasicAngle.
976
                                                            // at the moment this is always true as
977
                                                            // there are no bandaged objects with
978
                                                            // different per-layer BasicAngles.
979 29b82486 Leszek Koltunski
        quat     = makeQuaternion(axis,angle);
980
981 a05b6e06 Leszek Koltunski
        for(int j=0; j<mNumCubits; j++)
982 29b82486 Leszek Koltunski
          if( belongsToRotation(j,axis,rowBitmap) )
983
            {
984 a05b6e06 Leszek Koltunski
            index = mCubits[j].removeRotationNow(quat);
985
            mMesh.setEffectAssociation(j, mCubits[j].computeAssociation(),index);
986 29b82486 Leszek Koltunski
            }
987
        }
988
      }
989
    }
990
991
///////////////////////////////////////////////////////////////////////////////////////////////////
992
993 f9a81f52 Leszek Koltunski
  public int getScrambleType()
994 29b82486 Leszek Koltunski
    {
995
    return 0;
996
    }
997
998
///////////////////////////////////////////////////////////////////////////////////////////////////
999
1000
  int computeBitmapFromRow(int rowBitmap, int axis)
1001
    {
1002
    if( mIsBandaged )
1003
      {
1004
      int bitmap, initBitmap=0;
1005
1006
      while( initBitmap!=rowBitmap )
1007
        {
1008
        initBitmap = rowBitmap;
1009
1010 a05b6e06 Leszek Koltunski
        for(int cubit=0; cubit<mNumCubits; cubit++)
1011 29b82486 Leszek Koltunski
          {
1012 a05b6e06 Leszek Koltunski
          bitmap = mCubits[cubit].getRotRow(axis);
1013 29b82486 Leszek Koltunski
          if( (rowBitmap & bitmap) != 0 ) rowBitmap |= bitmap;
1014
          }
1015
        }
1016
      }
1017
1018
    return rowBitmap;
1019
    }
1020
1021 c9e40dfc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1022
1023
  private int computeRowFromBitmap(int rowBitmap)
1024
    {
1025
    int index = 0;
1026
1027
    while(index<32)
1028
      {
1029
      if( (rowBitmap&0x1) != 0 ) return index;
1030
      rowBitmap>>=1;
1031
      index++;
1032
      }
1033
    return 0;
1034
    }
1035
1036 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1037
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
1038
// Do so only if minimal Error is appropriately low (shape-shifting puzzles - Square-1)
1039
1040
  void clampPos(float[] pos, int offset)
1041
    {
1042
    float currError, minError = Float.MAX_VALUE;
1043
    int minErrorIndex1 = -1;
1044
    int minErrorIndex2 = -1;
1045
1046
    float x = pos[offset  ];
1047
    float y = pos[offset+1];
1048
    float z = pos[offset+2];
1049
1050
    float xo,yo,zo;
1051
1052 a05b6e06 Leszek Koltunski
    for(int i=0; i<mNumCubits; i++)
1053 29b82486 Leszek Koltunski
      {
1054
      int len = mOrigPos[i].length / 3;
1055
1056
      for(int j=0; j<len; j++)
1057
        {
1058
        xo = mOrigPos[i][3*j  ];
1059
        yo = mOrigPos[i][3*j+1];
1060
        zo = mOrigPos[i][3*j+2];
1061
1062
        currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
1063
1064
        if( currError<minError )
1065
          {
1066
          minError = currError;
1067
          minErrorIndex1 = i;
1068
          minErrorIndex2 = j;
1069
          }
1070
        }
1071
      }
1072
1073
    if( minError< 0.1f ) // TODO: 0.1 ?
1074
      {
1075
      pos[offset  ] = mOrigPos[minErrorIndex1][3*minErrorIndex2  ];
1076
      pos[offset+1] = mOrigPos[minErrorIndex1][3*minErrorIndex2+1];
1077
      pos[offset+2] = mOrigPos[minErrorIndex1][3*minErrorIndex2+2];
1078
      }
1079
    }
1080
1081
///////////////////////////////////////////////////////////////////////////////////////////////////
1082
// remember about the double cover or unit quaternions!
1083
1084
  int mulQuat(int q1, int q2)
1085
    {
1086 d55d2c6a Leszek Koltunski
    Static4D result = QuatHelper.quatMultiply(mObjectQuats[q1],mObjectQuats[q2]);
1087 29b82486 Leszek Koltunski
1088
    float rX = result.get0();
1089
    float rY = result.get1();
1090
    float rZ = result.get2();
1091
    float rW = result.get3();
1092
1093
    final float MAX_ERROR = 0.1f;
1094
    float dX,dY,dZ,dW;
1095
1096 a05b6e06 Leszek Koltunski
    for(int i=0; i<mNumQuats; i++)
1097 29b82486 Leszek Koltunski
      {
1098 d55d2c6a Leszek Koltunski
      dX = mObjectQuats[i].get0() - rX;
1099
      dY = mObjectQuats[i].get1() - rY;
1100
      dZ = mObjectQuats[i].get2() - rZ;
1101
      dW = mObjectQuats[i].get3() - rW;
1102 29b82486 Leszek Koltunski
1103
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
1104
          dY<MAX_ERROR && dY>-MAX_ERROR &&
1105
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
1106
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
1107
1108 d55d2c6a Leszek Koltunski
      dX = mObjectQuats[i].get0() + rX;
1109
      dY = mObjectQuats[i].get1() + rY;
1110
      dZ = mObjectQuats[i].get2() + rZ;
1111
      dW = mObjectQuats[i].get3() + rW;
1112 29b82486 Leszek Koltunski
1113
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
1114
          dY<MAX_ERROR && dY>-MAX_ERROR &&
1115
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
1116
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
1117
      }
1118
1119
    return -1;
1120
    }
1121
1122 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1123
1124
  private float getAngle()
1125
    {
1126
    int pointNum = mRotationAngle.getNumPoints();
1127
1128
    if( pointNum>=1 )
1129
      {
1130
      return mRotationAngle.getPoint(pointNum-1).get0();
1131
      }
1132
    else
1133
      {
1134 32c1697e Leszek Koltunski
      mInterface.reportProblem("points in RotationAngle: "+pointNum, false);
1135 c8bc83d9 Leszek Koltunski
      return 0;
1136
      }
1137
    }
1138
1139 d887aa16 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1140
1141
  void setLibInterface(ObjectLibInterface inter)
1142
    {
1143
    mInterface = inter;
1144 c8bc83d9 Leszek Koltunski
    }
1145
1146 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1147 198c5bf0 Leszek Koltunski
1148 880beeea Leszek Koltunski
  void initializeObject(int[][] moves)
1149 7c111294 Leszek Koltunski
    {
1150
    solve();
1151
    setupPosition(moves);
1152
    }
1153 198c5bf0 Leszek Koltunski
1154 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1155 198c5bf0 Leszek Koltunski
1156 880beeea Leszek Koltunski
  synchronized void removeRotationNow()
1157 7c111294 Leszek Koltunski
    {
1158
    float angle = getAngle();
1159
    double nearestAngleInRadians = angle*Math.PI/180;
1160
    float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
1161
    float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
1162 59c20632 Leszek Koltunski
    float axisX = mAxis[mCurrentRotAxis].get0();
1163
    float axisY = mAxis[mCurrentRotAxis].get1();
1164
    float axisZ = mAxis[mCurrentRotAxis].get2();
1165 7c111294 Leszek Koltunski
    Static4D quat = new Static4D( axisX*sinA, axisY*sinA, axisZ*sinA, cosA);
1166 198c5bf0 Leszek Koltunski
1167 7c111294 Leszek Koltunski
    mRotationAngle.removeAll();
1168
    mRotationAngleStatic.set0(0);
1169
1170 a05b6e06 Leszek Koltunski
    for(int i=0; i<mNumCubits; i++)
1171 59c20632 Leszek Koltunski
      if( belongsToRotation(i, mCurrentRotAxis,mRotRowBitmap) )
1172 198c5bf0 Leszek Koltunski
        {
1173 a05b6e06 Leszek Koltunski
        int index = mCubits[i].removeRotationNow(quat);
1174
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(),index);
1175 198c5bf0 Leszek Koltunski
        }
1176
    }
1177
1178 c8bc83d9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1179
1180 880beeea Leszek Koltunski
  long finishRotationNow(EffectListener listener, int nearestAngleInDegrees)
1181 c8bc83d9 Leszek Koltunski
    {
1182 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
1183
      {
1184
      float angle = getAngle();
1185
      mRotationAngleStatic.set0(angle);
1186
      mRotationAngleFinal.set0(nearestAngleInDegrees);
1187
      mRotationAngleMiddle.set0( nearestAngleInDegrees + (nearestAngleInDegrees-angle)*0.2f );
1188 c8bc83d9 Leszek Koltunski
1189 7c111294 Leszek Koltunski
      mRotationAngle.setDuration(POST_ROTATION_MILLISEC);
1190
      mRotationAngle.resetToBeginning();
1191
      mRotationAngle.removeAll();
1192
      mRotationAngle.add(mRotationAngleStatic);
1193
      mRotationAngle.add(mRotationAngleMiddle);
1194
      mRotationAngle.add(mRotationAngleFinal);
1195
      mRotateEffect.notifyWhenFinished(listener);
1196 c8bc83d9 Leszek Koltunski
1197 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
1198
      }
1199 c8bc83d9 Leszek Koltunski
1200 7c111294 Leszek Koltunski
    return 0;
1201 c8bc83d9 Leszek Koltunski
    }
1202
1203
///////////////////////////////////////////////////////////////////////////////////////////////////
1204
1205 880beeea Leszek Koltunski
  synchronized long addNewRotation( int axis, int rowBitmap, int angle, long durationMillis, EffectListener listener )
1206 c8bc83d9 Leszek Koltunski
    {
1207 7c111294 Leszek Koltunski
    if( wasRotateApplied() )
1208
      {
1209 59c20632 Leszek Koltunski
      mCurrentRotAxis = axis;
1210 7c111294 Leszek Koltunski
      mRotRowBitmap= computeBitmapFromRow( rowBitmap,axis );
1211 c8bc83d9 Leszek Koltunski
1212 7c111294 Leszek Koltunski
      mRotationAngleStatic.set0(0.0f);
1213
      mRotationAxis.set( mAxis[axis] );
1214
      mRotationAngle.setDuration(durationMillis);
1215
      mRotationAngle.resetToBeginning();
1216
      mRotationAngle.add(new Static1D(0));
1217
      mRotationAngle.add(new Static1D(angle));
1218 d55d2c6a Leszek Koltunski
      mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1219 7c111294 Leszek Koltunski
      mRotateEffect.notifyWhenFinished(listener);
1220 c8bc83d9 Leszek Koltunski
1221 7c111294 Leszek Koltunski
      return mRotateEffect.getID();
1222
      }
1223
1224
    return 0;
1225 c8bc83d9 Leszek Koltunski
    }
1226
1227 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1228
1229 880beeea Leszek Koltunski
  void continueRotation(float angleInDegrees)
1230
    {
1231
    mRotationAngleStatic.set0(angleInDegrees);
1232
    }
1233
1234
///////////////////////////////////////////////////////////////////////////////////////////////////
1235
1236
  synchronized void beginNewRotation(int axis, int row )
1237
    {
1238 d55d2c6a Leszek Koltunski
    if( axis<0 || axis>=mNumAxis )
1239 880beeea Leszek Koltunski
      {
1240
      android.util.Log.e("object", "invalid rotation axis: "+axis);
1241
      return;
1242
      }
1243 a57e6870 Leszek Koltunski
    if( row<0 || row>=mNumLayers[axis] )
1244 880beeea Leszek Koltunski
      {
1245
      android.util.Log.e("object", "invalid rotation row: "+row);
1246
      return;
1247
      }
1248
1249 59c20632 Leszek Koltunski
    mCurrentRotAxis = axis;
1250 880beeea Leszek Koltunski
    mRotRowBitmap= computeBitmapFromRow( (1<<row),axis );
1251
    mRotationAngleStatic.set0(0.0f);
1252
    mRotationAxis.set( mAxis[axis] );
1253
    mRotationAngle.add(mRotationAngleStatic);
1254 d55d2c6a Leszek Koltunski
    mRotateEffect.setMeshAssociation( mRotRowBitmap<<(axis*mMaxNumLayers) , -1);
1255 880beeea Leszek Koltunski
    }
1256
1257
///////////////////////////////////////////////////////////////////////////////////////////////////
1258
1259
  void setTextureMap(int cubit, int face, int newColor)
1260 29b82486 Leszek Koltunski
    {
1261 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
1262
    final float ratioH = 1.0f/mNumTexRows;
1263
    final Static4D[] maps = new Static4D[mNumCubitFaces];
1264
    int row = (mNumTexRows-1) - newColor/mNumTexCols;
1265
    int col = newColor%mNumTexCols;
1266
1267
    maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1268
    mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1269 29b82486 Leszek Koltunski
    }
1270
1271 9b1fe915 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1272
1273 ed0988c0 Leszek Koltunski
  private int getCubitFaceColor(int cubit, int face)
1274 9b1fe915 Leszek Koltunski
    {
1275 ed0988c0 Leszek Koltunski
    int puzzleFace = getCubitFaceMap(cubit,face);
1276
    if( puzzleFace>=0 ) puzzleFace %= mNumFaceColors;
1277
    return puzzleFace;
1278 1b7ece90 Leszek Koltunski
    }
1279
1280
///////////////////////////////////////////////////////////////////////////////////////////////////
1281
1282 ed0988c0 Leszek Koltunski
  public int getCubitFaceMap(int cubit, int face)
1283 1b7ece90 Leszek Koltunski
    {
1284
    int numFaces = mCubitFaceColors[cubit].length;
1285
    int puzzleFace = face<numFaces ? mCubitFaceColors[cubit][face] : -1;
1286
    return puzzleFace<0 ? -1 : puzzleFace;
1287
    }
1288
1289 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1290
1291 880beeea Leszek Koltunski
  void resetAllTextureMaps()
1292 29b82486 Leszek Koltunski
    {
1293 7c111294 Leszek Koltunski
    final float ratioW = 1.0f/mNumTexCols;
1294
    final float ratioH = 1.0f/mNumTexRows;
1295 a75ae1ee Leszek Koltunski
    int cubColor, varColor, color, variant, row, col;
1296 29b82486 Leszek Koltunski
1297 a05b6e06 Leszek Koltunski
    for(int cubit=0; cubit<mNumCubits; cubit++)
1298 29b82486 Leszek Koltunski
      {
1299 7c111294 Leszek Koltunski
      final Static4D[] maps = new Static4D[mNumCubitFaces];
1300 a75ae1ee Leszek Koltunski
      variant = getCubitVariant(cubit,mNumLayers);
1301 29b82486 Leszek Koltunski
1302 a75ae1ee Leszek Koltunski
      for(int face=0; face<mNumCubitFaces; face++)
1303 7c111294 Leszek Koltunski
        {
1304 d4105efe Leszek Koltunski
        cubColor = getCubitFaceColor(cubit,face);
1305 ec42a6fe Leszek Koltunski
        varColor = getVariantFaceColor(variant,face);
1306 a05b6e06 Leszek Koltunski
        color    = cubColor<0 || varColor<0 ? mNumTextures : varColor*mNumFaceColors + cubColor;
1307 a75ae1ee Leszek Koltunski
        row      = (mNumTexRows-1) - color/mNumTexCols;
1308
        col      = color%mNumTexCols;
1309
1310
        maps[face] = new Static4D( col*ratioW, row*ratioH, ratioW, ratioH);
1311 7c111294 Leszek Koltunski
        }
1312 29b82486 Leszek Koltunski
1313 7c111294 Leszek Koltunski
      mMesh.setTextureMap(maps,mNumCubitFaces*cubit);
1314
      }
1315 29b82486 Leszek Koltunski
    }
1316
1317
///////////////////////////////////////////////////////////////////////////////////////////////////
1318
1319 880beeea Leszek Koltunski
  void releaseResources()
1320 29b82486 Leszek Koltunski
    {
1321 7c111294 Leszek Koltunski
    mTexture.markForDeletion();
1322
    mMesh.markForDeletion();
1323
    mEffects.markForDeletion();
1324
1325 a05b6e06 Leszek Koltunski
    for(int j=0; j<mNumCubits; j++)
1326 7c111294 Leszek Koltunski
      {
1327 a05b6e06 Leszek Koltunski
      mCubits[j].releaseResources();
1328 7c111294 Leszek Koltunski
      }
1329 29b82486 Leszek Koltunski
    }
1330
1331
///////////////////////////////////////////////////////////////////////////////////////////////////
1332
1333 880beeea Leszek Koltunski
  synchronized void restorePreferences(SharedPreferences preferences)
1334 29b82486 Leszek Koltunski
    {
1335
    boolean error = false;
1336 8b5894af Leszek Koltunski
    String key = getShortName();
1337 29b82486 Leszek Koltunski
1338 a05b6e06 Leszek Koltunski
    for(int i=0; i<mNumCubits; i++)
1339 29b82486 Leszek Koltunski
      {
1340 3ef1609d Leszek Koltunski
      mQuatDebug[i] = mCubits[i].restorePreferences(key,i,preferences);
1341 29b82486 Leszek Koltunski
1342 a05b6e06 Leszek Koltunski
      if( mQuatDebug[i]>=0 && mQuatDebug[i]<mNumQuats )
1343 29b82486 Leszek Koltunski
        {
1344 d55d2c6a Leszek Koltunski
        mCubits[i].modifyCurrentPosition(mObjectQuats[mQuatDebug[i]]);
1345 a05b6e06 Leszek Koltunski
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(),mQuatDebug[i]);
1346 29b82486 Leszek Koltunski
        }
1347
      else
1348
        {
1349
        error = true;
1350
        }
1351
      }
1352
1353
    if( error )
1354
      {
1355 a05b6e06 Leszek Koltunski
      for(int i=0; i<mNumCubits; i++)
1356 29b82486 Leszek Koltunski
        {
1357 a05b6e06 Leszek Koltunski
        mCubits[i].solve();
1358
        mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(),0);
1359 29b82486 Leszek Koltunski
        }
1360
      }
1361
    }
1362
1363
///////////////////////////////////////////////////////////////////////////////////////////////////
1364
1365 880beeea Leszek Koltunski
  void savePreferences(SharedPreferences.Editor editor)
1366 29b82486 Leszek Koltunski
    {
1367 8b5894af Leszek Koltunski
    String key = getShortName();
1368 3ef1609d Leszek Koltunski
    for(int i=0; i<mNumCubits; i++) mCubits[i].savePreferences(key,i,editor);
1369 29b82486 Leszek Koltunski
    }
1370
1371 00f4980d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1372
1373 d53fb890 Leszek Koltunski
  private float computeRadiusCorrection(float[] sticker, int curr, int len)
1374 00f4980d Leszek Koltunski
    {
1375
    final float A = 0.8f;  // 0<A<1
1376
1377
    int prev = curr>0 ? curr-1 : len-1;
1378
    int next = curr<len-1 ? curr+1 : 0;
1379
1380
    float v1x = sticker[2*prev  ]-sticker[2*curr  ];
1381
    float v1y = sticker[2*prev+1]-sticker[2*curr+1];
1382
    float v2x = sticker[2*next  ]-sticker[2*curr  ];
1383
    float v2y = sticker[2*next+1]-sticker[2*curr+1];
1384
1385
    float len1= v1x*v1x+v1y*v1y;
1386
    float len2= v2x*v2x+v2y*v2y;
1387
1388
    float cos = (v1x*v2x+v1y*v2y) / ( (float)Math.sqrt(len1*len2) );
1389
1390
    return 1-A*cos;
1391
    }
1392
1393 d53fb890 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1394
1395
  public ObjectSticker retSticker(int sticker)
1396
    {
1397
    if( mStickers==null )
1398
      {
1399
      float rad = getStickerRadius();
1400
      float str = getStickerStroke();
1401
      float[][] angles = getStickerAngles();
1402
      int numStickers = mStickerCoords.length;
1403
      mStickers = new ObjectSticker[numStickers];
1404
1405
      for(int s=0; s<numStickers; s++)
1406
        {
1407
        float scale = mStickerScales[s];
1408
        float radius = rad / scale;
1409
        float stroke = str / scale;
1410
        int len = mStickerCoords[s].length/2;
1411
        float[] radii = new float[len];
1412
        for(int r=0; r<len; r++) radii[r] = radius*computeRadiusCorrection(mStickerCoords[s],r,len);
1413
        mStickers[s] = new ObjectSticker(mStickerCoords[s],angles==null ? null : angles[s],radii,stroke);
1414
        }
1415
      }
1416
1417
    return mStickers[sticker];
1418
    }
1419
1420 3d766df3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1421 e3937019 Leszek Koltunski
// some objects (currently Kilominx,Ivy,Rex) might want to change the stickers.
1422 3d766df3 Leszek Koltunski
1423
  public void adjustStickerCoords()
1424
    {
1425
1426
    }
1427
1428 802fe251 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1429
1430
  public Static4D[] getQuats()
1431
    {
1432
    if( mObjectQuats==null )
1433
      {
1434
      mObjectQuats = QuatGroupGenerator.computeGroup(mAxis,mBasicAngles);
1435
      }
1436
1437
    return mObjectQuats;
1438
    }
1439
1440 253e440f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1441
1442
  public int getInternalColor()
1443
    {
1444
    return COLOR_INTERNAL;
1445
    }
1446
1447 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1448 7c111294 Leszek Koltunski
// the getFaceColors + final black in a grid (so that we do not exceed the maximum texture size)
1449 29b82486 Leszek Koltunski
1450 d5c71d02 Leszek Koltunski
  private void createTexture()
1451 29b82486 Leszek Koltunski
    {
1452 7c111294 Leszek Koltunski
    Paint paint = new Paint();
1453 3afd2fe4 Leszek Koltunski
    mBitmap = Bitmap.createBitmap( mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, Bitmap.Config.ARGB_4444);
1454 d5c71d02 Leszek Koltunski
    Canvas canvas = new Canvas(mBitmap);
1455 29b82486 Leszek Koltunski
1456 7c111294 Leszek Koltunski
    paint.setAntiAlias(true);
1457
    paint.setTextAlign(Paint.Align.CENTER);
1458
    paint.setStyle(Paint.Style.FILL);
1459
1460 253e440f Leszek Koltunski
    paint.setColor(getInternalColor());
1461 7c111294 Leszek Koltunski
    canvas.drawRect(0, 0, mNumTexCols*TEXTURE_HEIGHT, mNumTexRows*TEXTURE_HEIGHT, paint);
1462
1463 43a4ccff Leszek Koltunski
    int texture = 0;
1464 7c111294 Leszek Koltunski
    FactorySticker factory = FactorySticker.getInstance();
1465
1466
    for(int row=0; row<mNumTexRows; row++)
1467
      for(int col=0; col<mNumTexCols; col++)
1468 29b82486 Leszek Koltunski
        {
1469 a05b6e06 Leszek Koltunski
        if( texture>=mNumTextures ) break;
1470
        ObjectSticker sticker = retSticker(texture/mNumFaceColors);
1471
        int color = getColor(texture% mNumFaceColors);
1472 a0ccffb4 Leszek Koltunski
        factory.drawRoundedPolygon(canvas, paint, col*TEXTURE_HEIGHT, (mNumTexRows-row)*TEXTURE_HEIGHT, color, sticker);
1473 43a4ccff Leszek Koltunski
        texture++;
1474 29b82486 Leszek Koltunski
        }
1475 d5c71d02 Leszek Koltunski
    }
1476
1477
///////////////////////////////////////////////////////////////////////////////////////////////////
1478
1479
  void setTexture()
1480
    {
1481
    if( mBitmap==null ) createTexture();
1482 29b82486 Leszek Koltunski
1483 a0ccffb4 Leszek Koltunski
    if( !mTexture.setTextureAlreadyInverted(mBitmap) )
1484 7c111294 Leszek Koltunski
      {
1485
      int max = DistortedLibrary.getMaxTextureSize();
1486 32c1697e Leszek Koltunski
      mInterface.reportProblem("failed to set texture of size "+mBitmap.getWidth()+"x"+mBitmap.getHeight()+" max is "+max, true);
1487 29b82486 Leszek Koltunski
      }
1488
    }
1489
1490 ee6bb8d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1491
1492 45e0065d Leszek Koltunski
  void setObjectRatioNow(float sc, int nodeSize)
1493 ee6bb8d7 Leszek Koltunski
    {
1494 64c209f5 Leszek Koltunski
    mObjectScreenRatio = sc;
1495 45e0065d Leszek Koltunski
    float scale = mObjectScreenRatio*mInitScreenRatio*nodeSize/mSize;
1496 ee6bb8d7 Leszek Koltunski
    mObjectScale.set(scale,scale,scale);
1497 23afe4c4 Leszek Koltunski
1498 11fa413d Leszek Koltunski
    if( mTouchControl ==null ) mTouchControl = getTouchControl();
1499 c9c71c3f Leszek Koltunski
    mTouchControl.setObjectRatio(mObjectScreenRatio*mInitScreenRatio);
1500 ee6bb8d7 Leszek Koltunski
    }
1501
1502 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1503
1504 45e0065d Leszek Koltunski
  void setObjectRatio(float sizeChange, int nodeSize)
1505 29b82486 Leszek Koltunski
    {
1506 7c111294 Leszek Koltunski
    mObjectScreenRatio *= (1.0f+sizeChange)/2;
1507 29b82486 Leszek Koltunski
1508 7c111294 Leszek Koltunski
    if( mObjectScreenRatio>MAX_SIZE_CHANGE) mObjectScreenRatio = MAX_SIZE_CHANGE;
1509
    if( mObjectScreenRatio<MIN_SIZE_CHANGE) mObjectScreenRatio = MIN_SIZE_CHANGE;
1510
1511 45e0065d Leszek Koltunski
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1512
    }
1513
1514
///////////////////////////////////////////////////////////////////////////////////////////////////
1515
1516
  void setNodeSize(int nodeSize)
1517
    {
1518
    setObjectRatioNow(mObjectScreenRatio, nodeSize);
1519 29b82486 Leszek Koltunski
    }
1520
1521 02d80fe6 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1522
1523
  public float getRatio()
1524
    {
1525
    return mObjectScreenRatio;
1526
    }
1527
1528 11fa413d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1529
1530
  public float getObjectRatio()
1531
    {
1532
    return mObjectScreenRatio*mInitScreenRatio;
1533
    }
1534
1535 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1536 29b82486 Leszek Koltunski
1537 880beeea Leszek Koltunski
  boolean isSolved()
1538 7c111294 Leszek Koltunski
    {
1539 880beeea Leszek Koltunski
    if( mSolvedFunctionIndex==0 ) return isSolved0();
1540
    if( mSolvedFunctionIndex==1 ) return isSolved1();
1541 7c111294 Leszek Koltunski
1542 880beeea Leszek Koltunski
    return false;
1543 29b82486 Leszek Koltunski
    }
1544
1545 7c111294 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1546 29b82486 Leszek Koltunski
1547 beee90ab Leszek Koltunski
  int computeNearestAngle(int basicAngle, float angle, float speed)
1548 7c111294 Leszek Koltunski
    {
1549 880beeea Leszek Koltunski
    int nearestAngle = 360/basicAngle;
1550
    int tmp = (int)((angle+nearestAngle/2)/nearestAngle);
1551
    if( angle< -(nearestAngle*0.5) ) tmp-=1;
1552
1553
    if( tmp!=0 ) return nearestAngle*tmp;
1554
1555
    return speed> 1.2f ? nearestAngle*(angle>0 ? 1:-1) : 0;
1556 7c111294 Leszek Koltunski
    }
1557 29b82486 Leszek Koltunski
1558 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1559
// INTERNAL API - those are called from 'effects' package
1560 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1561
1562 880beeea Leszek Koltunski
  public void randomizeNewScramble(int[][] scramble, Random rnd, int curr, int total)
1563 29b82486 Leszek Koltunski
    {
1564 880beeea Leszek Koltunski
    mScrambler.randomizeNewScramble(scramble,rnd,curr,total);
1565 7c111294 Leszek Koltunski
    }
1566 29b82486 Leszek Koltunski
1567
///////////////////////////////////////////////////////////////////////////////////////////////////
1568
1569 880beeea Leszek Koltunski
  public Static4D getRotationQuat()
1570 59c20632 Leszek Koltunski
    {
1571
    return mQuat;
1572
    }
1573
1574
///////////////////////////////////////////////////////////////////////////////////////////////////
1575
1576
  public float getSize()
1577
    {
1578
    return mSize;
1579
    }
1580 7c111294 Leszek Koltunski
1581 2df35810 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1582
1583
  public void apply(Effect effect, int position)
1584
    {
1585
    mEffects.apply(effect, position);
1586
    }
1587
1588
///////////////////////////////////////////////////////////////////////////////////////////////////
1589
1590
  public void remove(long effectID)
1591
    {
1592
    mEffects.abortById(effectID);
1593
    }
1594
1595 3e9df6aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1596
1597 758b028d Leszek Koltunski
  public MeshBase getObjectMesh()
1598 3e9df6aa Leszek Koltunski
    {
1599
    return mMesh;
1600
    }
1601
1602 758b028d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1603
1604
  public DistortedEffects getObjectEffects()
1605
    {
1606
    return mEffects;
1607
    }
1608
1609 880beeea Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1610
// PUBLIC API
1611 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1612
1613 880beeea Leszek Koltunski
  public int getCubitFaceColorIndex(int cubit, int face)
1614 29b82486 Leszek Koltunski
    {
1615 a05b6e06 Leszek Koltunski
    Static4D texMap = mMesh.getTextureMap(mNumFaceColors *cubit + face);
1616 29b82486 Leszek Koltunski
1617 880beeea Leszek Koltunski
    int x = (int)(texMap.get0()/texMap.get2());
1618
    int y = (int)(texMap.get1()/texMap.get3());
1619 29b82486 Leszek Koltunski
1620 880beeea Leszek Koltunski
    return (mNumTexRows-1-y)*NUM_STICKERS_IN_ROW + x;
1621 29b82486 Leszek Koltunski
    }
1622
1623
///////////////////////////////////////////////////////////////////////////////////////////////////
1624
1625 a57e6870 Leszek Koltunski
  public int[] getNumLayers()
1626 29b82486 Leszek Koltunski
    {
1627 880beeea Leszek Koltunski
    return mNumLayers;
1628
    }
1629 29b82486 Leszek Koltunski
1630
///////////////////////////////////////////////////////////////////////////////////////////////////
1631
1632 880beeea Leszek Koltunski
  public synchronized void solve()
1633 29b82486 Leszek Koltunski
    {
1634 a05b6e06 Leszek Koltunski
    for(int i=0; i<mNumCubits; i++)
1635 880beeea Leszek Koltunski
      {
1636 a05b6e06 Leszek Koltunski
      mCubits[i].solve();
1637
      mMesh.setEffectAssociation(i, mCubits[i].computeAssociation(), 0);
1638 880beeea Leszek Koltunski
      }
1639 29b82486 Leszek Koltunski
    }
1640
1641 57ef6378 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1642
1643
  public int getCubitQuatIndex(int cubit)
1644
    {
1645 05b0a7dd Leszek Koltunski
    return (cubit>=0 && cubit<mNumCubits) ? mCubits[cubit].mQuatIndex : 0;
1646 57ef6378 Leszek Koltunski
    }
1647
1648 92a6fc8b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1649
1650
  public int getCubitRotRow(int cubit, int axis)
1651
    {
1652 a05b6e06 Leszek Koltunski
    return mCubits[cubit].getRotRow(axis);
1653 92a6fc8b Leszek Koltunski
    }
1654
1655 d5c71d02 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1656
1657
  public Bitmap getStickerBitmap()
1658
    {
1659
    return mBitmap;
1660
    }
1661
1662 7ba38dd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1663
1664
  public DistortedNode getNode()
1665
    {
1666
    return mNode;
1667
    }
1668
1669 7af68038 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1670
1671
  public int getNumStickerTypes()
1672
    {
1673
    return mNumStickerTypes;
1674
    }
1675
1676 d66e98d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1677
// this is here only so it can be overridden in TwistyJSON soo that we can get this from JSON.
1678
1679
  public int getNumCubitFaces()
1680
    {
1681
    return 0;
1682
    }
1683
1684 59c20632 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1685
1686 11fa413d Leszek Koltunski
  public TouchControl getTouchControl()
1687 59c20632 Leszek Koltunski
    {
1688 57ef6378 Leszek Koltunski
    if( mTouchControl==null )
1689 59c20632 Leszek Koltunski
      {
1690 11fa413d Leszek Koltunski
      switch(getTouchControlType())
1691 59c20632 Leszek Koltunski
        {
1692 cd2e8d4c Leszek Koltunski
        case TC_TETRAHEDRON      : mTouchControl = new TouchControlTetrahedron(this);
1693
                                   break;
1694
        case TC_HEXAHEDRON       : mTouchControl = new TouchControlHexahedron(this);
1695
                                   break;
1696
        case TC_OCTAHEDRON       : mTouchControl = new TouchControlOctahedron(this);
1697
                                   break;
1698
        case TC_DODECAHEDRON     : mTouchControl = new TouchControlDodecahedron(this);
1699
                                   break;
1700
        case TC_CUBOID           : int[] numLayers = getNumLayers();
1701
                                   mTouchControl = new TouchControlCuboids(this,getDist3D(numLayers));
1702
                                   break;
1703
        case TC_CHANGING_MIRROR  : mTouchControl = new TouchControlMirror(this);
1704
                                   break;
1705
        case TC_CHANGING_SQUARE  : mTouchControl = new TouchControlSquare(this);
1706
                                   break;
1707
        case TC_CHANGING_SHAPEMOD: mTouchControl = new TouchControlShapemod(this);
1708
                                   break;
1709 59c20632 Leszek Koltunski
        }
1710
      }
1711 c9c71c3f Leszek Koltunski
    return mTouchControl;
1712 59c20632 Leszek Koltunski
    }
1713
1714 29b82486 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1715
1716 82eb152a Leszek Koltunski
  protected void setReader(JsonReader reader)
1717
    {
1718
    // empty
1719
    }
1720
1721
///////////////////////////////////////////////////////////////////////////////////////////////////
1722 59c20632 Leszek Koltunski
  // for JSON only
1723 11fa413d Leszek Koltunski
  public abstract int getTouchControlType();
1724
  public abstract int getTouchControlSplit();
1725 59c20632 Leszek Koltunski
  public abstract boolean[][] getLayerRotatable(int[] numLayers);
1726
  public abstract int[][][] getEnabled();
1727
  public abstract float[] getDist3D(int[] numLayers);
1728 4c9ca251 Leszek Koltunski
  public abstract Static3D[] getFaceAxis();
1729 f9a81f52 Leszek Koltunski
  public abstract ScrambleState[] getScrambleStates();
1730 7bbfc84f Leszek Koltunski
  public abstract float[][] getCuts(int[] numLayers);
1731 d53fb890 Leszek Koltunski
  public abstract float getStickerRadius();
1732
  public abstract float getStickerStroke();
1733
  public abstract float[][] getStickerAngles();
1734 e30c522a Leszek Koltunski
  public abstract int getCubitVariant(int cubit, int[] numLayers);
1735
  public abstract ObjectShape getObjectShape(int variant);
1736 3ee1d662 Leszek Koltunski
  public abstract ObjectFaceShape getObjectFaceShape(int variant);
1737 e30c522a Leszek Koltunski
  public abstract int getNumCubitVariants(int[] numLayers);
1738 7b832206 Leszek Koltunski
  public abstract float[][] getCubitPositions(int[] numLayers);
1739 d0e6cf7f Leszek Koltunski
  public abstract Static4D getCubitQuats(int cubit, int[] numLayers);
1740 a75ae1ee Leszek Koltunski
  public abstract int getNumFaceColors();
1741 82eb152a Leszek Koltunski
  public abstract float getScreenRatio();
1742
  public abstract int getColor(int face);
1743 5f54927b Leszek Koltunski
  public abstract String getShortName();
1744
  public abstract long getSignature();
1745 7b832206 Leszek Koltunski
1746 a75ae1ee Leszek Koltunski
  // not only for JSON
1747 29b82486 Leszek Koltunski
  public abstract Static3D[] getRotationAxis();
1748 beee90ab Leszek Koltunski
  public abstract int[][] getBasicAngles();
1749 2289cab1 Leszek Koltunski
  public abstract int getNumFaces();
1750 e26eb4e7 Leszek Koltunski
  public abstract String getObjectName();
1751
  public abstract String getInventor();
1752
  public abstract int getYearOfInvention();
1753
  public abstract int getComplexity();
1754 7ba38dd4 Leszek Koltunski
  public abstract int getFOV();
1755 052e0362 Leszek Koltunski
  public abstract String[][] getTutorials();
1756 29b82486 Leszek Koltunski
  }