Skip to content

Commit

Permalink
ENH: Make Vector construction from scalar value explicit
Browse files Browse the repository at this point in the history
This prevents unintentional conversion of a scalar value into a Vector
filled with that value.

For example, when assigning the result of a Vector dot product back to
another Vector without an explicit usage of the Vector constructor,
a compilation error will now be raised.

Thanks to Wang Chengjia and Tim Allman for reporting this issue.

COMP: Keep deprecated version around unless ITK_FUTURE_LEGACY_REMOVE

Use of ::Zero is deprecated since commit 3f712cd.

Change-Id: Ie64b73af31390f9b6bc34912cf62be627fb815a0
  • Loading branch information
brianhelba authored and Christopher Mullins committed Jan 26, 2015
1 parent ce1c049 commit a890962
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 16 deletions.
11 changes: 10 additions & 1 deletion Modules/Core/Common/include/itkVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,17 @@ class Vector:public FixedArray< T, NVectorDimension >
/** Default constructor and copy constructors. */
Vector():BaseArray() {}

/** Constructor to initialize entire vector to one value. */
#if !defined( ITK_LEGACY_FUTURE_REMOVE )
/** Constructor to initialize entire vector to one value.
* \warning Not intended to convert a scalar value into
* a Vector filled with that value.
* \deprecated */
Vector(const ValueType & r);
#else
/** Constructor to initialize entire vector to one value,
* if explicitly invoked. */
explicit Vector(const ValueType & r);
#endif

/** Pass-through constructor for the Array base class. */
template< typename TVectorValueType >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ void CopyMeshToMeshCellData(const TInputMesh *in, TOutputMesh *out)
InputCellDataContainerConstIterator inIt = inputCellData->Begin();
while ( inIt != inputCellData->End() )
{
outputCellData->SetElement( inIt.Index(), inIt.Value() );
inIt++;
typename OutputCellDataContainer::Element point(inIt.Value());
outputCellData->SetElement( inIt.Index(), point );
++inIt;
}

out->SetCellData(outputCellData);
Expand Down Expand Up @@ -196,7 +197,8 @@ void CopyMeshToMeshPointData(const TInputMesh *in, TOutputMesh *out)
InputPointDataContainerConstIterator inIt = inputPointData->Begin();
while ( inIt != inputPointData->End() )
{
outputPointData->SetElement( inIt.Index(), inIt.Value() );
typename OutputPointDataContainer::Element point( inIt.Value() );
outputPointData->SetElement( inIt.Index(), point );
inIt++;
}

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 @@ -152,7 +152,7 @@ TubeSpatialObject< TDimension, TTubePointType >
{
// First we compute the bounding box in the index space
typename BoundingBoxType::Pointer bb = BoundingBoxType::New();
VectorType rad = ( *it ).GetRadius();
VectorType rad(( *it ).GetRadius());
PointType ptMin = ( *it ).GetPosition() - rad;
PointType ptMax = ( *it ).GetPosition() + rad;
bb->SetMinimum(ptMin);
Expand All @@ -166,7 +166,7 @@ TubeSpatialObject< TDimension, TTubePointType >
it++;
while ( it != end )
{
rad = ( *it ).GetRadius();
rad = VectorType(( *it ).GetRadius());
ptMin = ( *it ).GetPosition() - rad;
ptMax = ( *it ).GetPosition() + rad;
bb->ConsiderPoint(ptMin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int itkSpatialObjectToImageStatisticsCalculatorTest(int, char * [] )
std::cout << "Sample mean = " << calculator->GetMean() << std::endl;
std::cout << "Sample covariance = " << calculator->GetCovarianceMatrix();

if(calculator->GetMean() != 255
if(calculator->GetMean() != CalculatorType::VectorType(255)
|| calculator->GetCovarianceMatrix()[0][0] != 0
)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ BSplineScatteredDataPointSetToImageFilter<TInputPointSet, TOutputImage>
this->m_DeltaLatticePerThread[n] = PointDataImageType::New();
this->m_DeltaLatticePerThread[n]->SetRegions( size );
this->m_DeltaLatticePerThread[n]->Allocate();
this->m_DeltaLatticePerThread[n]->FillBuffer( 0.0 );
this->m_DeltaLatticePerThread[n]->FillBuffer( NumericTraits<PointDataType>::Zero );
}
}
}
Expand Down Expand Up @@ -852,7 +852,7 @@ BSplineScatteredDataPointSetToImageFilter<TInputPointSet, TOutputImage>
this->m_PhiLattice = PointDataImageType::New();
this->m_PhiLattice->SetRegions( size );
this->m_PhiLattice->Allocate();
this->m_PhiLattice->FillBuffer( 0.0 );
this->m_PhiLattice->FillBuffer( NumericTraits<PointDataType>::Zero );

ImageRegionIterator<PointDataImageType> ItP(
this->m_PhiLattice, this->m_PhiLattice->GetLargestPossibleRegion() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int itkBSplineControlPointImageFunctionTest( int, char * [] )
phiLattice->SetSpacing( spacing );
phiLattice->SetRegions( size );
phiLattice->Allocate();
phiLattice->FillBuffer( 0.0 );
phiLattice->FillBuffer( VectorType(0.0) );

// To create the specified function, the first and last control points have
// a value of 1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int itkWarpVectorImageFilterTest(int, char* [] )

for( Iterator inIter( input, region ); !inIter.IsAtEnd(); ++inIter )
{
inIter.Set( pattern.Evaluate( inIter.GetIndex(), size, size, padValue ) );
inIter.Set( PixelType(pattern.Evaluate( inIter.GetIndex(), size, size, padValue )) );
}

//=============================================================
Expand Down Expand Up @@ -175,7 +175,7 @@ int itkWarpVectorImageFilterTest(int, char* [] )

warper->SetInput( input );
warper->SetDisplacementField( field );
warper->SetEdgePaddingValue( padValue );
warper->SetEdgePaddingValue( PixelType(padValue) );

ShowProgressObject progressWatch(warper);
itk::SimpleMemberCommand<ShowProgressObject>::Pointer command;
Expand Down Expand Up @@ -266,7 +266,7 @@ int itkWarpVectorImageFilterTest(int, char* [] )
if( validRegion.IsInside( index ) )
{

PixelType trueValue = pattern.Evaluate( outIter.GetIndex(), validSize, clampSize, padValue );
PixelType trueValue(pattern.Evaluate( outIter.GetIndex(), validSize, clampSize, padValue));
for( unsigned int k=0; k<ImageDimension; k++ )
{
if( vnl_math_abs( trueValue[k] - value[k] ) > 1e-4 )
Expand All @@ -282,7 +282,7 @@ int itkWarpVectorImageFilterTest(int, char* [] )
else
{

if( value != padValue )
if( value != PixelType(padValue) )
{
testPassed = false;
std::cout << "Error at Index: " << index << " ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ WindowConvergenceMonitoringFunction<TScalar>
windowPoint[0] = static_cast<typename ProfilePointType::CoordRepType>( n );

energyProfileWindow->SetPoint( n, windowPoint );
energyProfileWindow->SetPointData( n, this->m_EnergyValues[n] / this->m_TotalEnergy );
energyProfileWindow->SetPointData( n, ProfilePointDataType(this->m_EnergyValues[n] / this->m_TotalEnergy) );
}

bspliner->SetInput( energyProfileWindow );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ int RunTest(testImageType* fixed, testImageType* moving, FieldType* initField,
// Test ApplyLoad() function
FieldType::PixelType pixelVal;
for (unsigned int d = 0; d < ImageDimension; d++)
pixelVal = 0.0;
pixelVal = FieldType::PixelType( 0.0 );
outField->FillBuffer(pixelVal);
const FieldType::RegionType& region = outField->GetLargestPossibleRegion();

Expand Down

0 comments on commit a890962

Please sign in to comment.