retrofire_core::math::vec

Struct Vector

Source
#[repr(transparent)]
pub struct Vector<Repr, Space = ()>(pub Repr, _);
Expand description

A generic vector type. Represents an element of a vector space or a module, a generalization of a vector space where the scalars can be integers (technically, the scalar type can be any ring-like type).

§Type parameters

  • Repr: Representation of the scalar components of the vector, for example an array or a SIMD vector.
  • Space: The space that the vector is an element of. A tag type used to prevent mixing up vectors of different spaces and bases.

§Examples

TODO

Tuple Fields§

§0: Repr

Implementations§

Source§

impl Vector<[f32; 2], Polar>

Source

pub fn r(&self) -> f32

Returns the radial component of self.

Source

pub fn az(&self) -> Angle

Returns the azimuthal component of self.

Source§

impl Vector<[f32; 3], Spherical>

Source

pub fn r(&self) -> f32

Returns the radial component of self.

Source

pub fn az(&self) -> Angle

Returns the azimuthal component of self.

Source

pub fn alt(&self) -> Angle

Returns the altitude (elevation) component of self.

Source§

impl<R, Sp> Vector<R, Sp>

Source

pub const fn new(repr: R) -> Self

Returns a new vector with representation repr.

Source

pub fn to<S>(self) -> Vector<R, S>

Returns a vector with value equal to self but in space S.

This method can be used to coerce a vector from one space to another in order to make types match. One use case is to cast a “generic” vector returned by one of the constructor functions to a more specific space.

Source§

impl<Sp, const N: usize> Vector<[f32; N], Sp>

Source

pub fn normalize(&self) -> Self

Returns self normalized to unit length.

§Examples
let normalized: Vec2 = vec2(3.0, 4.0).normalize();
assert_approx_eq!(normalized, vec2(0.6, 0.8), eps=1e-2);
assert_approx_eq!(normalized.len_sqr(), 1.0, eps=1e-2);
§Panics

Panics in dev mode if self is a zero vector.

Source

pub fn clamp(&self, min: &Self, max: &Self) -> Self

Returns self clamped component-wise to the given range.

In other words, for each component self[i], the result r has r[i] equal to self[i].clamp(min[i], max[i]).

§Examples
let v: Vec3 = vec3(0.5, 1.5, -2.0);
// Clamp to the unit cube
let v = v.clamp(&splat(-1.0), &splat(1.0));
assert_eq!(v, vec3(0.5, 1.0, -1.0));
Source§

impl<Sc, Sp, const N: usize> Vector<[Sc; N], Sp>
where Self: Linear<Scalar = Sc>, Sc: Linear<Scalar = Sc> + Copy,

Source

pub fn len_sqr(&self) -> Sc

Returns the length of self, squared.

This avoids taking the square root in cases it’s not needed and works with scalars for which a square root is not defined.

Source

pub fn dot(&self, other: &Self) -> Sc

Returns the dot product of self and other.

Source

pub fn scalar_project(&self, other: &Self) -> Sc
where Sc: Div<Sc, Output = Sc>,

Returns the scalar projection of self onto other (the length of the component of self parallel to other).

Source

pub fn vector_project(&self, other: &Self) -> Self
where Sc: Div<Sc, Output = Sc>,

Returns the vector projection of self onto other (the vector component of self parallel to other).

           self
           ^
          /.
        /  .
      /    .
    /      .
  /       _.
 +-------'->-----> other
        result
Source

pub fn map<T>(self, f: impl FnMut(Sc) -> T) -> Vector<[T; N], Sp>

Returns a vector of the same dimension as self by applying f component-wise.

Source§

impl<R, Sc, B> Vector<R, Real<2, B>>
where R: Index<usize, Output = Sc>, Sc: Copy,

Source

pub fn x(&self) -> Sc

Returns the x component of self.

Source

pub fn y(&self) -> Sc

Returns the y component of self.

Source§

impl<R, Sc, B> Vector<R, Real<3, B>>
where R: Index<usize, Output = Sc>, Sc: Copy,

Source

pub fn x(&self) -> Sc

Returns the x component of self.

Source

pub fn y(&self) -> Sc

Returns the y component of self.

Source

pub fn z(&self) -> Sc

Returns the z component of self.

Source

pub fn cross(&self, other: &Self) -> Self
where Sc: Linear<Scalar = Sc>, [Sc; 3]: Into<Self>,

Returns the cross product of self with other.

The result is a vector perpendicular to both input vectors, its length proportional to the area of the parallelogram formed by the vectors. Specifically, the length is given by the identity:

    |𝗮 × 𝗯| = |𝗮| |𝗯| sin 𝜽

where |·| denotes the length of a vector and 𝜽 equals the angle between 𝗮 and 𝗯.

       ^
    r  |
    e  |
    s  |    other
    u  |     ^ - - - - - +
    l  |   /           /
    t  | /           /
       +-----------> self
Source§

impl<R, Sc> Vector<R, Proj4>
where R: Index<usize, Output = Sc>, Sc: Copy,

Source

pub fn x(&self) -> Sc

Returns the x component of self.

Source

pub fn y(&self) -> Sc

Returns the y component of self.

Source

pub fn z(&self) -> Sc

Returns the z component of self.

Source

pub fn w(&self) -> Sc

Returns the w component of self.

Source§

impl Vector<[f32; 2], Real<2, Tex>>

Source

pub const fn u(&self) -> f32

Returns the u (horizontal) component of self.

Source

pub const fn v(&self) -> f32

Returns the v (vertical) component of self.

Trait Implementations§

Source§

impl<R, Sp> Add<<Vector<R, Sp> as Affine>::Diff> for Vector<R, Sp>
where Self: Affine,

Source§

fn add(self, rhs: <Self as Affine>::Diff) -> Self

TODO

Source§

type Output = Vector<R, Sp>

The resulting type after applying the + operator.
Source§

impl<R, Sp> AddAssign<<Vector<R, Sp> as Affine>::Diff> for Vector<R, Sp>
where Self: Affine,

The vector += vector operator.

Source§

fn add_assign(&mut self, rhs: <Self as Affine>::Diff)

Performs the += operation. Read more
Source§

impl<Sc, Sp, const DIM: usize> Affine for Vector<[Sc; DIM], Sp>
where Sc: Affine, Sc::Diff: Linear<Scalar = Sc::Diff> + Copy,

Source§

const DIM: usize = DIM

The dimension (number of components) of Self.

Source§

type Space = Sp

The space that Self is the element type of.
Source§

type Diff = Vector<[<Sc as Affine>::Diff; DIM], Sp>

The (signed) difference of two values of Self. Diff must have the same dimension as Self.
Source§

fn add(&self, other: &Self::Diff) -> Self

Adds diff to self component-wise. Read more
Source§

fn sub(&self, other: &Self) -> Self::Diff

Subtracts other from self, returning the (signed) difference. Read more
Source§

impl<Sc: ApproxEq, Sp, const N: usize> ApproxEq<Vector<[Sc; N], Sp>, Sc> for Vector<[Sc; N], Sp>

Source§

fn approx_eq_eps(&self, other: &Self, eps: &Sc) -> bool

Returns whether self and other are approximately equal, using the relative epsilon rel_eps.
Source§

fn relative_epsilon() -> Sc

Returns the default relative epsilon of type E.
Source§

fn approx_eq(&self, other: &Other) -> bool

Returns whether self and other are approximately equal. Uses the epsilon returned by Self::relative_epsilon.
Source§

impl<R: Clone, S> Clone for Vector<R, S>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<R: Debug, Sp: Debug + Default> Debug for Vector<R, Sp>

Source§

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

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

impl<R: Default, S> Default for Vector<R, S>

Source§

fn default() -> Self

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

impl<R, Sp> Div<f32> for Vector<R, Sp>
where Self: Linear<Scalar = f32>,

Source§

fn div(self, rhs: f32) -> Self

TODO

Source§

type Output = Vector<R, Sp>

The resulting type after applying the / operator.
Source§

impl<R, Sp> DivAssign<f32> for Vector<R, Sp>
where Self: Linear<Scalar = f32>,

Source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
Source§

impl<R, Sp> From<R> for Vector<R, Sp>

Source§

fn from(els: R) -> Self

Converts to this type from the input type.
Source§

impl<Sp, Sc: Clone, const DIM: usize> From<Sc> for Vector<[Sc; DIM], Sp>

Source§

fn from(scalar: Sc) -> Self

Returns a vector with all components equal to scalar.

This operation is also called “splat” or “broadcast”.

Source§

impl<R, Sp> Index<usize> for Vector<R, Sp>
where Self: Affine, R: Index<usize>,

Source§

fn index(&self, i: usize) -> &Self::Output

Returns the component of self with index i.

§Panics

If i >= Self::DIM. Note that Self::DIM can be less than the number of elements in R.

Source§

type Output = <R as Index<usize>>::Output

The returned type after indexing.
Source§

impl<R, Sp> IndexMut<usize> for Vector<R, Sp>
where Self: Affine, R: IndexMut<usize>,

Source§

fn index_mut(&mut self, i: usize) -> &mut Self::Output

Returns a mutable reference to the component of self with index i.

§Panics

If i >= Self::DIM. Note that Self::DIM can be less than the number of elements in R.

Source§

impl<Sc, Sp, const DIM: usize> Linear for Vector<[Sc; DIM], Sp>
where Self: Affine<Diff = Self>, Sc: Linear<Scalar = Sc> + Copy,

Source§

fn zero() -> Self

Returns a vector with all-zero components, also called a null vector.

Source§

type Scalar = Sc

The scalar type associated with Self.
Source§

fn neg(&self) -> Self

Returns the additive inverse of self.
Source§

fn mul(&self, scalar: Self::Scalar) -> Self

Multiplies all components of self by scalar. Read more
Source§

impl<R, Sp> Mul<<Vector<R, Sp> as Linear>::Scalar> for Vector<R, Sp>
where Self: Linear,

Source§

fn mul(self, rhs: <Self as Linear>::Scalar) -> Self

TODO

Source§

type Output = Vector<R, Sp>

The resulting type after applying the * operator.
Source§

impl<R, Sp> Mul<Vector<R, Sp>> for f32
where Vector<R, Sp>: Linear<Scalar = f32>,

Source§

type Output = Vector<R, Sp>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector<R, Sp>) -> Self::Output

Performs the * operation. Read more
Source§

impl<R, Sp> Mul<Vector<R, Sp>> for i32
where Vector<R, Sp>: Linear<Scalar = i32>,

Source§

type Output = Vector<R, Sp>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector<R, Sp>) -> Self::Output

Performs the * operation. Read more
Source§

impl<R, Sp> Mul<Vector<R, Sp>> for u32
where Vector<R, Sp>: Linear<Scalar = u32>,

Source§

type Output = Vector<R, Sp>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vector<R, Sp>) -> Self::Output

Performs the * operation. Read more
Source§

impl<R, Sp> MulAssign<<Vector<R, Sp> as Linear>::Scalar> for Vector<R, Sp>
where Self: Linear,

Source§

fn mul_assign(&mut self, rhs: <Self as Linear>::Scalar)

Performs the *= operation. Read more
Source§

impl<R, Sp> Neg for Vector<R, Sp>
where Self: Linear,

The vector negation operator.

Source§

type Output = Vector<R, Sp>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<R: PartialEq, S> PartialEq for Vector<R, S>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<R, Sp> Sub<<Vector<R, Sp> as Affine>::Diff> for Vector<R, Sp>
where Self: Affine,

Source§

fn sub(self, rhs: <Self as Affine>::Diff) -> Self

TODO

Source§

type Output = Vector<R, Sp>

The resulting type after applying the - operator.
Source§

impl<R, Sp> SubAssign<<Vector<R, Sp> as Affine>::Diff> for Vector<R, Sp>
where Self: Affine,

The vector -= vector operator.

Source§

fn sub_assign(&mut self, rhs: <Self as Affine>::Diff)

Performs the -= operation. Read more
Source§

impl<R, Sp> Sum for Vector<R, Sp>
where Self: Linear,

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<R: Copy, S> Copy for Vector<R, S>

Source§

impl<R: Eq, S> Eq for Vector<R, S>

Auto Trait Implementations§

§

impl<Repr, Space> Freeze for Vector<Repr, Space>
where Repr: Freeze,

§

impl<Repr, Space> RefUnwindSafe for Vector<Repr, Space>
where Repr: RefUnwindSafe, Space: RefUnwindSafe,

§

impl<Repr, Space> Send for Vector<Repr, Space>
where Repr: Send, Space: Send,

§

impl<Repr, Space> Sync for Vector<Repr, Space>
where Repr: Sync, Space: Sync,

§

impl<Repr, Space> Unpin for Vector<Repr, Space>
where Repr: Unpin, Space: Unpin,

§

impl<Repr, Space> UnwindSafe for Vector<Repr, Space>
where Repr: UnwindSafe, Space: UnwindSafe,

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V> Vary for V
where V: Clone + Linear<Scalar = f32>,

Source§

fn step(&self, delta: &<V as Vary>::Diff) -> V

Adds delta to self.

Source§

type Iter = Iter<V>

The iterator returned by the vary method.
Source§

type Diff = <V as Affine>::Diff

The difference type of Self.
Source§

fn vary(self, step: <V as Vary>::Diff, n: Option<u32>) -> <V as Vary>::Iter

Returns an iterator that yields values such that the first value equals self, and each subsequent value is offset by step from its predecessor using the step method. If max is Some(n), stops after n steps, otherwise infinite. Read more
Source§

fn dv_dt(&self, other: &V, recip_dt: f32) -> <V as Vary>::Diff

Returns, conceptually, (other - self) / dt.
Source§

fn z_div(&self, z: f32) -> V

Performs perspective division.
Source§

fn vary_to(self, other: Self, n: u32) -> Self::Iter

Linearly distributes values between self and other inclusive.
Source§

fn lerp(&self, other: &Self, t: f32) -> Self

Linearly interpolates between self and other. Read more