Skip to content

Commit

Permalink
STYLE: Use std::abs, instead of doing if (x < 0) x = -x manually
Browse files Browse the repository at this point in the history
Aims to make the code clearer. Cases found by regular expressions like
` (\w+) = -\1;` and ` \w+ \*= -1;`

Removed old comments from inside two `DistanceToLine` member functions, saying
that they are "trying to avoid an expensive fabs". `std::abs` isn't that
expensive anymore nowadays.
  • Loading branch information
N-Dekker committed Nov 10, 2024
1 parent 4f6bdcb commit ec2d28f
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 40 deletions.
6 changes: 1 addition & 5 deletions Modules/Core/Common/include/itkMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,7 @@ FloatAlmostEqual(T x1,
return false;
}

typename Detail::FloatIEEE<T>::IntType ulps = FloatDifferenceULP(x1, x2);
if (ulps < 0)
{
ulps = -ulps;
}
typename Detail::FloatIEEE<T>::IntType ulps = std::abs(FloatDifferenceULP(x1, x2));
return ulps <= maxUlps;
}

Expand Down
8 changes: 2 additions & 6 deletions Modules/Core/Common/include/itkTriangleCell.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "vnl/algo/vnl_determinant.h"

#include <algorithm> // For copy_n.
#include <cmath> // For abs.

namespace itk
{
Expand Down Expand Up @@ -237,12 +238,7 @@ TriangleCell<TCellInterface>::DistanceToLine(PointType x,
denom += static_cast<double>(v21[i] * v21[i]);
}

// trying to avoid an expensive fabs
double tolerance = 1.e-05 * num;
if (tolerance < 0.0)
{
tolerance = -tolerance;
}
double tolerance = std::abs(1.e-05 * num);
if ((-tolerance < denom) && (denom < tolerance)) // numerically bad!
{
closestPoint = p1; // arbitrary, point is (numerically) far away
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "itkNeighborhoodAlgorithm.h"
#include "itkProgressReporter.h"

#include <cmath> // For abs.

namespace itk
{
namespace Testing
Expand Down Expand Up @@ -165,12 +167,8 @@ ComparisonImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(const Out
InputPixelType t = valid.Get();

// Assume a good match - so test center pixel first, for speed
RealType difference = static_cast<RealType>(t) - test.GetCenterPixel();
if (NumericTraits<RealType>::IsNegative(difference))
{
difference = -difference;
}
auto minimumDifference = static_cast<OutputPixelType>(difference);
RealType difference = std::abs(static_cast<RealType>(t) - test.GetCenterPixel());
auto minimumDifference = static_cast<OutputPixelType>(difference);

// If center pixel isn't good enough, then test the neighborhood
if (minimumDifference > m_DifferenceThreshold)
Expand All @@ -182,12 +180,8 @@ ComparisonImageFilter<TInputImage, TOutputImage>::ThreadedGenerateData(const Out
{
// Use the RealType for the difference to make sure we get the
// sign.
RealType differenceReal = static_cast<RealType>(t) - test.GetPixel(i);
if (NumericTraits<RealType>::IsNegative(differenceReal))
{
differenceReal = -differenceReal;
}
auto d = static_cast<OutputPixelType>(differenceReal);
RealType differenceReal = std::abs(static_cast<RealType>(t) - test.GetPixel(i));
auto d = static_cast<OutputPixelType>(differenceReal);
if (d < minimumDifference)
{
minimumDifference = d;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "itkTotalProgressReporter.h"
#include "itkStatisticsImageFilter.h"

#include <cmath> // For abs.

namespace itk
{
template <typename TInputImage, typename TOutputImage>
Expand Down Expand Up @@ -280,12 +282,8 @@ BilateralImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(
{
// range distance between neighborhood pixel and neighborhood center
pixel = static_cast<OutputPixelRealType>(b_iter.GetPixel(i));
rangeDistance = pixel - centerPixel;
// flip sign if needed
if (rangeDistance < 0.0)
{
rangeDistance *= -1.0;
}
rangeDistance = std::abs(pixel - centerPixel);

// if the range distance is close enough, then use the pixel
if (rangeDistance < rangeDistanceThreshold)
Expand Down
9 changes: 2 additions & 7 deletions Modules/Numerics/FEM/src/itkFEMElement2DC0LinearLine.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkFEMElement2DC0LinearLine.h"
#include <cmath> // For abs.

namespace itk
{
Expand Down Expand Up @@ -144,13 +145,7 @@ Element2DC0LinearLine::DistanceToLine(const VectorType & x,
//
num = p21[0] * (x[0] - p1[0]) + p21[1] * (x[1] - p1[1]) + p21[2] * (x[2] - p1[2]);
denom = p21[0] * p21[0] + p21[1] * p21[1] + p21[2] * p21[2];

// trying to avoid an expensive fabs
tolerance = 1e-5 * num;
if (tolerance < 0.0)
{
tolerance = -tolerance;
}
tolerance = std::abs(1e-5 * num);
if (-tolerance < denom && denom < tolerance) // numerically bad!
{
closest = p1; // arbitrary, point is (numerically) far away
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkEuclideanDistancePointMetric_hxx

#include "itkImageRegionConstIteratorWithIndex.h"
#include <cmath> // For abs.

namespace itk
{
Expand Down Expand Up @@ -86,13 +87,9 @@ EuclideanDistancePointMetric<TFixedPointSet, TMovingPointSet, TDistanceMap>::Get
typename DistanceMapType::IndexType index;
if (m_DistanceMap->TransformPhysicalPointToIndex(transformedPoint, index))
{
minimumDistance = m_DistanceMap->GetPixel(index);
// In case the provided distance map was signed,
// we correct here the distance to take its absolute value.
if (minimumDistance < 0.0)
{
minimumDistance = -minimumDistance;
}
minimumDistance = std::abs(m_DistanceMap->GetPixel(index));
closestPoint = true;
}
}
Expand Down

0 comments on commit ec2d28f

Please sign in to comment.