Skip to content

Instantly share code, notes, and snippets.

@flutefreak7
Last active October 31, 2021 04:33
Show Gist options
  • Save flutefreak7/bd621a9a836c8224e92305980ed829b9 to your computer and use it in GitHub Desktop.
Save flutefreak7/bd621a9a836c8224e92305980ed829b9 to your computer and use it in GitHub Desktop.
Convex Hull with vtk, pyvista, and scipy
import numpy as np
from scipy.spatial import ConvexHull
from pyvista import PolyData
def polyhull(x, y, z):
hull = ConvexHull(np.column_stack((x, y, z)))
faces = np.column_stack((3*np.ones((len(hull.simplices), 1), dtype=np.int), hull.simplices)).flatten()
poly = PolyData(hull.points, faces)
return poly
x, y, z = np.random.rand(100, 3).T
hull = polyhull(x, y, z)
hull.plot()
@flutefreak7
Copy link
Author

image

@flutefreak7
Copy link
Author

I've found scipy's ConvexHull to be 500X faster than using vtkDelaunay3D and vtkDataSetSurfaceFilter because you skip the expensive 3D tesselation of the volume.

@flutefreak7
Copy link
Author

Ok, so it looks like Delaunay scales at log(n) with the number of points while Convex Hull doesn't. Above 1000 points, the Delaunay method becomes prohibitively slow.

Without logging the y-axis the difference is stark. Delaunay starts taking seconds near 10,000 where scipy's ConvexHull stays in millisecond territory.
image

Logging the y-axis doesn't show the contrast as starkly, but allows the data to be read more clearly.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment