1 #ifndef MSP_GEOMETRY_HYPERBOX_H_
2 #define MSP_GEOMETRY_HYPERBOX_H_
7 #include <msp/linal/vector.h>
10 #include "surfacepoint.h"
16 A shape bounded by planar faces at right angles to each other. Two- and three-
17 dimensional cases are Rectangle and Box, respectively.
19 template<typename T, unsigned D>
20 class HyperBox: public Shape<T, D>
23 LinAl::Vector<T, D> dimensions;
27 explicit HyperBox(const LinAl::Vector<T, D> &);
29 virtual HyperBox *clone() const;
31 const LinAl::Vector<T, D> &get_dimensions() const { return dimensions; }
32 T get_dimension(unsigned) const;
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 bool check_intersection(const Ray<T, D> &) const;
37 virtual unsigned get_max_ray_intersections() const { return 2; }
38 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
41 template<typename T, unsigned D>
42 inline HyperBox<T, D>::HyperBox()
44 for(unsigned i=0; i<D; ++i)
48 template<typename T, unsigned D>
49 inline HyperBox<T, D>::HyperBox(const LinAl::Vector<T, D> &d):
52 for(unsigned i=0; i<D; ++i)
53 if(dimensions[i]<=T(0))
54 throw std::invalid_argument("HyperBox::HyperBox");
57 template<typename T, unsigned D>
58 inline HyperBox<T, D> *HyperBox<T, D>::clone() const
60 return new HyperBox<T, D>(dimensions);
63 template<typename T, unsigned D>
64 inline T HyperBox<T, D>::get_dimension(unsigned i) const
69 template<typename T, unsigned D>
70 inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
74 for(unsigned i=0; i<D; ++i)
75 if(abs(point[i])>dimensions[i]/T(2))
80 template<typename T, unsigned D>
81 inline bool HyperBox<T, D>::check_intersection(const Ray<T, D> &ray) const
83 return get_intersections(ray, 0, 1);
86 template<typename T, unsigned D>
87 inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
91 LinAl::Vector<T, D> half_dim = dimensions/T(2);
93 for(unsigned i=0; i<D; ++i)
95 if(!ray.get_direction()[i])
98 for(int j=-1; j<=1; j+=2)
100 T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
101 if(!ray.check_limits(x))
104 LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
107 for(unsigned k=0; (inside && k<D); ++k)
108 inside = (k==i || abs(p[k])<=half_dim[k]);
114 points[n].position = p;
115 points[n].normal = LinAl::Vector<T, D>();
116 points[n].normal[i] = j;
117 points[n].distance = x;
119 if(n==1 && x<points[0].distance)
120 std::swap(points[0], points[1]);
133 } // namespace Geometry