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

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>

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

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>

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

§

fn clone(&self) -> AngularSmoother

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for AngularSmoother

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Copy for AngularSmoother

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.