Skip to main content

Vector3

Struct Vector3 

Source
pub struct Vector3<T> {
    pub x: T,
    pub y: T,
    pub z: T,
}
Expand description

A vector of three components x, y, and z.

Fields§

§x: T

The first component.

§y: T

The second component.

§z: T

The third component.

Implementations§

Source§

impl<T> Vector3<T>

Source

pub const fn new(x: T, y: T, z: T) -> Self

Constructs a vector from its three components.

Source

pub fn from_array(array: [T; 3]) -> Self

Constructs a vector from an array [x, y, z].

Source

pub fn to_array(self) -> [T; 3]

Returns the components as an array [x, y, z].

Source

pub fn with_x(self, x: T) -> Self

Returns a copy of self with the x component replaced.

Source

pub fn with_y(self, y: T) -> Self

Returns a copy of self with the y component replaced.

Source

pub fn with_z(self, z: T) -> Self

Returns a copy of self with the z component replaced.

Source

pub fn map<U, F: FnMut(T) -> U>(self, f: F) -> Vector3<U>

Applies f to every component, returning the resulting vector.

Source

pub fn zip_map<U, R, F: FnMut(T, U) -> R>( self, rhs: Vector3<U>, f: F, ) -> Vector3<R>

Combines self and rhs component-wise through f.

Source§

impl<T: Copy> Vector3<T>

Source

pub const fn splat(value: T) -> Self

Constructs a vector with all three components set to value.

Source

pub fn from_slice(slice: &[T]) -> Self

Constructs a vector from the first three elements of a slice.

§Panics

Panics if slice has fewer than three elements.

Source§

impl<T: Add<Output = T>> Vector3<T>

Source

pub fn element_sum(self) -> T

Returns the sum of the three components, x + y + z.

Source§

impl<T: Add<Output = T> + Sub<Output = T> + Copy> Vector3<T>

Source

pub fn lerp<S: Scalar>(self, rhs: Self, t: S) -> Self
where T: Mul<S, Output = T>,

Linearly interpolates from self toward rhs by the factor t.

t == 0 yields self, t == 1 yields rhs.

Source§

impl<V: Scalar> Vector3<V>

Source

pub const ZERO: Self

The zero vector, (0, 0, 0).

Source

pub const ONE: Self

The vector with every component set to one, (1, 1, 1).

Source

pub const X: Self

The unit vector along the x axis, (1, 0, 0).

Source

pub const Y: Self

The unit vector along the y axis, (0, 1, 0).

Source

pub const Z: Self

The unit vector along the z axis, (0, 0, 1).

Source

pub fn dot(self, rhs: Self) -> V

Returns the dot product self · rhs.

Source

pub fn cross(self, rhs: Self) -> Self

Returns the cross product self × rhs.

Source

pub fn norm_squared(self) -> V

Returns the squared Euclidean norm self · self.

Cheaper than norm and sufficient whenever only relative magnitudes are compared.

Source

pub fn norm(self) -> V

Returns the Euclidean norm (length) of the vector.

Source

pub fn normalize(self) -> Self

Returns self rescaled to unit length.

Yields a non-finite vector when the norm is zero or non-finite; use try_normalize or normalize_or_zero to handle those cases.

Source

pub fn try_normalize(self) -> Option<Self>

Returns self rescaled to unit length, or None if the result would not be finite (e.g. for the zero vector).

Source

pub fn normalize_or_zero(self) -> Self

Returns self rescaled to unit length, or the zero vector if the result would not be finite.

Source

pub fn is_normalized(self) -> bool

Returns true if the vector is of unit length within a small tolerance (2.0e-4).

Source

pub fn angle_between(self, rhs: Self) -> V

Returns the unsigned angle between self and rhs, in radians within [0, π].

The computation is numerically stable across the whole range, including near-parallel and near-antiparallel inputs.

Source

pub fn project_onto(self, onto: Self) -> Self

Returns the vector projection of self onto onto.

Source

pub fn reject_from(self, from: Self) -> Self

Returns the component of self orthogonal to from.

Source

pub fn reflect(self, normal: Self) -> Self

Reflects self across the plane through the origin with unit normal normal.

Source

pub fn recip(self) -> Self

Returns the component-wise reciprocal (1/x, 1/y, 1/z).

Source

pub fn element_product(self) -> V

Returns the product of the three components, x * y * z.

Source

pub fn abs(self) -> Self

Returns the component-wise absolute value.

Source

pub fn min(self, rhs: Self) -> Self

Returns the component-wise minimum of self and rhs.

Source

pub fn max(self, rhs: Self) -> Self

Returns the component-wise maximum of self and rhs.

Source

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

Restricts every component to the interval [min, max].

§Panics

Panics if any component of min exceeds the corresponding component of max.

Source

pub fn min_element(self) -> V

Returns the smallest of the three components.

Source

pub fn max_element(self) -> V

Returns the largest of the three components.

Source

pub fn floor(self) -> Self

Returns the component-wise floor.

Source

pub fn ceil(self) -> Self

Returns the component-wise ceiling.

Source

pub fn round(self) -> Self

Returns the component-wise nearest integer, rounding halves away from zero.

Source

pub fn round_ties_even(self) -> Self

Returns the component-wise nearest integer, rounding halves to even.

Source

pub fn trunc(self) -> Self

Returns the component-wise truncation toward zero.

Source

pub fn fract(self) -> Self

Returns the component-wise fractional part.

Source

pub fn copysign(self, sign: Self) -> Self

Returns a vector with the magnitudes of self and the component-wise signs of sign.

Source

pub fn signum(self) -> Self

Returns the component-wise sign, each 1, -1, or NaN.

Source

pub fn rem_euclid(self, rhs: Self) -> Self

Returns the component-wise least nonnegative remainder against rhs.

Source

pub fn is_finite(self) -> bool

Returns true if every component is finite.

Source

pub fn is_infinite(self) -> bool

Returns true if any component is positive or negative infinity.

Source

pub fn is_nan(self) -> bool

Returns true if any component is NaN.

Trait Implementations§

Source§

impl<T: Add<Output = T>> Add for Vector3<T>

Source§

fn add(self, rhs: Self) -> Self

Returns the component-wise sum of self and rhs.

Source§

type Output = Vector3<T>

The resulting type after applying the + operator.
Source§

impl<T: Add<Output = T>> Add<Vector3<T>> for Point3<T>

Source§

fn add(self, rhs: Vector3<T>) -> Self

Translates the point by the displacement rhs.

Source§

type Output = Point3<T>

The resulting type after applying the + operator.
Source§

impl<T: AddAssign> AddAssign for Vector3<T>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<T: AddAssign> AddAssign<Vector3<T>> for Point3<T>

Source§

fn add_assign(&mut self, rhs: Vector3<T>)

Performs the += operation. Read more
Source§

impl<T: Clone> Clone for Vector3<T>

Source§

fn clone(&self) -> Self

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<T: Copy> Copy for Vector3<T>

Source§

impl<T: Debug> Debug for Vector3<T>

Source§

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

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

impl<T: Default> Default for Vector3<T>

Source§

fn default() -> Self

Returns the zero vector.

Source§

impl<T, S: Scalar> Div<S> for Vector3<T>
where T: Div<S, Output = T>,

Source§

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

Divides every component by the scalar rhs, preserving the element’s unit.

Source§

type Output = Vector3<T>

The resulting type after applying the / operator.
Source§

impl<T, S: Scalar> DivAssign<S> for Vector3<T>
where T: DivAssign<S>,

Source§

fn div_assign(&mut self, rhs: S)

Performs the /= operation. Read more
Source§

impl<T> Index<usize> for Vector3<T>

Source§

fn index(&self, index: usize) -> &T

Returns the component at index, where 0, 1, and 2 map to x, y, and z.

§Panics

Panics if index is greater than 2.

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IndexMut<usize> for Vector3<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut T

Returns the component at index, where 0, 1, and 2 map to x, y, and z.

§Panics

Panics if index is greater than 2.

Source§

impl<T, S: Scalar> Mul<S> for Vector3<T>
where T: Mul<S, Output = T>,

Source§

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

Scales every component by the scalar rhs, preserving the element’s unit.

Source§

type Output = Vector3<T>

The resulting type after applying the * operator.
Source§

impl<V: Scalar> Mul<Vector3<V>> for Matrix3<V>

Source§

fn mul(self, rhs: Vector3<V>) -> Vector3<V>

Applies the matrix to the vector, returning self * rhs.

Source§

type Output = Vector3<V>

The resulting type after applying the * operator.
Source§

impl<T, S: Scalar> MulAssign<S> for Vector3<T>
where T: MulAssign<S>,

Source§

fn mul_assign(&mut self, rhs: S)

Performs the *= operation. Read more
Source§

impl<T: Neg<Output = T>> Neg for Vector3<T>

Source§

fn neg(self) -> Self

Returns the component-wise negation of self.

Source§

type Output = Vector3<T>

The resulting type after applying the - operator.
Source§

impl<T: PartialEq> PartialEq for Vector3<T>

Source§

fn eq(&self, other: &Self) -> 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<T: Sub<Output = T>> Sub for Vector3<T>

Source§

fn sub(self, rhs: Self) -> Self

Returns the component-wise difference of self and rhs.

Source§

type Output = Vector3<T>

The resulting type after applying the - operator.
Source§

impl<T: Sub<Output = T>> Sub<Vector3<T>> for Point3<T>

Source§

fn sub(self, rhs: Vector3<T>) -> Self

Translates the point by the negated displacement rhs.

Source§

type Output = Point3<T>

The resulting type after applying the - operator.
Source§

impl<T: SubAssign> SubAssign for Vector3<T>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<T: SubAssign> SubAssign<Vector3<T>> for Point3<T>

Source§

fn sub_assign(&mut self, rhs: Vector3<T>)

Performs the -= operation. Read more
Source§

impl<T: Add<Output = T> + Default + Copy> Sum for Vector3<T>

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<'a, T: Add<Output = T> + Default + Copy> Sum<&'a Vector3<T>> for Vector3<T>

Source§

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

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

Auto Trait Implementations§

§

impl<T> Freeze for Vector3<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Vector3<T>
where T: RefUnwindSafe,

§

impl<T> Send for Vector3<T>
where T: Send,

§

impl<T> Sync for Vector3<T>
where T: Sync,

§

impl<T> Unpin for Vector3<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Vector3<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Vector3<T>
where T: 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, 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> 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.