]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/extrudedshape.h
Add a function to test whether a shapes covers a bounding box
[libs/math.git] / source / geometry / extrudedshape.h
index 84addc46e7dde3fee65dea0283f56f2c0b29fc91..e702f90a7a0f844d93f417b53d38a8c6aea2359a 100644 (file)
@@ -35,6 +35,7 @@ public:
        virtual bool contains(const LinAl::Vector<T, D> &) const;
        virtual unsigned get_max_ray_intersections() const;
        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>
@@ -116,7 +117,7 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
        {
                T offset = T();
                T limit = T();
-               if(ray.get_direction()[D-1]!=T(0))
+               if(ray_direction[D-1]!=T(0))
                {
                        offset = (half_length-ray_start[D-1])/ray_direction[D-1];
                        limit = (-half_length-ray_start[D-1])/ray_direction[D-1];
@@ -147,6 +148,7 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
                                        points[n].position = ray_start+ray_direction*x;
                                        points[n].normal = compose(base_points[i].normal, T(0));
                                        points[n].distance = x;
+                                       points[n].entry = base_points[i].entry;
                                }
 
                                ++n;
@@ -156,7 +158,7 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
 
        /* If the ray is not parallel to the base space, it may pass through the
        caps. */
-       if(n<size && ray_direction[D-1])
+       if(n<size && ray_direction[D-1]!=T(0))
        {
                for(int i=-1; (n<size && i<=1); i+=2)
                {
@@ -173,6 +175,7 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
                                        points[n].normal = LinAl::Vector<T, D>();
                                        points[n].normal[D-1] = i;
                                        points[n].distance = x;
+                                       points[n].entry = (T(i)*ray_direction[D-1]<T(0));
                                }
 
                                ++n;
@@ -185,6 +188,24 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
        return n;
 }
 
+template<typename T, unsigned D>
+inline Coverage ExtrudedShape<T, D>::get_coverage(const BoundingBox<T, D> &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<T, D-1> base_bbox(bbox.get_minimum_point().template slice<D-1>(0), bbox.get_maximum_point().template slice<D-1>(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