#include <algorithm>
#include <cmath>
+#include <stdexcept>
#include <msp/linal/vector.h>
-#include "ray.h"
#include "shape.h"
-#include "surfacepoint.h"
namespace Msp {
namespace Geometry {
+/**
+A shape bounded by planar faces at right angles to each other. Two- and three-
+dimensional cases are Rectangle and Box, respectively.
+*/
template<typename T, unsigned D>
class HyperBox: public Shape<T, D>
{
const LinAl::Vector<T, D> &get_dimensions() const { return dimensions; }
T get_dimension(unsigned) const;
- virtual HyperBox<T, D> get_axis_aligned_bounding_box() const { return *this; }
+ virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned = 0) const;
virtual bool contains(const LinAl::Vector<T, D> &) const;
- virtual bool check_intersection(const Ray<T, D> &) const;
virtual unsigned get_max_ray_intersections() const { return 2; }
virtual unsigned get_intersections(const Ray<T, D> &, SurfacePoint<T, D> *, unsigned) const;
+ virtual Coverage get_coverage(const BoundingBox<T, D> &) const;
};
template<typename T, unsigned D>
template<typename T, unsigned D>
inline HyperBox<T, D>::HyperBox(const LinAl::Vector<T, D> &d):
dimensions(d)
-{ }
+{
+ for(unsigned i=0; i<D; ++i)
+ if(dimensions[i]<=T(0))
+ throw std::invalid_argument("HyperBox::HyperBox");
+}
template<typename T, unsigned D>
inline HyperBox<T, D> *HyperBox<T, D>::clone() const
}
template<typename T, unsigned D>
-inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
+inline BoundingBox<T, D> HyperBox<T, D>::get_axis_aligned_bounding_box(unsigned) const
{
- for(unsigned i=0; i<D; ++i)
- if(abs(point[i])>dimensions[i]/2)
- return false;
- return true;
+ LinAl::Vector<T, D> half_dim = dimensions/T(2);
+ return BoundingBox<T, D>(-half_dim, half_dim);
}
template<typename T, unsigned D>
-inline bool HyperBox<T, D>::check_intersection(const Ray<T, D> &ray) const
+inline bool HyperBox<T, D>::contains(const LinAl::Vector<T, D> &point) const
{
- return get_intersections(ray, 0, 1);
+ using std::abs;
+
+ for(unsigned i=0; i<D; ++i)
+ if(abs(point[i])>dimensions[i]/T(2))
+ return false;
+ return true;
}
template<typename T, unsigned D>
{
using std::abs;
+ if(size>2)
+ size = 2;
+
LinAl::Vector<T, D> half_dim = dimensions/T(2);
unsigned n = 0;
- T first_depth = T();
- for(unsigned i=0; i<D; ++i)
+ for(unsigned i=0; (n<size && i<D); ++i)
{
if(!ray.get_direction()[i])
continue;
- for(int j=-1; j<=1; j+=2)
+ for(int j=-1; (n<size && j<=1); j+=2)
{
T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
- if(x<0)
+ if(!ray.check_limits(x))
continue;
LinAl::Vector<T, D> p = ray.get_start()+ray.get_direction()*x;
for(unsigned k=0; (inside && k<D); ++k)
inside = (k==i || abs(p[k])<=half_dim[k]);
- if(inside && n<size)
+ if(inside)
{
if(points)
{
- points[n].position = p;
- points[n].normal = LinAl::Vector<T, D>();
- points[n].normal[i] = j;
- if(n==0)
- first_depth = x;
- else if(n==1 && x<first_depth)
- std::swap(points[0], points[1]);
+ bool entry = (T(j)*ray.get_direction()[i]<T(0));
+ unsigned k = 0;
+ if(n>0 && entry!=points[0].entry)
+ {
+ if(entry)
+ points[1] = points[0];
+ else
+ ++k;
+ }
+ if(k<n && entry==points[k].entry)
+ --n;
+
+ points[k].position = p;
+ points[k].normal = LinAl::Vector<T, D>();
+ points[k].normal[i] = j;
+ points[k].distance = x;
+ points[k].entry = (T(j)*ray.get_direction()[i]<T(0));
}
++n;
- if(n==size || n==2)
- return n;
}
}
}
return n;
}
+template<typename T, unsigned D>
+inline Coverage HyperBox<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
+{
+ const LinAl::Vector<T, D> &min_pt = bbox.get_minimum_point();
+ const LinAl::Vector<T, D> &max_pt = bbox.get_maximum_point();
+ LinAl::Vector<T, D> half_dim = dimensions/T(2);
+
+ Coverage coverage = FULL_COVERAGE;
+ for(unsigned i=0; i<D; ++i)
+ {
+ if(max_pt[i]<-half_dim[i] || min_pt[i]>half_dim[i])
+ return NO_COVERAGE;
+ if(min_pt[i]<-half_dim[i] || max_pt[i]>half_dim[i])
+ coverage = PARTIAL_COVERAGE;
+ }
+
+ return coverage;
+}
+
} // namespace Geometry
} // namespace Msp