Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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.object;
21

    
22
import android.content.SharedPreferences;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26

    
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
import org.distorted.library.mesh.MeshBase;
35
import org.distorted.library.mesh.MeshRectangles;
36
import org.distorted.library.message.EffectListener;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import static org.distorted.magic.RubikRenderer.NODE_FBO_SIZE;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
public abstract class RubikObject extends DistortedNode
47
  {
48
  static final int TEXTURE_HEIGHT = 128;
49
  final float[] LEGAL_QUATS;
50
  final Static3D[] ROTATION_AXIS;
51

    
52
  static float OBJECT_SCREEN_RATIO;
53

    
54
  private static final int POST_ROTATION_MILLISEC = 500;
55
  private final int NUM_CUBITS;
56
  private int mRotRow;
57
  private int mRotAxis;
58
  private Static3D[] mOrigPos;
59
  private Static3D mScale, mNodeScale;
60
  private Static4D mQuatAccumulated;
61
  private Cubit[] mCubits;
62
  private int mSize;
63

    
64
  float mStart, mStep;
65

    
66
  Static1D mRotationAngleStatic, mRotationAngleMiddle, mRotationAngleFinal;
67
  DistortedTexture mTexture;
68

    
69
  VertexEffectSink mSinkEffect;
70
  MatrixEffectScale mScaleEffect;
71
  MatrixEffectQuaternion mQuatCEffect;
72
  MatrixEffectQuaternion mQuatAEffect;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  RubikObject(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture nodeTexture, MeshRectangles nodeMesh, DistortedEffects nodeEffects)
77
    {
78
    super(nodeTexture,nodeEffects,nodeMesh);
79

    
80
    resizeFBO(NODE_FBO_SIZE, NODE_FBO_SIZE);
81

    
82
    mOrigPos = getCubitPositions(size);
83

    
84
    LEGAL_QUATS = getLegalQuats();
85
    NUM_CUBITS  = mOrigPos.length;
86
    ROTATION_AXIS = getRotationAxis();
87
    OBJECT_SCREEN_RATIO = getScreenRatio();
88

    
89
    mSize = size;
90
    computeStartAndStep(mOrigPos);
91

    
92
    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
    Static3D center = new Static3D(0,0,0);
102

    
103
    mSinkEffect  = getSink(mSize);
104
    mScaleEffect = new MatrixEffectScale(mScale);
105
    mQuatCEffect = new MatrixEffectQuaternion(quatCur, center);
106
    mQuatAEffect = new MatrixEffectQuaternion(quatAcc, center);
107

    
108
    MatrixEffectScale nodeScaleEffect = new MatrixEffectScale(mNodeScale);
109
    nodeEffects.apply(nodeScaleEffect);
110

    
111
    mCubits = new Cubit[NUM_CUBITS];
112
    mTexture = new DistortedTexture();
113

    
114
    int vertices = (int)(24.0f/mSize + 2.0f);
115

    
116
    for(int i=0; i<NUM_CUBITS; i++)
117
      {
118
      MeshBase cubitMesh = createCubitMesh(i,vertices);
119
      mCubits[i] = new Cubit(this,cubitMesh,mOrigPos[i]);
120
      textureCubitMesh(cubitMesh,i);
121

    
122
      attach(mCubits[i].mNode);
123
      }
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    if( 2*ROTATION_AXIS.length == numFaces )  // i.e. there are faces on both ends of the axis (cube)
136
      {
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
    else if( ROTATION_AXIS.length == numFaces )  // just a single face on the right end of an axis (pyraminx)
144
      {
145
      for(int i=0; i<numFaces; i++)
146
        {
147
        belongs = belongsToRotation(cubit, i, 0 );
148
        maps[i] = new Static4D( (belongs?i:6)*ratio, 0.0f, ratio, 1.0f);
149
        }
150
      }
151

    
152
    mesh.setTextureMap(maps);
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// 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

    
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
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  private boolean belongsToRotation( int cubit, int axis, int row)
187
    {
188
    return mCubits[cubit].mRotationRow[axis]==row;
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  private void resetRotationAngle(Dynamic1D rotationAngle)
194
    {
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  public int getSize()
264
    {
265
    return mSize;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  public void continueRotation(float angleInDegrees)
271
    {
272
    mRotationAngleStatic.set0(angleInDegrees);
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  public Static4D getRotationQuat()
278
      {
279
      return mQuatAccumulated;
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public void recomputeScaleFactor(int scrWidth, int scrHeight)
285
    {
286
    float factor = Math.min(scrWidth,scrHeight);
287
    float scaleFactor = OBJECT_SCREEN_RATIO*NODE_FBO_SIZE/mSize;
288

    
289
    mNodeScale.set(factor,factor,factor);
290
    mScale.set(scaleFactor,scaleFactor,scaleFactor);
291
    }
292

    
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294

    
295
  public void savePreferences(SharedPreferences.Editor editor)
296
    {
297
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
298
    }
299

    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

    
302
  public void restorePreferences(SharedPreferences preferences)
303
    {
304
    for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences);
305
    }
306

    
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308

    
309
  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
      if( belongsToRotation(i,mRotAxis,mRotRow) )
317
        {
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

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  public void beginNewRotation(int axis, int row )
384
    {
385
    mRotAxis = axis;
386
    mRotRow  = row;
387

    
388
    mRotationAngleStatic.set0(0.0f);
389

    
390
    for(int i=0; i<NUM_CUBITS; i++)
391
      if( belongsToRotation(i,mRotAxis,mRotRow) )
392
        {
393
        mCubits[i].beginNewRotation(axis);
394
        }
395
     }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
  public long addNewRotation( int axis, int row, int angle, long durationMillis, EffectListener listener )
400
     {
401
     long effectID=0;
402
     boolean first = true;
403

    
404
     mRotAxis = axis;
405
     mRotRow  = row;
406

    
407
     mRotationAngleStatic.set0(0.0f);
408

    
409
     for(int i=0; i<NUM_CUBITS; i++)
410
       if( belongsToRotation(i,mRotAxis,mRotRow) )
411
         {
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
       if( belongsToRotation(i,mRotAxis,mRotRow) )
433
         {
434
         if( first )
435
           {
436
           first = false;
437
           quat = mCubits[i].returnRotationQuat(mRotAxis);
438
           }
439

    
440
         mCubits[i].removeRotationNow(quat);
441
         }
442

    
443
     mRotationAngleStatic.set0(0);
444
     }
445

    
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447

    
448
  public int getNumAxis()
449
    {
450
    return ROTATION_AXIS.length;
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  abstract float getScreenRatio();
456
  abstract VertexEffectSink getSink(int size);
457
  abstract Static3D[] getCubitPositions(int size);
458
  abstract float[] getLegalQuats();
459
  abstract int getNumFaces();
460
  abstract void createFaceTexture(Canvas canvas, Paint paint, int face);
461
  abstract MeshBase createCubitMesh(int cubit, int vertices);
462
  abstract Static3D[] getRotationAxis();
463
  public abstract int getBasicAngle();
464
  }
(4-4/8)