Skip to content

Commit

Permalink
catch bad acos args that cause nans
Browse files Browse the repository at this point in the history
  • Loading branch information
mlanighan committed Jul 15, 2024
1 parent e1853e4 commit 49bf3a9
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/shapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ void Mesh::scaleAndPadd(double scaleX, double scaleY, double scaleZ, double padd
double scaledX = sx + dx * scaleX;
double scaledY = sy + dy * scaleY;
double scaledZ = sz + dz * scaleZ;

// Padding in each direction
vertices[i3] = scaledX + vertex_normals[i3] * paddX;
vertices[i3 + 1] = scaledY + vertex_normals[i3 + 1] * paddY;
Expand Down Expand Up @@ -548,7 +547,14 @@ void Mesh::computeVertexNormals()
if (vec1_norm == 0.0 || vec2_norm == 0.0)
return 0.0;

return std::acos(vec1.dot(vec2) / (vec1_norm * vec2_norm));
auto acos_arg = vec1.dot(vec2) / vec1_norm * vec2_norm;
// std::acos range is [-1.0, 1.0] and returns nans if arg > 1.0
// if the arg is approximately 1.0 return 0.0 angle as float imprecision will lead to nans
if (std::abs(acos_arg - 1.0) < 1e-6)
{
return 0.0;
}
return std::acos(acos_arg);
};

// Use law of cosines to compute angles
Expand Down

0 comments on commit 49bf3a9

Please sign in to comment.