]> git.tdb.fi Git - libs/math.git/commitdiff
Fix a case in ExtrudedShape with certain ray parameters
authorMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 19:48:45 +0000 (22:48 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 22 May 2013 19:51:21 +0000 (22:51 +0300)
A ray starting outside of the length of the shape and pointing away from
it caused a local ray with a negative limit to be constructed.

source/geometry/extrudedshape.h

index de582e3d26bf278139600aca6c3cc1d15609a798..c00111a549a67fa0bbdd4682f859030acbad4a74 100644 (file)
@@ -125,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;
+                       }
                }
        }