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
use core::ops::{Add, Mul};
use rust_gpu_bridge::mix::Mix;
use type_fields::Field;
use crate::prelude::{Distance, Operator, SignedDistanceField, SignedDistanceOperator};
#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd, Field)]
pub struct SmoothIntersectionOp<Sdf> {
pub sdf: Sdf,
pub k: f32,
}
impl<SdfB, Dim> SignedDistanceOperator<Dim, Distance> for SmoothIntersectionOp<SdfB>
where
SdfB: SignedDistanceField<Dim, Distance>,
Dim: Clone,
{
fn operator<SdfA>(&self, sdf: &SdfA, p: Dim) -> Distance
where
SdfA: SignedDistanceField<Dim, Distance>,
{
let d1 = *sdf.evaluate(p.clone());
let d2 = *self.sdf.evaluate(p);
let h = (0.5 - 0.5 * (d2 - d1) / self.k).clamp(0.0, 1.0);
d2.mix(d1, h).add(self.k.mul(h).mul(1.0 - h)).into()
}
}
pub type SmoothIntersection<SdfA, SdfB> = Operator<SmoothIntersectionOp<SdfB>, SdfA>;
impl<SdfA, SdfB> SmoothIntersection<SdfA, SdfB> {
pub fn sdf(&mut self) -> &mut SdfB {
&mut self.op.sdf
}
pub fn k(&mut self) -> &mut f32 {
&mut self.op.k
}
}
#[cfg(test)]
pub mod test {
use type_fields::field::Field;
use crate::signed_distance_field::shapes::composite::{Cube, Sphere};
use super::SmoothIntersection;
#[test]
fn test_smooth_intersection() {
SmoothIntersection::<Cube, Sphere>::default()
.with(SmoothIntersection::sdf, Sphere::default())
.with(SmoothIntersection::k, f32::default());
}
}