Struct InterpolationPair
pub struct InterpolationPair<T> { /* private fields */ }Expand description
Exactly two values, a presentation fraction, a hold flag and a local generation.
The pair never allocates its own storage or retains a sampler/output. Construction
and snapping clone one value; commits move values without cloning. Allocations,
side effects or panics in T::clone, T::drop or the supplied interpolator are
caller-owned. Use small value poses for allocation-free presentation.
sample_with records its effective alpha for
hold; it does not advance either endpoint. A pure interpolator
and stable values are needed to reproduce the same image while held.
See the module recipes for Vec2 and Vec3/Quat consumers.
Implementations§
§impl<T> InterpolationPair<T>
impl<T> InterpolationPair<T>
pub fn new(value: T) -> InterpolationPair<T>where
T: Clone,
pub fn new(value: T) -> InterpolationPair<T>where
T: Clone,
Initialize both endpoints from one value, unheld, at alpha 1 and generation 0.
Clones value once; see the module recipes for construction and use.
pub fn previous(&self) -> &T
pub fn previous(&self) -> &T
The preceding committed value. Initially, and after a snap, both agree.
See Self::commit for a runnable history example.
pub fn current(&self) -> &T
pub fn current(&self) -> &T
The most recently committed value; see Self::commit.
pub fn alpha(&self) -> f32
pub fn alpha(&self) -> f32
Last successful sample’s effective fraction, or 1 after new/commit/snap.
This fraction applies to the current pair only. Related presentation such
as hazard time can use it after sampling. See Self::hold for an example.
pub fn is_held(&self) -> bool
pub fn is_held(&self) -> bool
Whether samples use the retained alpha; see Self::hold.
pub fn generation(&self) -> u64
pub fn generation(&self) -> u64
Count explicit snaps since construction, wrapping from u64::MAX to 0.
Compare for change to invalidate presentation caches; this is neither a
tick ID nor a globally unique identity. Clones copy the generation, new
pairs start at 0, and a full wrap can alias an old value. Ordinary commits,
samples, holds and resumes do not change it. See Self::snap.
pub fn commit(&mut self, value: T)
pub fn commit(&mut self, value: T)
Shift current to previous and retain an actual update’s new value.
Drops the oldest value without cloning. Call after each accepted update, never just because a tick was scheduled or a render frame occurred. A catch-up batch keeps only its last two executed states. Holds are released and alpha becomes 1, so holding before the next sample selects this newest committed state. A commit does not advance the discontinuity generation.
use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0);
for executed_state in [1, 2, 3] { history.commit(executed_state); }
assert_eq!((history.previous(), history.current()), (&2, &3));
assert_eq!(history.generation(), 0);pub fn sample_with<R>(
&mut self,
alpha: f32,
interpolate: impl FnOnce(&T, &T, f32) -> R,
) -> Result<R, InterpolationError>
pub fn sample_with<R>( &mut self, alpha: f32, interpolate: impl FnOnce(&T, &T, f32) -> R, ) -> Result<R, InterpolationError>
Sample borrowed endpoints with a caller-provided interpolation function.
Validates the supplied alpha even while held. Calls interpolate exactly
once with (previous, current, effective_alpha), including at 0/1 or after
a snap. While held, effective alpha is the retained fraction; otherwise it
is the supplied fraction. Records it only after the callback returns.
The callback may return any presentation type; the pair does not inspect
that result or validate endpoint contents. A callback returning its own
Err is still a completed sample, and produces Ok(Err(..)).
§Errors
Nonfinite or out-of-range alpha returns InterpolationError::InvalidAlpha
without invoking the callback or mutating any state. Both signed zeros
and exactly 1 are valid; no clamping or extrapolation is performed.
use ferravex_engine::interpolation::{InterpolationError, InterpolationPair};
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
assert_eq!(history.sample_with(0.25, |a, b, t| a + (b - a) * t)?, 2.0);
assert_eq!(history.sample_with(f32::NAN, |_, _, _| panic!("not called")),
Err::<(), _>(InterpolationError::InvalidAlpha));
assert_eq!(history.alpha(), 0.25);pub fn hold(&mut self) -> bool
pub fn hold(&mut self) -> bool
Hold the last successful sample’s alpha; return whether a hold was entered.
Repeated calls are idempotent. Before any sample following new/commit/snap, alpha is 1, so holding selects the current endpoint. This operation retains no third value and performs no interpolation. A commit or snap releases it.
use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
let blend = |a: &f32, b: &f32, t| a + (b - a) * t;
assert_eq!(history.sample_with(0.75, blend)?, 6.0);
assert!(history.hold());
assert!(!history.hold());
assert!(history.is_held());
assert_eq!(history.sample_with(0.0, blend)?, 6.0);
assert_eq!(history.alpha(), 0.75);pub fn resume(&mut self) -> bool
pub fn resume(&mut self) -> bool
Release a hold; return whether one was active. Other state is unchanged.
The next sample uses its supplied fraction. Pass the active session’s retained fraction on a zero-tick resume, not an inactive frame’s zero. No smoothing or monotonic-alpha constraint is added: after an actual tick a smaller alpha refers to a newer interval and is expected. If an inspector displayed alpha 1 while paused, keep the hold until the next commit to avoid returning to a smaller retained session fraction.
use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
let blend = |a: &f32, b: &f32, t| a + (b - a) * t;
history.sample_with(0.5, blend)?;
history.hold();
assert!(history.resume());
assert!(!history.resume());
assert_eq!(history.sample_with(0.75, blend)?, 6.0);pub fn snap(&mut self, value: T) -> u64where
T: Clone,
pub fn snap(&mut self, value: T) -> u64where
T: Clone,
Reset both endpoints to an accepted discontinuity and return its generation.
Clones the value once, releases any hold and sets alpha to 1. The local generation increments with wrapping even if the new value equals the old value. Use for teleport/respawn/restart instead of replacing the whole pair, so existing observers can detect reset. There is no old pose to blend from.
use ferravex_engine::interpolation::InterpolationPair;
let mut history = InterpolationPair::new(0.0_f32);
history.commit(8.0);
history.hold();
assert_eq!(history.snap(100.0), 1);
assert!(!history.is_held());
assert_eq!(history.generation(), 1);
assert_eq!((history.previous(), history.current()), (&100.0, &100.0));
assert_eq!(history.sample_with(0.25, |a, b, t| a + (b - a) * t)?, 100.0);Trait Implementations§
§impl<T> Clone for InterpolationPair<T>where
T: Clone,
impl<T> Clone for InterpolationPair<T>where
T: Clone,
§fn clone(&self) -> InterpolationPair<T>
fn clone(&self) -> InterpolationPair<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more