]> git.tdb.fi Git - libs/math.git/blobdiff - source/geometry/hyperbox.h
Properly sort intersection points for complex shapes
[libs/math.git] / source / geometry / hyperbox.h
index 42e4d61a75a49ce0824aaf231388013246c1413f..9a161f35fb0fdbc5ff604735d51eb6e1d060eeaf 100644 (file)
@@ -88,14 +88,17 @@ inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfaceP
 {
        using std::abs;
 
+       if(size>2)
+               size = 2;
+
        LinAl::Vector<T, D> half_dim = dimensions/T(2);
        unsigned n = 0;
-       for(unsigned i=0; i<D; ++i)
+       for(unsigned i=0; (n<size && i<D); ++i)
        {
                if(!ray.get_direction()[i])
                        continue;
 
-               for(int j=-1; j<=1; j+=2)
+               for(int j=-1; (n<size && j<=1); j+=2)
                {
                        T x = (T(j)*half_dim[i]-ray.get_start()[i])/ray.get_direction()[i];
                        if(!ray.check_limits(x))
@@ -107,7 +110,7 @@ inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfaceP
                        for(unsigned k=0; (inside && k<D); ++k)
                                inside = (k==i || abs(p[k])<=half_dim[k]);
 
-                       if(inside && n<size)
+                       if(inside)
                        {
                                if(points)
                                {
@@ -121,8 +124,6 @@ inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfaceP
                                }
 
                                ++n;
-                               if(n==size || n==2)
-                                       return n;
                        }
                }
        }