1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Link (Chain Link) SDF (Deep Fried Edition)
//!
//! Exact SDF for a chain link shape centered at origin.
//! A torus stretched along Y-axis.
//!
//! Based on Inigo Quilez's sdLink formula.
//!
//! Author: Moroya Sakamoto
use glam::Vec3;
/// Exact SDF for a chain link shape centered at origin
///
/// - `half_length`: half the straight section length along Y
/// - `r1`: major radius (distance from center to tube center)
/// - `r2`: minor radius (tube thickness)
#[inline(always)]
pub fn sdf_link(p: Vec3, half_length: f32, r1: f32, r2: f32) -> f32 {
let qx = p.x;
let qy = (p.y.abs() - half_length).max(0.0);
let qz = p.z;
let xy_len = qx.hypot(qy) - r1;
xy_len.hypot(qz) - r2
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_link_origin_outside() {
// Origin is outside the tube (torus-like shape). Distance = r1 - r2
let d = sdf_link(Vec3::ZERO, 0.5, 1.0, 0.3);
assert!(
(d - 0.7).abs() < 0.001,
"Origin distance should be r1-r2=0.7, got {}",
d
);
}
#[test]
fn test_link_on_tube() {
// At (r1, 0, 0), distance should be approximately -r2
let d = sdf_link(Vec3::new(1.0, 0.0, 0.0), 0.5, 1.0, 0.3);
assert!(
(d + 0.3).abs() < 0.01,
"On major radius, d should be ~-r2, got {}",
d
);
}
#[test]
fn test_link_outside() {
let d = sdf_link(Vec3::new(5.0, 0.0, 0.0), 0.5, 1.0, 0.3);
assert!(d > 0.0, "Far point should be outside, got {}", d);
}
#[test]
fn test_link_symmetry_y() {
let d1 = sdf_link(Vec3::new(0.5, 1.0, 0.2), 0.5, 1.0, 0.3);
let d2 = sdf_link(Vec3::new(0.5, -1.0, 0.2), 0.5, 1.0, 0.3);
assert!((d1 - d2).abs() < 0.001, "Should be symmetric in Y");
}
}