1 #ifndef MSP_GEOMETRY_HYPERBOX_H_
2 #define MSP_GEOMETRY_HYPERBOX_H_
7 #include <msp/linal/vector.h>
14 A shape bounded by planar faces at right angles to each other. Two- and three-
15 dimensional cases are Rectangle and Box, respectively.
17 template<typename T, unsigned D>
18 class HyperBox: public Shape<T, D>
21 LinAl::Vector<T, D> dimensions;
25 explicit HyperBox(const LinAl::Vector<T, D> &);
27 virtual HyperBox *clone() const;
29 const LinAl::Vector<T, D> &get_dimensions() const { return dimensions; }
30 T get_dimension(unsigned) const;
32 virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
33 virtual bool contains(const LinAl::Vector<T, D> &) const;
34 virtual unsigned get_max_ray_intersections() const { return 2; }
35 virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
38 template<typename T, unsigned D>
39 inline HyperBox<T, D>::HyperBox()
41 for(unsigned i=0; i<D; ++i)
45 template<typename T, unsigned D>
46 inline HyperBox<T, D>::HyperBox(const LinAl::Vector<T, D> &d):
49 for(unsigned i=0; i<D; ++i)
50 if(dimensions[i]<=T(0))
51 throw std::invalid_argument("HyperBox::HyperBox");
54 template<typename T, unsigned D>
55 inline HyperBox<T, D> *HyperBox<T, D>::clone() const
57 return new HyperBox<T, D>(dimensions);
60 template<typename T, unsigned D>
61 inline T HyperBox<T, D>::get_dimension(unsigned i) const
66 template<typename T, unsigned D>
67 inline BoundingBox<T, D> HyperBox<T, D>::get_axis_aligned_bounding_box() const
69 LinAl::Vector<T, D> half_dim = dimensions/T(2);
70 return BoundingBox<T, D>(-half_dim, half_dim);
73 template<typename T, unsigned D>
74 inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
78 for(unsigned i=0; i<D; ++i)
79 if(abs(point[i])>dimensions[i]/T(2))
84 template<typename T, unsigned D>
85 inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
92 LinAl::Vector<T, D> half_dim = dimensions/T(2);
94 for(unsigned i=0; (n<size && i<D); ++i)
96 if(!ray.get_direction()[i])
99 for(int j=-1; (n<size && j<=1); j+=2)
101 T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
102 if(!ray.check_limits(x))
105 LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
108 for(unsigned k=0; (inside && k<D); ++k)
109 inside = (k==i || abs(p[k])<=half_dim[k]);
115 bool entry = (T(j)*ray.get_direction()[i]<T(0));
117 if(n>0 && entry!=points[0].entry)
120 points[1] = points[0];
124 if(k<n && entry==points[k].entry)
127 points[k].position = p;
128 points[k].normal = LinAl::Vector<T, D>();
129 points[k].normal[i] = j;
130 points[k].distance = x;
131 points[k].entry = (T(j)*ray.get_direction()[i]<T(0));
142 } // namespace Geometry