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