use super::Region;
use crate::math::Vector3;
use std::f32::consts::TAU;
use std::sync::Arc;
pub struct Conform<'a> {
map: Arc<dyn Fn(Vector3) -> Vector3 + Sync + Send + 'a>,
stretch: Box<dyn Fn(Vector3) -> f32 + Sync + Send + 'a>,
}
impl<'a> Conform<'a> {
pub fn new(map: impl Fn(Vector3) -> Vector3 + Sync + Send + 'a) -> Self {
let map: Arc<dyn Fn(Vector3) -> Vector3 + Sync + Send + 'a> = Arc::new(map);
let differenced = Arc::clone(&map);
Self {
map,
stretch: Box::new(move |p| numeric_stretch(&*differenced, p)),
}
}
pub fn with_stretch(
map: impl Fn(Vector3) -> Vector3 + Sync + Send + 'a,
stretch: impl Fn(Vector3) -> f32 + Sync + Send + 'a,
) -> Self {
Self {
map: Arc::new(map),
stretch: Box::new(stretch),
}
}
pub fn cylindrical(origin: Vector3, axis: Vector3, reference: f32) -> Self {
let axis = axis.normalize();
let (u, v) = frame(axis);
let reference = reference.abs().max(1e-6);
let map = move |p: Vector3| {
let w = p - origin;
let along = w.dot(axis);
let radial = w - axis * along;
let angle = radial.dot(v).atan2(radial.dot(u));
Vector3::new(angle * reference, along, radial.length())
};
let stretch = move |p: Vector3| {
let w = p - origin;
let r = (w - axis * w.dot(axis)).length().max(1e-6);
(reference / r + 2.0) / 3.0
};
Self::with_stretch(map, stretch)
}
pub fn spherical(center: Vector3, reference: f32) -> Self {
let reference = reference.abs().max(1e-6);
let map = move |p: Vector3| {
let w = p - center;
let r = w.length();
let azimuth = w.y.atan2(w.x);
let polar = (w.z / r.max(1e-6)).clamp(-1.0, 1.0).acos();
Vector3::new(azimuth * reference, polar * reference, r)
};
let stretch = move |p: Vector3| {
let w = p - center;
let r = w.length().max(1e-6);
let sin_polar = ((w.x * w.x + w.y * w.y).sqrt() / r).max(0.05);
(reference / (r * sin_polar) + reference / r + 1.0) / 3.0
};
Self::with_stretch(map, stretch)
}
pub fn depth(region: Region<'a>) -> Self {
Self::with_stretch(move |p| Vector3::new(p.x, p.y, region.distance(p)), |_| 1.0)
}
pub fn ring_pitch(radius: f32, count: usize) -> f32 {
TAU * radius.abs() / count.max(1) as f32
}
pub fn point(&self, p: Vector3) -> Vector3 {
(self.map)(p)
}
pub fn stretch_at(&self, p: Vector3) -> f32 {
let s = (self.stretch)(p);
if s.is_finite() && s > 1e-6 {
s
} else {
1.0
}
}
}
fn frame(axis: Vector3) -> (Vector3, Vector3) {
let seed = if axis.x.abs() < 0.9 {
Vector3::new(1.0, 0.0, 0.0)
} else {
Vector3::new(0.0, 1.0, 0.0)
};
let u = axis.cross(seed).normalize();
(u, axis.cross(u))
}
fn numeric_stretch(map: &(dyn Fn(Vector3) -> Vector3 + Sync + Send), p: Vector3) -> f32 {
let h = 1e-3 * (1.0 + p.length());
let at = map(p);
let mut total = 0.0;
for axis in 0..3 {
let mut step = Vector3::ZERO;
match axis {
0 => step.x = h,
1 => step.y = h,
_ => step.z = h,
}
total += (map(p + step) - at).length() / h;
}
total / 3.0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cylindrical_is_an_isometry_at_the_reference_radius() {
let c = Conform::cylindrical(Vector3::ZERO, Vector3::new(0.0, 0.0, 1.0), 10.0);
assert!((c.stretch_at(Vector3::new(10.0, 0.0, 3.0)) - 1.0).abs() < 1e-5);
assert!((c.stretch_at(Vector3::new(5.0, 0.0, 0.0)) - 4.0 / 3.0).abs() < 1e-5);
}
#[test]
fn cylindrical_maps_arc_length_to_x() {
let c = Conform::cylindrical(Vector3::ZERO, Vector3::new(0.0, 0.0, 1.0), 4.0);
let a = c.point(Vector3::new(4.0, 0.0, 1.0));
let b = c.point(Vector3::new(0.0, 4.0, 1.0));
assert!((b.x - a.x - TAU * 4.0 / 4.0).abs() < 1e-4, "{a:?} {b:?}");
assert!((a.y - 1.0).abs() < 1e-5 && (a.z - 4.0).abs() < 1e-5);
}
#[test]
fn ring_pitch_closes_the_seam() {
let radius = 7.0;
let count = 12;
let pitch = Conform::ring_pitch(radius, count);
assert!((pitch * count as f32 - TAU * radius).abs() < 1e-4);
}
#[test]
fn depth_measures_from_the_surface() {
let c = Conform::depth(Region::sphere(Vector3::ZERO, 5.0));
assert!(c.point(Vector3::new(5.0, 0.0, 0.0)).z.abs() < 1e-4);
assert!((c.point(Vector3::ZERO).z - 5.0).abs() < 1e-4);
assert!(c.point(Vector3::new(8.0, 0.0, 0.0)).z < 0.0);
let p = Vector3::new(1.0, 2.0, 3.0);
let q = c.point(p);
assert_eq!((q.x, q.y), (p.x, p.y));
}
#[test]
fn a_differenced_stretch_finds_a_uniform_scale() {
let c = Conform::new(|p| p * 3.0);
assert!((c.stretch_at(Vector3::new(1.0, -2.0, 0.5)) - 3.0).abs() < 1e-2);
}
#[test]
fn a_degenerate_stretch_falls_back_to_one() {
let c = Conform::with_stretch(|p| p, |_| 0.0);
assert_eq!(c.stretch_at(Vector3::ZERO), 1.0);
let nan = Conform::with_stretch(|p| p, |_| f32::NAN);
assert_eq!(nan.stretch_at(Vector3::ZERO), 1.0);
}
}