Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / TablebasesCube2.java @ 61b37d19

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 android.content.res.Resources;
13

    
14
import org.distorted.library.main.QuatHelper;
15
import org.distorted.library.type.Static3D;
16
import org.distorted.library.type.Static4D;
17
import org.distorted.objectlib.R;
18

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

    
21
public class TablebasesCube2 extends TablebasesPruning
22
{
23
  private static final int[][] P =
24
      {
25
            { 2,-1,-1},
26
            { 2,-1, 1},
27
            { 2, 1,-1},
28
            { 2, 1, 1},
29
            {-2,-1,-1},
30
            {-2,-1, 1},
31
            {-2, 1,-1},
32
            {-2, 1, 1},
33

    
34
            {-1, 2,-1},
35
            { 1, 2,-1},
36
            {-1, 2, 1},
37
            { 1, 2, 1},
38
            {-1,-2,-1},
39
            { 1,-2,-1},
40
            {-1,-2, 1},
41
            { 1,-2, 1},
42

    
43
            {-1,-1, 2},
44
            { 1,-1, 2},
45
            {-1, 1, 2},
46
            { 1, 1, 2},
47
            {-1,-1,-2},
48
            { 1,-1,-2},
49
            {-1, 1,-2},
50
            { 1, 1,-2},
51
      };
52

    
53
  private static final int[][] PT =
54
      {
55
          {4,0},{5,0},{6,0},{7,0},
56
          {0,0},{1,0},{2,0},{3,0},
57
          {2,2},{6,1},{3,1},{7,2},
58
          {0,1},{4,2},{1,2},{5,1},
59
          {1,1},{5,2},{3,2},{7,1},
60
          {0,2},{4,1},{2,1},{6,2}
61
      };
62

    
63
  private int[][][] mQuatsMap;
64

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

    
67
  public TablebasesCube2()
68
    {
69
    super();
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  public TablebasesCube2(Resources res)
75
    {
76
    super(res, new int[] {R.raw.cube_2_pruning5}, new int[] {R.raw.cube_2_pruning11} );
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  int[][] getBasicAngles()
82
    {
83
    int[] tmp = {4,4};
84
    return new int[][] { tmp,tmp,tmp };
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  Static3D[] getRotationAxis()
90
    {
91
    return new Static3D[]
92
         {
93
           new Static3D(1,0,0),
94
           new Static3D(0,1,0),
95
           new Static3D(0,0,1)
96
         };
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  float[][] getPosition()
102
    {
103
    return new float[][]
104
      {
105
        { -0.5f, -0.5f, -0.5f },
106
        { -0.5f, -0.5f,  0.5f },
107
        { -0.5f,  0.5f, -0.5f },
108
        { -0.5f,  0.5f,  0.5f },
109
        {  0.5f, -0.5f, -0.5f },
110
        {  0.5f, -0.5f,  0.5f },
111
        {  0.5f,  0.5f, -0.5f },
112
        {  0.5f,  0.5f,  0.5f },
113
      };
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  float[][] getCuts()
119
    {
120
    return new float[][] { {0.0f}, {0.0f}, {0.0f} };
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  boolean[][] getRotatable()
126
    {
127
    return new boolean[][] { {false,true},{false,true},{true,false} };
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
// specifically for the tablebase
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// 7!*3^6 --> https://www.jaapsch.net/puzzles/cube2.htm
134

    
135
  int getSize()
136
    {
137
    return 3674160;
138
    }
139

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

    
142
  int getMinScramble()
143
    {
144
    return 9;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  int[] getMidPruningLevels()
150
    {
151
    return new int[] {4,5};
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  int[] getHighPruningLevels()
157
    {
158
    return new int[] {11};
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  int getGodsNumber()
164
    {
165
    return 11;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  private int[] getPermTwist(float[] point)
171
    {
172
    float ERR = 0.01f;
173

    
174
    for(int i=0; i<24; i++)
175
      {
176
      float dx = point[0]-P[i][0];
177
      float dy = point[1]-P[i][1];
178
      float dz = point[2]-P[i][2];
179

    
180
      if( dx*dx + dy*dy + dz*dz < ERR ) return PT[i];
181
      }
182

    
183
    return null;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void fillOutMap(float[] buffer, int[][] map, float[] point, Static4D quat, int quatIndex)
189
    {
190
    QuatHelper.rotateVectorByQuat(buffer,point[0],point[1],point[2],1,quat);
191
    int[] pt = getPermTwist(buffer);
192
    map[pt[0]][pt[1]] = quatIndex;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  private void computeMap()
198
    {
199
    final float[][] POINTS =
200
        {
201
            {-2,-1,-1},
202
            {-2,-1, 1},
203
            {-2, 1,-1},
204
            {-2, 1, 1},
205
            { 2,-1,-1},
206
            { 2,-1, 1},
207
            { 2, 1,-1},
208
            { 2, 1, 1},
209
        };
210

    
211
    mQuatsMap = new int[8][8][3];
212
    Static4D[] quats = getQuats();
213
    float[] buffer = new float[4];
214

    
215
    for(int c=0; c<8; c++)
216
      for(int q=0; q<24; q++)
217
        fillOutMap(buffer,mQuatsMap[c],POINTS[c],quats[q],q);
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public static int[] shrinkPerm(int[] perm8)
223
    {
224
    int[] perm7 = new int[7];
225

    
226
    int tmp = perm8[0];
227
    perm7[0] = tmp>1 ? tmp-1 : tmp;
228
    tmp = perm8[2];
229
    perm7[1] = tmp>1 ? tmp-1 : tmp;
230
    tmp = perm8[3];
231
    perm7[2] = tmp>1 ? tmp-1 : tmp;
232
    tmp = perm8[4];
233
    perm7[3] = tmp>1 ? tmp-1 : tmp;
234
    tmp = perm8[5];
235
    perm7[4] = tmp>1 ? tmp-1 : tmp;
236
    tmp = perm8[6];
237
    perm7[5] = tmp>1 ? tmp-1 : tmp;
238
    tmp = perm8[7];
239
    perm7[6] = tmp>1 ? tmp-1 : tmp;
240

    
241
    return perm7;
242
    }
243

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

    
246
  private static int[] growPerm(int[] perm7)
247
    {
248
    int[] perm8 = new int[8];
249
    int tmp = perm7[0];
250
    perm8[0] = tmp>=1 ? tmp+1 : tmp;
251
    perm8[1] = 1;
252
    tmp = perm7[1];
253
    perm8[2] = tmp>=1 ? tmp+1 : tmp;
254
    tmp = perm7[2];
255
    perm8[3] = tmp>=1 ? tmp+1 : tmp;
256
    tmp = perm7[3];
257
    perm8[4] = tmp>=1 ? tmp+1 : tmp;
258
    tmp = perm7[4];
259
    perm8[5] = tmp>=1 ? tmp+1 : tmp;
260
    tmp = perm7[5];
261
    perm8[6] = tmp>=1 ? tmp+1 : tmp;
262
    tmp = perm7[6];
263
    perm8[7] = tmp>=1 ? tmp+1 : tmp;
264

    
265
    return perm8;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  private int[] getPerm(int permNum)
271
    {
272
    int[] perm7 = new int[7];
273
    TablebaseHelpers.getPermutationFromNum(perm7,7,permNum);
274
    return growPerm(perm7);
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

    
279
  private int[] getTwist(int twistNum)
280
    {
281
    int[] twist = new int[8];
282
    twist[0] = twistNum%3;
283
    twist[1] = 0;
284
    twistNum /=3;
285
    twist[2] = twistNum%3;
286
    twistNum /=3;
287
    twist[3] = twistNum%3;
288
    twistNum /=3;
289
    twist[4] = twistNum%3;
290
    twistNum /=3;
291
    twist[5] = twistNum%3;
292
    twistNum /=3;
293
    twist[6] = twistNum%3;
294

    
295
    int total = (twist[0]+twist[2]+twist[3]+twist[4]+twist[5]+twist[6])%3;
296
    if( total==0 ) twist[7] = 0;
297
    else twist[7] = 3-total;
298

    
299
    return twist;
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  public int[] getQuats(int index)
305
    {
306
    if( mQuatsMap==null ) computeMap();
307

    
308
    int twistNum = index%729;
309
    int permuNum = index/729;
310

    
311
    int[] twist = getTwist(twistNum);
312
    int[] perm  = getPerm(permuNum);
313

    
314
    int[] quats = new int[8];
315
    for(int i=0; i<8; i++) quats[i] = mQuatsMap[i][perm[i]][twist[i]];
316

    
317
    return quats;
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  private void extractTwistAndPerm(int quat, int[][] map, int[] twist, int[] perm, int index)
323
    {
324
    for(int i=0; i<8; i++)
325
      for(int j=0; j<3; j++)
326
        if( map[i][j]==quat )
327
          {
328
          twist[index] = j;
329
          perm[index] = i;
330
          break;
331
          }
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335

    
336
  public int getIndex(int[] quats)
337
    {
338
    if( mQuatsMap==null ) computeMap();
339

    
340
    int[] twist = new int[8];
341
    int[] perm8 = new int[8];
342

    
343
    for(int i=0; i<8; i++) extractTwistAndPerm(quats[i],mQuatsMap[i],twist,perm8,i);
344

    
345
    int[] perm7 = shrinkPerm(perm8);
346
    int twistNum = twist[0] + 3*(twist[2] + 3*(twist[3] + 3*(twist[4] + 3*(twist[5] + 3*twist[6]))));
347
    int permNum  = TablebaseHelpers.computePermutationNum(perm7);
348

    
349
    return twistNum + 729*permNum;
350
    }
351
}  
352

    
(6-6/12)