Project

General

Profile

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

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

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