use super::*;
use scirs2_core::ndarray::{arr2, Array2};
use scirs2_core::random::{Rng, RngExt};
use std::collections::HashSet;
#[test]
fn test_delaunay_simple() {
let points = arr2(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]]);
let tri = Delaunay::new(&points).expect("Operation failed");
assert_eq!(tri.simplices().len(), 2);
for simplex in tri.simplices() {
assert_eq!(simplex.len(), 3);
for &idx in simplex {
assert!(idx < points.nrows());
}
}
let hull = tri.convex_hull();
assert_eq!(hull.len(), 4); }
#[test]
fn test_delaunay_with_interior_point() {
let points = arr2(&[
[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0],
[0.5, 0.5], ]);
let tri = Delaunay::new(&points).expect("Operation failed");
assert!(!tri.simplices().is_empty(), "Expected at least 1 triangle");
for simplex in tri.simplices() {
assert_eq!(simplex.len(), 3, "2D simplices should have 3 vertices");
for &idx in simplex {
assert!(idx < 4, "Vertex index {} out of bounds", idx);
}
}
for simplex in tri.simplices() {
let unique: std::collections::HashSet<_> = simplex.iter().collect();
assert_eq!(
unique.len(),
simplex.len(),
"Simplex has duplicate vertices"
);
}
}
#[test]
fn test_delaunay_3d() {
let points = arr2(&[
[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, 1.0],
]);
let tri = Delaunay::new(&points).expect("Operation failed");
for simplex in tri.simplices() {
assert_eq!(simplex.len(), 4);
}
}
#[test]
fn test_delaunay_4d() {
let points = arr2(&[
[0.0, 0.0, 0.0, 0.0],
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
[0.5, 0.5, 0.5, 0.5], ]);
let tri = Delaunay::new(&points).expect("Operation failed");
assert_eq!(tri.ndim(), 4);
assert_eq!(tri.npoints(), 6);
assert!(!tri.simplices().is_empty(), "Should have simplices");
for simplex in tri.simplices() {
assert_eq!(
simplex.len(),
5,
"4D simplices should have 5 vertices (ndim+1)"
);
for &idx in simplex {
assert!(idx < 6, "Vertex index {} out of bounds", idx);
}
let unique: HashSet<_> = simplex.iter().collect();
assert_eq!(
unique.len(),
simplex.len(),
"Simplex has duplicate vertices"
);
}
}
#[test]
fn test_delaunay_5d() {
let points = arr2(&[
[0.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0],
]);
let tri = Delaunay::new(&points).expect("Operation failed");
assert_eq!(tri.ndim(), 5);
assert_eq!(tri.npoints(), 6);
assert!(!tri.simplices().is_empty(), "Should have simplices");
for simplex in tri.simplices() {
assert_eq!(
simplex.len(),
6,
"5D simplices should have 6 vertices (ndim+1)"
);
for &idx in simplex {
assert!(idx < 6, "Vertex index {} out of bounds", idx);
}
}
}
#[test]
fn test_delaunay_high_dim_with_interior() {
let points = arr2(&[
[0.0, 0.0, 0.0, 0.0],
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
[1.0, 1.0, 0.0, 0.0],
[0.25, 0.25, 0.25, 0.25], ]);
let tri = Delaunay::new(&points).expect("Operation failed");
assert!(!tri.simplices().is_empty());
for simplex in tri.simplices() {
assert_eq!(simplex.len(), 5, "4D simplices should have 5 vertices");
for &idx in simplex {
assert!(idx < 7);
}
let unique: HashSet<_> = simplex.iter().collect();
assert_eq!(unique.len(), simplex.len());
}
}
#[test]
fn test_find_simplex() {
let points = arr2(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
let tri = Delaunay::new(&points).expect("Operation failed");
let inside_point = [0.3, 0.3];
assert!(tri.find_simplex(&inside_point).is_some());
let outside_point = [1.5, 1.5];
assert!(tri.find_simplex(&outside_point).is_none());
}
#[test]
fn test_random_points_2d() {
let mut rng = scirs2_core::random::rng();
let n = 20;
let mut points_data = Vec::with_capacity(n * 2);
for _ in 0..n {
points_data.push(rng.random_range(0.0..1.0));
points_data.push(rng.random_range(0.0..1.0));
}
let points = Array2::from_shape_vec((n, 2), points_data).expect("Operation failed");
let tri = Delaunay::new(&points).expect("Operation failed");
assert_eq!(tri.ndim(), 2);
assert_eq!(tri.npoints(), n);
for simplex in tri.simplices() {
assert_eq!(simplex.len(), 3);
for &idx in simplex {
assert!(idx < n);
}
}
}
#[test]
fn test_constrained_delaunay_basic() {
let points = arr2(&[
[0.0, 0.0],
[1.0, 0.0],
[1.0, 1.0],
[0.0, 1.0],
[0.5, 0.5], ]);
let constraints = vec![(0, 1), (1, 2), (2, 3), (3, 0)];
let tri = Delaunay::new_constrained(&points, constraints.clone()).expect("Operation failed");
assert_eq!(tri.constraints().len(), 4);
for &constraint in &constraints {
assert!(tri.constraints().contains(&constraint));
}
assert!(tri.simplices().len() >= 2); }
#[test]
fn test_constrained_delaunay_invalid_constraints() {
let points = arr2(&[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0]]);
let invalid_constraints = vec![(0, 5)];
let result = Delaunay::new_constrained(&points, invalid_constraints);
assert!(result.is_err());
let self_constraint = vec![(0, 0)];
let result = Delaunay::new_constrained(&points, self_constraint);
assert!(result.is_err());
}
#[test]
fn test_constrained_delaunay_3d() {
let points_3d = arr2(&[
[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, 1.0],
]);
let constraints = vec![(0, 1)];
let result = Delaunay::new_constrained(&points_3d, constraints);
assert!(result.is_ok());
let tri = result.expect("Operation failed");
assert!(tri.constraints().contains(&(0, 1)));
}
#[test]
fn test_edge_exists() {
let points = arr2(&[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]);
let tri = Delaunay::new(&points).expect("Operation failed");
assert!(tri.edge_exists(0, 1) || tri.edge_exists(1, 0));
assert!(tri.edge_exists(1, 2) || tri.edge_exists(2, 1));
assert!(tri.edge_exists(0, 2) || tri.edge_exists(2, 0));
}