Skip to content

Comments

Add itk::MakeIndexRange functions and use them#5812

Merged
thewtex merged 7 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:MakeIndexRange
Feb 19, 2026
Merged

Add itk::MakeIndexRange functions and use them#5812
thewtex merged 7 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:MakeIndexRange

Conversation

@N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented Feb 17, 2026

There are two kinds of IndexRange's:

  1. ZeroBasedIndexRange is optimized for an image region that has a start index of all zeros.
  2. ImageRegionIndexRange supports any arbitrary image region, having any arbitrary start index.

The proposed overload set of MakeIndexRange functions aims to ease creating the most appropriate IndexRange type for each use case:

  • MakeIndexRange(size) returns a ZeroBasedIndexRange
  • MakeIndexRange(imageRegion) returns an ImageRegionIndexRange
  • MakeIndexRange(index, size) returns an ImageRegionIndexRange

This pull request also proposes to start using MakeIndexRange in various tests, filters and other components, to improve code readability.

@github-actions github-actions bot added type:Enhancement Improvement of existing methods or implementation type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Registration Issues affecting the Registration module labels Feb 17, 2026
There are two kinds of IndexRange's: `ZeroBasedIndexRange` is optimized for an
image region that has a start index of all zeros. `ImageRegionIndexRange`
supports any arbitrary image region, having any arbitrary start index. The
overload set of `MakeIndexRange` functions aims to ease creating the most
appropriate IndexRange type:

 - `MakeIndexRange(size)` returns a `ZeroBasedIndexRange`
 - `MakeIndexRange(imageRegion)` returns an `ImageRegionIndexRange`
 - `MakeIndexRange(index, size)` returns an `ImageRegionIndexRange`

Adjusted the IndexRange unit tests and the Doxygen code example to use
`MakeIndexRange`.
@github-actions github-actions bot added the area:Filtering Issues affecting the Filtering module label Feb 18, 2026
@N-Dekker N-Dekker changed the title ENH: Add itk::MakeIndexRange functions, to ease creating an IndexRange Add itk::MakeIndexRange functions and use them Feb 18, 2026
Simplified unit tests by calling the new `itk::MakeIndexRange` function, instead
of constructing an `itk::ZeroBasedIndexRange<Dimension>` or an
`itk::ImageRegionIndexRange<Dimension>`.
Modernized `TimeVaryingBSplineVelocityFieldImageRegistrationMethod` by calling
MakeIndexRange within range-based `for` loops, instead of using local
`ImageRegionConstIteratorWithOnlyIndex` variables.
Simplified its Evaluate member function by calling the new `MakeIndexRange`
function, instead of constructing a `ZeroBasedIndexRange<SpaceDimension>`.
Simplified its Fill member function by calling `MakeIndexRange(index, size)`,
instead of manually constructing an `ImageRegionIndexRange<VDimension>`.
Simplified the code by replacing `ImageRegionIndexRange<InputImageDimension>`
with `MakeIndexRange`.
Simplified its CreateSingleContour member function  by replacing the use of its
RegionIndexRange type alias with a call to `MakeIndexRange`.

Deprecated its RegionIndexRange type alias, marking it "future remove".
@github-actions github-actions bot removed the type:Enhancement Improvement of existing methods or implementation label Feb 18, 2026
@N-Dekker N-Dekker marked this pull request as ready for review February 18, 2026 20:36
Comment on lines -148 to +152
using RegionIndexRange = ImageRegionIndexRange<InputImageType::ImageDimension>;
#ifndef ITK_FUTURE_LEGACY_REMOVE
using RegionIndexRange ITK_FUTURE_DEPRECATED(
"Please use `itk::ImageRegionIndexRange` or `itk::MakeIndexRange` directly!") =
ImageRegionIndexRange<InputImageType::ImageDimension>;
#endif
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that this nested RegionIndexRange type alias was only used internally by the filter itself, so now it is no longer necessary!

Comment on lines +476 to +499
/* Creates a range of indices for the specified grid size. */
template <unsigned int VDimension>
[[nodiscard]] constexpr auto
MakeIndexRange(const Size<VDimension> & gridSize)
{
return ZeroBasedIndexRange<VDimension>(gridSize);
}

/* Creates a range of indices for the specified image region. */
template <unsigned int VDimension>
[[nodiscard]] auto
MakeIndexRange(const ImageRegion<VDimension> & imageRegion)
{
return ImageRegionIndexRange<VDimension>(imageRegion);
}

/* Creates a range of indices for the image region specified by its index and size. */
template <unsigned int VDimension>
[[nodiscard]] auto
MakeIndexRange(const Index<VDimension> & imageRegionIndex, const Size<VDimension> & imageRegionSize)
{
return ImageRegionIndexRange<VDimension>(ImageRegion<VDimension>{ imageRegionIndex, imageRegionSize });
}

Copy link
Contributor Author

@N-Dekker N-Dekker Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of these simple MakeIndexRange functions, I think we might as well add similar functionality by using CTAD (class template argument deduction). But we had discussion about CTAD before. It might sometimes be risky to use CTAD (looking at Arthur O'Dwyer's blog). And it might sometimes cause compiler warnings (warning: 'T' may not intend to support class template argument deduction [-Wctad-maybe-unsupported]). So maybe we should just stick with such simple Make functions 🤷

@N-Dekker
Copy link
Contributor Author

N-Dekker commented Feb 18, 2026

macOS-Build22156645989-MakeIndexRange fails, saying:

Error in CheckBuffer(image5.GetPointer(), std::vector<float>(10, 3.14))
  In /Users/runner/work/ITK/ITK/Modules/Core/Common/test/itkImageAlgorithmCopyTest2.cxx, line 120
Expected true
  but got  0

Does anyone have a clue? 🤷


🎉 Update: The CI is green now! Apparently it was sufficient to re-run "ARMBUILD-x86_64-rosetta". Still wondering why...!

Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@N-Dekker thank you for this beautiful contribution! 🥇

@thewtex thewtex merged commit aff0a03 into InsightSoftwareConsortium:main Feb 19, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:Registration Issues affecting the Registration module type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants