Replace GLM with GLAM
This commit is contained in:
parent
07034a6c7a
commit
a805c5e06e
|
|
@ -12,8 +12,7 @@ opt-level = 2
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
glm = "0.2.3"
|
glam = "0.27.0"
|
||||||
flo_draw = "0.3.1"
|
flo_draw = "0.3.1"
|
||||||
flo_canvas = "0.3.1"
|
flo_canvas = "0.3.1"
|
||||||
num-traits = "0.2.18"
|
|
||||||
itertools-num = "0.1.3"
|
itertools-num = "0.1.3"
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use flo_draw::*;
|
use flo_draw::*;
|
||||||
use flo_canvas::*;
|
use flo_canvas::*;
|
||||||
use glm::*;
|
use glam::*;
|
||||||
use num_traits::identities::Zero;
|
|
||||||
use riemann::{Decomp2, Metric, trace_iter};
|
use riemann::{Decomp2, Metric, trace_iter};
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
|
@ -44,7 +43,7 @@ fn draw_ray(gc: &mut Vec<Draw>, space: &impl Metric, base: Vec2, dir: Vec2) {
|
||||||
gc.move_to(base.x, base.y);
|
gc.move_to(base.x, base.y);
|
||||||
for pt in trace_iter(space, base, dir, 1.0).take(10000) {
|
for pt in trace_iter(space, base, dir, 1.0).take(10000) {
|
||||||
gc.line_to(pt.x, pt.y);
|
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
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -60,26 +59,24 @@ struct Coil {
|
||||||
|
|
||||||
impl Metric for Coil {
|
impl Metric for Coil {
|
||||||
fn halfmetric(&self, pos: Vec2) -> Decomp2 {
|
fn halfmetric(&self, pos: Vec2) -> Decomp2 {
|
||||||
let r = length(pos);
|
let r = pos.length();
|
||||||
let dir = normalize(pos);
|
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 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{
|
Decomp2{
|
||||||
ortho: mat2(
|
ortho: Mat2::from_cols_array(&[
|
||||||
dir.x, - dir.y,
|
dir.x, -dir.y,
|
||||||
dir.y, dir.x,
|
dir.y, dir.x,
|
||||||
),
|
]),
|
||||||
diag : vec2(1.0, t),
|
diag : vec2(1.0, t),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod riemann {
|
mod riemann {
|
||||||
use glm::*;
|
use glam::*;
|
||||||
use glm::ext::pow2;
|
|
||||||
use num_traits::identities::Zero;
|
|
||||||
|
|
||||||
pub struct Decomp2 {
|
pub struct Decomp2 {
|
||||||
pub ortho: Mat2,
|
pub ortho: Mat2,
|
||||||
|
|
@ -90,21 +87,21 @@ mod riemann {
|
||||||
fn square(&self) -> Self {
|
fn square(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ortho: self.ortho,
|
ortho: self.ortho,
|
||||||
diag: pow2(self.diag),
|
diag: self.diag * self.diag,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inverse(&self) -> Self {
|
fn inverse(&self) -> Self {
|
||||||
Self {
|
Self {
|
||||||
ortho: self.ortho,
|
ortho: self.ortho,
|
||||||
diag: Vec2::from_s(1.0) / self.diag,
|
diag: Vec2::splat(1.0) / self.diag,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Decomp2> for Mat2 {
|
impl From<Decomp2> for Mat2 {
|
||||||
fn from(value: Decomp2) -> Self {
|
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 {
|
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 {
|
fn normalize(&self, at: Vec2, v: Vec2) -> Vec2 {
|
||||||
|
|
@ -170,11 +167,11 @@ mod riemann {
|
||||||
let g = &space.invmetric(pos); // с верхними индексами
|
let g = &space.invmetric(pos); // с верхними индексами
|
||||||
let d = space.dmetric(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]))
|
// 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::<f32>())
|
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::<f32>())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dir_deriv(f: impl Fn(Vec2) -> Mat2, pos: Vec2, delta: Vec2) -> Mat2 {
|
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 {
|
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 {
|
fn convolute(t: Tens2, v: Vec2) -> Vec2 {
|
||||||
vec2(
|
vec2(
|
||||||
dot(v, t[0] * v),
|
v.dot(t[0] * v),
|
||||||
dot(v, t[1] * 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 {
|
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 {
|
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 {
|
fn make_tens2(f: impl Fn(usize, usize, usize) -> f32) -> Tens2 {
|
||||||
|
|
@ -210,23 +203,23 @@ mod riemann {
|
||||||
#[test]
|
#[test]
|
||||||
fn m2() {
|
fn m2() {
|
||||||
let m = make_mat2(|i, j| (i + 2 * j) as f32);
|
let m = make_mat2(|i, j| (i + 2 * j) as f32);
|
||||||
assert_eq!(m[0][0], 0.0);
|
assert_eq!(m.col(0)[0], 0.0);
|
||||||
assert_eq!(m[1][0], 1.0);
|
assert_eq!(m.col(1)[0], 1.0);
|
||||||
assert_eq!(m[0][1], 2.0);
|
assert_eq!(m.col(0)[1], 2.0);
|
||||||
assert_eq!(m[1][1], 3.0);
|
assert_eq!(m.col(1)[1], 3.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn t2() {
|
fn t2() {
|
||||||
let t = make_tens2(|i, j, k| (i + 2 * j + 4 * k) as f32);
|
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[0].col(0)[0], 0.0);
|
||||||
assert_eq!(t[1][0][0], 1.0);
|
assert_eq!(t[1].col(0)[0], 1.0);
|
||||||
assert_eq!(t[0][1][0], 2.0);
|
assert_eq!(t[0].col(1)[0], 2.0);
|
||||||
assert_eq!(t[1][1][0], 3.0);
|
assert_eq!(t[1].col(1)[0], 3.0);
|
||||||
assert_eq!(t[0][0][1], 4.0);
|
assert_eq!(t[0].col(0)[1], 4.0);
|
||||||
assert_eq!(t[1][0][1], 5.0);
|
assert_eq!(t[1].col(0)[1], 5.0);
|
||||||
assert_eq!(t[0][1][1], 6.0);
|
assert_eq!(t[0].col(1)[1], 6.0);
|
||||||
assert_eq!(t[1][1][1], 7.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 {
|
fn smoothbox(val: f32, range: Vec2, pad: f32) -> f32 {
|
||||||
let slope1 = 1.0 + (val - range.x) / pad;
|
let slope1 = 1.0 + (val - range.x) / pad;
|
||||||
let slope2 = 1.0 - (val - range.y) / pad;
|
let slope2 = 1.0 - (val - range.y) / pad;
|
||||||
let lin = min(slope1, slope2);
|
let lin = slope1.min(slope2);
|
||||||
smoothstep(clamp(lin, 0.0, 1.0))
|
smoothstep(lin.clamp(0.0, 1.0))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user