Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / TablebasesCube2.java @ 7363eaa6

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 TablebasesMITM
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,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[] getPruningLevels()
150
    {
151
    return new int[] {5,11};
152
    }
153

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

    
156
  private int[] getPermTwist(float[] point)
157
    {
158
    float ERR = 0.01f;
159

    
160
    for(int i=0; i<24; i++)
161
      {
162
      float dx = point[0]-P[i][0];
163
      float dy = point[1]-P[i][1];
164
      float dz = point[2]-P[i][2];
165

    
166
      if( dx*dx + dy*dy + dz*dz < ERR ) return PT[i];
167
      }
168

    
169
    return null;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  private void fillOutMap(float[] buffer, int[][] map, float[] point, Static4D quat, int quatIndex)
175
    {
176
    QuatHelper.rotateVectorByQuat(buffer,point[0],point[1],point[2],1,quat);
177
    int[] pt = getPermTwist(buffer);
178
    map[pt[0]][pt[1]] = quatIndex;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private void computeMap()
184
    {
185
    final float[][] POINTS =
186
        {
187
            {-2,-1,-1},
188
            {-2,-1, 1},
189
            {-2, 1,-1},
190
            {-2, 1, 1},
191
            { 2,-1,-1},
192
            { 2,-1, 1},
193
            { 2, 1,-1},
194
            { 2, 1, 1},
195
        };
196

    
197
    mQuatsMap = new int[8][8][3];
198
    Static4D[] quats = getQuats();
199
    float[] buffer = new float[4];
200

    
201
    for(int c=0; c<8; c++)
202
      for(int q=0; q<24; q++)
203
        fillOutMap(buffer,mQuatsMap[c],POINTS[c],quats[q],q);
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public static int[] shrinkPerm(int[] perm8)
209
    {
210
    int[] perm7 = new int[7];
211

    
212
    int tmp = perm8[0];
213
    perm7[0] = tmp>1 ? tmp-1 : tmp;
214
    tmp = perm8[2];
215
    perm7[1] = tmp>1 ? tmp-1 : tmp;
216
    tmp = perm8[3];
217
    perm7[2] = tmp>1 ? tmp-1 : tmp;
218
    tmp = perm8[4];
219
    perm7[3] = tmp>1 ? tmp-1 : tmp;
220
    tmp = perm8[5];
221
    perm7[4] = tmp>1 ? tmp-1 : tmp;
222
    tmp = perm8[6];
223
    perm7[5] = tmp>1 ? tmp-1 : tmp;
224
    tmp = perm8[7];
225
    perm7[6] = tmp>1 ? tmp-1 : tmp;
226

    
227
    return perm7;
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

    
232
  private static int[] growPerm(int[] perm7)
233
    {
234
    int[] perm8 = new int[8];
235
    int tmp = perm7[0];
236
    perm8[0] = tmp>=1 ? tmp+1 : tmp;
237
    perm8[1] = 1;
238
    tmp = perm7[1];
239
    perm8[2] = tmp>=1 ? tmp+1 : tmp;
240
    tmp = perm7[2];
241
    perm8[3] = tmp>=1 ? tmp+1 : tmp;
242
    tmp = perm7[3];
243
    perm8[4] = tmp>=1 ? tmp+1 : tmp;
244
    tmp = perm7[4];
245
    perm8[5] = tmp>=1 ? tmp+1 : tmp;
246
    tmp = perm7[5];
247
    perm8[6] = tmp>=1 ? tmp+1 : tmp;
248
    tmp = perm7[6];
249
    perm8[7] = tmp>=1 ? tmp+1 : tmp;
250

    
251
    return perm8;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  private int[] getPerm(int permNum)
257
    {
258
    int[] perm7 = new int[7];
259
    TablebaseHelpers.getPermutationFromNum(perm7,7,permNum);
260
    return growPerm(perm7);
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

    
265
  private int[] getTwist(int twistNum)
266
    {
267
    int[] twist = new int[8];
268
    twist[0] = twistNum%3;
269
    twist[1] = 0;
270
    twistNum /=3;
271
    twist[2] = twistNum%3;
272
    twistNum /=3;
273
    twist[3] = twistNum%3;
274
    twistNum /=3;
275
    twist[4] = twistNum%3;
276
    twistNum /=3;
277
    twist[5] = twistNum%3;
278
    twistNum /=3;
279
    twist[6] = twistNum%3;
280

    
281
    int total = (twist[0]+twist[2]+twist[3]+twist[4]+twist[5]+twist[6])%3;
282
    if( total==0 ) twist[7] = 0;
283
    else twist[7] = 3-total;
284

    
285
    return twist;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  public int[] getQuats(int index)
291
    {
292
    if( mQuatsMap==null ) computeMap();
293

    
294
    int twistNum = index%729;
295
    int permuNum = index/729;
296

    
297
    int[] twist = getTwist(twistNum);
298
    int[] perm  = getPerm(permuNum);
299

    
300
    int[] quats = new int[8];
301
    for(int i=0; i<8; i++) quats[i] = mQuatsMap[i][perm[i]][twist[i]];
302

    
303
    return quats;
304
    }
305

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

    
308
  private void extractTwistAndPerm(int quat, int[][] map, int[] twist, int[] perm, int index)
309
    {
310
    for(int i=0; i<8; i++)
311
      for(int j=0; j<3; j++)
312
        if( map[i][j]==quat )
313
          {
314
          twist[index] = j;
315
          perm[index] = i;
316
          break;
317
          }
318
    }
319

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

    
322
  public int getIndex(int[] quats)
323
    {
324
    if( mQuatsMap==null ) computeMap();
325

    
326
    int[] twist = new int[8];
327
    int[] perm8 = new int[8];
328

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

    
331
    int[] perm7 = shrinkPerm(perm8);
332
    int twistNum = twist[0] + 3*(twist[2] + 3*(twist[3] + 3*(twist[4] + 3*(twist[5] + 3*twist[6]))));
333
    int permNum  = TablebaseHelpers.computePermutationNum(perm7);
334

    
335
    return twistNum + 729*permNum;
336
    }
337
}  
338

    
(6-6/12)