1 #ifndef MSP_GEOMETRY_HYPERSPHERE_H_
2 #define MSP_GEOMETRY_HYPERSPHERE_H_
4 #include <msp/linal/vector.h>
12 template<typename T, unsigned D>
13 class HyperSphere: public Shape<T, D>
20 explicit HyperSphere(T);
22 virtual HyperSphere *clone() const;
24 T get_radius() const { return radius; }
26 virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
27 virtual bool check_intersection(const Ray<T, D> &) const;
30 template<typename T, unsigned D>
31 inline HyperSphere<T, D>::HyperSphere():
35 template<typename T, unsigned D>
36 inline HyperSphere<T, D>::HyperSphere(T r):
40 template<typename T, unsigned D>
41 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
43 return new HyperSphere<T, D>(radius);
46 template<typename T, unsigned D>
47 inline HyperBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
49 LinAl::Vector<T, D> dimensions;
50 for(unsigned i=0; i<D; ++i)
51 dimensions[i] = radius;
52 return HyperBox<T, D>(dimensions);
55 template<typename T, unsigned D>
56 inline bool HyperSphere<T, D>::check_intersection(const Ray<T, D> &ray) const
58 T x = inner_product(ray.get_direction(), ray.get_start());
60 return inner_product(ray.get_start(), ray.get_start())<=radius*radius;
63 LinAl::Vector<T, D> nearest = ray.get_start()-ray.get_direction()*x;
64 return inner_product(nearest, nearest)<=radius*radius;
68 } // namespace Geometry