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
//! Gyroid SDF (Deep Fried Edition)
//!
//! Gyroid triply-periodic minimal surface (TPMS).
//! Popular in procedural art, 3D printing lattices, and architectural design.
//!
//! The gyroid surface satisfies: sin(x)*cos(y) + sin(y)*cos(z) + sin(z)*cos(x) = 0
//!
//! Author: Moroya Sakamoto
use glam::Vec3;
/// Approximate SDF for a gyroid surface
///
/// - `scale`: spatial frequency (larger = more repetitions)
/// - `thickness`: shell thickness (half)
#[inline(always)]
pub fn sdf_gyroid(p: Vec3, scale: f32, thickness: f32) -> f32 {
let sp = p * scale;
let d = alice_det_math::sin(sp.z) * alice_det_math::cos(sp.x)
+ (alice_det_math::sin(sp.x) * alice_det_math::cos(sp.y)
+ (alice_det_math::sin(sp.y) * alice_det_math::cos(sp.z)));
// Normalize by scale for proper distance metric
d.abs() / scale - thickness
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gyroid_on_surface() {
// At (PI/2, 0, 0): sin(PI/2)*cos(0) + sin(0)*cos(0) + sin(0)*cos(PI/2) = 1
// So distance from surface is (1/scale - thickness)
let d = sdf_gyroid(Vec3::new(std::f32::consts::FRAC_PI_2, 0.0, 0.0), 1.0, 0.5);
assert!((d - 0.5).abs() < 0.01, "Expected ~0.5, got {}", d);
}
#[test]
fn test_gyroid_origin() {
// At origin: sin(0)*cos(0) + sin(0)*cos(0) + sin(0)*cos(0) = 0
// Should be on the surface minus thickness
let d = sdf_gyroid(Vec3::ZERO, 1.0, 0.1);
assert!(
(d + 0.1).abs() < 0.01,
"Origin should be -thickness, got {}",
d
);
}
#[test]
fn test_gyroid_periodicity() {
let tau = 2.0 * std::f32::consts::PI;
let d1 = sdf_gyroid(Vec3::new(0.5, 0.3, 0.7), 1.0, 0.1);
let d2 = sdf_gyroid(Vec3::new(0.5 + tau, 0.3 + tau, 0.7 + tau), 1.0, 0.1);
assert!((d1 - d2).abs() < 0.01, "Should be periodic with 2*PI");
}
#[test]
fn test_gyroid_scale() {
// Higher scale = more frequent oscillations
let d1 = sdf_gyroid(Vec3::new(0.5, 0.3, 0.7), 1.0, 0.1);
let d2 = sdf_gyroid(Vec3::new(0.5, 0.3, 0.7), 2.0, 0.1);
// Just check both are finite
assert!(d1.is_finite() && d2.is_finite());
}
}