token_value_map/shutter.rs
1use crate::*;
2use std::ops::Range;
3
4/// Shutter timing for motion blur sampling.
5///
6/// A [`Shutter`] `struct` defines a time range and opening pattern for
7/// generating motion blur samples. The opening function determines the weight
8/// of samples at different times within the shutter interval.
9#[derive(Clone, Debug, PartialEq, Hash, Default)]
10#[cfg_attr(feature = "rkyv", derive(Archive, RkyvSerialize, RkyvDeserialize))]
11pub struct Shutter {
12 /// The overall time range for sampling.
13 pub range: Range<Time>,
14 /// The time range during which the shutter is opening.
15 pub opening: Range<Time>,
16}
17
18impl Shutter {
19 /// Evaluate the shutter at `time`.
20 ///
21 /// Returns a value between 0.0 and 1.0 that represents the shutter's
22 /// opening at `time`.
23 #[inline]
24 pub fn opening(&self, pos: Time) -> f32 {
25 if pos < self.opening.start {
26 f32::from(pos) / f32::from(self.opening.start)
27 } else {
28 1.0f32 - f32::from(pos - self.opening.end) / f32::from(self.opening.end)
29 }
30 }
31
32 #[inline]
33 pub fn evaluate(&self, pos: f32) -> Time {
34 self.range.start.lerp(self.range.end, pos as _)
35 }
36
37 /// Returns the center of the shutter.
38 #[inline]
39 pub fn center(&self) -> Time {
40 (self.range.start + self.range.end) * 0.5
41 }
42}
43
44// Manual Eq implementation for Shutter
45// This is safe because we handle floating point comparison deterministically
46impl Eq for Shutter {}