Skip to content

Commit 1295d23

Browse files
N-Dekkerdzenanz
authored andcommitted
BUG: region.IsInside(zeroSizedRegion) should always return false
As documented by commit a872710 "STYLE: Documenting the behavior of the IsInside() method when the argument region has zero size in any of its dimensions", by Luis Ibanez, 18 December 2009. Included GoogleTest unit tests for both zero-sized and one-sized regions as argument.
1 parent 3a8307b commit 1295d23

3 files changed

Lines changed: 77 additions & 13 deletions

File tree

Modules/Core/Common/include/itkImageRegion.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,23 +273,19 @@ class ITK_TEMPLATE_EXPORT ImageRegion final : public Region
273273
* zero, then it will not be considered to be inside of the current region,
274274
* even its starting index is inside. */
275275
bool
276-
IsInside(const Self & region) const
276+
IsInside(const Self & otherRegion) const
277277
{
278-
IndexType beginCorner = region.GetIndex();
278+
const auto otherIndex = otherRegion.m_Index;
279+
const auto otherSize = otherRegion.m_Size;
279280

280-
if (!this->IsInside(beginCorner))
281-
{
282-
return false;
283-
}
284-
IndexType endCorner;
285-
const SizeType & size = region.GetSize();
286281
for (unsigned int i = 0; i < ImageDimension; ++i)
287282
{
288-
endCorner[i] = beginCorner[i] + static_cast<OffsetValueType>(size[i]) - 1;
289-
}
290-
if (!this->IsInside(endCorner))
291-
{
292-
return false;
283+
if (otherIndex[i] < m_Index[i] || otherSize[i] == 0 ||
284+
otherIndex[i] + static_cast<IndexValueType>(otherSize[i]) >
285+
m_Index[i] + static_cast<IndexValueType>(m_Size[i]))
286+
{
287+
return false;
288+
}
293289
}
294290
return true;
295291
}

Modules/Core/Common/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ set(ITKCommonGTests
616616
itkImageBufferRangeGTest.cxx
617617
itkImageRegionRangeGTest.cxx
618618
itkImageIORegionGTest.cxx
619+
itkImageRegionGTest.cxx
619620
itkIndexGTest.cxx
620621
itkIndexRangeGTest.cxx
621622
itkMatrixGTest.cxx
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*=========================================================================
2+
*
3+
* Copyright NumFOCUS
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0.txt
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*=========================================================================*/
18+
19+
// First include the header file to be tested:
20+
#include "itkImageRegion.h"
21+
#include "itkIndexRange.h"
22+
#include <gtest/gtest.h>
23+
#include <type_traits> // For remove_const_t and remove_reference_t.
24+
25+
26+
// Tests that a zero-sized region is not considered to be inside of another region.
27+
TEST(ImageRegion, ZeroSizedRegionIsNotInside)
28+
{
29+
using RegionType = itk::ImageRegion<2>;
30+
using IndexType = RegionType::IndexType;
31+
using SizeType = RegionType::SizeType;
32+
33+
const RegionType region(SizeType::Filled(2));
34+
35+
for (const auto indexValue : { -1, 0, 1 })
36+
{
37+
const RegionType zeroSizedRegion{ IndexType::Filled(indexValue), SizeType{ { 0 } } };
38+
39+
EXPECT_FALSE(region.IsInside(zeroSizedRegion));
40+
}
41+
}
42+
43+
44+
// Tests that regions of size 1 are considered to be inside of a region, if and only if ("iff") their index is inside of
45+
// this region.
46+
TEST(ImageRegion, OneSizedRegionIsInsideIffItsIndexIsInside)
47+
{
48+
const auto check = [](const auto & region) {
49+
using RegionType = std::remove_const_t<std::remove_reference_t<decltype(region)>>;
50+
using SizeType = typename RegionType::SizeType;
51+
52+
auto paddedRegion = region;
53+
paddedRegion.PadByRadius(1);
54+
55+
for (const auto & index : itk::ImageRegionIndexRange<RegionType::ImageDimension>(paddedRegion))
56+
{
57+
const RegionType oneSizedRegion{ index, SizeType::Filled(1) };
58+
59+
// The one-sized region is inside this region if and only if its index is inside this region.
60+
EXPECT_EQ(region.IsInside(oneSizedRegion), region.IsInside(index));
61+
}
62+
};
63+
64+
// Check for a 2D and a 3D image region.
65+
check(itk::ImageRegion<2>(itk::Size<2>::Filled(3)));
66+
check(itk::ImageRegion<3>(itk::MakeIndex(-1, 0, 1), itk::MakeSize(2, 3, 4)));
67+
}

0 commit comments

Comments
 (0)