pub struct Transform { /* private fields */ }Expand description
A small helper struct which represents a 1 dimensional affine transformation from a signed 64 bit space A, to a signed 64 bit space B. Conceptually, this is the function…
f(a) = b = (a * scale) + offset
Internally, however, the exact function used is
f(a) = b = (((a - A_offset) * B_scale) / A_scale) + B_offset
Where the offsets involved are 64 bit signed integers, and the scale factors are 32 bit unsigned integers.
Overflow/Underflow saturation behavior is as follows. The transformation operation is divided into three stages.
- Offset by A_offset
- Scale by (B_scale / A_scale)
- Offset by B_offset
Each stage is saturated independently. That is to say, if the result of stage #1 is clamped at int64::min, this is the input value which will be fed into stage #2. The calculations are not done with infinite precision and then clamped at the end.
TODO(johngro): Reconsider this. Clamping at intermediate stages can make it more difficult to understand that saturation happened at all, and might be important to a client. It may be better to either signal explicitly that this happened, or to extend the precision of the operation in the rare slow path so that saturation behavior occurs only at the end of the op, and produces a correct result if the transform would have saturated at an intermediate step, but got brought back into range by a subsequent operation.
Saturation is enabled by default, but may be disabled by choosing the Saturate::No form of Apply/ApplyInverse. When saturation behavior is disabled, the results of a transformation where over/underflow occurs at any stage is undefined.
Implementations§
Source§impl Transform
impl Transform
pub fn new_linear(ratio: Ratio) -> Self
pub fn invertible(&self) -> bool
pub fn a_offset(&self) -> i64
pub fn b_offset(&self) -> i64
pub fn ratio(&self) -> Ratio
pub fn numerator(&self) -> u32
pub fn denominator(&self) -> u32
pub fn inverse(&self) -> Self
pub fn apply_static<const SATURATE: bool>( a_offset: i64, b_offset: i64, ratio: Ratio, val: i64, ) -> i64
pub fn apply_inverse_static<const SATURATE: bool>( a_offset: i64, b_offset: i64, ratio: Ratio, val: i64, ) -> i64
pub fn apply<const SATURATE: bool>(&self, val: i64) -> i64
pub fn apply_inverse<const SATURATE: bool>(&self, val: i64) -> i64
pub fn compose(bc: &Transform, ab: &Transform, exact: Exact) -> Transform
Trait Implementations§
impl Copy for Transform
impl Eq for Transform
Source§impl Mul for Transform
Composes two timeline functions B->C and A->B producing A->C.
impl Mul for Transform
Composes two timeline functions B->C and A->B producing A->C.
Panics on loss of precision.