Skip to main content

Vector

Struct Vector 

Source
pub struct Vector {
    pub x: f64,
    pub y: f64,
    pub z: f64,
}
Expand description

A free vector in space.

Fields§

§x: f64

X component.

§y: f64

Y component.

§z: f64

Z component.

Implementations§

Source§

impl Vector

Source

pub const ZERO: Self

The zero vector.

Source

pub const X: Self

Unit vector along +X.

Source

pub const Y: Self

Unit vector along +Y.

Source

pub const Z: Self

Unit vector along +Z.

Source

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

A vector from components.

Source

pub const fn splat(v: f64) -> Self

A vector with all components equal.

Source

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

Components as an array.

Source

pub const fn from_array([x, y, z]: [f64; 3]) -> Self

A vector from an array.

Source

pub fn coord(self, index: usize) -> OgeomResult<f64>

Component by index, 0..3.

§Errors

OgeomError::Range if index >= 3.

Source

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

Dot product.

Source

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

Cross product. Right-handed: X.cross(Y) == Z.

Each component is a two-term difference ab - cd, evaluated without a fused multiply-add on purpose. An FMA rounds one product and not the other, which destroys the exact cancellation that makes the cross product of collinear vectors come out at exactly zero.

Source

pub fn triple(self, a: Self, b: Self) -> f64

Scalar triple product self · (a × b): the signed volume of the parallelepiped the three vectors span.

Source

pub fn square_magnitude(self) -> f64

Squared magnitude. Prefer this to Vector::magnitude when comparing lengths: it avoids a square root and the rounding that comes with it.

Source

pub fn magnitude(self) -> f64

Magnitude.

Source

pub fn normalized(self, tol: Tolerances) -> OgeomResult<Self>

This vector scaled to unit length.

§Errors

OgeomError::Construction if the magnitude is below tol.confusion(), or if any component is non-finite. Normalizing a near-zero vector amplifies whatever noise it holds into an arbitrary direction, so it is refused rather than approximated.

Source

pub fn is_finite(self) -> bool

Whether every component is finite.

Source

pub fn is_zero(self, tol: Tolerances) -> bool

Whether the magnitude is within tol.confusion() of zero.

Source

pub fn is_equal(self, other: Self, tol: Tolerances) -> bool

Whether two vectors agree component-wise within tol.confusion().

Source

pub fn angle(self, other: Self, tol: Tolerances) -> OgeomResult<f64>

Angle to other, in [0, π].

Uses atan2 of the cross and dot products rather than acos of the normalized dot product: acos loses most of its precision for nearly parallel or nearly antiparallel vectors, which is exactly where angle tests matter.

§Errors

OgeomError::Construction if either vector is degenerate.

Source

pub fn signed_angle( self, other: Self, reference: Self, tol: Tolerances, ) -> OgeomResult<f64>

Angle to other measured about reference, in (-π, π].

Positive when self turns towards other counter-clockwise as seen from the tip of reference.

§Errors

OgeomError::Construction if any vector is degenerate, or if reference is not perpendicular to both within tol.angular().

Source

pub fn is_parallel(self, other: Self, tol: Tolerances) -> bool

Whether self and other point the same way, within tol.angular().

Source

pub fn is_collinear(self, other: Self, tol: Tolerances) -> bool

Whether self and other are parallel, ignoring sense.

Source

pub fn is_normal(self, other: Self, tol: Tolerances) -> bool

Whether self and other are perpendicular, within tol.angular().

Source

pub fn projected_onto(self, other: Self, tol: Tolerances) -> OgeomResult<Self>

Component of self along other.

§Errors

OgeomError::Construction if other is degenerate.

Source

pub fn lerp(self, other: Self, t: f64) -> Self

Linear interpolation, t = 0 giving self.

Source

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

Component-wise minimum.

Source

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

Component-wise maximum.

Source

pub const fn xy(self) -> Vector2

The 2D vector formed by dropping the Z component.

Trait Implementations§

Source§

impl Add for Vector

Source§

type Output = Vector

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<Vector> for Point

Source§

type Output = Point

The resulting type after applying the + operator.
Source§

fn add(self, v: Vector) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Vector

Source§

fn add_assign(&mut self, o: Self)

Performs the += operation. Read more
Source§

impl AddAssign<Vector> for Point

Source§

fn add_assign(&mut self, v: Vector)

Performs the += operation. Read more
Source§

impl Blend for Vector

Source§

fn zero() -> Self

The additive identity.
Source§

fn scale(self, k: f64) -> Self

Scale by a factor.
Source§

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

Add another value.
Source§

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

self * (1 - t) + other * t.
Source§

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

Subtract, via scaling by -1.
Source§

impl Clone for Vector

Source§

fn clone(&self) -> Vector

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 Vector

Source§

impl Debug for Vector

Source§

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

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

impl Default for Vector

Source§

fn default() -> Vector

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

impl Div<f64> for Vector

Source§

type Output = Vector

The resulting type after applying the / operator.
Source§

fn div(self, s: f64) -> Self

Performs the / operation. Read more
Source§

impl DivAssign<f64> for Vector

Source§

fn div_assign(&mut self, s: f64)

Performs the /= operation. Read more
Source§

impl From<Direction> for Vector

Source§

fn from(d: Direction) -> Self

Converts to this type from the input type.
Source§

impl Mul<Vector> for Matrix3

Source§

type Output = Vector

The resulting type after applying the * operator.
Source§

fn mul(self, v: Vector) -> Vector

Performs the * operation. Read more
Source§

impl Mul<Vector> for f64

Source§

type Output = Vector

The resulting type after applying the * operator.
Source§

fn mul(self, v: Vector) -> Vector

Performs the * operation. Read more
Source§

impl Mul<f64> for Vector

Source§

type Output = Vector

The resulting type after applying the * operator.
Source§

fn mul(self, s: f64) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<f64> for Vector

Source§

fn mul_assign(&mut self, s: f64)

Performs the *= operation. Read more
Source§

impl Neg for Vector

Source§

type Output = Vector

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Vector

Source§

fn eq(&self, other: &Vector) -> 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 Vector

Source§

impl Sub for Vector

Source§

type Output = Vector

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<Vector> for Point

Source§

type Output = Point

The resulting type after applying the - operator.
Source§

fn sub(self, v: Vector) -> Self

Performs the - operation. Read more
Source§

impl SubAssign for Vector

Source§

fn sub_assign(&mut self, o: Self)

Performs the -= operation. Read more
Source§

impl SubAssign<Vector> for Point

Source§

fn sub_assign(&mut self, v: Vector)

Performs the -= operation. Read more

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
§

impl<T, Right> ClosedAdd<Right> for T
where T: Add<Right, Output = T> + AddAssign<Right>,

§

impl<T, Right> ClosedAddAssign<Right> for T
where T: ClosedAdd<Right> + AddAssign<Right>,

§

impl<T, Right> ClosedDiv<Right> for T
where T: Div<Right, Output = T> + DivAssign<Right>,

§

impl<T, Right> ClosedDivAssign<Right> for T
where T: ClosedDiv<Right> + DivAssign<Right>,

§

impl<T, Right> ClosedMul<Right> for T
where T: Mul<Right, Output = T> + MulAssign<Right>,

§

impl<T, Right> ClosedMulAssign<Right> for T
where T: ClosedMul<Right> + MulAssign<Right>,

§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

§

impl<T, Right> ClosedSub<Right> for T
where T: Sub<Right, Output = T> + SubAssign<Right>,

§

impl<T, Right> ClosedSubAssign<Right> for T
where T: ClosedSub<Right> + SubAssign<Right>,

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.