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)§

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() -> Transform

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

impl Eq for Transform

Source§

impl FromBytes for Transform
where i64: FromBytes, Ratio: FromBytes,

Source§

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
Source§

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
Source§

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
Source§

impl FromZeros for Transform
where i64: FromZeros, Ratio: FromZeros,

Source§

fn zero(&mut self)

Overwrites self with zeros. Read more
Source§

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
Source§

impl Immutable for Transform
where i64: Immutable, Ratio: Immutable,

Source§

impl IntoBytes for Transform
where i64: IntoBytes, Ratio: IntoBytes, (): PaddingFree<Self, { _ }>,

Source§

fn as_bytes(&self) -> &[u8]
where Self: Immutable,

Gets the bytes of this value. Read more
Source§

fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
Source§

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
Source§

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
Source§

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
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

Equality operator ==. Read more
1.0.0 (const: unstable)§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Transform

Source§

impl TryFromBytes for Transform

Source§

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
Source§

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
Source§

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more

Auto Trait Implementations§

§

impl Freeze for Transform

§

impl RefUnwindSafe for Transform

§

impl Send for Transform

§

impl Sync for Transform

§

impl Unpin for Transform

§

impl UnsafeUnpin for Transform

§

impl UnwindSafe for Transform

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

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
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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

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

§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.