Skip to content

Commit 4c2a8aa

Browse files
committed
Do not take square root of negative values.
FP exceptions were triggered in unit tests. FIXES #2092 Signed-off-by: Bram Stolk <[email protected]>
1 parent 0f138c1 commit 4c2a8aa

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

openvdb/openvdb/math/Stencils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,9 @@ class CurvatureStencil: public BaseStencil<CurvatureStencil<GridT, IsSafe>, Grid
16271627
Real alphaM, alphaG, normGrad;
16281628
if (this->curvatures(alphaM, alphaG, normGrad)) {
16291629
const Real mean = alphaM*mInv2Dx/math::Pow3(normGrad);
1630-
const Real tmp = std::sqrt(mean*mean - alphaG*mInvDx2/math::Pow4(normGrad));
1630+
const Real base = mean*mean - alphaG*mInvDx2/math::Pow4(normGrad);
1631+
const Real clampVal = 0;
1632+
const Real tmp = std::sqrt(std::max(base, clampVal));
16311633
pair.first = ValueType(mean - tmp);
16321634
pair.second = ValueType(mean + tmp);
16331635
}

openvdb/openvdb/tools/impl/ConvexVoxelizer.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,10 @@ class ConvexVoxelizer
389389
circleBottom(const ValueT& x0, const ValueT& y0,
390390
const ValueT& r, const ValueT& x)
391391
{
392-
return y0 - math::Sqrt(math::Pow2(r) - math::Pow2(x-x0));
392+
const ValueT base = math::Pow2(r) - math::Pow2(x-x0);
393+
return base <= 0
394+
? y0
395+
: y0 - math::Sqrt(base);
393396
}
394397

395398
/// @brief Computes the top y-coordinate of a circle at a given x position.
@@ -402,7 +405,10 @@ class ConvexVoxelizer
402405
circleTop(const ValueT& x0, const ValueT& y0,
403406
const ValueT& r, const ValueT& x)
404407
{
405-
return y0 + math::Sqrt(math::Pow2(r) - math::Pow2(x-x0));
408+
const ValueT base = math::Pow2(r) - math::Pow2(x-x0);
409+
return base <= 0
410+
? y0
411+
: y0 + math::Sqrt(base);
406412
}
407413

408414
/// @brief Computes the bottom z-coordinate of a sphere at a given (x, y) position.
@@ -417,7 +423,10 @@ class ConvexVoxelizer
417423
sphereBottom(const ValueT& x0, const ValueT& y0, const ValueT& z0,
418424
const ValueT& r, const ValueT& x, const ValueT& y)
419425
{
420-
return z0 - math::Sqrt(math::Pow2(r) - math::Pow2(x-x0) - math::Pow2(y-y0));
426+
const ValueT base = math::Pow2(r) - math::Pow2(x-x0) - math::Pow2(y-y0);
427+
return base <= 0 // Do not take square root of negative numbers.
428+
? z0
429+
: z0 - math::Sqrt(base);
421430
}
422431

423432
/// @brief Computes the top z-coordinate of a sphere at a given (x, y) position.
@@ -432,7 +441,10 @@ class ConvexVoxelizer
432441
sphereTop(const ValueT& x0, const ValueT& y0, const ValueT& z0,
433442
const ValueT& r, const ValueT& x, const ValueT& y)
434443
{
435-
return z0 + math::Sqrt(math::Pow2(r) - math::Pow2(x-x0) - math::Pow2(y-y0));
444+
const ValueT base = math::Pow2(r) - math::Pow2(x-x0) - math::Pow2(y-y0);
445+
return base <= 0 // Do not take square root of negative numbers.
446+
? z0
447+
: z0 + math::Sqrt(base);
436448
}
437449

438450
// ------------ nested classes ------------

0 commit comments

Comments
 (0)