Struct AngularSmoother
pub struct AngularSmoother { /* private fields */ }Expand description
Allocation-free shortest-arc smoothing backed by ExponentialSmoother.
All angles are radians, normalized to [-PI, PI). At exactly half a turn the
negative direction wins. Values can jump numerically at the wrap boundary,
while the represented orientation remains continuous. This tracks orientation,
not accumulated revolutions; a target more than one turn away is wrapped.
Very large input angles have only the precision of the supplied f64.
Use Self::snap on teleport/restart to reset the current angle immediately.
There is no cached target, velocity, or elapsed time to clear.
Implementations§
§impl AngularSmoother
impl AngularSmoother
pub fn new(
value_radians: f64,
half_life_seconds: f64,
) -> Result<AngularSmoother, AngularSmoothingError>
pub fn new( value_radians: f64, half_life_seconds: f64, ) -> Result<AngularSmoother, AngularSmoothingError>
Start at a finite angle with a finite, strictly positive half-life.
Checks the angle before the half-life. Finite angles outside the canonical
interval are wrapped, including angles with magnitude up to f64::MAX.
use ferravex_engine::smoothing::{AngularSmoother, AngularSmoothingError};
use std::f64::consts::PI;
let angle = AngularSmoother::new(3.0 * PI, 0.25)?;
assert_eq!(angle.value(), -PI);
assert_eq!(AngularSmoother::new(0.0, 0.0).unwrap_err(),
AngularSmoothingError::InvalidHalfLife);pub fn update(
&mut self,
target_radians: f64,
seconds: f64,
) -> Result<f64, AngularSmoothingError>
pub fn update( &mut self, target_radians: f64, seconds: f64, ) -> Result<f64, AngularSmoothingError>
Move toward a finite target along the shortest arc and return the angle.
Checks the target before elapsed seconds, even for a zero-duration update. Non-finite targets, non-finite deltas, and negative deltas return an error without changing state. Zero seconds (including negative zero) return the current value exactly, as does a target equal to the current wrapped angle. Large finite deltas converge to the wrapped target; no catch-up time is retained. A half-life removes half the angular error, up to floating-point rounding.
use ferravex_engine::smoothing::{AngularSmoother, AngularSmoothingError};
use std::f64::consts::PI;
let mut angle = AngularSmoother::new(170.0_f64.to_radians(), 0.5)?;
let midpoint = angle.update((-170.0_f64).to_radians(), 0.5)?;
assert!((midpoint.abs() - PI).abs() < 1e-12); // Across the wrap boundary.
assert_eq!(angle.update(0.0, 0.0)?, midpoint);
assert_eq!(angle.update(f64::NAN, 1.0), Err(AngularSmoothingError::NonFiniteAngle));
assert_eq!(angle.value(), midpoint);pub fn value(&self) -> f64
pub fn value(&self) -> f64
Read the current angle in radians, always in [-PI, PI).
use ferravex_engine::smoothing::AngularSmoother;
let angle = AngularSmoother::new(350.0_f64.to_radians(), 0.1)?;
assert!((angle.value().to_degrees() + 10.0).abs() < 1e-12);pub fn snap(&mut self, value_radians: f64) -> Result<(), AngularSmoothingError>
pub fn snap(&mut self, value_radians: f64) -> Result<(), AngularSmoothingError>
Immediately reset to a finite angle while retaining the half-life.
The angle is wrapped to [-PI, PI). Non-finite input returns an error
without changing state. Subsequent updates behave like a fresh smoother
constructed at this angle with the original half-life.
use ferravex_engine::smoothing::{AngularSmoother, AngularSmoothingError};
use std::f64::consts::PI;
let mut angle = AngularSmoother::new(0.0, 0.5)?;
angle.update(1.0, 0.1)?;
angle.snap(3.0 * PI)?; // Reset orientation after teleport/restart.
assert_eq!(angle.value(), -PI);
assert_eq!(angle.snap(f64::INFINITY), Err(AngularSmoothingError::NonFiniteAngle));
assert_eq!(angle.value(), -PI);Trait Implementations§
§impl Clone for AngularSmoother
impl Clone for AngularSmoother
§fn clone(&self) -> AngularSmoother
fn clone(&self) -> AngularSmoother
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more