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