Project

General

Profile

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

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

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 TablebasesAbstract
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, R.raw.cube_2_tablebase);
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
  private int[] getPermTwist(float[] point)
150
    {
151
    float ERR = 0.01f;
152

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

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

    
162
    return null;
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  private void computeMap()
177
    {
178
    final float[][] POINTS =
179
        {
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
            { 2, 1, 1},
188
        };
189

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

    
220
    return perm7;
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

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

    
244
    return perm8;
245
    }
246

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

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

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

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

    
278
    return twist;
279
    }
280

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

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

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

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

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

    
296
    return quats;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

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

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314

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

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

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

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

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

    
(6-6/12)