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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Torus primitive SDF (Deep Fried Edition)
//!
//! # Deep Fried Optimizations
//! - **Forced Inlining**: Zero call overhead.
//! - **Vectorized Operations**: Uses glam's optimized Vec2 operations.
//!
//! Author: Moroya Sakamoto
use glam::{Vec2, Vec3};
/// Signed distance to a torus in the XZ plane centered at origin
///
/// # Arguments
/// * `point` - Point to evaluate
/// * `major_radius` - Distance from center of torus to center of tube
/// * `minor_radius` - Radius of the tube
///
/// # Returns
/// Signed distance (negative inside, positive outside)
#[inline(always)]
pub fn sdf_torus(point: Vec3, major_radius: f32, minor_radius: f32) -> f32 {
let q = Vec2::new(Vec2::new(point.x, point.z).length() - major_radius, point.y);
q.length() - minor_radius
}
/// Signed distance to a torus with arbitrary orientation
///
/// The torus is rotated so its axis aligns with `axis`
#[allow(dead_code)]
#[inline(always)]
pub fn sdf_torus_oriented(point: Vec3, axis: Vec3, major_radius: f32, minor_radius: f32) -> f32 {
let axis = axis.normalize();
// Project point onto the plane perpendicular to axis
let projected_dist = point.dot(axis);
let in_plane = point - axis * projected_dist;
let radial_dist = in_plane.length();
let q = Vec2::new(radial_dist - major_radius, projected_dist);
q.length() - minor_radius
}
/// Signed distance to a capped torus (partial torus)
///
/// # Arguments
/// * `point` - Point to evaluate
/// * `major_radius` - Distance from center to tube center
/// * `minor_radius` - Tube radius
/// * `angle` - Half-angle of the cap in radians (0 to PI)
#[inline(always)]
pub fn sdf_torus_capped(point: Vec3, major_radius: f32, minor_radius: f32, angle: f32) -> f32 {
let sc = Vec2::new(angle.sin(), angle.cos());
let p_xz = Vec2::new(point.x.abs(), point.z);
// Branchless selection would require select intrinsic
// For now, this branch is predictable per-call
let k = if sc.y * p_xz.x > sc.x * p_xz.y {
p_xz.dot(sc)
} else {
p_xz.length()
};
let q = Vec2::new(
(2.0 * major_radius)
.mul_add(
-k,
major_radius.mul_add(major_radius, p_xz.length_squared()),
)
.sqrt(),
point.y,
);
q.length() - minor_radius
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_torus_center() {
// At the very center of the torus hole
let d = sdf_torus(Vec3::ZERO, 2.0, 0.5);
// Distance should be major_radius - minor_radius = 1.5
assert!((d - 1.5).abs() < 0.0001);
}
#[test]
fn test_torus_tube_center() {
// Inside the tube at (major_radius, 0, 0)
let d = sdf_torus(Vec3::new(2.0, 0.0, 0.0), 2.0, 0.5);
// Should be exactly on the tube center, distance = -minor_radius
assert!((d + 0.5).abs() < 0.0001);
}
#[test]
fn test_torus_surface_outer() {
// On outer surface
let d = sdf_torus(Vec3::new(2.5, 0.0, 0.0), 2.0, 0.5);
assert!(d.abs() < 0.0001);
}
#[test]
fn test_torus_surface_inner() {
// On inner surface
let d = sdf_torus(Vec3::new(1.5, 0.0, 0.0), 2.0, 0.5);
assert!(d.abs() < 0.0001);
}
#[test]
fn test_torus_surface_top() {
// On top surface
let d = sdf_torus(Vec3::new(2.0, 0.5, 0.0), 2.0, 0.5);
assert!(d.abs() < 0.0001);
}
#[test]
fn test_torus_oriented() {
// Torus with Y axis (same as default)
let d = sdf_torus_oriented(Vec3::new(2.0, 0.0, 0.0), Vec3::Y, 2.0, 0.5);
assert!((d + 0.5).abs() < 0.0001);
}
}