pub struct ConvexHull { /* private fields */ }Expand description
A ConvexHull represents the convex hull of a set of points.
It provides methods to access hull properties, check if points are inside the hull, and access hull facets and vertices.
Implementations§
Source§impl ConvexHull
impl ConvexHull
Sourcepub fn new(points: &ArrayView2<'_, f64>) -> SpatialResult<Self>
pub fn new(points: &ArrayView2<'_, f64>) -> SpatialResult<Self>
Create a new ConvexHull from a set of points.
§Arguments
points- Input points (shape: npoints x n_dim)
§Returns
- Result containing a ConvexHull instance or an error
§Errors
- Returns error if hull computation fails or input is invalid
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
let hull = ConvexHull::new(&points.view()).unwrap();Sourcepub fn new_with_algorithm(
points: &ArrayView2<'_, f64>,
algorithm: ConvexHullAlgorithm,
) -> SpatialResult<Self>
pub fn new_with_algorithm( points: &ArrayView2<'_, f64>, algorithm: ConvexHullAlgorithm, ) -> SpatialResult<Self>
Create a new ConvexHull from a set of points using a specific algorithm.
§Arguments
points- Input points (shape: npoints x n_dim)algorithm- Algorithm to use for convex hull computation
§Returns
- Result containing a ConvexHull instance or an error
§Errors
- Returns error if hull computation fails or input is invalid
- Returns error if algorithm is not supported for the given dimensionality
§Examples
use scirs2_spatial::convex_hull::{ConvexHull, ConvexHullAlgorithm};
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
let hull = ConvexHull::new_with_algorithm(&points.view(), ConvexHullAlgorithm::GrahamScan).unwrap();Sourcepub fn vertices(&self) -> Vec<Vec<f64>>
pub fn vertices(&self) -> Vec<Vec<f64>>
Get the vertices of the convex hull
§Returns
- Array of vertices of the convex hull
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
let hull = ConvexHull::new(&points.view()).unwrap();
// The hull vertices should be the corners, not the interior point
// The number of vertices can vary depending on QHull implementation
assert!(hull.vertices().len() >= 3);Sourcepub fn vertices_array(&self) -> Array2<f64>
pub fn vertices_array(&self) -> Array2<f64>
Get the vertices of the convex hull as an Array2
§Returns
- Array2 of shape (n_vertices, n_dim) containing the vertices of the convex hull
Sourcepub fn vertex_indices(&self) -> &[usize]
pub fn vertex_indices(&self) -> &[usize]
Get the indices of the vertices of the convex hull
§Returns
- Vector of indices of the hull vertices (into the original points array)
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
let hull = ConvexHull::new(&points.view()).unwrap();
// Get the vertex indices of the hull
let vertices = hull.vertex_indices();
println!("Convex hull vertices: {:?}", vertices);Sourcepub fn simplices(&self) -> &[Vec<usize>]
pub fn simplices(&self) -> &[Vec<usize>]
Get the simplices (facets) of the convex hull
§Returns
- Vector of simplices, where each simplex is a vector of vertex indices
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
let hull = ConvexHull::new(&points.view()).unwrap();
// For a 2D hull, examine the simplices
for simplex in hull.simplices() {
// Print each simplex
println!("Simplex: {:?}", simplex);
}Sourcepub fn ndim(&self) -> usize
pub fn ndim(&self) -> usize
Get the dimensionality of the convex hull
§Returns
- Number of dimensions of the convex hull
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [0.5, 0.5]];
let hull = ConvexHull::new(&points.view()).unwrap();
assert_eq!(hull.ndim(), 2);Sourcepub fn contains<T: AsRef<[f64]>>(&self, point: T) -> SpatialResult<bool>
pub fn contains<T: AsRef<[f64]>>(&self, point: T) -> SpatialResult<bool>
Check if a point is inside the convex hull
§Arguments
point- The point to check
§Returns
- Result containing a boolean indicating if the point is inside the hull
§Errors
- Returns error if point dimension doesn’t match hull dimension
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
let points = array![[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]];
let hull = ConvexHull::new(&points.view()).unwrap();
// Check a point inside the hull
// Result may vary depending on QHull implementation and special case handling
let inside = hull.contains(&[0.25, 0.25]).unwrap();
println!("Point [0.25, 0.25] inside: {}", inside);
// Check a point outside the hull
assert!(!hull.contains(&[2.0, 2.0]).unwrap());Sourcepub fn volume(&self) -> SpatialResult<f64>
pub fn volume(&self) -> SpatialResult<f64>
Get the volume of the convex hull
For 2D hulls, this returns the area. For 3D hulls, this returns the volume. For higher dimensions, this returns the hypervolume.
§Returns
- Result containing the volume/area of the convex hull
§Errors
- Returns error if volume computation fails
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
// Create a square with area 1
let points = array![[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
let hull = ConvexHull::new(&points.view()).unwrap();
let area = hull.volume().unwrap();
assert!((area - 1.0).abs() < 1e-10);Sourcepub fn area(&self) -> SpatialResult<f64>
pub fn area(&self) -> SpatialResult<f64>
Get the surface area of the convex hull
For 2D hulls, this returns the perimeter. For 3D hulls, this returns the surface area. For higher dimensions, this returns the surface hypervolume.
§Returns
- Result containing the surface area/perimeter of the convex hull
§Errors
- Returns error if area computation fails
§Examples
use scirs2_spatial::convex_hull::ConvexHull;
use scirs2_core::ndarray::array;
// Create a 3D cube
let points = array![
[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0],
[0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0], [0.0, 1.0, 1.0]
];
let hull = ConvexHull::new(&points.view()).unwrap();
// Surface area of the cube should be 6 square units
let area = hull.area().unwrap();
assert!((area - 6.0).abs() < 1e-10);Auto Trait Implementations§
impl !Freeze for ConvexHull
impl !RefUnwindSafe for ConvexHull
impl !Send for ConvexHull
impl !Sync for ConvexHull
impl Unpin for ConvexHull
impl UnwindSafe for ConvexHull
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.