]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/extrudedshape.h
Properly sort intersection points for complex shapes
[libs/math.git] / source / geometry / extrudedshape.h
index 0d895ac5bc0aecbac9d6de46f5e93c9c932ca0dd..7aeedb0cd146e30e761e3c88b41d54dde680cdfb 100644 (file)
@@ -141,7 +141,7 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
                        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; i<count; ++i)
+               for(unsigned i=0; (n<size && i<count); ++i)
                {
                        if(points)
                        {
@@ -152,23 +152,21 @@ inline unsigned ExtrudedShape<T, D>::get_intersections(const Ray<T, D> &ray, Sur
                        }
 
                        ++n;
-                       if(n==size)
-                               return n;
                }
        }
 
        /* If the ray is not parallel to the base space, it may pass through the
        caps. */
-       if(ray_direction[D-1])
+       if(n<size && ray_direction[D-1])
        {
-               for(int i=-1; i<=1; i+=2)
+               for(int i=-1; (n<size && i<=1); i+=2)
                {
                        T x = (half_length*i-ray_start[D-1])/ray_direction[D-1];
                        if(!ray.check_limits(x))
                                continue;
 
                        LinAl::Vector<T, D> p = ray_start+ray_direction*x;
-                       if(base->contains(LinAl::Vector<T, D-1>(p)) && n<size)
+                       if(base->contains(LinAl::Vector<T, D-1>(p)))
                        {
                                if(points)
                                {
@@ -176,16 +174,13 @@ 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;
-
-                                       if(n==1 && x<points[0].distance)
-                                               swap(points[0], points[1]);
                                }
 
                                ++n;
-                               if(n==size)
-                                       return n;
                        }
                }
+
+               sort_points(points, n);
        }
 
        return n;