Skip to content

Replace buffered region iteration with FillBuffer in FastMarching filters#5970

Open
N-Dekker wants to merge 3 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:Replace-BufferedRegion-iteration-FastMarching
Open

Replace buffered region iteration with FillBuffer in FastMarching filters#5970
N-Dekker wants to merge 3 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:Replace-BufferedRegion-iteration-FastMarching

Conversation

@N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented Mar 19, 2026

Removed manual iterations over buffered regions that just set all elements to the same value, in FastMarching filters. Added equivalent (but more efficient) calls to FillBuffer.

@github-actions github-actions bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Filtering Issues affecting the Filtering module labels Mar 19, 2026
}

// set all gradient vectors to zero
if (m_GenerateGradientImage)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This pull request would also merge the two consecutive if (m_GenerateGradientImage) statements:

if (m_GenerateGradientImage)
{
m_GradientImage->CopyInformation(this->GetInput());
m_GradientImage->SetBufferedRegion(output->GetBufferedRegion());
m_GradientImage->Allocate();
}
// set all gradient vectors to zero
if (m_GenerateGradientImage)

Following the principle behind Include What You Use.
Replaced two manual iterations over buffered regions that just set all elements
to the same value with equivalent (but more efficient) FillBuffer calls.

Removed an unnecessary local `outputPixel` variable.
Replaced manual iterations over buffered regions by both
FastMarchingUpwindGradientImageFilter and FastMarchingUpwindGradientImageFilterBase,
that set all elements to a gradient of zero with equivalent (more efficient)
FillBuffer calls.
@N-Dekker N-Dekker force-pushed the Replace-BufferedRegion-iteration-FastMarching branch from 6a491cb to a85ada9 Compare March 20, 2026 18:59
@N-Dekker N-Dekker changed the title Replace region iteration with AllocateInitialized and FillBuffer in FastMarching filters Replace buffered region iteration with FillBuffer in FastMarching filters Mar 20, 2026
@N-Dekker
Copy link
Contributor Author

/azp run ITK.Linux

@N-Dekker N-Dekker marked this pull request as ready for review March 21, 2026 10:41
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 21, 2026

Greptile Summary

This PR simplifies initialization logic in three FastMarching filter implementation files by replacing verbose manual ImageRegionIterator loops (that simply fill a buffer with a single value) with equivalent FillBuffer() calls. The change is purely a code-quality and minor performance improvement — FillBuffer can use memset-style bulk operations under the hood, while the iterator loops required per-pixel method calls and iterator overhead.

Key changes:

  • itkFastMarchingImageFilter.hxx: Two manual loops (filling output pixels with m_LargeValue and filling m_LabelImage with LabelEnum::FarPoint) replaced by output->FillBuffer(m_LargeValue) and m_LabelImage->FillBuffer(LabelEnum::FarPoint). Three unnecessary outputPixel temporary variables are also eliminated.
  • itkFastMarchingUpwindGradientImageFilter.hxx: Two separate if (m_GenerateGradientImage) blocks merged into one; the gradient-zeroing loop replaced with m_GradientImage->FillBuffer(GradientPixelType{}).
  • itkFastMarchingUpwindGradientImageFilterBase.hxx: A 13-line while loop zeroing gradient vectors replaced with GradientImage->FillBuffer(GradientPixelType{}), consistent with the itkFastMarchingImageFilterBase.hxx base class which already used this pattern.
  • All three test files correctly add an explicit #include "itkImageRegionIterator.h" since they use ImageRegionIterator directly and were previously relying on its transitive inclusion through the filter headers — good include hygiene.

The FillBuffer pattern for this type of bulk initialization is already established in itkFastMarchingImageFilterBase.hxx (lines 400 and 430), so these changes bring the older filter variants into alignment with the modern base class convention.

Confidence Score: 5/5

  • This PR is safe to merge — all changes are semantically equivalent refactors with no behavioral differences.
  • Every FillBuffer replacement is a direct semantic equivalent of the manual iterator loop it replaces. The GradientPixelType{} value-initialization correctly produces a zero-filled vector (the same pattern is already proven correct in itkFastMarchingImageFilterBase.hxx). The include changes in the three test files are accurate — each test file directly uses ImageRegionIterator and needed the explicit include after the transitive dependency was removed. No logic is altered, no new code paths are introduced, and the existing test suite exercises all affected initialization paths.
  • No files require special attention.

Important Files Changed

Filename Overview
Modules/Filtering/FastMarching/include/itkFastMarchingImageFilter.hxx Replaces two manual ImageRegionIterator loops with FillBuffer calls and removes three unnecessary outputPixel temporaries; semantically equivalent and cleaner.
Modules/Filtering/FastMarching/include/itkFastMarchingUpwindGradientImageFilter.hxx Merges two if (m_GenerateGradientImage) blocks into one and replaces the manual gradient-zeroing loop with FillBuffer(GradientPixelType{}), matching the pattern already established in the base class.
Modules/Filtering/FastMarching/include/itkFastMarchingUpwindGradientImageFilterBase.hxx Removes a manual 13-line while loop that zeroed gradient vectors and replaces it with a single FillBuffer(GradientPixelType{}) call; straightforward and correct.
Modules/Filtering/FastMarching/test/itkFastMarchingTest.cxx Adds an explicit #include "itkImageRegionIterator.h" that was previously satisfied transitively through the filter header; the test uses ImageRegionIterator at line 198, so the include is necessary.
Modules/Filtering/FastMarching/test/itkFastMarchingTest2.cxx Adds an explicit #include "itkImageRegionIterator.h" needed by the ImageRegionIterator usage at line 171; the pre-existing itkImageRegionIteratorWithIndex.h include does not cover ImageRegionIterator.
Modules/Filtering/FastMarching/test/itkFastMarchingUpwindGradientTest.cxx Adds an explicit #include "itkImageRegionIterator.h" for the ImageRegionIterator usage at line 178; correct hygiene fix after removing the transitive include from the filter header.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[FastMarchingImageFilter::Initialize] --> B[output->Allocate]
    B --> C["output->FillBuffer(m_LargeValue)\n(was: ImageRegionIterator loop)"]
    C --> D["m_LabelImage->FillBuffer(LabelEnum::FarPoint)\n(was: ImageRegionIterator loop)"]
    D --> E[Process AlivePoints / OutsidePoints / TrialPoints]

    F[FastMarchingUpwindGradientImageFilter::Initialize] --> G[Superclass::Initialize]
    G --> C
    F --> H{m_GenerateGradientImage?}
    H -- Yes --> I[m_GradientImage->Allocate]
    I --> J["m_GradientImage->FillBuffer(GradientPixelType{})\n(was: ImageRegionIterator loop)"]
    H -- No --> K[Skip]

    L[FastMarchingUpwindGradientImageFilterBase::InitializeOutput] --> M[Superclass::InitializeOutput]
    M --> N["oImage->FillBuffer(m_LargeValue)"]
    L --> O[GradientImage->Allocate]
    O --> P["GradientImage->FillBuffer(GradientPixelType{})\n(was: while loop)"]
Loading

Last reviewed commit: "PERF: Use FillBuffer..."

// set all output value to infinity
using OutputIterator = ImageRegionIterator<LevelSetImageType>;

PixelType outputPixel = m_LargeValue;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

greptile-apps says at #5970 (comment)

Three unnecessary outputPixel temporary variables are also eliminated.

But it's really just one variable! So AI is inaccurate (or maybe I should say incorrect) this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Filtering Issues affecting the Filtering 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.

1 participant