Skip to content

Commit

Permalink
STYLE: Add constexpr to if (Dimension ...) statements
Browse files Browse the repository at this point in the history
Made it clearer that the condition of these `if` statements can be evaluated at
compile-time. Cases found by the regular expression ` if \(\w*Dimension `.

Also addressed `-Wunused-but-set-variable` warnings from ITK.Linux
(Ubuntu 9.4.0-1ubuntu1~20.04.2) saying:

> itkSobelOperator.hxx: warning: variable 'center' set but not used
> itkJensenHavrdaCharvatTsallisPointSetMetricTest.cxx: warning: variable 'metricValues2D' set but not used
> itkJensenHavrdaCharvatTsallisPointSetMetricTest.cxx: warning: variable 'metricValues3D' set but not used
  • Loading branch information
N-Dekker authored and hjmjohnson committed Jun 12, 2024
1 parent f448e3e commit a2707a8
Show file tree
Hide file tree
Showing 51 changed files with 131 additions and 124 deletions.
4 changes: 2 additions & 2 deletions Modules/Core/Common/include/itkCrossHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class CrossHelper
{
VectorType oCross;

if (Dimension > 2)
if constexpr (Dimension > 2)
{
oCross[0] = iU[1] * iV[2] - iV[1] * iU[2];
oCross[1] = iV[0] * iU[2] - iU[0] * iV[2];
oCross[2] = iU[0] * iV[1] - iV[0] * iU[1];

if (Dimension > 3)
if constexpr (Dimension > 3)
{
for (unsigned int dim = 3; dim < Dimension; ++dim)
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ operator<<(std::ostream & os, const Index<VDimension> & obj)
{
os << obj[i] << ", ";
}
if (VDimension >= 1)
if constexpr (VDimension >= 1)
{
os << obj[VDimension - 1];
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkOffset.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ operator<<(std::ostream & os, const Offset<VDimension> & ind)
{
os << ind[i] << ", ";
}
if (VDimension >= 1)
if constexpr (VDimension >= 1)
{
os << ind[VDimension - 1];
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkPoint.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ std::ostream &
operator<<(std::ostream & os, const Point<T, TPointDimension> & vct)
{
os << '[';
if (TPointDimension == 1)
if constexpr (TPointDimension == 1)
{
os << vct[0];
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkSize.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ operator<<(std::ostream & os, const Size<VDimension> & obj)
{
os << obj[i] << ", ";
}
if (VDimension >= 1)
if constexpr (VDimension >= 1)
{
os << obj[VDimension - 1];
}
Expand Down
55 changes: 29 additions & 26 deletions Modules/Core/Common/include/itkSobelOperator.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,46 @@ SobelOperator<TPixel, VDimension, TAllocator>::Fill(const CoefficientVector & co
{
this->InitializeToZero();

// Note that this code is only good for 2d and 3d operators. Places the
// coefficients in the exact center of the neighborhood
const unsigned int center = this->GetCenterNeighborhoodIndex();

if (VDimension == 3)
if constexpr (VDimension == 2 || VDimension == 3)
{
unsigned int coeff_index = 0;
for (int z = -1; z <= 1; ++z)
// Note that this code is only good for 2d and 3d operators. Places the
// coefficients in the exact center of the neighborhood
const unsigned int center = this->GetCenterNeighborhoodIndex();

if constexpr (VDimension == 3)
{
for (int y = -1; y <= 1; ++y)
unsigned int coeff_index = 0;
for (int z = -1; z <= 1; ++z)
{
for (int x = -1; x <= 1; ++x)
for (int y = -1; y <= 1; ++y)
{
const int pos = center + z * this->GetStride(2) + y * this->GetStride(1) + x * this->GetStride(0);
for (int x = -1; x <= 1; ++x)
{
const int pos = center + z * this->GetStride(2) + y * this->GetStride(1) + x * this->GetStride(0);

this->operator[](pos) = static_cast<TPixel>(coeff[coeff_index]);
this->operator[](pos) = static_cast<TPixel>(coeff[coeff_index]);

++coeff_index;
++coeff_index;
}
}
}
}
}
else if (VDimension == 2)
{
unsigned int coeff_index = 0;
for (int y = -1; y <= 1; ++y)
else // So now VDimension == 2
{
for (int x = -1; x <= 1; ++x)
unsigned int coeff_index = 0;
for (int y = -1; y <= 1; ++y)
{
const int pos = center + y * this->GetStride(1) + x * this->GetStride(0);
// Note, The following line copies the double precision
// coefficients of SobelOperator to the pixel type
// of the neighborhood operator which may not support
// negative numbers, or floating point numbers.
this->operator[](pos) = static_cast<TPixel>(coeff[coeff_index]);

++coeff_index;
for (int x = -1; x <= 1; ++x)
{
const int pos = center + y * this->GetStride(1) + x * this->GetStride(0);
// Note, The following line copies the double precision
// coefficients of SobelOperator to the pixel type
// of the neighborhood operator which may not support
// negative numbers, or floating point numbers.
this->operator[](pos) = static_cast<TPixel>(coeff[coeff_index]);

++coeff_index;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkVector.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ std::ostream &
operator<<(std::ostream & os, const Vector<T, TVectorDimension> & vct)
{
os << '[';
if (TVectorDimension == 1)
if constexpr (TVectorDimension == 1)
{
os << vct[0];
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/test/itkNeighborhoodAlgorithmTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ NeighborhoodAlgorithmTest()
region.SetIndex(ind);

size.Fill(5);
if (VDimension > 1)
if constexpr (VDimension > 1)
{
size[VDimension - 1] = 1;
}
Expand Down Expand Up @@ -184,7 +184,7 @@ NeighborhoodAlgorithmTest()
return false;
}

if (VDimension == 2)
if constexpr (VDimension == 2)
{
ind[0] = 0;
ind[1] = 249;
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/ImageAdaptors/test/itkVectorImageTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ itkVectorImageTest(int, char * argv[])
failed = true;
}

if (Dimension == 3)
if constexpr (Dimension == 3)
{

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ MetaBlobConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectType
Blob->GetPoints().push_back(pnt);
}

if (VDimension == 2)
if constexpr (VDimension == 2)
{
Blob->PointDim("x y red green blue alpha");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ MetaContourConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectT
contourMO->GetControlPoints().push_back(pnt);
}

if (VDimension == 2)
if constexpr (VDimension == 2)
{
contourMO->ControlPointDim("id x y xp yp v1 v2 r g b a");
}
else if (VDimension == 3)
else if constexpr (VDimension == 3)
{
contourMO->ControlPointDim("id x y z xp yp zp v1 v2 v3 r gn be a");
}
Expand All @@ -201,11 +201,11 @@ MetaContourConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectT
contourMO->GetInterpolatedPoints().push_back(pnt);
}

if (VDimension == 2)
if constexpr (VDimension == 2)
{
contourMO->InterpolatedPointDim("id x y r g b a");
}
else if (VDimension == 3)
else if constexpr (VDimension == 3)
{
contourMO->InterpolatedPointDim("id x y z r g b a");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ MetaDTITubeConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectT
{
pnt->AddField("v1x", it->GetNormal1InObjectSpace()[0]);
pnt->AddField("v1y", it->GetNormal1InObjectSpace()[1]);
if (VDimension == 3)
if constexpr (VDimension == 3)
{
pnt->AddField("v1z", it->GetNormal1InObjectSpace()[2]);
}
Expand All @@ -288,7 +288,7 @@ MetaDTITubeConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectT
{
pnt->AddField("v2x", it->GetNormal2InObjectSpace()[0]);
pnt->AddField("v2y", it->GetNormal2InObjectSpace()[1]);
if (VDimension == 3)
if constexpr (VDimension == 3)
{
pnt->AddField("v2z", it->GetNormal2InObjectSpace()[2]);
}
Expand All @@ -298,7 +298,7 @@ MetaDTITubeConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectT
{
pnt->AddField("tx", it->GetTangentInObjectSpace()[0]);
pnt->AddField("ty", it->GetTangentInObjectSpace()[1]);
if (VDimension == 3)
if constexpr (VDimension == 3)
{
pnt->AddField("tz", it->GetTangentInObjectSpace()[2]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ MetaLandmarkConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObject
landmarkMO->GetPoints().push_back(pnt);
}

if (VDimension == 2)
if constexpr (VDimension == 2)
{
landmarkMO->PointDim("x y red green blue alpha");
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/SpatialObjects/include/itkMetaLineConverter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ MetaLineConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectType
lineMO->GetPoints().push_back(pnt);
}

if (VDimension == 2)
if constexpr (VDimension == 2)
{
lineMO->PointDim("x y v1x v1y v2x v2y red green blue alpha");
}
else if (VDimension == 3)
else if constexpr (VDimension == 3)
{
lineMO->PointDim("x y z v1x v1y v1z v2x v2y v2z red green blue alpha");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ MetaSurfaceConverter<VDimension>::SpatialObjectToMetaObject(const SpatialObjectT
surfaceMO->GetPoints().push_back(pnt);
}

if (VDimension == 2)
if constexpr (VDimension == 2)
{
surfaceMO->PointDim("x y v1 v2 red green blue alpha");
}
else if (VDimension == 3)
else if constexpr (VDimension == 3)
{
surfaceMO->PointDim("x y z v1 v2 v3 red green blue alpha");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ SurfaceSpatialObject<TDimension, TSurfacePointType>::ComputeNormals()
PointType v2 = this->m_Points[identifier[1]].GetPositionInObjectSpace();
PointType v3 = this->m_Points[identifier[2]].GetPositionInObjectSpace();

if (TDimension == 3)
if constexpr (TDimension == 3)
{
double coa = -(v1[1] * (v2[2] - v3[2]) + v2[1] * (v3[2] - v1[2]) + v3[1] * (v1[2] - v2[2]));
double cob = -(v1[2] * (v2[0] - v3[0]) + v2[2] * (v3[0] - v1[0]) + v3[2] * (v1[0] - v2[0]));
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/SpatialObjects/include/itkTubeSpatialObject.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ TubeSpatialObject<TDimension, TTubePointType>::ComputeTangentsAndNormals()

// Compute the normal
CovariantVectorType n1;
if (TDimension == 2)
if constexpr (TDimension == 2)
{
// The normal to the tangent in 2D is the orthogonal direction to the tangent.
n1[0] = t[1];
Expand All @@ -447,7 +447,7 @@ TubeSpatialObject<TDimension, TTubePointType>::ComputeTangentsAndNormals()
((TubePointType *)(this->GetPoint(it1)))->SetNormal1InObjectSpace(n1);
prevN1 = n1;
}
else if (TDimension == 3)
else if constexpr (TDimension == 3)
{
// The normal to the tangent in 3D is the cross product of adjacent tangent directions.
n1[0] = t[1] * t2[2] - t[2] * t2[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ MRIBiasFieldCorrectionFilter<TInputImage, TOutputImage, TMaskImage>::MRIBiasFiel
{
m_NormalVariateGenerator->Initialize(time(nullptr));

if (ImageDimension == 3)
if constexpr (ImageDimension == 3)
{
m_UsingInterSliceIntensityCorrection = true;
m_SlicingDirection = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ TimeVaryingVelocityFieldIntegrationImageFilter<TTimeVaryingVelocityField,
this->m_NumberOfTimePoints = 0;
this->SetNumberOfRequiredInputs(1);

if (InputImageDimension - 1 != OutputImageDimension)
if constexpr (InputImageDimension - 1 != OutputImageDimension)
{
itkExceptionMacro("The time-varying velocity field (input) should have "
<< "dimensionality of 1 greater than the deformation field (output). ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,13 @@ FastMarchingImageFilterBase<TInput, TOutput>::InitializeOutput(OutputImageType *
// Initialize indices if this->m_TopologyCheck is activated
if (this->m_TopologyCheck != Superclass::TopologyCheckEnum::Nothing)
{
if (ImageDimension == 2)
if constexpr (ImageDimension == 2)
{
InitializeIndices2D();
}
else
{
if (ImageDimension == 3)
if constexpr (ImageDimension == 3)
{
InitializeIndices3D();
}
Expand All @@ -596,7 +596,7 @@ bool
FastMarchingImageFilterBase<TInput, TOutput>::DoesVoxelChangeViolateWellComposedness(const NodeType & idx) const
{
bool isChangeWellComposed = false;
if (ImageDimension == 2)
if constexpr (ImageDimension == 2)
{
isChangeWellComposed = this->IsChangeWellComposed2D(idx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ GPUScalarAnisotropicDiffusionFunction<TImage>::GPUScalarAnisotropicDiffusionFunc
// load GPU kernel
std::ostringstream defines;

if (ImageDimension > 3 || ImageDimension < 1)
if constexpr (ImageDimension > 3 || ImageDimension < 1)
{
itkExceptionMacro("GPUScalarAnisotropicDiffusionFunction supports 1/2/3D image.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ runGPUMeanImageFilterTest(const std::string & inFile, const std::string & outFil
typename InputImageType::SizeType indexRadius;
indexRadius[0] = 2; // radius along x
indexRadius[1] = 2; // radius along y
if (VImageDimension > 2)
if constexpr (VImageDimension > 2)
{
indexRadius[2] = 2; // radius along z
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ runGPUImageFilterTest(const std::string & inFile, const std::string & outFile)
typename InputImageType::SizeType indexRadius;
indexRadius[0] = 2; // radius along x
indexRadius[1] = 2; // radius along y
if (VImageDimension > 2)
if constexpr (VImageDimension > 2)
{
indexRadius[2] = 2; // radius along z
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ DiscreteGaussianDerivativeImageFilter<TInputImage, TOutputImage>::GenerateData()
}

// Create a chain of filters
if (ImageDimension == 1)
if constexpr (ImageDimension == 1)
{
// Use just a single filter
SingleFilterPointer singleFilter = SingleFilterType::New();
Expand Down Expand Up @@ -212,7 +212,7 @@ DiscreteGaussianDerivativeImageFilter<TInputImage, TOutputImage>::GenerateData()

// Middle filters convolves from real to real
std::vector<IntermediateFilterPointer> intermediateFilters;
if (ImageDimension > 2)
if constexpr (ImageDimension > 2)
{
const unsigned int max_dim = ImageDimension - 1;
for (unsigned int i = 1; i != max_dim; ++i)
Expand All @@ -239,7 +239,7 @@ DiscreteGaussianDerivativeImageFilter<TInputImage, TOutputImage>::GenerateData()
LastFilterPointer lastFilter = LastFilterType::New();
lastFilter->SetOperator(oper[ImageDimension - 1]);
lastFilter->ReleaseDataFlagOn();
if (ImageDimension > 2)
if constexpr (ImageDimension > 2)
{
const unsigned int temp_dim = ImageDimension - 3;
lastFilter->SetInput(intermediateFilters[temp_dim]->GetOutput());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ LaplacianRecursiveGaussianImageFilter<TInputImage, TOutputImage>::GenerateData()

// Because the output of last filter in the mini-pipeline is not
// pipelined the data must be manually released
if (ImageDimension > 1)
if constexpr (ImageDimension > 1)
{
m_SmoothingFilters[ImageDimension - 2]->GetOutput()->ReleaseData();
}
Expand Down
Loading

0 comments on commit a2707a8

Please sign in to comment.