]> git.tdb.fi Git - libs/math.git/blob - source/geometry/hypersphere.h
Put the common #includes in shape.h
[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 };
35
36 template<typename T, unsigned D>
37 inline HyperSphere<T, D>::HyperSphere(T r):
38         radius(r)
39 {
40         if(r<=T(0))
41                 throw std::invalid_argument("HyperSphere::HyperShpere");
42 }
43
44 template<typename T, unsigned D>
45 inline HyperSphere<T, D> *HyperSphere<T, D>::clone() const
46 {
47         return new HyperSphere<T, D>(radius);
48 }
49
50 template<typename T, unsigned D>
51 inline BoundingBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
52 {
53         LinAl::Vector<T, D> extent;
54         for(unsigned i=0; i<D; ++i)
55                 extent[i] = radius;
56         return BoundingBox<T, D>(-extent, extent);
57 }
58
59 template<typename T, unsigned D>
60 inline bool HyperSphere<T, D>::contains(const LinAl::Vector<T, D> &point) const
61 {
62         return inner_product(point, point)<=radius*radius;
63 }
64
65 template<typename T, unsigned D>
66 inline unsigned HyperSphere<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
67 {
68         using std::sqrt;
69
70         T mid = -inner_product(ray.get_direction(), ray.get_start());
71         LinAl::Vector<T, D> nearest = ray.get_start()+ray.get_direction()*mid;
72         T offset_sq = radius*radius-inner_product(nearest, nearest);
73         if(offset_sq<0)
74                 return 0;
75         T offset = sqrt(offset_sq);
76
77         unsigned n = 0;
78         for(int i=-1; (n<size && i<=1); i+=2)
79         {
80                 T x = mid+offset*i;
81                 if(ray.check_limits(x))
82                 {
83                         if(points)
84                         {
85                                 points[n].position = ray.get_start()+ray.get_direction()*x;
86                                 points[n].normal = normalize(points[n].position);
87                                 points[n].distance = x;
88                         }
89
90                         ++n;
91                 }
92         }
93
94         return n;
95 }
96
97 } // namespace Geometry
98 } // namespace Msp
99
100 #endif