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 unsigned get_max_ray_intersections() const { return 2; }
37 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
40 template<typename T, unsigned D>
41 inline HyperBox<T, D>::HyperBox()
43 for(unsigned i=0; i<D; ++i)
47 template<typename T, unsigned D>
48 inline HyperBox<T, D>::HyperBox(const LinAl::Vector<T, D> &d):
51 for(unsigned i=0; i<D; ++i)
52 if(dimensions[i]<=T(0))
53 throw std::invalid_argument("HyperBox::HyperBox");
56 template<typename T, unsigned D>
57 inline HyperBox<T, D> *HyperBox<T, D>::clone() const
59 return new HyperBox<T, D>(dimensions);
62 template<typename T, unsigned D>
63 inline T HyperBox<T, D>::get_dimension(unsigned i) const
68 template<typename T, unsigned D>
69 inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
73 for(unsigned i=0; i<D; ++i)
74 if(abs(point[i])>dimensions[i]/T(2))
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
87 LinAl::Vector<T, D> half_dim = dimensions/T(2);
89 for(unsigned i=0; (n<size && i<D); ++i)
91 if(!ray.get_direction()[i])
94 for(int j=-1; (n<size && j<=1); j+=2)
96 T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
97 if(!ray.check_limits(x))
100 LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
103 for(unsigned k=0; (inside && k<D); ++k)
104 inside = (k==i || abs(p[k])<=half_dim[k]);
110 points[n].position = p;
111 points[n].normal = LinAl::Vector<T, D>();
112 points[n].normal[i] = j;
113 points[n].distance = x;
115 if(n==1 && x<points[0].distance)
116 std::swap(points[0], points[1]);
127 } // namespace Geometry