|
1 | 1 | from pathlib import Path |
2 | 2 |
|
3 | 3 | import numpy as np |
4 | | -from compas.colors import Color, ColorMap |
| 4 | +from compas.colors import Color |
| 5 | +from compas.colors import ColorMap |
5 | 6 | from compas.datastructures import Mesh |
6 | | -from compas.geometry import Box, Point, Polyline, Translation |
| 7 | +from compas.geometry import Box |
| 8 | +from compas.geometry import Point |
| 9 | +from compas.geometry import Polyline |
| 10 | +from compas.geometry import Translation |
7 | 11 | from compas_viewer import Viewer |
8 | 12 | from compas_viewer.config import Config |
9 | 13 |
|
10 | | -from compas_cgal.geodesics import ( |
11 | | - HeatGeodesicSolver, |
12 | | - geodesic_isolines, |
13 | | - geodesic_isolines_split, |
14 | | - heat_geodesic_distances, |
15 | | -) |
16 | | - |
17 | | -# Load mesh |
18 | | -FILE = Path(__file__).parent.parent.parent / "data" / "elephant.off" |
19 | | -mesh = Mesh.from_off(FILE) |
20 | | -mesh.quads_to_triangles() |
21 | | -V, F = mesh.to_vertices_and_faces() |
22 | | -V_np = np.array(V) |
23 | | - |
24 | | -# Config |
25 | | -X_OFF, Y_OFF = 0.75, 1.0 |
26 | | -ISOVALUES = [i/50 for i in range(50)] |
27 | | -SPLIT_ISOVALUES = [i/20 for i in range(20)] |
28 | | -COLORS = [Color.red(), Color.orange(), Color.yellow(), Color.green(), |
29 | | - Color.cyan(), Color.blue(), Color.purple(), Color.magenta()] |
30 | | -cmap = ColorMap.from_two_colors(Color.blue(), Color.red()) |
| 14 | +from compas_cgal.geodesics import HeatGeodesicSolver |
| 15 | +from compas_cgal.geodesics import geodesic_isolines |
| 16 | +from compas_cgal.geodesics import geodesic_isolines_split |
| 17 | +from compas_cgal.geodesics import heat_geodesic_distances |
31 | 18 |
|
32 | 19 |
|
33 | 20 | def make_mesh(V, F, offset): |
@@ -61,41 +48,98 @@ def make_source_points(sources, offset): |
61 | 48 | return [Point(V[s][0] + offset[0], V[s][1] + offset[1], V[s][2]) for s in sources] |
62 | 49 |
|
63 | 50 |
|
64 | | -# Row 1: Single source |
| 51 | +# ============================================================================= |
| 52 | +# Load mesh |
| 53 | +# ============================================================================= |
| 54 | + |
| 55 | +FILE = Path(__file__).parent.parent.parent / "data" / "elephant.off" |
| 56 | +mesh = Mesh.from_off(FILE) |
| 57 | +mesh.quads_to_triangles() |
| 58 | +V, F = mesh.to_vertices_and_faces() |
| 59 | +V_np = np.array(V) |
| 60 | + |
| 61 | +# ============================================================================= |
| 62 | +# Config |
| 63 | +# ============================================================================= |
| 64 | + |
| 65 | +X_OFF, Y_OFF = 0.75, 1.0 |
| 66 | +ISOVALUES = [i / 50 for i in range(50)] |
| 67 | +SPLIT_ISOVALUES = [i / 20 for i in range(20)] |
| 68 | +COLORS = [ |
| 69 | + Color.red(), |
| 70 | + Color.orange(), |
| 71 | + Color.yellow(), |
| 72 | + Color.green(), |
| 73 | + Color.cyan(), |
| 74 | + Color.blue(), |
| 75 | + Color.purple(), |
| 76 | + Color.magenta(), |
| 77 | +] |
| 78 | +cmap = ColorMap.from_two_colors(Color.blue(), Color.red()) |
| 79 | + |
| 80 | +# ============================================================================= |
| 81 | +# Single sources |
| 82 | +# ============================================================================= |
| 83 | + |
65 | 84 | src1 = [0] |
66 | 85 | dist1 = heat_geodesic_distances((V, F), src1) |
67 | 86 |
|
68 | | -# Row 2: Multiple sources (bbox corners) |
| 87 | +# ============================================================================= |
| 88 | +# Multiple sources |
| 89 | +# ============================================================================= |
| 90 | + |
69 | 91 | solver = HeatGeodesicSolver((V, F)) |
70 | 92 | bbox = Box.from_points(V_np) |
71 | 93 | src2 = list(dict.fromkeys([int(np.argmin(np.linalg.norm(V_np - c, axis=1))) for c in bbox.vertices])) |
72 | 94 | dist2 = solver.solve(src2) |
73 | 95 |
|
74 | | -# Viewer |
| 96 | +# ============================================================================= |
| 97 | +# Viz |
| 98 | +# ============================================================================= |
| 99 | + |
75 | 100 | config = Config() |
76 | 101 | config.camera.target = [X_OFF, -Y_OFF / 2, 0] |
77 | 102 | config.camera.position = [X_OFF, -2.0, 0.8] |
| 103 | + |
78 | 104 | viewer = Viewer(config=config) |
79 | 105 |
|
80 | 106 | # Row 1: Single Source |
| 107 | + |
81 | 108 | g1 = viewer.scene.add_group("Single Source") |
82 | | -g1.add(make_mesh(V, F, (0, 0)), use_vertexcolors=True, vertexcolor=make_vertex_colors(dist1), show_lines=False) |
| 109 | + |
| 110 | +g1.add( |
| 111 | + make_mesh(V, F, (0, 0)), |
| 112 | + use_vertexcolors=True, |
| 113 | + vertexcolor=make_vertex_colors(dist1), |
| 114 | + show_lines=False, |
| 115 | +) |
| 116 | + |
83 | 117 | for pt in make_source_points(src1, (0, 0)): |
84 | 118 | g1.add(pt, pointcolor=Color.black(), pointsize=20) |
85 | | -# g1.add(make_mesh(V, F, (X_OFF, 0)), facecolor=Color.grey(), show_lines=True) |
| 119 | + |
86 | 120 | for pl in make_isolines(src1, (X_OFF, 0)): |
87 | 121 | g1.add(pl, linecolor=Color.red(), lineswidth=5) |
| 122 | + |
88 | 123 | for m, c in make_split_meshes(src1, (2 * X_OFF, 0)): |
89 | 124 | g1.add(m, facecolor=c, show_lines=False) |
90 | 125 |
|
91 | 126 | # Row 2: Multiple Sources |
| 127 | + |
92 | 128 | g2 = viewer.scene.add_group("Multiple Sources") |
93 | | -g2.add(make_mesh(V, F, (0, -Y_OFF)), use_vertexcolors=True, vertexcolor=make_vertex_colors(dist2), show_lines=False) |
| 129 | + |
| 130 | +g2.add( |
| 131 | + make_mesh(V, F, (0, -Y_OFF)), |
| 132 | + use_vertexcolors=True, |
| 133 | + vertexcolor=make_vertex_colors(dist2), |
| 134 | + show_lines=False, |
| 135 | +) |
| 136 | + |
94 | 137 | for pt in make_source_points(src2, (0, -Y_OFF)): |
95 | 138 | g2.add(pt, pointcolor=Color.black(), pointsize=20) |
96 | | -# g2.add(make_mesh(V, F, (X_OFF, -Y_OFF)), facecolor=Color.grey(), show_lines=True) |
| 139 | + |
97 | 140 | for pl in make_isolines(src2, (X_OFF, -Y_OFF)): |
98 | 141 | g2.add(pl, linecolor=Color.red(), lineswidth=5) |
| 142 | + |
99 | 143 | for m, c in make_split_meshes(src2, (2 * X_OFF, -Y_OFF)): |
100 | 144 | g2.add(m, facecolor=c, show_lines=False) |
101 | 145 |
|
|
0 commit comments