Repository navigation
Conversation
There was a problem hiding this comment.
Thank you for contributing a pull request! 🙏
Welcome to the ITK community! 🤗👋☀️
We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜
More support and guidance on the contribution process can be found in our contributing guide. 📖
This is an automatic message. Allow for time for the ITK community to be able to read the pull request and comment
on it.
[High impact] The change reaches ITK's shared image-iteration API. Fix mixed writable/read-only comparisons before merging. Findings
|
8ae2f18 to
4e86f55
Compare
The original inheritance-based design of the iterator class pairs has several problems, notably the pervasive need to const_cast away the pointer-to-const member whenever an iterator needs write access. This opens up opportunities for accidental or deliberate const-correctness violation. This refactor makes itk::ImageIterator a standalone template class that models both read-only and read/write access semantics based solely on the constness of the template type. ImageConstIterator<T> then functionally acts as an alias template for ImageIterator<const T>. It is implemented as a minimal derived class due to CTAD not being available for alias templates pre-C++20.
4e86f55 to
10c4420
Compare
dzenanz
left a comment
There was a problem hiding this comment.
Looks good to me. It would be good if someone else reviewed this before merging.
|
The |
SimonRit
left a comment
There was a problem hiding this comment.
Thanks, looks good to me. Two points for your consideration:
- should you (first) revert 9a6c182 in this PR?
- I would remove all includes of
itkImageConstIterator.hin other files and trigger a compilation error if it is included withITK_FUTURE_LEGACY_REMOVEguards.
Yes, I think eventually all other iterators should follow the same rework. I kept this PR focused to edit: For the record, I applied the same refactor to |
Since only one iterator pair is modified so far, we should keep using the changes of 9a6c182 for all the other iterators.
Good point, I am actually not so familiar with the migration strategy in ITK. I tried to make the PR minimal and as backward compatible as possible e.g. by keeping the now mostly empty itkImageConstIterator.h header. But I understand it might be better to fail loudly instead? Especially considering that the required change for users is straightforward ("include itkImageIterator.h instead"). |
The transition is made with this |
|
|
hjmjohnson
left a comment
There was a problem hiding this comment.
Requesting changes for one reason: the fix for #7003 is not in place yet, and nothing tests for it. A downstream build forest found no regressions from this PR.
ImageIterator<const T>::Set and Value() = x now correctly fail to compile. But the case #7003 reports still compiles on this branch, because ImageRegionIterator<const T> derives from ImageRegionConstIterator → ImageConstIterator, which keeps the const_cast:
auto it = itk::ImageRegionIterator<const ImageType>{ constImage, constImage->GetBufferedRegion() };
it.Set(2); // still compilesPlease either narrow the PR description ("would incidentally fix all of #7003" holds only once the derived iterators are refactored too) or extend the change. In both cases, add a compile-time negative test (inline suggestion on the GTest) so the fix is checked rather than assumed. The other inline comments are improvements and judgement calls, not blockers.
Design context: this is a narrower take on #6141 than the closed #6142. #6142 stalled partly because it had no downstream validation, so I ran that validation here.
Downstream build forest: no regressions
ITK built with every default module, Python wrapping, and Module_RLEImage ON (RLEImage specializes both iterator classes, and PR CI does not build it because it is EXCLUDE_FROM_DEFAULT). The PR was merged locally onto a recent main; consumers were built against it with USE_SYSTEM_ITK on macOS arm64.
| Consumer | Result |
|---|---|
| ITK, including RLEImage | builds |
| ANTs | builds, ctest 179/179 (same as without this PR) |
| BRAINSTools | builds, ctest 458/458 (same as without this PR) |
| SimpleITK, Cleaver, PerformanceBenchmarking, Impact, TractographyTRX, RTK | build |
elastix, c3d, SimpleITKFilters, TubeTK, and some remote modules failed for reasons unrelated to this PR: removed VNL/legacy macros, a configure-time target clash, and missing headers. The one iterator-looking failure, a TubeTK test using ImageRegionIteratorWithIndex without including it, fails identically against main's headers.
API changes measured with compile probes
These compile on main and fail on this branch (clang, -std=c++17 -fsyntax-only):
ImageIterator<T>::Superclasstemplate <class T> void f(const ImageConstIterator<T> &); f(imageIterator);(deduction no longer sees a base)void f(ImageConstIterator<T> &); f(imageIterator);(non-const reference)ImageConstIterator<T> * p = &imageIterator;
Silent change: f(const ImageConstIterator<T> &) given an ImageIterator now binds to a converted temporary, not the original object.
No forest consumer uses any of these shapes, so the measured cost is zero. They are still API breaks without an ITK_LEGACY_REMOVE path, and they belong in the migration guide.
Still compile on this branch: CTAD for both classes with SmartPointer, ConstPointer, and raw pointers; mutable-to-const conversion; mixed comparisons in both operand orders; overloads on the two types.
Commit message
- "ImageConstIterator then functionally acts as an alias template for ImageIterator":
ImageIterator<const T>does not convert toImageConstIterator<T>(inherited constructors exclude copy/move constructors), so the two are not interchangeable. Please qualify the sentence. - Four body lines run 75–77 characters; ITK's limit is 72.
|
Thank you, @mymonin, for this PR. Making the image's constness choose between read-only and read/write access is the design #6141 needed; the earlier attempt in #6142 had missed it. #7065 continues this work. Its first commit is your commit, unchanged and with you as author. The rest of the PR applies the same pattern to every image iterator family, so |
|
Closing as superseded by #7065, which contains this PR's commit unchanged (authored by @mymonin) and applies the same design to every image iterator family. Thank you, @mymonin. How the open review threads here are handled in #7065:
|
ENH: Select image iterator access by image constness (supersedes #7021)
Addresses #6141 for the
itk::ImageIteratoranditk::ImageConstIteratortemplate classes.Compared to the changes described in #6142 (comment) which were left unmerged:
ImageIterator<const T>I am submitting this PR not only to propose a design variant, but also to highlight that fixing #6141 would incidentally fix all of #7003, and also avoid the "hack" (as described by the participants) needed for #6955 regarding the
TImage::GetBufferPointer()overload.The downside is that these changes should break code that specifically relies on
itk::ImageIteratorinheriting fromitk::ImageConstIterator, e.g. usingImageIterator<TImage>::Superclass. The fact that none of the tests nor derived iterators likeitk::ImageRegionIteratorrequired subsequent changes suggests this should not occur very often, if at all. However, I am not familiar with the cited dependent codebases (ANTs, BRAINSTools and Slicer) so I could be wrong.Note: the diff looks large but most of it is moving the content of
itkImageConstIterator.htoitkImageIterator.h.PR Checklist
Refer to the ITK Software Guide for
further development details if necessary.