aboutsummaryrefslogtreecommitdiff
path: root/src/cube.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cube.c')
-rw-r--r--src/cube.c237
1 files changed, 237 insertions, 0 deletions
diff --git a/src/cube.c b/src/cube.c
index f6e6a8f..b853b06 100644
--- a/src/cube.c
+++ b/src/cube.c
@@ -181,6 +181,7 @@ cube_t solvedcube = {
181static cube_t errorcube = { .e = {0}, .c = {0} }; 181static cube_t errorcube = { .e = {0}, .c = {0} };
182 182
183static bool isconsistent(cube_t); 183static bool isconsistent(cube_t);
184static cube_t flipallcorners(cube_t);
184static uint8_t readco(char *); 185static uint8_t readco(char *);
185static uint8_t readcp(char *); 186static uint8_t readcp(char *);
186static uint8_t readeo(char *); 187static uint8_t readeo(char *);
@@ -904,8 +905,244 @@ compose_inconsistent:
904 return errorcube; 905 return errorcube;
905} 906}
906 907
908static cube_t
909flipallcorners(cube_t c)
910{
911 uint8_t i, piece, orien;
912 cube_t ret;
913
914 ret = c;
915 for (i = 0; i < 8; i++) {
916 piece = ret.c[i];
917 orien = ((piece << 1) | (piece >> 1)) & _cobits2;
918 ret.c[i] = (piece & _pbits) | orien;
919 }
920
921 return ret;
922}
923
907cube_t 924cube_t
908transform(cube_t c, trans_t t) 925transform(cube_t c, trans_t t)
909{ 926{
927 static cube_t trans_move_cube[] = {
928 [UFr] = {
929 .c = {0, 1, 2, 3, 4, 5, 6, 7},
930 .e = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
931 },
932 [ULr] = {
933 .c = {4, 5, 7, 6, 1, 0, 2, 3},
934 .e = {5, 4, 7, 6, 0, 1, 2, 3, 25, 26, 27, 24}
935 },
936 [UBr] = {
937 .c = {1, 0, 3, 2, 5, 4, 7, 6},
938 .e = {1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9}
939 },
940 [URr] = {
941 .c = {5, 4, 6, 7, 0, 1, 3, 2},
942 .e = {4, 5, 6, 7, 1, 0, 3, 2, 27, 24, 25, 26}
943 },
944 [DFr] = {
945 .c = {2, 3, 0, 1, 6, 7, 4, 5},
946 .e = {3, 2, 1, 0, 6, 7, 4, 5, 9, 8, 11, 10}
947 },
948 [DLr] = {
949 .c = {7, 6, 4, 5, 2, 3, 1, 0},
950 .e = {6, 7, 4, 5, 2, 3, 0, 1, 26, 25, 24, 27}
951 },
952 [DBr] = {
953 .c = {3, 2, 1, 0, 7, 6, 5, 4},
954 .e = {2, 3, 0, 1, 7, 6, 5, 4, 11, 10, 9, 8}
955 },
956 [DRr] = {
957 .c = {6, 7, 5, 4, 3, 2, 0, 1},
958 .e = {7, 6, 5, 4, 3, 2, 1, 0, 24, 27, 26, 25}
959 },
960 [RUr] = {
961 .c = {64, 67, 65, 66, 37, 38, 36, 39},
962 .e = {20, 23, 22, 21, 24, 27, 26, 25, 0, 1, 2, 3}
963 },
964 [RFr] = {
965 .c = {38, 37, 36, 39, 64, 67, 66, 65},
966 .e = {24, 27, 26, 25, 23, 20, 21, 22, 19, 16, 17, 18}
967 },
968 [RDr] = {
969 .c = {67, 64, 66, 65, 38, 37, 39, 36},
970 .e = {23, 20, 21, 22, 27, 24, 25, 26, 2, 3, 0, 1}
971 },
972 [RBr] = {
973 .c = {37, 38, 39, 36, 67, 64, 65, 66},
974 .e = {27, 24, 25, 26, 20, 23, 22, 21, 17, 18, 19, 16}
975 },
976 [LUr] = {
977 .c = {65, 66, 64, 67, 36, 39, 37, 38},
978 .e = {21, 22, 23, 20, 26, 25, 24, 27, 1, 0, 3, 2}
979 },
980 [LFr] = {
981 .c = {36, 39, 38, 37, 66, 65, 64, 67},
982 .e = {25, 26, 27, 24, 21, 22, 23, 20, 16, 19, 18, 17}
983 },
984 [LDr] = {
985 .c = {66, 65, 67, 64, 39, 36, 38, 37},
986 .e = {22, 21, 20, 23, 25, 26, 27, 24, 3, 2, 1, 0}
987 },
988 [LBr] = {
989 .c = {39, 36, 37, 38, 65, 66, 67, 64},
990 .e = {26, 25, 24, 27, 22, 21, 20, 23, 18, 17, 16, 19}
991 },
992 [FUr] = {
993 .c = {68, 70, 69, 71, 32, 34, 33, 35},
994 .e = {16, 19, 18, 17, 9, 8, 11, 10, 5, 4, 7, 6}
995 },
996 [FRr] = {
997 .c = {32, 34, 35, 33, 70, 68, 69, 71},
998 .e = {8, 9, 10, 11, 16, 19, 18, 17, 20, 23, 22, 21}
999 },
1000 [FDr] = {
1001 .c = {70, 68, 71, 69, 34, 32, 35, 33},
1002 .e = {19, 16, 17, 18, 8, 9, 10, 11, 7, 6, 5, 4}
1003 },
1004 [FLr] = {
1005 .c = {34, 32, 33, 35, 68, 70, 71, 69},
1006 .e = {9, 8, 11, 10, 19, 16, 17, 18, 22, 21, 20, 23}
1007 },
1008 [BUr] = {
1009 .c = {69, 71, 68, 70, 33, 35, 32, 34},
1010 .e = {17, 18, 19, 16, 11, 10, 9, 8, 4, 5, 6, 7}
1011 },
1012 [BRr] = {
1013 .c = {35, 33, 32, 34, 69, 71, 70, 68},
1014 .e = {11, 10, 9, 8, 18, 17, 16, 19, 23, 20, 21, 22}
1015 },
1016 [BDr] = {
1017 .c = {71, 69, 70, 68, 35, 33, 34, 32},
1018 .e = {18, 17, 16, 19, 10, 11, 8, 9, 6, 7, 4, 5}
1019 },
1020 [BLr] = {
1021 .c = {33, 35, 34, 32, 71, 69, 68, 70},
1022 .e = {10, 11, 8, 9, 17, 18, 19, 16, 21, 22, 23, 20}
1023 },
1024 [UFm] = {
1025 .c = {4, 5, 6, 7, 0, 1, 2, 3},
1026 .e = {0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10}
1027 },
1028 [ULm] = {
1029 .c = {0, 1, 3, 2, 5, 4, 6, 7},
1030 .e = {4, 5, 6, 7, 0, 1, 2, 3, 24, 27, 26, 25}
1031 },
1032 [UBm] = {
1033 .c = {5, 4, 7, 6, 1, 0, 3, 2},
1034 .e = {1, 0, 3, 2, 4, 5, 6, 7, 11, 10, 9, 8}
1035 },
1036 [URm] = {
1037 .c = {1, 0, 2, 3, 4, 5, 7, 6},
1038 .e = {5, 4, 7, 6, 1, 0, 3, 2, 26, 25, 24, 27}
1039 },
1040 [DFm] = {
1041 .c = {6, 7, 4, 5, 2, 3, 0, 1},
1042 .e = {3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11}
1043 },
1044 [DLm] = {
1045 .c = {3, 2, 0, 1, 6, 7, 5, 4},
1046 .e = {7, 6, 5, 4, 2, 3, 0, 1, 27, 24, 25, 26}
1047 },
1048 [DBm] = {
1049 .c = {7, 6, 5, 4, 3, 2, 1, 0},
1050 .e = {2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9}
1051 },
1052 [DRm] = {
1053 .c = {2, 3, 1, 0, 7, 6, 4, 5},
1054 .e = {6, 7, 4, 5, 3, 2, 1, 0, 25, 26, 27, 24}
1055 },
1056 [RUm] = {
1057 .c = {68, 71, 69, 70, 33, 34, 32, 35},
1058 .e = {21, 22, 23, 20, 25, 26, 27, 24, 0, 1, 2, 3}
1059 },
1060 [RFm] = {
1061 .c = {34, 33, 32, 35, 68, 71, 70, 69},
1062 .e = {25, 26, 27, 24, 22, 21, 20, 23, 19, 16, 17, 18}
1063 },
1064 [RDm] = {
1065 .c = {71, 68, 70, 69, 34, 33, 35, 32},
1066 .e = {22, 21, 20, 23, 26, 25, 24, 27, 2, 3, 0, 1}
1067 },
1068 [RBm] = {
1069 .c = {33, 34, 35, 32, 71, 68, 69, 70},
1070 .e = {26, 25, 24, 27, 21, 22, 23, 20, 17, 18, 19, 16}
1071 },
1072 [LUm] = {
1073 .c = {69, 70, 68, 71, 32, 35, 33, 34},
1074 .e = {20, 23, 22, 21, 27, 24, 25, 26, 1, 0, 3, 2}
1075 },
1076 [LFm] = {
1077 .c = {32, 35, 34, 33, 70, 69, 68, 71},
1078 .e = {24, 27, 26, 25, 20, 23, 22, 21, 16, 19, 18, 17}
1079 },
1080 [LDm] = {
1081 .c = {70, 69, 71, 68, 35, 32, 34, 33},
1082 .e = {23, 20, 21, 22, 24, 27, 26, 25, 3, 2, 1, 0}
1083 },
1084 [LBm] = {
1085 .c = {35, 32, 33, 34, 69, 70, 71, 68},
1086 .e = {27, 24, 25, 26, 23, 20, 21, 22, 18, 17, 16, 19}
1087 },
1088 [FUm] = {
1089 .c = {64, 66, 65, 67, 36, 38, 37, 39},
1090 .e = {16, 19, 18, 17, 8, 9, 10, 11, 4, 5, 6, 7}
1091 },
1092 [FRm] = {
1093 .c = {36, 38, 39, 37, 66, 64, 65, 67},
1094 .e = {9, 8, 11, 10, 16, 19, 18, 17, 21, 22, 23, 20}
1095 },
1096 [FDm] = {
1097 .c = {66, 64, 67, 65, 38, 36, 39, 37},
1098 .e = {19, 16, 17, 18, 9, 8, 11, 10, 6, 7, 4, 5}
1099 },
1100 [FLm] = {
1101 .c = {38, 36, 37, 39, 64, 66, 67, 65},
1102 .e = {8, 9, 10, 11, 19, 16, 17, 18, 23, 20, 21, 22}
1103 },
1104 [BUm] = {
1105 .c = {65, 67, 64, 66, 37, 39, 36, 38},
1106 .e = {17, 18, 19, 16, 10, 11, 8, 9, 5, 4, 7, 6}
1107 },
1108 [BRm] = {
1109 .c = {39, 37, 36, 38, 65, 67, 66, 64},
1110 .e = {10, 11, 8, 9, 18, 17, 16, 19, 22, 21, 20, 23}
1111 },
1112 [BDm] = {
1113 .c = {67, 65, 66, 64, 39, 37, 38, 36},
1114 .e = {18, 17, 16, 19, 11, 10, 9, 8, 7, 6, 5, 4}
1115 },
1116 [BLm] = {
1117 .c = {37, 39, 38, 36, 67, 65, 64, 66},
1118 .e = {11, 10, 9, 8, 17, 18, 19, 16, 20, 23, 22, 21}
1119 },
1120 };
1121
1122 cube_t ret;
1123
1124#ifdef DEBUG
1125 if (!isconsistent(c))
1126 goto transform_inconsistent;
1127 if (t >= 48)
1128 goto transform_errortrans;
1129#endif
1130
1131 ret = compose(solvedcube, trans_move_cube[t]);
1132 ret = compose(ret, c);
1133 /* TODO: pre-compute inverse for performance */
1134 ret = compose(ret, inverse(trans_move_cube[t]));
1135
1136 /* TODO: work out a better way to do this */
1137 if (t >= 24)
1138 ret = flipallcorners(ret);
1139
1140 return ret;
1141
1142transform_inconsistent:
1143 fprintf(stderr, "transform error, inconsistent cube\n");
1144 return errorcube;
1145transform_errortrans:
1146 fprintf(stderr, "transform error, unknown transformation\n");
910 return errorcube; 1147 return errorcube;
911} 1148}

Generated with cgit - Back to sebastiano.tronto.net