Skip to content

Commit

Permalink
STYLE: Use range-based loops from C++11
Browse files Browse the repository at this point in the history
C++11 Range based for loops can be used in

Used as a more readable equivalent to the traditional for loop operating over a
range of values, such as all elements in a container, in the forward direction..

Range based loopes are more explicit for only computing the
end location once for containers.
  • Loading branch information
hjmjohnson committed Dec 11, 2024
1 parent 63b463b commit 4ba07dc
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1003,29 +1003,29 @@ RayCastInterpolateImageFunction<TInputImage, TCoordinate>::RayCastHelper::Reset(

else
{
for (int i = 0; i < 3; ++i)
for (double & it : m_RayVoxelStartPosition)
{
m_RayVoxelStartPosition[i] = 0.;
it = 0.;
}
for (int i = 0; i < 3; ++i)
for (double & it : m_RayVoxelEndPosition)
{
m_RayVoxelEndPosition[i] = 0.;
it = 0.;
}
for (int i = 0; i < 3; ++i)
for (double & it : m_VoxelIncrement)
{
m_VoxelIncrement[i] = 0.;
it = 0.;
}
m_TraversalDirection = TraversalDirectionEnum::UNDEFINED_DIRECTION;

m_TotalRayVoxelPlanes = 0;

for (int i = 0; i < 4; ++i)
for (auto & m_RayIntersectionVoxel : m_RayIntersectionVoxels)
{
m_RayIntersectionVoxels[i] = nullptr;
m_RayIntersectionVoxel = nullptr;
}
for (int i = 0; i < 3; ++i)
for (int & it : m_RayIntersectionVoxelIndex)
{
m_RayIntersectionVoxelIndex[i] = 0;
it = 0;
}
}
}
Expand Down Expand Up @@ -1304,38 +1304,38 @@ RayCastInterpolateImageFunction<TInputImage, TCoordinate>::RayCastHelper::ZeroSt
m_VoxelDimensionInY = 0;
m_VoxelDimensionInZ = 0;

for (int i = 0; i < 3; ++i)
for (double & it : m_CurrentRayPositionInMM)
{
m_CurrentRayPositionInMM[i] = 0.;
it = 0.;
}
for (int i = 0; i < 3; ++i)
for (double & it : m_RayDirectionInMM)
{
m_RayDirectionInMM[i] = 0.;
it = 0.;
}
for (int i = 0; i < 3; ++i)
for (double & it : m_RayVoxelStartPosition)
{
m_RayVoxelStartPosition[i] = 0.;
it = 0.;
}
for (int i = 0; i < 3; ++i)
for (double & it : m_RayVoxelEndPosition)
{
m_RayVoxelEndPosition[i] = 0.;
it = 0.;
}
for (int i = 0; i < 3; ++i)
for (double & it : m_VoxelIncrement)
{
m_VoxelIncrement[i] = 0.;
it = 0.;
}
m_TraversalDirection = TraversalDirectionEnum::UNDEFINED_DIRECTION;

m_TotalRayVoxelPlanes = 0;
m_NumVoxelPlanesTraversed = -1;

for (int i = 0; i < 4; ++i)
for (auto & m_RayIntersectionVoxel : m_RayIntersectionVoxels)
{
m_RayIntersectionVoxels[i] = nullptr;
m_RayIntersectionVoxel = nullptr;
}
for (int i = 0; i < 3; ++i)
for (int & it : m_RayIntersectionVoxelIndex)
{
m_RayIntersectionVoxelIndex[i] = 0;
it = 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ itkGaussianInterpolateImageFunctionTest(int, char *[])
{
point[1] = 0.0;

for (unsigned int j = 0; j < 5; ++j)
for (double it : expectedValue)
{
const InterpolatorType::OutputType computedValue = interpolator->Evaluate(point);

if (!itk::Math::FloatAlmostEqual(computedValue, expectedValue[j], 7, 5e-6))
if (!itk::Math::FloatAlmostEqual(computedValue, it, 7, 5e-6))
{
std::cerr << "Error: computed and expected values are different" << std::endl;
std::cerr << "Point: " << point << std::endl;
std::cerr << "Computed: " << computedValue << std::endl;
std::cerr << "Expected: " << expectedValue[j] << std::endl;
std::cerr << "Expected: " << it << std::endl;
return EXIT_FAILURE;
}

Expand Down
14 changes: 5 additions & 9 deletions Modules/Core/Mesh/test/itkMeshTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,10 @@ itkMeshTest(int, char *[])
auto cellVectorContainer = MeshType::CellsVectorContainer::New();
int index = 0;
unsigned long value = 1;
for (std::map<itk::CellGeometryEnum, unsigned int>::iterator iter = cellPointMap.begin();
iter != cellPointMap.end();
++iter)
for (auto & iter : cellPointMap)
{
const itk::CellGeometryEnum cellType = iter->first;
const unsigned int numOfPoints = iter->second;
const itk::CellGeometryEnum cellType = iter.first;
const unsigned int numOfPoints = iter.second;

// Insert cell type
cellVectorContainer->InsertElement(index++, static_cast<unsigned long>(cellType));
Expand All @@ -231,11 +229,9 @@ itkMeshTest(int, char *[])

// Check if right cells are recovered
index = 0;
for (std::map<itk::CellGeometryEnum, unsigned int>::iterator iter = cellPointMap.begin();
iter != cellPointMap.end();
++iter)
for (auto & iter : cellPointMap)
{
const unsigned int numOfPoints = iter->second;
const unsigned int numOfPoints = iter.second;
CellAutoPointer temp_cell;

if (mesh0->GetCell(index++, temp_cell))
Expand Down
8 changes: 4 additions & 4 deletions Modules/Core/Transform/include/itkCompositeTransform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ CompositeTransform<TParametersValueType, VDimension>::GetInverse(Self * inverse)

/* Copy the optimization flags */
inverse->m_TransformsToOptimizeFlags.clear();
for (auto ofit = this->m_TransformsToOptimizeFlags.begin(); ofit != this->m_TransformsToOptimizeFlags.end(); ++ofit)
for (bool m_TransformsToOptimizeFlag : this->m_TransformsToOptimizeFlags)
{
inverse->m_TransformsToOptimizeFlags.push_front(*ofit);
inverse->m_TransformsToOptimizeFlags.push_front(m_TransformsToOptimizeFlag);
}

return true;
Expand Down Expand Up @@ -951,9 +951,9 @@ CompositeTransform<TParametersValueType, VDimension>::PrintSelf(std::ostream & o
Superclass::PrintSelf(os, indent);

os << indent << "TransformsToOptimizeFlags: " << std::endl << indent << indent;
for (auto it = m_TransformsToOptimizeFlags.begin(); it != m_TransformsToOptimizeFlags.end(); ++it)
for (bool m_TransformsToOptimizeFlag : m_TransformsToOptimizeFlags)
{
os << indent.GetNextIndent() << *it << ' ';
os << indent.GetNextIndent() << m_TransformsToOptimizeFlag << ' ';
}
os << std::endl;

Expand Down
30 changes: 15 additions & 15 deletions Modules/Core/Transform/test/itkBSplineDeformableTransformTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,26 @@ itkBSplineDeformableTransformTest1()
using CoefficientType = ParametersType::ValueType;
using CoefficientImageType = itk::Image<CoefficientType, SpaceDimension>;

CoefficientImageType::Pointer coeffImage[SpaceDimension];
CoefficientImageType::Pointer coeffImages[SpaceDimension];
const unsigned int numberOfControlPoints = region.GetNumberOfPixels();
CoefficientType * dataPointer = parameters.data_block();
for (unsigned int j = 0; j < SpaceDimension; ++j)
for (auto & it : coeffImages)
{
coeffImage[j] = CoefficientImageType::New();
coeffImage[j]->SetRegions(region);
coeffImage[j]->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
it = CoefficientImageType::New();
it->SetRegions(region);
it->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
dataPointer += numberOfControlPoints;
coeffImage[j]->FillBuffer(0.0);
it->FillBuffer(0.0);
}

/**
* Populate the spline coefficients with some values.
*/
auto index = CoefficientImageType::IndexType::Filled(5);

coeffImage[1]->SetPixel(index, 1.0);
coeffImages[1]->SetPixel(index, 1.0);

const unsigned long n = coeffImage[1]->ComputeOffset(index) + numberOfControlPoints;
const unsigned long n = coeffImages[1]->ComputeOffset(index) + numberOfControlPoints;

/**
* Set the parameters in the transform
Expand Down Expand Up @@ -619,24 +619,24 @@ itkBSplineDeformableTransformTest3()
using CoefficientType = ParametersType::ValueType;
using CoefficientImageType = itk::Image<CoefficientType, SpaceDimension>;

CoefficientImageType::Pointer coeffImage[SpaceDimension];
CoefficientImageType::Pointer coeffImages[SpaceDimension];
const unsigned int numberOfControlPoints = region.GetNumberOfPixels();
CoefficientType * dataPointer = parameters.data_block();
for (unsigned int j = 0; j < SpaceDimension; ++j)
for (auto & it : coeffImages)
{
coeffImage[j] = CoefficientImageType::New();
coeffImage[j]->SetRegions(region);
coeffImage[j]->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
it = CoefficientImageType::New();
it->SetRegions(region);
it->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
dataPointer += numberOfControlPoints;
coeffImage[j]->FillBuffer(0.0);
it->FillBuffer(0.0);
}

/**
* Populate the spline coefficients with some values.
*/
auto index = CoefficientImageType::IndexType::Filled(5);

coeffImage[1]->SetPixel(index, 1.0);
coeffImages[1]->SetPixel(index, 1.0);

/**
* Set the parameters in the transform
Expand Down
30 changes: 15 additions & 15 deletions Modules/Core/Transform/test/itkBSplineTransformTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ itkBSplineTransformTest1()
using CoefficientType = ParametersType::ValueType;
using CoefficientImageType = itk::Image<CoefficientType, SpaceDimension>;

CoefficientImageType::Pointer coeffImage[SpaceDimension];
CoefficientImageType::Pointer coeffImages[SpaceDimension];
CoefficientImageType::SizeType size;
unsigned int numberOfControlPoints = 0;
for (unsigned int j = 0; j < SpaceDimension; ++j)
Expand All @@ -147,23 +147,23 @@ itkBSplineTransformTest1()
numberOfControlPoints += size[j];
}
CoefficientType * dataPointer = parameters.data_block();
for (unsigned int j = 0; j < SpaceDimension; ++j)
for (auto & it : coeffImages)
{
coeffImage[j] = CoefficientImageType::New();
coeffImage[j]->SetRegions(size);
coeffImage[j]->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
it = CoefficientImageType::New();
it->SetRegions(size);
it->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
dataPointer += numberOfControlPoints;
coeffImage[j]->FillBuffer(0.0);
it->FillBuffer(0.0);
}

/**
* Populate the spline coefficients with some values.
*/
auto index = CoefficientImageType::IndexType::Filled(5);

coeffImage[1]->SetPixel(index, 1.0);
coeffImages[1]->SetPixel(index, 1.0);

const unsigned long n = coeffImage[1]->ComputeOffset(index) + numberOfControlPoints;
const unsigned long n = coeffImages[1]->ComputeOffset(index) + numberOfControlPoints;

/**
* Set the parameters in the transform
Expand Down Expand Up @@ -642,7 +642,7 @@ itkBSplineTransformTest3()
using CoefficientType = ParametersType::ValueType;
using CoefficientImageType = itk::Image<CoefficientType, SpaceDimension>;

CoefficientImageType::Pointer coeffImage[SpaceDimension];
CoefficientImageType::Pointer coeffImages[SpaceDimension];
CoefficientImageType::SizeType size;
unsigned int numberOfControlPoints = 0;
for (unsigned int j = 0; j < SpaceDimension; ++j)
Expand All @@ -651,21 +651,21 @@ itkBSplineTransformTest3()
numberOfControlPoints += size[j];
}
CoefficientType * dataPointer = parameters.data_block();
for (unsigned int j = 0; j < SpaceDimension; ++j)
for (auto & it : coeffImages)
{
coeffImage[j] = CoefficientImageType::New();
coeffImage[j]->SetRegions(size);
coeffImage[j]->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
it = CoefficientImageType::New();
it->SetRegions(size);
it->GetPixelContainer()->SetImportPointer(dataPointer, numberOfControlPoints);
dataPointer += numberOfControlPoints;
coeffImage[j]->FillBuffer(0.0);
it->FillBuffer(0.0);
}

/**
* Populate the spline coefficients with some values.
*/
auto index = CoefficientImageType::IndexType::Filled(5);

coeffImage[1]->SetPixel(index, 1.0);
coeffImages[1]->SetPixel(index, 1.0);

/**
* Set the parameters in the transform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ MRIBiasEnergyFunction<TImage, TImageMask, TBiasField>::MRIBiasEnergyFunction()
, m_Image(nullptr)
, m_Mask(nullptr)
{
for (unsigned int i = 0; i < SpaceDimension; ++i)
for (unsigned int & it : m_SamplingFactor)
{
m_SamplingFactor[i] = 1;
it = 1;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,12 @@ FastMarchingImageFilterBase<TInput, TOutput>::IsChangeWellComposed2D(const NodeT

// Check for critical configurations: 4 90-degree rotations

for (unsigned int i = 0; i < 4; ++i)
for (const auto & m_RotationIndice : this->m_RotationIndices)
{
for (unsigned int j = 0; j < 9; ++j)
{
neighborhoodPixels[j] = (It.GetPixel(this->m_RotationIndices[i][j]) != Traits::Alive);
if (this->m_RotationIndices[i][j] == 4)
neighborhoodPixels[j] = (It.GetPixel(m_RotationIndice[j]) != Traits::Alive);
if (m_RotationIndice[j] == 4)
{
neighborhoodPixels.flip(j);
}
Expand All @@ -653,12 +653,12 @@ FastMarchingImageFilterBase<TInput, TOutput>::IsChangeWellComposed2D(const NodeT
// Note that the reflections for the C1 and C2 cases are covered by the
// rotation cases above (except in the case of FullInvariance == false).

for (unsigned int i = 0; i < 2; ++i)
for (const auto & m_ReflectionIndice : this->m_ReflectionIndices)
{
for (unsigned int j = 0; j < 9; ++j)
{
neighborhoodPixels[j] = (It.GetPixel(this->m_ReflectionIndices[i][j]) != Traits::Alive);
if (this->m_ReflectionIndices[i][j] == 4)
neighborhoodPixels[j] = (It.GetPixel(m_ReflectionIndice[j]) != Traits::Alive);
if (m_ReflectionIndice[j] == 4)
{
neighborhoodPixels.flip(j);
}
Expand Down Expand Up @@ -871,13 +871,13 @@ template <typename TInput, typename TOutput>
void
FastMarchingImageFilterBase<TInput, TOutput>::InitializeIndices3D()
{
for (unsigned int i = 0; i < 12; ++i)
for (auto & m_C1Indice : this->m_C1Indices)
{
this->m_C1Indices[i].SetSize(4);
m_C1Indice.SetSize(4);
}
for (unsigned int i = 0; i < 8; ++i)
for (auto & m_C2Indice : this->m_C2Indices)
{
this->m_C2Indices[i].SetSize(8);
m_C2Indice.SetSize(8);
}

this->m_C1Indices[0][0] = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ MaskFeaturePointSelectionFilter<TImage, TMask, TFeatures>::GenerateData()

// mark off connected points
constexpr MapPixelType ineligeblePointCode = 0;
for (size_t j = 0, n = m_NonConnectivityOffsets.size(); j < n; ++j)
for (const auto & m_NonConnectivityOffset : m_NonConnectivityOffsets)
{
IndexType idx = rit->second;
idx += m_NonConnectivityOffsets[j];
idx += m_NonConnectivityOffset;
selectionMap->SetPixel(idx, ineligeblePointCode);
}
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Filtering/ImageGrid/test/itkExpandImageFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ itkExpandImageFilterTest(int, char *[])

ImagePattern<ImageDimension> pattern;
pattern.m_Offset = 64;
for (unsigned int j = 0; j < ImageDimension; ++j)
for (double & it : pattern.m_Coeff)
{
pattern.m_Coeff[j] = 1.0;
it = 1.0;
}

using Iterator = itk::ImageRegionIteratorWithIndex<ImageType>;
Expand Down
Loading

0 comments on commit 4ba07dc

Please sign in to comment.