ndarray_cg
This crate provides math functionality for computer graphics based on ndarray
. The approach used in ndarray for computer graphics math is highly flexible and performant, even though there are many specialized crates focused on game development and computer graphics.
To use it within workspace, simply add it to the Cargo.toml
= { = true, = { }
Example: Trivial
use *;
// Will create the following matrix
// [ 1.0, 2.0,
// 3.0, 4.0,
// 5.0, 6.0 ]
let matrix = from_row_major;
// For computer graphics related matrices, you can use a shortcut:
// Will create the following matrix
// [ 1.0, 2.0,
// 3.0, 4.0 ]
let matrix = from_row_major;
// You can multiply two matrices
let rotated_matrix = rot * matrix;
// You can iterate over your matrix in different ways
let matrix = from_row_major;
for in rotated_matrix.iter_indexed_msfirst
for in rotated_matrix.lane_indexed_iter
Example: Adding
use *;
let mat_a = from_row_major
;
let mat_b = from_row_major
;
let mut mat_r = default;
// Perform addition
add;
let exp = from_row_major
;
assert_eq!;
// Operator overloading
let mat_r = &mat_a + &mat_b;
assert_eq!;
Example: Multiplication
use *;
use DescriptorOrderRowMajor;
let mat_a = from_row_major
;
let mat_b = from_row_major
;
let mut mat_r = default;
// Perform multiplication
mul;
let exp = from_row_major
;
assert_eq!;
// Operator overloading
let mat_r = &mat_a * &mat_b;
assert_eq!;
Example: Angle rotation
use *;
use PI;
let angle_radians = PI / 4.0;
let cos_theta = angle_radians.cos;
let sin_theta = angle_radians.sin;
let exp = from_row_major
;
let got = rot;
assert_eq!;
Example: Translation
use *;
let tx = 2.0;
let ty = 3.0;
let exp = from_row_major
;
let got = translate;
assert_eq!;
Example: Basic scaling
use *;
let sx = 2.0;
let sy = 3.0;
let exp = from_row_major
;
let got = scale;
assert_eq!;
Example: Reflecting
use *;
let exp = from_row_major
;
let got = reflect_x;
assert_eq!;
Example: Shearing
use *;
let shx = 1.0;
let shy = 0.5;
let exp = from_row_major
;
let got = shear;
assert_eq!;
Example: Line iteration
use *;
use DescriptorOrderRowMajor;
let mat = from_row_major;
let row_iter : = mat.lane_iter.collect;
let exp = vec!;
assert_eq!;