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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use super::math::*;

///
/// A bounding box that aligns with the x, y and z axes.
///
#[derive(Debug, Copy, Clone)]
pub struct AxisAlignedBoundingBox {
    min: Vec3,
    max: Vec3,
}

impl AxisAlignedBoundingBox {
    /// An empty bounding box.
    pub const EMPTY: Self = Self {
        min: Vec3::new(std::f32::INFINITY, std::f32::INFINITY, std::f32::INFINITY),
        max: Vec3::new(
            std::f32::NEG_INFINITY,
            std::f32::NEG_INFINITY,
            std::f32::NEG_INFINITY,
        ),
    };

    /// An infinitely large bounding box.
    pub const INFINITE: Self = Self {
        min: Vec3::new(
            std::f32::NEG_INFINITY,
            std::f32::NEG_INFINITY,
            std::f32::NEG_INFINITY,
        ),
        max: Vec3::new(std::f32::INFINITY, std::f32::INFINITY, std::f32::INFINITY),
    };

    ///
    /// Constructs a new bounding box and expands it such that all of the given positions are contained inside the bounding box.
    ///
    pub fn new_with_positions(positions: &[Vec3]) -> Self {
        let mut aabb = Self::EMPTY;
        aabb.expand(positions);
        aabb
    }

    ///
    /// Constructs a new bounding box and expands it such that all of the given positions transformed with the given transformation are contained inside the bounding box.
    /// A position consisting of an x, y and z coordinate corresponds to three consecutive value in the positions array.
    ///
    pub fn new_with_transformed_positions(positions: &[Vec3], transformation: &Mat4) -> Self {
        let mut aabb = Self::EMPTY;
        aabb.expand_with_transformation(positions, transformation);
        aabb
    }

    ///
    /// Returns true if the bounding box is empty (ie. constructed by [AxisAlignedBoundingBox::EMPTY]).
    ///
    pub fn is_empty(&self) -> bool {
        self.max.x == f32::NEG_INFINITY
    }

    ///
    /// Returns true if the bounding box is infinitely large (ie. constructed by [AxisAlignedBoundingBox::INFINITE]).
    ///
    pub fn is_infinite(&self) -> bool {
        self.max.x == f32::INFINITY
    }

    ///
    /// Get the minimum coordinate of the bounding box.
    ///
    pub fn min(&self) -> Vec3 {
        self.min
    }

    ///
    /// Get the maximum coordinate of the bounding box.
    ///
    pub fn max(&self) -> Vec3 {
        self.max
    }

    ///
    /// Get the center of the bounding box.
    ///
    pub fn center(&self) -> Vec3 {
        if self.is_infinite() {
            Vec3::new(0.0, 0.0, 0.0)
        } else {
            0.5 * self.max + 0.5 * self.min
        }
    }

    ///
    /// Get the size of the bounding box.
    ///
    pub fn size(&self) -> Vec3 {
        self.max - self.min
    }

    ///
    /// Expands the bounding box such that all of the given positions are contained inside the bounding box.
    /// A position consisting of an x, y and z coordinate corresponds to three consecutive value in the positions array.
    ///
    pub fn expand(&mut self, positions: &[Vec3]) {
        for p in positions {
            self.min.x = self.min.x.min(p.x);
            self.min.y = self.min.y.min(p.y);
            self.min.z = self.min.z.min(p.z);

            self.max.x = self.max.x.max(p.x);
            self.max.y = self.max.y.max(p.y);
            self.max.z = self.max.z.max(p.z);
        }
    }

    ///
    /// Expands the bounding box such that all of the given positions transformed with the given transformation are contained inside the bounding box.
    /// A position consisting of an x, y and z coordinate corresponds to three consecutive value in the positions array.
    ///
    pub fn expand_with_transformation(&mut self, positions: &[Vec3], transformation: &Mat4) {
        self.expand(
            &positions
                .iter()
                .map(|p| (transformation * p.extend(1.0)).truncate())
                .collect::<Vec<_>>(),
        )
    }

    ///
    /// Expand the bounding box such that it also contains the given other bounding box.
    ///
    pub fn expand_with_aabb(&mut self, other: &AxisAlignedBoundingBox) {
        self.min = Vec3::new(
            f32::min(self.min.x, other.min.x),
            f32::min(self.min.y, other.min.y),
            f32::min(self.min.z, other.min.z),
        );
        self.max = Vec3::new(
            f32::max(self.max.x, other.max.x),
            f32::max(self.max.y, other.max.y),
            f32::max(self.max.z, other.max.z),
        );
    }

    ///
    /// Transforms the bounding box by the given transformation.
    ///
    /// **Note:** Use [new_with_transformed_positions](crate::AxisAlignedBoundingBox::new_with_transformed_positions) instead of
    /// [new_with_positions](crate::AxisAlignedBoundingBox::new_with_positions) followed by this method to create a more tight bounding box.
    ///
    pub fn transform(&mut self, transformation: &Mat4) {
        let aabb = Self::new_with_transformed_positions(
            &[
                self.min,
                Vec3::new(self.max.x, self.min.y, self.min.z),
                Vec3::new(self.min.x, self.max.y, self.min.z),
                Vec3::new(self.min.x, self.min.y, self.max.z),
                Vec3::new(self.min.x, self.max.y, self.max.z),
                Vec3::new(self.max.x, self.min.y, self.max.z),
                Vec3::new(self.max.x, self.max.y, self.min.z),
                self.max,
            ],
            transformation,
        );
        self.min = aabb.min;
        self.max = aabb.max;
    }

    ///
    /// The distance from position to the point in this bounding box that is closest to position.
    ///
    pub fn distance(&self, position: &Vec3) -> f32 {
        let x = (self.min.x - position.x)
            .max(position.x - self.max.x)
            .max(0.0);
        let y = (self.min.y - position.y)
            .max(position.y - self.max.y)
            .max(0.0);
        let z = (self.min.z - position.z)
            .max(position.z - self.max.z)
            .max(0.0);
        let d2 = x * x + y * y + z * z;
        if d2 > 0.001 {
            d2.sqrt()
        } else {
            d2
        }
    }

    ///
    /// The distance from position to the point in this bounding box that is furthest away from position.
    ///
    pub fn distance_max(&self, position: &Vec3) -> f32 {
        let x = (position.x - self.min.x)
            .abs()
            .max((self.max.x - position.x).abs());
        let y = (position.y - self.min.y)
            .abs()
            .max((self.max.y - position.y).abs());
        let z = (position.z - self.min.z)
            .abs()
            .max((self.max.z - position.z).abs());
        let d2 = x * x + y * y + z * z;
        if d2 > 0.001 {
            d2.sqrt()
        } else {
            d2
        }
    }
}