h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

transform.h (9437B)


      1 #define TRANS_EDGES_ROTATION(T, c) \
      2         compose_edges(compose_edges(TRANS_CUBE_ ## T, c), \
      3         TRANS_CUBE_ ## T ## _INVERSE)
      4 #define TRANS_EDGES_MIRRORED(T, c) TRANS_EDGES_ROTATION(T, c)
      5 
      6 #define TRANS_CORNERS_ROTATION(T, c) \
      7         compose_corners(compose_corners(TRANS_CUBE_ ## T, c), \
      8         TRANS_CUBE_ ## T ## _INVERSE)
      9 #define TRANS_CORNERS_MIRRORED(T, c) \
     10         invertco(compose_corners( \
     11 	compose_corners(TRANS_CUBE_ ## T, c), TRANS_CUBE_ ## T ## _INVERSE))
     12 
     13 #define TRANS_ROTATION(T, c) \
     14         compose(compose(TRANS_CUBE_ ## T, c), \
     15         TRANS_CUBE_ ## T ## _INVERSE)
     16 #define TRANS_MIRRORED(T, c) \
     17         invertco(compose(compose(TRANS_CUBE_ ## T, c), \
     18         TRANS_CUBE_ ## T ## _INVERSE))
     19 
     20 STATIC cube_t transform_edges(cube_t, uint8_t);
     21 STATIC cube_t transform_corners(cube_t, uint8_t);
     22 STATIC cube_t transform(cube_t, uint8_t);
     23 STATIC cube_t applytrans(cube_t, const char *);
     24 
     25 STATIC cube_t
     26 transform_edges(cube_t c, uint8_t t)
     27 {
     28 	switch (t) {
     29 	case TRANS_UFr:
     30 		return c;
     31 	case TRANS_ULr:
     32 		return TRANS_EDGES_ROTATION(ULr, c);
     33 	case TRANS_UBr:
     34 		return TRANS_EDGES_ROTATION(UBr, c);
     35 	case TRANS_URr:
     36 		return TRANS_EDGES_ROTATION(URr, c);
     37 	case TRANS_DFr:
     38 		return TRANS_EDGES_ROTATION(DFr, c);
     39 	case TRANS_DLr:
     40 		return TRANS_EDGES_ROTATION(DLr, c);
     41 	case TRANS_DBr:
     42 		return TRANS_EDGES_ROTATION(DBr, c);
     43 	case TRANS_DRr:
     44 		return TRANS_EDGES_ROTATION(DRr, c);
     45 	case TRANS_RUr:
     46 		return TRANS_EDGES_ROTATION(RUr, c);
     47 	case TRANS_RFr:
     48 		return TRANS_EDGES_ROTATION(RFr, c);
     49 	case TRANS_RDr:
     50 		return TRANS_EDGES_ROTATION(RDr, c);
     51 	case TRANS_RBr:
     52 		return TRANS_EDGES_ROTATION(RBr, c);
     53 	case TRANS_LUr:
     54 		return TRANS_EDGES_ROTATION(LUr, c);
     55 	case TRANS_LFr:
     56 		return TRANS_EDGES_ROTATION(LFr, c);
     57 	case TRANS_LDr:
     58 		return TRANS_EDGES_ROTATION(LDr, c);
     59 	case TRANS_LBr:
     60 		return TRANS_EDGES_ROTATION(LBr, c);
     61 	case TRANS_FUr:
     62 		return TRANS_EDGES_ROTATION(FUr, c);
     63 	case TRANS_FRr:
     64 		return TRANS_EDGES_ROTATION(FRr, c);
     65 	case TRANS_FDr:
     66 		return TRANS_EDGES_ROTATION(FDr, c);
     67 	case TRANS_FLr:
     68 		return TRANS_EDGES_ROTATION(FLr, c);
     69 	case TRANS_BUr:
     70 		return TRANS_EDGES_ROTATION(BUr, c);
     71 	case TRANS_BRr:
     72 		return TRANS_EDGES_ROTATION(BRr, c);
     73 	case TRANS_BDr:
     74 		return TRANS_EDGES_ROTATION(BDr, c);
     75 	case TRANS_BLr:
     76 		return TRANS_EDGES_ROTATION(BLr, c);
     77 	case TRANS_UFm:
     78 		return TRANS_EDGES_MIRRORED(UFm, c);
     79 	case TRANS_ULm:
     80 		return TRANS_EDGES_MIRRORED(ULm, c);
     81 	case TRANS_UBm:
     82 		return TRANS_EDGES_MIRRORED(UBm, c);
     83 	case TRANS_URm:
     84 		return TRANS_EDGES_MIRRORED(URm, c);
     85 	case TRANS_DFm:
     86 		return TRANS_EDGES_MIRRORED(DFm, c);
     87 	case TRANS_DLm:
     88 		return TRANS_EDGES_MIRRORED(DLm, c);
     89 	case TRANS_DBm:
     90 		return TRANS_EDGES_MIRRORED(DBm, c);
     91 	case TRANS_DRm:
     92 		return TRANS_EDGES_MIRRORED(DRm, c);
     93 	case TRANS_RUm:
     94 		return TRANS_EDGES_MIRRORED(RUm, c);
     95 	case TRANS_RFm:
     96 		return TRANS_EDGES_MIRRORED(RFm, c);
     97 	case TRANS_RDm:
     98 		return TRANS_EDGES_MIRRORED(RDm, c);
     99 	case TRANS_RBm:
    100 		return TRANS_EDGES_MIRRORED(RBm, c);
    101 	case TRANS_LUm:
    102 		return TRANS_EDGES_MIRRORED(LUm, c);
    103 	case TRANS_LFm:
    104 		return TRANS_EDGES_MIRRORED(LFm, c);
    105 	case TRANS_LDm:
    106 		return TRANS_EDGES_MIRRORED(LDm, c);
    107 	case TRANS_LBm:
    108 		return TRANS_EDGES_MIRRORED(LBm, c);
    109 	case TRANS_FUm:
    110 		return TRANS_EDGES_MIRRORED(FUm, c);
    111 	case TRANS_FRm:
    112 		return TRANS_EDGES_MIRRORED(FRm, c);
    113 	case TRANS_FDm:
    114 		return TRANS_EDGES_MIRRORED(FDm, c);
    115 	case TRANS_FLm:
    116 		return TRANS_EDGES_MIRRORED(FLm, c);
    117 	case TRANS_BUm:
    118 		return TRANS_EDGES_MIRRORED(BUm, c);
    119 	case TRANS_BRm:
    120 		return TRANS_EDGES_MIRRORED(BRm, c);
    121 	case TRANS_BDm:
    122 		return TRANS_EDGES_MIRRORED(BDm, c);
    123 	case TRANS_BLm:
    124 		return TRANS_EDGES_MIRRORED(BLm, c);
    125 	default:
    126 		LOG("transform error, unknown transformation %" PRIu8 "\n", t);
    127 		return ZERO_CUBE;
    128 	}
    129 }
    130 
    131 STATIC cube_t
    132 transform_corners(cube_t c, uint8_t t)
    133 {
    134 	switch (t) {
    135 	case TRANS_UFr:
    136 		return c;
    137 	case TRANS_ULr:
    138 		return TRANS_CORNERS_ROTATION(ULr, c);
    139 	case TRANS_UBr:
    140 		return TRANS_CORNERS_ROTATION(UBr, c);
    141 	case TRANS_URr:
    142 		return TRANS_CORNERS_ROTATION(URr, c);
    143 	case TRANS_DFr:
    144 		return TRANS_CORNERS_ROTATION(DFr, c);
    145 	case TRANS_DLr:
    146 		return TRANS_CORNERS_ROTATION(DLr, c);
    147 	case TRANS_DBr:
    148 		return TRANS_CORNERS_ROTATION(DBr, c);
    149 	case TRANS_DRr:
    150 		return TRANS_CORNERS_ROTATION(DRr, c);
    151 	case TRANS_RUr:
    152 		return TRANS_CORNERS_ROTATION(RUr, c);
    153 	case TRANS_RFr:
    154 		return TRANS_CORNERS_ROTATION(RFr, c);
    155 	case TRANS_RDr:
    156 		return TRANS_CORNERS_ROTATION(RDr, c);
    157 	case TRANS_RBr:
    158 		return TRANS_CORNERS_ROTATION(RBr, c);
    159 	case TRANS_LUr:
    160 		return TRANS_CORNERS_ROTATION(LUr, c);
    161 	case TRANS_LFr:
    162 		return TRANS_CORNERS_ROTATION(LFr, c);
    163 	case TRANS_LDr:
    164 		return TRANS_CORNERS_ROTATION(LDr, c);
    165 	case TRANS_LBr:
    166 		return TRANS_CORNERS_ROTATION(LBr, c);
    167 	case TRANS_FUr:
    168 		return TRANS_CORNERS_ROTATION(FUr, c);
    169 	case TRANS_FRr:
    170 		return TRANS_CORNERS_ROTATION(FRr, c);
    171 	case TRANS_FDr:
    172 		return TRANS_CORNERS_ROTATION(FDr, c);
    173 	case TRANS_FLr:
    174 		return TRANS_CORNERS_ROTATION(FLr, c);
    175 	case TRANS_BUr:
    176 		return TRANS_CORNERS_ROTATION(BUr, c);
    177 	case TRANS_BRr:
    178 		return TRANS_CORNERS_ROTATION(BRr, c);
    179 	case TRANS_BDr:
    180 		return TRANS_CORNERS_ROTATION(BDr, c);
    181 	case TRANS_BLr:
    182 		return TRANS_CORNERS_ROTATION(BLr, c);
    183 	case TRANS_UFm:
    184 		return TRANS_CORNERS_MIRRORED(UFm, c);
    185 	case TRANS_ULm:
    186 		return TRANS_CORNERS_MIRRORED(ULm, c);
    187 	case TRANS_UBm:
    188 		return TRANS_CORNERS_MIRRORED(UBm, c);
    189 	case TRANS_URm:
    190 		return TRANS_CORNERS_MIRRORED(URm, c);
    191 	case TRANS_DFm:
    192 		return TRANS_CORNERS_MIRRORED(DFm, c);
    193 	case TRANS_DLm:
    194 		return TRANS_CORNERS_MIRRORED(DLm, c);
    195 	case TRANS_DBm:
    196 		return TRANS_CORNERS_MIRRORED(DBm, c);
    197 	case TRANS_DRm:
    198 		return TRANS_CORNERS_MIRRORED(DRm, c);
    199 	case TRANS_RUm:
    200 		return TRANS_CORNERS_MIRRORED(RUm, c);
    201 	case TRANS_RFm:
    202 		return TRANS_CORNERS_MIRRORED(RFm, c);
    203 	case TRANS_RDm:
    204 		return TRANS_CORNERS_MIRRORED(RDm, c);
    205 	case TRANS_RBm:
    206 		return TRANS_CORNERS_MIRRORED(RBm, c);
    207 	case TRANS_LUm:
    208 		return TRANS_CORNERS_MIRRORED(LUm, c);
    209 	case TRANS_LFm:
    210 		return TRANS_CORNERS_MIRRORED(LFm, c);
    211 	case TRANS_LDm:
    212 		return TRANS_CORNERS_MIRRORED(LDm, c);
    213 	case TRANS_LBm:
    214 		return TRANS_CORNERS_MIRRORED(LBm, c);
    215 	case TRANS_FUm:
    216 		return TRANS_CORNERS_MIRRORED(FUm, c);
    217 	case TRANS_FRm:
    218 		return TRANS_CORNERS_MIRRORED(FRm, c);
    219 	case TRANS_FDm:
    220 		return TRANS_CORNERS_MIRRORED(FDm, c);
    221 	case TRANS_FLm:
    222 		return TRANS_CORNERS_MIRRORED(FLm, c);
    223 	case TRANS_BUm:
    224 		return TRANS_CORNERS_MIRRORED(BUm, c);
    225 	case TRANS_BRm:
    226 		return TRANS_CORNERS_MIRRORED(BRm, c);
    227 	case TRANS_BDm:
    228 		return TRANS_CORNERS_MIRRORED(BDm, c);
    229 	case TRANS_BLm:
    230 		return TRANS_CORNERS_MIRRORED(BLm, c);
    231 	default:
    232 		LOG("transform error: unknown transformation %" PRIu8 "\n", t);
    233 		return ZERO_CUBE;
    234 	}
    235 }
    236 
    237 STATIC cube_t
    238 transform(cube_t c, uint8_t t)
    239 {
    240 	switch (t) {
    241 	case TRANS_UFr:
    242 		return c;
    243 	case TRANS_ULr:
    244 		return TRANS_ROTATION(ULr, c);
    245 	case TRANS_UBr:
    246 		return TRANS_ROTATION(UBr, c);
    247 	case TRANS_URr:
    248 		return TRANS_ROTATION(URr, c);
    249 	case TRANS_DFr:
    250 		return TRANS_ROTATION(DFr, c);
    251 	case TRANS_DLr:
    252 		return TRANS_ROTATION(DLr, c);
    253 	case TRANS_DBr:
    254 		return TRANS_ROTATION(DBr, c);
    255 	case TRANS_DRr:
    256 		return TRANS_ROTATION(DRr, c);
    257 	case TRANS_RUr:
    258 		return TRANS_ROTATION(RUr, c);
    259 	case TRANS_RFr:
    260 		return TRANS_ROTATION(RFr, c);
    261 	case TRANS_RDr:
    262 		return TRANS_ROTATION(RDr, c);
    263 	case TRANS_RBr:
    264 		return TRANS_ROTATION(RBr, c);
    265 	case TRANS_LUr:
    266 		return TRANS_ROTATION(LUr, c);
    267 	case TRANS_LFr:
    268 		return TRANS_ROTATION(LFr, c);
    269 	case TRANS_LDr:
    270 		return TRANS_ROTATION(LDr, c);
    271 	case TRANS_LBr:
    272 		return TRANS_ROTATION(LBr, c);
    273 	case TRANS_FUr:
    274 		return TRANS_ROTATION(FUr, c);
    275 	case TRANS_FRr:
    276 		return TRANS_ROTATION(FRr, c);
    277 	case TRANS_FDr:
    278 		return TRANS_ROTATION(FDr, c);
    279 	case TRANS_FLr:
    280 		return TRANS_ROTATION(FLr, c);
    281 	case TRANS_BUr:
    282 		return TRANS_ROTATION(BUr, c);
    283 	case TRANS_BRr:
    284 		return TRANS_ROTATION(BRr, c);
    285 	case TRANS_BDr:
    286 		return TRANS_ROTATION(BDr, c);
    287 	case TRANS_BLr:
    288 		return TRANS_ROTATION(BLr, c);
    289 	case TRANS_UFm:
    290 		return TRANS_MIRRORED(UFm, c);
    291 	case TRANS_ULm:
    292 		return TRANS_MIRRORED(ULm, c);
    293 	case TRANS_UBm:
    294 		return TRANS_MIRRORED(UBm, c);
    295 	case TRANS_URm:
    296 		return TRANS_MIRRORED(URm, c);
    297 	case TRANS_DFm:
    298 		return TRANS_MIRRORED(DFm, c);
    299 	case TRANS_DLm:
    300 		return TRANS_MIRRORED(DLm, c);
    301 	case TRANS_DBm:
    302 		return TRANS_MIRRORED(DBm, c);
    303 	case TRANS_DRm:
    304 		return TRANS_MIRRORED(DRm, c);
    305 	case TRANS_RUm:
    306 		return TRANS_MIRRORED(RUm, c);
    307 	case TRANS_RFm:
    308 		return TRANS_MIRRORED(RFm, c);
    309 	case TRANS_RDm:
    310 		return TRANS_MIRRORED(RDm, c);
    311 	case TRANS_RBm:
    312 		return TRANS_MIRRORED(RBm, c);
    313 	case TRANS_LUm:
    314 		return TRANS_MIRRORED(LUm, c);
    315 	case TRANS_LFm:
    316 		return TRANS_MIRRORED(LFm, c);
    317 	case TRANS_LDm:
    318 		return TRANS_MIRRORED(LDm, c);
    319 	case TRANS_LBm:
    320 		return TRANS_MIRRORED(LBm, c);
    321 	case TRANS_FUm:
    322 		return TRANS_MIRRORED(FUm, c);
    323 	case TRANS_FRm:
    324 		return TRANS_MIRRORED(FRm, c);
    325 	case TRANS_FDm:
    326 		return TRANS_MIRRORED(FDm, c);
    327 	case TRANS_FLm:
    328 		return TRANS_MIRRORED(FLm, c);
    329 	case TRANS_BUm:
    330 		return TRANS_MIRRORED(BUm, c);
    331 	case TRANS_BRm:
    332 		return TRANS_MIRRORED(BRm, c);
    333 	case TRANS_BDm:
    334 		return TRANS_MIRRORED(BDm, c);
    335 	case TRANS_BLm:
    336 		return TRANS_MIRRORED(BLm, c);
    337 	default:
    338 		return ZERO_CUBE;
    339 	}
    340 }
    341 
    342 STATIC cube_t
    343 applytrans(cube_t cube, const char *buf)
    344 {
    345 	uint8_t t;
    346 
    347 	DBG_ASSERT(isconsistent(cube), ZERO_CUBE,
    348 	    "transformation error: inconsistent cube\n");
    349 
    350 	t = readtrans(buf);
    351 
    352 	if (t == UINT8_ERROR)
    353 		LOG("Unknown transformation '%s'\n", buf);
    354 
    355 	return transform(cube, t);
    356 }