Skip to content

Commit 20b0e6d

Browse files
N-Dekkerdzenanz
authored andcommitted
BUG: ImageIORegion::IsInside should return false for a zero-sized region
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
1 parent 9550497 commit 20b0e6d

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

Modules/Core/Common/src/itkImageIORegion.cxx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,28 @@ ImageIORegion::IsInside(const IndexType & index) const
190190
return true;
191191
}
192192

193-
/** Test if a region (the argument) is completely inside of this region */
193+
194+
/** Test if a region (the argument) is completely inside of this region. If
195+
* the region that is passed as argument has a size of value zero, or if the
196+
* dimensionality is zero, then it will not be considered to be inside of the
197+
* current region, even its starting index is inside. */
194198
bool
195-
ImageIORegion::IsInside(const Self & region) const
199+
ImageIORegion::IsInside(const Self & otherRegion) const
196200
{
197-
IndexType beginCorner = region.GetIndex();
198-
199-
if (!this->IsInside(beginCorner))
201+
if (m_ImageDimension == 0 || otherRegion.m_ImageDimension != m_ImageDimension)
200202
{
201203
return false;
202204
}
203-
IndexType endCorner(region.m_ImageDimension);
204-
SizeType size = region.GetSize();
205+
const auto & otherIndex = otherRegion.m_Index;
206+
const auto & otherSize = otherRegion.m_Size;
207+
205208
for (unsigned int i = 0; i < m_ImageDimension; ++i)
206209
{
207-
endCorner[i] = beginCorner[i] + size[i] - 1;
208-
}
209-
if (!this->IsInside(endCorner))
210-
{
211-
return false;
210+
if (otherIndex[i] < m_Index[i] || otherSize[i] == 0 ||
211+
otherIndex[i] + static_cast<IndexValueType>(otherSize[i]) > m_Index[i] + static_cast<IndexValueType>(m_Size[i]))
212+
{
213+
return false;
214+
}
212215
}
213216
return true;
214217
}

Modules/Core/Common/test/itkImageIORegionGTest.cxx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,22 @@ TEST(ImageIORegion, IsAssignable)
197197
Expect_Assignable(GenerateRandomRegion(2), GenerateRandomRegion(2));
198198
Expect_Assignable(GenerateRandomRegion(2), GenerateRandomRegion(3));
199199
}
200+
201+
202+
// Tests that a zero-sized region is not considered to be inside of another region.
203+
TEST(ImageIORegion, ZeroSizedRegionIsNotInside)
204+
{
205+
for (const unsigned int dimension : { 0, 2, 3 })
206+
{
207+
itk::ImageIORegion region(dimension);
208+
209+
region.SetSize(itk::ImageIORegion::SizeType(dimension, 2));
210+
211+
for (const auto indexValue : { -1, 0, 1 })
212+
{
213+
itk::ImageIORegion zeroSizedRegion(dimension);
214+
zeroSizedRegion.SetIndex(itk::ImageIORegion::IndexType(dimension, indexValue));
215+
EXPECT_FALSE(region.IsInside(zeroSizedRegion));
216+
};
217+
}
218+
}

0 commit comments

Comments
 (0)