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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use itertools::multizip;
use na::{convert, Matrix1xX, Matrix3x1, Matrix3xX, MatrixSlice3x1, RealField};

/// Magnitudes of a list of vectors 3xX.
pub fn magnitudes<T>(vectors: &Matrix3xX<T>) -> Matrix1xX<T>
where
    T: RealField,
{
    let number_vectors = vectors.ncols();
    let mut magnitudes = Matrix1xX::<T>::zeros(number_vectors);
    for (res, vector) in multizip((magnitudes.iter_mut(), vectors.column_iter())) {
        *res = magnitude_slice(vector);
    }
    magnitudes
}

/// Magnitude of a vector 3x1.
pub fn magnitude<T>(vector: &Matrix3x1<T>) -> T
where
    T: RealField,
{
    magnitude_slice(vector.column(0))
}

/// Magnitude of a slice 3x1.
pub fn magnitude_slice<T>(vector: MatrixSlice3x1<T>) -> T
where
    T: RealField,
{
    (vector).norm()
}

/// Directions of a list of vectors 3xX.
pub fn directions<T>(vectors: &Matrix3xX<T>) -> Matrix3xX<T>
where
    T: RealField,
{
    let number_vectors = vectors.ncols();
    let mut directions = Matrix3xX::zeros(number_vectors);
    for (mut direction, vector) in multizip((directions.column_iter_mut(), vectors.column_iter())) {
        direction.copy_from(&direction_slice(vector));
    }
    directions
}

/// Direction of a vector 3x1.
pub fn direction<T>(vector: &Matrix3x1<T>) -> Matrix3x1<T>
where
    T: RealField,
{
    direction_slice(vector.column(0))
}

/// Direction of a slice 3x1.
pub fn direction_slice<T>(vector: MatrixSlice3x1<T>) -> Matrix3x1<T>
where
    T: RealField,
{
    (vector).normalize()
}

/// Component-wise cartesian to spherical conversion.
pub fn cart_to_sph<T>(matrix: &Matrix3xX<T>) -> Matrix3xX<T>
where
    T: RealField,
{
    let mut sphericals = Matrix3xX::zeros(matrix.ncols());
    for (mut spherical, cartesian) in multizip((sphericals.column_iter_mut(), matrix.column_iter()))
    {
        if cartesian.norm() == convert(0.) {
            spherical.copy_from(&Matrix3x1::<T>::zeros());
        } else {
            spherical.copy_from_slice(&[
                cartesian[1].atan2(cartesian[0]),
                (cartesian[2] / cartesian.norm()).asin(),
                cartesian.norm(),
            ]);
        }
    }
    sphericals
}