Mercurial > touhou
diff utils/src/math.rs @ 790:d005f5927447
utils: Use const_for to make more const fn
| author | Link Mauve <linkmauve@linkmauve.fr> |
|---|---|
| date | Sat, 17 Jan 2026 14:12:08 +0100 |
| parents | b5bca9274335 |
| children | a29122662cde |
line wrap: on
line diff
--- a/utils/src/math.rs +++ b/utils/src/math.rs @@ -1,5 +1,7 @@ //! Various helpers to deal with vectors and matrices. +use const_for::const_for; + /// A 4×4 f32 matrix type. pub struct Mat4 { inner: [[f32; 4]; 4] @@ -34,18 +36,18 @@ } /// Scale the matrix in 2D. - pub fn scale2d(&mut self, x: f32, y: f32) { - for i in 0..4 { + pub const fn scale2d(&mut self, x: f32, y: f32) { + const_for!(i in 0..4 => { self.inner[0][i] *= x; self.inner[1][i] *= y; - } + }); } /// Flip the matrix. - pub fn flip(&mut self) { - for i in 0..4 { + pub const fn flip(&mut self) { + const_for!(i in 0..4 => { self.inner[0][i] = -self.inner[0][i]; - } + }); } /// Rotate the matrix around its x angle (in radians). @@ -53,14 +55,14 @@ let mut lines: [f32; 8] = [0.; 8]; let cos_a = angle.cos(); let sin_a = angle.sin(); - for i in 0..4 { + const_for!(i in 0..4 => { lines[ i] = self.inner[0][i]; lines[4 + i] = self.inner[1][i]; - } - for i in 0..4 { + }); + const_for!(i in 0..4 => { self.inner[1][i] = cos_a * lines[i] - sin_a * lines[4+i]; self.inner[2][i] = sin_a * lines[i] + cos_a * lines[4+i]; - } + }); } /// Rotate the matrix around its y angle (in radians). @@ -68,14 +70,14 @@ let mut lines: [f32; 8] = [0.; 8]; let cos_a = angle.cos(); let sin_a = angle.sin(); - for i in 0..4 { + const_for!(i in 0..4 => { lines[ i] = self.inner[0][i]; lines[4 + i] = self.inner[2][i]; - } - for i in 0..4 { + }); + const_for!(i in 0..4 => { self.inner[0][i] = cos_a * lines[i] + sin_a * lines[4+i]; self.inner[2][i] = -sin_a * lines[i] + cos_a * lines[4+i]; - } + }); } /// Rotate the matrix around its z angle (in radians). @@ -83,31 +85,31 @@ let mut lines: [f32; 8] = [0.; 8]; let cos_a = angle.cos(); let sin_a = angle.sin(); - for i in 0..4 { + const_for!(i in 0..4 => { lines[ i] = self.inner[0][i]; lines[4 + i] = self.inner[1][i]; - } - for i in 0..4 { + }); + const_for!(i in 0..4 => { self.inner[0][i] = cos_a * lines[i] - sin_a * lines[4+i]; self.inner[1][i] = sin_a * lines[i] + cos_a * lines[4+i]; - } + }); } /// Translate the matrix by a 3D offset. - pub fn translate(&mut self, offset: [f32; 3]) { + pub const fn translate(&mut self, offset: [f32; 3]) { let mut item: [f32; 3] = [0.; 3]; - for i in 0..3 { + const_for!(i in 0..3 => { item[i] = self.inner[3][i] * offset[i]; - } - for i in 0..3 { - for j in 0..4 { + }); + const_for!(i in 0..3 => { + const_for!(j in 0..4 => { self.inner[i][j] += item[i]; - } - } + }); + }); } /// Translate the matrix by a 2D offset. - pub fn translate_2d(&mut self, x: f32, y: f32) { + pub const fn translate_2d(&mut self, x: f32, y: f32) { let offset = [x, y, 0.]; self.translate(offset); } @@ -117,13 +119,13 @@ type Output = Mat4; fn mul(self, rhs: Mat4) -> Mat4 { let mut tmp = Mat4::zero(); - for i in 0..4 { - for j in 0..4 { - for k in 0..4 { + const_for!(i in 0..4 => { + const_for!(j in 0..4 => { + const_for!(k in 0..4 => { tmp.inner[i][j] += self.inner[i][k] * rhs.inner[k][j]; - } - } - } + }); + }); + }); tmp } }
