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 d2;
// Will create the following matrix
// [ 1.0, 2.0,
// 3.0, 4.0,
// 5.0, 6.0 ]
let matrix = Mat 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 = Mat from_row_major;
for in rotated_matrix.iter_indexed_msfirst
for in rotated_matrix.lane_indexed_iter
Example: Adding
use
;
let mat_a = zero.raw_set
;
let mat_b = zero.raw_set
;
let mut mat_r = zero;
// Perform addition
add;
let exp = zero.raw_set
;
assert_eq!;
// Operator overloading
let mat_r = &mat_a + &mat_b;
assert_eq!;
Example: Multiplication
use
;
let mat_a = zero.raw_set
;
let mat_b = zero.raw_set
;
let mut mat_r = zero;
// Perform multiplication
mul;
let exp = zero.raw_set
;
assert_eq!;
// Operator overloading
let mat_r = &mat_a * &mat_b;
assert_eq!;
Example: Angle rotation
use RawSlice;
let angle_radians = PI / 4.0;
let cos_theta = angle_radians.cos;
let sin_theta = angle_radians.sin;
let exp : =
;
let got = rot;
assert_eq!;
Example: Translation
use RawSliceMut;
let tx = 2.0;
let ty = 3.0;
let exp = zero.raw_set
;
let got = translate;
assert_eq!;
Example: Basic scaling
use RawSlice;
let sx = 2.0;
let sy = 3.0;
let exp = zero.raw_set
;
let got = scale;
assert_eq!;
Example: Reflecting
use RawSliceMut;
let exp = zero.raw_set
;
let got = reflect_x;
assert_eq!;
Example: Shearing
use RawSlice;
let shx = 1.0;
let shy = 0.5;
let exp = zero.raw_set
;
let got = shear;
assert_eq!;
Example: Line iteration
use
;
let mat = zero.raw_set;
let row_iter : = mat.lane_iter.collect;
let exp = vec!;
assert_eq!;