|
| 1 | +from typing import Optional |
| 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 earclip_polygon |
| 9 | + |
| 10 | + |
| 11 | +def mesh_to_edgesbuffer(mesh: Mesh, color: Color) -> tuple[list[list[float]], list[Color]]: |
| 12 | + positions = [] |
| 13 | + colors = [] |
| 14 | + |
| 15 | + for u, v in mesh.edges(): |
| 16 | + positions.append(mesh.vertex_coordinates(u)) |
| 17 | + positions.append(mesh.vertex_coordinates(v)) |
| 18 | + colors.append(color) |
| 19 | + colors.append(color) |
| 20 | + |
| 21 | + return positions, colors |
| 22 | + |
| 23 | + |
| 24 | +def meshes_to_edgesbuffer( |
| 25 | + meshes: list[Mesh], |
| 26 | + color: Color, |
| 27 | + material: Optional[three.LineBasicMaterial] = None, |
| 28 | +) -> three.LineSegments: |
| 29 | + positions = [] |
| 30 | + colors = [] |
| 31 | + |
| 32 | + for mesh in meshes: |
| 33 | + buffer = mesh_to_edgesbuffer(mesh, color) |
| 34 | + positions += buffer[0] |
| 35 | + colors += buffer[1] |
| 36 | + |
| 37 | + positions = np.array(positions, dtype=np.float32) |
| 38 | + colors = np.array(colors, dtype=np.float32) |
| 39 | + |
| 40 | + geometry = three.BufferGeometry( |
| 41 | + attributes={ |
| 42 | + "position": three.BufferAttribute(positions, normalized=False), |
| 43 | + "color": three.BufferAttribute(colors, normalized=False, itemSize=3), |
| 44 | + } |
| 45 | + ) |
| 46 | + |
| 47 | + if not material: |
| 48 | + material = three.LineBasicMaterial(vertexColors="VertexColors") |
| 49 | + |
| 50 | + return three.LineSegments(geometry, material) |
| 51 | + |
| 52 | + |
| 53 | +def mesh_to_facesbuffer(mesh: Mesh, color: Color) -> tuple[list[list[float]], list[Color]]: |
| 54 | + positions = [] |
| 55 | + colors = [] |
| 56 | + |
| 57 | + for face in mesh.faces(): |
| 58 | + vertices = mesh.face_vertices(face) |
| 59 | + |
| 60 | + if len(vertices) == 3: |
| 61 | + positions.append(mesh.vertex_coordinates(vertices[0])) |
| 62 | + positions.append(mesh.vertex_coordinates(vertices[1])) |
| 63 | + positions.append(mesh.vertex_coordinates(vertices[2])) |
| 64 | + colors.append(color) |
| 65 | + colors.append(color) |
| 66 | + colors.append(color) |
| 67 | + |
| 68 | + elif len(vertices) == 4: |
| 69 | + positions.append(mesh.vertex_coordinates(vertices[0])) |
| 70 | + positions.append(mesh.vertex_coordinates(vertices[1])) |
| 71 | + positions.append(mesh.vertex_coordinates(vertices[2])) |
| 72 | + colors.append(color) |
| 73 | + colors.append(color) |
| 74 | + colors.append(color) |
| 75 | + positions.append(mesh.vertex_coordinates(vertices[0])) |
| 76 | + positions.append(mesh.vertex_coordinates(vertices[2])) |
| 77 | + positions.append(mesh.vertex_coordinates(vertices[3])) |
| 78 | + colors.append(color) |
| 79 | + colors.append(color) |
| 80 | + colors.append(color) |
| 81 | + |
| 82 | + else: |
| 83 | + ears = earclip_polygon(Polygon([mesh.vertex_coordinates(v) for v in vertices])) |
| 84 | + for ear in ears: |
| 85 | + positions.append(mesh.vertex_coordinates(vertices[ear[0]])) |
| 86 | + positions.append(mesh.vertex_coordinates(vertices[ear[1]])) |
| 87 | + positions.append(mesh.vertex_coordinates(vertices[ear[2]])) |
| 88 | + colors.append(color) |
| 89 | + colors.append(color) |
| 90 | + colors.append(color) |
| 91 | + |
| 92 | + return positions, colors |
| 93 | + |
| 94 | + |
| 95 | +def meshes_to_facesbuffer( |
| 96 | + meshes: list[Mesh], |
| 97 | + color: Color, |
| 98 | + material: Optional[three.MeshBasicMaterial] = None, |
| 99 | +) -> three.Mesh: |
| 100 | + positions = [] |
| 101 | + colors = [] |
| 102 | + |
| 103 | + for mesh in meshes: |
| 104 | + buffer = mesh_to_facesbuffer(mesh, color) |
| 105 | + positions += buffer[0] |
| 106 | + colors += buffer[1] |
| 107 | + |
| 108 | + positions = np.array(positions, dtype=np.float32) |
| 109 | + colors = np.array(colors, dtype=np.float32) |
| 110 | + |
| 111 | + geometry = three.BufferGeometry( |
| 112 | + attributes={ |
| 113 | + "position": three.BufferAttribute(positions, normalized=False), |
| 114 | + "color": three.BufferAttribute(colors, normalized=False, itemSize=3), |
| 115 | + } |
| 116 | + ) |
| 117 | + |
| 118 | + if not material: |
| 119 | + material = three.MeshBasicMaterial( |
| 120 | + side="DoubleSide", |
| 121 | + vertexColors="VertexColors", |
| 122 | + ) |
| 123 | + |
| 124 | + return three.Mesh(geometry, material) |
0 commit comments