|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pythreejs as three |
| 5 | +from compas.colors import Color |
| 6 | +from compas.datastructures import Mesh |
| 7 | +from compas.geometry import Polygon |
| 8 | +from compas.geometry import Shape |
| 9 | +from compas.geometry import earclip_polygon |
| 10 | + |
| 11 | + |
| 12 | +class MeshShape: |
| 13 | + |
| 14 | + def __init__(self, mesh: Mesh): |
| 15 | + self.mesh = mesh |
| 16 | + self._vertices = None |
| 17 | + self._edges = None |
| 18 | + self._faces = None |
| 19 | + |
| 20 | + @property |
| 21 | + def vertices(self): |
| 22 | + if not self._vertices: |
| 23 | + self._vertices = self.mesh.vertices_attributes("xyz") |
| 24 | + return self._vertices |
| 25 | + |
| 26 | + @property |
| 27 | + def edges(self): |
| 28 | + if not self._edges: |
| 29 | + self._edges = list(self.mesh.edges()) |
| 30 | + return self._edges |
| 31 | + |
| 32 | + @property |
| 33 | + def faces(self): |
| 34 | + if not self._faces: |
| 35 | + self._faces = [self.mesh.face_vertices(face) for face in self.mesh.faces()] |
| 36 | + return self._faces |
| 37 | + |
| 38 | + |
| 39 | +def shapes_to_edgesbuffer( |
| 40 | + shapes: list[Mesh, Shape], |
| 41 | + color: Color, |
| 42 | +) -> three.LineSegments: |
| 43 | + """Convert the combined edges of a collection of shapes to one line segment buffer. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + shapes : list[:class:`compas.datastructures.Mesh`, :class:`compas.geometry.Shape`] |
| 48 | + The shape collection. |
| 49 | + color : :class:`compas.colors.Color` |
| 50 | + The color of the edges. |
| 51 | +
|
| 52 | + Returns |
| 53 | + ------- |
| 54 | + pythreejs.LineSegments |
| 55 | +
|
| 56 | + """ |
| 57 | + positions = [] |
| 58 | + colors = [] |
| 59 | + |
| 60 | + for shape in shapes: |
| 61 | + buffer = shape_to_edgesbuffer(shape, color) |
| 62 | + positions += buffer[0] |
| 63 | + colors += buffer[1] |
| 64 | + |
| 65 | + positions = np.array(positions, dtype=np.float32) |
| 66 | + colors = np.array(colors, dtype=np.float32) |
| 67 | + |
| 68 | + geometry = three.BufferGeometry( |
| 69 | + attributes={ |
| 70 | + "position": three.BufferAttribute(positions, normalized=False), |
| 71 | + "color": three.BufferAttribute(colors, normalized=False, itemSize=3), |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + material = three.LineBasicMaterial(vertexColors="VertexColors") |
| 76 | + |
| 77 | + return three.LineSegments(geometry, material) |
| 78 | + |
| 79 | + |
| 80 | +def shapes_to_facesbuffer( |
| 81 | + shapes: list[Mesh], |
| 82 | + color: Color, |
| 83 | +) -> three.Mesh: |
| 84 | + """Convert the combined faces of a collection of shapes to one mesh buffer. |
| 85 | +
|
| 86 | + Parameters |
| 87 | + ---------- |
| 88 | + shapes : list[:class:`compas.datastructures.Mesh`, :class:`compas.geometry.Shape`] |
| 89 | + The shape collection. |
| 90 | + color : :class:`compas.colors.Color` |
| 91 | + The color of the faces. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + pythreejs.Mesh |
| 96 | +
|
| 97 | + """ |
| 98 | + positions = [] |
| 99 | + colors = [] |
| 100 | + |
| 101 | + for shape in shapes: |
| 102 | + buffer = shape_to_facesbuffer(shape, color) |
| 103 | + positions += buffer[0] |
| 104 | + colors += buffer[1] |
| 105 | + |
| 106 | + positions = np.array(positions, dtype=np.float32) |
| 107 | + colors = np.array(colors, dtype=np.float32) |
| 108 | + |
| 109 | + geometry = three.BufferGeometry( |
| 110 | + attributes={ |
| 111 | + "position": three.BufferAttribute(positions, normalized=False), |
| 112 | + "color": three.BufferAttribute(colors, normalized=False, itemSize=3), |
| 113 | + } |
| 114 | + ) |
| 115 | + |
| 116 | + material = three.MeshBasicMaterial( |
| 117 | + side="DoubleSide", |
| 118 | + vertexColors="VertexColors", |
| 119 | + ) |
| 120 | + |
| 121 | + return three.Mesh(geometry, material) |
| 122 | + |
| 123 | + |
| 124 | +def shape_to_edgesbuffer(shape: Union[Mesh, Shape], color: Color) -> tuple[list[list[float]], list[Color]]: |
| 125 | + positions = [] |
| 126 | + colors = [] |
| 127 | + |
| 128 | + if isinstance(shape, Mesh): |
| 129 | + shape = MeshShape(shape) |
| 130 | + |
| 131 | + for u, v in shape.edges: |
| 132 | + positions.append(shape.vertices[u]) |
| 133 | + positions.append(shape.vertices[v]) |
| 134 | + colors.append(color) |
| 135 | + colors.append(color) |
| 136 | + |
| 137 | + return positions, colors |
| 138 | + |
| 139 | + |
| 140 | +def shape_to_facesbuffer(shape: Union[Mesh, Shape], color: Color) -> tuple[list[list[float]], list[Color]]: |
| 141 | + positions = [] |
| 142 | + colors = [] |
| 143 | + |
| 144 | + if isinstance(shape, Mesh): |
| 145 | + shape = MeshShape(shape) |
| 146 | + |
| 147 | + for face in shape.faces: |
| 148 | + |
| 149 | + if len(face) == 3: |
| 150 | + positions.append(shape.vertices[face[0]]) |
| 151 | + positions.append(shape.vertices[face[1]]) |
| 152 | + positions.append(shape.vertices[face[2]]) |
| 153 | + colors.append(color) |
| 154 | + colors.append(color) |
| 155 | + colors.append(color) |
| 156 | + |
| 157 | + elif len(face) == 4: |
| 158 | + positions.append(shape.vertices[face[0]]) |
| 159 | + positions.append(shape.vertices[face[1]]) |
| 160 | + positions.append(shape.vertices[face[2]]) |
| 161 | + colors.append(color) |
| 162 | + colors.append(color) |
| 163 | + colors.append(color) |
| 164 | + positions.append(shape.vertices[face[0]]) |
| 165 | + positions.append(shape.vertices[face[2]]) |
| 166 | + positions.append(shape.vertices[face[3]]) |
| 167 | + colors.append(color) |
| 168 | + colors.append(color) |
| 169 | + colors.append(color) |
| 170 | + |
| 171 | + else: |
| 172 | + ears = earclip_polygon(Polygon([shape.vertices[v] for v in face])) |
| 173 | + for ear in ears: |
| 174 | + positions.append(shape.vertices[ear[0]]) |
| 175 | + positions.append(shape.vertices[ear[1]]) |
| 176 | + positions.append(shape.vertices[ear[2]]) |
| 177 | + colors.append(color) |
| 178 | + colors.append(color) |
| 179 | + colors.append(color) |
| 180 | + |
| 181 | + return positions, colors |
0 commit comments