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
use std::{ fmt, ops };
use Vec3;
use BCube;
#[derive(Clone, Copy)]
pub struct BBox {
pub(crate) min: Vec3,
pub(crate) max: Vec3,
}
impl fmt::Debug for BBox {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} → {:?}", self.min, self.max)
}
}
impl ops::Sub<Vec3> for BBox {
type Output = BBox;
fn sub(self, other: Vec3) -> Self::Output {
BBox::new(self.min - other, self.max - other)
}
}
impl ops::Add<Vec3> for BBox {
type Output = BBox;
fn add(self, other: Vec3) -> Self::Output {
BBox::new(self.min + other, self.max + other)
}
}
impl Into<BCube> for BBox {
fn into(self) -> BCube {
let span = self.max - self.min;
BCube {
center: self.center(),
half_len: span.x.max(span.y).max(span.z) / 2.0,
}
}
}
impl BBox {
pub fn new(min: Vec3, max: Vec3) -> BBox {
assert!(min.x <= max.x);
assert!(min.y <= max.y);
assert!(min.z <= max.z);
BBox { min, max }
}
pub fn collide(&self, other: BBox) -> bool {
other.max.x >= self.min.x
&& self.max.x >= other.min.x
&& other.max.y >= self.min.y
&& self.max.y >= other.min.y
&& other.max.z >= self.min.z
&& self.max.z >= other.min.z
}
pub fn collide_bcube(&self, bcube: BCube) -> bool {
let (max, min) = bcube.to_point_pair();
self.collide(BBox::new(min, max))
}
pub(crate) fn bcube_sides(&self, bcube: BCube) -> (bool, bool, bool) {
let (max, min) = bcube.to_point_pair();
let cube = BBox::new(min, max);
let lt_dist = self.max.x - cube.max.x;
let rt_dist = cube.min.x - self.min.x;
let up_dist = self.max.y - cube.max.y;
let dn_dist = cube.min.y - self.min.y;
let nr_dist = self.max.z - cube.max.z;
let fr_dist = cube.min.z - self.min.z;
(rt_dist <= lt_dist, dn_dist <= up_dist, fr_dist <= nr_dist)
}
pub fn collide_vec3(&self, p: Vec3) -> bool {
(p.x >= self.min.x) &&
(p.x <= self.max.x) &&
(p.y >= self.min.y) &&
(p.y <= self.max.y) &&
(p.z >= self.min.z) &&
(p.z <= self.max.z)
}
pub fn all_points(&self) -> [Vec3; 8] {
[
Vec3::new(self.min.x, self.min.y, self.min.z),
Vec3::new(self.min.x, self.min.y, self.max.z),
Vec3::new(self.min.x, self.max.y, self.min.z),
Vec3::new(self.min.x, self.max.y, self.max.z),
Vec3::new(self.max.x, self.min.y, self.min.z),
Vec3::new(self.max.x, self.min.y, self.max.z),
Vec3::new(self.max.x, self.max.y, self.min.z),
Vec3::new(self.max.x, self.max.y, self.max.z),
]
}
pub fn side_points(&self) -> [Vec3; 6] {
let center = self.center();
[
Vec3::new(self.min.x, center.y, center.z),
Vec3::new(center.x, self.min.y, center.z),
Vec3::new(center.x, center.y, self.min.z),
Vec3::new(self.max.x, center.y, center.z),
Vec3::new(center.x, self.max.y, center.z),
Vec3::new(center.x, center.y, self.max.z),
]
}
pub fn center(&self) -> Vec3 {
Vec3::new(
(self.min.x + self.max.x) / 2.0,
(self.min.y + self.max.y) / 2.0,
(self.min.z + self.max.z) / 2.0,
)
}
}