|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Sequence |
| 4 | + |
| 5 | +from compas.colors import Color |
| 6 | +from compas.geometry import Shape |
| 7 | +from compas.scene import GeometryObject |
| 8 | +from matplotlib.patches import Polygon as PolygonPatch |
| 9 | + |
| 10 | +from .plotterobject import PlotterSceneObject |
| 11 | +from .plotterobject import to_rgb |
| 12 | + |
| 13 | + |
| 14 | +class ShapeObject(PlotterSceneObject, GeometryObject): |
| 15 | + """Plotter scene object for :class:`compas.geometry.Shape`. |
| 16 | +
|
| 17 | + Handles all COMPAS solids (Sphere, Cylinder, Cone, Capsule, Torus, |
| 18 | + Polyhedron, ...). The shape is tessellated with |
| 19 | + :meth:`compas.geometry.Shape.to_vertices_and_faces` and drawn as the |
| 20 | + projection of that mesh onto the XY plane: each face becomes a |
| 21 | + semi-transparent polygon with a solid outline, so the shape reads as a solid |
| 22 | + wireframe regardless of its orientation. |
| 23 | +
|
| 24 | + Parameters |
| 25 | + ---------- |
| 26 | + u |
| 27 | + Number of faces around curved shapes (longitude). Ignored by shapes |
| 28 | + without a resolution, such as :class:`compas.geometry.Polyhedron`. |
| 29 | + v |
| 30 | + Number of faces along curved shapes (latitude), where applicable. |
| 31 | + linewidth |
| 32 | + Width of the edges. |
| 33 | + linestyle |
| 34 | + Matplotlib line style (``"solid"``, ``"dotted"``, ``"dashed"``, ``"dashdot"``). |
| 35 | + facecolor |
| 36 | + Color of the faces. |
| 37 | + edgecolor |
| 38 | + Color of the edges. |
| 39 | + fill |
| 40 | + If True, fill the faces. |
| 41 | + alpha |
| 42 | + Transparency of the faces, between 0 and 1. |
| 43 | + """ |
| 44 | + |
| 45 | + def __init__( |
| 46 | + self, |
| 47 | + u: int = 16, |
| 48 | + v: int = 16, |
| 49 | + linewidth: float = 0.5, |
| 50 | + linestyle: str = "solid", |
| 51 | + facecolor: Color | Sequence[float] = (0.9, 0.9, 1.0), |
| 52 | + edgecolor: Color | Sequence[float] = (0.0, 0.0, 0.0), |
| 53 | + fill: bool = True, |
| 54 | + alpha: float = 0.2, |
| 55 | + **kwargs, |
| 56 | + ) -> None: |
| 57 | + super().__init__(zorder=1000, **kwargs) |
| 58 | + self.u = u |
| 59 | + self.v = v |
| 60 | + self.linewidth = linewidth |
| 61 | + self.linestyle = linestyle |
| 62 | + self.facecolor = facecolor |
| 63 | + self.edgecolor = edgecolor |
| 64 | + self.fill = fill |
| 65 | + self.alpha = alpha |
| 66 | + |
| 67 | + @property |
| 68 | + def shape(self) -> Shape: |
| 69 | + return self.geometry # type: ignore[return-value] |
| 70 | + |
| 71 | + def _vertices_and_faces(self) -> tuple[list, list]: |
| 72 | + try: |
| 73 | + return self.shape.to_vertices_and_faces(u=self.u, v=self.v) |
| 74 | + except TypeError: |
| 75 | + # Shapes without a resolution (e.g. Polyhedron) take no u/v. |
| 76 | + return self.shape.to_vertices_and_faces() |
| 77 | + |
| 78 | + def viewdata(self) -> list[list[float]]: |
| 79 | + vertices, _ = self._vertices_and_faces() |
| 80 | + return [list(vertex[:2]) for vertex in vertices] |
| 81 | + |
| 82 | + def draw(self) -> list: |
| 83 | + vertices, faces = self._vertices_and_faces() |
| 84 | + # Bake the transparency into the face color only, so the edges stay solid |
| 85 | + # even where projected faces overlap. |
| 86 | + facecolor = (*to_rgb(self.facecolor), self.alpha) if self.fill else "none" |
| 87 | + self._mpl_objects = [] |
| 88 | + for face in faces: |
| 89 | + polygon = PolygonPatch( |
| 90 | + [list(vertices[index][:2]) for index in face], |
| 91 | + linewidth=self.linewidth, |
| 92 | + linestyle=self.linestyle, |
| 93 | + facecolor=facecolor, |
| 94 | + edgecolor=to_rgb(self.edgecolor), |
| 95 | + fill=self.fill, |
| 96 | + zorder=self.zorder, |
| 97 | + ) |
| 98 | + self._mpl_objects.append(self.axes.add_patch(polygon)) |
| 99 | + return self._mpl_objects |
0 commit comments