Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / TBSkewb.java @ 2d697abd

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.tablebases;
11

    
12
import static org.distorted.objectlib.main.TwistyObject.SQ3;
13

    
14
import android.content.res.Resources;
15

    
16
import org.distorted.library.type.Static3D;
17
import org.distorted.objectlib.R;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public class TBSkewb extends TablebasesAbstract
22
{
23
  private static final int[][] freeIndex = { {1,3,2},{0,2,3},{3,1,0},{2,0,1} };
24

    
25
  private static final int[][][] centerQuats =
26
    {
27
        { {0,10},{9,11},{1,7},{4,6},{2,5},{3,8} },
28
        { {9,11},{0,10},{4,6},{1,7},{3,8},{2,5} },
29
        { {2,8},{3,5},{0,11},{9,10},{1,4},{6,7} },
30
        { {3,5},{2,8},{9,10},{0,11},{6,7},{1,4} },
31
        { {1,6},{4,7},{2,3},{5,8},{0,9},{10,11} },
32
        { {4,7},{1,6},{5,8},{2,3},{10,11},{0,9} }
33
    };
34

    
35
  // [1][] are the 3 quats the 1st 'fixed' corner will have when 'fakeTwisted' (which in case of
36
  // the fixed corners is the same as the final twist) with respectively twist 0,1,2.
37

    
38
  private static final int[][] fixedQuats = { {0,1,2},{0,7,8},{0,6,5},{0,4,3} };
39

    
40
  // [1][2][] are the 3 quats the 1st free corner, when permuted to the location of the 2nd corner,
41
  // will have when it is 'fakeTwisted' with fakeTwist 0,1,2.
42
  // fakeTwist is an intermediate twist which needs yet to be translated to the final twist of the
43
  // corners (which needs to sum up to something divisible by 3).
44

    
45
  private static final int[][][] freeQuats=
46
    {
47
        { {0,3,4},{9,8,1},{6,11,2},{7,10,5} },
48
        { {9,2,7},{0,5,6},{1,10,3},{4,11,8} },
49
        { {5,1,11},{2,4,10},{0,8,7},{9,3,6} },
50
        { {8,6,10},{3,7,11},{9,5,4},{0,2,1} }
51
    };
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public TBSkewb()
56
    {
57
    super();
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  public TBSkewb(Resources res)
63
    {
64
    super(res, R.raw.skew_2_tablebase);
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  int[][] getBasicAngles()
70
    {
71
    int[] tmp = {3,3};
72
    return new int[][] { tmp,tmp,tmp,tmp };
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  Static3D[] getRotationAxis()
78
    {
79
    return new Static3D[]
80
         {
81
           new Static3D( SQ3/3, SQ3/3, SQ3/3),
82
           new Static3D( SQ3/3, SQ3/3,-SQ3/3),
83
           new Static3D( SQ3/3,-SQ3/3, SQ3/3),
84
           new Static3D( SQ3/3,-SQ3/3,-SQ3/3)
85
         };
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
  float[][] getPosition()
91
    {
92
    return new float[][]
93
         {
94
             { 1, 1, 1 },
95
             { 1, 1,-1 },
96
             { 1,-1, 1 },
97
             { 1,-1,-1 },
98
             {-1, 1, 1 },
99
             {-1, 1,-1 },
100
             {-1,-1, 1 },
101
             {-1,-1,-1 },
102

    
103
             { 0, 0, 1 },
104
             { 0, 0,-1 },
105
             { 0, 1, 0 },
106
             { 0,-1, 0 },
107
             { 1, 0, 0 },
108
             {-1, 0, 0 }
109
         };
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  float[][] getCuts()
115
    {
116
    float[] cut = {0};
117
    return new float[][] { cut,cut,cut,cut };
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  boolean[][] getRotatable()
123
    {
124
    boolean[] t1 = new boolean[] {false,true};
125
    boolean[] t2 = new boolean[] {true,false};
126
    return new boolean[][] { t1,t2,t2,t1 };
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// specifically for the tablebase
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  int getSize()
134
    {
135
    return 3149280;  // see https://www.jaapsch.net/puzzles/skewb.htm
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  int getMinScramble()
141
    {
142
    return 9;
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  public static void fillInQuats(int[] output, int[] perm, int[] twist)
148
    {
149
    output[0] = fixedQuats[0][twist[0]];
150
    output[1] = freeQuats[0][perm[0]][twist[1]];
151
    output[2] = freeQuats[1][perm[1]][twist[2]];
152
    output[3] = fixedQuats[1][twist[3]];
153
    output[4] = freeQuats[2][perm[2]][twist[3]];
154
    output[5] = fixedQuats[2][twist[5]];
155
    output[6] = fixedQuats[3][twist[6]];
156
    output[7] = freeQuats[3][perm[3]][twist[7]];
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public static int computeLocation(int index, int quat)
162
    {
163
    for(int i=0; i<4; i++)
164
      {
165
      int[] q = freeQuats[index][i];
166
      if( quat==q[0] || quat==q[1] || quat==q[2] ) return i;
167
      }
168

    
169
    android.util.Log.e("D", "error in computeLocation, quat="+quat);
170
    return -1;
171
    }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
  private static int retFixed(int index, int quat)
176
    {
177
    int[] qs = fixedQuats[index];
178

    
179
    if( quat==qs[0]) return 0;
180
    if( quat==qs[1]) return 1;
181
    if( quat==qs[2]) return 2;
182

    
183
    android.util.Log.e("D", "error in retFixed, index="+index+" quat="+quat);
184
    return -1;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private static int retFree(int index, int quat)
190
    {
191
    int[][] qs = freeQuats[index];
192

    
193
    for(int i=0; i<4; i++)
194
      {
195
      if( quat==qs[i][0]) return 0;
196
      if( quat==qs[i][1]) return 1;
197
      if( quat==qs[i][2]) return 2;
198
      }
199

    
200
    android.util.Log.e("D", "error in retFree, index="+index+" quat="+quat);
201
    return -1;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// In case of the four 'fixed' corners (0,3,5,6) which do not change their location,
206
// the twist is natural: 0 in the init positions and increasing 1 mod 3 on each CW turn.
207
//
208
// In case of the four 'free' corners their twist is relative to the position of the 'free'
209
// tetrahedron. And so, for example the twist of free corner 1 (whose 0th face is orange) is equal
210
// to 0 if free corner 7 is on the same face like 0th face of corner 1 (which is the case in init
211
// position); then once free corners 2,4,7 permute CW, twist of corner 1 increases by 1 mod 3.
212

    
213
  public static void computeCornerTwists(int[] twists, int[] quats)
214
    {
215
    twists[0] = retFixed(0,quats[0]);
216
    twists[3] = retFixed(1,quats[3]);
217
    twists[5] = retFixed(2,quats[5]);
218
    twists[6] = retFixed(3,quats[6]);
219

    
220
    twists[1] = retFree(0,quats[1]);
221
    twists[2] = retFree(1,quats[2]);
222
    twists[4] = retFree(2,quats[4]);
223
    twists[7] = retFree(3,quats[7]);
224
    }
225

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

    
228
  private int[] computeCenterPerm(int[] quats)
229
    {
230
    int[] output = new int[6];
231

    
232
    for(int i=0; i<6; i++)
233
      {
234
      output[i] = -1;
235
      int[][] c = centerQuats[i];
236
      int q = quats[8+i];
237

    
238
      for(int j=0; j<6; j++)
239
        if( c[j][0]==q || c[j][1]==q )
240
          {
241
          output[i] = j;
242
          break;
243
          }
244
      }
245

    
246
    return output;
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  private void fillCenterQuats(int[] quats, int[] perm)
252
    {
253
    for(int i=0; i<6; i++)
254
      {
255
      quats[8+i] = centerQuats[i][perm[i]][0];
256
      }
257
    }
258

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

    
261
  private void fillUpToEvenPerm(int[] perm)
262
    {
263
    int min, max;
264

    
265
         if( perm[0]!=0 && perm[1]!=0 ) min=0;
266
    else if( perm[0]!=1 && perm[1]!=1 ) min=1;
267
    else                                min=2;
268

    
269
         if( perm[0]!=3 && perm[1]!=3 ) max=3;
270
    else if( perm[0]!=2 && perm[1]!=2 ) max=2;
271
    else                                max=1;
272

    
273
    int diff = perm[1]-perm[0];
274

    
275
    if( diff==-2 || diff==1 || diff==3 ) { perm[2]=min; perm[3]=max; }
276
    else                                 { perm[2]=max; perm[3]=min; }
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  public int[] getQuats(int index)
282
    {
283
    int[] locationFree = new int[4];
284
    int center_perm_num = (index%360);
285
    index /= 360;
286
    int totalTwist = (index%2187);
287
    locationFree[0] = (index/2187);
288

    
289
    int[] quats = new int[14];
290
    int[] center_perm = new int[6];
291
    int[] twist = new int[8];
292

    
293
    TablebaseHelpers.getEvenPermutationFromNum(center_perm, center_perm_num);
294
    fillCenterQuats(quats,center_perm);
295

    
296
    for(int i=0; i<7; i++)
297
      {
298
      twist[i] = (totalTwist%3);
299
      totalTwist /= 3;
300
      }
301

    
302
    int total = twist[1]+twist[2]+twist[4];
303
    twist[7] = ((6-total)%3);
304

    
305
    int sumFixedTwists = ((twist[0]+twist[3]+twist[5]+twist[6])%3);
306
    locationFree[1] = freeIndex[locationFree[0]][sumFixedTwists];
307
    fillUpToEvenPerm(locationFree);
308

    
309
    quats[0] = fixedQuats[0][twist[0]];
310
    quats[3] = fixedQuats[1][twist[3]];
311
    quats[5] = fixedQuats[2][twist[5]];
312
    quats[6] = fixedQuats[3][twist[6]];
313

    
314
    quats[1] = freeQuats[0][locationFree[0]][twist[1]];
315
    quats[2] = freeQuats[1][locationFree[1]][twist[2]];
316
    quats[4] = freeQuats[2][locationFree[2]][twist[4]];
317
    quats[7] = freeQuats[3][locationFree[3]][twist[7]];
318

    
319
//android.util.Log.e("D", "QUATS center_perm: "+center_perm_num+" totalTwist="+totalTwist+" locationOfFree0="+locationOfFree0);
320
//android.util.Log.e("D", "QUATS t0="+twist[0]+" t1="+twist[1]+" t2="+twist[2]+" t3="+twist[3]+" t4="+twist[4]+" t5="+twist[5]+" t6="+twist[6]+" t7="+twist[7]);
321

    
322
    return quats;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public int getIndex(int[] quats)
328
    {
329
    int[] center_perm = computeCenterPerm(quats);
330
    int center_perm_num = TablebaseHelpers.computeEvenPermutationNum(center_perm);
331
    int[] twist = new int[8];
332
    computeCornerTwists(twist,quats);
333
    int totalTwist = twist[0]+ 3*(twist[1]+ 3*(twist[2]+ 3*(twist[3]+ 3*(twist[4]+ 3*(twist[5]+ 3*twist[6])))));
334
    int locationOfFree0 = computeLocation(0,quats[1]);
335

    
336
//android.util.Log.e("D", "INDEX center_perm: "+center_perm_num+" totalTwist="+totalTwist+" locationOfFree0="+locationOfFree0);
337
//android.util.Log.e("D", "INDEX t0="+twist[0]+" t1="+twist[1]+" t2="+twist[2]+" t3="+twist[3]+" t4="+twist[4]+" t5="+twist[5]+" t6="+twist[6]+" t7="+twist[7]);
338

    
339
    return center_perm_num+ 360*(totalTwist + 2187*locationOfFree0);
340
    }
341
}  
342

    
(9-9/14)