1 #ifndef MSP_GEOMETRY_HYPERBOX_H_
2 #define MSP_GEOMETRY_HYPERBOX_H_
6 #include <msp/linal/vector.h>
9 #include "surfacepoint.h"
15 A shape bounded by planar faces at right angles to each other. Two- and three-
16 dimensional cases are Rectangle and Box, respectively.
18 template<typename T, unsigned D>
19 class HyperBox: public Shape<T, D>
22 LinAl::Vector<T, D> dimensions;
26 explicit HyperBox(const LinAl::Vector<T, D> &);
28 virtual HyperBox *clone() const;
30 const LinAl::Vector<T, D> &get_dimensions() const { return dimensions; }
31 T get_dimension(unsigned) const;
33 virtual HyperBox<T, D> get_axis_aligned_bounding_box() const { return *this; }
34 virtual bool contains(const LinAl::Vector<T, D> &) const;
35 virtual bool check_intersection(const Ray<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):
52 template<typename T, unsigned D>
53 inline HyperBox<T, D> *HyperBox<T, D>::clone() const
55 return new HyperBox<T, D>(dimensions);
58 template<typename T, unsigned D>
59 inline T HyperBox<T, D>::get_dimension(unsigned i) const
64 template<typename T, unsigned D>
65 inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
67 for(unsigned i=0; i<D; ++i)
68 if(abs(point[i])>dimensions[i]/2)
73 template<typename T, unsigned D>
74 inline bool HyperBox<T, D>::check_intersection(const Ray<T, D> &ray) const
76 return get_intersections(ray, 0, 1);
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
84 LinAl::Vector<T, D> half_dim = dimensions/T(2);
86 for(unsigned i=0; i<D; ++i)
88 if(!ray.get_direction()[i])
91 for(int j=-1; j<=1; j+=2)
93 T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
97 LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
100 for(unsigned k=0; (inside && k<D); ++k)
101 inside = (k==i || abs(p[k])<=half_dim[k]);
107 points[n].position = p;
108 points[n].normal = LinAl::Vector<T, D>();
109 points[n].normal[i] = j;
110 points[n].distance = x;
112 if(n==1 && x<points[0].distance)
113 std::swap(points[0], points[1]);
126 } // namespace Geometry