# What bool value should \`imageRegion.IsInside(zeroSizedRegion)\` return?

**URL:** https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734
**Category:** Algorithms
**Created:** [January 16, 2022, 6:21pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734 "2022-01-16T18:21:10Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 16, 2022, 6:21pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/1 "2022-01-16T18:21:10Z")

</div>

There appears a disagreement between the documentation of `ImageRegion::IsInside(const Self &)` and the actual behavior when the region passed as argument has zero as size value.

[ITK: itk::ImageRegion\< VImageDimension \> Class Template Reference](https://itk.org/Doxygen/html/classitk_1_1ImageRegion.html#ad2f880b233497930c9e36d3ab5885b58) says:

```auto
template<unsigned int VImageDimension>
bool itk::ImageRegion< VImageDimension >::IsInside( const Self & region ) const

```

> Test if a region (the argument) is completely inside of this region. If the region that is passed as argument to this method, has a size of value zero, then it will not be considered to be inside of the current region, even its starting index is inside.

However, it appears that `currentRegion.IsInside(zeroSizedRegion))` returns true when the index of such a “zero sized region” (`zeroSizedRegion.m_Index`) is inside the current region, specifically when this index is _not_ at the left or upper border of the current region!

I hope you all agree that either the code or its documentation (or both) should be fixed. So I submitted two proposals, which are mutually exclusive:

Fix the code…

> <https://github.com/InsightSoftwareConsortium/ITK/pull/3110>
>
> As documented by commit a872710c63ab223e78f45de8d3e4bc435172867d "STYLE: Documen…ting the behavior of the IsInside() method when the argument region has zero size in any of its dimensions", by Luis Ibanez (@luisibanez), 18 December 2009:
> 
> \> Test if a region (the argument) is completely inside of this region. If the region that is passed as argument to this method, has a size of value zero, then it will not be considered to be inside of the current region, even its starting index is inside.
> 
> Included GoogleTest unit tests for both zero-sized and one-sized regions as argument.
> 
> \---
> This pull request competes with pull request #3108 "DOC: Remove mistake from \`ImageRegion::IsInside(region)\` documentation" I think there is a choice to be made! There is a disagreement between the code and the documentation. So now, should the code follow the documentation (this pull request, #3110), or should the documentation follow the code (pull request #3108)?

… or fix the documentation (_also_ proposing a code improvement):

> <https://github.com/InsightSoftwareConsortium/ITK/pull/3108>
>
> Reverted commit a872710c63ab223e78f45de8d3e4bc435172867d
> "STYLE: Documenting th…e behavior of the IsInside() method when the argument region has zero size in any of its dimensions" by Luis Ibanez (@luisibanez), 18 December 2009
> 
> Unlike the original documentation, \`ImageRegion::IsInside(region)\` may in some cases return true, even when the argument region has zero size!
> 
> Examples:
> 
> using RegionType = ImageRegion\<2\>;
> using IndexType = RegionType::IndexType;
> using SizeType = RegionType::SizeType;
> 
> const RegionType currentRegion1(IndexType::Filled(-1), SizeType::Filled(2));
> const RegionType zeroSizedRegion1{};
> 
> EXPECT\_TRUE(currentRegion1.IsInside(zeroSizedRegion1)); // OK: returns true!
> 
> const RegionType currentRegion2(SizeType::Filled(2));
> const RegionType zeroSizedRegion2(IndexType::Filled(1), SizeType{ { 0 } });
> 
> EXPECT\_TRUE(currentRegion2.IsInside(zeroSizedRegion2)); // OK: returns true!
> 
> Simplified the implementation of \`ImageRegion::IsInside(const Self &)\`, assuming that it may return true for a zero-sized region as argument, when the index (start corner position) of this zero-sized region is inside the current region.

What do you think? Does it matter at all, I mean would you possibly pass a zero-sized region as argument to `ImageRegion::IsInside`. If so, what Boolean return value would you expect?

Note that both (mutually exclusive) pull requests propose a code adjustment that is likely to improve the performance of `ImageRegion::IsInside`😃

---

<div class="post-metadata">

### Author: ![Lee\_Newberg](https://discourse.itk.org/user_avatar/discourse.itk.org/lee_newberg/32/1461_2.png) [@Lee\_Newberg](https://discourse.itk.org/u/Lee_Newberg)
#### Post date: [January 17, 2022, 10:03pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/2 "2022-01-17T22:03:35Z")

</div>

I haven’t ever used `ThisRegion.IsInside(QueryRegion)` so I don’t know the typical use case. I _imagine_ that it is used to check whether iterating over the voxels of `QueryRegion` will give voxels that are guaranteed to be inside `ThisRegion`; the processing of each voxel can assume inside-ness without having to check each one. If this is the use case then returning `true` for a `QueryRegion` of zero voxels seems right; no checks for inside-ness will be required.

---

<div class="post-metadata">

### Author: ![hjmjohnson](https://discourse.itk.org/user_avatar/discourse.itk.org/hjmjohnson/32/23_2.png) [@hjmjohnson](https://discourse.itk.org/u/hjmjohnson)
#### Post date: [January 18, 2022, 8:19pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/3 "2022-01-18T20:19:10Z")

</div>

I agree with Lee’s assessment. Thinking of IsInside as a subset:

From set theory: The empty set is **a subset of any other set** , but not necessarily an element of it.

But I am OK with either fix.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 18, 2022, 10:04pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/4 "2022-01-18T22:04:57Z")

</div>

Well, I do understand the comparison to a _set_. But a zero-sized region isn’t just like a set to me. It still has an index (the start corner position). So I do think it’s reasonable to expect that a zero-sized region is considered _inside_ an outer region, when its index is inside the outer region.

But I am also OK with either fix. 🙂

---

<div class="post-metadata">

### Author: ![hjmjohnson](https://discourse.itk.org/user_avatar/discourse.itk.org/hjmjohnson/32/23_2.png) [@hjmjohnson](https://discourse.itk.org/u/hjmjohnson)
#### Post date: [January 18, 2022, 10:11pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/5 "2022-01-18T22:11:29Z")

</div>

OK. I guess that is that a zero-sized region with an index represents a point. I think that interpretation leads to.

“a zero sized region, with index inside ThisRegion” returns true.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 26, 2022, 7:20pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/6 "2022-01-26T19:20:01Z")

</div>

For the record, the pull request that ensures that `region.IsInside(zeroSizedRegion)` returns false for any zero-sized region as argument is now preferred over the other pull request, at least by @Lee_Newberg and @dzenanz. And it’s also fine by me 😃 This pull request implements the behavior, specified by Luis Ibanez [back in 2009](https://github.com/InsightSoftwareConsortium/ITK/commit/a872710c63ab223e78f45de8d3e4bc435172867d), and still conforms with [the current documentation](https://itk.org/Doxygen/html/classitk_1_1ImageRegion.html#ad2f880b233497930c9e36d3ab5885b58)

> <https://github.com/InsightSoftwareConsortium/ITK/pull/3110>
>
> As documented by commit a872710c63ab223e78f45de8d3e4bc435172867d "STYLE: Documen…ting the behavior of the IsInside() method when the argument region has zero size in any of its dimensions", by Luis Ibanez (@luisibanez), 18 December 2009:
> 
> \> Test if a region (the argument) is completely inside of this region. If the region that is passed as argument to this method, has a size of value zero, then it will not be considered to be inside of the current region, even its starting index is inside.
> 
> Included GoogleTest unit tests for both zero-sized and one-sized regions as argument.
> 
> \---
> This pull request competes with pull request #3108 "DOC: Remove mistake from \`ImageRegion::IsInside(region)\` documentation" I think there is a choice to be made! There is a disagreement between the code and the documentation. So now, should the code follow the documentation (this pull request, #3110), or should the documentation follow the code (pull request #3108)?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 26, 2022, 8:05pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/7 "2022-01-26T20:05:35Z")

</div>

And that PR has now been merged.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 26, 2022, 8:14pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/8 "2022-01-26T20:14:27Z")

</div>

Thanks @dzenanz

BTW, We could still place the old (inconsistent and unnecessarily slow) implementation within an `#ifndef ITK_LEGACY_REMOVE`, if some user might _really_ want to have it. But maybe it isn’t worth the effort. 🤔 What do you think?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 26, 2022, 8:52pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/9 "2022-01-26T20:52:00Z")

</div>

I doubt anyone relied on it, given its inconsistency and being the opposite of documentation.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 26, 2022, 8:52pm UTC](https://discourse.itk.org/t/what-bool-value-should-imageregion-isinside-zerosizedregion-return/4734/10 "2022-01-26T20:52:44Z")

</div>

Maybe we should update remote modules to use this version? Then we will have a much better sample.
