]> git.tdb.fi Git - libs/math.git/blob - source/geometry/hypersphere.h
8f21db1d78f40689cda50c40d7ccd18cb3aa93b8
[libs/math.git] / source / geometry / hypersphere.h
1 #ifndef MSP_GEOMETRY_HYPERSPHERE_H_
2 #define MSP_GEOMETRY_HYPERSPHERE_H_
3
4 #include <cmath>
5 #include <stdexcept>
6 #include <msp/linal/vector.h>
7 #include "shape.h"
8
9 namespace Msp {
10 namespace Geometry {
11
12 /**
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.
15 */
16 template<typename T, unsigned D>
17 class HyperSphere: public Shape<T, D>
18 {
19 private:
20         T radius;
21
22 public:
23         HyperSphere(): radius(1) { }
24         explicit HyperSphere(T);
25
26         virtual HyperSphere *clone() const;
27
28         T get_radius() const { return radius; }
29
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;
34         virtual Coverage get_coverage(const BoundingBox<T, D> &) const;
35 };
36
37 template<typename T, unsigned D>
38 inline HyperSphere<T, D>::HyperSphere(T r):
39         radius(r)
40 {
41         if(r<=T(0))
42                 throw std::invalid_argument("HyperSphere::HyperShpere");
43 }
44
45 template<typename T, unsigned D>
46 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
47 {
48         return new HyperSphere<T, D>(radius);
49 }
50
51 template<typename T, unsigned D>
52 inline BoundingBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
53 {
54         LinAl::Vector<T, D> extent;
55         for(unsigned i=0; i<D; ++i)
56                 extent[i] = radius;
57         return BoundingBox<T, D>(-extent, extent);
58 }
59
60 template<typename T, unsigned D>
61 inline bool HyperSphere<T, D>::contains(const LinAl::Vector<T, D> &point) const
62 {
63         return inner_product(point, point)<=radius*radius;
64 }
65
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
68 {
69         using std::sqrt;
70
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);
74         if(offset_sq<T(0))
75                 return 0;
76         T offset = sqrt(offset_sq);
77
78         unsigned n = 0;
79         for(int i=-1; (n<size && i<=1); i+=2)
80         {
81                 T x = mid+offset*i;
82                 if(ray.check_limits(x))
83                 {
84                         if(points)
85                         {
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);
90                         }
91
92                         ++n;
93                 }
94         }
95
96         return n;
97 }
98
99 template<typename T, unsigned D>
100 inline Coverage HyperSphere<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
101 {
102         const LinAl::Vector<T, D> &min_pt = bbox.get_minimum_point();
103         const LinAl::Vector<T, D> &max_pt = bbox.get_maximum_point();
104
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]));
108
109         if(contains(far_point))
110                 return FULL_COVERAGE;
111
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;
116
117         for(unsigned i=0; i<(1<<D); ++i)
118         {
119                 if(i&spanned_dimensions)
120                         continue;
121
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]);
126
127                 if(contains(point))
128                         return PARTIAL_COVERAGE;
129         }
130
131         return NO_COVERAGE;
132 }
133
134 } // namespace Geometry
135 } // namespace Msp
136
137 #endif