From a805c5e06e2acb6c01c2c65c6865a8470e9581b5 Mon Sep 17 00:00:00 2001 From: numzero Date: Mon, 29 Apr 2024 00:35:16 +0300 Subject: [PATCH] Replace GLM with GLAM --- Cargo.toml | 3 +- src/bin/flat.rs | 73 ++++++++++++++++++++++--------------------------- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a886d81..ca0a0e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,8 +12,7 @@ opt-level = 2 opt-level = 3 [dependencies] -glm = "0.2.3" +glam = "0.27.0" flo_draw = "0.3.1" flo_canvas = "0.3.1" -num-traits = "0.2.18" itertools-num = "0.1.3" diff --git a/src/bin/flat.rs b/src/bin/flat.rs index 17c5eea..063cb8d 100644 --- a/src/bin/flat.rs +++ b/src/bin/flat.rs @@ -1,7 +1,6 @@ use flo_draw::*; use flo_canvas::*; -use glm::*; -use num_traits::identities::Zero; +use glam::*; use riemann::{Decomp2, Metric, trace_iter}; pub fn main() { @@ -44,7 +43,7 @@ fn draw_ray(gc: &mut Vec, space: &impl Metric, base: Vec2, dir: Vec2) { gc.move_to(base.x, base.y); for pt in trace_iter(space, base, dir, 1.0).take(10000) { gc.line_to(pt.x, pt.y); - if any(greaterThan(abs(pt), Vec2::from_s(1000.0))) { + if pt.abs().cmpgt(Vec2::splat(1000.0)).any() { break } } @@ -60,26 +59,24 @@ struct Coil { impl Metric for Coil { fn halfmetric(&self, pos: Vec2) -> Decomp2 { - let r = length(pos); - let dir = normalize(pos); + let r = pos.length(); + let dir = pos.normalize(); let s = smoothbox (r, vec2(self.coil_r - self.coil_w, self.coil_r + self.coil_w), self.coil_m); - let t = mix(1.0, self.coil_r / r / self.coil_scale, s); + let t = 1.0.lerp(self.coil_r / r / self.coil_scale, s); Decomp2{ - ortho: mat2( - dir.x, - dir.y, + ortho: Mat2::from_cols_array(&[ + dir.x, -dir.y, dir.y, dir.x, - ), + ]), diag : vec2(1.0, t), } } } mod riemann { - use glm::*; - use glm::ext::pow2; - use num_traits::identities::Zero; + use glam::*; pub struct Decomp2 { pub ortho: Mat2, @@ -90,21 +87,21 @@ mod riemann { fn square(&self) -> Self { Self { ortho: self.ortho, - diag: pow2(self.diag), + diag: self.diag * self.diag, } } fn inverse(&self) -> Self { Self { ortho: self.ortho, - diag: Vec2::from_s(1.0) / self.diag, + diag: Vec2::splat(1.0) / self.diag, } } } impl From for Mat2 { fn from(value: Decomp2) -> Self { - transpose(&value.ortho) * diagonal(value.diag) * value.ortho + value.ortho.transpose() * Mat2::from_diagonal(value.diag) * value.ortho } } @@ -126,7 +123,7 @@ mod riemann { } fn length(&self, at: Vec2, v: Vec2) -> f32 { - sqrt(dot(v, self.metric(at) * v)) + v.dot(self.metric(at) * v).sqrt() } fn normalize(&self, at: Vec2, v: Vec2) -> Vec2 { @@ -170,11 +167,11 @@ mod riemann { let g = &space.invmetric(pos); // с верхними индексами let d = space.dmetric(pos); // ret[i][l][k] = sum((m) => .5f * g[m][i] * (d[k][l][m] + d[l][k][m] - d[m][k][l])) - make_tens2(|i, l, k| 0.5 * (0..2).map(|m| g[m][i] * (d[l][k][m] + d[k][m][l] - d[m][k][l])).sum::()) + make_tens2(|i, l, k| 0.5 * (0..2).map(|m| g.col(m)[i] * (d[l].col(k)[m] + d[k].col(m)[l] - d[m].col(k)[l])).sum::()) } fn dir_deriv(f: impl Fn(Vec2) -> Mat2, pos: Vec2, delta: Vec2) -> Mat2 { - (f(pos + delta) - f(pos - delta)) / (2.0 * length(delta)) + (f(pos + delta) - f(pos - delta)) / (2.0 * delta.length()) } fn part_deriv(f: impl Fn(Vec2) -> Mat2, pos: Vec2, eps: f32) -> Tens2 { @@ -186,21 +183,17 @@ mod riemann { fn convolute(t: Tens2, v: Vec2) -> Vec2 { vec2( - dot(v, t[0] * v), - dot(v, t[1] * v) + v.dot(t[0] * v), + v.dot(t[1] * v) ) } - fn diagonal(v: Vec2) -> Mat2 { - mat2(v.x, 0.0, 0.0, v.y) - } - fn make_vec2(f: impl Fn(usize) -> f32) -> Vec2 { - vec2(f(0), f(1)) + Vec2::from_array(std::array::from_fn(|i| f(i))) } fn make_mat2(f: impl Fn(usize, usize) -> f32) -> Mat2 { - mat2(f(0, 0), f(0, 1), f(1, 0), f(1, 1)) + Mat2::from_cols_array_2d(&std::array::from_fn(|i| std::array::from_fn(|j| f(i, j)))) } fn make_tens2(f: impl Fn(usize, usize, usize) -> f32) -> Tens2 { @@ -210,23 +203,23 @@ mod riemann { #[test] fn m2() { let m = make_mat2(|i, j| (i + 2 * j) as f32); - assert_eq!(m[0][0], 0.0); - assert_eq!(m[1][0], 1.0); - assert_eq!(m[0][1], 2.0); - assert_eq!(m[1][1], 3.0); + assert_eq!(m.col(0)[0], 0.0); + assert_eq!(m.col(1)[0], 1.0); + assert_eq!(m.col(0)[1], 2.0); + assert_eq!(m.col(1)[1], 3.0); } #[test] fn t2() { let t = make_tens2(|i, j, k| (i + 2 * j + 4 * k) as f32); - assert_eq!(t[0][0][0], 0.0); - assert_eq!(t[1][0][0], 1.0); - assert_eq!(t[0][1][0], 2.0); - assert_eq!(t[1][1][0], 3.0); - assert_eq!(t[0][0][1], 4.0); - assert_eq!(t[1][0][1], 5.0); - assert_eq!(t[0][1][1], 6.0); - assert_eq!(t[1][1][1], 7.0); + assert_eq!(t[0].col(0)[0], 0.0); + assert_eq!(t[1].col(0)[0], 1.0); + assert_eq!(t[0].col(1)[0], 2.0); + assert_eq!(t[1].col(1)[0], 3.0); + assert_eq!(t[0].col(0)[1], 4.0); + assert_eq!(t[1].col(0)[1], 5.0); + assert_eq!(t[0].col(1)[1], 6.0); + assert_eq!(t[1].col(1)[1], 7.0); } } @@ -237,6 +230,6 @@ fn smoothstep(x: f32) -> f32 { fn smoothbox(val: f32, range: Vec2, pad: f32) -> f32 { let slope1 = 1.0 + (val - range.x) / pad; let slope2 = 1.0 - (val - range.y) / pad; - let lin = min(slope1, slope2); - smoothstep(clamp(lin, 0.0, 1.0)) + let lin = slope1.min(slope2); + smoothstep(lin.clamp(0.0, 1.0)) }