Skip to content

Commit f196ca8

Browse files
committed
add mesh colorfield visualization support
- visualize_slicer now accepts mesh_colorfield param for vertex/face attribute coloring - auto-detect vertex vs face attributes, use viridis colormap - fix HeatGeodesicSolver to accept (V, F) tuple - update examples to use scalar_field/overhang coloring closes #179
1 parent d004139 commit f196ca8

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

examples/2_curved_slicing/ex2_curved_slicing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def main(visualize: bool = False):
7474
print("Total elapsed time", round(end_time - start_time, 2), "seconds")
7575

7676
if visualize:
77-
visualize_slicer(slicer, mesh)
77+
visualize_slicer(slicer, mesh, mesh_colorfield='scalar_field')
7878

7979

8080
if __name__ == "__main__":

examples/6_attributes_transfer/example_6_attributes_transfer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def main(visualize: bool = False):
8484
utils.save_to_json(utils.point_list_to_dict(direction_to_pt_list), OUTPUT_PATH, 'direction_to_pt_list.json')
8585

8686
if visualize:
87-
visualize_slicer(slicer, mesh)
87+
visualize_slicer(slicer, mesh, mesh_opacity=0.6, mesh_colorfield='overhang')
8888

8989

9090
if __name__ == '__main__':

src/compas_slicer/pre_processing/preprocessing_utils/geodesics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def get_heat_geodesic_distances(
5555
mesh_hash = hash((len(list(mesh.vertices())), len(list(mesh.faces()))))
5656
if mesh_hash not in _cgal_solver_cache:
5757
_cgal_solver_cache.clear() # Clear old solvers
58-
_cgal_solver_cache[mesh_hash] = HeatGeodesicSolver(mesh)
58+
V = mesh.vertices_attributes('xyz')
59+
F = [mesh.face_vertices(f) for f in mesh.faces()]
60+
_cgal_solver_cache[mesh_hash] = HeatGeodesicSolver((V, F))
5961

6062
solver = _cgal_solver_cache[mesh_hash]
6163

src/compas_slicer/visualization/visualization.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def visualize_slicer(
3333
mesh: Mesh | None = None,
3434
show_mesh: bool = True,
3535
mesh_opacity: float = 0.3,
36+
mesh_colorfield: str = "z",
3637
) -> None:
3738
"""Visualize slicer toolpaths in compas_viewer.
3839
@@ -46,17 +47,33 @@ def visualize_slicer(
4647
If True, display the mesh.
4748
mesh_opacity : float
4849
Opacity for mesh display (0-1).
50+
mesh_colorfield : str
51+
Vertex attribute name to use for mesh coloring.
4952
5053
"""
5154
from compas.colors import Color
55+
from compas.colors import ColorMap
5256
from compas.geometry import Polyline
5357
from compas_viewer import Viewer
5458

5559
viewer = Viewer()
5660

57-
# Add mesh if provided
61+
# Add mesh if provided, colored by vertex or face attribute
5862
if mesh and show_mesh:
59-
viewer.scene.add(mesh, opacity=mesh_opacity)
63+
cmap = ColorMap.from_mpl("viridis")
64+
65+
# check if colorfield is vertex or face attribute
66+
first_vertex = next(mesh.vertices())
67+
if mesh.vertex_attribute(first_vertex, mesh_colorfield) is not None:
68+
scalars = {v: mesh.vertex_attribute(v, mesh_colorfield) for v in mesh.vertices()}
69+
smin, smax = min(scalars.values()), max(scalars.values())
70+
vertexcolor = {v: cmap(s, minval=smin, maxval=smax) for v, s in scalars.items()}
71+
viewer.scene.add(mesh, vertexcolor=vertexcolor, opacity=mesh_opacity, use_vertexcolors=True, show_lines=False)
72+
else:
73+
scalars = {f: mesh.face_attribute(f, mesh_colorfield) for f in mesh.faces()}
74+
smin, smax = min(scalars.values()), max(scalars.values())
75+
facecolor = {f: cmap(s, minval=smin, maxval=smax) for f, s in scalars.items()}
76+
viewer.scene.add(mesh, facecolor=facecolor, opacity=mesh_opacity, show_lines=False)
6077

6178
# Add paths as polylines with color gradient by layer
6279
n_layers = len(slicer.layers)

0 commit comments

Comments
 (0)