-
Notifications
You must be signed in to change notification settings - Fork 5
/
glam_ext.rs
36 lines (30 loc) · 824 Bytes
/
glam_ext.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use glam::{Mat3, Mat4};
pub trait Mat4Ext {
fn minor(&self, i: usize, j: usize) -> Mat3;
fn cofactor(&self, i: usize, j: usize) -> f32;
}
impl Mat4Ext for Mat4 {
fn minor(&self, i: usize, j: usize) -> Mat3 {
let mut minor = Mat3::ZERO;
let mut yy = 0;
for y in 0..4 {
if y == j {
continue;
}
let mut xx = 0;
for x in 0..4 {
if x == i {
continue;
}
minor.col_mut(xx)[yy] = self.col(x)[y];
xx += 1;
}
yy += 1;
}
minor
}
fn cofactor(&self, i: usize, j: usize) -> f32 {
let minor = self.minor(i, j);
i32::pow(-1, (i + 1 + j + 1) as u32) as f32 * minor.determinant()
}
}