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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use na::{self, Real};
use bounding_volume::{BoundingVolume, HasBoundingVolume};
use math::{Point, Vector, Isometry};
pub fn aabb<N, G: ?Sized>(g: &G, m: &Isometry<N>) -> AABB<N>
where
N: Real,
G: HasBoundingVolume<N, AABB<N>>,
{
g.bounding_volume(m)
}
#[derive(Debug, PartialEq, Clone)]
pub struct AABB<N: Real> {
mins: Point<N>,
maxs: Point<N>,
}
impl<N: Real> AABB<N> {
pub fn new(mins: Point<N>, maxs: Point<N>) -> AABB<N> {
AABB {
mins: mins,
maxs: maxs,
}
}
#[inline]
pub fn mins(&self) -> &Point<N> {
&self.mins
}
#[inline]
pub fn maxs(&self) -> &Point<N> {
&self.maxs
}
#[inline]
pub fn center(&self) -> Point<N> {
na::center(&self.mins, &self.maxs)
}
#[inline]
pub fn half_extents(&self) -> Vector<N> {
let half: N = na::convert(0.5);
(self.maxs - self.mins) * half
}
}
impl<N: Real> BoundingVolume<N> for AABB<N> {
#[inline]
fn center(&self) -> Point<N> {
self.center()
}
#[inline]
fn intersects(&self, other: &AABB<N>) -> bool {
na::partial_le(&self.mins, &other.maxs) && na::partial_ge(&self.maxs, &other.mins)
}
#[inline]
fn contains(&self, other: &AABB<N>) -> bool {
na::partial_le(&self.mins, &other.mins) && na::partial_ge(&self.maxs, &other.maxs)
}
#[inline]
fn merge(&mut self, other: &AABB<N>) {
self.mins = na::inf(&self.mins, &other.mins);
self.maxs = na::sup(&self.maxs, &other.maxs);
}
#[inline]
fn merged(&self, other: &AABB<N>) -> AABB<N> {
AABB {
mins: na::inf(&self.mins, &other.mins),
maxs: na::sup(&self.maxs, &other.maxs),
}
}
#[inline]
fn loosen(&mut self, amount: N) {
assert!(
amount >= na::zero(),
"The loosening margin must be positive."
);
self.mins = self.mins + Vector::repeat(-amount);
self.maxs = self.maxs + Vector::repeat(amount);
}
#[inline]
fn loosened(&self, amount: N) -> AABB<N> {
assert!(
amount >= na::zero(),
"The loosening margin must be positive."
);
AABB {
mins: self.mins + Vector::repeat(-amount),
maxs: self.maxs + Vector::repeat(amount),
}
}
#[inline]
fn tighten(&mut self, amount: N) {
assert!(
amount >= na::zero(),
"The tightening margin must be positive."
);
self.mins = self.mins + Vector::repeat(amount);
self.maxs = self.maxs + Vector::repeat(-amount);
assert!(
na::partial_le(&self.mins, &self.maxs),
"The tightening margin is to large."
);
}
#[inline]
fn tightened(&self, amount: N) -> AABB<N> {
assert!(
amount >= na::zero(),
"The tightening margin must be positive."
);
AABB::new(
self.mins + Vector::repeat(amount),
self.maxs + Vector::repeat(-amount),
)
}
}