autd3_driver/acoustics/directivity/
mod.rs

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
mod sphere;
mod t4010a1;

use crate::{
    defined::{rad, Angle},
    geometry::Vector3,
};

pub use sphere::Sphere;
pub use t4010a1::T4010A1;

pub trait Directivity: Send + Sync {
    fn directivity(theta: Angle) -> f32;
    fn directivity_from_dir(axial_direction: &Vector3, target: &Vector3) -> f32 {
        Self::directivity(
            (axial_direction.cross(target).norm()).atan2(axial_direction.dot(target)) * rad,
        )
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;

    pub struct TestDirectivity {}

    impl Directivity for TestDirectivity {
        fn directivity(t: Angle) -> f32 {
            t.degree()
        }
    }

    #[rstest::rstest]
    #[test]
    #[case::dir_x(90., Vector3::x(), Vector3::z())]
    #[case::dir_y(90., Vector3::y(), Vector3::z())]
    #[case::dir_z(0., Vector3::z(), Vector3::z())]
    fn test_directivity_from_dir(
        #[case] expected: f32,
        #[case] target: Vector3,
        #[case] dir: Vector3,
    ) {
        approx::assert_abs_diff_eq!(
            expected,
            TestDirectivity::directivity_from_dir(&dir, &target)
        );
    }
}