Skip to content

Commit

Permalink
BUG: ImageIORegion::IsInside should return false for a zero-sized region
Browse files Browse the repository at this point in the history
This commit ensures that `ImageIORegion::IsInside` returns false whenever its
argument is an `ImageIORegion` that contains no pixels at all.

Following the adjustment of `itk::ImageRegion::IsInside(const Self &)` from
pull request #3110
commit 1295d23
"BUG: `region.IsInside(zeroSizedRegion)` should always return false",
which was included with ITK v5.3rc04

See also "What bool value should `imageRegion.IsInside(zeroSizedRegion)` return?"
at https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734
  • Loading branch information
N-Dekker authored and dzenanz committed Oct 12, 2022
1 parent 9550497 commit 20b0e6d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
27 changes: 15 additions & 12 deletions Modules/Core/Common/src/itkImageIORegion.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,28 @@ ImageIORegion::IsInside(const IndexType & index) const
return true;
}

/** Test if a region (the argument) is completely inside of this region */

/** Test if a region (the argument) is completely inside of this region. If
* the region that is passed as argument has a size of value zero, or if the
* dimensionality is zero, then it will not be considered to be inside of the
* current region, even its starting index is inside. */
bool
ImageIORegion::IsInside(const Self & region) const
ImageIORegion::IsInside(const Self & otherRegion) const
{
IndexType beginCorner = region.GetIndex();

if (!this->IsInside(beginCorner))
if (m_ImageDimension == 0 || otherRegion.m_ImageDimension != m_ImageDimension)
{
return false;
}
IndexType endCorner(region.m_ImageDimension);
SizeType size = region.GetSize();
const auto & otherIndex = otherRegion.m_Index;
const auto & otherSize = otherRegion.m_Size;

for (unsigned int i = 0; i < m_ImageDimension; ++i)
{
endCorner[i] = beginCorner[i] + size[i] - 1;
}
if (!this->IsInside(endCorner))
{
return false;
if (otherIndex[i] < m_Index[i] || otherSize[i] == 0 ||
otherIndex[i] + static_cast<IndexValueType>(otherSize[i]) > m_Index[i] + static_cast<IndexValueType>(m_Size[i]))
{
return false;
}
}
return true;
}
Expand Down
19 changes: 19 additions & 0 deletions Modules/Core/Common/test/itkImageIORegionGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,22 @@ TEST(ImageIORegion, IsAssignable)
Expect_Assignable(GenerateRandomRegion(2), GenerateRandomRegion(2));
Expect_Assignable(GenerateRandomRegion(2), GenerateRandomRegion(3));
}


// Tests that a zero-sized region is not considered to be inside of another region.
TEST(ImageIORegion, ZeroSizedRegionIsNotInside)
{
for (const unsigned int dimension : { 0, 2, 3 })
{
itk::ImageIORegion region(dimension);

region.SetSize(itk::ImageIORegion::SizeType(dimension, 2));

for (const auto indexValue : { -1, 0, 1 })
{
itk::ImageIORegion zeroSizedRegion(dimension);
zeroSizedRegion.SetIndex(itk::ImageIORegion::IndexType(dimension, indexValue));
EXPECT_FALSE(region.IsInside(zeroSizedRegion));
};
}
}

0 comments on commit 20b0e6d

Please sign in to comment.