pub struct Vector3 { /* private fields */ }Expand description
Represents a 3-dimensional vector with f32 components.
Implementations§
Source§impl Vector3
impl Vector3
Sourcepub fn new(data: [f32; 3]) -> Self
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);Sourcepub fn zero() -> Self
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);Sourcepub fn get(&self, index: usize) -> f32
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);Sourcepub fn set(&mut self, index: usize, value: f32)
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);Sourcepub fn dot(&self, other: &Self) -> f32
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);pub fn l2_norm(&self) -> f32
pub fn l1_norm(&self) -> f32
Sourcepub fn is_almost_zero(&self) -> bool
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);Sourcepub fn cross(&self, other: &Self) -> Self
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);Sourcepub fn magnitude_squared(&self) -> f32
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
Trait Implementations§
Source§impl Add<&Vector3> for &Vector3
Implements vector addition using the + operator (&Vector3 + &Vector3).
impl Add<&Vector3> for &Vector3
Implements vector addition using the + operator (&Vector3 + &Vector3).
Source§impl Index<usize> for Vector3
Allows accessing vector components using index vector[index].
impl Index<usize> for Vector3
Allows accessing vector components using index vector[index].
§Panics
Panics if index is out of bounds (>= 3).
Source§impl IndexMut<usize> for Vector3
Allows mutating vector components using index vector[index] = value.
impl IndexMut<usize> for Vector3
Allows mutating vector components using index vector[index] = value.
§Panics
Panics if index is out of bounds (>= 3).
Source§impl Mul<&Vector3> for &Matrix3x3
Implements Matrix * Vector multiplication (&Matrix3x3 * &Vector3).
Treats the vector as a column vector.
impl Mul<&Vector3> for &Matrix3x3
Implements Matrix * Vector multiplication (&Matrix3x3 * &Vector3).
Treats the vector as a column vector.
Source§impl Sub<&Vector3> for &Vector3
Implements vector subtraction using the - operator (&Vector3 - &Vector3).
impl Sub<&Vector3> for &Vector3
Implements vector subtraction using the - operator (&Vector3 - &Vector3).