1#[derive(Clone, Copy, Debug, PartialEq)]
2pub struct Bounds {
3 min: [f32; 3],
4 max: [f32; 3],
5}
6
7impl Bounds {
8 pub fn new(min: [f32; 3], max: [f32; 3]) -> Self {
9 assert!(
10 min.iter().chain(max.iter()).all(|value| value.is_finite()),
11 "bounds are finite"
12 );
13 assert!(
14 (0..3).all(|axis| min[axis] <= max[axis]),
15 "bounds are ordered"
16 );
17 Self { min, max }
18 }
19
20 pub fn min(self) -> [f32; 3] {
21 self.min
22 }
23
24 pub fn max(self) -> [f32; 3] {
25 self.max
26 }
27
28 pub fn center(self) -> [f32; 3] {
29 [0, 1, 2].map(|axis| 0.5 * (self.min[axis] + self.max[axis]))
30 }
31
32 pub fn size(self) -> [f32; 3] {
33 [0, 1, 2].map(|axis| self.max[axis] - self.min[axis])
34 }
35
36 pub fn contains(self, point: [f32; 3]) -> bool {
37 (0..3).all(|axis| self.min[axis] <= point[axis] && point[axis] <= self.max[axis])
38 }
39}