use scirs2_core::ndarray::array;
use scirs2_spatial::convex_hull::{convex_hull, ConvexHull};
#[allow(dead_code)]
fn main() {
println!("=== SciRS2 Spatial - Convex Hull Example ===\n");
let points_2d = array![
[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0],
[1.0, 1.0],
[0.5, 0.5], [0.25, 0.75], [0.75, 0.25], ];
println!("2D Points:");
for (i, row) in points_2d.outer_iter().enumerate() {
println!(" Point {}: [{:.2}, {:.2}]", i, row[0], row[1]);
}
println!();
println!("Method 1: Using convex_hull() function");
let hull_vertices = convex_hull(&points_2d.view()).expect("Operation failed");
println!("Hull Vertices:");
for (i, row) in hull_vertices.outer_iter().enumerate() {
println!(" Vertex {}: [{:.2}, {:.2}]", i, row[0], row[1]);
}
println!();
println!("Method 2: Using ConvexHull struct");
let hull = ConvexHull::new(&points_2d.view()).expect("Operation failed");
println!("Hull Vertex Indices: {:?}", hull.vertex_indices());
println!("Hull Simplices: {:?}", hull.simplices());
println!();
println!("Point Inside Check:");
let test_points = [
([0.5, 0.5], "center"),
([0.1, 0.1], "near corner"),
([2.0, 2.0], "outside"),
];
for (point, desc) in test_points.iter() {
let inside = hull.contains(point).expect("Operation failed");
println!(
" Point {} at {:?}: {}",
desc,
point,
if inside { "INSIDE" } else { "OUTSIDE" }
);
}
println!();
let points_3d = array![
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 1.0, 0.0],
[1.0, 0.0, 1.0],
[0.0, 1.0, 1.0],
[1.0, 1.0, 1.0],
[0.5, 0.5, 0.5], ];
println!("3D Example:");
let hull_3d = ConvexHull::new(&points_3d.view()).expect("Operation failed");
println!(" 3D Hull has {} vertices", hull_3d.vertex_indices().len());
println!(" 3D Hull has {} facets", hull_3d.simplices().len());
let inside_3d = hull_3d.contains([0.5, 0.5, 0.5]).expect("Operation failed");
println!(
" Center point [0.5, 0.5, 0.5] is {}",
if inside_3d { "INSIDE" } else { "OUTSIDE" }
);
println!();
}