From 4d6d780f3fb2cd0054108a2fc97034b77a5c1959 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 10 Nov 2024 00:24:43 +0100 Subject: [PATCH] STYLE: Use `std::abs`, instead of doing `if (x < 0) x = -x` manually Aims to make the code clearer. Cases found by regular expressions like ` (\w+) = -\1;` and ` \w+ \*= -1;` --- Modules/Core/Common/include/itkMath.h | 6 +----- .../Core/Common/include/itkTriangleCell.hxx | 8 ++------ .../itkTestingComparisonImageFilter.hxx | 18 ++++++------------ .../include/itkBilateralImageFilter.hxx | 8 +++----- .../FEM/src/itkFEMElement2DC0LinearLine.cxx | 7 ++----- .../itkEuclideanDistancePointMetric.hxx | 7 ++----- 6 files changed, 16 insertions(+), 38 deletions(-) diff --git a/Modules/Core/Common/include/itkMath.h b/Modules/Core/Common/include/itkMath.h index d20cb490c446..17dadefcd700 100644 --- a/Modules/Core/Common/include/itkMath.h +++ b/Modules/Core/Common/include/itkMath.h @@ -329,11 +329,7 @@ FloatAlmostEqual(T x1, return false; } - typename Detail::FloatIEEE::IntType ulps = FloatDifferenceULP(x1, x2); - if (ulps < 0) - { - ulps = -ulps; - } + typename Detail::FloatIEEE::IntType ulps = std::abs(FloatDifferenceULP(x1, x2)); return ulps <= maxUlps; } diff --git a/Modules/Core/Common/include/itkTriangleCell.hxx b/Modules/Core/Common/include/itkTriangleCell.hxx index 9df171acaf54..13169c09cd87 100644 --- a/Modules/Core/Common/include/itkTriangleCell.hxx +++ b/Modules/Core/Common/include/itkTriangleCell.hxx @@ -20,6 +20,7 @@ #include "vnl/algo/vnl_determinant.h" #include // For copy_n. +#include // For abs. namespace itk { @@ -237,12 +238,7 @@ TriangleCell::DistanceToLine(PointType x, denom += static_cast(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 diff --git a/Modules/Core/TestKernel/include/itkTestingComparisonImageFilter.hxx b/Modules/Core/TestKernel/include/itkTestingComparisonImageFilter.hxx index a93af7d24703..998319196e54 100644 --- a/Modules/Core/TestKernel/include/itkTestingComparisonImageFilter.hxx +++ b/Modules/Core/TestKernel/include/itkTestingComparisonImageFilter.hxx @@ -24,6 +24,8 @@ #include "itkNeighborhoodAlgorithm.h" #include "itkProgressReporter.h" +#include // For abs. + namespace itk { namespace Testing @@ -165,12 +167,8 @@ ComparisonImageFilter::ThreadedGenerateData(const Out InputPixelType t = valid.Get(); // Assume a good match - so test center pixel first, for speed - RealType difference = static_cast(t) - test.GetCenterPixel(); - if (NumericTraits::IsNegative(difference)) - { - difference = -difference; - } - auto minimumDifference = static_cast(difference); + RealType difference = std::abs(static_cast(t) - test.GetCenterPixel()); + auto minimumDifference = static_cast(difference); // If center pixel isn't good enough, then test the neighborhood if (minimumDifference > m_DifferenceThreshold) @@ -182,12 +180,8 @@ ComparisonImageFilter::ThreadedGenerateData(const Out { // Use the RealType for the difference to make sure we get the // sign. - RealType differenceReal = static_cast(t) - test.GetPixel(i); - if (NumericTraits::IsNegative(differenceReal)) - { - differenceReal = -differenceReal; - } - auto d = static_cast(differenceReal); + RealType differenceReal = std::abs(static_cast(t) - test.GetPixel(i)); + auto d = static_cast(differenceReal); if (d < minimumDifference) { minimumDifference = d; diff --git a/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx b/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx index fd250e6b0862..92bf5bed6cc5 100644 --- a/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx +++ b/Modules/Filtering/ImageFeature/include/itkBilateralImageFilter.hxx @@ -25,6 +25,8 @@ #include "itkTotalProgressReporter.h" #include "itkStatisticsImageFilter.h" +#include // For abs. + namespace itk { template @@ -280,12 +282,8 @@ BilateralImageFilter::DynamicThreadedGenerateData( { // range distance between neighborhood pixel and neighborhood center pixel = static_cast(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) diff --git a/Modules/Numerics/FEM/src/itkFEMElement2DC0LinearLine.cxx b/Modules/Numerics/FEM/src/itkFEMElement2DC0LinearLine.cxx index 2d16a3fb40e9..27395488afe4 100644 --- a/Modules/Numerics/FEM/src/itkFEMElement2DC0LinearLine.cxx +++ b/Modules/Numerics/FEM/src/itkFEMElement2DC0LinearLine.cxx @@ -17,6 +17,7 @@ *=========================================================================*/ #include "itkFEMElement2DC0LinearLine.h" +#include // For abs. namespace itk { @@ -146,11 +147,7 @@ Element2DC0LinearLine::DistanceToLine(const VectorType & x, 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 diff --git a/Modules/Registration/Common/include/itkEuclideanDistancePointMetric.hxx b/Modules/Registration/Common/include/itkEuclideanDistancePointMetric.hxx index 50d8bc92ba03..e5614e7b8547 100644 --- a/Modules/Registration/Common/include/itkEuclideanDistancePointMetric.hxx +++ b/Modules/Registration/Common/include/itkEuclideanDistancePointMetric.hxx @@ -19,6 +19,7 @@ #define itkEuclideanDistancePointMetric_hxx #include "itkImageRegionConstIteratorWithIndex.h" +#include // For abs. namespace itk { @@ -86,13 +87,9 @@ EuclideanDistancePointMetric::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; } }