Skip to content

Commit

Permalink
add example fast_simpl.py in #992
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Jan 13, 2024
1 parent 7388fb8 commit 8734c01
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ point in the same cloud of points.
- add `transformations.TransformInterpolator` class
- add `Line.find_index_at_position()` finds the index of the line vertex that is closest to a point
- add `visual.LightKit` class which provides "natural" lighting from 4 sources.
- add `fast-simplification` example by @Louis-Pujol in #992


## Breaking changes
Expand Down Expand Up @@ -65,6 +66,7 @@ examples/pyplot/plot_stream.py
examples/other/madcad1.py
examples/other/tetgen1.py
examples/other/nelder-mead.py
examples/other/fast_simpl.py
tests/issues/issue_968.py
tests/snippets/test_discourse_1956.py
Expand Down
36 changes: 36 additions & 0 deletions examples/other/fast_simpl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Use fast-simplification to decimate a mesh and transfer
data defined on the original faces to the decimated ones."""
# Credits: Louis Pujol
# https://github.com/pyvista/fast-simplification
# pip install fast-simplification
import numpy as np
import fast_simplification as fs
import vedo

# Load a mesh and define a signal on vertices
mesh = vedo.Sphere().lw(1)
points = mesh.vertices
faces = mesh.cells
signal = points[:, 2]
mesh.pointdata["signal"] = signal

# Decimate the mesh and compute the mapping between the original vertices
# and the decimated ones with fast-simplification
points_decim, faces_decim, collapses = fs.simplify(
points, faces, target_reduction=0.9, return_collapses=True
)
points_decim, faces_decim, index_mapping = fs.replay_simplification(
points, faces, collapses
)

# Compute the average of the signal on the decimated vertices (scatter operation)
unique_values, counts = np.unique(index_mapping, return_counts=True)
a = np.zeros(len(unique_values), dtype=signal.dtype)
np.add.at(a, index_mapping, signal) # scatter addition
a /= counts # divide by the counts of each vertex index to get the average

# Create a new mesh with the decimated vertices and the averaged signal
decimated_mesh = vedo.Mesh([points_decim, faces_decim]).lw(1)
decimated_mesh.pointdata["signal"] = a

vedo.show(mesh, decimated_mesh, N=2, axes=1).close()

0 comments on commit 8734c01

Please sign in to comment.