Project

General

Profile

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

magiccube / src / main / java / org / distorted / object / RubikPyraminx.java @ 89a11f7b

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
package org.distorted.object;
21
22
import android.graphics.Canvas;
23
import android.graphics.Paint;
24
25 988f434e Leszek Koltunski
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 e844c116 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshBase;
32 988f434e Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
33 e844c116 Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
34 988f434e Leszek Koltunski
import org.distorted.library.mesh.MeshTriangles;
35
import org.distorted.library.type.Static1D;
36 e844c116 Leszek Koltunski
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
public class RubikPyraminx extends RubikObject
42
{
43 9f4c44fe Leszek Koltunski
  private static final float SQ2 = (float)Math.sqrt(2);
44
  private static final float SQ3 = (float)Math.sqrt(3);
45
46 e844c116 Leszek Koltunski
  private static final Static3D[] AXIS = new Static3D[]
47
         {
48 9f4c44fe Leszek Koltunski
           new Static3D(         0,        1,       0 ),
49 49f67f9b Leszek Koltunski
           new Static3D(         0,  -1.0f/3, 2*SQ2/3 ),
50 9f4c44fe Leszek Koltunski
           new Static3D(-SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 ),
51 49f67f9b Leszek Koltunski
           new Static3D( SQ2*SQ3/3,  -1.0f/3,  -SQ2/3 )
52 e844c116 Leszek Koltunski
         };
53
54
  private static final int[] FACE_COLORS = new int[]
55
         {
56
           0xffffff00, 0xff00ff00,  // AXIS[0]right (YELLOW) AXIS[1]right (GREEN )
57
           0xff0000ff, 0xffff0000   // AXIS[2]right (BLUE  ) AXIS[3]right (RED   )
58
         };
59
60 9f4c44fe Leszek Koltunski
  // computed with res/raw/compute_quats.c
61 e844c116 Leszek Koltunski
  private static final float[] LEGALQUATS = new float[]
62
         {
63 9f4c44fe Leszek Koltunski
           0.0f, 1.0f, -1.0f, 0.5f, -0.5f, SQ2/2, -SQ2/2, SQ3/2, -SQ3/2,
64
           SQ3/3, -SQ3/3, SQ3/6, -SQ3/6, SQ2*SQ3/3, -SQ2*SQ3/3, SQ2*SQ3/6, -SQ2*SQ3/6
65 e844c116 Leszek Koltunski
         };
66
67 89a11f7b Leszek Koltunski
  private int[] mRotArray;
68
  private static MatrixEffectRotate[][] ROTATION;
69
70
  static
71
    {
72
    Static3D center = new Static3D(0,0,0);
73
    Static1D angle  = new Static1D(180.0f);
74
75
    ROTATION = new MatrixEffectRotate[AXIS.length][1];
76
77
    for(int i=0; i<AXIS.length; i++)
78
      {
79
      ROTATION[i][0] = new MatrixEffectRotate( angle, AXIS[i], center);
80
      }
81
    }
82 49f67f9b Leszek Koltunski
83 e844c116 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85
  RubikPyraminx(int size, Static4D quatCur, Static4D quatAcc, DistortedTexture texture, MeshRectangles mesh, DistortedEffects effects)
86
    {
87
    super(size,quatCur,quatAcc,texture,mesh,effects);
88
    }
89
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
92 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)
93 49f67f9b Leszek Koltunski
    {
94
    for(int i=0; i<n; i++)
95
      {
96
      mRotArray[i+index] = rot;
97
      array[i+index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
98
      x += dx;
99
      y += dy;
100
      z += dz;
101
      }
102
    }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  private int emitLowermost(float x, float y, float z, int n, Static3D[] array)
107
    {
108
    int added = 0;
109
110
    for(int i=n; i>=1; i--)
111
      {
112 89a11f7b Leszek Koltunski
      emitRow(x,y,z, 1,0,0, i, -1, array, added);
113 49f67f9b Leszek Koltunski
      added += i;
114
      x += 0.5f;
115
      y += 0.0f;
116
      z += SQ3/2;
117
      }
118
119
    return added;
120
    }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
  private int emitUpper(float x, float y, float z, int n, Static3D[] array, int index)
125
    {
126
    if( n>1 )
127
      {
128 89a11f7b Leszek Koltunski
      emitRow( x     , y, z      ,  1.0f, 0,     0, n-1, -1, array, index);
129 49f67f9b Leszek Koltunski
      index += (n-1);
130 89a11f7b Leszek Koltunski
      emitRow( x+0.5f, y, z+SQ3/2,  0.5f, 0, SQ3/2, n-1, -1, array, index);
131 49f67f9b Leszek Koltunski
      index += (n-1);
132 89a11f7b Leszek Koltunski
      emitRow( x+n-1 , y, z      , -0.5f, 0, SQ3/2, n-1, -1, array, index);
133 49f67f9b Leszek Koltunski
      index += (n-1);
134
      }
135
    else
136
      {
137 89a11f7b Leszek Koltunski
      mRotArray[index] = -1;
138 49f67f9b Leszek Koltunski
      array[index] = new Static3D(x+0.5f,y+SQ2*SQ3/12,z+SQ3/6);
139
      index++;
140
      }
141
142
    return index;
143
    }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
// size^2 + 3*(size-1) in the lowermost layer, then 6*(size-2) in the next, 6*(size-3) in the next,
147
// ... 6 in the forelast, 1 in the last = 4size^2 - 6size +4 (if size>1)
148
149 e844c116 Leszek Koltunski
  Static3D[] getCubitPositions(int size)
150
    {
151 49f67f9b Leszek Koltunski
    int numCubits = size>1 ? 2*size*size-4*size+4:1;//4*size*size - 6*size +4 : 1;
152 c2cb520d Leszek Koltunski
    Static3D[] tmp = new Static3D[numCubits];
153 89a11f7b Leszek Koltunski
    mRotArray = new int[numCubits];
154 49f67f9b Leszek Koltunski
155
    int currentIndex = emitLowermost( -0.5f*size, -(SQ2*SQ3/12)*size, -(SQ3/6)*size, size, tmp);
156 c2cb520d Leszek Koltunski
157 49f67f9b Leszek Koltunski
    for(int i=size-1; i>=1; i--)
158 cc7dc72a Leszek Koltunski
      {
159 49f67f9b Leszek Koltunski
      currentIndex = emitUpper( -0.5f*i, ((SQ2*SQ3)/12)*(3*size-4*i), -(SQ3/6)*i, i, tmp, currentIndex);
160 cc7dc72a Leszek Koltunski
      }
161 49f67f9b Leszek Koltunski
162 c2cb520d Leszek Koltunski
    return tmp;
163 e844c116 Leszek Koltunski
    }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
  float[] getLegalQuats()
168
    {
169
    return LEGALQUATS;
170
    }
171
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
  int getNumFaces()
175
    {
176
    return FACE_COLORS.length;
177
    }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
  Static3D[] getRotationAxis()
182
    {
183
    return AXIS;
184
    }
185
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
  void createFaceTexture(Canvas canvas, Paint paint, int face)
189
    {
190 cc7dc72a Leszek Koltunski
    int xoffset = face*TEXTURE_HEIGHT;
191
    float STROKE = 0.05f*TEXTURE_HEIGHT;
192
    float OFF = STROKE/2 -1;
193 49f67f9b Leszek Koltunski
    float OFF2 = 0.5f*TEXTURE_HEIGHT + OFF;
194 cc7dc72a Leszek Koltunski
    float HEIGHT = TEXTURE_HEIGHT - OFF;
195
    float RADIUS = TEXTURE_HEIGHT/12;
196 49f67f9b Leszek Koltunski
    float ARC1_H = 0.2f*TEXTURE_HEIGHT;
197 cc7dc72a Leszek Koltunski
    float ARC1_W = TEXTURE_HEIGHT*0.5f;
198 49f67f9b Leszek Koltunski
    float ARC2_W = 0.153f*TEXTURE_HEIGHT;
199
    float ARC2_H = 0.905f*TEXTURE_HEIGHT;
200
    float ARC3_W = TEXTURE_HEIGHT-ARC2_W;
201 cc7dc72a Leszek Koltunski
202
    paint.setAntiAlias(true);
203
    paint.setStrokeWidth(STROKE);
204
    paint.setColor(FACE_COLORS[face]);
205
    paint.setStyle(Paint.Style.FILL);
206 97d2f701 Leszek Koltunski
207 cc7dc72a Leszek Koltunski
    canvas.drawRect(xoffset,0,xoffset+TEXTURE_HEIGHT,TEXTURE_HEIGHT,paint);
208 97d2f701 Leszek Koltunski
209 cc7dc72a Leszek Koltunski
    paint.setColor(0xff000000);
210
    paint.setStyle(Paint.Style.STROKE);
211
212
    canvas.drawLine(                     xoffset,         HEIGHT,  TEXTURE_HEIGHT       +xoffset, HEIGHT, paint);
213
    canvas.drawLine(                OFF +xoffset, TEXTURE_HEIGHT,                 OFF2  +xoffset,      0, paint);
214
    canvas.drawLine((TEXTURE_HEIGHT-OFF)+xoffset, TEXTURE_HEIGHT, (TEXTURE_HEIGHT-OFF2) +xoffset,      0, paint);
215
216
    canvas.drawArc( ARC1_W-RADIUS+xoffset, ARC1_H-RADIUS, ARC1_W+RADIUS+xoffset, ARC1_H+RADIUS, 225, 90, false, paint);
217
    canvas.drawArc( ARC2_W-RADIUS+xoffset, ARC2_H-RADIUS, ARC2_W+RADIUS+xoffset, ARC2_H+RADIUS, 105, 90, false, paint);
218
    canvas.drawArc( ARC3_W-RADIUS+xoffset, ARC2_H-RADIUS, ARC3_W+RADIUS+xoffset, ARC2_H+RADIUS, 345, 90, false, paint);
219 e844c116 Leszek Koltunski
    }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
223 89a11f7b Leszek Koltunski
  MeshBase createCubitMesh(int cubit, int vertices)
224 e844c116 Leszek Koltunski
    {
225 988f434e Leszek Koltunski
    final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
226
    final int MESHES=4;
227
228
    MeshBase[] meshes = new MeshTriangles[MESHES];
229
    for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(5);
230
231
    MatrixEffect[] effects0 = new MatrixEffect[3];
232
    effects0[0] = new MatrixEffectScale( new Static3D(1,SQ3/2,1) );
233
    effects0[1] = new MatrixEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
234
    effects0[2] = new MatrixEffectMove( new Static3D(0,-SQ3/6,SQ3/12) );
235
236
    meshes[0].apply(effects0);
237
238
    Static1D angle = new Static1D(angleFaces);
239
    Static3D axis  = new Static3D(-1,0,0);
240
    Static3D center= new Static3D(0,-SQ3/6,-SQ3/6);
241
242
    MatrixEffect[] effects1 = new MatrixEffect[5];
243
    effects1[0] = effects0[0];
244
    effects1[1] = effects0[1];
245
    effects1[2] = effects0[2];
246
    effects1[3] = new MatrixEffectRotate( new Static1D(180), new Static3D(0,0,1), center );
247
    effects1[4] = new MatrixEffectRotate( angle, axis, center );
248
    meshes[1].apply(effects1);
249
250
    axis.set(0.5f,0,-SQ3/2);
251
    center.set2(SQ3/3);
252
    meshes[2].apply(effects1);
253
254
    axis.set2(SQ3/2);
255
    meshes[3].apply(effects1);
256
257 89a11f7b Leszek Koltunski
    MeshJoined result = new MeshJoined(meshes);
258
259
    if( mRotArray[cubit]>=0 )
260
      {
261
      result.apply( ROTATION[mRotArray[cubit]] );
262
      }
263
264
    return result;
265 e844c116 Leszek Koltunski
    }
266
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
// PUBLIC API
269
270
  public int getBasicAngle()
271
    {
272
    return 3;
273
    }
274
}