Skip to content

Commit

Permalink
STYLE: Replace std::min, std::max in SliceImageFilter with std::clamp
Browse files Browse the repository at this point in the history
Follow-up to pull request #3992
commit fa667eb
"STYLE: Replace `std::min` and `std::max` calls with C++17 `std::clamp`"
  • Loading branch information
N-Dekker committed Nov 14, 2023
1 parent 118581e commit 007d93f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions Modules/Filtering/ImageGrid/include/itkSliceImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ SliceImageFilter<TInputImage, TOutputImage>::DynamicThreadedGenerateData(
InputIndexType start;
for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
{
start[i] = std::max(m_Start[i], inputIndex[i]);
start[i] = std::min(start[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
start[i] = std::clamp(m_Start[i], inputIndex[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
}

// Define/declare an iterator that will walk the output region for this thread
Expand Down Expand Up @@ -179,8 +178,7 @@ SliceImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion()
{
// clamp to valid index range and don't include one past end, so
// that a zero size RR would be valid
start[i] = std::max(m_Start[i], inputIndex[i]);
start[i] = std::min(start[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
start[i] = std::clamp(m_Start[i], inputIndex[i], static_cast<IndexValueType>(inputIndex[i] + inputSize[i] - 1));
}


Expand Down Expand Up @@ -247,14 +245,17 @@ SliceImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
outputSpacing[i] = inputSpacing[i] * itk::Math::abs(m_Step[i]);

// clamp start, inclusive start interval
IndexValueType start = std::max(m_Start[i], inputIndex[i] - static_cast<int>(m_Step[i] < 0));
start =
std::min(start, static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));
IndexValueType start =
std::clamp(m_Start[i],
inputIndex[i] - static_cast<int>(m_Step[i] < 0),
static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));

// clamp stop as open interval
// Based on the sign of the step include 1 after the end.
IndexValueType stop = std::max(m_Stop[i], inputIndex[i] - static_cast<int>(m_Step[i] < 0));
stop = std::min(stop, static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));
IndexValueType stop =
std::clamp(m_Stop[i],
inputIndex[i] - static_cast<int>(m_Step[i] < 0),
static_cast<IndexValueType>(inputIndex[i] + inputSize[i]) - static_cast<int>(m_Step[i] < 0));

// If both the numerator and the denominator have the same sign,
// then the range is a valid and non-zero sized. Truncation is the
Expand Down

0 comments on commit 007d93f

Please sign in to comment.