Function convex_hull

Source
pub fn convex_hull(points: &[Point3<f32>]) -> (Vec<Point3<f32>>, Vec<[u32; 3]>)
Expand description

Computes the convex hull of a set of 3D points.

The convex hull is the smallest convex shape that contains all input points. Imagine wrapping a rubber band (2D) or shrink-wrap (3D) tightly around the points.

§Algorithm

Uses the quickhull algorithm, which is efficient for most inputs:

  • Average case: O(n log n)
  • Worst case: O(n²) for specially constructed inputs

§Returns

A tuple (vertices, indices):

  • vertices: The hull vertices (subset of input points, duplicates removed)
  • indices: Triangle faces as triplets of vertex indices (CCW winding)

§Panics

Panics if the input is degenerate or the algorithm fails. For error handling, use try_convex_hull instead.

§Example

use parry3d::transformation::convex_hull;
use nalgebra::Point3;

// Points forming a tetrahedron
let points = vec![
    Point3::origin(),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
    Point3::new(0.0, 0.0, 1.0),
];

let (vertices, indices) = convex_hull(&points);

// Hull has 4 vertices (all input points are on the hull)
assert_eq!(vertices.len(), 4);

// Hull has 4 triangular faces
assert_eq!(indices.len(), 4);

§See Also