Parsing more stuff.

This commit is contained in:
hal8174 2025-08-02 22:44:06 +02:00
parent 96a24fcc28
commit 8fcf815dbf
Signed by: hal8174
SSH key fingerprint: SHA256:NN98ZYwnrreQLSOV/g+amY7C3yL/mS1heD7bi5t6PPw
3 changed files with 253 additions and 17 deletions

View file

@ -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 {