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(unsigned = 0) 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;
34 virtual Coverage get_coverage(const BoundingBox<T, D> &) const;
37 template<typename T, unsigned D>
38 inline HyperSphere<T, D>::HyperSphere(T r):
42 throw std::invalid_argument("HyperSphere::HyperShpere");
45 template<typename T, unsigned D>
46 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
48 return new HyperSphere<T, D>(radius);
51 template<typename T, unsigned D>
52 inline BoundingBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box(unsigned) const
54 LinAl::Vector<T, D> extent;
55 for(unsigned i=0; i<D; ++i)
57 return BoundingBox<T, D>(-extent, extent);
60 template<typename T, unsigned D>
61 inline bool HyperSphere<T, D>::contains(const LinAl::Vector<T, D> &point) const
63 return inner_product(point, point)<=radius*radius;
66 template<typename T, unsigned D>
67 inline unsigned HyperSphere<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
71 T mid = -inner_product(ray.get_direction(), ray.get_start());
72 LinAl::Vector<T, D> nearest = ray.get_start()+ray.get_direction()*mid;
73 T offset_sq = radius*radius-inner_product(nearest, nearest);
76 T offset = sqrt(offset_sq);
79 for(int i=-1; (n<size && i<=1); i+=2)
82 if(ray.check_limits(x))
86 points[n].position = ray.get_start()+ray.get_direction()*x;
87 points[n].normal = normalize(points[n].position);
88 points[n].distance = x;
89 points[n].entry = (i<0);
99 template<typename T, unsigned D>
100 inline Coverage HyperSphere<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
102 const LinAl::Vector<T, D> &min_pt = bbox.get_minimum_point();
103 const LinAl::Vector<T, D> &max_pt = bbox.get_maximum_point();
105 LinAl::Vector<T, D> far_point;
106 for(unsigned i=0; i<D; ++i)
107 far_point[i] = std::max(std::abs(min_pt[i]), std::abs(max_pt[i]));
109 if(contains(far_point))
110 return FULL_COVERAGE;
112 unsigned spanned_dimensions = 0;
113 for(unsigned i=0; i<D; ++i)
114 if(min_pt[i]<T(0) && max_pt[i]>T(0))
115 spanned_dimensions |= 1<<i;
117 for(unsigned i=0; i<(1<<D); ++i)
119 if(i&spanned_dimensions)
122 LinAl::Vector<T, D> point;
123 for(unsigned j=0; j<D; ++j)
124 if(!((spanned_dimensions>>j)&1))
125 point[j] = ((i>>j)&1 ? max_pt[j] : min_pt[j]);
128 return PARTIAL_COVERAGE;
134 } // namespace Geometry