Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / TablebasesCube2.java @ 618f3d75

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

    
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
public class TablebasesCube2 extends TablebasesAbstract
21
{
22
  private static final int[][] P =
23
      {
24
            { 2,-1,-1},
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

    
33
            {-1, 2,-1},
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

    
42
            {-1,-1, 2},
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
      };
51

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

    
62
  private int[][][] mQuatsMap;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  public TablebasesCube2(Resources res)
74
    {
75
    super(res,org.distorted.objectlib.R.raw.cube_2_tablebase);
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

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

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

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  float[][] getPosition()
101
    {
102
    return new float[][]
103
      {
104
        { -0.5f, -0.5f, -0.5f },
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
      };
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

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

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  private int[] getPermTwist(float[] point)
149
    {
150
    float ERR = 0.01f;
151

    
152
    for(int i=0; i<24; i++)
153
      {
154
      float dx = point[0]-P[i][0];
155
      float dy = point[1]-P[i][1];
156
      float dz = point[2]-P[i][2];
157

    
158
      if( dx*dx + dy*dy + dz*dz < ERR ) return PT[i];
159
      }
160

    
161
    return null;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  private void fillOutMap(float[] buffer, int[][] map, float[] point, Static4D quat, int quatIndex)
167
    {
168
    QuatHelper.rotateVectorByQuat(buffer,point[0],point[1],point[2],1,quat);
169
    int[] pt = getPermTwist(buffer);
170
    map[pt[0]][pt[1]] = quatIndex;
171
    }
172

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

    
175
  private void computeMap()
176
    {
177
    final float[][] POINTS =
178
        {
179
            {-2,-1,-1},
180
            {-2,-1, 1},
181
            {-2, 1,-1},
182
            {-2, 1, 1},
183
            { 2,-1,-1},
184
            { 2,-1, 1},
185
            { 2, 1,-1},
186
            { 2, 1, 1},
187
        };
188

    
189
    mQuatsMap = new int[8][8][3];
190
    Static4D[] quats = getQuats();
191
    float[] buffer = new float[4];
192

    
193
    for(int c=0; c<8; c++)
194
      for(int q=0; q<24; q++)
195
        fillOutMap(buffer,mQuatsMap[c],POINTS[c],quats[q],q);
196
    }
197

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

    
200
  public static int[] shrinkPerm(int[] perm8)
201
    {
202
    int[] perm7 = new int[7];
203

    
204
    int tmp = perm8[0];
205
    perm7[0] = tmp>1 ? tmp-1 : tmp;
206
    tmp = perm8[2];
207
    perm7[1] = tmp>1 ? tmp-1 : tmp;
208
    tmp = perm8[3];
209
    perm7[2] = tmp>1 ? tmp-1 : tmp;
210
    tmp = perm8[4];
211
    perm7[3] = tmp>1 ? tmp-1 : tmp;
212
    tmp = perm8[5];
213
    perm7[4] = tmp>1 ? tmp-1 : tmp;
214
    tmp = perm8[6];
215
    perm7[5] = tmp>1 ? tmp-1 : tmp;
216
    tmp = perm8[7];
217
    perm7[6] = tmp>1 ? tmp-1 : tmp;
218

    
219
    return perm7;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  private static int[] growPerm(int[] perm7)
225
    {
226
    int[] perm8 = new int[8];
227
    int tmp = perm7[0];
228
    perm8[0] = tmp>=1 ? tmp+1 : tmp;
229
    perm8[1] = 1;
230
    tmp = perm7[1];
231
    perm8[2] = tmp>=1 ? tmp+1 : tmp;
232
    tmp = perm7[2];
233
    perm8[3] = tmp>=1 ? tmp+1 : tmp;
234
    tmp = perm7[3];
235
    perm8[4] = tmp>=1 ? tmp+1 : tmp;
236
    tmp = perm7[4];
237
    perm8[5] = tmp>=1 ? tmp+1 : tmp;
238
    tmp = perm7[5];
239
    perm8[6] = tmp>=1 ? tmp+1 : tmp;
240
    tmp = perm7[6];
241
    perm8[7] = tmp>=1 ? tmp+1 : tmp;
242

    
243
    return perm8;
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  private int[] getPerm(int permNum)
249
    {
250
    int[] perm7 = new int[7];
251
    TablebaseHelpers.getPermutationFromNum(perm7,7,permNum);
252
    return growPerm(perm7);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  private int[] getTwist(int twistNum)
258
    {
259
    int[] twist = new int[8];
260
    twist[0] = twistNum%3;
261
    twist[1] = 0;
262
    twistNum /=3;
263
    twist[2] = twistNum%3;
264
    twistNum /=3;
265
    twist[3] = twistNum%3;
266
    twistNum /=3;
267
    twist[4] = twistNum%3;
268
    twistNum /=3;
269
    twist[5] = twistNum%3;
270
    twistNum /=3;
271
    twist[6] = twistNum%3;
272

    
273
    int total = (twist[0]+twist[2]+twist[3]+twist[4]+twist[5]+twist[6])%3;
274
    if( total==0 ) twist[7] = 0;
275
    else twist[7] = 3-total;
276

    
277
    return twist;
278
    }
279

    
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

    
282
  public int[] getQuats(int index)
283
    {
284
    if( mQuatsMap==null ) computeMap();
285

    
286
    int twistNum = index%729;
287
    int permuNum = index/729;
288

    
289
    int[] twist = getTwist(twistNum);
290
    int[] perm  = getPerm(permuNum);
291

    
292
    int[] quats = new int[8];
293
    for(int i=0; i<8; i++) quats[i] = mQuatsMap[i][perm[i]][twist[i]];
294

    
295
    return quats;
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

    
300
  private void extractTwistAndPerm(int quat, int[][] map, int[] twist, int[] perm, int index)
301
    {
302
    for(int i=0; i<8; i++)
303
      for(int j=0; j<3; j++)
304
        if( map[i][j]==quat )
305
          {
306
          twist[index] = j;
307
          perm[index] = i;
308
          break;
309
          }
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

    
314
  public int getIndex(int[] quats)
315
    {
316
    if( mQuatsMap==null ) computeMap();
317

    
318
    int[] twist = new int[8];
319
    int[] perm8 = new int[8];
320

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

    
323
    int[] perm7 = shrinkPerm(perm8);
324
    int twistNum = twist[0] + 3*(twist[2] + 3*(twist[3] + 3*(twist[4] + 3*(twist[5] + 3*twist[6]))));
325
    int permNum  = TablebaseHelpers.computePermutationNum(perm7);
326

    
327
    return twistNum + 729*permNum;
328
    }
329
}  
330

    
(5-5/10)