use collide::Bounded;
use collide_sphere::Sphere;
use inner_space::InnerSpace;
use scalars::{One, Zero};
use crate::Convex;
impl<V: Copy + InnerSpace> Bounded<Sphere<V>> for Convex<V> {
fn bounding_volume(&self) -> Sphere<V> {
if self.points.is_empty() {
return Sphere::new(V::zero(), V::Scalar::zero());
}
let mut count = V::Scalar::zero();
let sum = self.points.iter().copied().fold(V::zero(), |a, b| {
count = count + V::Scalar::one();
a + b
});
let center = sum / count;
let max_distance = self
.points
.iter()
.map(|&p| (p - center).magnitude())
.fold(V::Scalar::zero(), |a, b| if a > b { a } else { b });
Sphere::new(center, max_distance + self.rad)
}
}