Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / main / TwistyObject.java @ 1d581993

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