Project

General

Profile

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

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

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[] { 35, 55, 38, 48, 41, 42, 58, 57,  0, 46, 29, 59 };
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( mCurrCornerV==null || mBasicCornerV==null ) initializeCornerV();
97

    
98
    Static4D quat = mObjectQuats[mQuatCornerIndices[corner]];
99

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

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

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

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

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

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

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

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

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

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

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

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

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

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

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

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

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

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

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

    
219
    return CENTERS;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  public Static4D getCubitQuats(int cubit, int[] numLayers)
225
    {
226
    int numL = numLayers[0];
227
    int numCubitsPerCorner = numCubitsPerCorner(numL);
228
    int numCubitsPerEdge   = numCubitsPerEdge(numL);
229

    
230
    return mObjectQuats[getQuat(cubit,numCubitsPerCorner,numCubitsPerEdge)];
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  private int getQuat(int cubit, int numCubitsPerCorner, int numCubitsPerEdge)
236
    {
237
    if( mQuatCornerIndices==null || mQuatEdgeIndices==null ) initializeQuatIndices();
238
    if( mQuatCenterIndices==null ) initializeCenterIndices();
239

    
240
    if( cubit < NUM_CORNERS*numCubitsPerCorner )
241
      {
242
      int corner = cubit/numCubitsPerCorner;
243
      return mQuatCornerIndices[corner];
244
      }
245

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

    
252
    int center = cubit - NUM_CORNERS*numCubitsPerCorner - NUM_EDGES*numCubitsPerEdge;
253
    return mQuatCenterIndices[center];
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public ObjectShape getObjectShape(int variant)
259
    {
260
    int[] numLayers = getNumLayers();
261
    int numVariants = getNumCubitVariants(numLayers);
262
    int numL        = numLayers[0];
263

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

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

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

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

    
301
      float W = width/2;
302
      float X = height*SIN_HALFD;
303
      float Y = height*SIN18/COS18;
304
      float Z = height*COS_HALFD;
305

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

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

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

    
337
      float R  = 0.5f*width/COS54;
338
      float X1 = R*COS54;
339
      float Y1 = R*SIN54;
340
      float X2 = R*COS18;
341
      float Y2 = R*SIN18;
342

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

    
353
      int[][] indices =
354
        {
355
          {0,1,2,3,4},
356
          {5,1,0},
357
          {5,2,1},
358
          {5,3,2},
359
          {5,4,3},
360
          {5,0,4}
361
        };
362

    
363
      return new ObjectShape(vertices, indices);
364
      }
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

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

    
376
    if( variant==0 )
377
      {
378
      float A = (2*SQ3/3)*SIN54;
379
      float B = 0.4f;
380

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

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

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

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

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

    
419
      return new ObjectFaceShape(bands,bandIndices,corners,cornerIndices,centers,centerIndices,null);
420
      }
421
    }
422

    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424

    
425
  public int getNumCubitVariants(int[] numLayers)
426
    {
427
    return 2 + numLayers[0]/2;
428
    }
429

    
430
///////////////////////////////////////////////////////////////////////////////////////////////////
431

    
432
  public int getCubitVariant(int cubit, int[] numLayers)
433
    {
434
    int numL = numLayers[0];
435
    int numCubitsPerCorner = numCubitsPerCorner(numL);
436

    
437
    if( cubit<NUM_CORNERS*numCubitsPerCorner ) return 0;
438

    
439
    int numCubitsPerEdge = numCubitsPerEdge(numL);
440

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

    
447
    return getNumCubitVariants(numLayers)-1;
448
    }
449

    
450
///////////////////////////////////////////////////////////////////////////////////////////////////
451

    
452
  public float getStickerRadius()
453
    {
454
    return 0.13f;
455
    }
456

    
457
///////////////////////////////////////////////////////////////////////////////////////////////////
458

    
459
  public float getStickerStroke()
460
    {
461
    float stroke = 0.18f;
462

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

    
469
    return stroke;
470
    }
471

    
472
///////////////////////////////////////////////////////////////////////////////////////////////////
473

    
474
  public float[][] getStickerAngles()
475
    {
476
    return null;
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480

    
481
  public String getShortName()
482
    {
483
    switch(getNumLayers()[0])
484
      {
485
      case 3: return ObjectType.MEGA_3.name();
486
      case 5: return ObjectType.MEGA_5.name();
487
      }
488

    
489
    return ObjectType.MEGA_3.name();
490
    }
491

    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493

    
494
  public long getSignature()
495
    {
496
    switch(getNumLayers()[0])
497
      {
498
      case 3: return ObjectType.MEGA_3.ordinal();
499
      case 5: return ObjectType.MEGA_5.ordinal();
500
      }
501

    
502
    return ObjectType.MEGA_3.ordinal();
503
    }
504

    
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506

    
507
  public String getObjectName()
508
    {
509
    switch(getNumLayers()[0])
510
      {
511
      case 3: return "Megaminx";
512
      case 5: return "Gigaminx";
513
      }
514
    return "Megaminx";
515
    }
516

    
517
///////////////////////////////////////////////////////////////////////////////////////////////////
518

    
519
  public String getInventor()
520
    {
521
    switch(getNumLayers()[0])
522
      {
523
      case 3: return "Ferenc Szlivka";
524
      case 5: return "Tyler Fox";
525
      }
526
    return "Ferenc Szlivka";
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530

    
531
  public int getYearOfInvention()
532
    {
533
    switch(getNumLayers()[0])
534
      {
535
      case 3: return 1982;
536
      case 5: return 2006;
537
      }
538
    return 2006;
539
    }
540

    
541
///////////////////////////////////////////////////////////////////////////////////////////////////
542

    
543
  public int getComplexity()
544
    {
545
    switch(getNumLayers()[0])
546
      {
547
      case 3: return 3;
548
      case 5: return 4;
549
      }
550
    return 9;
551
    }
552

    
553
///////////////////////////////////////////////////////////////////////////////////////////////////
554

    
555
  public String[][] getTutorials()
556
    {
557
    int[] numLayers = getNumLayers();
558

    
559
    switch(numLayers[0])
560
      {
561
      case 3: return new String[][] {
562
                          {"gb","j4x61L5Onzk","How to Solve the Megaminx","Z3"},
563
                          {"es","xuKbT6Il0Ko","Resolver Megaminx","Cuby"},
564
                          {"ru","WgoguOY3tKI","Как собрать Мегаминкс","Алексей Ярыгин"},
565
                          {"fr","Ln1vl85puKo","Résoudre le Megaminx","Victor Colin"},
566
                          {"de","d-GQD6CBdB8","Megaminx lösen","Pezcraft"},
567
                          {"pl","BZTW6ApeRZE","Jak ułożyć: Megaminx","DżoDżo"},
568
                          {"br","0BTzkDZW078","Como resolver o Megaminx 1/2","Pedro Filho"},
569
                          {"br","VVHzZI73BN0","Como resolver o Megaminx 2/2","Pedro Filho"},
570
                          {"kr","2NUsMclrD-0","메가밍크스 예시솔빙","iamzoone"},
571
                         };
572
      case 5: return new String[][] {
573
                          {"gb","MNBMm8BnHtQ","Solve the Gigaminx Part 1","BeardedCubing"},
574
                          {"gb","QrrP4GwqVMw","Solve the Gigaminx Part 2","BeardedCubing"},
575
                          {"es","ex5EQMBxV1U","Tutorial Gigaminx","RubikArt"},
576
                          {"ru","UJYK3SHjSGg","Как собрать Гигаминкс ч.1","Артем Мартиросов"},
577
                          {"ru","-iBCpr4Gwsw","Как собрать Гигаминкс ч.2","Артем Мартиросов"},
578
                          {"ru","4-dI7NCW8n8","Как собрать Гигаминкс ч.3","Артем Мартиросов"},
579
                          {"fr","e485fh0V1dg","Résolution du Gigaminx","Asthalis"},
580
                          {"de","APSAj4UtOAg","Megaminx 5x5 lösen","JamesKnopf"},
581
                          {"pl","qbKLMCX1wKg","Jak ułożyć Gigaminxa cz.1","chomik19751"},
582
                          {"pl","JQOXD3qleH4","Jak ułożyć Gigaminxa cz.2","chomik19751"},
583
                          {"pl","WF2katJ22FA","Jak ułożyć Gigaminxa cz.3","chomik19751"},
584
                          {"pl","jlyRrJjH4qQ","Jak ułożyć Gigaminxa cz.4","chomik19751"},
585
                          {"kr","HfPFrWuz6z4","기가밍크스 gigaminx","큐브놀이터"},
586
                         };
587
      }
588
    return null;
589
    }
590
}
(19-19/33)