Struct Vec4

Source
#[repr(C)]
pub struct Vec4 { pub x: f32, pub y: f32, pub z: f32, pub w: f32, }
Expand description

A vector with 4 components: x, y, z, and w. Can be useful for things like shaders, where the registers are aligned to 4 float vectors.

This is a wrapper on System.Numerics.Vector4, so it’s SIMD optimized, and can be cast to and from implicitly. https://stereokit.net/Pages/StereoKit/Vec4.html

see also glam::Vec4

§Examples

use stereokit_rust::maths::Vec4;

let vec4        = Vec4::new(1.0, 2.0, 3.0, 4.0);
let vec4a       = Vec4 { x: 1.0, y: 2.0, z:3.0, w:4.0 };
let vec4b       = Vec4::X + Vec4::Y * 2.0;
let vec4c: Vec4 = [1.0, 2.0, 3.0, 4.0].into();

assert_eq!(vec4, vec4a);
assert_eq!(vec4a + vec4b,                   Vec4 { x: 2.0, y: 4.0, z: 3.0, w: 4.0 });
assert_eq!(vec4c,                           Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 });
assert_eq!(vec4a.length_sq(),               30.0);
assert_eq!(Vec4::dot(Vec4::X, Vec4::Y),     0.0);

Fields§

§x: f32§y: f32§z: f32§w: f32

Implementations§

Source§

impl Vec4

Source

pub const ZERO: Vec4

all components to 0

Source

pub const ONE: Vec4

all components to 1

Source

pub const X: Vec4

A normalized Vector that points down the X axis, this is the same as new Vec4(1,0,0,0). https://stereokit.net/Pages/StereoKit/Vec4/UnitX.html

Source

pub const Y: Vec4

A normalized Vector that points down the Y axis, this is the same as new Vec4(0,1,0,0). https://stereokit.net/Pages/StereoKit/Vec4/UnitY.html

Source

pub const Z: Vec4

A normalized Vector that points down the Z axis, this is the same as new Vec4(0,0,1,0). https://stereokit.net/Pages/StereoKit/Vec4/UnitZ.html

Source

pub const W: Vec4

A normalized Vector that points down the W axis, this is the same as new Vec4(0,0,0,1). https://stereokit.net/Pages/StereoKit/Vec4/UnitW.html

Source

pub const NEG_X: Vec4

A normalized Vector that points up the X axis, this is the same as new Vec4(1,0,0,0).

Source

pub const NEG_Y: Vec4

A normalized Vector that points up the Y axis, this is the same as new Vec4(0,1,0,0).

Source

pub const NEG_Z: Vec4

A normalized Vector that points up the Z axis, this is the same as new Vec4(0,0,1,0).

Source

pub const NEG_W: Vec4

A normalized Vector that points up the W axis, this is the same as new Vec4(0,0,0,1).

Source

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

Source

pub fn xy(&self) -> Vec2

This extracts the Vec2 from the X and Y axes. https://stereokit.net/Pages/StereoKit/Vec4/XY.html

§Examples
use stereokit_rust::maths::{Vec2, Vec4};

let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
let xy = v.xy();
assert_eq!(xy, Vec2::new(1.0, 2.0));
Source

pub fn yz(&self) -> Vec2

This extracts the Vec2 from the Y and Z axes. https://stereokit.net/Pages/StereoKit/Vec4/YZ.html

§Examples
use stereokit_rust::maths::{Vec2, Vec4};

let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
let yz = v.yz();
assert_eq!(yz, Vec2::new(2.0, 3.0));
Source

pub fn xz(&self) -> Vec2

This extracts the Vec2 from the X and Z axes. https://stereokit.net/Pages/StereoKit/Vec4/XZ.html

§Examples
use stereokit_rust::maths::{Vec2, Vec4};

let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
let xz = v.xz();
assert_eq!(xz, Vec2::new(1.0, 3.0));
Source

pub fn zw(&self) -> Vec2

This extracts the Vec2 from the Z and W axes. https://stereokit.net/Pages/StereoKit/Vec4/ZW.html

§Examples
use stereokit_rust::maths::{Vec2, Vec4};

let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
let zw = v.zw();
assert_eq!(zw, Vec2::new(3.0, 4.0));
Source

pub fn xyz(&self) -> Vec3

This extracts a Vec3 from the X, Y, and Z axes. https://stereokit.net/Pages/StereoKit/Vec4/XYZ.html

§Examples
use stereokit_rust::maths::{Vec3, Vec4};

let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
let v3 = v.xyz();
assert_eq!(v3, Vec3::new(1.0, 2.0, 3.0));
Source

pub fn get_as_quat(&self) -> Quat

A Vec4 and a Quat are only really different by name and purpose. So, if you need to do Quat math with your Vec4, or visa versa, who am I to judge? https://stereokit.net/Pages/StereoKit/Vec4/Quat.html

§Examples
use stereokit_rust::maths::{Vec4, Quat};

let vec4 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let quat = vec4.get_as_quat();
assert_eq!(quat, Quat::new(1.0, 2.0, 3.0, 4.0));
Source

pub fn length_sq(&self) -> f32

This is the squared length/magnitude of the vector! It skips the Sqrt call, and just gives you the squared version for speedy calculations that can work with it squared. https://stereokit.net/Pages/StereoKit/Vec4.html

§Examples
use stereokit_rust::maths::Vec4;

let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
assert_eq!(v.length_sq(), 30.0);
Source

pub fn dot(a: Self, b: Self) -> f32

What’s a dot product do for 4D vectors, you might ask? Well, I’m no mathematician, so hopefully you are! I’ve never used it before. Whatever you’re doing with this function, it’s SIMD fast! https://stereokit.net/Pages/StereoKit/Vec4/Dot.html

  • a - The first vector.
  • b - The second vector.

Returns the dot product of the two vectors.

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
let b = Vec4::new(5.0, 6.0, 7.0, 8.0);
let result = Vec4::dot(a, b);
assert_eq!(result, 70.0);
Source

pub fn lerp(a: Self, b: Self, blend: f32) -> Self

Blends (Linear Interpolation) between two vectors, based on a ‘blend’ value, where 0 is a, and 1 is b. Doesn’t clamp percent for you. https://stereokit.net/Pages/StereoKit/Vec4/Lerp.html

  • a - First item in the blend, or ’0.0 blend.
  • b - Second item in the blend, or ’1.0 blend.
  • blend - A blend value between 0 and 1. Can be outside this range, it’ll just interpolate outside of the a, b range.

Returns an unclamped blend of a and b.

§Examples
use stereokit_rust::maths::Vec4;
let a = Vec4::new(0.0, 0.0, 0.0, 0.0);
let b = Vec4::new(1.0, 1.0, 1.0, 1.0);
let result = Vec4::lerp(a, b, 0.75);
assert_eq!(result, Vec4::new(0.75, 0.75, 0.75, 0.75));
Source

pub fn max(a: Self, b: Self) -> Self

Returns a vector where each element is the maximum value for each corresponding pair. https://stereokit.net/Pages/StereoKit/Vec4/Max.html

  • a - Order isn’t important here.
  • b - Order isn’t important here.

Returns a vector where each element is the maximum value for each corresponding pair.

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
let b = Vec4::new(4.0, 3.0, 2.0, 1.0);
let c = Vec4::max(a, b);
assert_eq!(c, Vec4::new(4.0, 3.0, 3.0, 4.0));
Source

pub fn min(a: Self, b: Self) -> Self

Returns a vector where each element is the minimum value for each corresponding pair. https://stereokit.net/Pages/StereoKit/Vec4/Min.html

  • a - Order isn’t important here.
  • b - Order isn’t important here.

Returns a new vector with the minimum values.

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
let b = Vec4::new(4.0, 3.0, 2.0, 1.0);
let c = Vec4::min(a, b);
assert_eq!(c, Vec4::new(1.0, 2.0, 2.0, 1.0));

Trait Implementations§

Source§

impl Add for Vec4

Adds matching components together. Commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Addition.html

Source§

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

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 5.0);
let b = Vec4::new(2.0, 3.0, 4.0, 6.0);
let c = a + b;
assert_eq!(c, Vec4::new(3.0, 5.0, 7.0, 11.0));
Source§

type Output = Vec4

The resulting type after applying the + operator.
Source§

impl AddAssign for Vec4

Adds matching components together. Commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Addition.html

Source§

fn add_assign(&mut self, rhs: Self)

§Examples
use stereokit_rust::maths::Vec4;

let mut v1 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let v2 = Vec4::new(5.0, 6.0, 7.0, 8.0);
v1 += v2;
assert_eq!(v1, Vec4::new(6.0, 8.0, 10.0, 12.0));
Source§

impl Clone for Vec4

Source§

fn clone(&self) -> Vec4

Returns a duplicate 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 Debug for Vec4

Source§

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

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

impl Default for Vec4

Source§

fn default() -> Vec4

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

impl Display for Vec4

Source§

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

Mostly for debug purposes, this is a decent way to log or inspect the vector in debug mode. Looks like “[x, y, z, w]” https://stereokit.net/Pages/StereoKit/Vec4/ToString.html

§Examples
use stereokit_rust::maths::Vec4;

let v = Vec4::new(1.0, 2.2, 3.0, 4.0);
assert_eq!(format!("{}", v), "[x:1, y:2.2, z:3, w:4]");
Source§

impl Div<Vec4> for f32

Source§

fn div(self, rhs: Vec4) -> Self::Output

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 7.0, 3.0, 4.0);
let b = a / 2.0;
assert_eq!(b, Vec4::new(0.5, 3.5, 1.5, 2.0));
Source§

type Output = Vec4

The resulting type after applying the / operator.
Source§

impl Div<f32> for Vec4

Source§

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

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
let b = a / 2.0;
assert_eq!(b, Vec4::new(0.5, 1.0, 1.5, 2.0));
Source§

type Output = Vec4

The resulting type after applying the / operator.
Source§

impl Div for Vec4

Source§

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

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
let b = Vec4::new(2.0, 2.0, 2.0, 2.0);
let c = a / b;
assert_eq!(c, Vec4::new(0.5, 1.0, 1.5, 2.0));
Source§

type Output = Vec4

The resulting type after applying the / operator.
Source§

impl DivAssign<f32> for Vec4

Source§

fn div_assign(&mut self, rhs: f32)

§Examples
use stereokit_rust::maths::Vec4;

let mut v = Vec4::new(1.0, 2.0, 3.0, 4.0);
v /= 2.0;
assert_eq!(v, Vec4::new(0.5, 1.0, 1.5, 2.0));
Source§

impl DivAssign for Vec4

Source§

fn div_assign(&mut self, rhs: Self)

§Examples
use stereokit_rust::maths::*;

let mut a = Vec4::new(1.0, 2.0, 3.0, 4.0);
let b = Vec4::new(2.0, 2.0, 2.0, 2.0);
a /= b;
assert_eq!(a, Vec4::new(0.5, 1.0, 1.5, 2.0));
Source§

impl From<[f32; 4]> for Vec4

Source§

fn from(val: [f32; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<Quat> for Vec4

Source§

fn from(val: Quat) -> Self

Converts to this type from the input type.
Source§

impl From<Vec4> for Quat

Source§

fn from(val: Vec4) -> Self

Converts to this type from the input type.
Source§

impl From<Vec4> for Vec4

Source§

fn from(value: Vec4) -> Self

Converts to this type from the input type.
Source§

impl From<Vec4> for Vec4

Source§

fn from(value: Vec4) -> Self

Converts to this type from the input type.
Source§

impl Mul<Matrix> for Vec4

Source§

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

§Examples
use stereokit_rust::maths::{Vec4, Matrix};

let transform = Matrix::t([1.0, 2.0, 3.0]);
let v4 = Vec4::new(1.0, 1.0, 1.0, 1.0);

let transformed_v4 = v4 * transform;
assert_eq!(transformed_v4, Vec4::new(2.0, 3.0, 4.0, 1.0));
Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

impl Mul<Vec4> for Matrix

Source§

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

§Examples
use stereokit_rust::maths::{Vec4, Matrix};

let transform = Matrix::t([1.0, 2.0, 3.0]);
let v4 = Vec4::new(1.0, 1.0, 1.0, 1.0);

let transformed_v4 = transform * v4;
assert_eq!(transformed_v4, Vec4::new(2.0, 3.0, 4.0, 1.0));
Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

impl Mul<Vec4> for f32

A component-wise vector multiplication, same thing as a non-uniform scale. NOT a dot or cross product! Commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Multiply.html

Source§

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

§Examples
use stereokit_rust::maths::Vec4;

let a = 2.0;
let b = Vec4::new(2.0, 3.0, 4.0, 6.0);
let c = a * b;
assert_eq!(c, Vec4::new(4.0, 6.0, 8.0, 12.0));
Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

impl Mul<f32> for Vec4

A component-wise vector multiplication, same thing as a non-uniform scale. NOT a dot or cross product! Commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Multiply.html

Source§

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

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 5.0);
let b = a * 2.0;
assert_eq!(b, Vec4::new(2.0, 4.0, 6.0, 10.0));
Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

impl Mul for Vec4

A component-wise vector multiplication, same thing as a non-uniform scale. NOT a dot or cross product! Commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Multiply.html

Source§

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

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 5.0);
let b = Vec4::new(2.0, 3.0, 4.0, 6.0);
let c = a * b;
assert_eq!(c, Vec4::new(2.0, 6.0, 12.0, 30.0));
Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

impl MulAssign<Matrix> for Vec4

Source§

fn mul_assign(&mut self, rhs: Matrix)

§Examples
use stereokit_rust::maths::{Vec4, Matrix};

let mut v4 = Vec4::new(1.0, 1.0, 1.0, 1.0);
let transform = Matrix::t([1.0, 2.0, 3.0]);

v4 *= transform;
assert_eq!(v4, Vec4::new(2.0, 3.0, 4.0, 1.0));
Source§

impl MulAssign<f32> for Vec4

A component-wise vector multiplication, same thing as a non-uniform scale. NOT a dot or cross product! Commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Multiply.html

Source§

fn mul_assign(&mut self, rhs: f32)

§Examples
use stereokit_rust::maths::Vec4;

let mut a = Vec4::new(1.0, 2.0, 7.0, 5.0);
a *= 2.0;
assert_eq!(a, Vec4::new(2.0, 4.0, 14.0, 10.0));
Source§

impl MulAssign for Vec4

A component-wise vector multiplication, same thing as a non-uniform scale. NOT a dot or cross product! Commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Multiply.html

Source§

fn mul_assign(&mut self, rhs: Self)

§Examples
use stereokit_rust::maths::Vec4;

let mut a = Vec4::new(1.0, 2.0, 3.0, 5.0);
let b = Vec4::new(2.0, 3.0, 4.0, 6.0);
a *= b;
assert_eq!(a, Vec4::new(2.0, 6.0, 12.0, 30.0));
Source§

impl Neg for Vec4

Vector negation, returns a vector where each component has been negated. https://stereokit.net/Pages/StereoKit/Vec4/op_UnaryNegation.html

Source§

fn neg(self) -> Self::Output

§Examples
use stereokit_rust::maths::Vec4;

let v = Vec4::new(1.0, 2.0, 3.0, 4.0);
let neg_v = -v;
assert_eq!(neg_v, Vec4::new(-1.0, -2.0, -3.0, -4.0));
Source§

type Output = Vec4

The resulting type after applying the - operator.
Source§

impl PartialEq for Vec4

Warning: Equality with a precision of 0.1 millimeter

Source§

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

Warning: Equality with a precision of 0.1 millimeter

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 Sub for Vec4

Source§

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

§Examples
use stereokit_rust::maths::Vec4;

let a = Vec4::new(1.0, 2.0, 3.0, 4.0);
let b = Vec4::new(4.0, 3.0, 2.0, 1.0);
let c = a - b;
assert_eq!(c, Vec4::new(-3.0, -1.0, 1.0, 3.0));
Source§

type Output = Vec4

The resulting type after applying the - operator.
Source§

impl SubAssign for Vec4

Subtracts matching components from eachother. Not commutative. https://stereokit.net/Pages/StereoKit/Vec4/op_Subtraction.html

Source§

fn sub_assign(&mut self, rhs: Vec4)

§Examples
use stereokit_rust::maths::Vec4;

let mut v1 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let v2 = Vec4::new(1.0, 2.0, 3.0, 4.0);
v1 -= v2;
assert_eq!(v1, Vec4::new(0.0, 0.0, 0.0, 0.0));
Source§

impl Copy for Vec4

Auto Trait Implementations§

§

impl Freeze for Vec4

§

impl RefUnwindSafe for Vec4

§

impl Send for Vec4

§

impl Sync for Vec4

§

impl Unpin for Vec4

§

impl UnwindSafe for Vec4

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more