Vector3

Struct Vector3 

Source
pub struct Vector3 { /* private fields */ }
Expand description

Represents a 3-dimensional vector with f32 components.

Implementations§

Source§

impl Vector3

Source

pub fn new(data: [f32; 3]) -> Self

Creates a new 3-vector from the provided array data.

§Example
use bb_geometry::linear_algebra::vector3::*;
let v = Vector3::new([1.0, 2.0, 3.0]);
assert_eq!(v.get(0), 1.0);
Source

pub fn zero() -> Self

Creates a zero vector (all components are 0.0).

§Example
use bb_geometry::linear_algebra::vector3::*;
let zero = Vector3::zero();
assert_eq!(zero.get(1), 0.0);
Source

pub fn unit_x() -> Self

Creates a unit vector along the X axis ([1.0, 0.0, 0.0]).

Source

pub fn unit_y() -> Self

Creates a unit vector along the Y axis ([0.0, 1.0, 0.0]).

Source

pub fn unit_z() -> Self

Creates a unit vector along the Z axis ([0.0, 0.0, 1.0]).

Source

pub fn get(&self, index: usize) -> f32

Gets the value at the specified index using a method call.

Prefer using the index operator vector[index] for more idiomatic access.

§Panics

Panics if index is out of bounds (>= 3).

§Example
use bb_geometry::linear_algebra::vector3::*;
let v = Vector3::new([1.0, 2.0, 3.0]);
assert_eq!(v.get(1), 2.0);
Source

pub fn set(&mut self, index: usize, value: f32)

Sets the value at the specified index using a method call.

Prefer using the mutable index operator vector[index] = value for more idiomatic modification.

§Panics

Panics if index is out of bounds (>= 3).

§Example
use bb_geometry::linear_algebra::vector3::*;
let mut v = Vector3::zero();
v.set(2, 5.0);
assert_eq!(v.get(2), 5.0);
Source

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

Calculates the dot (inner) product of this vector with another vector.

result = self[0]*other[0] + self[1]*other[1] + self[2]*other[2]

§Example
use bb_geometry::linear_algebra::vector3::*;
let v1 = Vector3::new([1.0, 2.0, 3.0]);
let v2 = Vector3::new([4.0, 5.0, 6.0]);
// 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
assert_eq!(v1.dot(&v2), 32.0);
Source

pub fn l2_norm(&self) -> f32

Source

pub fn l1_norm(&self) -> f32

Source

pub fn is_almost_zero(&self) -> bool

Is true if none component of the vector exceeds EPSILON

§Example
use crate::bb_geometry::linear_algebra::vector3::*;
use crate::bb_geometry::linear_algebra::EPSILON;

let zero_vector = Vector3::zero();
let vector_with_epsilon_entries = Vector3::new([EPSILON, EPSILON, 0.0]);
let vector_with_entries_exceeding_epsilon = Vector3::new([EPSILON, 1.1 * EPSILON, 0.0]);

let zero_vector_is_almost_zero = zero_vector.is_almost_zero();
let vector_with_epsilon_entries_is_almost_zero = vector_with_epsilon_entries.is_almost_zero();
let vector_with_entries_exceeding_epsilon_is_not_almost_zero = !vector_with_entries_exceeding_epsilon.is_almost_zero();

assert!(zero_vector_is_almost_zero);
assert!(vector_with_epsilon_entries_is_almost_zero);
assert!(vector_with_entries_exceeding_epsilon_is_not_almost_zero);
Source

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

Calculates the cross product of this vector with another vector (self x other).

Returns a new vector that is perpendicular to both input vectors, following the right-hand rule.

§Example
use bb_geometry::linear_algebra::vector3::*;
let x_axis = Vector3::new([1.0, 0.0, 0.0]);
let y_axis = Vector3::new([0.0, 1.0, 0.0]);
let z_axis = Vector3::new([0.0, 0.0, 1.0]);

let xy_cross = x_axis.cross(&y_axis);
// x x y = z
assert_eq!(xy_cross, z_axis);

let yx_cross = y_axis.cross(&x_axis);
// y x x = -z
assert_eq!(yx_cross, -z_axis);

let v1 = Vector3::new([1.0, 2.0, 3.0]);
let v2 = Vector3::new([4.0, 5.0, 6.0]);
let cross_v1_v2 = v1.cross(&v2);
// x = 2*6 - 3*5 = 12 - 15 = -3
// y = 3*4 - 1*6 = 12 - 6 = 6
// z = 1*5 - 2*4 = 5 - 8 = -3
assert_eq!(cross_v1_v2, Vector3::new([-3.0, 6.0, -3.0]));
let t1 = cross_v1_v2.dot(&v1);
let t2 = cross_v1_v2.dot(&v2);
assert_eq!(t1, 0.0);
assert_eq!(t2, 0.0);
Source

pub fn magnitude_squared(&self) -> f32

Calculates the squared magnitude (length squared) of the vector. Often useful as it avoids a square root calculation. result = xx + yy + z*z

Source

pub fn magnitude(&self) -> f32

Calculates the magnitude (length) of the vector. result = sqrt(xx + yy + z*z)

Source

pub fn normalize(&self) -> Self

Returns a new vector with the same direction but a magnitude of 1. Returns a zero vector if the original vector’s magnitude is zero.

Trait Implementations§

Source§

impl Add<&Vector3> for &Vector3

Implements vector addition using the + operator (&Vector3 + &Vector3).

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<&Vector3> for Vector3

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<Vector3> for &Vector3

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add for Vector3

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Vector3

Source§

fn clone(&self) -> Vector3

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 Vector3

Source§

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

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

impl Index<usize> for Vector3

Allows accessing vector components using index vector[index].

§Panics

Panics if index is out of bounds (>= 3).

Source§

type Output = f32

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Vector3

Allows mutating vector components using index vector[index] = value.

§Panics

Panics if index is out of bounds (>= 3).

Source§

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

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Mul<&Vector3> for &Matrix3x3

Implements Matrix * Vector multiplication (&Matrix3x3 * &Vector3). Treats the vector as a column vector.

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Vector3> for Matrix3x3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Vector3> for f32

Implements scalar multiplication (f32 * &Vector3).

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Vector3> for &Matrix3x3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Vector3> for Matrix3x3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Vector3> for f32

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<f32> for &Vector3

Implements scalar multiplication (&Vector3 * f32).

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<f32> for Vector3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Neg for &Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for Vector3

Implements vector negation using the unary - operator (-vector).

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for Vector3

Source§

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

Implements vector subtraction using the - operator (&Vector3 - &Vector3).

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<&Vector3> for Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<Vector3> for &Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Copy for Vector3

Source§

impl StructuralPartialEq for Vector3

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
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, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V