Skip to content

Commit

Permalink
STYLE: Move GetSpacing() calls out of for loops
Browse files Browse the repository at this point in the history
Avoided calling `GetSpacing()` repetitively in a `for` loop, by using a local
`spacing` variable.

Cases found by the following regular expressions:

    if \(m_UseImageSpacing\).+    for .+->GetSpacing\(\)\[
    if \(this->GetUseImageSpacing\(\)\).+    for .+->GetSpacing\(\)\[

Aims to improve code readability.
  • Loading branch information
N-Dekker authored and hjmjohnson committed Mar 17, 2024
1 parent 2ff0480 commit c0ebd8e
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ AnisotropicDiffusionImageFilter<TInputImage, TOutputImage>::InitializeIteration(
double minSpacing;
if (this->GetUseImageSpacing())
{
minSpacing = this->GetInput()->GetSpacing()[0];
const auto & spacing = this->GetInput()->GetSpacing();

minSpacing = spacing[0];
for (unsigned int i = 1; i < ImageDimension; ++i)
{
if (this->GetInput()->GetSpacing()[i] < minSpacing)
if (spacing[i] < minSpacing)
{
minSpacing = this->GetInput()->GetSpacing()[i];
minSpacing = spacing[i];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ DisplacementFieldJacobianDeterminantFilter<TInputImage, TRealType, TOutputImage>
// in case our input image has changed.
if (m_UseImageSpacing)
{
const auto & spacing = this->GetInput()->GetSpacing();

for (unsigned int i = 0; i < ImageDimension; ++i)
{
if (static_cast<TRealType>(this->GetInput()->GetSpacing()[i]) == 0.0)
if (static_cast<TRealType>(spacing[i]) == 0.0)
{
itkExceptionMacro("Image spacing in dimension " << i << " is zero.");
}
m_DerivativeWeights[i] = static_cast<TRealType>(1.0 / static_cast<TRealType>(this->GetInput()->GetSpacing()[i]));
m_DerivativeWeights[i] = static_cast<TRealType>(1.0 / static_cast<TRealType>(spacing[i]));
m_HalfDerivativeWeights[i] = 0.5 * m_DerivativeWeights[i];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ GPUAnisotropicDiffusionImageFilter<TInputImage, TOutputImage, TParentImageFilter
double minSpacing;
if (this->GetUseImageSpacing())
{
minSpacing = this->GetInput()->GetSpacing()[0];
const auto & spacing = this->GetInput()->GetSpacing();

minSpacing = spacing[0];
for (unsigned int i = 1; i < ImageDimension; ++i)
{
if (this->GetInput()->GetSpacing()[i] < minSpacing)
if (spacing[i] < minSpacing)
{
minSpacing = this->GetInput()->GetSpacing()[i];
minSpacing = spacing[i];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ VectorGradientMagnitudeImageFilter<TInputImage, TRealType, TOutputImage>::Before
// in case our input image has changed.
if (m_UseImageSpacing)
{
const auto & spacing = this->GetInput()->GetSpacing();

for (unsigned int i = 0; i < ImageDimension; ++i)
{
if (static_cast<TRealType>(this->GetInput()->GetSpacing()[i]) == 0.0)
if (static_cast<TRealType>(spacing[i]) == 0.0)
{
itkExceptionMacro("Image spacing in dimension " << i << " is zero.");
}
m_DerivativeWeights[i] = static_cast<TRealType>(1.0 / static_cast<TRealType>(this->GetInput()->GetSpacing()[i]));
m_DerivativeWeights[i] = static_cast<TRealType>(1.0 / static_cast<TRealType>(spacing[i]));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ DiscreteGaussianImageFilter<TInputImage, TOutputImage>::GetKernelVarianceArray()
itkExceptionMacro("Could not get kernel variance! UseImageSpacing is ON but no input image was provided");
}

const auto & spacing = this->GetInput()->GetSpacing();

ArrayType adjustedVariance;
// Adjusted variance = var / (spacing ^ 2)
for (unsigned int dim = 0; dim < ImageDimension; ++dim)
{
// convert the variance from physical units to pixels
double s = this->GetInput()->GetSpacing()[dim];
double s = spacing[dim];
s = s * s;
adjustedVariance[dim] = m_Variance[dim] / s;
}
Expand Down
4 changes: 3 additions & 1 deletion Modules/Nonunit/Review/include/itkAreaClosingImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ class AreaClosingImageFilter
this->m_AttributeValuePerPixel = 1;
if (m_UseImageSpacing)
{
const auto & spacing = this->GetInput()->GetSpacing();

// compute pixel size
double psize = 1.0;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
psize *= this->GetInput()->GetSpacing()[i];
psize *= spacing[i];
}
this->m_AttributeValuePerPixel = static_cast<AttributeType>(psize);
// std::cout << "m_AttributeValuePerPixel: " <<
Expand Down
4 changes: 3 additions & 1 deletion Modules/Nonunit/Review/include/itkAreaOpeningImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ class AreaOpeningImageFilter
this->m_AttributeValuePerPixel = 1;
if (m_UseImageSpacing)
{
const auto & spacing = this->GetInput()->GetSpacing();

// compute pixel size
double psize = 1.0;
for (unsigned int i = 0; i < ImageDimension; ++i)
{
psize *= this->GetInput()->GetSpacing()[i];
psize *= spacing[i];
}
this->m_AttributeValuePerPixel = static_cast<AttributeType>(psize);
// std::cout << "m_AttributeValuePerPixel: " <<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ MultiphaseFiniteDifferenceImageFilter<TInputImage, TFeatureImage, TOutputImage,
unsigned int i;
if (m_UseImageSpacing)
{
const auto & spacing = m_LevelSet[0]->GetSpacing();

for (i = 0; i < ImageDimension; ++i)
{
coeffs[i] = 1.0 / m_LevelSet[0]->GetSpacing()[i];
coeffs[i] = 1.0 / spacing[i];
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ WarpHarmonicEnergyCalculator<TInputImage>::Compute()
// in case our input image has changed.
if (m_UseImageSpacing)
{
const auto & spacing = m_Image->GetSpacing();

for (unsigned int i = 0; i < ImageDimension; ++i)
{
if (m_Image->GetSpacing()[i] <= 0.0)
if (spacing[i] <= 0.0)
{
itkExceptionMacro("Image spacing in dimension " << i << " is zero.");
}
m_DerivativeWeights[i] = 1.0 / static_cast<double>(m_Image->GetSpacing()[i]);
m_DerivativeWeights[i] = 1.0 / static_cast<double>(spacing[i]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,12 @@ ParallelSparseFieldLevelSetImageFilter<TInputImage, TOutputImage>::InitializeAct

if (this->GetUseImageSpacing())
{
const auto & spacing = this->GetInput()->GetSpacing();

SpacePrecisionType minSpacing = NumericTraits<SpacePrecisionType>::max();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
minSpacing = std::min(minSpacing, this->GetInput()->GetSpacing()[i]);
minSpacing = std::min(minSpacing, spacing[i]);
}
MIN_NORM *= minSpacing;
}
Expand Down Expand Up @@ -1267,10 +1269,12 @@ ParallelSparseFieldLevelSetImageFilter<TInputImage, TOutputImage>::ThreadedCalcu
ValueType MIN_NORM = 1.0e-6;
if (this->GetUseImageSpacing())
{
const auto & spacing = this->GetInput()->GetSpacing();

SpacePrecisionType minSpacing = NumericTraits<SpacePrecisionType>::max();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
minSpacing = std::min(minSpacing, this->GetInput()->GetSpacing()[i]);
minSpacing = std::min(minSpacing, spacing[i]);
}
MIN_NORM *= minSpacing;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,12 @@ SparseFieldLevelSetImageFilter<TInputImage, TOutputImage>::Initialize()

if (this->GetUseImageSpacing())
{
const auto & spacing = this->GetInput()->GetSpacing();

SpacePrecisionType minSpacing = NumericTraits<SpacePrecisionType>::max();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
minSpacing = std::min(minSpacing, this->GetInput()->GetSpacing()[i]);
minSpacing = std::min(minSpacing, spacing[i]);
}
m_ConstantGradientValue = minSpacing;
}
Expand Down Expand Up @@ -782,10 +784,12 @@ SparseFieldLevelSetImageFilter<TInputImage, TOutputImage>::InitializeActiveLayer

if (this->GetUseImageSpacing())
{
const auto & spacing = this->GetInput()->GetSpacing();

SpacePrecisionType minSpacing = NumericTraits<SpacePrecisionType>::max();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
minSpacing = std::min(minSpacing, this->GetInput()->GetSpacing()[i]);
minSpacing = std::min(minSpacing, spacing[i]);
}
MIN_NORM *= minSpacing;
}
Expand Down Expand Up @@ -858,10 +862,12 @@ SparseFieldLevelSetImageFilter<TInputImage, TOutputImage>::CalculateChange() ->
ValueType MIN_NORM = 1.0e-6;
if (this->GetUseImageSpacing())
{
const auto & spacing = this->GetInput()->GetSpacing();

SpacePrecisionType minSpacing = NumericTraits<SpacePrecisionType>::max();
for (i = 0; i < ImageDimension; ++i)
{
minSpacing = std::min(minSpacing, this->GetInput()->GetSpacing()[i]);
minSpacing = std::min(minSpacing, spacing[i]);
}
MIN_NORM *= minSpacing;
}
Expand Down

0 comments on commit c0ebd8e

Please sign in to comment.