use crate::bbox::BBox;
use crate::index::*;
use crate::mesh::topology::*;
use crate::ops::*;
use crate::Real;
use std::slice::{Iter, IterMut};
pub trait VertexPositions {
type Element;
fn vertex_positions(&self) -> &[Self::Element];
fn vertex_positions_mut(&mut self) -> &mut [Self::Element];
#[inline]
fn vertex_position_iter(&self) -> Iter<Self::Element> {
self.vertex_positions().iter()
}
#[inline]
fn vertex_position_iter_mut(&mut self) -> IterMut<Self::Element> {
self.vertex_positions_mut().iter_mut()
}
#[inline]
fn vertex_position<VI>(&self, vidx: VI) -> Self::Element
where
VI: Into<VertexIndex>,
Self::Element: Clone,
{
let idx: Index = vidx.into().into();
self.vertex_positions()[idx.unwrap()].clone()
}
}
impl<M, T: Real> BoundingBox<T> for M
where
M: VertexPositions<Element = [T; 3]>,
{
fn bounding_box(&self) -> BBox<T> {
let mut bbox = BBox::empty();
for &pos in self.vertex_position_iter() {
bbox.absorb(pos);
}
bbox
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mesh::TriMesh;
#[test]
fn triangle_bounding_box() {
let pos = vec![[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 0.0]];
let trimesh = TriMesh::new(pos, vec![[0, 1, 2]]);
let bbox = trimesh.bounding_box();
assert_eq!(bbox.min_corner(), [0.0, 0.0, 0.0]);
assert_eq!(bbox.max_corner(), [1.0, 0.0, 1.0]);
}
}