Parsing more stuff.
This commit is contained in:
parent
96a24fcc28
commit
8fcf815dbf
3 changed files with 253 additions and 17 deletions
|
|
@ -9,6 +9,11 @@ pub struct AffineTransform {
|
|||
}
|
||||
|
||||
impl AffineTransform {
|
||||
pub fn new(mat: [[Float; 4]; 3]) -> Option<Self> {
|
||||
let inv = Self::invert(mat)?;
|
||||
Some(Self { mat, inv })
|
||||
}
|
||||
|
||||
pub fn identity() -> Self {
|
||||
Self {
|
||||
mat: [
|
||||
|
|
@ -77,7 +82,7 @@ impl AffineTransform {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn look_at(pos: Pos3, look: Pos3, up: Dir3) -> Self {
|
||||
pub fn look_at(pos: Pos3, look: Pos3, up: Dir3) -> Option<Self> {
|
||||
let dir = (look - pos).normalize();
|
||||
let right = Dir3::cross(up.normalize(), dir).normalize();
|
||||
let new_up = Dir3::cross(dir, right);
|
||||
|
|
@ -88,14 +93,55 @@ impl AffineTransform {
|
|||
[right.z(), new_up.z(), dir.z(), pos.z()],
|
||||
];
|
||||
|
||||
let inv = Self::invert(mat);
|
||||
|
||||
Self { mat, inv }
|
||||
Self::new(mat)
|
||||
}
|
||||
|
||||
fn invert(mat: [[Float; 4]; 3]) -> [[Float; 3]; 3] {
|
||||
eprintln!("Matrix inversion not implemented");
|
||||
[[0.0; 3]; 3]
|
||||
fn invert(mat: [[Float; 4]; 3]) -> Option<[[Float; 3]; 3]> {
|
||||
// using cramers rule
|
||||
let adj = |y, x| {
|
||||
let cofactor_indices = |p| match p {
|
||||
0 => (1, 2),
|
||||
1 => (0, 2),
|
||||
2 => (0, 1),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let (y1, y2) = cofactor_indices(y);
|
||||
let (x1, x2) = cofactor_indices(x);
|
||||
|
||||
(if (x + y) % 2 == 0 { 1.0 } else { -1.0 })
|
||||
* (mat[y1][x1] * mat[y2][x2] - mat[y1][x2] * mat[y2][x1])
|
||||
};
|
||||
|
||||
let det = mat[0][0] * mat[1][1] * mat[2][2]
|
||||
+ mat[0][1] * mat[1][2] * mat[2][0]
|
||||
+ mat[0][2] * mat[1][0] * mat[2][1]
|
||||
- mat[0][2] * mat[1][1] * mat[2][0]
|
||||
- mat[0][1] * mat[1][0] * mat[2][2]
|
||||
- mat[0][0] * mat[1][2] * mat[2][1];
|
||||
|
||||
if det != 0.0 {
|
||||
let det_frac = 1.0 / det;
|
||||
|
||||
Some([
|
||||
[
|
||||
adj(0, 0) * det_frac,
|
||||
adj(1, 0) * det_frac,
|
||||
adj(2, 0) * det_frac,
|
||||
],
|
||||
[
|
||||
adj(0, 1) * det_frac,
|
||||
adj(1, 1) * det_frac,
|
||||
adj(2, 1) * det_frac,
|
||||
],
|
||||
[
|
||||
adj(0, 2) * det_frac,
|
||||
adj(1, 2) * det_frac,
|
||||
adj(2, 2) * det_frac,
|
||||
],
|
||||
])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transform_pos(&self, pos: Pos3) -> Pos3 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue