Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#4480 from N-Dekker/Repla…
Browse files Browse the repository at this point in the history
…ce-if-array-subscript-with-std-min-max
  • Loading branch information
dzenanz authored Feb 27, 2024
2 parents 0a30e7f + 22d6bab commit 8f64fdb
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 133 deletions.
6 changes: 2 additions & 4 deletions Modules/Core/Common/include/itkNeighborhoodAlgorithm.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "itkImageRegionIterator.h"
#include "itkImageRegion.h"
#include "itkConstSliceIterator.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -106,10 +107,7 @@ ImageBoundaryFacesCalculator<TImage>::Compute(const TImage & img, RegionType reg
}

// Boundary region cannot be outside the region to process
if (fSize[j] > rSize[j])
{
fSize[j] = rSize[j];
}
fSize[j] = std::min(fSize[j], rSize[j]);
}
// avoid unsigned overflow if the non-boundary region is too small to
// process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "itkInterpolateImageFunction.h"
#include "itkVariableLengthVector.h"
#include <algorithm> // For max.

namespace itk
{
Expand Down Expand Up @@ -133,10 +134,7 @@ class ITK_TEMPLATE_EXPORT LinearInterpolateImageFunction : public InterpolateIma
{
IndexType basei;
basei[0] = Math::Floor<IndexValueType>(index[0]);
if (basei[0] < this->m_StartIndex[0])
{
basei[0] = this->m_StartIndex[0];
}
basei[0] = std::max(basei[0], this->m_StartIndex[0]);

const InternalComputationType & distance = index[0] - static_cast<InternalComputationType>(basei[0]);

Expand All @@ -163,17 +161,11 @@ class ITK_TEMPLATE_EXPORT LinearInterpolateImageFunction : public InterpolateIma
IndexType basei;

basei[0] = Math::Floor<IndexValueType>(index[0]);
if (basei[0] < this->m_StartIndex[0])
{
basei[0] = this->m_StartIndex[0];
}
basei[0] = std::max(basei[0], this->m_StartIndex[0]);
const InternalComputationType & distance0 = index[0] - static_cast<InternalComputationType>(basei[0]);

basei[1] = Math::Floor<IndexValueType>(index[1]);
if (basei[1] < this->m_StartIndex[1])
{
basei[1] = this->m_StartIndex[1];
}
basei[1] = std::max(basei[1], this->m_StartIndex[1]);
const InternalComputationType & distance1 = index[1] - static_cast<InternalComputationType>(basei[1]);

const TInputImage * const inputImagePtr = this->GetInputImage();
Expand Down Expand Up @@ -239,24 +231,15 @@ class ITK_TEMPLATE_EXPORT LinearInterpolateImageFunction : public InterpolateIma
{
IndexType basei;
basei[0] = Math::Floor<IndexValueType>(index[0]);
if (basei[0] < this->m_StartIndex[0])
{
basei[0] = this->m_StartIndex[0];
}
basei[0] = std::max(basei[0], this->m_StartIndex[0]);
const InternalComputationType & distance0 = index[0] - static_cast<InternalComputationType>(basei[0]);

basei[1] = Math::Floor<IndexValueType>(index[1]);
if (basei[1] < this->m_StartIndex[1])
{
basei[1] = this->m_StartIndex[1];
}
basei[1] = std::max(basei[1], this->m_StartIndex[1]);
const InternalComputationType & distance1 = index[1] - static_cast<InternalComputationType>(basei[1]);

basei[2] = Math::Floor<IndexValueType>(index[2]);
if (basei[2] < this->m_StartIndex[2])
{
basei[2] = this->m_StartIndex[2];
}
basei[2] = std::max(basei[2], this->m_StartIndex[2]);
const InternalComputationType & distance2 = index[2] - static_cast<InternalComputationType>(basei[2]);

const TInputImage * const inputImagePtr = this->GetInputImage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkConceptChecking.h"

#include "itkMath.h"
#include <algorithm> // For min and max.

namespace itk
{
Expand Down Expand Up @@ -77,20 +78,14 @@ LinearInterpolateImageFunction<TInputImage, TCoordRep>::EvaluateUnoptimized(cons
++(neighIndex[dim]);
// Take care of the case where the pixel is just
// in the outer upper boundary of the image grid.
if (neighIndex[dim] > this->m_EndIndex[dim])
{
neighIndex[dim] = this->m_EndIndex[dim];
}
neighIndex[dim] = std::min(neighIndex[dim], this->m_EndIndex[dim]);
overlap *= distance[dim];
}
else
{
// Take care of the case where the pixel is just
// in the outer lower boundary of the image grid.
if (neighIndex[dim] < this->m_StartIndex[dim])
{
neighIndex[dim] = this->m_StartIndex[dim];
}
neighIndex[dim] = std::max(neighIndex[dim], this->m_StartIndex[dim]);
overlap *= 1.0 - distance[dim];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


#include "itkMath.h"
#include <algorithm> // For min and max.

namespace itk
{
Expand Down Expand Up @@ -72,21 +73,15 @@ VectorLinearInterpolateImageFunction<TInputImage, TCoordRep>::EvaluateAtContinuo
neighIndex[dim] = baseIndex[dim] + 1;
// Take care of the case where the pixel is just
// in the outer upper boundary of the image grid.
if (neighIndex[dim] > this->m_EndIndex[dim])
{
neighIndex[dim] = this->m_EndIndex[dim];
}
neighIndex[dim] = std::min(neighIndex[dim], this->m_EndIndex[dim]);
overlap *= distance[dim];
}
else
{
neighIndex[dim] = baseIndex[dim];
// Take care of the case where the pixel is just
// in the outer lower boundary of the image grid.
if (neighIndex[dim] < this->m_StartIndex[dim])
{
neighIndex[dim] = this->m_StartIndex[dim];
}
neighIndex[dim] = std::max(neighIndex[dim], this->m_StartIndex[dim]);
overlap *= 1.0 - distance[dim];
}
upper >>= 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkImageRegionIteratorWithIndex.h"
#include "itkNumericTraits.h"
#include <cstdlib>
#include <algorithm> // For max.

namespace itk
{
Expand Down Expand Up @@ -227,10 +228,7 @@ TriangleMeshToBinaryImageFilter<TInputMesh, TOutputImage>::PolygonToImageRaster(
}

// cap to the volume extents
if (zmin < extent[4])
{
zmin = extent[4];
}
zmin = std::max(zmin, extent[4]);
if (zmax >= extent[5])
{
zmax = extent[5] + 1;
Expand Down
11 changes: 3 additions & 8 deletions Modules/Core/SpatialObjects/include/itkContourSpatialObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkContourSpatialObject_hxx

#include "itkNumericTraits.h"
#include <algorithm> // For min and max.

namespace itk
{
Expand Down Expand Up @@ -81,14 +82,8 @@ ContourSpatialObject<TDimension>::GetOrientationInObjectSpace() const
PointType curpoint = it->GetPositionInObjectSpace();
for (unsigned int i = 0; i < TDimension; ++i)
{
if (minPnt[i] > curpoint[i])
{
minPnt[i] = curpoint[i];
}
if (maxPnt[i] < curpoint[i])
{
maxPnt[i] = curpoint[i];
}
minPnt[i] = std::min(minPnt[i], curpoint[i]);
maxPnt[i] = std::max(maxPnt[i], curpoint[i]);
}
++it;
}
Expand Down
11 changes: 3 additions & 8 deletions Modules/Core/SpatialObjects/include/itkPolygonSpatialObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkPolygonSpatialObject_hxx

#include "itkMath.h"
#include <algorithm> // For min and max.

namespace itk
{
Expand Down Expand Up @@ -69,14 +70,8 @@ PolygonSpatialObject<TDimension>::GetOrientationInObjectSpace() const
PointType curpoint = it->GetPositionInObjectSpace();
for (unsigned int i = 0; i < TDimension; ++i)
{
if (minPnt[i] > curpoint[i])
{
minPnt[i] = curpoint[i];
}
if (maxPnt[i] < curpoint[i])
{
maxPnt[i] = curpoint[i];
}
minPnt[i] = std::min(minPnt[i], curpoint[i]);
maxPnt[i] = std::max(maxPnt[i], curpoint[i]);
}
++it;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkMeanSampleFilter.h"
#include "itkCovarianceSampleFilter.h"
#include "itkImageMaskSpatialObject.h"
#include <algorithm> // For max.

namespace itk
{
Expand Down Expand Up @@ -173,10 +174,7 @@ SpatialObjectToImageStatisticsCalculator<TInputImage, TInputSpatialObject, TSamp
indMin[i] = indMax[i];
indMax[i] = tmpI;
}
if (indMin[i] < imageIndex[i])
{
indMin[i] = imageIndex[i];
}
indMin[i] = std::max(indMin[i], imageIndex[i]);
size[i] = indMax[i] - indMin[i] + 1;
if (indMin[i] + size[i] < imageIndex[i] + imageSize[i])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "vnl/algo/vnl_symmetric_eigensystem.h"
#include "vnl/algo/vnl_matrix_inverse.h"
#include "itkCastImageFilter.h"
#include <algorithm> // For min and max.

namespace itk
{
Expand Down Expand Up @@ -258,14 +259,8 @@ DisplacementFieldTransform<TParametersValueType, VDimension>::ComputeJacobianWit
difIndex[1][col] -= 1;
difIndex[2][col] += 1;
difIndex[3][col] += 2;
if (difIndex[0][col] < startingIndex[col])
{
difIndex[0][col] = startingIndex[col];
}
if (difIndex[3][col] > upperIndex[col])
{
difIndex[3][col] = upperIndex[col];
}
difIndex[0][col] = std::max(difIndex[0][col], startingIndex[col]);
difIndex[3][col] = std::min(difIndex[3][col], upperIndex[col]);

OutputVectorType pixDisp[4];
for (unsigned int i = 0; i < 4; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "itkReflectiveImageRegionConstIterator.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include <algorithm> // For max.

namespace itk
{
Expand Down Expand Up @@ -120,10 +121,7 @@ DanielssonDistanceMapImageFilter<TInputImage, TOutputImage, TVoronoiImage>::Prep

for (unsigned int dim = 0; dim < InputImageDimension; ++dim)
{
if (maxLength < size[dim])
{
maxLength = size[dim];
}
maxLength = std::max(maxLength, size[dim]);
}

ImageRegionConstIteratorWithIndex<InputImageType> it(inputImage, region);
Expand Down
6 changes: 2 additions & 4 deletions Modules/Filtering/ImageSources/include/itkGridImageSource.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "itkImageLinearIteratorWithIndex.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkTotalProgressReporter.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -50,10 +51,7 @@ GridImageSource<TOutputImage>::BeforeThreadedGenerateData()

for (unsigned int i = 0; i < ImageDimension; ++i)
{
if (this->m_GridOffset[i] > this->m_GridSpacing[i])
{
this->m_GridOffset[i] = this->m_GridSpacing[i];
}
this->m_GridOffset[i] = std::min(this->m_GridOffset[i], this->m_GridSpacing[i]);
PixelArrayType pixels = m_PixelArrays->CreateElementAt(i);
pixels.set_size(this->GetSize()[i]);
pixels.fill(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkImageLinearConstIteratorWithIndex.h"
#include "itkImageScanlineConstIterator.h"
#include "itkTotalProgressReporter.h"
#include <algorithm> // For min and max.

namespace itk
{
Expand Down Expand Up @@ -87,14 +88,9 @@ LabelStatisticsImageFilter<TInputImage, TLabelImage>::MergeMap(MapType & m1, Map
// bounding box is min,max pairs
for (unsigned int ii = 0; ii < (ImageDimension * 2); ii += 2)
{
if (labelStats.m_BoundingBox[ii] > m2_value.second.m_BoundingBox[ii])
{
labelStats.m_BoundingBox[ii] = m2_value.second.m_BoundingBox[ii];
}
if (labelStats.m_BoundingBox[ii + 1] < m2_value.second.m_BoundingBox[ii + 1])
{
labelStats.m_BoundingBox[ii + 1] = m2_value.second.m_BoundingBox[ii + 1];
}
labelStats.m_BoundingBox[ii] = std::min(labelStats.m_BoundingBox[ii], m2_value.second.m_BoundingBox[ii]);
labelStats.m_BoundingBox[ii + 1] =
std::max(labelStats.m_BoundingBox[ii + 1], m2_value.second.m_BoundingBox[ii + 1]);
}

// if enabled, update the histogram for this label
Expand Down Expand Up @@ -222,14 +218,8 @@ LabelStatisticsImageFilter<TInputImage, TLabelImage>::ThreadedStreamedGenerateDa
for (unsigned int i = 0; i < (2 * TInputImage::ImageDimension); i += 2)
{
const IndexType & index = it.GetIndex();
if (labelStats.m_BoundingBox[i] > index[i / 2])
{
labelStats.m_BoundingBox[i] = index[i / 2];
}
if (labelStats.m_BoundingBox[i + 1] < index[i / 2])
{
labelStats.m_BoundingBox[i + 1] = index[i / 2];
}
labelStats.m_BoundingBox[i] = std::min(labelStats.m_BoundingBox[i], index[i / 2]);
labelStats.m_BoundingBox[i + 1] = std::max(labelStats.m_BoundingBox[i + 1], index[i / 2]);
}

labelStats.m_Sum += value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkImageRegion.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionReverseIterator.h"
#include <algorithm> // For min and max.

namespace itk
{
Expand Down Expand Up @@ -73,16 +74,10 @@ BinomialBlurImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion
for (unsigned int i = 0; i < inputPtr->GetImageDimension(); ++i)
{
inputIndex[i] -= m_Repetitions;
if (inputIndex[i] < inputLargestPossibleRegionIndex[i])
{
inputIndex[i] = inputLargestPossibleRegionIndex[i];
}
inputIndex[i] = std::max(inputIndex[i], inputLargestPossibleRegionIndex[i]);

inputSize[i] += m_Repetitions;
if (inputSize[i] > inputLargestPossibleRegionSize[i])
{
inputSize[i] = inputLargestPossibleRegionSize[i];
}
inputSize[i] = std::min(inputSize[i], inputLargestPossibleRegionSize[i]);
}

inputRegion.SetIndex(inputIndex);
Expand Down
Loading

0 comments on commit 8f64fdb

Please sign in to comment.