From f883b5e0366ea8d9b405fd02a1b448cc09361930 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Mon, 12 Jul 2021 11:23:24 -0600 Subject: [PATCH] Avoid divide by zero in rendering TriangleIntersections --- .../raytracing/TriangleIntersections.h | 95 ++++++++++--------- 1 file changed, 51 insertions(+), 44 deletions(-) diff --git a/vtkm/rendering/raytracing/TriangleIntersections.h b/vtkm/rendering/raytracing/TriangleIntersections.h index b66975f7e..7566dc791 100644 --- a/vtkm/rendering/raytracing/TriangleIntersections.h +++ b/vtkm/rendering/raytracing/TriangleIntersections.h @@ -198,27 +198,29 @@ public: Precision low = vtkm::Min(u, vtkm::Min(v, w)); Precision high = vtkm::Max(u, vtkm::Max(v, w)); - bool invalid = (low < 0.) && (high > 0.); - Precision det = u + v + w; - if (det == 0.) - invalid = true; + if (!((low < 0.) && (high > 0.)) && (det != 0.)) + { + // Intersection is valid + const Precision Az = s[2] * A[k[2]]; + const Precision Bz = s[2] * B[k[2]]; + const Precision Cz = s[2] * C[k[2]]; - const Precision Az = s[2] * A[k[2]]; - const Precision Bz = s[2] * B[k[2]]; - const Precision Cz = s[2] * C[k[2]]; + det = 1.f / det; - det = 1.f / det; + u = u * det; + v = v * det; - u = u * det; - v = v * det; - - distance = (u * Az + v * Bz + w * det * Cz); - u = v; - v = w * det; - if (invalid) + distance = (u * Az + v * Bz + w * det * Cz); + u = v; + v = w * det; + } + else + { + // Intersection is invalid distance = -1.; + } } template @@ -266,27 +268,29 @@ public: Precision low = vtkm::Min(u, vtkm::Min(v, w)); Precision high = vtkm::Max(u, vtkm::Max(v, w)); - bool invalid = (low < 0.) && (high > 0.); - Precision det = u + v + w; - if (det == 0.) - invalid = true; + if (!((low < 0.) && (high > 0.)) && (det != 0.)) + { + // Intersection is valid + const Precision Az = s[2] * A[k[2]]; + const Precision Bz = s[2] * B[k[2]]; + const Precision Cz = s[2] * C[k[2]]; - const Precision Az = s[2] * A[k[2]]; - const Precision Bz = s[2] * B[k[2]]; - const Precision Cz = s[2] * C[k[2]]; + det = 1.f / det; - det = 1.f / det; + u = u * det; + v = v * det; - u = u * det; - v = v * det; - - distance = (u * Az + v * Bz + w * det * Cz); - u = v; - v = w * det; - if (invalid) + distance = (u * Az + v * Bz + w * det * Cz); + u = v; + v = w * det; + } + else + { + // Intersection is invalid distance = -1.; + } } }; //WaterTight @@ -357,27 +361,30 @@ VTKM_EXEC inline void WaterTight::IntersectTri(const vtkm::Vec3f_ vtkm::Float64 low = vtkm::Min(u, vtkm::Min(v, w)); vtkm::Float64 high = vtkm::Max(u, vtkm::Max(v, w)); - bool invalid = (low < 0.) && (high > 0.); vtkm::Float64 det = u + v + w; - if (det == 0.) - invalid = true; + if (!((low < 0.) && (high > 0.)) && (det != 0.)) + { + // Intersection is valid + const vtkm::Float64 Az = Sz * A[kz]; + const vtkm::Float64 Bz = Sz * B[kz]; + const vtkm::Float64 Cz = Sz * C[kz]; - const vtkm::Float64 Az = Sz * A[kz]; - const vtkm::Float64 Bz = Sz * B[kz]; - const vtkm::Float64 Cz = Sz * C[kz]; + det = 1. / det; - det = 1. / det; + u = u * det; + v = v * det; - u = u * det; - v = v * det; - - distance = (u * Az + v * Bz + w * det * Cz); - u = v; - v = w * det; - if (invalid) + distance = (u * Az + v * Bz + w * det * Cz); + u = v; + v = w * det; + } + else + { + // Intersection is invalid distance = -1.; + } } } }