nalgebra_glm/gtx/
norm.rs

1use crate::aliases::TVec;
2use crate::RealNumber;
3
4/// The squared distance between two points.
5///
6/// # See also:
7///
8/// * [`distance()`](crate::distance)
9pub fn distance2<T: RealNumber, const D: usize>(p0: &TVec<T, D>, p1: &TVec<T, D>) -> T {
10    (p1 - p0).norm_squared()
11}
12
13/// The l1-norm of `x - y`.
14///
15/// # See also:
16///
17/// * [`l1_norm()`]
18/// * [`l2_distance()`]
19/// * [`l2_norm()`]
20pub fn l1_distance<T: RealNumber, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>) -> T {
21    l1_norm(&(y - x))
22}
23
24/// The l1-norm of `v`.
25///
26/// This is also known as the "Manhattan distance" or "taxicab distance" and
27/// corresponds to the sum of the absolute values of the components of `v`.
28///
29/// # See also:
30///
31/// * [`l1_distance()`]
32/// * [`l2_distance()`]
33/// * [`l2_norm()`]
34pub fn l1_norm<T: RealNumber, const D: usize>(v: &TVec<T, D>) -> T {
35    crate::comp_add(&v.abs())
36}
37
38/// The l2-norm of `x - y`.
39///
40/// This is the same value as returned by [`length2()`] and
41/// [`magnitude2()`].
42///
43/// # See also:
44///
45/// * [`l1_distance()`]
46/// * [`l1_norm()`]
47/// * [`l2_norm()`]
48/// * [`length()`](crate::length)
49/// * [`length2()`]
50/// * [`magnitude()`](crate::magnitude)
51/// * [`magnitude2()`]
52pub fn l2_distance<T: RealNumber, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>) -> T {
53    l2_norm(&(y - x))
54}
55
56/// The l2-norm of `v`.
57///
58/// This is also known as the Euclidean norm.
59///
60/// This is the same value as returned by [`length()`](crate::length) and
61/// [`magnitude()`](crate::magnitude).
62///
63/// # See also:
64///
65/// * [`l1_distance()`]
66/// * [`l1_norm()`]
67/// * [`l2_distance()`]
68/// * [`length()`](crate::length)
69/// * [`length2()`]
70/// * [`magnitude()`](crate::magnitude)
71/// * [`magnitude2()`]
72pub fn l2_norm<T: RealNumber, const D: usize>(x: &TVec<T, D>) -> T {
73    x.norm()
74}
75
76/// The squared magnitude of `x`.
77///
78/// A synonym for [`magnitude2()`].
79///
80/// # See also:
81///
82/// * [`distance()`](crate::distance)
83/// * [`distance2()`]
84/// * [`length()`](crate::length)
85/// * [`magnitude()`](crate::magnitude)
86/// * [`magnitude2()`]
87pub fn length2<T: RealNumber, const D: usize>(x: &TVec<T, D>) -> T {
88    x.norm_squared()
89}
90
91/// The squared magnitude of `x`.
92///
93/// A wrapper around [`nalgebra::norm_squared`](../nalgebra/fn.norm_squared.html).
94///
95/// # See also:
96///
97/// * [`distance()`](crate::distance)
98/// * [`distance2()`]
99/// * [`length2()`]
100/// * [`magnitude()`](crate::magnitude)
101/// * [`nalgebra::norm_squared`](../nalgebra/fn.norm_squared.html)
102pub fn magnitude2<T: RealNumber, const D: usize>(x: &TVec<T, D>) -> T {
103    x.norm_squared()
104}
105
106//pub fn lxNorm<T: RealNumber, const D: usize>(x: &TVec<T, D>, y: &TVec<T, D>, unsigned int Depth) -> T {
107//    unimplemented!()
108//}
109//
110//pub fn lxNorm<T: RealNumber, const D: usize>(x: &TVec<T, D>, unsigned int Depth) -> T  {
111//    unimplemented!()
112//}