JFLib

physics/src/hull.rs#

Source: lib/physics/src/hull.rs

hull -- a convex hull from points (quickhull in 3D) and its support function

Quickhull: a first tetrahedron from extreme points, every other point in the outside set of the face it
lies farthest above; then, as long as a face has an outside set, its farthest point (the eye) removes the
faces it sees (a connected region from that face), the horizon around them gets new faces to the eye,
and the points of the removed faces go to the new ones (or are inside now).  A point within `eps` of a
face's plane counts as on it, so the hull can leave out points that lie that close to it.  Everything in
a fixed order (faces and points by index, ties to the lower index): the same hull on every target.
/// at most this many input points for [`ConvexHull::new`]
MAX_HULL_POINTS: usize = 1 << 16
  • enum HullError why [ConvexHull::new] refused the points

    • Count -- fewer than 4 points, or more than [MAX_HULL_POINTS]
    • NotFinite -- a coordinate is NaN or infinite
    • Flat -- all points lie on a plane (or a line or a point), within the tolerance: no volume
  • struct ConvexHull A convex polyhedron: the hull of the points it was made from. Vertices, triangles (counter-clockwise seen from outside) and their planes, in its own coordinates.

impl ConvexHull

  • fn new(points: &[Vec3]) -> Result<ConvexHull, HullError> the convex hull of points (at least 4, not all on a plane)
  • fn points(&self) -> &[Vec3] the vertices (a subset of the input points, in their order)
  • fn faces(&self) -> &[[u32; 3]] the triangles, as indices into [ConvexHull::points], counter-clockwise seen from outside
  • fn planes(&self) -> &[(Vec3, f64)] per face the outward unit normal n and d, with n . p = d on the face
  • fn aabb(&self) -> Aabb the box around the hull in its own coordinates
  • fn tolerance(&self) -> f64 the tolerance of the construction: an input point lies at most this far outside the hull
  • fn support(&self, d: Vec3) -> Vec3 a vertex farthest in direction d: the first of equals among up to 32 vertices, beyond that by hill climbing over the vertex adjacency (exact for a convex polyhedron, always the same path)
  • fn volume(&self) -> f64 the volume (for tests and mass properties later)

Source: docs/API.md in the JFLib repository.