atomecs/
maths.rs

1//! Mathematical utilities
2
3use crate::constant::EXP;
4use crate::constant::PI;
5extern crate nalgebra;
6use nalgebra::Vector3;
7
8/// Get relative coordinates between a point and a line.
9///
10/// # Arguments
11///
12/// `pos`: position of the point
13///
14/// `line_point`: a point on the line
15///
16/// `dir`: vector pointing along the line.
17pub fn get_relative_coordinates_line_point(
18    pos: &Vector3<f64>,
19    line_point: &Vector3<f64>,
20    dir: &Vector3<f64>,
21    frame: &crate::laser::frame::Frame,
22) -> (f64, f64, f64) {
23    let rela_cood = pos - line_point;
24    let z = rela_cood.dot(dir) / dir.norm();
25    let r_vec = rela_cood - z * dir;
26    let x = r_vec.dot(&frame.x_vector);
27    let y = r_vec.dot(&frame.y_vector);
28    (x, y, z)
29}
30
31/// Get miniminum distance between a point and a line.
32///
33/// # Arguments
34///
35/// `pos`: position of the point
36///
37/// `line_point`: a point on the line
38///
39/// `dir`: vector pointing along the line.
40pub fn get_minimum_distance_line_point(
41    pos: &Vector3<f64>,
42    line_point: &Vector3<f64>,
43    dir: &Vector3<f64>,
44) -> (f64, f64) {
45    let rela_cood = pos - line_point;
46    let distance = (dir.cross(&rela_cood) / dir.norm()).norm();
47    let z = rela_cood.dot(dir) / dir.norm();
48    (distance, z)
49}
50
51/// A normalised gaussian distribution.
52///
53/// The distribution is normalised such that the 2D area underneath a gaussian dist with sigma_x=sigma_y=std is equal to 1.
54pub fn gaussian_dis(std: f64, distance_squared: f64) -> f64 {
55    1.0 / (2.0 * PI * std * std) * EXP.powf(-distance_squared / 2.0 / (std * std))
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_minimum_distance_line_point() {
64        let pos = Vector3::new(1., 1., 1.);
65        let centre = Vector3::new(0., 1., 1.);
66        let dir = Vector3::new(1., 2., 2.);
67        let (distance, _) = get_minimum_distance_line_point(&pos, &centre, &dir);
68        assert!(distance > 0.942, "{}", distance < 0.943);
69    }
70}