Mercurial > touhou
comparison utils/src/math.rs @ 789:b5bca9274335
utils: Make some math functions const
Sadly f32::sqrt() isn’t const, so we can’t make normalize() const nor any
function which depends on it.
| author | Link Mauve <linkmauve@linkmauve.fr> |
|---|---|
| date | Sun, 04 Jan 2026 11:49:03 +0100 |
| parents | 21b186be2590 |
| children | d005f5927447 |
comparison
equal
deleted
inserted
replaced
| 788:f56b10812b77 | 789:b5bca9274335 |
|---|---|
| 5 inner: [[f32; 4]; 4] | 5 inner: [[f32; 4]; 4] |
| 6 } | 6 } |
| 7 | 7 |
| 8 impl Mat4 { | 8 impl Mat4 { |
| 9 /// Create a new matrix from a set of 16 f32. | 9 /// Create a new matrix from a set of 16 f32. |
| 10 pub fn new(inner: [[f32; 4]; 4]) -> Mat4 { | 10 pub const fn new(inner: [[f32; 4]; 4]) -> Mat4 { |
| 11 Mat4 { | 11 Mat4 { |
| 12 inner | 12 inner |
| 13 } | 13 } |
| 14 } | 14 } |
| 15 | 15 |
| 16 fn zero() -> Mat4 { | 16 const fn zero() -> Mat4 { |
| 17 Mat4 { | 17 Mat4 { |
| 18 inner: [[0.; 4]; 4] | 18 inner: [[0.; 4]; 4] |
| 19 } | 19 } |
| 20 } | 20 } |
| 21 | 21 |
| 22 fn identity() -> Mat4 { | 22 const fn identity() -> Mat4 { |
| 23 Mat4 { | 23 Mat4 { |
| 24 inner: [[1., 0., 0., 0.], | 24 inner: [[1., 0., 0., 0.], |
| 25 [0., 1., 0., 0.], | 25 [0., 1., 0., 0.], |
| 26 [0., 0., 1., 0.], | 26 [0., 0., 1., 0.], |
| 27 [0., 0., 0., 1.]] | 27 [0., 0., 0., 1.]] |
| 28 } | 28 } |
| 29 } | 29 } |
| 30 | 30 |
| 31 /// Immutably borrow the array of f32 inside this matrix. | 31 /// Immutably borrow the array of f32 inside this matrix. |
| 32 pub fn borrow_inner(&self) -> &[[f32; 4]; 4] { | 32 pub const fn borrow_inner(&self) -> &[[f32; 4]; 4] { |
| 33 &self.inner | 33 &self.inner |
| 34 } | 34 } |
| 35 | 35 |
| 36 /// Scale the matrix in 2D. | 36 /// Scale the matrix in 2D. |
| 37 pub fn scale2d(&mut self, x: f32, y: f32) { | 37 pub fn scale2d(&mut self, x: f32, y: f32) { |
| 127 tmp | 127 tmp |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 /// Create an orthographic projection matrix. | 131 /// Create an orthographic projection matrix. |
| 132 pub fn ortho_2d(left: f32, right: f32, bottom: f32, top: f32) -> Mat4 { | 132 pub const fn ortho_2d(left: f32, right: f32, bottom: f32, top: f32) -> Mat4 { |
| 133 let mut mat = Mat4::identity(); | 133 let mut mat = Mat4::identity(); |
| 134 mat.inner[0][0] = 2. / (right - left); | 134 mat.inner[0][0] = 2. / (right - left); |
| 135 mat.inner[1][1] = 2. / (top - bottom); | 135 mat.inner[1][1] = 2. / (top - bottom); |
| 136 mat.inner[2][2] = -1.; | 136 mat.inner[2][2] = -1.; |
| 137 mat.inner[3][0] = -(right + left) / (right - left); | 137 mat.inner[3][0] = -(right + left) / (right - left); |
| 178 [s[1], u[1], -f[1], 0.], | 178 [s[1], u[1], -f[1], 0.], |
| 179 [s[2], u[2], -f[2], 0.], | 179 [s[2], u[2], -f[2], 0.], |
| 180 [-dot(s, eye), -dot(u, eye), dot(f, eye), 1.]]) | 180 [-dot(s, eye), -dot(u, eye), dot(f, eye), 1.]]) |
| 181 } | 181 } |
| 182 | 182 |
| 183 fn sub(a: Vec3, b: Vec3) -> Vec3 { | 183 const fn sub(a: Vec3, b: Vec3) -> Vec3 { |
| 184 [a[0] - b[0], | 184 [a[0] - b[0], |
| 185 a[1] - b[1], | 185 a[1] - b[1], |
| 186 a[2] - b[2]] | 186 a[2] - b[2]] |
| 187 } | 187 } |
| 188 | 188 |
| 189 fn normalize(vec: Vec3) -> Vec3 { | 189 fn normalize(vec: Vec3) -> Vec3 { |
| 190 let normal = 1. / (vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]).sqrt(); | 190 let normal = 1. / (vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]).sqrt(); |
| 191 [vec[0] * normal, vec[1] * normal, vec[2] * normal] | 191 [vec[0] * normal, vec[1] * normal, vec[2] * normal] |
| 192 } | 192 } |
| 193 | 193 |
| 194 fn cross(a: Vec3, b: Vec3) -> Vec3 { | 194 const fn cross(a: Vec3, b: Vec3) -> Vec3 { |
| 195 [a[1] * b[2] - b[1] * a[2], | 195 [a[1] * b[2] - b[1] * a[2], |
| 196 a[2] * b[0] - b[2] * a[0], | 196 a[2] * b[0] - b[2] * a[0], |
| 197 a[0] * b[1] - b[0] * a[1]] | 197 a[0] * b[1] - b[0] * a[1]] |
| 198 } | 198 } |
| 199 | 199 |
| 200 fn dot(a: Vec3, b: Vec3) -> f32 { | 200 const fn dot(a: Vec3, b: Vec3) -> f32 { |
| 201 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] | 201 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] |
| 202 } | 202 } |
