66 lines
1.8 KiB
Rust
66 lines
1.8 KiB
Rust
//! Module containing the bounding box struct.
|
|
|
|
use math::Number;
|
|
|
|
macro_rules! make_bounding_box {
|
|
|
|
($name: ident, $vector: ident, $size: expr) => {
|
|
|
|
use math::vector::$vector;
|
|
|
|
/// Represents a bounding box.
|
|
pub struct $name<T> {
|
|
min: $vector<T>,
|
|
max: $vector<T>,
|
|
}
|
|
|
|
impl<T: PartialOrd + Copy + Clone> $name<T> {
|
|
/// Creates a bounding box from its min and max coordinates.
|
|
pub fn new(min: $vector<T>, max: $vector<T>) -> $name<T> {
|
|
$name {
|
|
min: min,
|
|
max: max,
|
|
}
|
|
}
|
|
|
|
/// Enlarges the bounding box so it contains the point passed as parameter.
|
|
pub fn add_point(&mut self, point: &$vector<T>) {
|
|
for i in 0..$size {
|
|
if point[i] < self.min[i] {
|
|
self.min[i] = point[i];
|
|
}
|
|
if point[i] > self.max[i] {
|
|
self.max[i] = point[i];
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Returns the minimum value of the bounding box.
|
|
pub fn min(&self) -> $vector<T> {
|
|
self.min
|
|
}
|
|
|
|
/// Returns the maximum value of the bounding box.
|
|
pub fn max(&self) -> $vector<T> {
|
|
self.max
|
|
}
|
|
|
|
}
|
|
|
|
impl<T: Number> $name<T> {
|
|
/// Scales a bounding box from its center.
|
|
pub fn scale(&mut self, factor: T) {
|
|
let diag = self.max - self.min;
|
|
|
|
self.min -= diag * factor;
|
|
self.max += diag * factor;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
make_bounding_box!(BoundingBox2, Vector2, 2);
|
|
make_bounding_box!(BoundingBox3, Vector3, 3);
|
|
make_bounding_box!(BoundingBox4, Vector4, 4);
|