]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/halfspace.h
Use the coverage function to calculate tighter bounding boxes
[libs/math.git] / source / geometry / halfspace.h
index 5fdea34c885eaf2dbf17296b07d4677984589fef..9fdcdadfbd02b0dec41ef88c3e97539013cf2cbd 100644 (file)
@@ -7,8 +7,8 @@ namespace Msp {
 namespace Geometry {
 
 /**
-An infinite shape consisting of the space on one side of a plane.  Mostly
-useful when composited with other shapes.
+An unbounded shape consisting of the space on one side of a plane.  Mostly
+useful when intersected with other shapes.
 */
 template<typename T, unsigned D>
 class HalfSpace: public Shape<T, D>
@@ -24,16 +24,17 @@ public:
 
        const LinAl::Vector<T, D> &get_normal() const { return normal; }
 
-       virtual BoundingBox<T, D> get_axis_aligned_bounding_box() const;
+       virtual BoundingBox<T, D> get_axis_aligned_bounding_box(unsigned = 0) const;
        virtual bool contains(const LinAl::Vector<T, D> &) const;
        virtual unsigned get_max_ray_intersections() const { return 1; }
        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>
 inline HalfSpace<T, D>::HalfSpace()
 {
-       normal[0] = 1;
+       normal[0] = T(1);
 }
 
 template<typename T, unsigned D>
@@ -48,7 +49,7 @@ inline HalfSpace<T, D> *HalfSpace<T, D>::clone() const
 }
 
 template<typename T, unsigned D>
-inline BoundingBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box() const
+inline BoundingBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box(unsigned) const
 {
        // XXX If the normal is aligned to an axis, should the bounding box reflect that?
        return ~BoundingBox<T, D>();
@@ -57,13 +58,18 @@ inline BoundingBox<T, D> HalfSpace<T, D>::get_axis_aligned_bounding_box() const
 template<typename T, unsigned D>
 inline bool HalfSpace<T, D>::contains(const LinAl::Vector<T, D> &point) const
 {
-       return inner_product(point, normal)<=0;
+       return inner_product(point, normal)<=T(0);
 }
 
 template<typename T, unsigned D>
 inline unsigned HalfSpace<T, D>::get_intersections(const Ray<T, D> &ray, SurfacePoint<T, D> *points, unsigned size) const
 {
-       T x = -inner_product(ray.get_start(), normal)/inner_product(ray.get_direction(), normal);
+       T d = inner_product(ray.get_start(), normal);
+       T c = inner_product(ray.get_direction(), normal);
+       if(c==T(0))
+               return 0;
+
+       T x = -d/c;
        if(ray.check_limits(x))
        {
                if(points && size>0)
@@ -71,6 +77,7 @@ inline unsigned HalfSpace<T, D>::get_intersections(const Ray<T, D> &ray, Surface
                        points[0].position = ray.get_start()+ray.get_direction()*x;
                        points[0].normal = normal;
                        points[0].distance = x;
+                       points[0].entry = (c<T(0));
                }
 
                return 1;
@@ -79,6 +86,25 @@ inline unsigned HalfSpace<T, D>::get_intersections(const Ray<T, D> &ray, Surface
        return 0;
 }
 
+template<typename T, unsigned D>
+inline Coverage HalfSpace<T, D>::get_coverage(const BoundingBox<T, D> &bbox) const
+{
+       LinAl::Vector<T, D> low_point;
+       LinAl::Vector<T, D> high_point;
+       for(unsigned i=0; i<D; ++i)
+       {
+               low_point[i] = (normal[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