Skip to content

Comments

Add ComputeIndex() member function to ImageConstIterator#5787

Merged
hjmjohnson merged 2 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:ImageConstIterator-ComputeIndex
Feb 13, 2026
Merged

Add ComputeIndex() member function to ImageConstIterator#5787
hjmjohnson merged 2 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:ImageConstIterator-ComputeIndex

Conversation

@N-Dekker
Copy link
Contributor

Provides an alternative to ImageConstIterator::GetIndex(), making it more clear that potentially expensive computation is involved.

@github-actions github-actions bot added the area:Core Issues affecting the Core module label Feb 10, 2026
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

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

I have reservations about the main change.

@github-actions github-actions bot added area:Filtering Issues affecting the Filtering module area:Segmentation Issues affecting the Segmentation module area:Numerics Issues affecting the Numerics module labels Feb 11, 2026
@N-Dekker N-Dekker marked this pull request as ready for review February 11, 2026 17:07
Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

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

You should implement your own performance-enhancing suggestions 😄, preferably as a separate PR (on which this one should then be rebased).

@dzenanz
Copy link
Member

dzenanz commented Feb 11, 2026

Possibly also take the first 2 non-contentious commits to the separate PR, which should then be merged quickly.

Provides an alternative to `ImageConstIterator::GetIndex()`, making it more
clear that potentially expensive computation is involved.
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`. For
those iterators, it may take a relatively expensive computation to get the index.
Using `ComputeIndex()` instead of the equivalent `GetIndex()` aims to raise
awareness of the potential performance cost of those function calls.
@N-Dekker N-Dekker force-pushed the ImageConstIterator-ComputeIndex branch from a4c0b2e to cc5b301 Compare February 12, 2026 10:35
Copy link

@SimonRit SimonRit left a comment

Choose a reason for hiding this comment

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

Approved although I would directly add "future legacy remove" and not wait for a separate PR (as discussed on discourse)

Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

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

Good even as is, but future legacy remove of GetIndex could totally be a third commit here.

@N-Dekker
Copy link
Contributor Author

Good even as is, but future legacy remove of GetIndex could totally be a third commit here.

Thanks @dzenanz. I prefer to do the future legacy remove in a separate PR, which would then also replace the "computative" ImageConstIterator::GetIndex() calls in the unit tests with ComputeIndex(). I intentionally try to keep this PR small, so that it can be reviewed easily. 🤷

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a clearer alternative to ImageConstIterator::GetIndex() by introducing ComputeIndex(), emphasizing that computing the index from the iterator offset may be relatively expensive. It also updates a broad set of call sites across ITK modules to use the new name where appropriate.

Changes:

  • Add ImageConstIterator::ComputeIndex() and make GetIndex() delegate to it.
  • Update many filters/iterators to call ComputeIndex() instead of GetIndex().
  • Adjust iterator implementations that depended on index computation to use the new API.

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
Modules/Core/Common/include/itkImageConstIterator.h Introduces ComputeIndex() and updates GetIndex() to call it, with updated docs.
Modules/Core/Common/include/itkImageScanlineConstIterator.h Uses ComputeIndex() in a cast constructor to make index computation cost explicit.
Modules/Core/Common/include/itkImageReverseConstIterator.h Uses ComputeIndex() when constructing from an ImageConstIterator.
Modules/Segmentation/Watersheds/include/itkWatershedSegmenter.hxx Switches index retrieval to ComputeIndex() in watershed routines.
Modules/Segmentation/Watersheds/include/itkTobogganImageFilter.hxx Switches to ComputeIndex() for current position tracking.
Modules/Segmentation/SuperPixel/include/itkSLICImageFilter.hxx Switches multiple iterator index reads to ComputeIndex().
Modules/Segmentation/LevelSets/include/itkLevelSetNeighborhoodExtractor.hxx Uses ComputeIndex() before distance calculations.
Modules/Segmentation/ConnectedComponents/include/itkHardConnectedComponentImageFilter.hxx Uses ComputeIndex() for neighborhood index manipulation.
Modules/Segmentation/ConnectedComponents/include/itkConnectedComponentImageFilter.hxx Uses ComputeIndex() at run starts in scanline iteration.
Modules/Numerics/FEM/include/itkFEMSolver.hxx Uses ComputeIndex() for transforming index to physical points.
Modules/Numerics/FEM/include/itkFEMScatteredDataPointSetToImageFilter.hxx Uses ComputeIndex() for index-to-physical transforms.
Modules/Numerics/FEM/include/itkFEMRobustSolver.hxx Uses ComputeIndex() for index-to-physical transforms in initialization.
Modules/Filtering/SpatialFunction/include/itkSpatialFunctionImageEvaluatorFilter.hxx Uses ComputeIndex() before evaluating the spatial function.
Modules/Filtering/Smoothing/include/itkBinomialBlurImageFilter.hxx Uses ComputeIndex() when determining output pixel indices.
Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx Uses ComputeIndex() while building label bounding boxes.
Modules/Filtering/MathematicalMorphology/include/itkValuedRegionalExtremaImageFilter.hxx Uses ComputeIndex() when syncing neighborhood iterators to the output iterator.
Modules/Filtering/LabelMap/include/itkBinaryImageToLabelMapFilter.hxx Uses ComputeIndex() at run starts in scanline iteration.
Modules/Filtering/ImageStatistics/include/itkAccumulateImageFilter.hxx Uses ComputeIndex() while accumulating along a dimension.
Modules/Filtering/ImageSources/include/itkGaussianImageSource.hxx Uses ComputeIndex() to evaluate the source function at each pixel.
Modules/Filtering/ImageLabel/include/itkLabelContourImageFilter.hxx Uses ComputeIndex() for line ID computation and scanline integration.
Modules/Filtering/ImageLabel/include/itkBinaryContourImageFilter.hxx Uses ComputeIndex() for line IDs and run start indices.
Modules/Filtering/ImageGrid/include/itkResampleImageFilter.hxx Uses ComputeIndex() while deriving scanline geometry for resampling.
Modules/Filtering/ImageGrid/include/itkMirrorPadImageFilter.hxx Uses ComputeIndex() when converting output indices to input indices.
Modules/Filtering/ImageGrid/include/itkFlipImageFilter.hxx Uses ComputeIndex() to determine output scanline start indices.
Modules/Filtering/ImageGrid/include/itkExpandImageFilter.hxx Uses ComputeIndex() to determine output indices per scanline.
Modules/Filtering/ImageGrid/include/itkBinShrinkImageFilter.hxx Uses ComputeIndex() for mapping output scanlines back to input.
Modules/Filtering/ImageGradient/include/itkDifferenceOfGaussiansGradientImageFilter.hxx Uses ComputeIndex() for output pixel index determination.
Modules/Filtering/ImageFeature/include/itkHoughTransform2DLinesImageFilter.hxx Uses ComputeIndex() while interpreting accumulator space indices.
Modules/Filtering/ImageFeature/include/itkCannyEdgeDetectionImageFilter.hxx Uses ComputeIndex() to seed and follow edges.
Modules/Filtering/DisplacementField/include/itkTransformToDisplacementFieldFilter.hxx Uses ComputeIndex() while generating displacement fields (including scanline logic).
Modules/Filtering/DisplacementField/include/itkIterativeInverseDisplacementFieldImageFilter.hxx Uses ComputeIndex() for output index-to-point transforms.
Modules/Filtering/DisplacementField/include/itkInvertDisplacementFieldImageFilter.hxx Uses ComputeIndex() for boundary condition enforcement indexing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@N-Dekker
Copy link
Contributor Author

Sorry, I'm not putting all possible code improvements in one big PR. Adjustment of ComputeIndex() calls in existing filters must be done very carefully, to avoid breaking code. In some cases, the Iterator variable should be replaced with the corresponding IteratorWithIndex. In other cases, the computed index should be cached into a local variable. ComputeIndex() should also be used in the tests. And ImageConstIterator::GetIndex() should be deprecated. I prefer to do it carefully, with small pull requests that are easy to review.

@hjmjohnson hjmjohnson merged commit 121605a into InsightSoftwareConsortium:main Feb 13, 2026
23 checks passed
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
tests.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
…ilter

Replaced `outputIt.GetIndex()` with `outputIt.ComputeIndex()`.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
tests.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Modules/Registration/Common/test/RegistrationITKv3

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Examples.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Indicated that `ImageConstIterator::GetIndex()` will be removed in the future.

The name "ComputeIndex" appears much clearer, as it indicates that it does a
potentially expensive computation, in order to retrieve the index.

Follow-up to pull request InsightSoftwareConsortium#5787
commit 3e5a75a
"ENH: Add `ComputeIndex()` member function to ImageConstIterator"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced `outputIt.GetIndex()` with `outputIt.ComputeIndex()`, in
`PolylineMaskImageFilter::GenerateData()`.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
tests.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Modules/Registration/Common/test/RegistrationITKv3

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Examples.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Indicated that `ImageConstIterator::GetIndex()` will be removed in the future.

The name "ComputeIndex" appears much clearer, as it indicates that it does a
potentially expensive computation, in order to retrieve the index.

Follow-up to pull request InsightSoftwareConsortium#5787
commit 3e5a75a
"ENH: Add `ComputeIndex()` member function to ImageConstIterator"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 13, 2026
Indicated that `ImageConstIterator::GetIndex()` will be removed in the future.

The name "ComputeIndex" appears much clearer, as it indicates that it does a
potentially expensive computation, in order to retrieve the index.

Follow-up to pull request InsightSoftwareConsortium#5787
commit 3e5a75a
"ENH: Add `ComputeIndex()` member function to ImageConstIterator"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 14, 2026
Replaced `outputIt.GetIndex()` with `outputIt.ComputeIndex()`, in
`PolylineMaskImageFilter::GenerateData()`.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 14, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
tests.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 14, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Modules/Registration/Common/test/RegistrationITKv3

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 14, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Examples.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 14, 2026
Indicated that `ImageConstIterator::GetIndex()` will be removed in the future.

The name "ComputeIndex" appears much clearer, as it indicates that it does a
potentially expensive computation, in order to retrieve the index.

Follow-up to pull request InsightSoftwareConsortium#5787
commit 3e5a75a
"ENH: Add `ComputeIndex()` member function to ImageConstIterator"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 15, 2026
Replaced `outputIt.GetIndex()` with `outputIt.ComputeIndex()`, in
`PolylineMaskImageFilter::GenerateData()`.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 15, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
tests.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 15, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Modules/Registration/Common/test/RegistrationITKv3

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 15, 2026
Replaced function calls of the form `iterator.GetIndex()` with
`iterator.ComputeIndex()`, for iterators derived from `ImageConstIterator`, in
Examples.

Follow-up to pull request InsightSoftwareConsortium#5787
commit cc5b301
"STYLE: Replace GetIndex() calls on iterators with ComputeIndex()"
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Feb 15, 2026
Indicated that `ImageConstIterator::GetIndex()` will be removed in the future.
Adding a note to the migration guide.

The name "ComputeIndex" appears much clearer, as it indicates that it does a
potentially expensive computation, in order to retrieve the index.

Follow-up to pull request InsightSoftwareConsortium#5787
commit 3e5a75a
"ENH: Add `ComputeIndex()` member function to ImageConstIterator"
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:Numerics Issues affecting the Numerics module area:Segmentation Issues affecting the Segmentation module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants