Ticket #12083: trac12083_rebased_5.4.1.patch

File trac12083_rebased_5.4.1.patch, 24.7 KB (added by jipilab, 10 years ago)

Rebased version of the tikz method

  • sage/geometry/polyhedron/plot.py

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