Project

General

Profile

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

magiccube / src / main / java / org / distorted / objects / RubikPyraminx.java @ e36b59bc

1 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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 1f9772f3 Leszek Koltunski
package org.distorted.objects;
21 e844c116 Leszek Koltunski
22
import android.graphics.Canvas;
23
import android.graphics.Paint;
24
25 40ab026e Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
26
import org.distorted.library.effect.VertexEffectMove;
27
import org.distorted.library.effect.VertexEffectRotate;
28
import org.distorted.library.effect.VertexEffectScale;
29 f0fa83ae Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
30 e844c116 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.mesh.MeshBase;
33 988f434e Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
34 e844c116 Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
35 988f434e Leszek Koltunski
import org.distorted.library.mesh.MeshTriangles;
36
import org.distorted.library.type.Static1D;
37 e844c116 Leszek Koltunski
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42
public class RubikPyraminx extends RubikObject
43
{
44 9f4c44fe Leszek Koltunski
  private static final float SQ2 = (float)Math.sqrt(2);
45
  private static final float SQ3 = (float)Math.sqrt(3);
46
47 37a25788 Leszek Koltunski
  static final Static3D[] AXIS = new Static3D[]
48 e844c116 Leszek Koltunski
         {
49 9f4c44fe Leszek Koltunski
           new Static3D(         0,        1,       0 ),
50 49f67f9b Leszek Koltunski
           new Static3D(         0,  -1.0f/3, 2*SQ2/3 ),
51 9f4c44fe Leszek Koltunski
           new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 ),
52 49f67f9b Leszek Koltunski
           new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 )
53 e844c116 Leszek Koltunski
         };
54
55
  private static final int[] FACE_COLORS = new int[]
56
         {
57 906cc928 Leszek Koltunski
           0xff00ff00, 0xffffff00,  // AXIS[0]right (GREEN ) AXIS[1]right (YELLOW )
58
           0xff0000ff, 0xffff0000   // AXIS[2]right (BLUE  ) AXIS[3]right (RED    )
59 e844c116 Leszek Koltunski
         };
60
61 9f4c44fe Leszek Koltunski
  // computed with res/raw/compute_quats.c
62 e844c116 Leszek Koltunski
  private static final float[] LEGALQUATS = new float[]
63
         {
64 9f4c44fe Leszek Koltunski
           0.0f, 1.0f, -1.0f, 0.5f, -0.5f, SQ2/2, -SQ2/2, SQ3/2, -SQ3/2,
65
           SQ3/3, -SQ3/3, SQ3/6, -SQ3/6, SQ2*SQ3/3, -SQ2*SQ3/3, SQ2*SQ3/6, -SQ2*SQ3/6
66 e844c116 Leszek Koltunski
         };
67
68 89a11f7b Leszek Koltunski
  private int[] mRotArray;
69 40ab026e Leszek Koltunski
  private static VertexEffectRotate[] ROTATION;
70
71
  private static MeshBase mMesh =null;
72
  private static MeshBase[] mMeshRotated = new MeshBase[AXIS.length];
73 89a11f7b Leszek Koltunski
74
  static
75
    {
76
    Static3D center = new Static3D(0,0,0);
77
    Static1D angle  = new Static1D(180.0f);
78
79 40ab026e Leszek Koltunski
    ROTATION = new VertexEffectRotate[AXIS.length];
80 89a11f7b Leszek Koltunski
81
    for(int i=0; i<AXIS.length; i++)
82
      {
83 40ab026e Leszek Koltunski
      ROTATION[i] = new VertexEffectRotate( angle, AXIS[i], center);
84
      mMeshRotated[i] = null;
85 89a11f7b Leszek Koltunski
      }
86
    }
87 49f67f9b Leszek Koltunski
88 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90 a31d25de Leszek Koltunski
  RubikPyraminx(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects, int[][] moves)
91 e844c116 Leszek Koltunski
    {
92 aa171dee Leszek Koltunski
    super(size, 30, quatCur,quatAcc,texture,mesh,effects,moves, RubikObjectList.PYRA);
93 e844c116 Leszek Koltunski
    }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97 89a11f7b Leszek Koltunski
  private void emitRow(float x, float y, float z, float dx, float dy, float dz, int n, int rot, Static3D[] array, int index)
98 49f67f9b Leszek Koltunski
    {
99
    for(int i=0; i<n; i++)
100
      {
101
      mRotArray[i+index] = rot;
102
      array[i+index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
103
      x += dx;
104
      y += dy;
105
      z += dz;
106
      }
107
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
  private int emitLowermost(float x, float y, float z, int n, Static3D[] array)
112
    {
113
    int added = 0;
114
115 769409d2 Leszek Koltunski
    emitRow( x      +0.5f, y+SQ3*SQ2/9, z+ SQ3/18,  1.0f, 0,     0, n-1, 1, array, added);
116
    added += (n-1);
117
    emitRow( x    +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9,  0.5f, 0, SQ3/2, n-1, 3, array, added);
118
    added += (n-1);
119
    emitRow( x+n-1-1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, -0.5f, 0, SQ3/2, n-1, 2, array, added);
120
    added += (n-1);
121
122 49f67f9b Leszek Koltunski
    for(int i=n; i>=1; i--)
123
      {
124 769409d2 Leszek Koltunski
      emitRow(x     , y, z      , 1,0,0, i  , -1, array, added);
125 49f67f9b Leszek Koltunski
      added += i;
126 769409d2 Leszek Koltunski
      emitRow(x+0.5f, y, z+SQ3/6, 1,0,0, i-1,  0, array, added);
127
      added += (i-1);
128 49f67f9b Leszek Koltunski
      x += 0.5f;
129
      y += 0.0f;
130
      z += SQ3/2;
131
      }
132
133
    return added;
134
    }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
  private int emitUpper(float x, float y, float z, int n, Static3D[] array, int index)
139
    {
140
    if( n>1 )
141
      {
142 769409d2 Leszek Koltunski
      emitRow( x           , y          , z        ,  1.0f, 0,     0, n-1, -1, array, index);
143
      index += (n-1);
144
      emitRow( x+0.5f      , y+SQ3*SQ2/9, z+SQ3/18 ,  1.0f, 0,     0, n-1,  1, array, index);
145
      index += (n-1);
146
      emitRow( x+0.5f      , y          , z+SQ3/2  ,  0.5f, 0, SQ3/2, n-1, -1, array, index);
147
      index += (n-1);
148
      emitRow( x    +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9,  0.5f, 0, SQ3/2, n-1,  3, array, index);
149 49f67f9b Leszek Koltunski
      index += (n-1);
150 769409d2 Leszek Koltunski
      emitRow( x+n-1       , y          , z        , -0.5f, 0, SQ3/2, n-1, -1, array, index);
151 49f67f9b Leszek Koltunski
      index += (n-1);
152 769409d2 Leszek Koltunski
      emitRow( x+n-1-1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9, -0.5f, 0, SQ3/2, n-1,  2, array, index);
153 49f67f9b Leszek Koltunski
      index += (n-1);
154
      }
155
    else
156
      {
157 89a11f7b Leszek Koltunski
      mRotArray[index] = -1;
158 49f67f9b Leszek Koltunski
      array[index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
159
      index++;
160
      }
161
162
    return index;
163
    }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
// size^2 + 3*(size-1) in the lowermost layer, then 6*(size-2) in the next, 6*(size-3) in the next,
167
// ... 6 in the forelast, 1 in the last = 4size^2 - 6size +4 (if size>1)
168
169 e844c116 Leszek Koltunski
  Static3D[] getCubitPositions(int size)
170
    {
171 769409d2 Leszek Koltunski
    int numCubits = size>1 ? 4*size*size - 6*size +4 : 1;
172 c2cb520d Leszek Koltunski
    Static3D[] tmp = new Static3D[numCubits];
173 89a11f7b Leszek Koltunski
    mRotArray = new int[numCubits];
174 49f67f9b Leszek Koltunski
175
    int currentIndex = emitLowermost( -0.5f*size, -(SQ2*SQ3/12)*size, -(SQ3/6)*size, size, tmp);
176 c2cb520d Leszek Koltunski
177 49f67f9b Leszek Koltunski
    for(int i=size-1; i>=1; i--)
178 cc7dc72a Leszek Koltunski
      {
179 49f67f9b Leszek Koltunski
      currentIndex = emitUpper( -0.5f*i, ((SQ2*SQ3)/12)*(3*size-4*i), -(SQ3/6)*i, i, tmp, currentIndex);
180 cc7dc72a Leszek Koltunski
      }
181 49f67f9b Leszek Koltunski
182 c2cb520d Leszek Koltunski
    return tmp;
183 e844c116 Leszek Koltunski
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
187
  float[] getLegalQuats()
188
    {
189
    return LEGALQUATS;
190
    }
191
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
  int getNumFaces()
195
    {
196
    return FACE_COLORS.length;
197
    }
198
199 f0fa83ae Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
  float getScreenRatio()
202
    {
203 7381193e Leszek Koltunski
    return 0.82f;
204 f0fa83ae Leszek Koltunski
    }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208 14bd7976 Leszek Koltunski
  private MeshBase createStaticMesh(int cubit)
209 e844c116 Leszek Koltunski
    {
210 40ab026e Leszek Koltunski
    final float SQ2 = (float)Math.sqrt(2);
211
    final float SQ3 = (float)Math.sqrt(3);
212 988f434e Leszek Koltunski
    final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
213
    final int MESHES=4;
214
215 40ab026e Leszek Koltunski
    int association = 1;
216 988f434e Leszek Koltunski
    MeshBase[] meshes = new MeshTriangles[MESHES];
217
218 40ab026e Leszek Koltunski
    meshes[0] = new MeshTriangles(5);
219 7e8b9852 Leszek Koltunski
    meshes[0].setEffectAssociation(0,association,0);
220 988f434e Leszek Koltunski
221 40ab026e Leszek Koltunski
    for(int i=1; i<MESHES; i++)
222
      {
223
      association <<= 1;
224
      meshes[i] = meshes[0].copy(true);
225 7e8b9852 Leszek Koltunski
      meshes[i].setEffectAssociation(0,association,0);
226 40ab026e Leszek Koltunski
      }
227 988f434e Leszek Koltunski
228 40ab026e Leszek Koltunski
    MeshBase result = new MeshJoined(meshes);
229
230
    Static3D a0 = new Static3D(         0,        1,       0 );
231
    Static3D a1 = new Static3D(         0,  -1.0f/3, 2*SQ2/3 );
232
    Static3D a2 = new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
233
    Static3D a3 = new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 );
234
235
    float tetraHeight = SQ2*SQ3/3;
236
    float d1 = 0.75f*tetraHeight;
237
    float d2 =-0.10f*tetraHeight;
238
    float d3 = 0.20f*tetraHeight;
239
240
    Static3D dCen0 = new Static3D( d1*a0.get0(), d1*a0.get1(), d1*a0.get2() );
241
    Static3D dCen1 = new Static3D( d1*a1.get0(), d1*a1.get1(), d1*a1.get2() );
242
    Static3D dCen2 = new Static3D( d1*a2.get0(), d1*a2.get1(), d1*a2.get2() );
243
    Static3D dCen3 = new Static3D( d1*a3.get0(), d1*a3.get1(), d1*a3.get2() );
244
245
    Static3D dVec0 = new Static3D( d2*a0.get0(), d2*a0.get1(), d2*a0.get2() );
246
    Static3D dVec1 = new Static3D( d2*a1.get0(), d2*a1.get1(), d2*a1.get2() );
247
    Static3D dVec2 = new Static3D( d2*a2.get0(), d2*a2.get1(), d2*a2.get2() );
248
    Static3D dVec3 = new Static3D( d2*a3.get0(), d2*a3.get1(), d2*a3.get2() );
249
250
    Static4D dReg  = new Static4D(0,0,0,d3);
251
    Static1D dRad  = new Static1D(1);
252
253
    Static1D angle  = new Static1D(angleFaces);
254
    Static3D axis1  = new Static3D(  -1, 0,      0);
255
    Static3D axis2  = new Static3D(0.5f, 0, -SQ3/2);
256
    Static3D axis3  = new Static3D(0.5f, 0, +SQ3/2);
257
    Static3D center1= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
258
    Static3D center2= new Static3D(0,-SQ3*SQ2/12,+SQ3/3);
259
260 14bd7976 Leszek Koltunski
    Static3D center = new Static3D(0,0,0);
261
    Static4D region = new Static4D(0,0,0,0.6f);
262
263 7e8b9852 Leszek Koltunski
    VertexEffectScale  effect1 = new VertexEffectScale ( new Static3D(1,SQ3/2,1) );
264
    VertexEffectRotate effect2 = new VertexEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
265
    VertexEffectMove   effect3 = new VertexEffectMove  ( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
266
    VertexEffectRotate effect4 = new VertexEffectRotate( new Static1D(180), new Static3D(0,0,1), center1 );
267
    VertexEffectRotate effect5 = new VertexEffectRotate( angle, axis1, center1 );
268
    VertexEffectRotate effect6 = new VertexEffectRotate( angle, axis2, center2 );
269
    VertexEffectRotate effect7 = new VertexEffectRotate( angle, axis3, center2 );
270 40ab026e Leszek Koltunski
271
    VertexEffectDeform effect8 = new VertexEffectDeform(dVec0, dRad, dCen0, dReg);
272
    VertexEffectDeform effect9 = new VertexEffectDeform(dVec1, dRad, dCen1, dReg);
273
    VertexEffectDeform effect10= new VertexEffectDeform(dVec2, dRad, dCen2, dReg);
274
    VertexEffectDeform effect11= new VertexEffectDeform(dVec3, dRad, dCen3, dReg);
275
276 14bd7976 Leszek Koltunski
    VertexEffectSink   effect12= new VertexEffectSink( new Static1D(1.3f), center, region );
277
278 7e8b9852 Leszek Koltunski
    effect4.setMeshAssociation(14,-1);  // apply to mesh[1], [2] and [3]
279
    effect5.setMeshAssociation( 2,-1);  // apply only to mesh[1]
280
    effect6.setMeshAssociation( 4,-1);  // apply only to mesh[2]
281
    effect7.setMeshAssociation( 8,-1);  // apply only to mesh[3]
282 40ab026e Leszek Koltunski
283
    result.apply(effect1);
284
    result.apply(effect2);
285
    result.apply(effect3);
286
    result.apply(effect4);
287
    result.apply(effect5);
288
    result.apply(effect6);
289
    result.apply(effect7);
290
    result.apply(effect8);
291
    result.apply(effect9);
292
    result.apply(effect10);
293
    result.apply(effect11);
294 14bd7976 Leszek Koltunski
    result.apply(effect12);
295 89a11f7b Leszek Koltunski
296
    if( mRotArray[cubit]>=0 )
297
      {
298
      result.apply( ROTATION[mRotArray[cubit]] );
299
      }
300
301
    return result;
302 e844c116 Leszek Koltunski
    }
303
304 40ab026e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
305
306 14bd7976 Leszek Koltunski
  MeshBase createCubitMesh(int cubit)
307 40ab026e Leszek Koltunski
    {
308
    int kind = mRotArray[cubit];
309
310
    if( kind>=0 )
311
      {
312 14bd7976 Leszek Koltunski
      if( mMeshRotated[kind]==null ) mMeshRotated[kind] = createStaticMesh(cubit);
313 40ab026e Leszek Koltunski
      return mMeshRotated[kind].copy(false);
314
      }
315
    else
316
      {
317 14bd7976 Leszek Koltunski
      if( mMesh==null ) mMesh = createStaticMesh(cubit);
318 40ab026e Leszek Koltunski
      return mMesh.copy(false);
319
      }
320
    }
321
322 7289fd6c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
323
324
  void createFaceTexture(Canvas canvas, Paint paint, int face, int left, int top, int side)
325
    {
326
    float STROKE = 0.06f*side;
327
    float OFF = STROKE/2 -1;
328
    float OFF2 = 0.5f*side + OFF;
329
    float HEIGHT = side - OFF;
330
    float RADIUS = side/12.0f;
331
    float ARC1_H = 0.2f*side;
332
    float ARC1_W = side*0.5f;
333
    float ARC2_W = 0.153f*side;
334
    float ARC2_H = 0.905f*side;
335
    float ARC3_W = side-ARC2_W;
336
337
    paint.setAntiAlias(true);
338
    paint.setStrokeWidth(STROKE);
339
    paint.setColor(FACE_COLORS[face]);
340
    paint.setStyle(Paint.Style.FILL);
341
342
    canvas.drawRect(left,top,left+side,top+side,paint);
343
344 14bd7976 Leszek Koltunski
    paint.setColor(INTERIOR_COLOR);
345 7289fd6c Leszek Koltunski
    paint.setStyle(Paint.Style.STROKE);
346
347
    canvas.drawLine(           left, HEIGHT,  side       +left, HEIGHT, paint);
348
    canvas.drawLine(      OFF +left, side  ,       OFF2  +left,      0, paint);
349
    canvas.drawLine((side-OFF)+left, side  , (side-OFF2) +left,      0, paint);
350
351
    canvas.drawArc( ARC1_W-RADIUS+left, ARC1_H-RADIUS, ARC1_W+RADIUS+left, ARC1_H+RADIUS, 225, 90, false, paint);
352
    canvas.drawArc( ARC2_W-RADIUS+left, ARC2_H-RADIUS, ARC2_W+RADIUS+left, ARC2_H+RADIUS, 105, 90, false, paint);
353
    canvas.drawArc( ARC3_W-RADIUS+left, ARC2_H-RADIUS, ARC3_W+RADIUS+left, ARC2_H+RADIUS, 345, 90, false, paint);
354
    }
355
356 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
357
// PUBLIC API
358
359 12ad3fca Leszek Koltunski
  public Static3D[] getRotationAxis()
360
    {
361
    return AXIS;
362
    }
363
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365
366 e844c116 Leszek Koltunski
  public int getBasicAngle()
367
    {
368
    return 3;
369
    }
370 39e74052 Leszek Koltunski
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372 e36b59bc Leszek Koltunski
// I don't quite understand it, but 0.82 works better than the theoretically correct SQ3/2 ( 0.866 )
373 39e74052 Leszek Koltunski
374 9621255f Leszek Koltunski
  public float returnMultiplier()
375 39e74052 Leszek Koltunski
    {
376 e36b59bc Leszek Koltunski
    return getSize()/0.82f;//(SQ3/2);
377 39e74052 Leszek Koltunski
    }
378 e46e17fb Leszek Koltunski
379 5cf34c5f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
380
381
  public float[] getRowChances()
382
    {
383
    int size = getSize();
384
    int total = size*(size+1)/2;
385
    float running=0.0f;
386
    float[] chances = new float[size];
387
388
    for(int i=0; i<size; i++)
389
      {
390
      running += (size-i);
391
      chances[i] = running / total;
392
      }
393
394
    return chances;
395
    }
396
397 e46e17fb Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
398
399
  public float returnRotationFactor(float offset)
400
    {
401
    int size = getSize();
402
    int row  = (int)(size*offset/(SQ3/2));
403
404
    return ((float)size)/(size-row);
405
    }
406 f0336037 Leszek Koltunski
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
// TODO
409
410 20931cf6 Leszek Koltunski
  public String retObjectString()
411 f0336037 Leszek Koltunski
    {
412
    return "";
413
    }
414 e844c116 Leszek Koltunski
}