use manifold_csg_sys::*;
pub struct Rect {
pub(crate) ptr: *mut ManifoldRect,
}
unsafe impl Send for Rect {}
unsafe impl Sync for Rect {}
impl Clone for Rect {
fn clone(&self) -> Self {
Self::new(self.min(), self.max())
}
}
impl Drop for Rect {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { manifold_delete_rect(self.ptr) };
}
}
}
impl std::fmt::Debug for Rect {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Rect")
.field("min", &self.min())
.field("max", &self.max())
.finish()
}
}
impl Rect {
#[must_use]
pub fn new(min: [f64; 2], max: [f64; 2]) -> Self {
let ptr = unsafe { manifold_alloc_rect() };
unsafe {
manifold_rect(ptr, min[0], min[1], max[0], max[1]);
}
Self { ptr }
}
pub(crate) fn from_ptr(ptr: *mut ManifoldRect) -> Self {
Self { ptr }
}
#[must_use]
pub fn min(&self) -> [f64; 2] {
let v = unsafe { manifold_rect_min(self.ptr) };
[v.x, v.y]
}
#[must_use]
pub fn max(&self) -> [f64; 2] {
let v = unsafe { manifold_rect_max(self.ptr) };
[v.x, v.y]
}
#[must_use]
pub fn dimensions(&self) -> [f64; 2] {
let v = unsafe { manifold_rect_dimensions(self.ptr) };
[v.x, v.y]
}
#[must_use]
pub fn center(&self) -> [f64; 2] {
let v = unsafe { manifold_rect_center(self.ptr) };
[v.x, v.y]
}
#[must_use]
pub fn scale(&self) -> f64 {
unsafe { manifold_rect_scale(self.ptr) }
}
#[must_use]
pub fn is_empty(&self) -> bool {
unsafe { manifold_rect_is_empty(self.ptr) != 0 }
}
#[must_use]
pub fn is_finite(&self) -> bool {
unsafe { manifold_rect_is_finite(self.ptr) != 0 }
}
#[must_use]
pub fn contains_point(&self, point: [f64; 2]) -> bool {
unsafe { manifold_rect_contains_pt(self.ptr, point[0], point[1]) != 0 }
}
#[must_use]
pub fn contains_rect(&self, other: &Rect) -> bool {
unsafe { manifold_rect_contains_rect(self.ptr, other.ptr) != 0 }
}
#[must_use]
pub fn overlaps_rect(&self, other: &Rect) -> bool {
unsafe { manifold_rect_does_overlap_rect(self.ptr, other.ptr) != 0 }
}
pub fn include_point(&mut self, point: [f64; 2]) {
unsafe { manifold_rect_include_pt(self.ptr, point[0], point[1]) };
}
#[must_use]
pub fn union(&self, other: &Rect) -> Rect {
let ptr = unsafe { manifold_alloc_rect() };
unsafe { manifold_rect_union(ptr, self.ptr, other.ptr) };
Rect { ptr }
}
#[must_use]
pub fn transform(&self, m: &[f64; 6]) -> Rect {
let ptr = unsafe { manifold_alloc_rect() };
unsafe {
manifold_rect_transform(ptr, self.ptr, m[0], m[1], m[2], m[3], m[4], m[5]);
}
Rect { ptr }
}
#[must_use]
pub fn translate(&self, v: [f64; 2]) -> Rect {
let ptr = unsafe { manifold_alloc_rect() };
unsafe { manifold_rect_translate(ptr, self.ptr, v[0], v[1]) };
Rect { ptr }
}
#[must_use]
pub fn mul(&self, v: [f64; 2]) -> Rect {
let ptr = unsafe { manifold_alloc_rect() };
unsafe { manifold_rect_mul(ptr, self.ptr, v[0], v[1]) };
Rect { ptr }
}
}