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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use crate::VectorSpace;
use num_traits::real::Real;
pub trait InnerSpace: VectorSpace {
fn scalar(self, other: Self) -> Self::Scalar;
#[inline]
fn magnitude2(self) -> Self::Scalar {
self.scalar(self)
}
#[inline]
fn magnitude(self) -> Self::Scalar {
self.magnitude2().sqrt()
}
#[inline]
fn normalize(self) -> Self {
self / self.magnitude()
}
#[inline]
fn angle(self, other: Self) -> Self::Scalar {
(self.scalar(other) / (self.magnitude() * other.magnitude())).acos()
}
#[inline]
fn with_magnitude(self, magnitude: Self::Scalar) -> Self {
self * (magnitude / self.magnitude())
}
#[inline]
fn with_direction(self, dir: Self) -> Self {
dir * self.magnitude()
}
#[inline]
fn query_axis(self, dir: Self) -> Self::Scalar {
self.scalar(dir.normalize())
}
#[inline]
fn normalized_project(self, dir: Self) -> Self {
dir * self.scalar(dir)
}
#[inline]
fn project(self, dir: Self) -> Self {
self.normalized_project(dir.normalize())
}
#[inline]
fn normalized_reject(self, dir: Self) -> Self {
self - self.normalized_project(dir)
}
#[inline]
fn reject(self, dir: Self) -> Self {
self.normalized_reject(dir.normalize())
}
#[inline]
fn normalized_reflect(self, dir: Self) -> Self {
let proj = self.normalized_project(dir);
proj + proj - self
}
#[inline]
fn reflect(self, dir: Self) -> Self {
self.normalized_reflect(dir.normalize())
}
}
impl InnerSpace for f32 {
fn scalar(self, other: Self) -> Self {
self * other
}
}
impl InnerSpace for f64 {
fn scalar(self, other: Self) -> Self {
self * other
}
}