Project

General

Profile

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

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

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[][][] centerQuats =
24
    {
25
        { {0,10},{9,11},{1,7},{4,6},{2,5},{3,8} },
26
        { {9,11},{0,10},{4,6},{1,7},{3,8},{2,5} },
27
        { {2,8},{3,5},{0,11},{9,10},{1,4},{6,7} },
28
        { {3,5},{2,8},{9,10},{0,11},{6,7},{1,4} },
29
        { {1,6},{4,7},{2,3},{5,8},{0,9},{10,11} },
30
        { {4,7},{1,6},{5,8},{2,3},{10,11},{0,9} }
31
    };
32

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

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

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

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public TBSkewb()
54
    {
55
    super();
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  public TBSkewb(Resources res)
61
    {
62
    super(res, R.raw.pyra_3_tablebase);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  float[][] getPosition()
89
    {
90
    return new float[][]
91
         {
92
             { 1, 1, 1 },
93
             { 1, 1,-1 },
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

    
101
             { 0, 0, 1 },
102
             { 0, 0,-1 },
103
             { 0, 1, 0 },
104
             { 0,-1, 0 },
105
             { 1, 0, 0 },
106
             {-1, 0, 0 }
107
         };
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
// specifically for the tablebase
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  int getMinScramble()
139
    {
140
    return 9;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

    
177
    if( quat==qs[0]) return 0;
178
    if( quat==qs[1]) return 1;
179
    if( quat==qs[2]) return 2;
180

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

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

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

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

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

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

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

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

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

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

    
244
    return output;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  private int freeIndex(int taken, int seq)
260
    {
261
    return seq + (seq>=taken ? 1:0);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public int[] getQuats(int index)
267
    {
268
    int center_perm_num = (index%360);
269
    index /= 360;
270
    int totalTwist = (index%2187);
271
    int locationOfFree0 = (index/2187);
272

    
273
    int[] quats = new int[14];
274
    int[] center_perm = new int[6];
275
    int[] twist = new int[8];
276

    
277
    TablebaseHelpers.getEvenPermutationFromNum(center_perm, center_perm_num);
278
    fillCenterQuats(quats,center_perm);
279

    
280
    for(int i=0; i<7; i++)
281
      {
282
      twist[i] = (totalTwist%3);
283
      totalTwist /= 3;
284
      }
285

    
286
    int total = twist[1]+twist[2]+twist[4];
287
    twist[7] = ((6-total)%3);
288

    
289
    int locationOfFree1 = freeIndex(locationOfFree0,0);
290
    int locationOfFree2 = freeIndex(locationOfFree0,1);
291
    int locationOfFree3 = freeIndex(locationOfFree0,2);
292

    
293
    quats[0] = fixedQuats[0][twist[0]];
294
    quats[3] = fixedQuats[1][twist[3]];
295
    quats[5] = fixedQuats[2][twist[5]];
296
    quats[6] = fixedQuats[3][twist[6]];
297

    
298
    quats[1] = freeQuats[0][locationOfFree0][twist[1]];
299
    quats[2] = freeQuats[1][locationOfFree1][twist[2]];
300
    quats[4] = freeQuats[2][locationOfFree2][twist[4]];
301
    quats[7] = freeQuats[3][locationOfFree3][twist[7]];
302

    
303
    return quats;
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  public int getIndex(int[] quats)
309
    {
310
    int[] center_perm = computeCenterPerm(quats);
311
    int center_perm_num = TablebaseHelpers.computeEvenPermutationNum(center_perm);
312
    int[] twist = new int[8];
313
    computeCornerTwists(twist,quats);
314
    int totalTwist = twist[0]+ 3*(twist[1]+ 3*(twist[2]+ 3*(twist[3]+ 3*(twist[4]+ 3*(twist[5]+ 3*twist[6])))));
315
    int locationOfFree0 = computeLocation(quats[1]);
316

    
317
    return center_perm_num+ 360*(totalTwist + 2187*locationOfFree0);
318
    }
319
}  
320

    
(9-9/14)