use alloc::vec::Vec;
use super::cdt::orient;
use crate::geom::Point;
pub(crate) fn funnel(start: Point, end: Point, portals: &[(Point, Point)]) -> Vec<Point> {
let mut gates: Vec<(Point, Point)> = Vec::with_capacity(portals.len() + 2);
gates.push((start, start));
gates.extend_from_slice(portals);
gates.push((end, end));
let mut path = alloc::vec![start];
let (mut apex, mut left, mut right) = (start, start, start);
let (mut left_i, mut right_i) = (0, 0);
let mut i = 1;
while i < gates.len() {
let (pl, pr) = gates[i];
if orient(apex, right, pr) >= 0 {
if apex == right || orient(apex, left, pr) < 0 {
right = pr;
right_i = i;
} else {
path.push(left);
apex = left;
(right, right_i) = (apex, left_i);
i = left_i + 1;
continue;
}
}
if orient(apex, left, pl) <= 0 {
if apex == left || orient(apex, right, pl) > 0 {
left = pl;
left_i = i;
} else {
path.push(right);
apex = right;
(left, left_i) = (apex, right_i);
i = right_i + 1;
continue;
}
}
i += 1;
}
if path.last() != Some(&end) {
path.push(end);
}
path.dedup();
path
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bends_at_the_corner() {
let portals = [
(Point::new(10, 10), Point::new(10, 0)),
(Point::new(10, 10), Point::new(20, 0)),
];
let path = funnel(Point::new(0, 20), Point::new(20, 20), &portals);
assert_eq!(
path,
[Point::new(0, 20), Point::new(10, 10), Point::new(20, 20)]
);
let portals = [
(Point::new(10, 30), Point::new(10, 10)),
(Point::new(20, 30), Point::new(10, 10)),
];
let path = funnel(Point::new(0, 0), Point::new(20, 0), &portals);
assert_eq!(
path,
[Point::new(0, 0), Point::new(10, 10), Point::new(20, 0)]
);
}
}