1 #ifndef MSP_GEOMETRY_HYPERSPHERE_H_
2 #define MSP_GEOMETRY_HYPERSPHERE_H_
6 #include <msp/linal/vector.h>
13 A shape consisting of the points within a specific distance from the origin.
14 Two- and three-dimensional cases are Circle and Sphere, respectively.
16 template<typename T, unsigned D>
17 class HyperSphere: public Shape<T, D>
23 HyperSphere(): radius(1) { }
24 explicit HyperSphere(T);
26 virtual HyperSphere *clone() const;
28 T get_radius() const { return radius; }
30 virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
31 virtual bool contains(const LinAl::Vector<T, D> &) const;
32 virtual unsigned get_max_ray_intersections() const { return 2; }
33 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
36 template<typename T, unsigned D>
37 inline HyperSphere<T, D>::HyperSphere(T r):
41 throw std::invalid_argument("HyperSphere::HyperShpere");
44 template<typename T, unsigned D>
45 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
47 return new HyperSphere<T, D>(radius);
50 template<typename T, unsigned D>
51 inline BoundingBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
53 LinAl::Vector<T, D> extent;
54 for(unsigned i=0; i<D; ++i)
56 return BoundingBox<T, D>(-extent, extent);
59 template<typename T, unsigned D>
60 inline bool HyperSphere<T, D>::contains(const LinAl::Vector<T, D> &point) const
62 return inner_product(point, point)<=radius*radius;
65 template<typename T, unsigned D>
66 inline unsigned HyperSphere<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
70 T mid = -inner_product(ray.get_direction(), ray.get_start());
71 LinAl::Vector<T, D> nearest = ray.get_start()+ray.get_direction()*mid;
72 T offset_sq = radius*radius-inner_product(nearest, nearest);
75 T offset = sqrt(offset_sq);
78 for(int i=-1; (n<size && i<=1); i+=2)
81 if(ray.check_limits(x))
85 points[n].position = ray.get_start()+ray.get_direction()*x;
86 points[n].normal = normalize(points[n].position);
87 points[n].distance = x;
88 points[n].entry = (i<0);
98 } // namespace Geometry