| 956 | |
| 957 | |
| 958 | def tikz(self, view=[0,0,1], angle=0, scale=2, |
| 959 | edge_color='blue!95!black', facet_color='blue!95!black', |
| 960 | opacity=0.8, vertex_color='green', axis=False): |
| 961 | """ |
| 962 | Return a string ``tikz_pic`` consisting of a tikz picture of ``self`` |
| 963 | according to a projection ``view`` and an angle ``angle`` |
| 964 | obtained via Jmol through the current state property. |
| 965 | |
| 966 | INPUT: |
| 967 | |
| 968 | - ``view`` - list (default: [0,0,1]) representing the rotation axis (see note below). |
| 969 | - ``angle`` - integer (default: 0) angle of rotation in degree from 0 to 360 (see note |
| 970 | below). |
| 971 | - ``scale`` - integer (default: 2) specifying the scaling of the tikz picture. |
| 972 | - ``edge_color`` - string (default: 'blue!95!black') representing colors which tikz |
| 973 | recognize. |
| 974 | - ``facet_color`` - string (default: 'blue!95!black') representing colors which tikz |
| 975 | recognize. |
| 976 | - ``vertex_color`` - string (default: 'green') representing colors which tikz |
| 977 | recognize. |
| 978 | - ``opacity`` - real number (default: 0.8) between 0 and 1 giving the opacity of |
| 979 | the front facets. |
| 980 | - ``axis`` - Boolean (default: False) draw the axes at the origin or not. |
| 981 | |
| 982 | OUTPUT: |
| 983 | |
| 984 | - LatexExpr -- containing the TikZ picture. |
| 985 | |
| 986 | .. NOTE:: |
| 987 | |
| 988 | The inputs ``view`` and ``angle`` can be obtained from the viewer |
| 989 | Jmol: |
| 990 | |
| 991 | 1) Right click on the image; |
| 992 | 2) Select ``Console``; |
| 993 | 3) Select the tab ``State``; |
| 994 | 4) Scroll to the line ``moveto`` |
| 995 | |
| 996 | It read something like:: |
| 997 | |
| 998 | moveto 0.0 {x y z angle} Scale |
| 999 | |
| 1000 | The ``view`` is then [x,y,z] and ``angle`` is angle. |
| 1001 | The following number is the scale. |
| 1002 | Jmol performs a rotation of ``angle`` degrees along the vector [x,y,z] and show |
| 1003 | the result from the z-axis. |
| 1004 | |
| 1005 | EXAMPLES:: |
| 1006 | |
| 1007 | sage: P1 = polytopes.small_rhombicuboctahedron() |
| 1008 | sage: Image1 = P1.projection().tikz([1,3,5], 175, scale=4) |
| 1009 | sage: type(Image1) |
| 1010 | <class 'sage.misc.latex.LatexExpr'> |
| 1011 | sage: print '\n'.join(Image1.splitlines()[:4]) |
| 1012 | \begin{tikzpicture}% |
| 1013 | [x={(-0.939161cm, 0.244762cm)}, |
| 1014 | y={(0.097442cm, -0.482887cm)}, |
| 1015 | z={(0.329367cm, 0.840780cm)}, |
| 1016 | sage: open('polytope-tikz1.tex', 'w').write(Image1) # not tested |
| 1017 | |
| 1018 | sage: P2 = Polyhedron(vertices=[[1, 1],[1, 2],[2, 1]]) |
| 1019 | sage: Image2 = P2.projection().tikz(scale=3, edge_color='blue!95!black', facet_color='orange!95!black', opacity=0.4, vertex_color='yellow', axis=True) |
| 1020 | sage: type(Image1) |
| 1021 | <class 'sage.misc.latex.LatexExpr'> |
| 1022 | sage: print '\n'.join(Image2.splitlines()[:4]) |
| 1023 | \begin{tikzpicture}% |
| 1024 | [scale=3.000000, |
| 1025 | back/.style={loosely dotted, thin}, |
| 1026 | edge/.style={color=blue!95!black, thick}, |
| 1027 | sage: open('polytope-tikz2.tex', 'w').write(Image2) # not tested |
| 1028 | |
| 1029 | sage: P3 = Polyhedron(vertices=[[-1, -1, 2],[-1, 2, -1],[2, -1, -1]]) |
| 1030 | sage: P3 |
| 1031 | A 2-dimensional polyhedron in QQ^3 defined as the convex hull of 3 vertices |
| 1032 | sage: Image3 = P3.projection().tikz([0.5,-1,-0.1], 55, scale=3, edge_color='blue!95!black',facet_color='orange!95!black', opacity=0.7, vertex_color='yellow', axis=True) |
| 1033 | sage: print '\n'.join(Image3.splitlines()[:4]) |
| 1034 | \begin{tikzpicture}% |
| 1035 | [x={(0.658184cm, -0.242192cm)}, |
| 1036 | y={(-0.096240cm, 0.912008cm)}, |
| 1037 | z={(-0.746680cm, -0.331036cm)}, |
| 1038 | sage: open('polytope-tikz3.tex', 'w').write(Image3) # not tested |
| 1039 | |
| 1040 | sage: P=Polyhedron(vertices=[[1,1,0,0],[1,2,0,0],[2,1,0,0],[0,0,1,0],[0,0,0,1]]) |
| 1041 | sage: P |
| 1042 | A 4-dimensional polyhedron in QQ^4 defined as the convex hull of 5 vertices |
| 1043 | sage: P.projection().tikz() |
| 1044 | Traceback (most recent call last): |
| 1045 | ... |
| 1046 | NotImplementedError: The polytope has to live in 2 or 3 dimensions. |
| 1047 | |
| 1048 | .. TO DO:: |
| 1049 | |
| 1050 | Make it possible to draw Schlegel diagram for 4-polytope. |
| 1051 | |
| 1052 | sage: P=Polyhedron(vertices=[[1,1,0,0],[1,2,0,0],[2,1,0,0],[0,0,1,0],[0,0,0,1]]) |
| 1053 | sage: P |
| 1054 | A 4-dimensional polyhedron in QQ^4 defined as the convex hull of 5 vertices |
| 1055 | sage: P.projection().tikz() |
| 1056 | Traceback (most recent call last): |
| 1057 | ... |
| 1058 | NotImplementedError: The polytope has to live in 2 or 3 dimensions. |
| 1059 | |
| 1060 | Make it possible to draw 3-polytope living in higher dimension. |
| 1061 | |
| 1062 | """ |
| 1063 | if self.polyhedron_ambient_dim > 3 or self.polyhedron_ambient_dim < 2: |
| 1064 | raise NotImplementedError("The polytope has to live in 2 or 3 dimensions.") |
| 1065 | elif self.polyhedron_ambient_dim == 2: |
| 1066 | return self._tikz_2d(scale, edge_color, facet_color, opacity, |
| 1067 | vertex_color, axis) |
| 1068 | elif self.dimension == 2: |
| 1069 | return self._tikz_2d_in_3d(view, angle, scale, edge_color, |
| 1070 | facet_color, opacity, vertex_color, axis) |
| 1071 | else: |
| 1072 | return self._tikz_3d_in_3d(view, angle, scale, edge_color, |
| 1073 | facet_color, opacity, vertex_color, axis) |
| 1074 | |
| 1075 | def _tikz_2d(self, scale, edge_color, facet_color, opacity, vertex_color, axis): |
| 1076 | """ |
| 1077 | Return a string ``tikz_pic`` consisting of a tikz picture of ``self`` |
| 1078 | |
| 1079 | INPUT: |
| 1080 | |
| 1081 | - ``scale`` - integer specifying the scaling of the tikz picture. |
| 1082 | - ``edge_color`` - string representing colors which tikz |
| 1083 | recognize. |
| 1084 | - ``facet_color`` - string representing colors which tikz |
| 1085 | recognize. |
| 1086 | - ``vertex_color`` - string representing colors which tikz |
| 1087 | recognize. |
| 1088 | - ``opacity`` - real number between 0 and 1 giving the opacity of |
| 1089 | the front facets. |
| 1090 | - ``axis`` - Boolean (default: False) draw the axes at the origin or not. |
| 1091 | |
| 1092 | OUTPUT: |
| 1093 | |
| 1094 | - LatexExpr -- containing the TikZ picture. |
| 1095 | |
| 1096 | EXAMPLES:: |
| 1097 | |
| 1098 | sage: P = Polyhedron(vertices=[[1, 1],[1, 2],[2, 1]]) |
| 1099 | sage: Image = P.projection()._tikz_2d(scale=3, edge_color='black', facet_color='orange', opacity=0.75, vertex_color='yellow', axis=True) |
| 1100 | sage: type(Image) |
| 1101 | <class 'sage.misc.latex.LatexExpr'> |
| 1102 | sage: print '\n'.join(Image.splitlines()[:4]) |
| 1103 | \begin{tikzpicture}% |
| 1104 | [scale=3.000000, |
| 1105 | back/.style={loosely dotted, thin}, |
| 1106 | edge/.style={color=black, thick}, |
| 1107 | sage: open('polytope-tikz2.tex', 'w').write(Image2) # not tested |
| 1108 | |
| 1109 | .. NOTE:: |
| 1110 | |
| 1111 | The ``facet_color`` is the filing color of the polytope (polygon). |
| 1112 | """ |
| 1113 | |
| 1114 | # Creates the nodes, coordinate and tag for every vertex of the polytope. |
| 1115 | # The tag is used to draw the front facets later on. |
| 1116 | dict_drawing = {} |
| 1117 | edges = '' |
| 1118 | for index1,index2 in self.lines: |
| 1119 | v = self.coords[index1] |
| 1120 | v_vect = str([i.n(digits=3) for i in v]) |
| 1121 | v_vect = v_vect.replace('[','(') |
| 1122 | v_vect = v_vect.replace(']',')') |
| 1123 | tag = '%s' %v_vect |
| 1124 | node = "\\node[%s] at %s {};\n" %('vertex',tag) |
| 1125 | |
| 1126 | nei = self.coords[index2] |
| 1127 | nei_vect = str([i.n(digits=3) for i in nei]) |
| 1128 | nei_vect = nei_vect.replace('[','(') |
| 1129 | nei_vect = nei_vect.replace(']',')') |
| 1130 | tag_nei = '%s' %nei_vect |
| 1131 | edges += "\\draw[%s] %s -- %s;\n" %('edge',tag,tag_nei) |
| 1132 | |
| 1133 | coord = '\coordinate %s at %s;\n' %(tag,tag) |
| 1134 | dict_drawing[tuple(v)] = node,coord,tag |
| 1135 | |
| 1136 | # Start to write the output |
| 1137 | tikz_pic = '' |
| 1138 | tikz_pic += '\\begin{tikzpicture}%\n' |
| 1139 | tikz_pic += '\t[scale=%f,\n' %scale |
| 1140 | tikz_pic += '\tback/.style={loosely dotted, thin},\n' |
| 1141 | tikz_pic += '\tedge/.style={color=%s, thick},\n' %edge_color |
| 1142 | tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' %(facet_color,opacity) |
| 1143 | tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' %vertex_color |
| 1144 | tikz_pic += 'fill=%s!75!black,thick,anchor=base}]\n%%\n%%\n' %vertex_color |
| 1145 | |
| 1146 | # Draws the axes if True |
| 1147 | if axis: |
| 1148 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};\n' |
| 1149 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};\n' |
| 1150 | |
| 1151 | # Create the coordinate of the vertices: |
| 1152 | tikz_pic += '%% Coordinate of the vertices:\n%%\n' |
| 1153 | for v in dict_drawing: |
| 1154 | tikz_pic += dict_drawing[v][1] |
| 1155 | |
| 1156 | # Draw the interior by going in a cycle |
| 1157 | tikz_pic += '%%\n%%\n%% Drawing the interior\n%%\n' |
| 1158 | cyclic_vert = cyclic_sort_vertices_2d(list(self.parent_polyhedron.Vrep_generator())) |
| 1159 | tikz_pic += '\\fill[facet] ' |
| 1160 | for v in cyclic_vert: |
| 1161 | if tuple(v) in dict_drawing: |
| 1162 | tikz_pic += '%s -- ' %dict_drawing[tuple(v)][2] |
| 1163 | tikz_pic += 'cycle {};\n' |
| 1164 | |
| 1165 | # Draw the edges in the front |
| 1166 | tikz_pic += '%%\n%%\n%% Drawing edges in the front\n%%\n' |
| 1167 | tikz_pic += edges |
| 1168 | |
| 1169 | # Finally, the vertices in front are drawn on top of everything. |
| 1170 | tikz_pic += '%%\n%%\n%% Drawing the vertices in the front\n%%\n' |
| 1171 | for v in dict_drawing: |
| 1172 | tikz_pic += dict_drawing[v][0] |
| 1173 | tikz_pic += '%%\n%%\n\\end{tikzpicture}' |
| 1174 | |
| 1175 | return LatexExpr(tikz_pic) |
| 1176 | |
| 1177 | def _tikz_2d_in_3d(self, view, angle, scale, edge_color, facet_color, |
| 1178 | opacity, vertex_color, axis): |
| 1179 | """ |
| 1180 | Return a string ``tikz_pic`` consisting of a tikz picture of ``self`` |
| 1181 | according to a projection ``view`` and an angle ``angle`` |
| 1182 | obtained via Jmol through the current state property. |
| 1183 | |
| 1184 | INPUT: |
| 1185 | |
| 1186 | - ``view`` - list (default: [0,0,1]) representing the rotation axis. |
| 1187 | - ``angle`` - integer angle of rotation in degree from 0 to 360. |
| 1188 | - ``scale`` - integer specifying the scaling of the tikz picture. |
| 1189 | - ``edge_color`` - string representing colors which tikz |
| 1190 | recognize. |
| 1191 | - ``facet_color`` - string representing colors which tikz |
| 1192 | recognize. |
| 1193 | - ``vertex_color`` - string representing colors which tikz |
| 1194 | recognize. |
| 1195 | - ``opacity`` - real number between 0 and 1 giving the opacity of |
| 1196 | the front facets. |
| 1197 | - ``axis`` - Boolean draw the axes at the origin or not. |
| 1198 | |
| 1199 | OUTPUT: |
| 1200 | |
| 1201 | - LatexExpr -- containing the TikZ picture. |
| 1202 | |
| 1203 | EXAMPLE:: |
| 1204 | |
| 1205 | sage: P = Polyhedron(vertices=[[-1, -1, 2],[-1, 2, -1],[2, -1, -1]]) |
| 1206 | sage: P |
| 1207 | A 2-dimensional polyhedron in QQ^3 defined as the convex hull of 3 vertices |
| 1208 | sage: Image = P.projection()._tikz_2d_in_3d(view=[0.5,-1,-0.5], angle=55, scale=3, edge_color='blue!95!black', facet_color='orange', opacity=0.5, vertex_color='yellow', axis=True) |
| 1209 | sage: print '\n'.join(Image.splitlines()[:4]) |
| 1210 | \begin{tikzpicture}% |
| 1211 | [x={(0.644647cm, -0.476559cm)}, |
| 1212 | y={(0.192276cm, 0.857859cm)}, |
| 1213 | z={(-0.739905cm, -0.192276cm)}, |
| 1214 | sage: open('polytope-tikz3.tex', 'w').write(Image3) # not tested |
| 1215 | |
| 1216 | .. NOTE:: |
| 1217 | |
| 1218 | The ``facet_color`` is the filing color of the polytope (polygon). |
| 1219 | |
| 1220 | """ |
| 1221 | |
| 1222 | view_vector = vector(RDF, view) |
| 1223 | rot = rotate_arbitrary(view_vector,-(angle/360)*2*pi) |
| 1224 | rotation_matrix = rot[:2].transpose() |
| 1225 | |
| 1226 | # Creates the nodes, coordinate and tag for every vertex of the polytope. |
| 1227 | # The tag is used to draw the front facets later on. |
| 1228 | dict_drawing = {} |
| 1229 | edges = '' |
| 1230 | for index1, index2 in self.lines: |
| 1231 | v = self.coords[index1] |
| 1232 | v_vect = str([i.n(digits=3) for i in v]) |
| 1233 | v_vect = v_vect.replace('[','(') |
| 1234 | v_vect = v_vect.replace(']',')') |
| 1235 | tag = '%s' %v_vect |
| 1236 | node = "\\node[%s] at %s {};\n" %('vertex',tag) |
| 1237 | |
| 1238 | nei = self.coords[index2] |
| 1239 | nei_vect = str([i.n(digits=3) for i in nei]) |
| 1240 | nei_vect = nei_vect.replace('[','(') |
| 1241 | nei_vect = nei_vect.replace(']',')') |
| 1242 | tag_nei = '%s' %nei_vect |
| 1243 | edges += "\\draw[%s] %s -- %s;\n" %('edge',tag,tag_nei) |
| 1244 | |
| 1245 | coord = '\coordinate %s at %s;\n' %(tag,tag) |
| 1246 | dict_drawing[tuple(v)] = node,coord,tag |
| 1247 | |
| 1248 | # Start to write the output |
| 1249 | tikz_pic = '' |
| 1250 | tikz_pic += '\\begin{tikzpicture}%\n' |
| 1251 | tikz_pic += '\t[x={(%fcm, %fcm)},\n' %(RDF(rotation_matrix[0][0]),RDF(rotation_matrix[0][1])) |
| 1252 | tikz_pic += '\ty={(%fcm, %fcm)},\n' %(RDF(rotation_matrix[1][0]),RDF(rotation_matrix[1][1])) |
| 1253 | tikz_pic += '\tz={(%fcm, %fcm)},\n' %(RDF(rotation_matrix[2][0]),RDF(rotation_matrix[2][1])) |
| 1254 | tikz_pic += '\tscale=%f,\n' %scale |
| 1255 | tikz_pic += '\tback/.style={loosely dotted, thin},\n' |
| 1256 | tikz_pic += '\tedge/.style={color=%s, thick},\n' %edge_color |
| 1257 | tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' %(facet_color,opacity) |
| 1258 | tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' %vertex_color |
| 1259 | tikz_pic += 'fill=%s!75!black,thick,anchor=base}]\n%%\n%%\n' %vertex_color |
| 1260 | |
| 1261 | # Draws the axes if True |
| 1262 | if axis: |
| 1263 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};\n' |
| 1264 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};\n' |
| 1265 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};\n' |
| 1266 | |
| 1267 | # Create the coordinate of the vertices: |
| 1268 | tikz_pic += '%% Coordinate of the vertices:\n%%\n' |
| 1269 | for v in dict_drawing: |
| 1270 | tikz_pic += dict_drawing[v][1] |
| 1271 | |
| 1272 | # Draw the interior by going in a cycle |
| 1273 | tikz_pic += '%%\n%%\n%% Drawing the interior\n%%\n' |
| 1274 | cyclic_vert = cyclic_sort_vertices_2d(list(self.parent_polyhedron.Vrep_generator())) |
| 1275 | tikz_pic += '\\fill[facet] ' |
| 1276 | for v in cyclic_vert: |
| 1277 | if tuple(v) in dict_drawing: |
| 1278 | tikz_pic += '%s -- ' %dict_drawing[tuple(v)][2] |
| 1279 | tikz_pic += 'cycle {};\n' |
| 1280 | |
| 1281 | # Draw the edges in the front |
| 1282 | tikz_pic += '%%\n%%\n%% Drawing edges in the front\n%%\n' |
| 1283 | tikz_pic += edges |
| 1284 | |
| 1285 | # Finally, the vertices in front are drawn on top of everything. |
| 1286 | tikz_pic += '%%\n%%\n%% Drawing the vertices in the front\n%%\n' |
| 1287 | for v in dict_drawing: |
| 1288 | tikz_pic += dict_drawing[v][0] |
| 1289 | tikz_pic += '%%\n%%\n\\end{tikzpicture}' |
| 1290 | |
| 1291 | return LatexExpr(tikz_pic) |
| 1292 | |
| 1293 | def _tikz_3d_in_3d(self, view, angle, scale, edge_color, |
| 1294 | facet_color, opacity, vertex_color, axis): |
| 1295 | """ |
| 1296 | Return a string ``tikz_pic`` consisting of a tikz picture of ``self`` |
| 1297 | according to a projection ``view`` and an angle ``angle`` |
| 1298 | obtained via Jmol through the current state property. |
| 1299 | |
| 1300 | INPUT: |
| 1301 | |
| 1302 | - ``view`` - list (default: [0,0,1]) representing the rotation axis. |
| 1303 | - ``angle`` - integer angle of rotation in degree from 0 to 360. |
| 1304 | - ``scale`` - integer specifying the scaling of the tikz picture. |
| 1305 | - ``edge_color`` - string representing colors which tikz |
| 1306 | recognize. |
| 1307 | - ``facet_color`` - string representing colors which tikz |
| 1308 | recognize. |
| 1309 | - ``vertex_color`` - string representing colors which tikz |
| 1310 | recognize. |
| 1311 | - ``opacity`` - real number between 0 and 1 giving the opacity of |
| 1312 | the front facets. |
| 1313 | - ``axis`` - Boolean draw the axes at the origin or not. |
| 1314 | |
| 1315 | OUTPUT: |
| 1316 | |
| 1317 | - LatexExpr -- containing the TikZ picture. |
| 1318 | |
| 1319 | EXAMPLES:: |
| 1320 | |
| 1321 | sage: P = polytopes.small_rhombicuboctahedron() |
| 1322 | sage: Image = P.projection()._tikz_3d_in_3d([3,7,5], 100, scale=3, edge_color='blue', facet_color='orange', opacity=0.5, vertex_color='green', axis=True) |
| 1323 | sage: type(Image) |
| 1324 | <class 'sage.misc.latex.LatexExpr'> |
| 1325 | sage: print '\n'.join(Image.splitlines()[:4]) |
| 1326 | \begin{tikzpicture}% |
| 1327 | [x={(-0.046385cm, 0.837431cm)}, |
| 1328 | y={(-0.243536cm, 0.519228cm)}, |
| 1329 | z={(0.968782cm, 0.170622cm)}, |
| 1330 | sage: open('polytope-tikz1.tex', 'w').write(Image1) # not tested |
| 1331 | |
| 1332 | """ |
| 1333 | |
| 1334 | view_vector = vector(RDF, view) |
| 1335 | rot = rotate_arbitrary(view_vector,-(angle/360)*2*pi) |
| 1336 | rotation_matrix = rot[:2].transpose() |
| 1337 | proj_vector = (rot**(-1))*vector(RDF, [0,0,1]) |
| 1338 | |
| 1339 | # First compute the back and front vertices and facets |
| 1340 | front_facets = [] |
| 1341 | back_facets = [] |
| 1342 | for facet in self.face_inequalities: |
| 1343 | A = facet.vector()[1:] |
| 1344 | B = facet.vector()[0] |
| 1345 | if A*(2000*proj_vector)+B<0: |
| 1346 | front_facets += [facet] |
| 1347 | else: |
| 1348 | back_facets += [facet] |
| 1349 | |
| 1350 | front_vertices = [] |
| 1351 | for facet in front_facets: |
| 1352 | A = facet.vector()[1:] |
| 1353 | B = facet.vector()[0] |
| 1354 | for v in self.coords: |
| 1355 | if A*v+B<0.0005: |
| 1356 | front_vertices += [v] |
| 1357 | |
| 1358 | back_vertices = [] |
| 1359 | for facet in back_facets: |
| 1360 | A = facet.vector()[1:] |
| 1361 | B = facet.vector()[0] |
| 1362 | for v in self.coords: |
| 1363 | if A*v+B<0.0005: |
| 1364 | back_vertices += [v] |
| 1365 | |
| 1366 | # Creates the nodes, coordinate and tag for every vertex of the polytope. |
| 1367 | # The tag is used to draw the front facets later on. |
| 1368 | dict_drawing = {} |
| 1369 | first_part = '' |
| 1370 | second_part = '' |
| 1371 | for index1, index2 in self.lines: |
| 1372 | v = self.coords[index1] |
| 1373 | v_vect = str([i.n(digits=3) for i in v]) |
| 1374 | v_vect = v_vect.replace('[','(') |
| 1375 | v_vect = v_vect.replace(']',')') |
| 1376 | tag = '%s' %v_vect |
| 1377 | node = "\\node[%s] at %s {};\n" %('vertex',tag) |
| 1378 | nei = self.coords[index2] |
| 1379 | nei_vect = str([i.n(digits=3) for i in nei]) |
| 1380 | nei_vect = nei_vect.replace('[','(') |
| 1381 | nei_vect = nei_vect.replace(']',')') |
| 1382 | tag_nei = '%s' %nei_vect |
| 1383 | H_v = set(self.parent_polyhedron.vertex_incidences()[index1][1]) |
| 1384 | H_nei = set(self.parent_polyhedron.vertex_incidences()[index2][1]) |
| 1385 | H_v_nei = [h in back_facets for h in H_v.intersection(H_nei)] |
| 1386 | |
| 1387 | # The back edge has to be between two vertices in the Back |
| 1388 | # AND such that the 2 facets touching them are in the Back |
| 1389 | if v in back_vertices and nei in back_vertices and sum(H_v_nei)==2: |
| 1390 | first_part += "\\draw[%s,back] %s -- %s;\n" %('edge',tag,tag_nei) |
| 1391 | else: |
| 1392 | second_part += "\\draw[%s] %s -- %s;\n" %('edge',tag,tag_nei) |
| 1393 | coord = '\coordinate %s at %s;\n' %(tag,tag) |
| 1394 | dict_drawing[tuple(v)] = node,coord,tag |
| 1395 | |
| 1396 | # Start to write the output |
| 1397 | tikz_pic = '' |
| 1398 | tikz_pic += '\\begin{tikzpicture}%\n' |
| 1399 | tikz_pic += '\t[x={(%fcm, %fcm)},\n' %(RDF(rotation_matrix[0][0]),RDF(rotation_matrix[0][1])) |
| 1400 | tikz_pic += '\ty={(%fcm, %fcm)},\n' %(RDF(rotation_matrix[1][0]),RDF(rotation_matrix[1][1])) |
| 1401 | tikz_pic += '\tz={(%fcm, %fcm)},\n' %(RDF(rotation_matrix[2][0]),RDF(rotation_matrix[2][1])) |
| 1402 | tikz_pic += '\tscale=%f,\n' %scale |
| 1403 | tikz_pic += '\tback/.style={loosely dotted, thin},\n' |
| 1404 | tikz_pic += '\tedge/.style={color=%s, thick},\n' %edge_color |
| 1405 | tikz_pic += '\tfacet/.style={fill=%s,fill opacity=%f},\n' %(facet_color,opacity) |
| 1406 | tikz_pic += '\tvertex/.style={inner sep=1pt,circle,draw=%s!25!black,' %vertex_color |
| 1407 | tikz_pic += 'fill=%s!75!black,thick,anchor=base}]\n%%\n%%\n' %vertex_color |
| 1408 | |
| 1409 | # Draws the axes if True |
| 1410 | if axis: |
| 1411 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (1,0,0) node[anchor=north east]{$x$};\n' |
| 1412 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (0,1,0) node[anchor=north west]{$y$};\n' |
| 1413 | tikz_pic += '\\draw[color=black,thick,->] (0,0,0) -- (0,0,1) node[anchor=south]{$z$};\n' |
| 1414 | |
| 1415 | # Create the coordinate of the vertices |
| 1416 | tikz_pic += '%% Coordinate of the vertices:\n%%\n' |
| 1417 | for v in dict_drawing: |
| 1418 | tikz_pic += dict_drawing[v][1] |
| 1419 | |
| 1420 | # Draw the edges in the back |
| 1421 | tikz_pic += '%%\n%%\n%% Drawing edges in the back\n%%\n' |
| 1422 | tikz_pic += first_part |
| 1423 | |
| 1424 | # Draw the vertices on top of the back-edges |
| 1425 | tikz_pic += '%%\n%%\n%% Drawing vertices in the back\n%%\n' |
| 1426 | for v in back_vertices: |
| 1427 | if not v in front_vertices and tuple(v) in dict_drawing: |
| 1428 | tikz_pic += dict_drawing[tuple(v)][0] |
| 1429 | |
| 1430 | # Draw the facets in the front by going in cycles for every facet. |
| 1431 | tikz_pic += '%%\n%%\n%% Drawing the facets\n%%\n' |
| 1432 | for facet in front_facets: |
| 1433 | cyclic_vert = cyclic_sort_vertices_2d(list(facet.incident())) |
| 1434 | tikz_pic += '\\fill[facet] ' |
| 1435 | for v in cyclic_vert: |
| 1436 | if tuple(v) in dict_drawing: |
| 1437 | tikz_pic += '%s -- ' %dict_drawing[tuple(v)][2] |
| 1438 | tikz_pic += 'cycle {};\n' |
| 1439 | |
| 1440 | # Draw the edges in the front |
| 1441 | tikz_pic += '%%\n%%\n%% Drawing edges in the front\n%%\n' |
| 1442 | tikz_pic += second_part |
| 1443 | |
| 1444 | # Finally, the vertices in front are drawn on top of everything. |
| 1445 | tikz_pic += '%%\n%%\n%% Drawing the vertices in the front\n%%\n' |
| 1446 | for v in self.coords: |
| 1447 | if v in front_vertices: |
| 1448 | if tuple(v) in dict_drawing: |
| 1449 | tikz_pic += dict_drawing[tuple(v)][0] |
| 1450 | tikz_pic += '%%\n%%\n\\end{tikzpicture}' |
| 1451 | |
| 1452 | return LatexExpr(tikz_pic) |