Skip to main content

threers_math/
box2.rs

1use super::Vector2;
2
3/// Axis-aligned bounding box in 2D. Mirrors three.js's `Box2`.
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct Box2 {
6    pub min: Vector2,
7    pub max: Vector2,
8}
9
10impl Default for Box2 {
11    fn default() -> Self {
12        Self::empty()
13    }
14}
15
16impl Box2 {
17    pub const fn new(min: Vector2, max: Vector2) -> Self {
18        Self { min, max }
19    }
20
21    pub fn empty() -> Self {
22        Self {
23            min: Vector2::new(f32::INFINITY, f32::INFINITY),
24            max: Vector2::new(f32::NEG_INFINITY, f32::NEG_INFINITY),
25        }
26    }
27
28    pub fn is_empty(&self) -> bool {
29        self.max.x < self.min.x || self.max.y < self.min.y
30    }
31
32    pub fn from_points(points: &[Vector2]) -> Self {
33        let mut b = Self::empty();
34        for p in points {
35            b.expand_by_point(*p);
36        }
37        b
38    }
39
40    pub fn from_center_and_size(center: Vector2, size: Vector2) -> Self {
41        let half = size * 0.5;
42        Self {
43            min: center - half,
44            max: center + half,
45        }
46    }
47
48    pub fn expand_by_point(&mut self, p: Vector2) -> &mut Self {
49        self.min = self.min.min(p);
50        self.max = self.max.max(p);
51        self
52    }
53
54    pub fn expand_by_vector(&mut self, v: Vector2) -> &mut Self {
55        self.min = self.min - v;
56        self.max = self.max + v;
57        self
58    }
59
60    pub fn expand_by_scalar(&mut self, s: f32) -> &mut Self {
61        self.expand_by_vector(Vector2::new(s, s))
62    }
63
64    pub fn center(&self) -> Vector2 {
65        if self.is_empty() {
66            Vector2::ZERO
67        } else {
68            (self.min + self.max) * 0.5
69        }
70    }
71
72    pub fn size(&self) -> Vector2 {
73        if self.is_empty() {
74            Vector2::ZERO
75        } else {
76            self.max - self.min
77        }
78    }
79
80    pub fn contains_point(&self, p: Vector2) -> bool {
81        p.x >= self.min.x && p.x <= self.max.x && p.y >= self.min.y && p.y <= self.max.y
82    }
83
84    pub fn contains_box(&self, other: &Self) -> bool {
85        self.min.x <= other.min.x
86            && other.max.x <= self.max.x
87            && self.min.y <= other.min.y
88            && other.max.y <= self.max.y
89    }
90
91    pub fn intersects_box(&self, other: &Self) -> bool {
92        !(other.max.x < self.min.x
93            || other.min.x > self.max.x
94            || other.max.y < self.min.y
95            || other.min.y > self.max.y)
96    }
97
98    pub fn clamp_point(&self, p: Vector2) -> Vector2 {
99        p.clamp(self.min, self.max)
100    }
101
102    pub fn distance_to_point(&self, p: Vector2) -> f32 {
103        self.clamp_point(p).distance_to(p)
104    }
105
106    pub fn union(&self, other: &Self) -> Self {
107        Self {
108            min: self.min.min(other.min),
109            max: self.max.max(other.max),
110        }
111    }
112
113    pub fn intersect(&self, other: &Self) -> Self {
114        Self {
115            min: self.min.max(other.min),
116            max: self.max.min(other.max),
117        }
118    }
119
120    pub fn translate(&self, offset: Vector2) -> Self {
121        Self {
122            min: self.min + offset,
123            max: self.max + offset,
124        }
125    }
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131
132    #[test]
133    fn from_points_encloses_all() {
134        let pts = [Vector2::new(-1.0, 2.0), Vector2::new(3.0, -2.0)];
135        let b = Box2::from_points(&pts);
136        assert_eq!(b.min, Vector2::new(-1.0, -2.0));
137        assert_eq!(b.max, Vector2::new(3.0, 2.0));
138    }
139}