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 = isOnFace(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 = isOnFace(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 ((int)(mCubits[cubit].mRotationRow[axis]+0.5f))==row;
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
// we cannot use belongsToRotation for deciding if to texture a face. Counterexample: the 'rotated'
|
193
|
// tetrahedrons of Pyraminx nearby the edge: they belong to rotation but their face which is rotated
|
194
|
// away from the face of the Pyraminx shouldn't be textured.
|
195
|
|
196
|
private boolean isOnFace( int cubit, int axis, int row)
|
197
|
{
|
198
|
final float MAX_ERROR = 0.0001f;
|
199
|
float diff = mCubits[cubit].mRotationRow[axis] - row;
|
200
|
return diff*diff < MAX_ERROR;
|
201
|
}
|
202
|
|
203
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
204
|
|
205
|
private void resetRotationAngle(Dynamic1D rotationAngle)
|
206
|
{
|
207
|
rotationAngle.setDuration(POST_ROTATION_MILLISEC);
|
208
|
rotationAngle.resetToBeginning();
|
209
|
rotationAngle.removeAll();
|
210
|
rotationAngle.add(mRotationAngleStatic);
|
211
|
rotationAngle.add(mRotationAngleMiddle);
|
212
|
rotationAngle.add(mRotationAngleFinal);
|
213
|
}
|
214
|
|
215
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216
|
// Clamp all rotated positions to one of those original ones to avoid accumulating errors.
|
217
|
|
218
|
void clampPos(Static3D pos)
|
219
|
{
|
220
|
float currError, minError = Float.MAX_VALUE;
|
221
|
int minErrorIndex= -1;
|
222
|
float x = pos.get0();
|
223
|
float y = pos.get1();
|
224
|
float z = pos.get2();
|
225
|
float xo,yo,zo;
|
226
|
|
227
|
for(int i=0; i<NUM_CUBITS; i++)
|
228
|
{
|
229
|
xo = mOrigPos[i].get0();
|
230
|
yo = mOrigPos[i].get1();
|
231
|
zo = mOrigPos[i].get2();
|
232
|
|
233
|
currError = (xo-x)*(xo-x) + (yo-y)*(yo-y) + (zo-z)*(zo-z);
|
234
|
|
235
|
if( currError<minError )
|
236
|
{
|
237
|
minError = currError;
|
238
|
minErrorIndex = i;
|
239
|
}
|
240
|
}
|
241
|
|
242
|
pos.set( mOrigPos[minErrorIndex] );
|
243
|
}
|
244
|
|
245
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
246
|
// the getFaceColors + final black in a horizontal strip.
|
247
|
|
248
|
public void createTexture()
|
249
|
{
|
250
|
Bitmap bitmap;
|
251
|
|
252
|
final int numColors = getNumFaces();
|
253
|
|
254
|
Paint paint = new Paint();
|
255
|
bitmap = Bitmap.createBitmap( (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, Bitmap.Config.ARGB_8888);
|
256
|
Canvas canvas = new Canvas(bitmap);
|
257
|
|
258
|
paint.setAntiAlias(true);
|
259
|
paint.setTextAlign(Paint.Align.CENTER);
|
260
|
paint.setStyle(Paint.Style.FILL);
|
261
|
|
262
|
paint.setColor(0xff000000);
|
263
|
canvas.drawRect(0, 0, (numColors+1)*TEXTURE_HEIGHT, TEXTURE_HEIGHT, paint);
|
264
|
|
265
|
for(int i=0; i<numColors; i++)
|
266
|
{
|
267
|
createFaceTexture(canvas,paint,i);
|
268
|
}
|
269
|
|
270
|
mTexture.setTexture(bitmap);
|
271
|
}
|
272
|
|
273
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274
|
|
275
|
public int getSize()
|
276
|
{
|
277
|
return mSize;
|
278
|
}
|
279
|
|
280
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
281
|
|
282
|
public void continueRotation(float angleInDegrees)
|
283
|
{
|
284
|
mRotationAngleStatic.set0(angleInDegrees);
|
285
|
}
|
286
|
|
287
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
288
|
|
289
|
public Static4D getRotationQuat()
|
290
|
{
|
291
|
return mQuatAccumulated;
|
292
|
}
|
293
|
|
294
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
295
|
|
296
|
public void recomputeScaleFactor(int scrWidth, int scrHeight)
|
297
|
{
|
298
|
float factor = Math.min(scrWidth,scrHeight);
|
299
|
float scaleFactor = OBJECT_SCREEN_RATIO*NODE_FBO_SIZE/mSize;
|
300
|
|
301
|
mNodeScale.set(factor,factor,factor);
|
302
|
mScale.set(scaleFactor,scaleFactor,scaleFactor);
|
303
|
}
|
304
|
|
305
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306
|
|
307
|
public void savePreferences(SharedPreferences.Editor editor)
|
308
|
{
|
309
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].savePreferences(editor);
|
310
|
}
|
311
|
|
312
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
313
|
|
314
|
public void restorePreferences(SharedPreferences preferences)
|
315
|
{
|
316
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].restorePreferences(preferences);
|
317
|
}
|
318
|
|
319
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
320
|
|
321
|
public long finishRotationNow(EffectListener listener)
|
322
|
{
|
323
|
boolean first = true;
|
324
|
long effectID=0;
|
325
|
|
326
|
for(int i=0; i<NUM_CUBITS; i++)
|
327
|
{
|
328
|
if( belongsToRotation(i,mRotAxis,mRotRow) )
|
329
|
{
|
330
|
if( first )
|
331
|
{
|
332
|
first=false;
|
333
|
effectID = mCubits[i].finishRotationNow(listener);
|
334
|
}
|
335
|
resetRotationAngle(mCubits[i].mRotationAngle);
|
336
|
}
|
337
|
}
|
338
|
|
339
|
return effectID;
|
340
|
}
|
341
|
|
342
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
343
|
|
344
|
public void releaseResources()
|
345
|
{
|
346
|
mTexture.markForDeletion();
|
347
|
|
348
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].releaseResources();
|
349
|
}
|
350
|
|
351
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
352
|
|
353
|
public void apply(Effect effect, int position)
|
354
|
{
|
355
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.apply(effect, position);
|
356
|
}
|
357
|
|
358
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
359
|
|
360
|
public void remove(long effectID)
|
361
|
{
|
362
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].mEffect.abortById(effectID);
|
363
|
}
|
364
|
|
365
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
366
|
|
367
|
public void solve()
|
368
|
{
|
369
|
for(int i=0; i<NUM_CUBITS; i++) mCubits[i].solve();
|
370
|
}
|
371
|
|
372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373
|
|
374
|
public boolean isSolved()
|
375
|
{
|
376
|
Static4D q = mCubits[0].mQuatScramble;
|
377
|
|
378
|
float x = q.get0();
|
379
|
float y = q.get1();
|
380
|
float z = q.get2();
|
381
|
float w = q.get3();
|
382
|
|
383
|
for(int i=0; i<NUM_CUBITS; i++)
|
384
|
{
|
385
|
q = mCubits[i].mQuatScramble;
|
386
|
|
387
|
if( q.get0()!=x || q.get1()!=y || q.get2()!=z || q.get3()!=w ) return false;
|
388
|
}
|
389
|
|
390
|
return true;
|
391
|
}
|
392
|
|
393
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
394
|
|
395
|
public void beginNewRotation(int axis, int row )
|
396
|
{
|
397
|
if( axis<0 || axis>=ROTATION_AXIS.length )
|
398
|
{
|
399
|
android.util.Log.e("object", "invalid rotation axis: "+axis);
|
400
|
return;
|
401
|
}
|
402
|
if( row<0 || row>=mSize )
|
403
|
{
|
404
|
android.util.Log.e("object", "invalid rotation row: "+row);
|
405
|
return;
|
406
|
}
|
407
|
|
408
|
mRotAxis = axis;
|
409
|
mRotRow = row;
|
410
|
|
411
|
mRotationAngleStatic.set0(0.0f);
|
412
|
|
413
|
for(int i=0; i<NUM_CUBITS; i++)
|
414
|
if( belongsToRotation(i,mRotAxis,mRotRow) )
|
415
|
{
|
416
|
mCubits[i].beginNewRotation(axis);
|
417
|
}
|
418
|
}
|
419
|
|
420
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
421
|
|
422
|
public long addNewRotation( int axis, int row, int angle, long durationMillis, EffectListener listener )
|
423
|
{
|
424
|
long effectID=0;
|
425
|
boolean first = true;
|
426
|
|
427
|
mRotAxis = axis;
|
428
|
mRotRow = row;
|
429
|
|
430
|
mRotationAngleStatic.set0(0.0f);
|
431
|
|
432
|
for(int i=0; i<NUM_CUBITS; i++)
|
433
|
if( belongsToRotation(i,mRotAxis,mRotRow) )
|
434
|
{
|
435
|
mCubits[i].addNewRotation(axis,durationMillis,angle);
|
436
|
|
437
|
if( first )
|
438
|
{
|
439
|
first = false;
|
440
|
effectID = mCubits[i].setUpCallback(listener);
|
441
|
}
|
442
|
}
|
443
|
|
444
|
return effectID;
|
445
|
}
|
446
|
|
447
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
448
|
|
449
|
public void removeRotationNow()
|
450
|
{
|
451
|
boolean first = true;
|
452
|
Static4D quat = null;
|
453
|
|
454
|
for(int i=0; i<NUM_CUBITS; i++)
|
455
|
if( belongsToRotation(i,mRotAxis,mRotRow) )
|
456
|
{
|
457
|
if( first )
|
458
|
{
|
459
|
first = false;
|
460
|
quat = mCubits[i].returnRotationQuat(mRotAxis);
|
461
|
}
|
462
|
|
463
|
mCubits[i].removeRotationNow(quat);
|
464
|
}
|
465
|
|
466
|
mRotationAngleStatic.set0(0);
|
467
|
}
|
468
|
|
469
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
470
|
|
471
|
public int getNumAxis()
|
472
|
{
|
473
|
return ROTATION_AXIS.length;
|
474
|
}
|
475
|
|
476
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
477
|
|
478
|
abstract float getScreenRatio();
|
479
|
abstract VertexEffectSink getSink(int size);
|
480
|
abstract Static3D[] getCubitPositions(int size);
|
481
|
abstract float[] getLegalQuats();
|
482
|
abstract int getNumFaces();
|
483
|
abstract void createFaceTexture(Canvas canvas, Paint paint, int face);
|
484
|
abstract MeshBase createCubitMesh(int cubit, int vertices);
|
485
|
abstract Static3D[] getRotationAxis();
|
486
|
public abstract int getBasicAngle();
|
487
|
}
|