]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/extrudedshape.h
Use the coverage function to calculate tighter bounding boxes
[libs/math.git] / source / geometry / extrudedshape.h
index 83933b0808a02f32d8febcce5eee8c8a23ad85d0..fd4597b9b1a5c935f50a3eb3a6e02296cd3f3a9f 100644 (file)
@@ -31,17 +31,18 @@ public:
        const Shape<T, D-1> &get_base() const { return *base; }
        T get_length() const { return length; }
 
-       virtual HyperBox<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;
        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 ExtrudedShape<T, D>::ExtrudedShape(const Shape<T, D-1> &b, T l):
        length(l)
 {
-       if(l<=0)
+       if(l<=T(0))
                throw std::invalid_argument("ExtrudedShape::ExtrudedShape");
 
        base = b.clone();
@@ -74,10 +75,12 @@ inline ExtrudedShape<T, D> *ExtrudedShape<T, D>::clone() const
 }
 
 template<typename T, unsigned D>
-inline HyperBox<T, D> ExtrudedShape<T, D>::get_axis_aligned_bounding_box() const
+inline BoundingBox<T, D> ExtrudedShape<T, D>::get_axis_aligned_bounding_box(unsigned detail) const
 {
-       HyperBox<T, D-1> base_bbox = base->get_axis_aligned_bounding_box();
-       return HyperBox<T, D>(LinAl::Vector<T, D>(base_bbox.get_dimensions(), length));
+       BoundingBox<T, D-1> base_bbox = base->get_axis_aligned_bounding_box(detail);
+       T half_length = length/T(2);
+       return BoundingBox<T, D>(compose(base_bbox.get_minimum_point(), -half_length),
+               compose(base_bbox.get_maximum_point(), half_length));
 }
 
 template<typename T, unsigned D>
@@ -88,7 +91,7 @@ inline bool ExtrudedShape<T, D>::contains(const LinAl::Vector<T, D> &point) cons
        if(abs(point[D-1])>length/T(2))
                return false;
 
-       return base->contains(LinAl::Vector<T, D-1>(point));
+       return base->contains(point.template slice<D-1>(0));
 }
 
 template<typename T, unsigned D>
@@ -106,7 +109,7 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
        T half_length = length/T(2);
        const LinAl::Vector<T, D> &ray_start = ray.get_start();
        const LinAl::Vector<T, D> &ray_direction = ray.get_direction();
-       LinAl::Vector<T, D-1> base_dir(ray_direction);
+       LinAl::Vector<T, D-1> base_dir = ray_direction.template slice<D-1>(0);
 
        /* If the ray does not degenerate to a point in the base space, it could
        intersect the base shape. */
@@ -114,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];
@@ -123,34 +126,39 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
                        if(offset<T(0))
                                offset = T(0);
                }
-               T distortion = base_dir.norm();
-               Ray<T, D-1> base_ray(LinAl::Vector<T, D-1>(ray_start+ray_direction*offset),
-                       base_dir, (limit-offset)*distortion);
-
-               SurfacePoint<T, D-1> *base_points = 0;
-               if(points)
-                       /* Shamelessly reuse the provided storage.  Align to the end of the array
-                       so processing can start from the first (nearest) point. */
-                       base_points = reinterpret_cast<SurfacePoint<T, D-1> *>(points+size)-size;
-
-               unsigned count = base->get_intersections(base_ray, base_points, size);
-               for(unsigned i=0; (n<size && i<count); ++i)
+
+               if(limit>=offset)
                {
+                       T distortion = base_dir.norm();
+                       Ray<T, D-1> base_ray((ray_start+ray_direction*offset).template slice<D-1>(0),
+                               base_dir, (limit-offset)*distortion);
+
+                       SurfacePoint<T, D-1> *base_points = 0;
                        if(points)
+                               /* Shamelessly reuse the provided storage.  Align to the end of the
+                               array so processing can start from the first (nearest) point. */
+                               base_points = reinterpret_cast<SurfacePoint<T, D-1> *>(points+size)-size;
+
+                       unsigned count = base->get_intersections(base_ray, base_points, size);
+                       for(unsigned i=0; (n<size && i<count); ++i)
                        {
-                               T x = offset+base_points[i].distance/distortion;
-                               points[n].position = ray_start+ray_direction*x;
-                               points[n].normal = LinAl::Vector<T, D>(base_points[i].normal, T(0));
-                               points[n].distance = x;
-                       }
+                               if(points)
+                               {
+                                       T x = offset+base_points[i].distance/distortion;
+                                       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;
+                               ++n;
+                       }
                }
        }
 
        /* 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)
                {
@@ -159,7 +167,7 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
                                continue;
 
                        LinAl::Vector<T, D> p = ray_start+ray_direction*x;
-                       if(base->contains(LinAl::Vector<T, D-1>(p)))
+                       if(base->contains(p.template slice<D-1>(0)))
                        {
                                if(points)
                                {
@@ -167,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;
@@ -179,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