Skip to content

Commit

Permalink
BUG: region.IsInside(zeroSizedRegion) should always return false
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
N-Dekker authored and dzenanz committed Jan 26, 2022
1 parent 3a8307b commit 1295d23
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
22 changes: 9 additions & 13 deletions Modules/Core/Common/include/itkImageRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,23 +273,19 @@ class ITK_TEMPLATE_EXPORT ImageRegion final : public Region
* zero, then it will not be considered to be inside of the current region,
* even its starting index is inside. */
bool
IsInside(const Self & region) const
IsInside(const Self & otherRegion) const
{
IndexType beginCorner = region.GetIndex();
const auto otherIndex = otherRegion.m_Index;
const auto otherSize = otherRegion.m_Size;

if (!this->IsInside(beginCorner))
{
return false;
}
IndexType endCorner;
const SizeType & size = region.GetSize();
for (unsigned int i = 0; i < ImageDimension; ++i)
{
endCorner[i] = beginCorner[i] + static_cast<OffsetValueType>(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
1 change: 1 addition & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ set(ITKCommonGTests
itkImageBufferRangeGTest.cxx
itkImageRegionRangeGTest.cxx
itkImageIORegionGTest.cxx
itkImageRegionGTest.cxx
itkIndexGTest.cxx
itkIndexRangeGTest.cxx
itkMatrixGTest.cxx
Expand Down
67 changes: 67 additions & 0 deletions Modules/Core/Common/test/itkImageRegionGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// First include the header file to be tested:
#include "itkImageRegion.h"
#include "itkIndexRange.h"
#include <gtest/gtest.h>
#include <type_traits> // For remove_const_t and remove_reference_t.


// Tests that a zero-sized region is not considered to be inside of another region.
TEST(ImageRegion, ZeroSizedRegionIsNotInside)
{
using RegionType = itk::ImageRegion<2>;
using IndexType = RegionType::IndexType;
using SizeType = RegionType::SizeType;

const RegionType region(SizeType::Filled(2));

for (const auto indexValue : { -1, 0, 1 })
{
const RegionType zeroSizedRegion{ IndexType::Filled(indexValue), SizeType{ { 0 } } };

EXPECT_FALSE(region.IsInside(zeroSizedRegion));
}
}


// Tests that regions of size 1 are considered to be inside of a region, if and only if ("iff") their index is inside of
// this region.
TEST(ImageRegion, OneSizedRegionIsInsideIffItsIndexIsInside)
{
const auto check = [](const auto & region) {
using RegionType = std::remove_const_t<std::remove_reference_t<decltype(region)>>;
using SizeType = typename RegionType::SizeType;

auto paddedRegion = region;
paddedRegion.PadByRadius(1);

for (const auto & index : itk::ImageRegionIndexRange<RegionType::ImageDimension>(paddedRegion))
{
const RegionType oneSizedRegion{ index, SizeType::Filled(1) };

// The one-sized region is inside this region if and only if its index is inside this region.
EXPECT_EQ(region.IsInside(oneSizedRegion), region.IsInside(index));
}
};

// Check for a 2D and a 3D image region.
check(itk::ImageRegion<2>(itk::Size<2>::Filled(3)));
check(itk::ImageRegion<3>(itk::MakeIndex(-1, 0, 1), itk::MakeSize(2, 3, 4)));
}

0 comments on commit 1295d23

Please sign in to comment.