]> git.tdb.fi Git - libs/math.git/blob - source/geometry/hypersphere.h
Verify that parameters make sense
[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 "hyperbox.h"
8 #include "ray.h"
9 #include "shape.h"
10 #include "surfacepoint.h"
11
12 namespace Msp {
13 namespace Geometry {
14
15 /**
16 A shape consisting of the points within a specific distance from the origin.
17 Two- and three-dimensional cases are Circle and Sphere, respectively.
18 */
19 template<typename T, unsigned D>
20 class HyperSphere: public Shape<T, D>
21 {
22 private:
23         T radius;
24
25 public:
26         HyperSphere();
27         explicit HyperSphere(T);
28
29         virtual HyperSphere *clone() const;
30
31         T get_radius() const { return radius; }
32
33         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
34         virtual bool contains(const LinAl::Vector<T, D> &) const;
35         virtual bool check_intersection(const Ray<T, D> &) const;
36         virtual unsigned get_max_ray_intersections() const { return 2; }
37         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
38 };
39
40 template<typename T, unsigned D>
41 inline HyperSphere<T, D>::HyperSphere():
42         radius(1)
43 { }
44
45 template<typename T, unsigned D>
46 inline HyperSphere<T, D>::HyperSphere(T r):
47         radius(r)
48 {
49         if(r<=T(0))
50                 throw std::invalid_argument("HyperSphere::HyperShpere");
51 }
52
53 template<typename T, unsigned D>
54 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
55 {
56         return new HyperSphere<T, D>(radius);
57 }
58
59 template<typename T, unsigned D>
60 inline HyperBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
61 {
62         LinAl::Vector<T, D> dimensions;
63         for(unsigned i=0; i<D; ++i)
64                 dimensions[i] = radius;
65         return HyperBox<T, D>(dimensions);
66 }
67
68 template<typename T, unsigned D>
69 inline bool HyperSphere<T, D>::contains(const LinAl::Vector<T, D> &point) const
70 {
71         return inner_product(point, point)<=radius*radius;
72 }
73
74 template<typename T, unsigned D>
75 inline bool HyperSphere<T, D>::check_intersection(const Ray<T, D> &ray) const
76 {
77         T x = inner_product(ray.get_direction(), ray.get_start());
78         if(x>0)
79                 return contains(ray.get_start());
80         else
81                 return contains(ray.get_start()-ray.get_direction()*x);
82 }
83
84 template<typename T, unsigned D>
85 inline unsigned HyperSphere<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
86 {
87         using std::sqrt;
88
89         T mid = -inner_product(ray.get_direction(), ray.get_start());
90         LinAl::Vector<T, D> nearest = ray.get_start()+ray.get_direction()*mid;
91         T offset_sq = radius*radius-inner_product(nearest, nearest);
92         if(offset_sq<0)
93                 return 0;
94         T offset = sqrt(offset_sq);
95
96         unsigned n = 0;
97         for(int i=-1; i<=1; i+=2)
98         {
99                 T x = mid+offset*i;
100                 if(ray.check_limits(x) && n<size)
101                 {
102                         if(points)
103                         {
104                                 points[n].position = ray.get_start()+ray.get_direction()*x;
105                                 points[n].normal = normalize(points[n].position);
106                                 points[n].distance = x;
107                         }
108
109                         ++n;
110                         if(n==size)
111                                 return n;
112                 }
113         }
114
115         return n;
116 }
117
118 } // namespace Geometry
119 } // namespace Msp
120
121 #endif