Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / TwistyDiamond.java @ eb376d3a

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.objects;
21

    
22
import android.content.res.Resources;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25

    
26
import org.distorted.library.effect.MatrixEffectQuaternion;
27
import org.distorted.library.main.DistortedEffects;
28
import org.distorted.library.main.DistortedTexture;
29
import org.distorted.library.mesh.MeshBase;
30
import org.distorted.library.mesh.MeshSquare;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.main.RubikSurfaceView;
34

    
35
import java.util.Random;
36

    
37
import static org.distorted.effects.scramble.ScrambleEffect.START_AXIS;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
public class TwistyDiamond extends TwistyObject
42
{
43
  private static final float SQ2 = (float)Math.sqrt(2);
44
  private static final float SQ3 = (float)Math.sqrt(3);
45
  private static final float SQ6 = (float)Math.sqrt(6);
46

    
47
  private static final int FACES_PER_CUBIT =8;
48

    
49
  // the four rotation axis of a Diamond. Must be normalized.
50
  static final Static3D[] ROT_AXIS = new Static3D[]
51
         {
52
           new Static3D(+SQ6/3,+SQ3/3,     0),
53
           new Static3D(-SQ6/3,+SQ3/3,     0),
54
           new Static3D(     0,-SQ3/3,-SQ6/3),
55
           new Static3D(     0,-SQ3/3,+SQ6/3)
56
         };
57

    
58
  private static final int[] FACE_COLORS = new int[]
59
         {
60
           COLOR_YELLOW, COLOR_WHITE ,
61
           COLOR_BLUE  , COLOR_GREEN ,
62
           COLOR_PINK  , COLOR_VIOLET,
63
           COLOR_RED   , COLOR_BROWN ,
64
         };
65

    
66
  // All legal rotation quats of a Diamond: unit + three 180 deg turns + 8 generators
67
  private static final Static4D[] QUATS = new Static4D[]
68
         {
69
           new Static4D(  0.0f,  0.0f,   0.0f,  1.0f ),
70
           new Static4D(  0.0f,  1.0f,   0.0f,  0.0f ),
71
           new Static4D(+SQ2/2,  0.0f, -SQ2/2,  0.0f ),
72
           new Static4D(-SQ2/2,  0.0f, -SQ2/2,  0.0f ),
73

    
74
           new Static4D(+SQ2/2,  0.5f,   0.0f,  0.5f ),
75
           new Static4D(-SQ2/2,  0.5f,   0.0f,  0.5f ),
76
           new Static4D(  0.0f,  0.5f, +SQ2/2,  0.5f ),
77
           new Static4D(  0.0f,  0.5f, -SQ2/2,  0.5f ),
78
           new Static4D(+SQ2/2,  0.5f,   0.0f, -0.5f ),
79
           new Static4D(-SQ2/2,  0.5f,   0.0f, -0.5f ),
80
           new Static4D(  0.0f,  0.5f, +SQ2/2, -0.5f ),
81
           new Static4D(  0.0f,  0.5f, -SQ2/2, -0.5f )
82
         };
83

    
84
  private static final float DIST = 0.50f;
85

    
86
  // centers of the 6 octahedrons + 8 tetrahedrons ( i.e. of the all 14 cubits)
87
  private static final Static3D[] CENTERS = new Static3D[]
88
         {
89
           new Static3D( DIST,          0, DIST ),
90
           new Static3D( DIST,          0,-DIST ),
91
           new Static3D(-DIST,          0,-DIST ),
92
           new Static3D(-DIST,          0, DIST ),
93
           new Static3D(    0, DIST*SQ2  ,    0 ),
94
           new Static3D(    0,-DIST*SQ2  ,    0 ),
95

    
96
           new Static3D(    0, DIST*SQ2/2, DIST ),
97
           new Static3D( DIST, DIST*SQ2/2,    0 ),
98
           new Static3D(    0, DIST*SQ2/2,-DIST ),
99
           new Static3D(-DIST, DIST*SQ2/2,    0 ),
100
           new Static3D(    0,-DIST*SQ2/2, DIST ),
101
           new Static3D( DIST,-DIST*SQ2/2,    0 ),
102
           new Static3D(    0,-DIST*SQ2/2,-DIST ),
103
           new Static3D(-DIST,-DIST*SQ2/2,    0 )
104
         };
105

    
106
  // Colors of the faces of cubits. Each cubit has 8 faces
107
  private static final int[][] mFaceMap = new int[][]
108
         {
109
           { 6,1,8,8, 2,5,8,8 },
110
           { 8,1,3,8, 8,5,7,8 },
111
           { 8,8,3,4, 8,8,7,0 },
112
           { 6,8,8,4, 2,8,8,0 },
113
           { 6,1,3,4, 8,8,8,8 },
114
           { 8,8,8,8, 2,5,7,0 },
115

    
116
           { 6,8,8,8, 8,8,8,8 },
117
           { 1,8,8,8, 8,8,8,8 },
118
           { 3,8,8,8, 8,8,8,8 },
119
           { 4,8,8,8, 8,8,8,8 },
120
           { 2,8,8,8, 8,8,8,8 },
121
           { 5,8,8,8, 8,8,8,8 },
122
           { 7,8,8,8, 8,8,8,8 },
123
           { 0,8,8,8, 8,8,8,8 }
124
         };
125

    
126
  private static MeshBase mOctaMesh, mTetraMesh;
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  TwistyDiamond(int size, Static4D quat, DistortedTexture texture,
131
                MeshSquare mesh, DistortedEffects effects, int[][] moves, Resources res, int scrWidth)
132
    {
133
    super(size, 60, quat, texture, mesh, effects, moves, ObjectList.DIAM, res, scrWidth);
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  float getScreenRatio()
139
    {
140
    return 0.65f;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  Static4D[] getQuats()
146
    {
147
    return QUATS;
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  int getNumFaces()
153
    {
154
    return FACE_COLORS.length;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  boolean shouldResetTextureMaps()
160
    {
161
    return false;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  int getNumStickerTypes()
167
    {
168
    return 1;
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  float getBasicStep()
174
    {
175
    return SQ6/4;
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  int getNumCubitFaces()
181
    {
182
    return FACES_PER_CUBIT;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
  Static3D[] getCubitPositions(int size)
188
    {
189
    return CENTERS;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private Static4D getQuat(int cubit)
195
    {
196
    switch(cubit)
197
      {
198
      case  0:
199
      case  1:
200
      case  2:
201
      case  3:
202
      case  4:
203
      case  5:
204
      case  6: return QUATS[0];                          // unit quat
205
      case  7: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
206
      case  8: return QUATS[1];                          // 180 along Y
207
      case  9: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along Y
208
      case 10: return new Static4D(0,     0,1,    0);    // 180 along Z
209
      case 11: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
210
      case 12: return new Static4D(     1,0,0,    0);    // 180 along X
211
      case 13: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
212
      }
213

    
214
    return null;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  MeshBase createCubitMesh(int cubit)
220
    {
221
    MeshBase mesh;
222

    
223
    if( cubit<6 )
224
      {
225
      if( mOctaMesh==null ) mOctaMesh = CubitFactory.getInstance().createOctaMesh();
226
      mesh = mOctaMesh.copy(true);
227
      }
228
    else
229
      {
230
      if( mTetraMesh==null ) mTetraMesh = CubitFactory.getInstance().createTetraMesh();
231
      mesh = mTetraMesh.copy(true);
232
      }
233

    
234
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit), new Static3D(0,0,0) );
235
    mesh.apply(quat,0xffffffff,0);
236

    
237
    return mesh;
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  int getFaceColor(int cubit, int cubitface, int size)
243
    {
244
    return mFaceMap[cubit][cubitface];
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
250
    {
251
    float STROKE = 0.044f*side;
252
    float OFF = STROKE/2 -1;
253
    float OFF2 = 0.5f*side + OFF;
254
    float HEIGHT = side - OFF;
255
    float RADIUS = side/12.0f;
256
    float ARC1_H = 0.2f*side;
257
    float ARC1_W = side*0.5f;
258
    float ARC2_W = 0.153f*side;
259
    float ARC2_H = 0.905f*side;
260
    float ARC3_W = side-ARC2_W;
261

    
262
    float M = SQ3/2;
263
    float D = (M/2 - 0.51f)*side;
264

    
265
    paint.setAntiAlias(true);
266
    paint.setStrokeWidth(STROKE);
267
    paint.setColor(FACE_COLORS[face]);
268
    paint.setStyle(Paint.Style.FILL);
269

    
270
    canvas.drawRect(left,top,left+side,top+side,paint);
271

    
272
    paint.setColor(INTERIOR_COLOR);
273
    paint.setStyle(Paint.Style.STROKE);
274

    
275
    canvas.drawLine(           left, M*HEIGHT+D,  side       +left, M*HEIGHT+D, paint);
276
    canvas.drawLine(      OFF +left, M*side  +D,       OFF2  +left,          D, paint);
277
    canvas.drawLine((side-OFF)+left, M*side  +D, (side-OFF2) +left,          D, paint);
278

    
279
    canvas.drawArc( ARC1_W-RADIUS+left, M*(ARC1_H-RADIUS)+D, ARC1_W+RADIUS+left, M*(ARC1_H+RADIUS)+D, 225, 90, false, paint);
280
    canvas.drawArc( ARC2_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC2_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 105, 90, false, paint);
281
    canvas.drawArc( ARC3_W-RADIUS+left, M*(ARC2_H-RADIUS)+D, ARC3_W+RADIUS+left, M*(ARC2_H+RADIUS)+D, 345, 90, false, paint);
282
    }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
  float returnMultiplier()
287
    {
288
    return 1.5f;
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  float[] getRowChances()
294
    {
295
    float[] chances = new float[2];
296

    
297
    chances[0] = 0.5f;
298
    chances[1] = 1.0f;
299

    
300
    return chances;
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
// PUBLIC API
305

    
306
  public Static3D[] getRotationAxis()
307
    {
308
    return ROT_AXIS;
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  public int getBasicAngle()
314
    {
315
    return 3;
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  public int randomizeNewRotAxis(Random rnd, int oldRotAxis)
321
    {
322
    int numAxis = ROTATION_AXIS.length;
323

    
324
    if( oldRotAxis == START_AXIS )
325
      {
326
      return rnd.nextInt(numAxis);
327
      }
328
    else
329
      {
330
      int newVector = rnd.nextInt(numAxis-1);
331
      return (newVector>=oldRotAxis ? newVector+1 : newVector);
332
      }
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public int randomizeNewRow(Random rnd, int oldRotAxis, int oldRow, int newRotAxis)
338
    {
339
    float rowFloat = rnd.nextFloat();
340

    
341
    for(int row=0; row<mRowChances.length; row++)
342
      {
343
      if( rowFloat<=mRowChances[row] ) return row;
344
      }
345

    
346
    return 0;
347
    }
348

    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350

    
351
  int mulQuat(int q1, int q2)
352
    {
353
    Static4D result = RubikSurfaceView.quatMultiply(QUATS[q1],QUATS[q2]);
354

    
355
    float rX = result.get0();
356
    float rY = result.get1();
357
    float rZ = result.get2();
358
    float rW = result.get3();
359

    
360
    final float MAX_ERROR = 0.1f;
361
    float dX,dY,dZ,dW;
362

    
363
    for(int i=0; i<QUATS.length; i++)
364
      {
365
      dX = QUATS[i].get0() - rX;
366
      dY = QUATS[i].get1() - rY;
367
      dZ = QUATS[i].get2() - rZ;
368
      dW = QUATS[i].get3() - rW;
369

    
370
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
371
          dY<MAX_ERROR && dY>-MAX_ERROR &&
372
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
373
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
374

    
375
      dX = QUATS[i].get0() + rX;
376
      dY = QUATS[i].get1() + rY;
377
      dZ = QUATS[i].get2() + rZ;
378
      dW = QUATS[i].get3() + rW;
379

    
380
      if( dX<MAX_ERROR && dX>-MAX_ERROR &&
381
          dY<MAX_ERROR && dY>-MAX_ERROR &&
382
          dZ<MAX_ERROR && dZ>-MAX_ERROR &&
383
          dW<MAX_ERROR && dW>-MAX_ERROR  ) return i;
384
      }
385

    
386
    return -1;
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
// The Diamond is solved if and only if:
391
//
392
// 1) all 5 octahedrons are rotated with the same quat
393
// 2) the 8 tetrahedrons are rotated with the quat and, optionally, the can also be rotated
394
//    by multitudes of 120 degrees along the face they are the center of.
395
//
396
// so:
397
// 1) cubits 6,12: can also be QUAT 6,10
398
// 2) cubits 7,13: can also be QUAT 4,8
399
// 3) cubits 8,10: can also be QUAT 7,11
400
// 4) cubits 9,11: can also be QUAT 5,9
401

    
402
  public boolean isSolved()
403
    {
404
    int q = CUBITS[0].mQuatIndex;
405

    
406
    if ( CUBITS[ 1].mQuatIndex == q &&
407
         CUBITS[ 2].mQuatIndex == q &&
408
         CUBITS[ 3].mQuatIndex == q &&
409
         CUBITS[ 4].mQuatIndex == q &&
410
         CUBITS[ 5].mQuatIndex == q  )
411
      {
412
      int q1 = mulQuat(q,5);
413
      int q2 = mulQuat(q,9);
414

    
415
      if( CUBITS[ 9].mQuatIndex != q && CUBITS[ 9].mQuatIndex != q1 && CUBITS[ 9].mQuatIndex != q2 ) return false;
416
      if( CUBITS[11].mQuatIndex != q && CUBITS[11].mQuatIndex != q1 && CUBITS[11].mQuatIndex != q2 ) return false;
417

    
418
      q1 = mulQuat(q,4);
419
      q2 = mulQuat(q,8);
420

    
421
      if( CUBITS[ 7].mQuatIndex != q && CUBITS[ 7].mQuatIndex != q1 && CUBITS[ 7].mQuatIndex != q2 ) return false;
422
      if( CUBITS[13].mQuatIndex != q && CUBITS[13].mQuatIndex != q1 && CUBITS[13].mQuatIndex != q2 ) return false;
423

    
424
      q1 = mulQuat(q,6);
425
      q2 = mulQuat(q,10);
426

    
427
      if( CUBITS[ 6].mQuatIndex != q && CUBITS[ 6].mQuatIndex != q1 && CUBITS[ 6].mQuatIndex != q2 ) return false;
428
      if( CUBITS[12].mQuatIndex != q && CUBITS[12].mQuatIndex != q1 && CUBITS[12].mQuatIndex != q2 ) return false;
429

    
430
      q1 = mulQuat(q,7);
431
      q2 = mulQuat(q,11);
432

    
433
      if( CUBITS[ 8].mQuatIndex != q && CUBITS[ 8].mQuatIndex != q1 && CUBITS[ 8].mQuatIndex != q2 ) return false;
434
      if( CUBITS[10].mQuatIndex != q && CUBITS[10].mQuatIndex != q1 && CUBITS[10].mQuatIndex != q2 ) return false;
435

    
436
      return true;
437
      }
438

    
439
    return false;
440
    }
441

    
442
///////////////////////////////////////////////////////////////////////////////////////////////////
443
// only needed for solvers - there are no Diamond solvers ATM
444

    
445
  public String retObjectString()
446
    {
447
    return "";
448
    }
449

    
450
}
(12-12/19)