pub struct Vec3<T> {
pub x: T,
pub y: T,
pub z: T,
}Expand description
Represents a 3D vector with generic numeric components.
Vec3 is a generic structure that represents a 3-dimensional vector with components x, y, and z.
It provides methods for vector operations such as vector arithmetic, normalization, dot product,
cross product, distance calculation, and linear interpolation.
§Type Parameters
T: The numeric type of the vector components. This type must implement traits likeZero,One,NumAssign,Copy, and others depending on the method.
Fields§
§x: T§y: T§z: TImplementations§
Source§impl<T> Vec3<T>
impl<T> Vec3<T>
Sourcepub fn new(x: T, y: T, z: T) -> Self
pub fn new(x: T, y: T, z: T) -> Self
Constructs a new Vec3 with the given x, y, and z components.
§Parameters
x: The x component of the vector.y: The y component of the vector.z: The z component of the vector.
§Returns
Returns a new Vec3 instance with the specified x, y, and z values.
§Examples
let vec = Vec3::new(1.0, 2.0, 3.0);
assert_eq!(vec.x, 1.0);
assert_eq!(vec.y, 2.0);
assert_eq!(vec.z, 3.0);Sourcepub fn set(v: T) -> Self
pub fn set(v: T) -> Self
Constructs a Vec3 where all components are set to the same value v.
§Parameters
v: The value to set for all components of the vector.
§Returns
Returns a new Vec3 instance with all components set to v.
§Examples
let vec = Vec3::set(2.0);
assert_eq!(vec.x, 2.0);
assert_eq!(vec.y, 2.0);
assert_eq!(vec.z, 2.0);Sourcepub fn from_vec2(v: &Vec2<T>) -> Self
pub fn from_vec2(v: &Vec2<T>) -> Self
Constructs a Vec3 from a Vec2 by setting the z component to zero.
This function is useful for converting a 2D vector to a 3D vector when the z component is not needed or is implicitly zero.
§Parameters
v: TheVec2instance to convert.
§Returns
Returns a new Vec3 instance with the x and y components copied from v and the z component set to zero.
§Examples
let vec2 = Vec2::new(1.0, 2.0);
let vec3 = Vec3::from_vec2(&vec2);
assert_eq!(vec3.x, 1.0);
assert_eq!(vec3.y, 2.0);
assert_eq!(vec3.z, T::zero()); // z component is zeroSourcepub fn from_vec4(v: &Vec4<T>) -> Self
pub fn from_vec4(v: &Vec4<T>) -> Self
Constructs a Vec3 from a Vec4 by discarding the w component.
This function is useful for converting a 4D vector to a 3D vector when the w component is not needed or is implicitly zero.
§Parameters
v: TheVec4instance to convert.
§Returns
Returns a new Vec3 instance with the x, y, and z components copied from v.
§Examples
let vec4 = Vec4::new(1.0, 2.0, 3.0, 4.0);
let vec3 = Vec3::from_vec4(&vec4);
assert_eq!(vec3.x, 1.0);
assert_eq!(vec3.y, 2.0);
assert_eq!(vec3.z, 3.0);Sourcepub fn dot(&self, other: &Self) -> T
pub fn dot(&self, other: &Self) -> T
Computes the dot product of this vector with another vector.
The dot product of two vectors is a scalar value that represents their degree of alignment.
It is calculated as self.x * other.x + self.y * other.y + self.z * other.z.
§Parameters
other: The vector to compute the dot product with.
§Returns
Returns the dot product of this vector and other.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(4.0, 5.0, 6.0);
let dot_product = vec1.dot(&vec2);
assert_eq!(dot_product, 32.0); // 1*4 + 2*5 + 3*6 = 32Sourcepub fn cross(&self, other: &Self) -> Self
pub fn cross(&self, other: &Self) -> Self
Computes the cross product of this vector with another vector.
The cross product of two vectors results in a vector that is perpendicular to both of the original vectors. It is calculated using the formula:
x = self.y * other.z - self.z * other.y
y = self.z * other.x - self.x * other.z
z = self.x * other.y - self.y * other.x§Parameters
other: The vector to compute the cross product with.
§Returns
Returns a new Vec3 instance that is the result of the cross product.
§Examples
let vec1 = Vec3::new(1.0, 0.0, 0.0);
let vec2 = Vec3::new(0.0, 1.0, 0.0);
let cross_product = vec1.cross(&vec2);
assert_eq!(cross_product, Vec3::new(0.0, 0.0, 1.0)); // Perpendicular vectorSourcepub fn length_squared(&self) -> T
pub fn length_squared(&self) -> T
Computes the squared length (magnitude) of the vector.
The squared length is the dot product of the vector with itself, and it avoids the computation of the square root which is more computationally expensive. It is calculated as:
length_squared = self.x * self.x + self.y * self.y + self.z * self.z§Returns
Returns the squared length of the vector.
§Examples
let vec = Vec3::new(3.0, 4.0, 0.0);
let length_squared = vec.length_squared();
assert_eq!(length_squared, 25.0); // 3^2 + 4^2 = 25Sourcepub fn distance_squared(&self, other: &Self) -> T
pub fn distance_squared(&self, other: &Self) -> T
Computes the squared distance between this vector and another vector.
The squared distance is the squared length of the vector difference. It avoids the computation of the square root, which is more computationally expensive. It is calculated as:
distance_squared = (self.x - other.x) * (self.x - other.x) +
(self.y - other.y) * (self.y - other.y) +
(self.z - other.z) * (self.z - other.z)§Parameters
other: The vector to compute the distance to.
§Returns
Returns the squared distance between this vector and other.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(4.0, 5.0, 6.0);
let distance_squared = vec1.distance_squared(&vec2);
assert_eq!(distance_squared, 27.0); // (1-4)^2 + (2-5)^2 + (3-6)^2 = 27Sourcepub fn transform_mat3(&self, transform: &Mat3<T>) -> Self
pub fn transform_mat3(&self, transform: &Mat3<T>) -> Self
Transforms the vector using a 3x3 matrix.
This function applies a 3x3 matrix transformation to the vector. It is commonly used for linear transformations such as rotation, scaling, and shearing. The transformation is applied using the formula:
x' = transform.0.x * self.x + transform.1.x * self.y + transform.2.x * self.z
y' = transform.0.y * self.x + transform.1.y * self.y + transform.2.y * self.z
z' = transform.0.z * self.x + transform.1.z * self.y + transform.2.z * self.z§Parameters
transform: The 3x3 matrix to apply to the vector.
§Returns
Returns a new Vec3 instance that is the result of the transformation.
§Examples
// Define a 3x3 matrix and a vector
let transform = Mat3::new(
Vec3::new(1.0, 0.0, 0.0),
Vec3::new(0.0, 1.0, 0.0),
Vec3::new(0.0, 0.0, 1.0)
);
let vec = Vec3::new(1.0, 2.0, 3.0);
let transformed_vec = vec.transform_mat3(&transform);
assert_eq!(transformed_vec, vec); // Identity matrix, so result is the same as original vectorSourcepub fn transform_mat4(&self, transform: &Mat4<T>) -> Self
pub fn transform_mat4(&self, transform: &Mat4<T>) -> Self
Transforms the vector using a 4x4 matrix.
This function applies a 4x4 matrix transformation to the vector, treating the vector as if it were in homogeneous coordinates. This is useful for more complex transformations that involve translations. The transformation is applied using the formula:
x' = transform.0.x * self.x + transform.1.x * self.y + transform.2.x * self.z + transform.3.x
y' = transform.0.y * self.x + transform.1.y * self.y + transform.2.y * self.z + transform.3.y
z' = transform.0.z * self.x + transform.1.z * self.y + transform.2.z * self.z + transform.3.z§Parameters
transform: The 4x4 matrix to apply to the vector.
§Returns
Returns a new Vec3 instance that is the result of the transformation.
§Examples
// Define a 4x4 matrix and a vector
let transform = Mat4::new(
Vec4::new(1.0, 0.0, 0.0, 0.0),
Vec4::new(0.0, 1.0, 0.0, 0.0),
Vec4::new(0.0, 0.0, 1.0, 0.0),
Vec4::new(2.0, 3.0, 4.0, 1.0)
);
let vec = Vec3::new(1.0, 2.0, 3.0);
let transformed_vec = vec.transform_mat4(&transform);
assert_eq!(transformed_vec, Vec3::new(3.0, 5.0, 7.0)); // Transformed by matrixSource§impl<T> Vec3<T>
impl<T> Vec3<T>
Sourcepub fn min(&self, other: &Self) -> Self
pub fn min(&self, other: &Self) -> Self
Computes the component-wise minimum between this vector and another vector.
For each component (x, y, z), the resulting vector contains the smaller of the corresponding
components from this vector and other. This function is useful for finding the smallest
values between two vectors.
§Parameters
other: The vector to compare with.
§Returns
Returns a new Vec3 instance where each component is the minimum of the corresponding components
of self and other.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(4.0, 1.0, 2.0);
let min_vec = vec1.min(&vec2);
assert_eq!(min_vec, Vec3::new(1.0, 1.0, 2.0)); // Component-wise minimumSourcepub fn max(&self, other: &Self) -> Self
pub fn max(&self, other: &Self) -> Self
Computes the component-wise maximum between this vector and another vector.
For each component (x, y, z), the resulting vector contains the larger of the corresponding
components from this vector and other. This function is useful for finding the largest
values between two vectors.
§Parameters
other: The vector to compare with.
§Returns
Returns a new Vec3 instance where each component is the maximum of the corresponding components
of self and other.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(4.0, 1.0, 2.0);
let max_vec = vec1.max(&vec2);
assert_eq!(max_vec, Vec3::new(4.0, 2.0, 3.0)); // Component-wise maximumSourcepub fn clamp(&self, min: &Self, max: &Self) -> Self
pub fn clamp(&self, min: &Self, max: &Self) -> Self
Clamps the components of this vector between the corresponding components of two other vectors.
For each component (x, y, z), the resulting vector will have each component clamped to be within
the range specified by min and max. If a component is less than the corresponding min value,
it is set to min. If it is greater than the corresponding max value, it is set to max.
§Parameters
min: The vector specifying the minimum values for clamping.max: The vector specifying the maximum values for clamping.
§Returns
Returns a new Vec3 instance where each component is clamped to the range specified by the corresponding
components of min and max.
§Examples
let vec = Vec3::new(5.0, -1.0, 10.0);
let min_vec = Vec3::new(0.0, 0.0, 0.0);
let max_vec = Vec3::new(3.0, 3.0, 8.0);
let clamped_vec = vec.clamp(&min_vec, &max_vec);
assert_eq!(clamped_vec, Vec3::new(3.0, 0.0, 8.0)); // Clamped valuesSource§impl<T> Vec3<T>
impl<T> Vec3<T>
Sourcepub fn perpendicular(&self) -> Self
pub fn perpendicular(&self) -> Self
Computes a vector that is perpendicular to this vector.
This method finds a vector that is orthogonal to the current vector by identifying the axis with the smallest absolute component and constructing a perpendicular vector relative to that axis. The resulting perpendicular vector is calculated using the cross product with one of the cardinal axes (x, y, or z) based on which axis has the smallest absolute value.
The algorithm works as follows:
- Find the axis with the smallest absolute value in the current vector.
- Create a unit vector along this axis (cardinal axis).
- Compute the cross product between the original vector and the cardinal axis vector.
This method assumes that the input vector is not zero and will work correctly in 3D space to provide a perpendicular vector.
§Returns
Returns a new Vec3 instance that is perpendicular to self.
§Examples
let vec = Vec3::new(1.0, 2.0, 3.0);
let perpendicular_vec = vec.perpendicular();
assert_eq!(perpendicular_vec, Vec3::new(-3.0, 1.0, 0.0).normalize()); // A vector orthogonal to (1, 2, 3)§Panics
Panics if self is the zero vector, as no perpendicular vector can be determined.
Source§impl<T> Vec3<T>
impl<T> Vec3<T>
Sourcepub fn length(&self) -> T
pub fn length(&self) -> T
Computes the length (magnitude) of this vector.
The length is computed as the square root of the sum of the squares of its components.
§Returns
Returns the length of the vector as a value of type T.
§Examples
let vec = Vec3::new(3.0, 4.0, 0.0);
let len = vec.length();
assert_eq!(len, 5.0); // Length of vector (3, 4, 0) is 5Sourcepub fn normalize(&self) -> Option<Self>
pub fn normalize(&self) -> Option<Self>
Normalizes the vector.
Normalizing scales the vector to have a length of 1. If the vector is zero-length,
normalization is not possible, and None is returned.
§Returns
Returns an Option<Self> where Some(Self) is the normalized vector and None is
returned if the vector’s length is zero.
§Examples
let vec = Vec3::new(3.0, 4.0, 0.0);
if let Some(norm_vec) = vec.normalize() {
assert_eq!(norm_vec, Vec3::new(0.6, 0.8, 0.0)); // Normalized vector
}Sourcepub fn distance(&self, other: &Self) -> T
pub fn distance(&self, other: &Self) -> T
Computes the distance between this vector and another vector.
The distance is computed as the length of the vector difference between self and other.
§Parameters
other: The vector to calculate the distance to.
§Returns
Returns the distance between the vectors as a value of type T.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(4.0, 5.0, 6.0);
let dist = vec1.distance(&vec2);
assert_eq!(dist, 5.196152422706632); // Distance between (1, 2, 3) and (4, 5, 6)Sourcepub fn direction(&self, other: &Self) -> Option<Self>
pub fn direction(&self, other: &Self) -> Option<Self>
Computes the direction from this vector to another vector.
The direction is calculated as the normalized vector difference between other and self.
Returns None if the resulting vector has zero length.
§Parameters
other: The target vector to find the direction towards.
§Returns
Returns an Option<Self> where Some(Self) is the direction vector if non-zero, and
None if the direction vector is zero.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(4.0, 5.0, 6.0);
if let Some(dir) = vec1.direction(&vec2) {
assert_eq!(dir, Vec3::new(0.57735027, 0.57735027, 0.57735027)); // Direction vector
}Sourcepub fn angle(&self, other: &Self) -> T
pub fn angle(&self, other: &Self) -> T
Computes the angle between this vector and another vector.
The angle is calculated using the cross product and dot product of the two vectors. It returns the angle in radians between the vectors.
§Parameters
other: The vector to find the angle with respect to.
§Returns
Returns the angle between the vectors in radians.
§Examples
let vec1 = Vec3::new(1.0, 0.0, 0.0);
let vec2 = Vec3::new(0.0, 1.0, 0.0);
let angle = vec1.angle(&vec2);
assert_eq!(angle, 1.5707963267948966); // Angle between (1, 0, 0) and (0, 1, 0) is π/2Sourcepub fn project(&self, other: &Self) -> Self
pub fn project(&self, other: &Self) -> Self
Projects this vector onto another vector.
The projection of self onto other is computed as the scalar projection multiplied by the
other vector.
§Parameters
other: The vector to project onto.
§Returns
Returns the projection of self onto other as a new Vec3 instance.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(0.0, 1.0, 0.0);
let proj = vec1.project(&vec2);
assert_eq!(proj, Vec3::new(0.0, 2.0, 0.0)); // Projection of (1, 2, 3) onto (0, 1, 0)Sourcepub fn reject(&self, other: &Self) -> Self
pub fn reject(&self, other: &Self) -> Self
Rejects the component of this vector in the direction of another vector.
The rejection of self from other is the component of self orthogonal to other.
§Parameters
other: The vector to reject from.
§Returns
Returns the rejection of self from other as a new Vec3 instance.
§Examples
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(0.0, 1.0, 0.0);
let rej = vec1.reject(&vec2);
assert_eq!(rej, Vec3::new(1.0, 1.0, 3.0)); // Rejection of (1, 2, 3) from (0, 1, 0)Sourcepub fn ortho_normalize(&mut self, other: &mut Self)
pub fn ortho_normalize(&mut self, other: &mut Self)
Orthonormalizes self and other using the Gram-Schmidt process.
This process produces two orthonormal vectors where self is the first and other is the second.
§Parameters
other: The vector to orthonormalize withself.
§Examples
let mut vec1 = Vec3::new(1.0, 0.0, 0.0);
let mut vec2 = Vec3::new(0.0, 1.0, 1.0);
vec1.ortho_normalize(&mut vec2);
assert_eq!(vec1, Vec3::new(1.0, 0.0, 0.0)); // Orthonormalized vector1
assert_eq!(vec2, Vec3::new(0.0, 0.70710677, 0.70710677)); // Orthonormalized vector2Sourcepub fn rotate_by_axis(&self, axis: Self, angle: T) -> Self
pub fn rotate_by_axis(&self, axis: Self, angle: T) -> Self
Rotates this vector around a given axis by a specified angle.
The rotation is performed using Rodrigues’ rotation formula. The axis vector must be
normalized for the rotation to be correct.
§Parameters
axis: The axis to rotate around. It will be normalized if not already.angle: The angle of rotation in radians.
§Returns
Returns a new vector that is the result of rotating self around axis by angle.
§Examples
let vec = Vec3::new(1.0, 0.0, 0.0);
let axis = Vec3::new(0.0, 1.0, 0.0);
let rotated_vec = vec.rotate_by_axis(axis, std::f64::consts::PI / 2.0);
assert_eq!(rotated_vec, Vec3::new(0.0, 0.0, -1.0)); // Rotated vectorSourcepub fn reflect(&self, normal: &Self) -> Self
pub fn reflect(&self, normal: &Self) -> Self
Reflects this vector across a given normal vector.
The reflection is computed using the formula self - 2 * (self . normal) * normal.
§Parameters
normal: The normal vector to reflect across.
§Returns
Returns the reflected vector as a new Vec3 instance.
§Examples
let vec = Vec3::new(1.0, 2.0, 3.0);
let normal = Vec3::new(0.0, 1.0, 0.0);
let reflected_vec = vec.reflect(&normal);
assert_eq!(reflected_vec, Vec3::new(1.0, -2.0, 3.0)); // Reflected vectorSourcepub fn lerp(&self, other: &Self, t: T) -> Self
pub fn lerp(&self, other: &Self, t: T) -> Self
Linearly interpolates between this vector and another vector.
The interpolation factor t determines the blend between self and other. When t is
0.0, the result is self, and when t is 1.0, the result is other.
§Parameters
other: The target vector to interpolate towards.t: The interpolation factor, a value between 0 and 1.
§Returns
Returns the interpolated vector as a new Vec3 instance.
§Examples
let vec1 = Vec3::new(0.0, 0.0, 0.0);
let vec2 = Vec3::new(10.0, 10.0, 10.0);
let interpolated_vec = vec1.lerp(&vec2, 0.5);
assert_eq!(interpolated_vec, Vec3::new(5.0, 5.0, 5.0)); // Interpolated vectorSourcepub fn move_towards(&self, target: &Self, max_distance: T) -> Self
pub fn move_towards(&self, target: &Self, max_distance: T) -> Self
Moves this vector towards a target vector by a specified distance.
The movement is limited by max_distance, ensuring the vector does not overshoot
the target. If the target is within the distance, it is returned directly.
§Parameters
target: The target vector to move towards.max_distance: The maximum distance to move.
§Returns
Returns the new position after moving towards the target.
§Examples
let vec = Vec3::new(0.0, 0.0, 0.0);
let target = Vec3::new(10.0, 10.0, 10.0);
let new_vec = vec.move_towards(&target, 5.0);
assert_eq!(new_vec, Vec3::new(3.5355339, 3.5355339, 3.5355339)); // Moved vectorSourcepub fn recip(&self) -> Self
pub fn recip(&self) -> Self
Computes the reciprocal of each component of the vector.
This method returns a new vector where each component is the reciprocal of the
corresponding component in self.
§Returns
Returns a new Vec3 instance where each component is the reciprocal of the original.
§Examples
let vec = Vec3::new(1.0, 2.0, 4.0);
let recip_vec = vec.recip();
assert_eq!(recip_vec, Vec3::new(1.0, 0.5, 0.25)); // Reciprocal vectorTrait Implementations§
Source§impl<T> AddAssign for Vec3<T>
impl<T> AddAssign for Vec3<T>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+= operation. Read moreSource§impl<T> AddAssign<T> for Vec3<T>
impl<T> AddAssign<T> for Vec3<T>
Source§fn add_assign(&mut self, scalar: T)
fn add_assign(&mut self, scalar: T)
+= operation. Read moreimpl<T: Copy> Copy for Vec3<T>
Source§impl<T> DivAssign for Vec3<T>
impl<T> DivAssign for Vec3<T>
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
/= operation. Read moreSource§impl<T> DivAssign<T> for Vec3<T>
impl<T> DivAssign<T> for Vec3<T>
Source§fn div_assign(&mut self, scalar: T)
fn div_assign(&mut self, scalar: T)
/= operation. Read moreSource§impl<'a, T> IntoIterator for &'a Vec3<T>
impl<'a, T> IntoIterator for &'a Vec3<T>
Source§impl<'a, T> IntoIterator for &'a mut Vec3<T>
impl<'a, T> IntoIterator for &'a mut Vec3<T>
Source§impl<T> MulAssign for Vec3<T>
impl<T> MulAssign for Vec3<T>
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
*= operation. Read moreSource§impl<T> MulAssign<T> for Vec3<T>
impl<T> MulAssign<T> for Vec3<T>
Source§fn mul_assign(&mut self, scalar: T)
fn mul_assign(&mut self, scalar: T)
*= operation. Read more