Avoid divide by zero in rendering TriangleIntersections

This commit is contained in:
Kenneth Moreland 2021-07-12 11:23:24 -06:00
parent ede7756929
commit f883b5e036

@ -198,27 +198,29 @@ public:
Precision low = vtkm::Min(u, vtkm::Min(v, w)); Precision low = vtkm::Min(u, vtkm::Min(v, w));
Precision high = vtkm::Max(u, vtkm::Max(v, w)); Precision high = vtkm::Max(u, vtkm::Max(v, w));
bool invalid = (low < 0.) && (high > 0.);
Precision det = u + v + w; Precision det = u + v + w;
if (det == 0.) if (!((low < 0.) && (high > 0.)) && (det != 0.))
invalid = true; {
// 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]]; det = 1.f / det;
const Precision Bz = s[2] * B[k[2]];
const Precision Cz = s[2] * C[k[2]];
det = 1.f / det; u = u * det;
v = v * det;
u = u * det; distance = (u * Az + v * Bz + w * det * Cz);
v = v * det; u = v;
v = w * det;
distance = (u * Az + v * Bz + w * det * Cz); }
u = v; else
v = w * det; {
if (invalid) // Intersection is invalid
distance = -1.; distance = -1.;
}
} }
template <typename Precision> template <typename Precision>
@ -266,27 +268,29 @@ public:
Precision low = vtkm::Min(u, vtkm::Min(v, w)); Precision low = vtkm::Min(u, vtkm::Min(v, w));
Precision high = vtkm::Max(u, vtkm::Max(v, w)); Precision high = vtkm::Max(u, vtkm::Max(v, w));
bool invalid = (low < 0.) && (high > 0.);
Precision det = u + v + w; Precision det = u + v + w;
if (det == 0.) if (!((low < 0.) && (high > 0.)) && (det != 0.))
invalid = true; {
// 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]]; det = 1.f / det;
const Precision Bz = s[2] * B[k[2]];
const Precision Cz = s[2] * C[k[2]];
det = 1.f / det; u = u * det;
v = v * det;
u = u * det; distance = (u * Az + v * Bz + w * det * Cz);
v = v * det; u = v;
v = w * det;
distance = (u * Az + v * Bz + w * det * Cz); }
u = v; else
v = w * det; {
if (invalid) // Intersection is invalid
distance = -1.; distance = -1.;
}
} }
}; //WaterTight }; //WaterTight
@ -357,27 +361,30 @@ VTKM_EXEC inline void WaterTight::IntersectTri<vtkm::Float64>(const vtkm::Vec3f_
vtkm::Float64 low = vtkm::Min(u, vtkm::Min(v, w)); vtkm::Float64 low = vtkm::Min(u, vtkm::Min(v, w));
vtkm::Float64 high = vtkm::Max(u, vtkm::Max(v, w)); vtkm::Float64 high = vtkm::Max(u, vtkm::Max(v, w));
bool invalid = (low < 0.) && (high > 0.);
vtkm::Float64 det = u + v + w; vtkm::Float64 det = u + v + w;
if (det == 0.) if (!((low < 0.) && (high > 0.)) && (det != 0.))
invalid = true; {
// 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]; det = 1. / det;
const vtkm::Float64 Bz = Sz * B[kz];
const vtkm::Float64 Cz = Sz * C[kz];
det = 1. / det; u = u * det;
v = v * det;
u = u * det; distance = (u * Az + v * Bz + w * det * Cz);
v = v * det; u = v;
v = w * det;
distance = (u * Az + v * Bz + w * det * Cz); }
u = v; else
v = w * det; {
if (invalid) // Intersection is invalid
distance = -1.; distance = -1.;
}
} }
} }
} }