|
| 1 | +import math |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +from compas.datastructures import Mesh |
| 6 | +from compas.geometry import Plane |
| 7 | +from compas.geometry import Point |
| 8 | +from compas.geometry import Polyline |
| 9 | +from compas.geometry import Vector |
| 10 | +from compas_viewer import Viewer |
| 11 | +from compas_viewer.config import Config |
| 12 | +from line_profiler import profile |
| 13 | + |
| 14 | +from compas_cgal.slicer import slice_mesh |
| 15 | + |
| 16 | + |
| 17 | +@profile |
| 18 | +def main(): |
| 19 | + # Get Mesh from STL |
| 20 | + FILE = Path(__file__).parent.parent.parent / "data" / "3DBenchy.stl" |
| 21 | + benchy = Mesh.from_stl(FILE) |
| 22 | + |
| 23 | + V, F = benchy.to_vertices_and_faces() |
| 24 | + |
| 25 | + # Get Slice planes from the bounding box |
| 26 | + bbox = benchy.aabb() |
| 27 | + normal = Vector(0, 0, 1) |
| 28 | + planes = [] |
| 29 | + for i in np.linspace(bbox.zmin, bbox.zmax, 50): |
| 30 | + plane = Plane(Point(0, 0, i), normal) |
| 31 | + planes.append(plane) |
| 32 | + |
| 33 | + # Slice |
| 34 | + slicer_polylines = slice_mesh((V, F), planes) |
| 35 | + |
| 36 | + # Convert edges to polylines |
| 37 | + polylines = [] |
| 38 | + for polyline in slicer_polylines: |
| 39 | + points = [] |
| 40 | + for point in polyline: |
| 41 | + points.append(Point(*point)) |
| 42 | + polylines.append(Polyline(points)) |
| 43 | + |
| 44 | + return benchy, polylines |
| 45 | + |
| 46 | + |
| 47 | +mesh, polylines = main() |
| 48 | + |
| 49 | +# ============================================================================== |
| 50 | +# Visualize |
| 51 | +# ============================================================================== |
| 52 | + |
| 53 | +config = Config() |
| 54 | +config.camera.target = [0, 100, 0] |
| 55 | +config.camera.position = [0, -75, 50] |
| 56 | +config.camera.scale = 10 |
| 57 | + |
| 58 | +viewer = Viewer(config=config) |
| 59 | + |
| 60 | +viewer.scene.add(mesh, opacity=0.5, show_lines=False, show_points=False) |
| 61 | +for polyline in polylines: |
| 62 | + viewer.scene.add(polyline, linewidth=2, show_points=False) |
| 63 | + |
| 64 | +viewer.show() |
0 commit comments