aboutsummaryrefslogtreecommitdiff
path: root/old/2021-02-28-transformcube-works/src/transformations.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
committerSebastiano Tronto <sebastiano.tronto@gmail.com>2021-11-11 21:37:34 +0100
commit3568412f8f230774d0d11d7ed1c897424f95d3ef (patch)
tree77223792d8c925a9b1fc32b3f4341e943b5f8209 /old/2021-02-28-transformcube-works/src/transformations.c
parent67e1b5e6e6a2c917a2fe58a37a1382c982b1e5c5 (diff)
downloadnissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.tar.gz
nissy-3568412f8f230774d0d11d7ed1c897424f95d3ef.zip
Rewritten from scratch. Welocme nissy 2.0!
Diffstat (limited to 'old/2021-02-28-transformcube-works/src/transformations.c')
-rw-r--r--old/2021-02-28-transformcube-works/src/transformations.c224
1 files changed, 224 insertions, 0 deletions
diff --git a/old/2021-02-28-transformcube-works/src/transformations.c b/old/2021-02-28-transformcube-works/src/transformations.c
new file mode 100644
index 0000000..9ae10e9
--- /dev/null
+++ b/old/2021-02-28-transformcube-works/src/transformations.c
@@ -0,0 +1,224 @@
1#include "transformations.h"
2
3int edge_slice(int e); /* Return slice (e=0, s=1, m=2) to which e belongs */
4Cube rotate_via_compose(Transformation r, Cube c);
5bool read_rtables_file();
6bool write_rtables_file();
7
8/* Values mod 3 to determine from which side to take the state to convert */
9int epose_source[NROTATIONS]; /* 0 = epose, 1 = eposs, 2 = eposm */
10int eposs_source[NROTATIONS];
11int eposm_source[NROTATIONS];
12int eofb_source[NROTATIONS]; /* 0 = eoud, 1 = eorl, 2 = eofb */
13int eorl_source[NROTATIONS];
14int eoud_source[NROTATIONS];
15int coud_source[NROTATIONS]; /* 0 = coud, 1 = corl, 2 = cofb */
16int cofb_source[NROTATIONS];
17int corl_source[NROTATIONS];
18
19/* Transition tables for rotations (n+1 is mirror) */
20uint16_t epose_rtable[NROTATIONS][factorial12/factorial8];
21uint16_t eposs_rtable[NROTATIONS][factorial12/factorial8];
22uint16_t eposm_rtable[NROTATIONS][factorial12/factorial8];
23uint16_t eo_rtable[NROTATIONS][pow2to11];
24/*uint16_t eofb_rtable[NROTATIONS][pow2to11];
25uint16_t eorl_rtable[NROTATIONS][pow2to11];
26uint16_t eoud_rtable[NROTATIONS][pow2to11];*/
27uint16_t cp_rtable[NROTATIONS][factorial8];
28uint16_t co_rtable[NROTATIONS][pow3to7];
29/*uint16_t coud_rtable[NROTATIONS][pow3to7];
30uint16_t cofb_rtable[NROTATIONS][pow3to7];
31uint16_t corl_rtable[NROTATIONS][pow3to7];*/
32uint16_t cpos_rtable[NROTATIONS][factorial6];
33
34/* Same for moves */
35uint16_t move_rtable[NROTATIONS][NMOVES];
36
37NissMove rotation_niss[NROTATIONS][6];
38
39int edge_slice(int e) {
40 if (e == FR || e == FL || e == BL || e == BR)
41 return 0;
42 if (e == UR || e == UL || e == DR || e == DL)
43 return 1;
44 return 2;
45}
46
47Cube rotate_via_compose(Transformation r, Cube c) {
48 if (r != mirror) {
49 return apply_alg(rotation_niss[r], c);
50 } else {
51 static int zero12[12] = {0,0,0,0,0,0,0,0,0,0,0,0},
52 zero8[12] = {0,0,0,0,0,0,0,0},
53 mirror_ep[12] = {UF,UR,UB,UL,DF,DR,DB,DL,FL,FR,BR,BL},
54 mirror_cp[8] = {UFL, UFR, UBR, UBL, DFL, DFR, DBR, DBL},
55 mirror_cpos[6] =
56 {U_center,D_center,L_center,R_center,F_center,B_center};
57 return move_via_arrays((CubeArray){
58 .ep = mirror_ep, .eofb = zero12, .eorl = zero12, .eoud = zero12,
59 .cp = mirror_cp, .coud = zero8, .corl = zero8, .cofb = zero8,
60 .cpos = mirror_cpos}, c, pf_all);
61 }
62}
63
64bool read_rtables_file() {
65 FILE *ttf;
66 long unsigned int me[12] = { factorial12/factorial8, factorial12/factorial8,
67 factorial12/factorial8, pow2to11, pow2to11, pow2to11,
68 factorial8, pow3to7, pow3to7, pow3to7, factorial6, NMOVES };
69 if ((ttf = fopen("rtables", "rb")) != NULL) {
70 bool r = true;
71 for (int m = 0; m < NROTATIONS; m++) {
72 r = r && fread(epose_rtable[m], sizeof(uint16_t), me[0], ttf) == me[0];
73 r = r && fread(eposs_rtable[m], sizeof(uint16_t), me[1], ttf) == me[1];
74 r = r && fread(eposm_rtable[m], sizeof(uint16_t), me[2], ttf) == me[2];
75 r = r && fread(eo_rtable[m], sizeof(uint16_t), me[3], ttf) == me[3];
76 /*r = r && fread(eofb_rtable[m], sizeof(uint16_t), me[3], ttf) == me[3];
77 r = r && fread(eorl_rtable[m], sizeof(uint16_t), me[4], ttf) == me[4];
78 r = r && fread(eoud_rtable[m], sizeof(uint16_t), me[5], ttf) == me[5];*/
79 r = r && fread(cp_rtable[m], sizeof(uint16_t), me[6], ttf) == me[6];
80 r = r && fread(co_rtable[m], sizeof(uint16_t), me[7], ttf) == me[7];
81 /*r = r && fread(coud_rtable[m], sizeof(uint16_t), me[7], ttf) == me[7];
82 r = r && fread(corl_rtable[m], sizeof(uint16_t), me[8], ttf) == me[8];
83 r = r && fread(cofb_rtable[m], sizeof(uint16_t), me[9], ttf) == me[9];*/
84 r = r && fread(cpos_rtable[m], sizeof(uint16_t), me[10], ttf) == me[10];
85 r = r && fread(move_rtable[m], sizeof(uint16_t), me[11], ttf) == me[11];
86 }
87 fclose(ttf);
88 return r;
89 } else return false;
90}
91
92bool write_rtables_file() {
93 FILE *ttf;
94 long unsigned int me[12] = { factorial12/factorial8, factorial12/factorial8,
95 factorial12/factorial8, pow2to11, pow2to11, pow2to11,
96 factorial8, pow3to7, pow3to7, pow3to7, factorial6, NMOVES };
97 if ((ttf = fopen("rtables", "wb")) != NULL) {
98 bool r = true;
99 for (int m = 0; m < NROTATIONS; m++) {
100 r = r && fwrite(epose_rtable[m], sizeof(uint16_t), me[0], ttf) == me[0];
101 r = r && fwrite(eposs_rtable[m], sizeof(uint16_t), me[1], ttf) == me[1];
102 r = r && fwrite(eposm_rtable[m], sizeof(uint16_t), me[2], ttf) == me[2];
103 r = r && fwrite(eo_rtable[m], sizeof(uint16_t), me[3], ttf) == me[3];
104 /*r = r && fwrite(eofb_rtable[m], sizeof(uint16_t), me[3], ttf) == me[3];
105 r = r && fwrite(eorl_rtable[m], sizeof(uint16_t), me[4], ttf) == me[4];
106 r = r && fwrite(eoud_rtable[m], sizeof(uint16_t), me[5], ttf) == me[5];*/
107 r = r && fwrite(cp_rtable[m], sizeof(uint16_t), me[6], ttf) == me[6];
108 r = r && fwrite(co_rtable[m], sizeof(uint16_t), me[7], ttf) == me[7];
109 /*r = r && fwrite(coud_rtable[m], sizeof(uint16_t), me[7], ttf) == me[7];
110 r = r && fwrite(corl_rtable[m], sizeof(uint16_t), me[8], ttf) == me[8];
111 r = r && fwrite(cofb_rtable[m], sizeof(uint16_t), me[9], ttf) == me[9];*/
112 r = r && fwrite(cpos_rtable[m], sizeof(uint16_t), me[10],ttf) == me[10];
113 r = r && fwrite(move_rtable[m], sizeof(uint16_t), me[11],ttf) == me[11];
114 }
115 fclose(ttf);
116 return r;
117 } else return false;
118}
119
120void init_transformations(bool read, bool write) {
121 /* Compute sources */
122 for (int i = 0; i < NROTATIONS; i++) {
123 Cube cube = {0};
124 if (i != mirror)
125 cube = apply_alg(rotation_algs[i], (Cube){0});
126 epose_source[i] = edge_slice(edge_at(cube, FR));
127 eposs_source[i] = edge_slice(edge_at(cube, UR));
128 eposm_source[i] = edge_slice(edge_at(cube, UF));
129 eofb_source[i] = center_at(cube, F_center)/2;
130 eorl_source[i] = center_at(cube, R_center)/2;
131 eoud_source[i] = center_at(cube, U_center)/2;
132 coud_source[i] = center_at(cube, U_center)/2;
133 cofb_source[i] = center_at(cube, F_center)/2;
134 corl_source[i] = center_at(cube, R_center)/2;
135 }
136
137 /*TODO: maybe move down*/
138 /* Compute rotation_niss array, necessary for rotate_via_compose */
139 for (int r = 0; r != mirror; r++) {
140 concat(rotation_algs[r], rotation_algs[r], rotation_niss[r]);
141 for (int i = len(rotation_algs[r]); rotation_niss[r][i].m != NULLMOVE; i++)
142 rotation_niss[r][i].inverse = true;
143 }
144
145 /* If I can read tables from file, I stop here */
146 if (read)
147 if (read_rtables_file())
148 return;
149
150 /* Initialize tables */
151 for (int m = 0; m < NROTATIONS; m++) {
152 int eparr[12] = {0,0,0,0,0,0,0,0,0,0,0,0}, cparr[8] = {0,0,0,0,0,0,0,0};
153 CubeArray epcp = { .ep = eparr, .cp = cparr };
154 cube_to_arrays(apply_alg(rotation_algs[m], (Cube){0}), &epcp,
155 (PieceFilter){.epose=true,.eposs=true,.eposm=true,.cp=true});
156 for (uint16_t i = 0; i < factorial12/factorial8; i++) {
157 Cube c[3] = { admissible_ep((Cube){ .epose = i}, pf_e),
158 admissible_ep((Cube){ .eposs = i}, pf_s),
159 admissible_ep((Cube){ .eposm = i}, pf_m) };
160 epose_rtable[m][i] = rotate_via_compose(m, c[epose_source[m]]).epose;
161 eposs_rtable[m][i] = rotate_via_compose(m, c[eposs_source[m]]).eposs;
162 eposm_rtable[m][i] = rotate_via_compose(m, c[eposm_source[m]]).eposm;
163 }
164 for (uint16_t i = 0; i < pow2to11; i++ ) {
165 int eoarr[12];
166 int_to_sum_zero_array(i, 2, 12, eoarr);
167 apply_permutation(eparr, eoarr, 12);
168 eo_rtable[m][i] = digit_array_to_int(eoarr, 11, 2);
169 /*Cube c[3] = {(Cube){.eoud=i}, (Cube){.eorl=i}, (Cube){.eofb=i}};
170 eofb_rtable[m][i] = apply_alg(rotation_algs[m], (Cube){.eofb=i}).eofb;
171 eorl_rtable[m][i] = rotate_via_compose(m, c[eorl_source[m]]).eorl;
172 eoud_rtable[m][i] = rotate_via_compose(m, c[eoud_source[m]]).eoud;*/
173 }
174 for (uint16_t i = 0; i < pow3to7; i++) {
175 int coarr[12];
176 int_to_sum_zero_array(i, 3, 8, coarr);
177 apply_permutation(cparr, coarr, 8);
178 co_rtable[m][i] = digit_array_to_int(coarr, 8, 3);
179 /*Cube c[3] = {(Cube){.coud=i}, (Cube){.corl=i}, (Cube){.cofb=i}};
180 coud_rtable[m][i] = rotate_via_compose(m, c[coud_source[m]]).coud;
181 corl_rtable[m][i] = rotate_via_compose(m, c[corl_source[m]]).corl;
182 cofb_rtable[m][i] = rotate_via_compose(m, c[cofb_source[m]]).cofb;*/
183 }
184 for (uint16_t i = 0; i < factorial8; i++)
185 cp_rtable[m][i] = rotate_via_compose(m, (Cube){.cp=i}).cp;
186 for (uint16_t i = 0; i < factorial6; i++)
187 cpos_rtable[m][i] = rotate_via_compose(m, (Cube){.cpos=i}).cpos;
188 }
189
190 if (write)
191 if (!write_rtables_file())
192 printf("Error in writing rtables: file not writable\n");
193}
194
195Cube transform_cube(Transformation t, Cube cube) {
196 Cube transformed = {0};
197
198 uint16_t aux_epos[3] = { cube.epose, cube.eposs, cube.eposm },
199 aux_eo[3] = { cube.eoud, cube.eorl, cube.eofb },
200 aux_co[3] = { cube.coud, cube.corl, cube.cofb };
201
202 transformed.epose = epose_rtable[t][aux_epos[epose_source[t]]];
203 transformed.eposs = eposs_rtable[t][aux_epos[eposs_source[t]]];
204 transformed.eposm = eposm_rtable[t][aux_epos[eposm_source[t]]];
205 transformed.eofb = eo_rtable[t][aux_eo[eofb_source[t]]];
206 transformed.eorl = eo_rtable[t][aux_eo[eorl_source[t]]];
207 transformed.eoud = eo_rtable[t][aux_eo[eoud_source[t]]];
208 transformed.coud = co_rtable[t][aux_co[coud_source[t]]];
209 transformed.corl = co_rtable[t][aux_co[corl_source[t]]];
210 transformed.cofb = co_rtable[t][aux_co[cofb_source[t]]];
211 transformed.cp = cp_rtable[t][cube.cp];
212 transformed.cpos = cpos_rtable[t][cube.cpos];
213
214/*
215 printf("%d\n", coud_source[t]);
216 int ccc[8];
217 int_to_sum_zero_array(cube.cofb, 3, 8, ccc);
218 for (int i = 0; i < 8; i++)
219 printf("%d ", ccc[i]);
220 printf("\n");
221 */
222
223 return transformed;
224}

Generated with cgit - Back to sebastiano.tronto.net