clay/shape/
sphere.rs

1use std::collections::HashSet;
2use nalgebra::{Vector3};
3use crate::{
4    prelude::*,
5    map::*,
6    shape::*,
7};
8
9
10type SphereBase = ShapeMapper<UnitSphere, Chain<Scale, Shift>>;
11/// Shape obtained by scaling and shifting the unit sphere.
12pub struct Sphere(pub SphereBase);
13
14impl Sphere {
15    pub fn new(rad: f64, pos: Vector3<f64>) -> Self {
16        Self::from(UnitSphere::new().map(Scale::from(rad).chain(Shift::from(pos))))
17    }
18}
19impl From<SphereBase> for Sphere {
20    fn from(base: SphereBase) -> Self {
21        Self(base)
22    }
23}
24
25impl Shape for Sphere {}
26
27impl Instance<ShapeClass> for Sphere {
28    fn source(cache: &mut HashSet<u64>) -> String { SphereBase::source(cache) }
29    fn inst_name() -> String { SphereBase::inst_name() }
30}
31
32impl Pack for Sphere {
33    fn size_int() -> usize { SphereBase::size_int() }
34    fn size_float() -> usize { SphereBase::size_float() }
35    fn pack_to(&self, buffer_int: &mut [i32], buffer_float: &mut [f32]) {
36        self.0.pack_to(buffer_int, buffer_float);
37    }
38}
39
40impl Bound for Sphere {}
41impl Instance<BoundClass> for Sphere {
42    fn source(cache: &mut HashSet<u64>) -> String { UnitSphere::source(cache) }
43    fn inst_name() -> String { "sphere".to_string() }
44}
45
46impl Target for Sphere {}
47impl Instance<TargetClass> for Sphere {
48    fn source(cache: &mut HashSet<u64>) -> String { UnitSphere::source(cache) }
49    fn inst_name() -> String { "sphere_target".to_string() }
50}