Skip to main content

ear_algae/homogeneous/
mod.rs

1use culit::culit;
2
3use crate::{Mat, Nrml, Vect, op_wrapper::scs, traits::*};
4
5mod vect3;
6
7impl<S: Field> Vect<4, S> {
8    #[culit]
9    pub fn point(position: Vect<3, S>) -> Self {
10        Vect([position[0], position[1], position[2], 1S])
11    }
12
13    #[culit]
14    pub fn point_at_inf(direction: Nrml<3, S>) -> Self {
15        Vect([direction[0], direction[1], direction[2], 0S])
16    }
17}
18
19impl<S: Field> Mat<4, 4, S> {
20    #[culit]
21    pub fn affine(linear: Mat<3, 3, S>, translation: Vect<3, S>) -> Self {
22        let [[xx, xy, xz], [yx, yy, yz], [zx, zy, zz]] = linear.0;
23        let [x, y, z] = translation.0;
24        Mat([
25            [xx, xy, xz, x],
26            [yx, yy, yz, y],
27            [zx, zy, zz, z],
28            [0S, 0S, 0S, 1S],
29        ])
30    }
31
32    #[culit]
33    pub fn linear(linear: Mat<3, 3, S>) -> Self {
34        let [[xx, xy, xz], [yx, yy, yz], [zx, zy, zz]] = linear.0;
35        Mat([
36            [xx, xy, xz, 0S],
37            [yx, yy, yz, 0S],
38            [zx, zy, zz, 0S],
39            [0S, 0S, 0S, 1S],
40        ])
41    }
42
43    #[culit]
44    pub fn translation(translation: Vect<3, S>) -> Self {
45        let [x, y, z] = translation.0;
46        Mat([
47            [1S, 0S, 0S, x],
48            [0S, 1S, 0S, y],
49            [0S, 0S, 1S, z],
50            [0S, 0S, 0S, 1S],
51        ])
52    }
53
54    #[culit]
55    pub fn perspective_projection(aspect: S, fov: S, n: S, f: S) -> Self {
56        scs!(aspect, fov, f, n);
57        let view = (fov * 0.5Sc).tan();
58
59        Mat::from_scs([
60            [1Sc / (aspect * view), 0Sc, 0Sc, 0Sc],
61            [0Sc, 1Sc / view, 0Sc, 0Sc],
62            [0Sc, 0Sc, ((f + n) / (n - f) - 1Sc) / 2Sc, f * n / (n - f)],
63            [0Sc, 0Sc, -1Sc, 0Sc],
64        ])
65    }
66
67    #[culit]
68    pub fn uniform_scale(scale: S) -> Self {
69        Mat([
70            [scale, 0S, 0S, 0S],
71            [0S, scale, 0S, 0S],
72            [0S, 0S, scale, 0S],
73            [0S, 0S, 0S, 1S],
74        ])
75    }
76}