Skip to main content

Transform

Struct Transform 

Source
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.

  1. Offset by A_offset
  2. Scale by (B_scale / A_scale)
  3. 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

Source

pub fn new(a_offset: i64, b_offset: i64, ratio: Ratio) -> Self

Constructs a new Transform.

Source

pub fn new_linear(ratio: Ratio) -> Self

Source

pub fn invertible(&self) -> bool

Source

pub fn a_offset(&self) -> i64

Source

pub fn b_offset(&self) -> i64

Source

pub fn ratio(&self) -> Ratio

Source

pub fn numerator(&self) -> u32

Source

pub fn denominator(&self) -> u32

Source

pub fn inverse(&self) -> Self

Source

pub fn apply_static<const SATURATE: bool>( a_offset: i64, b_offset: i64, ratio: Ratio, val: i64, ) -> i64

Source

pub fn apply_inverse_static<const SATURATE: bool>( a_offset: i64, b_offset: i64, ratio: Ratio, val: i64, ) -> i64

Source

pub fn apply<const SATURATE: bool>(&self, val: i64) -> i64

Source

pub fn apply_inverse<const SATURATE: bool>(&self, val: i64) -> i64

Source

pub fn compose(bc: &Transform, ab: &Transform, exact: Exact) -> Transform

Trait Implementations§

Source§

impl Clone for Transform

Source§

fn clone(&self) -> Transform

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Transform

Source§

impl Debug for Transform

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Transform

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Transform

Source§

impl Mul for Transform

Composes two timeline functions B->C and A->B producing A->C.

Panics on loss of precision.

Source§

type Output = Transform

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl PartialEq for Transform

Source§

fn eq(&self, other: &Transform) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Transform

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> ToMutPtr for T
where T: ?Sized,

Source§

type Target = T

The target type of the pointer.
Source§

fn to_mut_ptr(&self) -> *mut T

Casts the reference to a mutable raw pointer.
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.