path_traits/project.rs
1//! Closest-point projection onto a path.
2//!
3//! This module defines the [`Project`] trait for finding the arc-length of the
4//! point on a path nearest to a given query point, along with a convenience
5//! method `closest_point` that returns the actual position.
6
7use crate::Path;
8
9/// Project a query point onto a path to find the nearest position.
10///
11/// Implementers should return the arc-length parameter of the closest point.
12/// The default `closest_point` method combines `project` with `sample_at` to
13/// produce the actual position.
14pub trait Project: Path {
15 /// Find the arc-length `s` of the point on the path closest to `p`.
16 ///
17 /// Returns an error when the projection cannot be computed (e.g. a
18 /// degenerate path).
19 fn project(&self, p: Self::Point) -> Result<Self::Scalar, Self::Error>;
20
21 /// Return the closest point on the path to `p`.
22 ///
23 /// This default implementation calls `project` followed by `sample_at`.
24 fn closest_point(&self, p: Self::Point) -> Result<Self::Point, Self::Error> {
25 let s = self.project(p)?;
26 self.sample_at(s)
27 }
28}