|
| 1 | +import numpy as np |
| 2 | +from compas.geometry import Polyline |
| 3 | +from compas_viewer import Viewer |
| 4 | +from compas_viewer.config import Config |
| 5 | + |
| 6 | +from compas_cgal.polylines import simplify_polyline |
| 7 | + |
| 8 | + |
| 9 | +# Create a complex polyline: spiral with noise |
| 10 | +n_points = 200 |
| 11 | +t = np.linspace(0, 6 * np.pi, n_points) |
| 12 | +radius = 1 + t / 10 |
| 13 | +noise = np.random.uniform(-0.05, 0.05, n_points) |
| 14 | + |
| 15 | +x = radius * np.cos(t) + noise |
| 16 | +y = radius * np.sin(t) + noise |
| 17 | +z = t / 5 |
| 18 | + |
| 19 | +original_points = np.column_stack([x, y, z]) |
| 20 | + |
| 21 | +# Simplify with different thresholds |
| 22 | +simplified_high = simplify_polyline(original_points, threshold=0.5) |
| 23 | + |
| 24 | +# Create polylines offset in X for visualization |
| 25 | +offset = 6.0 |
| 26 | +original_polyline = Polyline(original_points.tolist()) |
| 27 | +simplified_high_polyline = Polyline((simplified_high + [offset, 0, 0]).tolist()) |
| 28 | + |
| 29 | +# ============================================================================== |
| 30 | +# Visualize |
| 31 | +# ============================================================================== |
| 32 | + |
| 33 | +config = Config() |
| 34 | +config.camera.target = [5, 0, 5] |
| 35 | +config.camera.position = [5, -20, 10] |
| 36 | + |
| 37 | +viewer = Viewer(config=config) |
| 38 | + |
| 39 | +viewer.scene.add(original_polyline, linewidth=2, show_points=False) |
| 40 | +viewer.scene.add(simplified_high_polyline, linewidth=3, show_points=False) |
| 41 | + |
| 42 | +viewer.show() |
0 commit comments