Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikObject.java @ f0fa83ae

1 fdec60a3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 4f9f99a2 Leszek Koltunski
package org.distorted.object;
21 fdec60a3 Leszek Koltunski
22 27a70eae Leszek Koltunski
import android.content.SharedPreferences;
23 411c6285 Leszek Koltunski
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26 27a70eae Leszek Koltunski
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectSink;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedNode;
33
import org.distorted.library.main.DistortedTexture;
34 b32444ee Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
35 97c012ae Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
36 27a70eae Leszek Koltunski
import org.distorted.library.message.EffectListener;
37 f16ff19d Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38 27a70eae Leszek Koltunski
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41 4f9f99a2 Leszek Koltunski
42 ba740a0c Leszek Koltunski
import static org.distorted.magic.RubikRenderer.NODE_FBO_SIZE;
43 d41742f7 Leszek Koltunski
44 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 27a70eae Leszek Koltunski
public abstract class RubikObject extends DistortedNode
47 fdec60a3 Leszek Koltunski
  {
48 411c6285 Leszek Koltunski
  static final int TEXTURE_HEIGHT = 128;
49 a10ada2a Leszek Koltunski
  final float[] LEGAL_QUATS;
50 efef689c Leszek Koltunski
  final Static3D[] ROTATION_AXIS;
51 27a70eae Leszek Koltunski
52 f0fa83ae Leszek Koltunski
  static float OBJECT_SCREEN_RATIO;
53
54 f16ff19d Leszek Koltunski
  private static final int POST_ROTATION_MILLISEC = 500;
55 a10ada2a Leszek Koltunski
  private final int NUM_CUBITS;
56 3c4a326c Leszek Koltunski
  private int mRotRow;
57 efef689c Leszek Koltunski
  private int mRotAxis;
58 49f67f9b Leszek Koltunski
  private Static3D[] mOrigPos;
59 5ba13c05 Leszek Koltunski
  private Static3D mScale, mNodeScale;
60 27a70eae Leszek Koltunski
  private Static4D mQuatAccumulated;
61 b32444ee Leszek Koltunski
  private Cubit[] mCubits;
62 49f67f9b Leszek Koltunski
  private int mSize;
63 27a70eae Leszek Koltunski
64 e844c116 Leszek Koltunski
  float mStart, mStep;
65 fdec60a3 Leszek Koltunski
66 27a70eae Leszek Koltunski
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
67
  DistortedTexture mTexture;
68
69
  VertexEffectSink mSinkEffect;
70
  MatrixEffectScale mScaleEffect;
71
  MatrixEffectQuaternion mQuatCEffect;
72
  MatrixEffectQuaternion mQuatAEffect;
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75 fdec60a3 Leszek Koltunski
76 411c6285 Leszek Koltunski
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture nodeTexture, MeshRectangles nodeMesh, DistortedEffects nodeEffects)
77 fdec60a3 Leszek Koltunski
    {
78 411c6285 Leszek Koltunski
    super(nodeTexture,nodeEffects,nodeMesh);
79 fdec60a3 Leszek Koltunski
80 ba740a0c Leszek Koltunski
    resizeFBO(NODE_FBO_SIZE, NODE_FBO_SIZE);
81 d41742f7 Leszek Koltunski
82 49f67f9b Leszek Koltunski
    mOrigPos = getCubitPositions(size);
83 10a2e360 Leszek Koltunski
84 f16ff19d Leszek Koltunski
    LEGAL_QUATS = getLegalQuats();
85 49f67f9b Leszek Koltunski
    NUM_CUBITS  = mOrigPos.length;
86 efef689c Leszek Koltunski
    ROTATION_AXIS = getRotationAxis();
87 f0fa83ae Leszek Koltunski
    OBJECT_SCREEN_RATIO = getScreenRatio();
88 a10ada2a Leszek Koltunski
89 27a70eae Leszek Koltunski
    mSize = size;
90 49f67f9b Leszek Koltunski
    computeStartAndStep(mOrigPos);
91 e844c116 Leszek Koltunski
92 27a70eae Leszek Koltunski
    mRotationAngleStatic = new Static1D(0);
93
    mRotationAngleMiddle = new Static1D(0);
94
    mRotationAngleFinal  = new Static1D(0);
95
96
    mScale    = new Static3D(1,1,1);
97
    mNodeScale= new Static3D(1,1,1);
98
99
    mQuatAccumulated = quatAcc;
100
101 5ba13c05 Leszek Koltunski
    Static3D center = new Static3D(0,0,0);
102 27a70eae Leszek Koltunski
103 f0fa83ae Leszek Koltunski
    mSinkEffect  = getSink(mSize);
104 27a70eae Leszek Koltunski
    mScaleEffect = new MatrixEffectScale(mScale);
105 5ba13c05 Leszek Koltunski
    mQuatCEffect = new MatrixEffectQuaternion(quatCur, center);
106
    mQuatAEffect = new MatrixEffectQuaternion(quatAcc, center);
107 27a70eae Leszek Koltunski
108
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
109 411c6285 Leszek Koltunski
    nodeEffects.apply(nodeScaleEffect);
110 a10ada2a Leszek Koltunski
111
    mCubits = new Cubit[NUM_CUBITS];
112 5974d2ae Leszek Koltunski
    mTexture = new DistortedTexture();
113 a10ada2a Leszek Koltunski
114
    int vertices = (int)(24.0f/mSize + 2.0f);
115
116
    for(int i=0; i<NUM_CUBITS; i++)
117
      {
118 89a11f7b Leszek Koltunski
      MeshBase cubitMesh = createCubitMesh(i,vertices);
119 49f67f9b Leszek Koltunski
      mCubits[i] = new Cubit(this,cubitMesh,mOrigPos[i]);
120 efef689c Leszek Koltunski
      textureCubitMesh(cubitMesh,i);
121 a10ada2a Leszek Koltunski
122
      attach(mCubits[i].mNode);
123
      }
124 27a70eae Leszek Koltunski
    }
125
126 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128
  private void textureCubitMesh(MeshBase mesh, int cubit)
129
    {
130
    boolean belongs;
131
    final int numFaces = getNumFaces();
132
    final Static4D[] maps = new Static4D[numFaces];
133
    final float ratio = 1.0f/(numFaces+1);
134
135 97d2f701 Leszek Koltunski
    if( 2*ROTATION_AXIS.length == numFaces )  // i.e. there are faces on both ends of the axis (cube)
136 efef689c Leszek Koltunski
      {
137
      for(int i=0; i<numFaces; i++)
138
        {
139
        belongs = belongsToRotation(cubit, i/2, i%2==0 ? mSize-1:0 );
140
        maps[i] = new Static4D( (belongs?i:6)*ratio, 0.0f, ratio, 1.0f);
141
        }
142
      }
143 97d2f701 Leszek Koltunski
    else if( ROTATION_AXIS.length == numFaces )  // just a single face on the right end of an axis (pyraminx)
144 efef689c Leszek Koltunski
      {
145
      for(int i=0; i<numFaces; i++)
146
        {
147 49f67f9b Leszek Koltunski
        belongs = belongsToRotation(cubit, i, 0 );
148 efef689c Leszek Koltunski
        maps[i] = new Static4D( (belongs?i:6)*ratio, 0.0f, ratio, 1.0f);
149
        }
150
      }
151
152
    mesh.setTextureMap(maps);
153
    }
154
155 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156 97d2f701 Leszek Koltunski
// Cast centers of all Cubits on the first rotation Axis and compute the leftmost and rightmost
157
// one. From there compute the 'start' (i.e. the leftmost) and 'step' (i.e. distance between two
158
// consecutive).
159
// it is assumed that other rotation axis have the same 'start' and 'step' - this is the case with
160
// the Cube and the Pyraminx.
161
// Start and Step are then needed to compute which rotation row (with respect to a given axis) a
162
// given Cubit belongs to.
163 e844c116 Leszek Koltunski
164
  private void computeStartAndStep(Static3D[] pos)
165
    {
166
    float min = Float.MAX_VALUE;
167
    float max = Float.MIN_VALUE;
168
    float axisX = ROTATION_AXIS[0].get0();
169
    float axisY = ROTATION_AXIS[0].get1();
170
    float axisZ = ROTATION_AXIS[0].get2();
171
    float tmp;
172
173
    for(int i=0; i<NUM_CUBITS; i++)
174
      {
175
      tmp = pos[i].get0()*axisX + pos[i].get1()*axisY + pos[i].get2()*axisZ;
176
      if( tmp<min ) min=tmp;
177
      if( tmp>max ) max=tmp;
178
      }
179
180
    mStart = min;
181
    mStep  = (max-min+1.0f)/mSize;
182
    }
183
184 efef689c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
185
186
  private boolean belongsToRotation( int cubit, int axis, int row)
187
    {
188 10a2e360 Leszek Koltunski
    return mCubits[cubit].mRotationRow[axis]==row;
189 efef689c Leszek Koltunski
    }
190
191 f16ff19d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193 a10ada2a Leszek Koltunski
  private void resetRotationAngle(Dynamic1D rotationAngle)
194 f16ff19d Leszek Koltunski
    {
195
    rotationAngle.setDuration(POST_ROTATION_MILLISEC);
196
    rotationAngle.resetToBeginning();
197
    rotationAngle.removeAll();
198
    rotationAngle.add(mRotationAngleStatic);
199
    rotationAngle.add(mRotationAngleMiddle);
200
    rotationAngle.add(mRotationAngleFinal);
201
    }
202
203 49f67f9b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
205
206
  void clampPos(Static3D pos)
207
    {
208
    float currError, minError = Float.MAX_VALUE;
209
    int minErrorIndex= -1;
210
    float x = pos.get0();
211
    float y = pos.get1();
212
    float z = pos.get2();
213
    float xo,yo,zo;
214
215
    for(int i=0; i<NUM_CUBITS; i++)
216
      {
217
      xo = mOrigPos[i].get0();
218
      yo = mOrigPos[i].get1();
219
      zo = mOrigPos[i].get2();
220
221
      currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
222
223
      if( currError<minError )
224
        {
225
        minError = currError;
226
        minErrorIndex = i;
227
        }
228
      }
229
230
    pos.set( mOrigPos[minErrorIndex] );
231
    }
232
233 411c6285 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// the getFaceColors + final black in a horizontal strip.
235
236
  public void createTexture()
237
    {
238
    Bitmap bitmap;
239
240
    final int numColors = getNumFaces();
241
242
    Paint paint = new Paint();
243
    bitmap = Bitmap.createBitmap( (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
244
    Canvas canvas = new Canvas(bitmap);
245
246
    paint.setAntiAlias(true);
247
    paint.setTextAlign(Paint.Align.CENTER);
248
    paint.setStyle(Paint.Style.FILL);
249
250
    paint.setColor(0xff000000);
251
    canvas.drawRect(0, 0, (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
252
253
    for(int i=0; i<numColors; i++)
254
      {
255
      createFaceTexture(canvas,paint,i);
256
      }
257
258
    mTexture.setTexture(bitmap);
259
    }
260
261 dd73fdab Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
262
263 27a70eae Leszek Koltunski
  public int getSize()
264 fdec60a3 Leszek Koltunski
    {
265 27a70eae Leszek Koltunski
    return mSize;
266 fdec60a3 Leszek Koltunski
    }
267
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270 27a70eae Leszek Koltunski
  public void continueRotation(float angleInDegrees)
271 fdec60a3 Leszek Koltunski
    {
272 27a70eae Leszek Koltunski
    mRotationAngleStatic.set0(angleInDegrees);
273 fdec60a3 Leszek Koltunski
    }
274
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277 27a70eae Leszek Koltunski
  public Static4D getRotationQuat()
278
      {
279
      return mQuatAccumulated;
280
      }
281
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284 5ba13c05 Leszek Koltunski
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
285 fdec60a3 Leszek Koltunski
    {
286 f0fa83ae Leszek Koltunski
    float factor = Math.min(scrWidth,scrHeight);
287 ba740a0c Leszek Koltunski
    float scaleFactor = OBJECT_SCREEN_RATIO*NODE_FBO_SIZE/mSize;
288 27a70eae Leszek Koltunski
289 5ba13c05 Leszek Koltunski
    mNodeScale.set(factor,factor,factor);
290 27a70eae Leszek Koltunski
    mScale.set(scaleFactor,scaleFactor,scaleFactor);
291 fdec60a3 Leszek Koltunski
    }
292 27a70eae Leszek Koltunski
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
295 a10ada2a Leszek Koltunski
  public void savePreferences(SharedPreferences.Editor editor)
296
    {
297
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
298
    }
299 f16ff19d Leszek Koltunski
300 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
301 27a70eae Leszek Koltunski
302 a10ada2a Leszek Koltunski
  public void restorePreferences(SharedPreferences preferences)
303
    {
304
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences);
305
    }
306 27a70eae Leszek Koltunski
307 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
308 74686c71 Leszek Koltunski
309 a10ada2a Leszek Koltunski
  public long finishRotationNow(EffectListener listener)
310
    {
311
    boolean first = true;
312
    long effectID=0;
313
314
    for(int i=0; i<NUM_CUBITS; i++)
315
      {
316 efef689c Leszek Koltunski
      if( belongsToRotation(i,mRotAxis,mRotRow) )
317 a10ada2a Leszek Koltunski
        {
318
        if( first )
319
          {
320
          first=false;
321
          effectID = mCubits[i].finishRotationNow(listener);
322
          }
323
        resetRotationAngle(mCubits[i].mRotationAngle);
324
        }
325
      }
326
327
    return effectID;
328
    }
329
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
332
  public void releaseResources()
333
    {
334
    mTexture.markForDeletion();
335
336
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].releaseResources();
337
    }
338
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
341
  public void apply(Effect effect, int position)
342
    {
343
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.apply(effect, position);
344
    }
345
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347
348
  public void remove(long effectID)
349
    {
350
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.abortById(effectID);
351
    }
352 74686c71 Leszek Koltunski
353 a10ada2a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
354
355
  public void solve()
356
    {
357
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].solve();
358
    }
359
360
///////////////////////////////////////////////////////////////////////////////////////////////////
361
362
  public boolean isSolved()
363
    {
364
    Static4D q = mCubits[0].mQuatScramble;
365
366
    float x = q.get0();
367
    float y = q.get1();
368
    float z = q.get2();
369
    float w = q.get3();
370
371
    for(int i=0; i<NUM_CUBITS; i++)
372
      {
373
      q = mCubits[i].mQuatScramble;
374
375
      if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w ) return false;
376
      }
377
378
    return true;
379
    }
380
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
383 efef689c Leszek Koltunski
  public void beginNewRotation(int axis, int row )
384 a10ada2a Leszek Koltunski
    {
385 3c4a326c Leszek Koltunski
    mRotAxis = axis;
386 a10ada2a Leszek Koltunski
    mRotRow  = row;
387
388
    mRotationAngleStatic.set0(0.0f);
389
390
    for(int i=0; i<NUM_CUBITS; i++)
391 efef689c Leszek Koltunski
      if( belongsToRotation(i,mRotAxis,mRotRow) )
392 a10ada2a Leszek Koltunski
        {
393
        mCubits[i].beginNewRotation(axis);
394
        }
395
     }
396
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398
399 efef689c Leszek Koltunski
  public long addNewRotation( int axis, int row, int angle, long durationMillis, EffectListener listener )
400 a10ada2a Leszek Koltunski
     {
401
     long effectID=0;
402
     boolean first = true;
403
404 3c4a326c Leszek Koltunski
     mRotAxis = axis;
405 a10ada2a Leszek Koltunski
     mRotRow  = row;
406
407
     mRotationAngleStatic.set0(0.0f);
408
409
     for(int i=0; i<NUM_CUBITS; i++)
410 efef689c Leszek Koltunski
       if( belongsToRotation(i,mRotAxis,mRotRow) )
411 a10ada2a Leszek Koltunski
         {
412
         mCubits[i].addNewRotation(axis,durationMillis,angle);
413
414
         if( first )
415
           {
416
           first = false;
417
           effectID = mCubits[i].setUpCallback(listener);
418
           }
419
         }
420
421
     return effectID;
422
     }
423
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425
426
  public void removeRotationNow()
427
     {
428
     boolean first = true;
429
     Static4D quat = null;
430
431
     for(int i=0; i<NUM_CUBITS; i++)
432 efef689c Leszek Koltunski
       if( belongsToRotation(i,mRotAxis,mRotRow) )
433 a10ada2a Leszek Koltunski
         {
434
         if( first )
435
           {
436
           first = false;
437 3c4a326c Leszek Koltunski
           quat = mCubits[i].returnRotationQuat(mRotAxis);
438 a10ada2a Leszek Koltunski
           }
439
440
         mCubits[i].removeRotationNow(quat);
441
         }
442
443
     mRotationAngleStatic.set0(0);
444
     }
445
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
448 e844c116 Leszek Koltunski
  public int getNumAxis()
449 10a2e360 Leszek Koltunski
    {
450
    return ROTATION_AXIS.length;
451
    }
452
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454
455 f0fa83ae Leszek Koltunski
  abstract float getScreenRatio();
456
  abstract VertexEffectSink getSink(int size);
457 10a2e360 Leszek Koltunski
  abstract Static3D[] getCubitPositions(int size);
458 a10ada2a Leszek Koltunski
  abstract float[] getLegalQuats();
459 411c6285 Leszek Koltunski
  abstract int getNumFaces();
460
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face);
461 89a11f7b Leszek Koltunski
  abstract MeshBase createCubitMesh(int cubit, int vertices);
462 efef689c Leszek Koltunski
  abstract Static3D[] getRotationAxis();
463 e844c116 Leszek Koltunski
  public abstract int getBasicAngle();
464 fdec60a3 Leszek Koltunski
  }