pub struct Route3 { /* private fields */ }Expand description
Reusable piecewise-linear world-space route. Arc lengths are f64, repeated points are legal, and updates validate the complete input before mutation.
Implementations§
Source§impl Route3
impl Route3
pub fn new(capacity: usize) -> Result<Route3, NavigationError>
Sourcepub fn set(&mut self, points: &[Vec3]) -> Result<(), NavigationError>
pub fn set(&mut self, points: &[Vec3]) -> Result<(), NavigationError>
Replaces the route with an open polyline, including after set_closed.
Invalid input leaves the previous geometry and topology unchanged.
Sourcepub fn set_closed(&mut self, points: &[Vec3]) -> Result<(), NavigationError>
pub fn set_closed(&mut self, points: &[Vec3]) -> Result<(), NavigationError>
Replaces the route with a closed polyline, adding a last-to-first segment without storing another point or consuming capacity. Repeating the first point at the end is also legal. Empty and zero-length loops are legal. Invalid input leaves the previous route unchanged.
Closed distance samples and cursors wrap at the seam and never complete.
WaypointFollower retains its finite traversal of the authored vertices.
No query, cursor operation or successful replacement allocates.
use ferravex_math::Vec3;
use ferravex_play::navigation::{PathCursor, Route3};
let mut route = Route3::new(4)?;
route.set_closed(&[Vec3::ZERO, Vec3::X * 2., Vec3::new(2., 2., 0.), Vec3::Y * 2.])?;
assert!(route.is_closed());
assert_eq!(route.length(), 8.);
assert_eq!(route.sample(9.)?.unwrap().position, Vec3::X);
let mut cursor = PathCursor::default();
cursor.seek(&route, 7.)?;
assert_eq!(cursor.advance(&route, 2., 1.)?.unwrap().distance, 1.);
route.clear();
assert!(!route.is_closed());Sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Whether samples wrap over a last-to-first segment. See Self::set_closed.
pub fn clear(&mut self)
pub fn points(&self) -> &[Vec3]
pub fn length(&self) -> f64
Sourcepub fn sample(
&self,
travel: f64,
) -> Result<Option<RouteSample>, NavigationError>
pub fn sample( &self, travel: f64, ) -> Result<Option<RouteSample>, NavigationError>
Samples a finite nonnegative distance. Open routes clamp at the endpoint;
closed routes wrap, with zero-length loops staying at their sole position.
Empty routes return None. See Self::set_closed for a loop example.
Sourcepub fn sample_progress(
&self,
progress: f64,
) -> Result<Option<RouteSample>, NavigationError>
pub fn sample_progress( &self, progress: f64, ) -> Result<Option<RouteSample>, NavigationError>
Samples a normalized arc fraction in 0..=1; out-of-range and non-finite
inputs return InvalidInput, even on an empty route. One is the endpoint
for an open route and the start of a closed route.
use ferravex_math::Vec3;
use ferravex_play::navigation::Route3;
let mut route = Route3::new(2)?;
route.set(&[Vec3::ZERO, Vec3::X * 10.])?;
assert_eq!(route.sample_progress(0.25)?.unwrap().position, Vec3::X * 2.5);
assert!(route.sample_progress(1.)?.unwrap().complete);
assert!(route.sample_progress(1.1).is_err());Sourcepub fn project(
&self,
position: Vec3,
) -> Result<Option<RouteProjection>, NavigationError>
pub fn project( &self, position: Vec3, ) -> Result<Option<RouteProjection>, NavigationError>
Projects a finite world position onto the entire route in O(point count)
time and constant scratch space. Empty routes return None. Calculations
use f64, including differences between extreme finite f32 coordinates.
Zero-length segments are skipped. Equal squared distances select the lowest index of a nonzero-length segment, including at corners or intersections. A point-only route returns its first point, zero progress and zero tangent. Projection is global: teleports and nearby branches can change progress discontinuously. It does not infer motion, laps or checkpoint visits.
use ferravex_math::Vec3;
use ferravex_play::navigation::{Route3, RouteProjection};
let mut route = Route3::new(3)?;
route.set(&[Vec3::ZERO, Vec3::ZERO, Vec3::X * 10.])?;
let hit: RouteProjection = route.project(Vec3::new(4., 3., 0.))?.unwrap();
assert_eq!(hit.position, Vec3::X * 4.);
assert_eq!(hit.segment, 1);
assert_eq!(hit.distance, 4.);
assert_eq!(hit.progress, 0.4);
assert_eq!(hit.distance_to_route, 3.);