Project

General

Profile

« Previous | Next » 

Revision 680f921e

Added by Leszek Koltunski about 3 years ago

Progress making the Diamond class support any size.

View differences:

src/main/java/org/distorted/objects/TwistyDiamond.java
78 78

  
79 79
  private static final float DIST = 0.50f;
80 80

  
81
  // centers of the 6 octahedrons + 8 tetrahedrons ( i.e. of the all 14 cubits)
82
  private static final float[][] CENTERS = new float[][]
83
         {
84
             { DIST,          0, DIST },
85
             { DIST,          0,-DIST },
86
             {-DIST,          0,-DIST },
87
             {-DIST,          0, DIST },
88
             {    0, DIST*SQ2  ,    0 },
89
             {    0,-DIST*SQ2  ,    0 },
90

  
91
             {    0, DIST*SQ2/2, DIST },
92
             { DIST, DIST*SQ2/2,    0 },
93
             {    0, DIST*SQ2/2,-DIST },
94
             {-DIST, DIST*SQ2/2,    0 },
95
             {    0,-DIST*SQ2/2, DIST },
96
             { DIST,-DIST*SQ2/2,    0 },
97
             {    0,-DIST*SQ2/2,-DIST },
98
             {-DIST,-DIST*SQ2/2,    0 }
99
         };
100

  
101 81
  // Colors of the faces of cubits. Each cubit has 8 faces
102
  private static final int[][] mFaceMap = new int[][]
82
  private static final int[][] mOctaFaceMap = new int[][]
103 83
         {
104 84
           { 6,1,8,8, 2,5,8,8 },
105 85
           { 8,1,3,8, 8,5,7,8 },
......
107 87
           { 6,8,8,4, 2,8,8,0 },
108 88
           { 6,1,3,4, 8,8,8,8 },
109 89
           { 8,8,8,8, 2,5,7,0 },
110

  
111
           { 6,8,8,8, 8,8,8,8 },
112
           { 1,8,8,8, 8,8,8,8 },
113
           { 3,8,8,8, 8,8,8,8 },
114
           { 4,8,8,8, 8,8,8,8 },
115
           { 2,8,8,8, 8,8,8,8 },
116
           { 5,8,8,8, 8,8,8,8 },
117
           { 7,8,8,8, 8,8,8,8 },
118
           { 0,8,8,8, 8,8,8,8 }
119 90
         };
120 91

  
92
  private static final int[] mTetraFaceMap = new int[] { 1, 3, 4, 6, 5, 7, 0, 2 };
93

  
121 94
  private static MeshBase mOctaMesh, mTetraMesh;
122 95

  
123 96
///////////////////////////////////////////////////////////////////////////////////////////////////
......
165 138

  
166 139
///////////////////////////////////////////////////////////////////////////////////////////////////
167 140

  
168
  float[] getCuts(int size)
141
  float[] getCuts(int numLayers)
169 142
    {
170
    float[] cuts = new float[1];
171
    cuts[0] = 0.0f;
172
    return cuts;
143
    if( numLayers<2 )
144
      {
145
      return null;
146
      }
147
    else
148
      {
149
      float[] cuts = new float[numLayers-1];
150
      float dist = SQ6*0.666f;
151
      float cut  = 0.5f*dist*(2-numLayers);
152

  
153
      for(int i=0; i<numLayers-1; i++)
154
        {
155
        cuts[i] = cut;
156
        cut += dist;
157
        }
158

  
159
      return cuts;
160
      }
173 161
    }
174 162

  
175 163
///////////////////////////////////////////////////////////////////////////////////////////////////
......
181 169

  
182 170
///////////////////////////////////////////////////////////////////////////////////////////////////
183 171

  
184
  float[][] getCubitPositions(int size)
172
  private int getNumOctahedrons(int layers)
173
    {
174
    return layers==1 ? 1 : 4*(layers-1)*(layers-1) + 2;
175
    }
176

  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

  
179
  private int getNumTetrahedrons(int layers)
180
    {
181
    return 4*layers*(layers-1);
182
    }
183

  
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

  
186
  private int createOctahedrons(float[][] centers, int index, int layers, float height)
187
    {
188
    float x = DIST*(layers-1);
189
    float z = DIST*(layers+1);
190

  
191
    for(int i=0; i<layers; i++, index++)
192
      {
193
      z -= 2*DIST;
194
      centers[index][0] = x;
195
      centers[index][1] = height;
196
      centers[index][2] = z;
197
      }
198

  
199
    for(int i=0; i<layers-1; i++, index++)
200
      {
201
      x -= 2*DIST;
202
      centers[index][0] = x;
203
      centers[index][1] = height;
204
      centers[index][2] = z;
205
      }
206

  
207
    for(int i=0; i<layers-1; i++, index++)
208
      {
209
      z += 2*DIST;
210
      centers[index][0] = x;
211
      centers[index][1] = height;
212
      centers[index][2] = z;
213
      }
214

  
215
    for(int i=0; i<layers-2; i++, index++)
216
      {
217
      x += 2*DIST;
218
      centers[index][0] = x;
219
      centers[index][1] = height;
220
      centers[index][2] = z;
221
      }
222

  
223
    return index;
224
    }
225

  
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

  
228
  private int createTetrahedrons(float[][] centers, int index, int layers, float height)
185 229
    {
230
    float x = DIST*(layers-1);
231
    float z = DIST*layers;
232

  
233
    for(int i=0; i<layers-1; i++, index++)
234
      {
235
      z -= 2*DIST;
236
      centers[index][0] = x;
237
      centers[index][1] = height;
238
      centers[index][2] = z;
239
      }
240

  
241
    x += DIST;
242
    z -= DIST;
243

  
244
    for(int i=0; i<layers-1; i++, index++)
245
      {
246
      x -= 2*DIST;
247
      centers[index][0] = x;
248
      centers[index][1] = height;
249
      centers[index][2] = z;
250
      }
251

  
252
    x -= DIST;
253
    z -= DIST;
254

  
255
    for(int i=0; i<layers-1; i++, index++)
256
      {
257
      z += 2*DIST;
258
      centers[index][0] = x;
259
      centers[index][1] = height;
260
      centers[index][2] = z;
261
      }
262

  
263
    x -= DIST;
264
    z += DIST;
265

  
266
    for(int i=0; i<layers-1; i++, index++)
267
      {
268
      x += 2*DIST;
269
      centers[index][0] = x;
270
      centers[index][1] = height;
271
      centers[index][2] = z;
272
      }
273

  
274
    return index;
275
    }
276

  
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

  
279
  float[][] getCubitPositions(int layers)
280
    {
281
    int numO = getNumOctahedrons(layers);
282
    int numT = getNumTetrahedrons(layers);
283
    int index = 0;
284
    float height = 0.0f;
285

  
286
    float[][] CENTERS = new float[numO+numT][3];
287

  
288
    index = createOctahedrons(CENTERS,index,layers,height);
289

  
290
    for(int i=layers-1; i>0; i--)
291
      {
292
      height += SQ2*DIST;
293
      index = createOctahedrons(CENTERS,index,i,+height);
294
      index = createOctahedrons(CENTERS,index,i,-height);
295
      }
296

  
297
    height = DIST*SQ2/2;
298

  
299
    for(int i=layers; i>1; i--)
300
      {
301
      index = createTetrahedrons(CENTERS,index,i,+height);
302
      index = createTetrahedrons(CENTERS,index,i,-height);
303
      height += SQ2*DIST;
304
      }
305

  
186 306
    return CENTERS;
187 307
    }
188 308

  
189 309
///////////////////////////////////////////////////////////////////////////////////////////////////
310
// TODO:
190 311

  
191
  private Static4D getQuat(int cubit)
312
  private int retFaceTetraBelongsTo(int tetra, int numLayers)
192 313
    {
193
    switch(cubit)
314
    switch(tetra)
194 315
      {
195
      case  0:
196
      case  1:
197
      case  2:
198
      case  3:
199
      case  4:
200
      case  5:
201
      case  6: return QUATS[0];                          // unit quat
202
      case  7: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
203
      case  8: return QUATS[1];                          // 180 along Y
204
      case  9: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along Y
205
      case 10: return new Static4D(0,     0,1,    0);    // 180 along Z
206
      case 11: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
207
      case 12: return new Static4D(     1,0,0,    0);    // 180 along X
208
      case 13: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
316
      case 0 : return 0;
317
      case 1 : return 1;
318
      case 2 : return 2;
319
      case 3 : return 3;
320
      case 4 : return 4;
321
      case 5 : return 5;
322
      case 6 : return 6;
323
      case 7 : return 7;
324
      default: return 8;
325
      }
326
    }
327

  
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

  
330
  private Static4D getQuat(int cubit, int numLayers, int numO)
331
    {
332
    if( cubit<numO ) return QUATS[0];
333

  
334
    switch( retFaceTetraBelongsTo(cubit-numO, numLayers) )
335
      {
336
      case 0: return new Static4D(0,-SQ2/2,0,SQ2/2);    //  90 along Y
337
      case 1: return QUATS[1];                          // 180 along Y
338
      case 2: return new Static4D(0,+SQ2/2,0,SQ2/2);    //  90 along Y
339
      case 3: return QUATS[0];                          // unit quat
340
      case 4: return new Static4D(SQ2/2, 0,SQ2/2,0);    //
341
      case 5: return new Static4D(     1,0,0,    0);    // 180 along X
342
      case 6: return new Static4D(-SQ2/2,0,SQ2/2,0);    //
343
      case 7: return new Static4D(0,     0,1,    0);    // 180 along Z
209 344
      }
210 345

  
211 346
    return null;
......
216 351
  MeshBase createCubitMesh(int cubit, int numLayers)
217 352
    {
218 353
    MeshBase mesh;
354
    int numO = getNumOctahedrons(numLayers);
219 355

  
220
    if( cubit<6 )
356
    if( cubit<numO )
221 357
      {
222 358
      if( mOctaMesh==null ) mOctaMesh = FactoryCubit.getInstance().createOctaMesh();
223 359
      mesh = mOctaMesh.copy(true);
......
228 364
      mesh = mTetraMesh.copy(true);
229 365
      }
230 366

  
231
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( getQuat(cubit), new Static3D(0,0,0) );
367
    Static4D sQ = getQuat(cubit,numLayers,numO);
368
    MatrixEffectQuaternion quat = new MatrixEffectQuaternion( sQ, new Static3D(0,0,0) );
232 369
    mesh.apply(quat,0xffffffff,0);
233 370

  
234 371
    return mesh;
235 372
    }
236 373

  
237 374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
// TODO
238 376

  
239
  int getFaceColor(int cubit, int cubitface, int size)
377
  int getFaceColor(int cubit, int cubitface, int numLayers)
240 378
    {
241
    return mFaceMap[cubit][cubitface];
379
    int numO = getNumOctahedrons(numLayers);
380

  
381
    if( cubit<numO )
382
      {
383
      return mOctaFaceMap[cubit][cubitface];
384
      }
385
    else
386
      {
387
      return cubitface>0 ? 8 : mTetraFaceMap[retFaceTetraBelongsTo(cubit-numO, numLayers)];
388
      }
242 389
    }
243 390

  
244 391
///////////////////////////////////////////////////////////////////////////////////////////////////
......
266 413

  
267 414
  float[] getRowChances(int numLayers)
268 415
    {
269
    float[] chances = new float[2];
416
    float[] chances = new float[numLayers];
270 417

  
271
    chances[0] = 0.5f;
272
    chances[1] = 1.0f;
418
    for(int i=0; i<numLayers; i++) chances[i] = ((float)(i+1))/numLayers;
273 419

  
274 420
    return chances;
275 421
    }
......
373 519
// 2) cubits 7,13: can also be QUAT 4,8
374 520
// 3) cubits 8,10: can also be QUAT 7,11
375 521
// 4) cubits 9,11: can also be QUAT 5,9
522
// TODO
376 523

  
377 524
  public boolean isSolved()
378 525
    {

Also available in: Unified diff