use glam::Vec2;
use mln_style::feature::GeometryCoordinate;
#[derive(Clone, Copy, Debug, PartialEq, mln_wire::Wire)]
pub(crate) struct Anchor {
pub(crate) point: Vec2,
pub(crate) angle: f32,
pub(crate) segment: Option<usize>,
}
impl Anchor {
#[must_use]
pub(crate) fn new(x: f32, y: f32, angle: f32, segment: Option<usize>) -> Self {
Anchor { point: Vec2::new(x, y), angle, segment }
}
}
#[must_use]
pub(crate) fn to_vec2(p: GeometryCoordinate) -> Vec2 {
Vec2::new(f32::from(p.x), f32::from(p.y))
}
pub(crate) fn segments(line: &[GeometryCoordinate]) -> impl Iterator<Item = (Vec2, Vec2)> + '_ {
line.windows(2).map(|w| (to_vec2(w[0]), to_vec2(w[1])))
}
#[must_use]
pub(crate) fn line_length(line: &[GeometryCoordinate]) -> f32 {
segments(line).map(|(a, b)| a.distance(b)).sum()
}
#[must_use]
pub(crate) fn vertex_behind(line: &[GeometryCoordinate], point: Vec2, segment: usize, distance: f32) -> (usize, f32) {
let mut behind = 0.0;
let mut next = point;
for (i, &vertex) in line[..=segment].iter().enumerate().rev() {
let vertex = to_vec2(vertex);
behind += vertex.distance(next);
next = vertex;
if behind >= distance {
return (i, behind);
}
}
(0, behind)
}