Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikPyraminx.java @ 39e74052

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

    
22
import android.graphics.Canvas;
23
import android.graphics.Paint;
24

    
25
import org.distorted.library.effect.MatrixEffect;
26
import org.distorted.library.effect.MatrixEffectMove;
27
import org.distorted.library.effect.MatrixEffectRotate;
28
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.VertexEffectSink;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.mesh.MeshBase;
33
import org.distorted.library.mesh.MeshJoined;
34
import org.distorted.library.mesh.MeshRectangles;
35
import org.distorted.library.mesh.MeshTriangles;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class RubikPyraminx extends RubikObject
43
{
44
  private static final float SQ2 = (float)Math.sqrt(2);
45
  private static final float SQ3 = (float)Math.sqrt(3);
46

    
47
  static final Static3D[] AXIS = new Static3D[]
48
         {
49
           new Static3D(         0,        1,       0 ),
50
           new Static3D(         0,  -1.0f/3, 2*SQ2/3 ),
51
           new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 ),
52
           new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 )
53
         };
54

    
55
  private static final int[] FACE_COLORS = new int[]
56
         {
57
           0xffffff00, 0xff00ff00,  // AXIS[0]right (YELLOW) AXIS[1]right (GREEN )
58
           0xff0000ff, 0xffff0000   // AXIS[2]right (BLUE  ) AXIS[3]right (RED   )
59
         };
60

    
61
  // computed with res/raw/compute_quats.c
62
  private static final float[] LEGALQUATS = new float[]
63
         {
64
           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
         };
67

    
68
  private int[] mRotArray;
69
  private static MatrixEffectRotate[][] ROTATION;
70

    
71
  static
72
    {
73
    Static3D center = new Static3D(0,0,0);
74
    Static1D angle  = new Static1D(180.0f);
75

    
76
    ROTATION = new MatrixEffectRotate[AXIS.length][1];
77

    
78
    for(int i=0; i<AXIS.length; i++)
79
      {
80
      ROTATION[i][0] = new MatrixEffectRotate( angle, AXIS[i], center);
81
      }
82
    }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
  RubikPyraminx(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
87
    {
88
    super(size,quatCur,quatAcc,texture,mesh,effects);
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  private void emitRow(float x, float y, float z, float dx, float dy, float dz, int n, int rot, Static3D[] array, int index)
94
    {
95
    for(int i=0; i<n; i++)
96
      {
97
      mRotArray[i+index] = rot;
98
      array[i+index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
99
      x += dx;
100
      y += dy;
101
      z += dz;
102
      }
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  private int emitLowermost(float x, float y, float z, int n, Static3D[] array)
108
    {
109
    int added = 0;
110

    
111
    emitRow( x      +0.5f, y+SQ3*SQ2/9, z+ SQ3/18,  1.0f, 0,     0, n-1, 1, array, added);
112
    added += (n-1);
113
    emitRow( x    +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9,  0.5f, 0, SQ3/2, n-1, 3, array, added);
114
    added += (n-1);
115
    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);
116
    added += (n-1);
117

    
118
    for(int i=n; i>=1; i--)
119
      {
120
      emitRow(x     , y, z      , 1,0,0, i  , -1, array, added);
121
      added += i;
122
      emitRow(x+0.5f, y, z+SQ3/6, 1,0,0, i-1,  0, array, added);
123
      added += (i-1);
124
      x += 0.5f;
125
      y += 0.0f;
126
      z += SQ3/2;
127
      }
128

    
129
    return added;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  private int emitUpper(float x, float y, float z, int n, Static3D[] array, int index)
135
    {
136
    if( n>1 )
137
      {
138
      emitRow( x           , y          , z        ,  1.0f, 0,     0, n-1, -1, array, index);
139
      index += (n-1);
140
      emitRow( x+0.5f      , y+SQ3*SQ2/9, z+SQ3/18 ,  1.0f, 0,     0, n-1,  1, array, index);
141
      index += (n-1);
142
      emitRow( x+0.5f      , y          , z+SQ3/2  ,  0.5f, 0, SQ3/2, n-1, -1, array, index);
143
      index += (n-1);
144
      emitRow( x    +1.0f/3, y+SQ3*SQ2/9, z+2*SQ3/9,  0.5f, 0, SQ3/2, n-1,  3, array, index);
145
      index += (n-1);
146
      emitRow( x+n-1       , y          , z        , -0.5f, 0, SQ3/2, n-1, -1, array, index);
147
      index += (n-1);
148
      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);
149
      index += (n-1);
150
      }
151
    else
152
      {
153
      mRotArray[index] = -1;
154
      array[index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
155
      index++;
156
      }
157

    
158
    return index;
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
// size^2 + 3*(size-1) in the lowermost layer, then 6*(size-2) in the next, 6*(size-3) in the next,
163
// ... 6 in the forelast, 1 in the last = 4size^2 - 6size +4 (if size>1)
164

    
165
  Static3D[] getCubitPositions(int size)
166
    {
167
    int numCubits = size>1 ? 4*size*size - 6*size +4 : 1;
168
    Static3D[] tmp = new Static3D[numCubits];
169
    mRotArray = new int[numCubits];
170

    
171
    int currentIndex = emitLowermost( -0.5f*size, -(SQ2*SQ3/12)*size, -(SQ3/6)*size, size, tmp);
172

    
173
    for(int i=size-1; i>=1; i--)
174
      {
175
      currentIndex = emitUpper( -0.5f*i, ((SQ2*SQ3)/12)*(3*size-4*i), -(SQ3/6)*i, i, tmp, currentIndex);
176
      }
177

    
178
    return tmp;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  float[] getLegalQuats()
184
    {
185
    return LEGALQUATS;
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  int getNumFaces()
191
    {
192
    return FACE_COLORS.length;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  float getScreenRatio()
198
    {
199
    return 0.7f;
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  VertexEffectSink getSink(int size)
205
    {
206
    Static3D center = new Static3D(0,0,0);
207
    Static4D region = new Static4D(0,0,0,0.6f);
208
    return new VertexEffectSink( new Static1D(1.3f), center, region );
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  Static3D[] getRotationAxis()
214
    {
215
    return AXIS;
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  void createFaceTexture(Canvas canvas, Paint paint, int face)
221
    {
222
    int xoffset = face*TEXTURE_HEIGHT;
223
    float STROKE = 0.06f*TEXTURE_HEIGHT;
224
    float OFF = STROKE/2 -1;
225
    float OFF2 = 0.5f*TEXTURE_HEIGHT + OFF;
226
    float HEIGHT = TEXTURE_HEIGHT - OFF;
227
    float RADIUS = TEXTURE_HEIGHT/12.0f;
228
    float ARC1_H = 0.2f*TEXTURE_HEIGHT;
229
    float ARC1_W = TEXTURE_HEIGHT*0.5f;
230
    float ARC2_W = 0.153f*TEXTURE_HEIGHT;
231
    float ARC2_H = 0.905f*TEXTURE_HEIGHT;
232
    float ARC3_W = TEXTURE_HEIGHT-ARC2_W;
233

    
234
    paint.setAntiAlias(true);
235
    paint.setStrokeWidth(STROKE);
236
    paint.setColor(FACE_COLORS[face]);
237
    paint.setStyle(Paint.Style.FILL);
238

    
239
    canvas.drawRect(xoffset,0,xoffset+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
240

    
241
    paint.setColor(0xff000000);
242
    paint.setStyle(Paint.Style.STROKE);
243

    
244
    canvas.drawLine(                     xoffset,         HEIGHT,  TEXTURE_HEIGHT       +xoffset, HEIGHT, paint);
245
    canvas.drawLine(                OFF +xoffset, TEXTURE_HEIGHT,                 OFF2  +xoffset,      0, paint);
246
    canvas.drawLine((TEXTURE_HEIGHT-OFF)+xoffset, TEXTURE_HEIGHT, (TEXTURE_HEIGHT-OFF2) +xoffset,      0, paint);
247

    
248
    canvas.drawArc( ARC1_W-RADIUS+xoffset, ARC1_H-RADIUS, ARC1_W+RADIUS+xoffset, ARC1_H+RADIUS, 225, 90, false, paint);
249
    canvas.drawArc( ARC2_W-RADIUS+xoffset, ARC2_H-RADIUS, ARC2_W+RADIUS+xoffset, ARC2_H+RADIUS, 105, 90, false, paint);
250
    canvas.drawArc( ARC3_W-RADIUS+xoffset, ARC2_H-RADIUS, ARC3_W+RADIUS+xoffset, ARC2_H+RADIUS, 345, 90, false, paint);
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  MeshBase createCubitMesh(int cubit, int vertices)
256
    {
257
    final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
258
    final int MESHES=4;
259

    
260
    MeshBase[] meshes = new MeshTriangles[MESHES];
261
    for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(5);
262

    
263
    MatrixEffect[] effects0 = new MatrixEffect[3];
264
    effects0[0] = new MatrixEffectScale( new Static3D(1,SQ3/2,1) );
265
    effects0[1] = new MatrixEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
266
    effects0[2] = new MatrixEffectMove( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
267

    
268
    meshes[0].apply(effects0);
269

    
270
    Static1D angle = new Static1D(angleFaces);
271
    Static3D axis  = new Static3D(-1,0,0);
272
    Static3D center= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
273

    
274
    MatrixEffect[] effects1 = new MatrixEffect[5];
275
    effects1[0] = effects0[0];
276
    effects1[1] = effects0[1];
277
    effects1[2] = effects0[2];
278
    effects1[3] = new MatrixEffectRotate( new Static1D(180), new Static3D(0,0,1), center );
279
    effects1[4] = new MatrixEffectRotate( angle, axis, center );
280
    meshes[1].apply(effects1);
281

    
282
    axis.set(0.5f,0,-SQ3/2);
283
    center.set2(SQ3/3);
284
    meshes[2].apply(effects1);
285

    
286
    axis.set2(SQ3/2);
287
    meshes[3].apply(effects1);
288

    
289
    MeshJoined result = new MeshJoined(meshes);
290

    
291
    if( mRotArray[cubit]>=0 )
292
      {
293
      result.apply( ROTATION[mRotArray[cubit]] );
294
      }
295

    
296
    return result;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300
// PUBLIC API
301

    
302
  public int getBasicAngle()
303
    {
304
    return 3;
305
    }
306

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

    
309
  public int returnRowFromOffset(float offset)
310
    {
311
    return (int)(getSize()*offset/(SQ3/2));
312
    }
313
}
(7-7/8)