From: Mikko Rasa Date: Tue, 18 Apr 2017 06:04:02 +0000 (+0300) Subject: Add a function to test whether a shapes covers a bounding box X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=a29d2c17ca70a3ed5df1e863c92ccd851d5dba6b;p=libs%2Fmath.git Add a function to test whether a shapes covers a bounding box --- diff --git a/source/geometry/compositeshape.h b/source/geometry/compositeshape.h index e9aeadc..00ae55d 100644 --- a/source/geometry/compositeshape.h +++ b/source/geometry/compositeshape.h @@ -38,6 +38,7 @@ public: virtual bool contains(const LinAl::Vector &) const; virtual unsigned get_max_ray_intersections() const { return max_isect; } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; + virtual Coverage get_coverage(const BoundingBox &) const; }; template @@ -190,6 +191,23 @@ inline unsigned CompositeShape::get_intersections(const Ray &ray, return n; } +template +inline Coverage CompositeShape::get_coverage(const BoundingBox &bbox) const +{ + Coverage coverage = NO_COVERAGE; + for(typename ShapeArray::const_iterator i=shapes.begin(); i!=shapes.end(); ++i) + { + Coverage c = (*i)->get_coverage(bbox); + if(i==shapes.begin() || Ops::shortcircuit(c>coverage)) + coverage = c; + + if(coverage!=PARTIAL_COVERAGE && Ops::shortcircuit(coverage==FULL_COVERAGE)) + break; + } + + return coverage; +} + } // namespace Geometry } // namespace Msp diff --git a/source/geometry/extrudedshape.h b/source/geometry/extrudedshape.h index 78dfa49..e702f90 100644 --- a/source/geometry/extrudedshape.h +++ b/source/geometry/extrudedshape.h @@ -35,6 +35,7 @@ public: virtual bool contains(const LinAl::Vector &) const; virtual unsigned get_max_ray_intersections() const; virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; + virtual Coverage get_coverage(const BoundingBox &) const; }; template @@ -187,6 +188,24 @@ inline unsigned ExtrudedShape::get_intersections(const Ray &ray, Sur return n; } +template +inline Coverage ExtrudedShape::get_coverage(const BoundingBox &bbox) const +{ + T half_length = length/T(2); + if(bbox.get_maximum_coordinate(D-1)<-half_length || bbox.get_minimum_coordinate(D-1)>half_length) + return NO_COVERAGE; + + BoundingBox base_bbox(bbox.get_minimum_point().template slice(0), bbox.get_maximum_point().template slice(0)); + Coverage coverage = base->get_coverage(base_bbox); + if(coverage==NO_COVERAGE) + return NO_COVERAGE; + + if(bbox.get_minimum_coordinate(D-1)<-half_length || bbox.get_maximum_coordinate(D-1)>half_length) + return PARTIAL_COVERAGE; + else + return coverage; +} + } // namespace Geometry } // namespace Msp diff --git a/source/geometry/halfspace.h b/source/geometry/halfspace.h index b439851..09ec62c 100644 --- a/source/geometry/halfspace.h +++ b/source/geometry/halfspace.h @@ -28,6 +28,7 @@ public: virtual bool contains(const LinAl::Vector &) const; virtual unsigned get_max_ray_intersections() const { return 1; } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; + virtual Coverage get_coverage(const BoundingBox &) const; }; template @@ -85,6 +86,25 @@ inline unsigned HalfSpace::get_intersections(const Ray &ray, Surface return 0; } +template +inline Coverage HalfSpace::get_coverage(const BoundingBox &bbox) const +{ + LinAl::Vector low_point; + LinAl::Vector high_point; + for(unsigned i=0; i=T(0) ? bbox.get_minimum_coordinate(i) : bbox.get_maximum_coordinate(i)); + high_point[i] = (normal[i]>=T(0) ? bbox.get_maximum_coordinate(i) : bbox.get_minimum_coordinate(i)); + } + + if(contains(high_point)) + return FULL_COVERAGE; + else if(contains(low_point)) + return PARTIAL_COVERAGE; + else + return NO_COVERAGE; +} + } // namespace Geometry } // namespace Msp diff --git a/source/geometry/hyperbox.h b/source/geometry/hyperbox.h index b9fad33..c3bac57 100644 --- a/source/geometry/hyperbox.h +++ b/source/geometry/hyperbox.h @@ -33,6 +33,7 @@ public: 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 @@ -139,6 +140,25 @@ inline unsigned HyperBox::get_intersections(const Ray &ray, SurfaceP return n; } +template +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 coverage; +} + } // namespace Geometry } // namespace Msp diff --git a/source/geometry/hypersphere.h b/source/geometry/hypersphere.h index 6bb4352..8f21db1 100644 --- a/source/geometry/hypersphere.h +++ b/source/geometry/hypersphere.h @@ -31,6 +31,7 @@ public: 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 @@ -95,6 +96,41 @@ inline unsigned HyperSphere::get_intersections(const Ray &ray, Surfa return n; } +template +inline Coverage HyperSphere::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 far_point; + for(unsigned i=0; iT(0)) + spanned_dimensions |= 1< point; + for(unsigned j=0; j>j)&1)) + point[j] = ((i>>j)&1 ? max_pt[j] : min_pt[j]); + + if(contains(point)) + return PARTIAL_COVERAGE; + } + + return NO_COVERAGE; +} + } // namespace Geometry } // namespace Msp diff --git a/source/geometry/negation.h b/source/geometry/negation.h index 8e2b3f4..d90c4de 100644 --- a/source/geometry/negation.h +++ b/source/geometry/negation.h @@ -30,6 +30,7 @@ public: virtual bool contains(const LinAl::Vector &) const; virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; + virtual Coverage get_coverage(const BoundingBox &) const; }; template @@ -86,6 +87,18 @@ inline unsigned Negation::get_intersections(const Ray &ray, SurfaceP return count; } +template +inline Coverage Negation::get_coverage(const BoundingBox &bbox) const +{ + Coverage coverage = shape->get_coverage(bbox); + if(coverage==FULL_COVERAGE) + return NO_COVERAGE; + else if(coverage==NO_COVERAGE) + return FULL_COVERAGE; + else + return PARTIAL_COVERAGE; +} + } // namespace Geometry } // namespace Msp diff --git a/source/geometry/shape.h b/source/geometry/shape.h index 22b1887..ded8f13 100644 --- a/source/geometry/shape.h +++ b/source/geometry/shape.h @@ -10,6 +10,13 @@ namespace Msp { namespace Geometry { +enum Coverage +{ + NO_COVERAGE, + PARTIAL_COVERAGE, + FULL_COVERAGE +}; + /** Base class and interface for geometric shapes. Shapes may be bounded or unbounded. They are always considered to be solid, i.e. have a distinct inside @@ -31,6 +38,7 @@ public: virtual unsigned get_max_ray_intersections() const = 0; virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const = 0; std::vector > get_intersections(const Ray &) const; + virtual Coverage get_coverage(const BoundingBox &) const = 0; }; template diff --git a/source/geometry/transformedshape.h b/source/geometry/transformedshape.h index 6a14b0b..91790fc 100644 --- a/source/geometry/transformedshape.h +++ b/source/geometry/transformedshape.h @@ -33,6 +33,7 @@ public: virtual bool contains(const LinAl::Vector &) const; virtual unsigned get_max_ray_intersections() const { return shape->get_max_ray_intersections(); } virtual unsigned get_intersections(const Ray &, SurfacePoint *, unsigned) const; + virtual Coverage get_coverage(const BoundingBox &) const; }; template @@ -102,6 +103,12 @@ inline unsigned TransformedShape::get_intersections(const Ray &ray, return count; } +template +inline Coverage TransformedShape::get_coverage(const BoundingBox &bbox) const +{ + return shape->get_coverage(inverse_trans.transform(bbox)); +} + } // namespace Geometry } // namespace Msp