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