]> git.tdb.fi Git - libs/math.git/commitdiff
Ensure that HyperBox does not produce duplicate intersections
authorMikko Rasa <tdb@tdb.fi>
Sat, 2 Jul 2016 17:00:50 +0000 (20:00 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 2 Jul 2016 17:00:50 +0000 (20:00 +0300)
In certain corner cases (literally) two coincident intersections could be
produced when the ray passed through the edge of the box, preventing
intersctions on the opposite side from being produced.

source/geometry/hyperbox.h

index 546ad0ec3984c5b338978c23b37f0c8ea5f97533..b9fad33d620d205f1a5043e002cb88556cbc6e32 100644 (file)
@@ -112,14 +112,23 @@ inline unsigned HyperBox<T, D>::get_intersections(const Ray<T, D> &ray, SurfaceP
                        {
                                if(points)
                                {
-                                       points[n].position = p;
-                                       points[n].normal = LinAl::Vector<T, D>();
-                                       points[n].normal[i] = j;
-                                       points[n].distance = x;
-                                       points[n].entry = (T(j)*ray.get_direction()[i]<T(0));
-
-                                       if(n==1 && x<points[0].distance)
-                                               std::swap(points[0], points[1]);
+                                       bool entry = (T(j)*ray.get_direction()[i]<T(0));
+                                       unsigned k = 0;
+                                       if(n>0 && entry!=points[0].entry)
+                                       {
+                                               if(entry)
+                                                       points[1] = points[0];
+                                               else
+                                                       ++k;
+                                       }
+                                       if(k<n && entry==points[k].entry)
+                                               --n;
+
+                                       points[k].position = p;
+                                       points[k].normal = LinAl::Vector<T, D>();
+                                       points[k].normal[i] = j;
+                                       points[k].distance = x;
+                                       points[k].entry = (T(j)*ray.get_direction()[i]<T(0));
                                }
 
                                ++n;