1 #ifndef MSP_GEOMETRY_HYPERBOX_H_
2 #define MSP_GEOMETRY_HYPERBOX_H_
4 #include <msp/linal/vector.h>
11 template<typename T, unsigned D>
12 class HyperBox: public Shape<T, D>
15 LinAl::Vector<T, D> dimensions;
19 explicit HyperBox(const LinAl::Vector<T, D> &);
21 virtual HyperBox *clone() const;
23 const LinAl::Vector<T, D> &get_dimensions() const { return dimensions; }
24 T get_dimension(unsigned) const;
26 virtual HyperBox<T, D> get_axis_aligned_bounding_box() const { return *this; }
27 virtual bool check_intersection(const Ray<T, D> &) const;
30 template<typename T, unsigned D>
31 inline HyperBox<T, D>::HyperBox()
33 for(unsigned i=0; i<D; ++i)
37 template<typename T, unsigned D>
38 inline HyperBox<T, D>::HyperBox(const LinAl::Vector<T, D> &d):
42 template<typename T, unsigned D>
43 inline HyperBox<T, D> *HyperBox<T, D>::clone() const
45 return new HyperBox<T, D>(dimensions);
48 template<typename T, unsigned D>
49 inline T HyperBox<T, D>::get_dimension(unsigned i) const
54 template<typename T, unsigned D>
55 inline bool HyperBox<T, D>::check_intersection(const Ray<T, D> &ray) const
57 LinAl::Vector<T, D> half_dim = dimensions/T(2);
58 for(unsigned i=0; i<D; ++i)
59 for(int j=-1; j<=1; j+=2)
61 T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
64 LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
66 for(unsigned k=0; (inside && k<D); ++k)
67 inside = (k==i || (p[k]>=-half_dim[k] && p[k]<half_dim[k]));
76 } // namespace Geometry