From 2e75b538a7bf10942b1cf81e3ffca62460db6434 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 15 Feb 2026 17:11:31 +0100 Subject: [PATCH 1/4] PERF: Use RegionConstIteratorWithIndex in ContourExtractor2DImageFilter Replaced using RegionConstIterator with using ImageRegionConstIteratorWithIndex in `ContourExtractor2DImageFilter::GenerateDataForLabels()`, to avoid potentially expensive index computation with each iteration. --- .../include/itkContourExtractor2DImageFilter.hxx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx index 75c3d9a5be1..74b6b5aa216 100644 --- a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx +++ b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx @@ -24,6 +24,7 @@ #include "itkMultiThreaderBase.h" #include "itkShapedImageNeighborhoodRange.h" #include "itkTotalProgressReporter.h" +#include "itkImageRegionConstIteratorWithIndex.h" namespace itk { @@ -347,15 +348,16 @@ ContourExtractor2DImageFilter::GenerateDataForLabels() { labelBoundingBoxes[label] = BoundingBoxType{ right_bot, left_top }; } - // We use RegionConstIterator here instead of RegionRange because we want access to the GetIndex() method. - RegionConstIterator inputIt{ input, inputRegion }; + // We use ImageRegionConstIteratorWithIndex here instead of RegionRange because we want access to the GetIndex() + // method. + ImageRegionConstIteratorWithIndex inputIt{ input, inputRegion }; for (inputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt) { BoundingBoxType & bbox = labelBoundingBoxes[inputIt.Get()]; - bbox.min[0] = std::min(bbox.min[0], inputIt.ComputeIndex()[0]); - bbox.min[1] = std::min(bbox.min[1], inputIt.ComputeIndex()[1]); - bbox.max[0] = std::max(bbox.max[0], inputIt.ComputeIndex()[0]); - bbox.max[1] = std::max(bbox.max[1], inputIt.ComputeIndex()[1]); + bbox.min[0] = std::min(bbox.min[0], inputIt.GetIndex()[0]); + bbox.min[1] = std::min(bbox.min[1], inputIt.GetIndex()[1]); + bbox.max[0] = std::max(bbox.max[0], inputIt.GetIndex()[0]); + bbox.max[1] = std::max(bbox.max[1], inputIt.GetIndex()[1]); } // Build the extended regions from the bounding boxes for (const InputPixelType label : allLabels) From 4be530ede740e9b818cff1cfb8d6af731b6a6dbb Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 15 Feb 2026 17:19:32 +0100 Subject: [PATCH 2/4] STYLE: Avoid multiple GetIndex() calls in ContourExtractor2DImageFilter Avoided calling `inputIt.GetIndex()` repetitively during one and the same iteration. This is just a style improvement, as it is not a major performance improvement. (`inputIt.GetIndex()` is likely to be very fast.) --- .../Path/include/itkContourExtractor2DImageFilter.hxx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx index 74b6b5aa216..e200203dfb2 100644 --- a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx +++ b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx @@ -353,11 +353,12 @@ ContourExtractor2DImageFilter::GenerateDataForLabels() ImageRegionConstIteratorWithIndex inputIt{ input, inputRegion }; for (inputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt) { + const auto & index = inputIt.GetIndex(); BoundingBoxType & bbox = labelBoundingBoxes[inputIt.Get()]; - bbox.min[0] = std::min(bbox.min[0], inputIt.GetIndex()[0]); - bbox.min[1] = std::min(bbox.min[1], inputIt.GetIndex()[1]); - bbox.max[0] = std::max(bbox.max[0], inputIt.GetIndex()[0]); - bbox.max[1] = std::max(bbox.max[1], inputIt.GetIndex()[1]); + bbox.min[0] = std::min(bbox.min[0], index[0]); + bbox.min[1] = std::min(bbox.min[1], index[1]); + bbox.max[0] = std::max(bbox.max[0], index[0]); + bbox.max[1] = std::max(bbox.max[1], index[1]); } // Build the extended regions from the bounding boxes for (const InputPixelType label : allLabels) From 1dfd76be0de92b12d02b84842845067376345e9b Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 15 Feb 2026 17:37:32 +0100 Subject: [PATCH 3/4] STYLE: Move iterator declaration to `for` ContourExtractor2DImageFilter Reduced the scope of the local `inputIt` variable in `GenerateDataForLabels()`, by moving its declaration into the _init-statement_ of its `for` loop. Following C++ Core Guidelines, Jul 8, 2025 "Declare names in for-statement initializers and conditions to limit scope", https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#res-cond Removed the `inputIt.GoToBegin()` call, following pull request https://github.com/InsightSoftwareConsortium/ITK/pull/5736 commit f10bd8f5942aad7d0ea5751e94e422bc918f3f2a "STYLE: Remove GoToBegin() calls on ImageRegionIteratorWithIndex" --- .../Path/include/itkContourExtractor2DImageFilter.hxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx index e200203dfb2..3fe42ea6df3 100644 --- a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx +++ b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.hxx @@ -350,8 +350,7 @@ ContourExtractor2DImageFilter::GenerateDataForLabels() } // We use ImageRegionConstIteratorWithIndex here instead of RegionRange because we want access to the GetIndex() // method. - ImageRegionConstIteratorWithIndex inputIt{ input, inputRegion }; - for (inputIt.GoToBegin(); !inputIt.IsAtEnd(); ++inputIt) + for (ImageRegionConstIteratorWithIndex inputIt{ input, inputRegion }; !inputIt.IsAtEnd(); ++inputIt) { const auto & index = inputIt.GetIndex(); BoundingBoxType & bbox = labelBoundingBoxes[inputIt.Get()]; From 9ce536eee118bc01b780829908db489afe3e981d Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sun, 15 Feb 2026 17:50:54 +0100 Subject: [PATCH 4/4] STYLE: Deprecate RegionIterator aliases of ContourExtractor2DImageFilter These nested type aliases are no longer used. Originally, they were just used internally, within the implementation of `ContourExtractor2DImageFilter`. --- .../Path/include/itkContourExtractor2DImageFilter.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.h b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.h index 501508a8f11..9123386a2fa 100644 --- a/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.h +++ b/Modules/Filtering/Path/include/itkContourExtractor2DImageFilter.h @@ -148,8 +148,13 @@ class ITK_TEMPLATE_EXPORT ContourExtractor2DImageFilter using RegionIndexRange = ImageRegionIndexRange; using RegionRange = ImageRegionRange; using RegionConstRange = ImageRegionRange; - using RegionIterator = ImageRegionIterator; - using RegionConstIterator = ImageRegionConstIterator; + +#ifndef ITK_FUTURE_LEGACY_REMOVE + using RegionIterator ITK_FUTURE_DEPRECATED("Please use `itk::ImageRegionIterator` directly!") = + ImageRegionIterator; + using RegionConstIterator ITK_FUTURE_DEPRECATED("Please use `itk::ImageRegionConstIterator` directly!") = + ImageRegionConstIterator; +#endif /** Control the orientation of the contours with reference to the image * gradient. (See class documentation.) */