Skip to main content

Matrix

Trait Matrix 

Source
pub trait Matrix {
Show 23 methods // Required methods fn m00(&self) -> f32; fn m01(&self) -> f32; fn m02(&self) -> f32; fn m03(&self) -> f32; fn m10(&self) -> f32; fn m11(&self) -> f32; fn m12(&self) -> f32; fn m13(&self) -> f32; fn m20(&self) -> f32; fn m21(&self) -> f32; fn m22(&self) -> f32; fn m23(&self) -> f32; fn m30(&self) -> f32; fn m31(&self) -> f32; fn m32(&self) -> f32; fn m33(&self) -> f32; fn identity() -> Self; // Provided methods fn rotation_matrix(&self) -> Matrix3<f64> { ... } fn extract_scales(matrix: Matrix3<f64>) -> (f64, f64, f64) { ... } fn apply_scales( matrix: Matrix3<f64>, scales: (f64, f64, f64), ) -> Matrix3<f64> { ... } fn normalize_rotation_matrix( matrix: Matrix3<f64>, scales: (f64, f64, f64), ) -> Matrix3<f64> { ... } fn rotation_matrix_to_euler_angles( matrix: Matrix3<f64>, degrees: bool, ) -> (f64, f64, f64) { ... } fn euler_angles_to_rotation_matrix( angles: (f64, f64, f64), degrees: bool, ) -> Matrix3<f64> { ... }
}
Expand description

Common operations for transformation matrices.

This trait abstracts behavior shared between Transform3x4 and Transform4x4, providing matrix element access and transformation utilities.

§Provided Methods

  • Element Access: m00() through m33() - Access individual matrix elements
  • Rotation: rotation_matrix() - Extract 3x3 rotation submatrix
  • Scaling: extract_scales(), apply_scales(), normalize_rotation_matrix()
  • Euler Angles: rotation_matrix_to_euler_angles(), euler_angles_to_rotation_matrix()
  • Identity: identity() - Create identity transform

§Rotation Order

Euler angle conversions use ‘xyz’ extrinsic rotation order (roll-pitch-yaw).

§Example

use rpfm_lib::files::bmd::common::{Transform4x4, Matrix};

let transform = Transform4x4::identity();

// Extract rotation
let rotation = transform.rotation_matrix();
let scales = Transform4x4::extract_scales(rotation);

// Convert to Euler angles (in degrees)
let (x, y, z) = Transform4x4::rotation_matrix_to_euler_angles(rotation, true);
println!("Rotation: X={}, Y={}, Z={}", x, y, z);

Required Methods§

Source

fn m00(&self) -> f32

Returns matrix element at row 0, column 0.

Source

fn m01(&self) -> f32

Returns matrix element at row 0, column 1.

Source

fn m02(&self) -> f32

Returns matrix element at row 0, column 2.

Source

fn m03(&self) -> f32

Returns matrix element at row 0, column 3 (0.0 for 3x4 matrices).

Source

fn m10(&self) -> f32

Returns matrix element at row 1, column 0.

Source

fn m11(&self) -> f32

Returns matrix element at row 1, column 1.

Source

fn m12(&self) -> f32

Returns matrix element at row 1, column 2.

Source

fn m13(&self) -> f32

Returns matrix element at row 1, column 3 (0.0 for 3x4 matrices).

Source

fn m20(&self) -> f32

Returns matrix element at row 2, column 0.

Source

fn m21(&self) -> f32

Returns matrix element at row 2, column 1.

Source

fn m22(&self) -> f32

Returns matrix element at row 2, column 2.

Source

fn m23(&self) -> f32

Returns matrix element at row 2, column 3 (0.0 for 3x4 matrices).

Source

fn m30(&self) -> f32

Returns matrix element at row 3, column 0 (translation X).

Source

fn m31(&self) -> f32

Returns matrix element at row 3, column 1 (translation Y).

Source

fn m32(&self) -> f32

Returns matrix element at row 3, column 2 (translation Z).

Source

fn m33(&self) -> f32

Returns matrix element at row 3, column 3 (1.0 for 3x4 matrices).

Source

fn identity() -> Self

Creates an identity transformation matrix.

§Returns

Identity matrix (no rotation, no translation, unit scale)

Provided Methods§

Source

fn rotation_matrix(&self) -> Matrix3<f64>

Extracts the 3x3 rotation submatrix.

Converts from CA’s column-major serialization to standard row-major rotation matrix representation.

§Returns

3x3 rotation matrix as nalgebra Matrix3<f64>.

§Reference

See: https://developer.unigine.com/forum/uploads/monthly_2020_05/image.png.674c8b961433f2a7a62c54bc55cb599c.png

Source

fn extract_scales(matrix: Matrix3<f64>) -> (f64, f64, f64)

Extracts scale factors from a rotation matrix.

Computes the scale of each axis by taking the norm of each column vector.

§Parameters
  • matrix: 3x3 rotation/scale matrix
§Returns

Tuple of (scale_x, scale_y, scale_z)

§Note

Does not support negative scales. Negative scales will be treated as positive.

§Reference

See: https://math.stackexchange.com/a/1463487

Source

fn apply_scales(matrix: Matrix3<f64>, scales: (f64, f64, f64)) -> Matrix3<f64>

Applies scale factors to a rotation matrix.

Scales each column of the matrix by the corresponding scale factor.

§Parameters
  • matrix: 3x3 rotation matrix (should be normalized)
  • scales: Tuple of (scale_x, scale_y, scale_z)
§Returns

Scaled rotation matrix

Source

fn normalize_rotation_matrix( matrix: Matrix3<f64>, scales: (f64, f64, f64), ) -> Matrix3<f64>

Normalizes a rotation matrix by removing scale factors.

Divides each column by the corresponding scale factor to produce a pure rotation matrix.

§Parameters
  • matrix: 3x3 rotation/scale matrix
  • scales: Tuple of (scale_x, scale_y, scale_z) to remove
§Returns

Normalized rotation matrix (orthonormal)

Source

fn rotation_matrix_to_euler_angles( matrix: Matrix3<f64>, degrees: bool, ) -> (f64, f64, f64)

Converts a rotation matrix to Euler angles.

Uses ‘xyz’ extrinsic rotation order (roll-pitch-yaw).

§Parameters
  • matrix: 3x3 rotation matrix
  • degrees: If true, return angles in degrees; if false, in radians
§Returns

Tuple of (x_rotation, y_rotation, z_rotation) in specified units

§Example (Python equivalent using scipy)
from scipy.spatial.transform import Rotation as R
r = R.from_euler("xyz", [-130.0, 80.0, -30.0], degrees=True)
m = r.as_matrix()
r = R.from_matrix(m)
angles = r.as_euler("xyz", degrees=True)
Source

fn euler_angles_to_rotation_matrix( angles: (f64, f64, f64), degrees: bool, ) -> Matrix3<f64>

Converts Euler angles to a rotation matrix.

Uses ‘xyz’ extrinsic rotation order (roll-pitch-yaw).

§Parameters
  • angles: Tuple of (x_rotation, y_rotation, z_rotation)
  • degrees: If true, angles are in degrees; if false, in radians
§Returns

3x3 rotation matrix with values near zero cleaned up (< 1e-5 set to 0.0)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§