@@ -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