X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fgeometry%2Fhyperbox.h;h=c3bac57d97e3464d20535a66928106079777801a;hb=a29d2c17ca70a3ed5df1e863c92ccd851d5dba6b;hp=2ade28acddd42ba9da6eefcb8cbefe14040ee535;hpb=6ff13022b53830d35283905d562c2ef3af198cc1;p=libs%2Fmath.git diff --git a/source/geometry/hyperbox.h b/source/geometry/hyperbox.h index 2ade28a..c3bac57 100644 --- a/source/geometry/hyperbox.h +++ b/source/geometry/hyperbox.h @@ -1,13 +1,19 @@ #ifndef MSP_GEOMETRY_HYPERBOX_H_ #define MSP_GEOMETRY_HYPERBOX_H_ +#include +#include +#include #include -#include "ray.h" #include "shape.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 class HyperBox: public Shape { @@ -23,8 +29,11 @@ public: const LinAl::Vector &get_dimensions() const { return dimensions; } T get_dimension(unsigned) const; - virtual HyperBox get_axis_aligned_bounding_box() const { return *this; } - virtual bool check_intersection(const Ray &) const; + virtual BoundingBox get_axis_aligned_bounding_box() const; + virtual bool contains(const LinAl::Vector &) const; + virtual unsigned get_max_ray_intersections() const { return 2; } + virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; + virtual Coverage get_coverage(const BoundingBox &) const; }; template @@ -37,7 +46,11 @@ inline HyperBox::HyperBox() template inline HyperBox::HyperBox(const LinAl::Vector &d): dimensions(d) -{ } +{ + for(unsigned i=0; i inline HyperBox *HyperBox::clone() const @@ -52,25 +65,98 @@ inline T HyperBox::get_dimension(unsigned i) const } template -inline bool HyperBox::check_intersection(const Ray &ray) const +inline BoundingBox HyperBox::get_axis_aligned_bounding_box() const { LinAl::Vector half_dim = dimensions/T(2); + return BoundingBox(-half_dim, half_dim); +} + +template +inline bool HyperBox::contains(const LinAl::Vector &point) const +{ + using std::abs; + for(unsigned i=0; idimensions[i]/T(2)) + return false; + return true; +} + +template +inline unsigned HyperBox::get_intersections(const Ray &ray, SurfacePoint *points, unsigned size) const +{ + using std::abs; + + if(size>2) + size = 2; + + LinAl::Vector half_dim = dimensions/T(2); + unsigned n = 0; + for(unsigned i=0; (n0) + if(!ray.check_limits(x)) + continue; + + LinAl::Vector p = ray.get_start()+ray.get_direction()*x; + + bool inside = true; + for(unsigned k=0; (inside && k p = ray.get_start()+ray.get_direction()*x; - bool inside = true; - for(unsigned k=0; (inside && k=-half_dim[k] && p[k]0 && entry!=points[0].entry) + { + if(entry) + points[1] = points[0]; + else + ++k; + } + if(k(); + points[k].normal[i] = j; + points[k].distance = x; + points[k].entry = (T(j)*ray.get_direction()[i] +inline Coverage HyperBox::get_coverage(const BoundingBox &bbox) const +{ + const LinAl::Vector &min_pt = bbox.get_minimum_point(); + const LinAl::Vector &max_pt = bbox.get_maximum_point(); + LinAl::Vector half_dim = dimensions/T(2); + + Coverage coverage = FULL_COVERAGE; + for(unsigned i=0; ihalf_dim[i]) + return NO_COVERAGE; + if(min_pt[i]<-half_dim[i] || max_pt[i]>half_dim[i]) + coverage = PARTIAL_COVERAGE; + } - return false; + return coverage; } } // namespace Geometry