Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / objects / TwistyKilominx.java @ 59a971c1

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 TwistyKilominx extends TwistyMinx
39
{
40
  public TwistyKilominx(int[] numL, int meshState, Static4D quat, Static3D move, float scale, InputStream stream)
41
    {
42
    super(numL, meshState, quat, move, scale, stream);
43
    }
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  private int numCubitsPerCorner(int numLayers)
48
    {
49
    return 3*((numLayers-3)/2)*((numLayers-5)/2) + (numLayers<5 ? 0:1);
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  private int numCubitsPerEdge(int numLayers)
55
    {
56
    return numLayers<5 ? 0 : 2*(numLayers-4);
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  public float[][] getCuts(int[] numLayers)
62
    {
63
    return genericGetCuts(numLayers[0],0.5f);
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
// Fill out mCurrCorner{X,Y,Z} by applying appropriate Quat to mBasicCorner{X,Y,Z}
68
// Appropriate one: QUATS[QUAT_INDICES[corner]].
69

    
70
  private void computeBasicCornerVectors(int corner)
71
    {
72
    if( mQuatCornerIndices==null ) initializeQuatIndices();
73
    if( mQuats==null ) initializeQuats();
74
    if( mCurrCornerV==null || mBasicCornerV==null ) initializeCornerV();
75

    
76
    Static4D quat = mQuats[mQuatCornerIndices[corner]];
77

    
78
    mCurrCornerV[0] = QuatHelper.rotateVectorByQuat(mBasicCornerV[0],quat);
79
    mCurrCornerV[1] = QuatHelper.rotateVectorByQuat(mBasicCornerV[1],quat);
80
    mCurrCornerV[2] = QuatHelper.rotateVectorByQuat(mBasicCornerV[2],quat);
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  private float[] computeCorner(int numCubitsPerCorner, int numLayers, int corner, int part)
86
    {
87
    if( mCorners==null ) initializeCorners();
88
    if( mCurrCornerV==null || mBasicCornerV==null ) initializeCornerV();
89

    
90
    float D = numLayers/3.0f;
91
    float[] corn = mCorners[corner];
92

    
93
    if( part==0 )
94
      {
95
      return new float[] { corn[0]*D, corn[1]*D, corn[2]*D };
96
      }
97
    else
98
      {
99
      float E = D/(0.5f*(numLayers-1));   // ?? maybe 0.5*
100
      int N = (numCubitsPerCorner-1)/3;
101
      int block = (part-1) % N;
102
      int index = (part-1) / N;
103
      Static4D pri = mCurrCornerV[index];
104
      Static4D sec = mCurrCornerV[(index+2)%3];
105

    
106
      int layers= (numLayers-5)/2;
107
      int multP = (block % layers) + 1;
108
      int multS = (block / layers);
109

    
110
      return new float[] {
111
                          corn[0]*D + (pri.get0()*multP + sec.get0()*multS)*E,
112
                          corn[1]*D + (pri.get1()*multP + sec.get1()*multS)*E,
113
                          corn[2]*D + (pri.get2()*multP + sec.get2()*multS)*E
114
                         };
115
      }
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  private float[] computeCenter(int numLayers, int center, int part)
121
    {
122
    if( mCenterCoords==null ) initializeCenterCoords();
123
    if( mCorners     ==null ) initializeCorners();
124
    if( mCenterMap   ==null ) initializeCenterMap();
125

    
126
    int corner = mCenterMap[center][part];
127
    float[] cent = mCenterCoords[center];
128
    float[] corn = mCorners[corner];
129
    float D = numLayers/3.0f;
130
    float F = 1.0f - (2.0f*numLayers-6.0f)/(numLayers-1)*COS54*COS54;
131

    
132
    return new float[]
133
      {
134
        D * ( cent[0] + (corn[0]-cent[0])*F),
135
        D * ( cent[1] + (corn[1]-cent[1])*F),
136
        D * ( cent[2] + (corn[2]-cent[2])*F)
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 - 2*(part/4);
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

    
160
    int leftRight = 2*(part%2) -1;
161
    part /= 2;
162

    
163
    if( part==0 )
164
      {
165
      float T = 0.5f + leftRight/(numLayers-1.0f);
166
      float x = D * (T*c1[0]+(1.0f-T)*c2[0]);
167
      float y = D * (T*c1[1]+(1.0f-T)*c2[1]);
168
      float z = D * (T*c1[2]+(1.0f-T)*c2[2]);
169

    
170
      return new float[] { x, y, z };
171
      }
172
    else
173
      {
174
      int mult = (part+1)/2;
175
      int dir  = (part+1)%2;
176
      float[] center = mCenterCoords[ mEdgeMap[edge][dir+2] ];
177
      float x = 0.5f * D * (c1[0]+c2[0]);
178
      float y = 0.5f * D * (c1[1]+c2[1]);
179
      float z = 0.5f * D * (c1[2]+c2[2]);
180

    
181
      float vX = D*center[0] - x;
182
      float vY = D*center[1] - y;
183
      float vZ = D*center[2] - z;
184

    
185
      float T = 0.5f + leftRight*(mult*SIN18 + 1.0f)/(numLayers-1);
186

    
187
      x = D * (T*c1[0]+(1.0f-T)*c2[0]);
188
      y = D * (T*c1[1]+(1.0f-T)*c2[1]);
189
      z = D * (T*c1[2]+(1.0f-T)*c2[2]);
190

    
191
      float H = mult*D*COS18/(numLayers-1);
192
      H /= (float)Math.sqrt(vX*vX+vY*vY+vZ*vZ);
193

    
194
      return new float[] { x + H*vX, y + H*vY, z + H*vZ };
195
      }
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  public float[][] getCubitPositions(int[] numLayers)
201
    {
202
    if( mCorners==null ) initializeCorners();
203

    
204
    int numL = numLayers[0];
205
    if( numL<5 ) return mCorners;
206

    
207
    int numCubitsPerCorner = numCubitsPerCorner(numL);
208
    int numCubitsPerEdge   = numCubitsPerEdge(numL);
209
    int numCubitsPerCenter = 5;
210
    int numCubits = NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge + NUM_CENTERS*numCubitsPerCenter;
211
    int index=0;
212

    
213
    final float[][] CENTERS = new float[numCubits][];
214

    
215
    for(int corner=0; corner<NUM_CORNERS; corner++)
216
      {
217
      computeBasicCornerVectors(corner);
218

    
219
      for(int part=0; part<numCubitsPerCorner; part++, index++)
220
        {
221
        CENTERS[index] = computeCorner(numCubitsPerCorner,numL,corner,part);
222
        }
223
      }
224

    
225
    for(int edge=0; edge<NUM_EDGES; edge++)
226
      {
227
      for(int part=0; part<numCubitsPerEdge; part++, index++)
228
        {
229
        CENTERS[index] = computeEdge(numL, edge, part );
230
        }
231
      }
232

    
233
    for(int center=0; center<NUM_CENTERS; center++)
234
      {
235
      for(int part=0; part<numCubitsPerCenter; part++, index++)
236
        {
237
        CENTERS[index] = computeCenter(numL,center, part);
238
        }
239
      }
240

    
241
    return CENTERS;
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  private int getQuat(int cubit, int numCubitsPerCorner, int numCubitsPerEdge)
247
    {
248
    if( mQuatCornerIndices==null || mQuatEdgeIndices==null ) initializeQuatIndices();
249
    if( mCenterMap==null ) initializeCenterMap();
250

    
251
    if( cubit < NUM_CORNERS*numCubitsPerCorner )
252
      {
253
      int corner = cubit/numCubitsPerCorner;
254
      return mQuatCornerIndices[corner];
255
      }
256

    
257
    if( cubit < NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge )
258
      {
259
      int edge = (cubit-NUM_CORNERS*numCubitsPerCorner)/numCubitsPerEdge;
260
      return mQuatEdgeIndices[edge];
261
      }
262

    
263
    if( numCubitsPerCorner==0 )
264
      {
265
      return mQuatCornerIndices[cubit];
266
      }
267
    else
268
      {
269
      cubit -= (NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge);
270
      int numCubitsPerCenter = 5;
271
      int face = cubit/numCubitsPerCenter;
272
      int index= cubit%numCubitsPerCenter;
273
      int center=mCenterMap[face][index];
274
      return mQuatCornerIndices[center];
275
      }
276
    }
277

    
278
///////////////////////////////////////////////////////////////////////////////////////////////////
279

    
280
  public ObjectShape getObjectShape(int variant)
281
    {
282
    int[] numLayers = getNumLayers();
283
    int numVariants = getNumCubitVariants(numLayers);
284
    int numL        = numLayers[0];
285
    boolean small   = numL<=3;
286

    
287
    if( variant==0 && !small )
288
      {
289
      float width = numL/(numL-1.0f);
290
      float X = width*COS18*SIN_HALFD;
291
      float Y = width*SIN18;
292
      float Z = width*COS18*COS_HALFD;
293

    
294
      float[][] vertices =
295
        {
296
            {   0,   0      ,   0 },
297
            {   X,   Y      ,  -Z },
298
            {   0, 2*Y      ,-2*Z },
299
            {  -X,   Y      ,  -Z },
300
            {   0,   0-width,   0 },
301
            {   X,   Y-width,  -Z },
302
            {   0, 2*Y-width,-2*Z },
303
            {  -X,   Y-width,  -Z },
304
        };
305

    
306
      int[][] indices =
307
        {
308
            {4,5,1,0},
309
            {7,4,0,3},
310
            {0,1,2,3},
311
            {4,5,6,7},
312
            {6,5,1,2},
313
            {7,6,2,3}
314
        };
315

    
316
      return new ObjectShape(vertices, indices);
317
      }
318
    if( variant<numVariants-1 )
319
      {
320
      int type = variant-1;
321
      float tmpVal= numL/(numL-1.0f);
322
      float height= tmpVal*COS18;
323
      float width = tmpVal + (type/2)*tmpVal*SIN18;
324
      boolean left = (type%2)==0;
325

    
326
      float X = height*SIN_HALFD;
327
      float Y = height*SIN18/COS18;
328
      float Z = height*COS_HALFD;
329

    
330
      float[][] vertices =
331
        {
332
            {   0,   0   ,   0 },
333
            {   X,   Y   ,  -Z },
334
            {   0, 2*Y   ,-2*Z },
335
            {  -X,   Y   ,  -Z },
336
            {   0, -width,   0 },
337
            {   X, -width,  -Z },
338
            {   0, -width,-2*Z },
339
            {  -X, -width,  -Z },
340
        };
341

    
342
      int[][] indices =
343
        {
344
            {4,5,1,0},
345
            {7,4,0,3},
346
            {7,6,2,3},
347
            {6,5,1,2},
348
            {0,1,2,3},
349
            {4,5,6,7}
350
        };
351

    
352
      if( !left )
353
        {
354
        int tmp, len = vertices.length;
355
        for(int i=0; i<len; i++) vertices[i][1] = -vertices[i][1];
356

    
357
        len = indices.length;
358
        for(int i=0; i<len; i++)
359
          {
360
          tmp = indices[i][0];
361
          indices[i][0] = indices[i][3];
362
          indices[i][3] = tmp;
363
          tmp = indices[i][1];
364
          indices[i][1] = indices[i][2];
365
          indices[i][2] = tmp;
366
          }
367
        }
368

    
369
      return new ObjectShape(vertices, indices);
370
      }
371
    else
372
      {
373
      float width = (1+0.5f*(numL-3)*SIN18)*numL/(numL-1);
374
      float X = width*COS18*SIN_HALFD;
375
      float Y = width*SIN18;
376
      float Z = width*COS18*COS_HALFD;
377
      float H = width*(SIN54/COS54);
378
      float H3= H/COS_HALFD;
379
      float X3= H*SIN_HALFD;
380
      float Z3= H*COS_HALFD;
381
      float C = 1/(COS54*(float)Math.sqrt(2-2*SIN18));
382

    
383
      float[][] vertices =
384
        {
385
            {   0,   0  ,     0 },
386
            {   X,   Y  ,    -Z },
387
            {   0,C*2*Y ,-2*C*Z },
388
            {  -X,   Y  ,    -Z },
389
            {   0,-width,     0 },
390
            {  X3,-width,   -Z3 },
391
            {   0,-width,   -H3 },
392
            { -X3,-width,   -Z3 }
393
        };
394

    
395
      int[][] indices =
396
        {
397
            {4,5,1,0},
398
            {7,4,0,3},
399
            {0,1,2,3},
400
            {7,6,2,3},
401
            {6,5,1,2},
402
            {4,5,6,7}
403
        };
404

    
405
      return new ObjectShape(vertices, indices);
406
      }
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  public ObjectFaceShape getObjectFaceShape(int variant)
412
    {
413
    int[] numLayers = getNumLayers();
414
    int numVariants = getNumCubitVariants(numLayers);
415
    int numL        = numLayers[0];
416
    boolean small   = numL<=3;
417

    
418
    if( variant==0 && !small )
419
      {
420
      float A = (2*SQ3/3)*SIN54;
421
      float B = 0.4f;
422

    
423
      float[][] bands     = { {0.04f,34,0.3f,0.2f, 3, 0, 0}, {0.00f,34,0.0f,0.0f, 2, 0, 0} };
424
      int[] bandIndices   = { 0,0,0,1,1,1};
425
      float[][] corners   = { {0.04f,0.10f} };
426
      int[] cornerIndices = { 0,-1,-1,-1,-1,-1,-1,-1 };
427
      float[][] centers   = { {0.0f, -(float)Math.sqrt(1-A*A)*B,-A*B} };
428
      int[] centerIndices = { 0,-1,-1,-1,-1,-1,-1,-1 };
429

    
430
      return new ObjectFaceShape(bands,bandIndices,corners,cornerIndices,centers,centerIndices,null);
431
      }
432
    if( variant<numVariants-1 )
433
      {
434
      int type = variant-1;
435
      float tmpVal= numL/(numL-1.0f);
436
      float height= tmpVal*COS18;
437
      float width = tmpVal + (type/2)*tmpVal*SIN18;
438
      float Z = height*COS_HALFD;
439
      int E  = small ? 1 : 0;
440
      int N0 = small ? 4 : 3;
441
      int N1 = small ? 3 : 2;
442

    
443
      float[][] bands     = { {0.04f,34,0.2f,0.2f,N0,E,E}, {0.00f,34,0.0f,0.0f,N1,0,0} };
444
      int[] bandIndices   = { 0,0,1,1,1,1};
445
      float[][] corners   = { {0.04f,0.10f} };
446
      int[] cornerIndices = { 0,-1,-1,-1, 0,-1,-1,-1 };
447
      float[][] centers   = { {0.0f, -width/2, -2*Z} };
448
      int[] centerIndices = { 0,-1,-1,-1, 0,-1,-1,-1 };
449

    
450
      return new ObjectFaceShape(bands,bandIndices,corners,cornerIndices,centers,centerIndices,null);
451
      }
452
    else
453
      {
454
      float A = (2*SQ3/3)*SIN54;
455
      float B = 0.4f;
456
      int N = small ? 4 : 3;
457
      int E = small ? 1 : 0;
458

    
459
      float[][] bands     = { {0.04f,17,0.3f,0.2f,N,E,E},{0.00f,17,0.3f,0.2f,N,E,E} };
460
      int[] bandIndices   = { 0,0,0,1,1,1};
461
      float[][] corners   = { {0.03f,0.10f} };
462
      int[] cornerIndices = { 0, 0,-1, 0, 0,-1,-1,-1 };
463
      float[][] centers   = { {0.0f, -(float)Math.sqrt(1-A*A)*B,-A*B} };
464
      int[] centerIndices = { 0, 0,-1, 0, 0,-1,-1,-1 };
465

    
466
      return new ObjectFaceShape(bands,bandIndices,corners,cornerIndices,centers,centerIndices,null);
467
      }
468
    }
469

    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471

    
472
  public Static4D getQuat(int cubit, int[] numLayers)
473
    {
474
    if( mQuats==null ) initializeQuats();
475

    
476
    int numL = numLayers[0];
477
    int numCubitsPerCorner = numCubitsPerCorner(numL);
478
    int numCubitsPerEdge   = numCubitsPerEdge(numL);
479

    
480
    return mQuats[getQuat(cubit,numCubitsPerCorner,numCubitsPerEdge)];
481
    }
482

    
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484

    
485
  public int getNumCubitVariants(int[] numLayers)
486
    {
487
    switch(numLayers[0])
488
      {
489
      case 3: return 1;
490
      case 5: return 4;
491
      }
492

    
493
    return 1;
494
    }
495

    
496
///////////////////////////////////////////////////////////////////////////////////////////////////
497

    
498
  public int getCubitVariant(int cubit, int[] numLayers)
499
    {
500
    int numL = numLayers[0];
501
    int numCubitsPerCorner = numCubitsPerCorner(numL);
502

    
503
    if( cubit<NUM_CORNERS*numCubitsPerCorner ) return 0;
504

    
505
    int numCubitsPerEdge = numCubitsPerEdge(numL);
506

    
507
    if( cubit<NUM_CORNERS*numCubitsPerCorner + NUM_EDGES*numCubitsPerEdge )
508
      {
509
      int type = computeEdgeType(cubit,numCubitsPerCorner,numCubitsPerEdge);
510
      return type+1;
511
      }
512

    
513
    return getNumCubitVariants(numLayers)-1;
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
// make the 'center' sticker artificially larger, so that we paint over the area in the center of the face.
518

    
519
  public void adjustStickerCoords()
520
    {
521
    int[] numLayers = getNumLayers();
522
    int index = numLayers[0]==3 ? 0:2;
523
    float CENTER_CORR = 0.87f;
524

    
525
    mStickerCoords[index][2] *= CENTER_CORR;
526
    mStickerCoords[index][3] *= CENTER_CORR;
527
    }
528

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

    
531
  public float getStickerRadius()
532
    {
533
    return 0.18f;
534
    }
535

    
536
///////////////////////////////////////////////////////////////////////////////////////////////////
537

    
538
  public float getStickerStroke()
539
    {
540
    float stroke = 0.25f;
541

    
542
    if( ObjectControl.isInIconMode() )
543
      {
544
      int[] numLayers = getNumLayers();
545
      if( numLayers[0]>3 ) stroke*=1.5f;
546
      }
547

    
548
    return stroke;
549
    }
550

    
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552

    
553
  public float[][] getStickerAngles()
554
    {
555
    return null;
556
    }
557

    
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559

    
560
  public ObjectType intGetObjectType(int[] numLayers)
561
    {
562
    switch(numLayers[0])
563
      {
564
      case 3: return ObjectType.KILO_3;
565
      case 5: return ObjectType.KILO_5;
566
      }
567

    
568
    return ObjectType.KILO_3;
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572

    
573
  public String getObjectName()
574
    {
575
    switch(getNumLayers()[0])
576
      {
577
      case 3: return "Kilominx";
578
      case 5: return "Master Kilominx";
579
      }
580
    return "Kilominx";
581
    }
582

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584

    
585
  public String getInventor()
586
    {
587
    switch(getNumLayers()[0])
588
      {
589
      case 3: return "Thomas de Bruin";
590
      case 5: return "David Gugl";
591
      }
592
    return "Thomas de Bruin";
593
    }
594

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////
596

    
597
  public int getYearOfInvention()
598
    {
599
    switch(getNumLayers()[0])
600
      {
601
      case 3: return 2008;
602
      case 5: return 2010;
603
      }
604
    return 2008;
605
    }
606

    
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608

    
609
  public int getComplexity()
610
    {
611
    switch(getNumLayers()[0])
612
      {
613
      case 3: return 2;
614
      case 5: return 3;
615
      }
616
    return 8;
617
    }
618
}
(14-14/26)