]> git.tdb.fi Git - libs/math.git/blob - source/geometry/hypersphere.h
Add more collision check functions for shapes
[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 template<typename T, unsigned D>
14 class HyperSphere: public Shape<T, D>
15 {
16 private:
17         T radius;
18
19 public:
20         HyperSphere();
21         explicit HyperSphere(T);
22
23         virtual HyperSphere *clone() const;
24
25         T get_radius() const { return radius; }
26
27         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const;
28         virtual bool contains(const LinAl::Vector<T, D> &) const;
29         virtual bool check_intersection(const Ray<T, D> &) const;
30         virtual unsigned get_max_ray_intersections() const { return 2; }
31         virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
32 };
33
34 template<typename T, unsigned D>
35 inline HyperSphere<T, D>::HyperSphere():
36         radius(1)
37 { }
38
39 template<typename T, unsigned D>
40 inline HyperSphere<T, D>::HyperSphere(T r):
41         radius(r)
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 HyperBox<T, D> HyperSphere<T, D>::get_axis_aligned_bounding_box() const
52 {
53         LinAl::Vector<T, D> dimensions;
54         for(unsigned i=0; i<D; ++i)
55                 dimensions[i] = radius;
56         return HyperBox<T, D>(dimensions);
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 bool HyperSphere<T, D>::check_intersection(const Ray<T, D> &ray) const
67 {
68         T x = inner_product(ray.get_direction(), ray.get_start());
69         if(x>0)
70                 return contains(ray.get_start());
71         else
72                 return contains(ray.get_start()-ray.get_direction()*x);
73 }
74
75 template<typename T, unsigned D>
76 inline unsigned HyperSphere<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
77 {
78         T mid = -inner_product(ray.get_direction(), ray.get_start());
79         LinAl::Vector<T, D> nearest = ray.get_start()+ray.get_direction()*mid;
80         T offset_sq = radius*radius-inner_product(nearest, nearest);
81         if(offset_sq<0)
82                 return 0;
83         T offset = sqrt(offset_sq);
84
85         unsigned n = 0;
86         for(int i=-1; i<=1; i+=2)
87         {
88                 T x = mid+offset*i;
89                 if(x>0 && n<size)
90                 {
91                         if(points)
92                         {
93                                 points[n].position = ray.get_start()+ray.get_direction()*x;
94                                 points[n].normal = normalize(points[n].position);
95                         }
96
97                         ++n;
98                         if(n==size)
99                                 return n;
100                 }
101         }
102
103         return n;
104 }
105
106 } // namespace Geometry
107 } // namespace Msp
108
109 #endif