pub struct Matrix4x4 { /* private fields */ }Expand description
Represents a 4x4 linear_algebra with f32 values in row-major order.
Internal representation is [[f32; 4]; 4].
Implementations§
Source§impl Matrix4x4
impl Matrix4x4
Sourcepub fn new(data: [[f32; 4]; 4]) -> Self
pub fn new(data: [[f32; 4]; 4]) -> Self
Creates a new 4x4 linear_algebra from the provided 2D array data.
The data should be in row-major order: data[row][column].
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let linear_algebra = Matrix4x4::new([
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0],
]);
assert_eq!(linear_algebra.get(0, 1), 2.0);Sourcepub fn identity() -> Self
pub fn identity() -> Self
Creates an identity linear_algebra (1.0 on the diagonal, 0.0 elsewhere).
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let identity = Matrix4x4::identity();
assert_eq!(identity.get(0, 0), 1.0);
assert_eq!(identity.get(1, 1), 1.0);
assert_eq!(identity.get(0, 1), 0.0);Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a zero linear_algebra (all elements are 0.0).
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let zero = Matrix4x4::zero();
assert_eq!(zero.get(2, 3), 0.0);Sourcepub fn get(&self, row: usize, col: usize) -> f32
pub fn get(&self, row: usize, col: usize) -> f32
Gets the value at the specified row and column using a method call.
Prefer using the index operator linear_algebra[(row, col)] for more idiomatic access.
§Panics
Panics if row or col are out of bounds (>= 4).
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let linear_algebra = Matrix4x4::identity();
assert_eq!(linear_algebra.get(1, 1), 1.0);Sourcepub fn set(&mut self, row: usize, col: usize, value: f32)
pub fn set(&mut self, row: usize, col: usize, value: f32)
Sets the value at the specified row and column using a method call.
Prefer using the mutable index operator linear_algebra[(row, col)] = value
for more idiomatic modification.
§Panics
Panics if row or col are out of bounds (>= 4).
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let mut linear_algebra = Matrix4x4::zero();
linear_algebra.set(2, 3, 5.0);
assert_eq!(linear_algebra.get(2, 3), 5.0);Sourcepub fn transpose(&self) -> Self
pub fn transpose(&self) -> Self
Returns the transpose of the linear_algebra.
The element at (row, col) in the original linear_algebra becomes
the element at (col, row) in the transposed linear_algebra.
Does not modify the original linear_algebra.
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let linear_algebra = Matrix4x4::new([
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0],
]);
let transposed = linear_algebra.transpose();
assert_eq!(transposed.get(0, 1), 5.0); // linear_algebra.get(1, 0)
assert_eq!(transposed.get(1, 0), 2.0); // linear_algebra.get(0, 1)
assert_eq!(transposed.get(3, 2), 12.0); // linear_algebra.get(2, 3)Sourcepub fn multiply(&self, other: &Self) -> Self
pub fn multiply(&self, other: &Self) -> Self
Performs linear_algebra multiplication: self * other.
This method delegates to the * operator implementation provided by Mul.
Does not modify the original matrices.
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let a = Matrix4x4::new([
[1.0, 2.0, 0.0, 0.0],
[3.0, 4.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]);
let b = Matrix4x4::new([
[5.0, 6.0, 0.0, 0.0],
[7.0, 8.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
]);
let result = a.multiply(&b);
// 1*5 + 2*7 = 19
assert_eq!(result.get(0, 0), 19.0);
// 1*6 + 2*8 = 22
assert_eq!(result.get(0, 1), 22.0);
// 3*5 + 4*7 = 15 + 28 = 43
assert_eq!(result.get(1, 0), 43.0);
// 3*6 + 4*8 = 18 + 32 = 50
assert_eq!(result.get(1, 1), 50.0);pub fn is_almost_zero(&self) -> bool
Sourcepub fn affine_transformation(
rotation: &Matrix3x3,
translation: &Vector3,
) -> Self
pub fn affine_transformation( rotation: &Matrix3x3, translation: &Vector3, ) -> Self
Creates an affine transformation (rotation and translation) in 3D as 4x4 linear_algebra
§Example
use bb_geometry::linear_algebra::matrix4x4::*;
use bb_geometry::linear_algebra::matrix3x3::*;
use bb_geometry::linear_algebra::vector3::*;
use bb_geometry::linear_algebra::vector4::*;
let r = Matrix3x3::rotation_from_euler_angles(0.2, 0.4, -0.2);
let a = Vector3::new([5.0, 4.0, 3.0]);
let affine_transformation = Matrix4x4::affine_transformation(&r, &a);
let inverse_affine_transformation = Matrix4x4::affine_transformation(&(r.transpose()), &-(r.transpose() * a));
let should_be_zero_matrix = inverse_affine_transformation * affine_transformation - Matrix4x4::identity();
assert!(should_be_zero_matrix.is_almost_zero());Sourcepub fn axes_swap(dir1: usize, dir2: usize) -> Self
pub fn axes_swap(dir1: usize, dir2: usize) -> Self
Swaps basis directions
§Example
use bb_geometry::linear_algebra::matrix4x4::Matrix4x4;
use bb_geometry::linear_algebra::vector4::{E_W, E_X, E_Y, E_Z};
let swap_matrix = Matrix4x4::axes_swap(2, 3);
let transformed_e_x = swap_matrix * E_X;
let transformed_e_y = swap_matrix * E_Y;
let transformed_e_z = swap_matrix * E_Z;
let transformed_e_w = swap_matrix * E_W;
assert!((transformed_e_x - E_X).is_almost_zero());
assert!((transformed_e_y - E_Y).is_almost_zero());
assert!((transformed_e_z - E_W).is_almost_zero());
assert!((transformed_e_w - E_Z).is_almost_zero());Trait Implementations§
Source§impl Index<(usize, usize)> for Matrix4x4
Allows accessing linear_algebra elements using tuple indexing linear_algebra[(row, col)].
impl Index<(usize, usize)> for Matrix4x4
Allows accessing linear_algebra elements using tuple indexing linear_algebra[(row, col)].
Source§impl IndexMut<(usize, usize)> for Matrix4x4
Allows mutating linear_algebra elements using tuple indexing linear_algebra[(row, col)] = value.
impl IndexMut<(usize, usize)> for Matrix4x4
Allows mutating linear_algebra elements using tuple indexing linear_algebra[(row, col)] = value.
Source§impl Mul<&Matrix4x4> for &Matrix4x4
Implements linear_algebra multiplication using the * operator (&Matrix4x4 * &Matrix4x4).
impl Mul<&Matrix4x4> for &Matrix4x4
Implements linear_algebra multiplication using the * operator (&Matrix4x4 * &Matrix4x4).
This is often the most convenient way to perform linear_algebra multiplication.
§Example
use crate::bb_geometry::linear_algebra::matrix4x4::*;
let a = Matrix4x4::identity();
let b = Matrix4x4::new([
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0],
[13.0, 14.0, 15.0, 16.0],
]);
let result = &a * &b; // Multiplying by identity returns the original
assert_eq!(result, b);
let c = Matrix4x4::new([
[2.0, 0.0, 0.0, 0.0],
[0.0, 2.0, 0.0, 0.0],
[0.0, 0.0, 2.0, 0.0],
[0.0, 0.0, 0.0, 2.0],
]);
let scaled_b = &b * &c; // Scale b by 2
assert_eq!(scaled_b[(0, 0)], 2.0);
assert_eq!(scaled_b[(1, 1)], 12.0);Source§impl Mul<&Vector4> for &Matrix4x4
Implements Matrix * Vector multiplication (&Matrix4x4 * &Vector4).
Treats the vector as a column vector.
impl Mul<&Vector4> for &Matrix4x4
Implements Matrix * Vector multiplication (&Matrix4x4 * &Vector4).
Treats the vector as a column vector.