]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/extrudedshape.h
Fix a case in ExtrudedShape with certain ray parameters
[libs/math.git] / source / geometry / extrudedshape.h
index 83933b0808a02f32d8febcce5eee8c8a23ad85d0..c00111a549a67fa0bbdd4682f859030acbad4a74 100644 (file)
@@ -31,7 +31,7 @@ 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() 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;
@@ -74,10 +74,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() 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();
+       T half_length = length/T(2);
+       return BoundingBox<T, D>(LinAl::Vector<T, D>(base_bbox.get_minimum_point(), -half_length),
+               LinAl::Vector<T, D>(base_bbox.get_maximum_point(), half_length));
 }
 
 template<typename T, unsigned D>
@@ -123,28 +125,32 @@ 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(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)
                        {
-                               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 = LinAl::Vector<T, D>(base_points[i].normal, T(0));
+                                       points[n].distance = x;
+                               }
 
-                       ++n;
+                               ++n;
+                       }
                }
        }