use manifold_csg_sys::*;
pub struct BoundingBox {
pub(crate) ptr: *mut ManifoldBox,
}
unsafe impl Send for BoundingBox {}
unsafe impl Sync for BoundingBox {}
impl Clone for BoundingBox {
fn clone(&self) -> Self {
Self::new(self.min(), self.max())
}
}
impl Drop for BoundingBox {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { manifold_delete_box(self.ptr) };
}
}
}
impl std::fmt::Debug for BoundingBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BoundingBox")
.field("min", &self.min())
.field("max", &self.max())
.finish()
}
}
impl BoundingBox {
#[must_use]
pub fn new(min: [f64; 3], max: [f64; 3]) -> Self {
let ptr = unsafe { manifold_alloc_box() };
unsafe {
manifold_box(ptr, min[0], min[1], min[2], max[0], max[1], max[2]);
}
Self { ptr }
}
#[must_use]
pub fn min(&self) -> [f64; 3] {
let v = unsafe { manifold_box_min(self.ptr) };
[v.x, v.y, v.z]
}
#[must_use]
pub fn max(&self) -> [f64; 3] {
let v = unsafe { manifold_box_max(self.ptr) };
[v.x, v.y, v.z]
}
#[must_use]
pub fn dimensions(&self) -> [f64; 3] {
let v = unsafe { manifold_box_dimensions(self.ptr) };
[v.x, v.y, v.z]
}
#[must_use]
pub fn center(&self) -> [f64; 3] {
let v = unsafe { manifold_box_center(self.ptr) };
[v.x, v.y, v.z]
}
#[must_use]
pub fn scale(&self) -> f64 {
unsafe { manifold_box_scale(self.ptr) }
}
#[must_use]
pub fn is_empty(&self) -> bool {
let d = self.dimensions();
d[0] <= 0.0 || d[1] <= 0.0 || d[2] <= 0.0
}
#[must_use]
pub fn is_finite(&self) -> bool {
unsafe { manifold_box_is_finite(self.ptr) != 0 }
}
#[must_use]
pub fn contains_point(&self, point: [f64; 3]) -> bool {
unsafe { manifold_box_contains_pt(self.ptr, point[0], point[1], point[2]) != 0 }
}
#[must_use]
pub fn contains_box(&self, other: &BoundingBox) -> bool {
unsafe { manifold_box_contains_box(self.ptr, other.ptr) != 0 }
}
#[must_use]
pub fn overlaps_point(&self, point: [f64; 3]) -> bool {
unsafe { manifold_box_does_overlap_pt(self.ptr, point[0], point[1], point[2]) != 0 }
}
#[must_use]
pub fn overlaps_box(&self, other: &BoundingBox) -> bool {
unsafe { manifold_box_does_overlap_box(self.ptr, other.ptr) != 0 }
}
pub fn include_point(&mut self, point: [f64; 3]) {
unsafe { manifold_box_include_pt(self.ptr, point[0], point[1], point[2]) };
}
#[must_use]
pub fn union(&self, other: &BoundingBox) -> BoundingBox {
let ptr = unsafe { manifold_alloc_box() };
unsafe { manifold_box_union(ptr, self.ptr, other.ptr) };
BoundingBox { ptr }
}
#[must_use]
pub fn transform(&self, m: &[f64; 12]) -> BoundingBox {
let ptr = unsafe { manifold_alloc_box() };
unsafe {
manifold_box_transform(
ptr, self.ptr, m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10],
m[11],
);
}
BoundingBox { ptr }
}
#[must_use]
pub fn translate(&self, v: [f64; 3]) -> BoundingBox {
let ptr = unsafe { manifold_alloc_box() };
unsafe { manifold_box_translate(ptr, self.ptr, v[0], v[1], v[2]) };
BoundingBox { ptr }
}
#[must_use]
pub fn mul(&self, v: [f64; 3]) -> BoundingBox {
let ptr = unsafe { manifold_alloc_box() };
unsafe { manifold_box_mul(ptr, self.ptr, v[0], v[1], v[2]) };
BoundingBox { ptr }
}
}