From 45a9d1119260fd579e82c36f2b89c7dc14be9f8a Mon Sep 17 00:00:00 2001 From: jlait Date: Tue, 5 Feb 2019 16:12:33 -0500 Subject: [PATCH] Check for NANs when computing distances to avoid infinite loops with bad input geometry. Rename the computeDistance to updateDistance to better reflect its behaviour. Signed-off-by: jlait --- openvdb/tools/MeshToVolume.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/openvdb/tools/MeshToVolume.h b/openvdb/tools/MeshToVolume.h index 37e7ea2ce0..aa6ced8cc6 100644 --- a/openvdb/tools/MeshToVolume.h +++ b/openvdb/tools/MeshToVolume.h @@ -2137,7 +2137,10 @@ class VoxelizePolygons ijk = Coord::floor(prim.a); coordList.push_back(ijk); - computeDistance(ijk, prim, data); + // The first point may not be quite in bounds, and rely + // on one of the neighbours to have the first valid seed, + // so we cannot early-exit here. + updateDistance(ijk, prim, data); unsigned char primId = data.getNewPrimId(); data.primIdAcc.setValueOnly(ijk, primId); @@ -2150,13 +2153,13 @@ class VoxelizePolygons nijk = ijk + util::COORD_OFFSETS[i]; if (primId != data.primIdAcc.getValue(nijk)) { data.primIdAcc.setValueOnly(nijk, primId); - if(computeDistance(nijk, prim, data)) coordList.push_back(nijk); + if(updateDistance(nijk, prim, data)) coordList.push_back(nijk); } } } } - static bool computeDistance(const Coord& ijk, const Triangle& prim, VoxelizationDataType& data) + static bool updateDistance(const Coord& ijk, const Triangle& prim, VoxelizationDataType& data) { Vec3d uvw, voxelCenter(ijk[0], ijk[1], ijk[2]); @@ -2165,6 +2168,11 @@ class VoxelizePolygons const ValueType dist = ValueType((voxelCenter - closestPointOnTriangleToPoint(prim.a, prim.c, prim.b, voxelCenter, uvw)).lengthSqr()); + // Either the points may be NAN, or they could be far enough from + // the origin that computing distance fails. + if (std::isnan(dist)) + return false; + const ValueType oldDist = data.distAcc.getValue(ijk); if (dist < oldDist) {