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
use crate::kernel::Kernel;
use na::RealField;

/// The Poly6 smoothing kernel.
///
/// Refer to "Particle-Based Fluid Simulation for Interactive Applications", Müller et al.
#[derive(Copy, Clone, Debug)]
pub struct Poly6Kernel;

impl Kernel for Poly6Kernel {
    fn scalar_apply<N: RealField>(r: N, h: N) -> N {
        assert!(r >= N::zero());

        #[cfg(feature = "dim2")]
        let normalizer = na::convert::<_, N>(4.0) / (N::pi() * h.powi(8));
        #[cfg(feature = "dim3")]
        let normalizer = na::convert::<_, N>(315.0 / 64.0) / (N::pi() * h.powi(9));

        if r <= h {
            normalizer * (h * h - r * r).powi(3)
        } else {
            N::zero()
        }
    }

    fn scalar_apply_diff<N: RealField>(r: N, h: N) -> N {
        assert!(r >= N::zero());

        #[cfg(feature = "dim2")]
        let normalizer = na::convert::<_, N>(4.0) / (N::pi() * h.powi(8));
        #[cfg(feature = "dim3")]
        let normalizer = na::convert::<_, N>(315.0 / 64.0) / (N::pi() * h.powi(9));

        if r <= h {
            normalizer * (h * h - r * r).powi(2) * r * na::convert(-6.0)
        } else {
            N::zero()
        }
    }
}