1 #ifndef MSP_GEOMETRY_HYPERSPHERE_H_
2 #define MSP_GEOMETRY_HYPERSPHERE_H_
4 #include <msp/linal/vector.h>
8 #include "surfacepoint.h"
13 template<typename T, unsigned D>
14 class HyperSphere: public Shape<T, D>
21 explicit HyperSphere(T);
23 virtual HyperSphere *clone() const;
25 T get_radius() const { return radius; }
27 virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
28 virtual bool contains(const LinAl::Vector<T, D> &) const;
29 virtual bool check_intersection(const Ray<T, D> &) const;
30 virtual unsigned get_max_ray_intersections() const { return 2; }
31 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
34 template<typename T, unsigned D>
35 inline HyperSphere<T, D>::HyperSphere():
39 template<typename T, unsigned D>
40 inline HyperSphere<T, D>::HyperSphere(T r):
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 HyperBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
53 LinAl::Vector<T, D> dimensions;
54 for(unsigned i=0; i<D; ++i)
55 dimensions[i] = radius;
56 return HyperBox<T, D>(dimensions);
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 bool HyperSphere<T, D>::check_intersection(const Ray<T, D> &ray) const
68 T x = inner_product(ray.get_direction(), ray.get_start());
70 return contains(ray.get_start());
72 return contains(ray.get_start()-ray.get_direction()*x);
75 template<typename T, unsigned D>
76 inline unsigned HyperSphere<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
78 T mid = -inner_product(ray.get_direction(), ray.get_start());
79 LinAl::Vector<T, D> nearest = ray.get_start()+ray.get_direction()*mid;
80 T offset_sq = radius*radius-inner_product(nearest, nearest);
83 T offset = sqrt(offset_sq);
86 for(int i=-1; i<=1; i+=2)
93 points[n].position = ray.get_start()+ray.get_direction()*x;
94 points[n].normal = normalize(points[n].position);
106 } // namespace Geometry