#[cfg(not(feature = "glam"))]
compile_error!("The feature 'glam' must be active for the tests");
use crate::{LinestringError, linestring_2d::convex_hull};
use vector_traits::glam::{DVec2, Vec2, vec2};
#[test]
#[allow(clippy::vec_init_then_push)]
fn contains_point_exclusive_1() {
let mut hull = Vec::<DVec2>::with_capacity(5);
hull.push([0.0, 0.0].into());
hull.push([10.0, 0.0].into());
hull.push([10.0, 10.0].into());
hull.push([0.0, 10.0].into());
hull.push([0.0, 0.0].into());
assert!(convex_hull::contains_point_exclusive(
&hull,
[5.0, 6.0].into()
));
assert!(!convex_hull::contains_point_exclusive(
&hull,
[0.0, 0.0].into()
));
assert!(!convex_hull::contains_point_exclusive(
&hull,
[0.0, 10.0].into()
));
assert!(!convex_hull::contains_point_exclusive(
&hull,
[10.0, 10.0].into()
));
assert!(!convex_hull::contains_point_exclusive(
&hull,
[5.0, 10.0].into()
));
assert!(!convex_hull::contains_point_exclusive(
&hull,
[10.0000001, 10.0].into()
));
assert!(convex_hull::contains_point_exclusive(
&hull,
[9.99999, 9.99999].into()
));
assert!(!convex_hull::contains_point_exclusive(
&hull,
[10.0, 9.99999].into()
));
}
#[test]
#[allow(clippy::vec_init_then_push)]
fn contains_point_inclusive_1() {
let mut hull = Vec::<DVec2>::with_capacity(5);
hull.push([0.0, 0.0].into());
hull.push([10.0, 0.0].into());
hull.push([10.0, 10.0].into());
hull.push([0.0, 10.0].into());
hull.push([0.0, 0.0].into());
assert!(convex_hull::contains_point_inclusive(
&hull,
[5.0, 6.0].into()
));
assert!(convex_hull::contains_point_inclusive(
&hull,
[0.0, 0.0].into()
));
assert!(convex_hull::contains_point_inclusive(
&hull,
[0.0, 10.0].into()
));
assert!(convex_hull::contains_point_inclusive(
&hull,
[10.0, 10.0].into()
));
assert!(convex_hull::contains_point_inclusive(
&hull,
[5.0, 10.0].into()
));
assert!(!convex_hull::contains_point_inclusive(
&hull,
[10.0000001, 10.0].into()
));
assert!(convex_hull::contains_point_inclusive(
&hull,
[9.99999, 9.99999].into()
));
assert!(convex_hull::contains_point_inclusive(
&hull,
[10.0, 9.99999].into()
));
}
#[test]
fn convex_hull_circle() -> Result<(), LinestringError> {
use std::f32::consts::PI;
let num_points = 1000; let radius = 1.0;
let mut points: Vec<Vec2> = Vec::new();
for i in 0..num_points {
let theta = 2.0 * PI * (i as f32) / (num_points as f32);
let x = radius * theta.cos();
let y = radius * theta.sin();
points.push(vec2(x, y));
}
let indices: Vec<u32> = (0..points.len() as u32).collect();
points.push(points[0]);
let ghs = convex_hull::graham_scan(&points)?;
let gw = convex_hull::gift_wrap(&points)?;
let chp = convex_hull::convex_hull_par(&points, &indices, num_points / 5)?;
let ighs = convex_hull::indexed_graham_scan(&points, &indices)?;
let igw = convex_hull::indexed_gift_wrap(&points, &indices)?;
assert_eq!(ghs, gw);
assert_eq!(ghs.len(), chp.len());
assert_eq!(chp, ighs);
assert_eq!(chp, igw);
Ok(())
}