Project

General

Profile

Download (17.2 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / objects / TwistyMegaminx.java @ d0e6cf7f

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

    
22
import static org.distorted.objectlib.touchcontrol.TouchControlDodecahedron.COS54;
23
import static org.distorted.objectlib.touchcontrol.TouchControlDodecahedron.SIN54;
24

    
25
import java.io.InputStream;
26

    
27
import org.distorted.library.type.Static3D;
28
import org.distorted.library.type.Static4D;
29
import org.distorted.library.main.QuatHelper;
30

    
31
import org.distorted.objectlib.helpers.ObjectFaceShape;
32
import org.distorted.objectlib.main.ObjectControl;
33
import org.distorted.objectlib.main.ObjectType;
34
import org.distorted.objectlib.helpers.ObjectShape;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class TwistyMegaminx extends TwistyMinx
39
{
40
  static final float MEGA_D = 0.04f;
41
  private int[] mQuatCenterIndices;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
  public TwistyMegaminx(int[] numL, int meshState, Static4D quat, Static3D move, float scale, InputStream stream)
46
    {
47
    super(numL, meshState, quat, move, scale, stream);
48
    }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  private void initializeCenterIndices()
53
    {
54
    mQuatCenterIndices = new int[] { 16, 18, 22,  1, 20, 13, 14, 15,  0, 12,  2,  3 };
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  private int numCubitsPerCorner(int numLayers)
60
    {
61
    return 3*((numLayers-1)/2)*((numLayers-3)/2) + 1;
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  private int numCubitsPerEdge(int numLayers)
67
    {
68
    return numLayers-2;
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  public float[][] getCuts(int[] numLayers)
74
    {
75
    return genericGetCuts(numLayers[0],0.5f-MEGA_D);
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  private float[] computeCenter(int center, int numLayers)
81
    {
82
    if( mCenterCoords==null ) initializeCenterCoords();
83
    float[] coords = mCenterCoords[center];
84
    float A = (float)numLayers/3;
85

    
86
    return new float[] { A*coords[0], A*coords[1], A*coords[2] };
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
// Fill out mCurrCorner{X,Y,Z} by applying appropriate Quat to mBasicCorner{X,Y,Z}
91
// Appropriate one: QUATS[QUAT_INDICES[corner]].
92

    
93
  private void computeBasicCornerVectors(int corner)
94
    {
95
    if( mQuatCornerIndices==null ) initializeQuatIndices();
96
    if( mQuats==null ) initializeQuats();
97
    if( mCurrCornerV==null || mBasicCornerV==null ) initializeCornerV();
98

    
99
    Static4D quat = mQuats[mQuatCornerIndices[corner]];
100

    
101
    mCurrCornerV[0] = QuatHelper.rotateVectorByQuat(mBasicCornerV[0],quat);
102
    mCurrCornerV[1] = QuatHelper.rotateVectorByQuat(mBasicCornerV[1],quat);
103
    mCurrCornerV[2] = QuatHelper.rotateVectorByQuat(mBasicCornerV[2],quat);
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private float[] computeCorner(int numCubitsPerCorner, int numLayers, int corner, int part)
109
    {
110
    if( mCorners==null ) initializeCorners();
111
    if( mCurrCornerV==null || mBasicCornerV==null ) initializeCornerV();
112

    
113
    float D = numLayers/3.0f;
114
    float[] corn = mCorners[corner];
115

    
116
    if( part==0 )
117
      {
118
      return new float[] { corn[0]*D, corn[1]*D, corn[2]*D };
119
      }
120
    else
121
      {
122
      float E = 2.0f*D*(0.5f-MEGA_D)/(0.5f*(numLayers-1));
123
      int N = (numCubitsPerCorner-1)/3;
124
      int block = (part-1) % N;
125
      int index = (part-1) / N;
126
      Static4D pri = mCurrCornerV[index];
127
      Static4D sec = mCurrCornerV[(index+2)%3];
128

    
129
      int layers= (numLayers-3)/2;
130
      int multP = (block % layers) + 1;
131
      int multS = (block / layers);
132

    
133
      return new float[] {
134
                          corn[0]*D + (pri.get0()*multP + sec.get0()*multS)*E,
135
                          corn[1]*D + (pri.get1()*multP + sec.get1()*multS)*E,
136
                          corn[2]*D + (pri.get2()*multP + sec.get2()*multS)*E
137
                         };
138
      }
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  private int computeEdgeType(int cubit, int numCubitsPerCorner, int numCubitsPerEdge)
144
    {
145
    int part = (cubit - NUM_CORNERS*numCubitsPerCorner) % numCubitsPerEdge;
146
    return (part+1)/2;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
  private float[] computeEdge(int numLayers, int edge, int part)
152
    {
153
    if( mCenterCoords==null ) initializeCenterCoords();
154
    if( mCorners==null ) initializeCorners();
155
    if( mEdgeMap==null ) initializeEdgeMap();
156

    
157
    float D = numLayers/3.0f;
158
    float[] c1 = mCorners[ mEdgeMap[edge][0] ];
159
    float[] c2 = mCorners[ mEdgeMap[edge][1] ];
160
    float x = D * (c1[0]+c2[0]) / 2;
161
    float y = D * (c1[1]+c2[1]) / 2;
162
    float z = D * (c1[2]+c2[2]) / 2;
163

    
164
    if( part==0 )
165
      {
166
      return new float[] { x, y, z };
167
      }
168
    else
169
      {
170
      int mult = (part+1)/2;
171
      int dir  = (part+1)%2;
172
      float[] center = mCenterCoords[ mEdgeMap[edge][dir+2] ];
173

    
174
      float vX = D*center[0] - x;
175
      float vY = D*center[1] - y;
176
      float vZ = D*center[2] - z;
177

    
178
      float A = 3*mult*D*(0.5f-MEGA_D)*COS18/((numLayers-1)*0.5f);
179
      A /= (float)Math.sqrt(vX*vX+vY*vY+vZ*vZ);
180

    
181
      return new float[] { x+A*vX, y+A*vY, z+A*vZ };
182
      }
183
    }
184

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

    
187
  public float[][] getCubitPositions(int[] numLayers)
188
    {
189
    int numL = numLayers[0];
190
    int numCubitsPerCorner = numCubitsPerCorner(numL);
191
    int numCubitsPerEdge   = numCubitsPerEdge(numL);
192
    int numCubits = NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge + NUM_CENTERS;
193
    int index=0;
194

    
195
    final float[][] CENTERS = new float[numCubits][];
196

    
197
    for(int corner=0; corner<NUM_CORNERS; corner++)
198
      {
199
      computeBasicCornerVectors(corner);
200

    
201
      for(int part=0; part<numCubitsPerCorner; part++, index++)
202
        {
203
        CENTERS[index] = computeCorner(numCubitsPerCorner,numL,corner,part);
204
        }
205
      }
206

    
207
    for(int edge=0; edge<NUM_EDGES; edge++)
208
      {
209
      for(int part=0; part<numCubitsPerEdge; part++, index++)
210
        {
211
        CENTERS[index] = computeEdge(numL, edge, part );
212
        }
213
      }
214

    
215
    for(int center=0; center<NUM_CENTERS; center++, index++)
216
      {
217
      CENTERS[index] = computeCenter(center, numL);
218
      }
219

    
220
    return CENTERS;
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  public Static4D getCubitQuats(int cubit, int[] numLayers)
226
    {
227
    if( mQuats==null ) initializeQuats();
228

    
229
    int numL = numLayers[0];
230
    int numCubitsPerCorner = numCubitsPerCorner(numL);
231
    int numCubitsPerEdge   = numCubitsPerEdge(numL);
232

    
233
    return mQuats[getQuat(cubit,numCubitsPerCorner,numCubitsPerEdge)];
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  private int getQuat(int cubit, int numCubitsPerCorner, int numCubitsPerEdge)
239
    {
240
    if( mQuatCornerIndices==null || mQuatEdgeIndices==null ) initializeQuatIndices();
241
    if( mQuatCenterIndices==null ) initializeCenterIndices();
242

    
243
    if( cubit < NUM_CORNERS*numCubitsPerCorner )
244
      {
245
      int corner = cubit/numCubitsPerCorner;
246
      return mQuatCornerIndices[corner];
247
      }
248

    
249
    if( cubit < NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge )
250
      {
251
      int edge = (cubit-NUM_CORNERS*numCubitsPerCorner)/numCubitsPerEdge;
252
      return mQuatEdgeIndices[edge];
253
      }
254

    
255
    int center = cubit - NUM_CORNERS*numCubitsPerCorner - NUM_EDGES*numCubitsPerEdge;
256
    return mQuatCenterIndices[center];
257
    }
258

    
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260

    
261
  public ObjectShape getObjectShape(int variant)
262
    {
263
    int[] numLayers = getNumLayers();
264
    int numVariants = getNumCubitVariants(numLayers);
265
    int numL        = numLayers[0];
266

    
267
    if( variant==0 )
268
      {
269
      float width = numL*(0.5f-MEGA_D)/(0.5f*(numL-1));
270
      float X = width*COS18*SIN_HALFD;
271
      float Y = width*SIN18;
272
      float Z = width*COS18*COS_HALFD;
273

    
274
      float[][] vertices =
275
        {
276
            {   0,   0      ,   0 },
277
            {   X,   Y      ,  -Z },
278
            {   0, 2*Y      ,-2*Z },
279
            {  -X,   Y      ,  -Z },
280
            {   0,   0-width,   0 },
281
            {   X,   Y-width,  -Z },
282
            {   0, 2*Y-width,-2*Z },
283
            {  -X,   Y-width,  -Z },
284
        };
285

    
286
      int[][] indices =
287
        {
288
            {4,5,1,0},
289
            {7,4,0,3},
290
            {0,1,2,3},
291
            {4,5,6,7},
292
            {6,5,1,2},
293
            {7,6,2,3}
294
        };
295

    
296
      return new ObjectShape(vertices, indices);
297
      }
298
    if( variant<numVariants-1 )
299
      {
300
      int type = variant-1;
301
      float height= numL*(0.5f-MEGA_D)*COS18/((numL-1)*0.5f);
302
      float width = numL*2*MEGA_D + 2*type*height*SIN18/COS18;
303

    
304
      float W = width/2;
305
      float X = height*SIN_HALFD;
306
      float Y = height*SIN18/COS18;
307
      float Z = height*COS_HALFD;
308

    
309
      float[][] vertices =
310
        {
311
            {   0,   W   ,   0 },
312
            {   X, W+Y   ,  -Z },
313
            {   0, W+2*Y ,-2*Z },
314
            {  -X, W+Y   ,  -Z },
315
            {   0,  -W   ,   0 },
316
            {   X,-W-Y   ,  -Z },
317
            {   0,-W-2*Y ,-2*Z },
318
            {  -X,-W-Y   ,  -Z },
319
        };
320

    
321
      int[][] indices =
322
        {
323
            {4,5,1,0},
324
            {7,4,0,3},
325
            {7,6,2,3},
326
            {6,5,1,2},
327
            {0,1,2,3},
328
            {4,5,6,7}
329
        };
330

    
331
      return new ObjectShape(vertices, indices);
332
      }
333
    else
334
      {
335
      float width = 2*numL*(MEGA_D+(0.5f-MEGA_D)*SIN18);
336
      final double ANGLE = 0.825f*Math.PI;
337
      final float cosA  = (float)Math.cos(ANGLE);
338
      final float sinA  = (float)Math.sin(ANGLE);
339

    
340
      float R  = 0.5f*width/COS54;
341
      float X1 = R*COS54;
342
      float Y1 = R*SIN54;
343
      float X2 = R*COS18;
344
      float Y2 = R*SIN18;
345

    
346
      float[][] vertices =
347
        {
348
          {-X1,+Y1*sinA, Y1*cosA},
349
          {-X2,-Y2*sinA,-Y2*cosA},
350
          { 0 ,-R*sinA ,-R*cosA },
351
          {+X2,-Y2*sinA,-Y2*cosA},
352
          {+X1,+Y1*sinA, Y1*cosA},
353
          { 0 , R*cosA ,-R*sinA }
354
        };
355

    
356
      int[][] indices =
357
        {
358
          {0,1,2,3,4},
359
          {0,1,5},
360
          {1,2,5},
361
          {2,3,5},
362
          {3,4,5},
363
          {4,0,5}
364
        };
365

    
366
      return new ObjectShape(vertices, indices);
367
      }
368
    }
369

    
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

    
372
  public ObjectFaceShape getObjectFaceShape(int variant)
373
    {
374
    int[] numLayers = getNumLayers();
375
    int numVariants = getNumCubitVariants(numLayers);
376
    int numL        = numLayers[0];
377
    boolean small   = numL<=3;
378

    
379
    if( variant==0 )
380
      {
381
      float A = (2*SQ3/3)*SIN54;
382
      float B = 0.4f;
383

    
384
      float[][] bands     = { {0.04f,34,0.3f,0.2f, 3, 0, 0}, {0.00f, 0,0.0f,0.0f, 2, 0, 0} };
385
      int[] bandIndices   = { 0,0,0,1,1,1};
386
      float[][] corners   = { {0.04f,0.10f} };
387
      int[] cornerIndices = { 0,-1,-1,-1,-1,-1,-1,-1 };
388
      float[][] centers   = { {0.0f, -(float)Math.sqrt(1-A*A)*B,-A*B} };
389
      int[] centerIndices = { 0,-1,-1,-1,-1,-1,-1,-1 };
390

    
391
      return new ObjectFaceShape(bands,bandIndices,corners,cornerIndices,centers,centerIndices,null);
392
      }
393
    if( variant<numVariants-1 )
394
      {
395
      float height= numL*(0.5f-MEGA_D)*COS18/((numL-1)*0.5f);
396
      float Z = height*COS_HALFD;
397
      int N = small ? 5 : 3;
398

    
399
      float[][] bands     = { {0.04f,34,0.2f,0.2f,N,0,0},{0.00f,34,0.3f,0.2f,2,0,0} };
400
      int[] bandIndices   = { 0,0,1,1,1,1};
401
      float[][] corners   = { {0.04f,0.10f} };
402
      int[] cornerIndices = { -1,-1,-1,-1, -1,-1,-1,-1 };
403
      float[][] centers   = { {0.0f, 0.0f, -2*Z} };
404
      int[] centerIndices = { -1,-1,-1,-1, -1,-1,-1,-1 };
405

    
406
      return new ObjectFaceShape(bands,bandIndices,corners,cornerIndices,centers,centerIndices,null);
407
      }
408
    else
409
      {
410
      float width = 2*numL*(MEGA_D+(0.5f-MEGA_D)*SIN18);
411
      float R  = 0.5f*width/COS54;
412
      int N = small ? 4 : 3;
413
      float h = small ? 0.04f : 0.015f;
414

    
415
      float[][] bands     = { { h,45, R/3,0.2f,N,0,0},{0.00f,45, R/3,0.2f,2,0,0} };
416
      int[] bandIndices   = { 0,1,1,1,1,1 };
417
      float[][] corners   = { {0.04f,0.10f} };
418
      int[] cornerIndices = { -1,-1,-1,-1, -1,-1 };
419
      float[][] centers   = { {0.0f, 0.0f, 0.0f} };
420
      int[] centerIndices = { -1,-1,-1,-1, -1,-1 };
421

    
422
      return new ObjectFaceShape(bands,bandIndices,corners,cornerIndices,centers,centerIndices,null);
423
      }
424
    }
425

    
426
///////////////////////////////////////////////////////////////////////////////////////////////////
427

    
428
  public int getNumCubitVariants(int[] numLayers)
429
    {
430
    return 2 + numLayers[0]/2;
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434

    
435
  public int getCubitVariant(int cubit, int[] numLayers)
436
    {
437
    int numL = numLayers[0];
438
    int numCubitsPerCorner = numCubitsPerCorner(numL);
439

    
440
    if( cubit<NUM_CORNERS*numCubitsPerCorner ) return 0;
441

    
442
    int numCubitsPerEdge = numCubitsPerEdge(numL);
443

    
444
    if( cubit<NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge )
445
      {
446
      int type = computeEdgeType(cubit,numCubitsPerCorner,numCubitsPerEdge);
447
      return type+1;
448
      }
449

    
450
    return getNumCubitVariants(numLayers)-1;
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  public float getStickerRadius()
456
    {
457
    return 0.13f;
458
    }
459

    
460
///////////////////////////////////////////////////////////////////////////////////////////////////
461

    
462
  public float getStickerStroke()
463
    {
464
    float stroke = 0.18f;
465

    
466
    if( ObjectControl.isInIconMode() )
467
      {
468
      int[] numLayers = getNumLayers();
469
      stroke*= ( numLayers[0]==3 ? 1.5f : 2.2f );
470
      }
471

    
472
    return stroke;
473
    }
474

    
475
///////////////////////////////////////////////////////////////////////////////////////////////////
476

    
477
  public float[][] getStickerAngles()
478
    {
479
    return null;
480
    }
481

    
482
///////////////////////////////////////////////////////////////////////////////////////////////////
483

    
484
  public ObjectType intGetObjectType(int[] numLayers)
485
    {
486
    switch(numLayers[0])
487
      {
488
      case 3: return ObjectType.MEGA_3;
489
      case 5: return ObjectType.MEGA_5;
490
      }
491
    return ObjectType.MEGA_3;
492
    }
493

    
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495

    
496
  public String getObjectName()
497
    {
498
    switch(getNumLayers()[0])
499
      {
500
      case 3: return "Megaminx";
501
      case 5: return "Gigaminx";
502
      }
503
    return "Megaminx";
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507

    
508
  public String getInventor()
509
    {
510
    switch(getNumLayers()[0])
511
      {
512
      case 3: return "Ferenc Szlivka";
513
      case 5: return "Tyler Fox";
514
      }
515
    return "Ferenc Szlivka";
516
    }
517

    
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519

    
520
  public int getYearOfInvention()
521
    {
522
    switch(getNumLayers()[0])
523
      {
524
      case 3: return 1982;
525
      case 5: return 2006;
526
      }
527
    return 2006;
528
    }
529

    
530
///////////////////////////////////////////////////////////////////////////////////////////////////
531

    
532
  public int getComplexity()
533
    {
534
    switch(getNumLayers()[0])
535
      {
536
      case 3: return 3;
537
      case 5: return 4;
538
      }
539
    return 9;
540
    }
541
}
(15-15/26)