]> git.tdb.fi Git - libs/math.git/blob - source/geometry/hyperbox.h
Make the check_intersection function non-virtual
[libs/math.git] / source / geometry / hyperbox.h
1 #ifndef MSP_GEOMETRY_HYPERBOX_H_
2 #define MSP_GEOMETRY_HYPERBOX_H_
3
4 #include <algorithm>
5 #include <cmath>
6 #include <stdexcept>
7 #include <msp/linal/vector.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 bounded by planar faces at right angles to each other.  Two- and three-
17 dimensional cases are Rectangle and Box, respectively.
18 */
19 template<typename T, unsigned D>
20 class HyperBox: public Shape<T, D>
21 {
22 private:
23         LinAl::Vector<T, D> dimensions;
24
25 public:
26         HyperBox();
27         explicit HyperBox(const LinAl::Vector<T, D> &);
28
29         virtual HyperBox *clone() const;
30
31         const LinAl::Vector<T, D> &get_dimensions() const { return dimensions; }
32         T get_dimension(unsigned) const;
33
34         virtual HyperBox<T, D> get_axis_aligned_bounding_box() const { return *this; }
35         virtual bool contains(const LinAl::Vector<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 HyperBox<T, D>::HyperBox()
42 {
43         for(unsigned i=0; i<D; ++i)
44                 dimensions[i] = 1;
45 }
46
47 template<typename T, unsigned D>
48 inline HyperBox<T, D>::HyperBox(const LinAl::Vector<T, D> &d):
49         dimensions(d)
50 {
51         for(unsigned i=0; i<D; ++i)
52                 if(dimensions[i]<=T(0))
53                         throw std::invalid_argument("HyperBox::HyperBox");
54 }
55
56 template<typename T, unsigned D>
57 inline HyperBox<T, D> *HyperBox<T, D>::clone() const
58 {
59         return new HyperBox<T, D>(dimensions);
60 }
61
62 template<typename T, unsigned D>
63 inline T HyperBox<T, D>::get_dimension(unsigned i) const
64 {
65         return dimensions[i];
66 }
67
68 template<typename T, unsigned D>
69 inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
70 {
71         using std::abs;
72
73         for(unsigned i=0; i<D; ++i)
74                 if(abs(point[i])>dimensions[i]/T(2))
75                         return false;
76         return true;
77 }
78
79 template<typename T, unsigned D>
80 inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
81 {
82         using std::abs;
83
84         if(size>2)
85                 size = 2;
86
87         LinAl::Vector<T, D> half_dim = dimensions/T(2);
88         unsigned n = 0;
89         for(unsigned i=0; (n<size && i<D); ++i)
90         {
91                 if(!ray.get_direction()[i])
92                         continue;
93
94                 for(int j=-1; (n<size && j<=1); j+=2)
95                 {
96                         T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
97                         if(!ray.check_limits(x))
98                                 continue;
99
100                         LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
101
102                         bool inside = true;
103                         for(unsigned k=0; (inside && k<D); ++k)
104                                 inside = (k==i || abs(p[k])<=half_dim[k]);
105
106                         if(inside)
107                         {
108                                 if(points)
109                                 {
110                                         points[n].position = p;
111                                         points[n].normal = LinAl::Vector<T, D>();
112                                         points[n].normal[i] = j;
113                                         points[n].distance = x;
114
115                                         if(n==1 && x<points[0].distance)
116                                                 std::swap(points[0], points[1]);
117                                 }
118
119                                 ++n;
120                         }
121                 }
122         }
123
124         return n;
125 }
126
127 } // namespace Geometry
128 } // namespace Msp
129
130 #endif