|
| 1 | +import profile |
| 2 | + |
| 3 | +from compas.datastructures import Mesh |
| 4 | +from compas.geometry import Box |
| 5 | +from compas.geometry import Point |
| 6 | +from compas.geometry import Polyline |
| 7 | +from compas.geometry import Sphere |
| 8 | +from compas_viewer import Viewer |
| 9 | +from line_profiler import profile |
| 10 | + |
| 11 | +from compas_cgal.intersections import intersection_mesh_mesh |
| 12 | + |
| 13 | + |
| 14 | +@profile |
| 15 | +def main(): |
| 16 | + # ============================================================================== |
| 17 | + # Make a box and a sphere |
| 18 | + # ============================================================================== |
| 19 | + |
| 20 | + box = Box(2) |
| 21 | + A = box.to_vertices_and_faces(triangulated=True) |
| 22 | + |
| 23 | + sphere = Sphere(1, point=[1, 1, 1]) |
| 24 | + B = sphere.to_vertices_and_faces(u=32, v=32, triangulated=True) |
| 25 | + |
| 26 | + # ============================================================================== |
| 27 | + # Compute the intersections |
| 28 | + # ============================================================================== |
| 29 | + |
| 30 | + pointsets = intersection_mesh_mesh(A, B) |
| 31 | + |
| 32 | + # ============================================================================== |
| 33 | + # Process output |
| 34 | + # ============================================================================== |
| 35 | + |
| 36 | + polylines = [] |
| 37 | + for points in pointsets: |
| 38 | + points = [Point(*point) for point in points] |
| 39 | + polyline = Polyline(points) |
| 40 | + polylines.append(polyline) |
| 41 | + |
| 42 | + return A, B, polylines |
| 43 | + |
| 44 | + |
| 45 | +A, B, polylines = main() |
| 46 | + |
| 47 | +# ============================================================================= |
| 48 | +# Visualize |
| 49 | +# ============================================================================== |
| 50 | + |
| 51 | +viewer = Viewer() |
| 52 | + |
| 53 | +viewer.renderer.camera.target = [0, 0, 0] |
| 54 | +viewer.renderer.camera.position = [4, -6, 3] |
| 55 | + |
| 56 | +viewer.scene.add(Mesh.from_vertices_and_faces(*A), facecolor=(1.0, 0.0, 0.0), show_points=False) |
| 57 | +viewer.scene.add( |
| 58 | + Mesh.from_vertices_and_faces(*B), |
| 59 | + facecolor=(0.0, 1.0, 0.0), |
| 60 | + show_points=False, |
| 61 | + opacity=0.3, |
| 62 | +) |
| 63 | + |
| 64 | +for polyline in polylines: |
| 65 | + viewer.scene.add( |
| 66 | + polyline, |
| 67 | + linecolor=(0.0, 0.0, 1.0), |
| 68 | + lineswidth=3, |
| 69 | + pointcolor=(0.0, 0.0, 1.0), |
| 70 | + pointsize=20, |
| 71 | + show_points=True, |
| 72 | + ) |
| 73 | + |
| 74 | +viewer.show() |
0 commit comments