Rename stuff

This commit is contained in:
numzero 2024-05-27 16:58:21 +03:00
parent f276b82bdd
commit 6965d7360e

View File

@ -70,16 +70,16 @@ impl Space {
}
fn flat_to_global(&self, at: Vec2, v: Vec2) -> Vec2 {
Mat2::from(self.rect.halfmetric(at).inverse()) * v
Mat2::from(self.rect.sqrt_at(at).inverse()) * v
}
fn global_to_flat(&self, at: Vec2, v: Vec2) -> Vec2 {
Mat2::from(self.rect.halfmetric(at)) * v
Mat2::from(self.rect.sqrt_at(at)) * v
}
/// Выполняет один шаг трассировки. Работает в любой части пространства, но вне Boundary доступны более эффективные методы.
fn trace_step(&self, ray: Ray) -> Ray {
let a: Vec2 = -riemann::convolute2(riemann::krist(&self.rect, ray.pos), ray.dir);
let a: Vec2 = -riemann::contract2(riemann::krist(&self.rect, ray.pos), ray.dir);
let v = ray.dir + a * DT;
let p = ray.pos + v * DT;
Ray { pos: p, dir: v }
@ -87,7 +87,7 @@ impl Space {
/// Выполняет один шаг перемещения. Работает в любой части пространства, но вне Boundary доступны более эффективные методы.
fn move_step(&self, loc: Location, off: Vec2) -> Location {
let corr = Mat2::IDENTITY - riemann::convolute(riemann::krist(&self.rect, loc.pos), loc.rot * off);
let corr = Mat2::IDENTITY - riemann::contract(riemann::krist(&self.rect, loc.pos), loc.rot * off);
let p = loc.pos + corr * loc.rot * off;
Location { pos: p, rot: corr * loc.rot }
}
@ -99,9 +99,9 @@ fn draw_ray_2(gc: &mut Vec<Draw>, space: &Space, base: Vec2, dir: Vec2) {
gc.move_to(base.x, base.y);
let dt = DT;
let mut p = base;
let mut v = space.rect.normalize(base, dir);
let mut v = space.rect.normalize_vec_at(base, dir);
for _ in 0..10000 {
let a: Vec2 = -riemann::convolute2(riemann::krist(&space.rect, p), v);
let a: Vec2 = -riemann::contract2(riemann::krist(&space.rect, p), v);
v = v + a * dt;
p = p + v * dt;
gc.line_to(p.x, p.y);
@ -454,7 +454,7 @@ fn test_rect() {
}
impl Metric for Rect {
fn halfmetric(&self, pos: Vec2) -> Decomp2 {
fn sqrt_at(&self, pos: Vec2) -> Decomp2 {
let x = pos.y.abs();
let sx = ((pos.x.abs() - self.inner_radius) / (self.outer_radius - self.inner_radius)).clamp(0.0, 1.0);
let sy = if x <= self.external_halflength { 1.0 / self.root(x) } else { 1.0 };
@ -501,30 +501,30 @@ mod riemann {
type Tens2 = [Mat2; 2];
pub trait Metric {
fn halfmetric(&self, pos: Vec2) -> Decomp2;
fn sqrt_at(&self, pos: Vec2) -> Decomp2;
fn metric(&self, pos: Vec2) -> Mat2 {
self.halfmetric(pos).square().into()
fn at(&self, pos: Vec2) -> Mat2 {
self.sqrt_at(pos).square().into()
}
fn invmetric(&self, pos: Vec2) -> Mat2 {
self.halfmetric(pos).square().inverse().into()
fn inverse_at(&self, pos: Vec2) -> Mat2 {
self.sqrt_at(pos).square().inverse().into()
}
fn dmetric(&self, pos: Vec2) -> Tens2 {
part_deriv(|p| self.metric(p), pos, 1.0e-3)
fn part_derivs_at(&self, pos: Vec2) -> Tens2 {
part_deriv(|p| self.at(p), pos, 1.0e-3)
}
fn length(&self, at: Vec2, v: Vec2) -> f32 {
v.dot(self.metric(at) * v).sqrt()
fn vec_length_at(&self, at: Vec2, v: Vec2) -> f32 {
v.dot(self.at(at) * v).sqrt()
}
fn normalize(&self, at: Vec2, v: Vec2) -> Vec2 {
v / self.length(at, v)
fn normalize_vec_at(&self, at: Vec2, v: Vec2) -> Vec2 {
v / self.vec_length_at(at, v)
}
fn globalize(&self, at: Vec2, v: Vec2) -> Vec2 {
Mat2::from(self.halfmetric(at).inverse()) * v
Mat2::from(self.sqrt_at(at).inverse()) * v
}
}
@ -539,7 +539,7 @@ mod riemann {
type Item = Vec2;
fn next(&mut self) -> Option<Self::Item> {
let a: Vec2 = -convolute2(krist(self.space, self.p), self.v);
let a: Vec2 = -contract2(krist(self.space, self.p), self.v);
self.v = self.v + a * self.dt;
self.p = self.p + self.v * self.dt;
Some(self.p)
@ -550,15 +550,15 @@ mod riemann {
TraceIter {
space,
p: base,
v: space.normalize(base, dir),
v: space.normalize_vec_at(base, dir),
dt,
}
}
pub fn krist(space: &impl Metric, pos: Vec2) -> Tens2 {
// Γ^i_k_l = .5 * g^i^m * (g_m_k,l + g_m_l,k - g_k_l,m)
let g = &space.invmetric(pos); // с верхними индексами
let d = space.dmetric(pos);
let g = &space.inverse_at(pos); // с верхними индексами
let d = space.part_derivs_at(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.col(m)[i] * (d[l].col(k)[m] + d[k].col(m)[l] - d[m].col(k)[l])).sum::<f32>())
}
@ -575,13 +575,13 @@ mod riemann {
}
/// Сворачивает тензор t с вектором u
pub fn convolute(t: Tens2, u: Vec2) -> Mat2 {
pub fn contract(t: Tens2, u: Vec2) -> Mat2 {
mat2(t[0] * u, t[1] * u).transpose()
}
/// Сворачивает тензор t с вектором v дважды, по второму и третьему индексам.
pub fn convolute2(t: Tens2, v: Vec2) -> Vec2 {
convolute(t, v) * v
pub fn contract2(t: Tens2, v: Vec2) -> Vec2 {
contract(t, v) * v
}
fn make_vec2(f: impl Fn(usize) -> f32) -> Vec2 {