Skip to content

Commit

Permalink
BUG: Do not allow non-positive spacing values
Browse files Browse the repository at this point in the history
Non-positive spacing values are not allowed.  Make
the SpatialObjectToImage filter behave the same
way as itkImageBase when attempting to set a
non-positive spacing value.
  • Loading branch information
hjmjohnson committed Dec 22, 2024
1 parent 253001f commit e1a44fd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 108 deletions.
108 changes: 24 additions & 84 deletions Modules/Core/SpatialObjects/include/itkSpatialObjectToImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,18 @@ template <typename TInputSpatialObject, typename TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SetSpacing(const SpacingType & spacing)
{
unsigned int i;

for (i = 0; i < TOutputImage::ImageDimension; ++i)
{
if (Math::NotExactlyEquals(static_cast<double>(spacing[i]), m_Spacing[i]))
{
break;
}
}
if (i < TOutputImage::ImageDimension)
if (ContainerCopyWithCheck(m_Spacing, spacing, TOutputImage::ImageDimension))
{
for (i = 0; i < TOutputImage::ImageDimension; ++i)
this->Modified();
for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
{
if (spacing[i] != 0)
if (spacing[i] <= 0)
{
m_Spacing[i] = spacing[i];
itkExceptionMacro("Zero-valued spacing and negative spacings are not supported and may result in undefined "
"behavior.\nRefusing to change spacing from "
<< this->m_Spacing << " to " << spacing);
}
}
this->Modified();
}
}

Expand All @@ -111,51 +104,37 @@ template <typename TInputSpatialObject, typename TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SetSpacing(const double * spacing)
{
unsigned int i;

for (i = 0; i < OutputImageDimension; ++i)
{
if (Math::NotExactlyEquals(spacing[i], m_Spacing[i]))
{
break;
}
}
if (i < OutputImageDimension)
if (ContainerCopyWithCheck(m_Spacing, spacing, TOutputImage::ImageDimension))
{
for (i = 0; i < OutputImageDimension; ++i)
this->Modified();
for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
{
if (spacing[i] != 0)
if (spacing[i] <= 0)
{
m_Spacing[i] = spacing[i];
itkExceptionMacro("Zero-valued spacing and negative spacings are not supported and may result in undefined "
"behavior.\nRefusing to change spacing from "
<< this->m_Spacing << " to " << spacing);
}
}
this->Modified();
}
}

template <typename TInputSpatialObject, typename TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SetSpacing(const float * spacing)
{
unsigned int i;

for (i = 0; i < OutputImageDimension; ++i)
{
if (Math::NotExactlyEquals(static_cast<double>(spacing[i]), m_Spacing[i]))
{
break;
}
}
if (i < OutputImageDimension)
if (ContainerCopyWithCheck(m_Spacing, spacing, TOutputImage::ImageDimension))
{
for (i = 0; i < OutputImageDimension; ++i)
this->Modified();
for (unsigned int i = 0; i < TOutputImage::ImageDimension; ++i)
{
if (spacing[i] != 0)
if (spacing[i] <= 0)
{
m_Spacing[i] = spacing[i];
itkExceptionMacro("Zero-valued spacing and negative spacings are not supported and may result in undefined "
"behavior.\nRefusing to change spacing from "
<< this->m_Spacing << " to " << spacing);
}
}
this->Modified();
}
}

Expand All @@ -182,21 +161,8 @@ template <typename TInputSpatialObject, typename TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SetOrigin(const PointType & origin)
{
unsigned int i;

for (i = 0; i < OutputImageDimension; ++i)
{
if (Math::NotExactlyEquals(static_cast<double>(origin[i]), m_Origin[i]))
{
break;
}
}
if (i < OutputImageDimension)
if (ContainerCopyWithCheck(m_Origin, origin, OutputImageDimension))
{
for (i = 0; i < OutputImageDimension; ++i)
{
m_Origin[i] = origin[i];
}
this->Modified();
}
}
Expand All @@ -206,21 +172,8 @@ template <typename TInputSpatialObject, typename TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SetOrigin(const double * origin)
{
unsigned int i;

for (i = 0; i < OutputImageDimension; ++i)
{
if (Math::NotExactlyEquals(origin[i], m_Origin[i]))
{
break;
}
}
if (i < OutputImageDimension)
if (ContainerCopyWithCheck(m_Origin, origin, OutputImageDimension))
{
for (i = 0; i < OutputImageDimension; ++i)
{
m_Origin[i] = origin[i];
}
this->Modified();
}
}
Expand All @@ -229,21 +182,8 @@ template <typename TInputSpatialObject, typename TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject, TOutputImage>::SetOrigin(const float * origin)
{
unsigned int i;

for (i = 0; i < OutputImageDimension; ++i)
{
if (Math::NotExactlyEquals(static_cast<double>(origin[i]), m_Origin[i]))
{
break;
}
}
if (i < OutputImageDimension)
if (ContainerCopyWithCheck(m_Origin, origin, OutputImageDimension))
{
for (i = 0; i < OutputImageDimension; ++i)
{
m_Origin[i] = origin[i];
}
this->Modified();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ itkSpatialObjectToImageFilterTest(int, char *[])
{
if (spacing_result[i] != floatCheckValue)
{
std::cout << "[FAILURE]" << std::endl;
std::cout << "[FAILURE] floatCheckValue" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -106,48 +106,51 @@ itkSpatialObjectToImageFilterTest(int, char *[])
{
if (spacing_result[i] != doubleCheckValue)
{
std::cout << "[FAILURE]" << std::endl;
std::cout << "[FAILURE] doubleCheckValue" << std::endl;
return EXIT_FAILURE;
}
}
}
const auto spacing_vector_result = imageFilter->GetSpacingVector();
for (unsigned int i = 0; i < 2; ++i)
{
if (spacing_vector_result[i] != doubleCheckValue)
{
std::cout << "[FAILURE] spacing_vector_result" << std::endl;
return EXIT_FAILURE;
}
}


{
// NOTE: Passing all zeros does not change the spacing, but the timestamp is modified.
constexpr double allzeros[2]{ 0.0, 0.0 };
imageFilter->Update();
auto preTimestamp = imageFilter->GetTimeStamp();
imageFilter->SetSpacing(allzeros);
auto postTimestamp = imageFilter->GetTimeStamp();
if (preTimestamp != postTimestamp)
bool exceptionThrown = false;
try
{
std::cout << "Time Stamp modified." << std::endl;
imageFilter->SetSpacing(allzeros);
}
else
catch (const itk::ExceptionObject &)
{
std::cout << "Time Stamp not modified with passing all zero values to SetSpacing." << std::endl;
return EXIT_FAILURE;
exceptionThrown = true;
}

const double * spacing_result = imageFilter->GetSpacing();
for (unsigned int i = 0; i < 2; ++i)
if (!exceptionThrown)
{
if (spacing_result[i] != doubleCheckValue)
{
std::cout << "[FAILURE]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[FAILURE] : Attempting to set spacing values to zero should have thrown an exception." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Spacing values not changed when all zeros requested in change" << std::endl;
}


const auto spacing_vector_result = imageFilter->GetSpacingVector();
for (unsigned int i = 0; i < 2; ++i)
{
if (spacing_vector_result[i] != doubleCheckValue)
auto postTimestamp = imageFilter->GetTimeStamp();
if (preTimestamp != postTimestamp)
{
std::cout << "[FAILURE]" << std::endl;
std::cout << "Time Stamp modified." << std::endl;
}
else
{
std::cout << "Time Stamp not modified with passing all zero values to SetSpacing." << std::endl;
return EXIT_FAILURE;
}
}
Expand Down

0 comments on commit e1a44fd

Please sign in to comment.