Matrix4x4

Struct Matrix4x4 

Source
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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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)
Source

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);
Source

pub fn is_almost_zero(&self) -> bool

Source

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());
Source

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 Clone for Matrix4x4

Source§

fn clone(&self) -> Matrix4x4

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 Matrix4x4

Source§

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

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

impl Index<(usize, usize)> for Matrix4x4

Allows accessing linear_algebra elements using tuple indexing linear_algebra[(row, col)].

§Panics

Panics if row or col are out of bounds (>= 4).

§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[(1, 2)], 7.0);
Source§

type Output = f32

The returned type after indexing.
Source§

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

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

impl IndexMut<(usize, usize)> for Matrix4x4

Allows mutating linear_algebra elements using tuple indexing linear_algebra[(row, col)] = value.

§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[(2, 1)] = 99.0;
assert_eq!(linear_algebra[(2, 1)], 99.0);
Source§

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

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

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§

type Output = Matrix4x4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Matrix4x4> for Matrix4x4

Source§

type Output = Matrix4x4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Vector4> for &Matrix4x4

Implements Matrix * Vector multiplication (&Matrix4x4 * &Vector4). Treats the vector as a column vector.

Source§

type Output = Vector4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Vector4> for Matrix4x4

Source§

type Output = Vector4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Matrix4x4> for &Matrix4x4

Source§

type Output = Matrix4x4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Vector4> for &Matrix4x4

Source§

type Output = Vector4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Vector4> for Matrix4x4

Source§

type Output = Vector4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul for Matrix4x4

Source§

type Output = Matrix4x4

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl PartialEq for Matrix4x4

Source§

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

Source§

type Output = Matrix4x4

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<&Matrix4x4> for Matrix4x4

Source§

type Output = Matrix4x4

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<Matrix4x4> for &Matrix4x4

Source§

type Output = Matrix4x4

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for Matrix4x4

Source§

type Output = Matrix4x4

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Copy for Matrix4x4

Source§

impl StructuralPartialEq for Matrix4x4

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