Repository navigation
ENH: Select image iterator access by image constness (supersedes #7021) - #7065
hjmjohnson merged 43 commits into
Conversation
# Conflicts: # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.h # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.hxx
This comment was marked as resolved.
This comment was marked as resolved.
614d0e2 to
6dca3f7
Compare
# Conflicts: # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.h # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.hxx
|
@greptileai please review the latest commits (6dca3f7). |
6dca3f7 to
1e416e2
Compare
# Conflicts: # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.h # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.hxx
|
@greptileai please review the latest commits (1e416e2). |
1e416e2 to
3f3d071
Compare
|
@greptileai please review the latest commits (3f3d071). |
# Conflicts: # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.h # Modules/Core/Common/include/itkImageRandomConstIteratorWithIndex.hxx
|
Ran this PR at What was builtITK This PR and #7041 overlap in Results
* With three VNL includes added, without which elastix does not compile against ITK How each failure was tracedCompared against a build of
Outside the code this PR changes:
|
SimonRit
left a comment
There was a problem hiding this comment.
I did not do a thorough review, too many changes... But LGTM.
Thanks Simon. I just hope that others will do a thorough review, because the iterator framework is really a fundamental part of ITK. However, I also find |
Me too! It's not only the number of changes, large parts of the code have been moved without changes which makes the reviewing more difficult. The good news is that it's thoroughly tested as it is a fundamental part of ITK :-). |
Apply the ImageIterator design to the root of the WithIndex family: ImageIteratorWithIndex<T> holds the position as a pointer to const pixels only when T is const, so Set() and Value() need no const_cast and are absent for const images. ImageConstIteratorWithIndex<T> is ImageIteratorWithIndex<const T>.
Each XxxIterator<T> here derived from XxxConstIterator<T> and added Set() and Value() by casting away const. The next commits make the read-only class the implementation, with access selected by the constness of T, so the subclasses and their out-of-line definitions go away whole; only the headers included the .hxx files. Removing the files first lets the following rename show as a pure rename. This commit does not build alone.
git mv of each itkXxxConst* file onto the itkXxx* name; the content is unchanged. The read-only code becomes the implementation of both names, so its history follows it. This commit does not build alone.
Mechanical: in each renamed file, with C its read-only class and W the
writable one,
perl -pi -e 'next if /^\s*\*\s*\\(sa|sphinxexample)\b/;
s/\bitkC(_h|_hxx|\.h|\.hxx)\b/itkW$1/g; s/\bC\b/W/g' FILE
Other classes named in the file keep their names here.
\sa and \sphinxexample lines keep the read-only names. This commit
does not build alone.
Apply the ImageIteratorWithIndex design to the region, linear, slice, random, random non-repeating, and region exclusion iterators. Each XxxIteratorWithIndex<T> derives from its writable base, inherits the constness-gated Set() and Value(), and XxxConstIteratorWithIndex<T> is XxxIteratorWithIndex<const T>. Deduction guides from the indexed base iterator keep XxxIteratorWithIndex it(base) compiling. The random non-repeating conversion deep-copies the permutation it owns, and the slice and exclusion members that a conversion from the base leaves unset have default initializers.
Each XxxIterator<T> here derived from XxxConstIterator<T> and added Set() and Value() by casting away const. The next commits make the read-only class the implementation, with access selected by the constness of T, so the subclasses and their out-of-line definitions go away whole; only the headers included the .hxx files. Removing the files first lets the following rename show as a pure rename. This commit does not build alone.
git mv of each itkXxxConst* file onto the itkXxx* name; the content is unchanged. The read-only code becomes the implementation of both names, so its history follows it. This commit does not build alone.
Mechanical: in each renamed file, with C its read-only class and W the
writable one,
perl -pi -e 'next if /^\s*\*\s*\\(sa|sphinxexample)\b/;
s/\bitkC(_h|_hxx|\.h|\.hxx)\b/itkW$1/g; s/\bC\b/W/g' FILE
Other classes named in the file keep their names here.
\sa and \sphinxexample lines keep the read-only names. This commit
does not build alone.
The frequency-layout iterators and ReflectiveImageRegionIterator inherited from read-only bases and cast away const to write. They now derive from the writable WithIndex iterators, so writes need no cast and are unavailable for const images; each Const name is the iterator over the const image. Deduction guides from the indexed base iterator keep deduction from a base iterator, and the frequency members have default initializers.
Each XxxIterator<T> here derived from XxxConstIterator<T> and added Set() and Value() by casting away const. The next commits make the read-only class the implementation, with access selected by the constness of T, so the subclasses and their out-of-line definitions go away whole; only the headers included the .hxx files. Removing the files first lets the following rename show as a pure rename. This commit does not build alone.
git mv of each itkXxxConst* file onto the itkXxx* name; the content is unchanged. The read-only code becomes the implementation of both names, so its history follows it. This commit does not build alone.
Mechanical: in each renamed file, with C its read-only class and W the
writable one,
perl -pi -e 'next if /^\s*\*\s*\\(sa|sphinxexample)\b/;
s/\bitkC(_h|_hxx|\.h|\.hxx)\b/itkW$1/g; s/\bC\b/W/g' FILE
Other classes named in the file keep their names here.
\sa and \sphinxexample lines keep the read-only names. This commit
does not build alone.
LineIterator and PathIterator wrote through a const_cast of the const image held by their read-only bases. Each is now the iterator, writable only when TImage is not const, and the Const name iterates over the const image. PathIterator::Value() read a nonexistent member, so it could not compile; it now returns the current pixel.
The writable flood-filled iterators cast away the const image held by ConditionalConstIterator to write a pixel. They now keep the writable image pointer their constructor receives, offer Set() only for non-const images, and read through the const image without a cast.
The hand-written copy constructors and assignment operators copied every member and reset the accessor's begin pointer to the same buffer, which the defaults also do. Two behaviors change: copying a default-constructed ImageIteratorWithIndex no longer dereferences its null image pointer, and PathIterator assignment now copies m_IsAtEnd and m_ZeroOffset, which the hand-written operator skipped. A test checks each, the PathIterator one in a new itkPathIteratorGTest.
These iterators have no superclass that already declares the type aliases, and m_Image is a WeakPointer, not a smart pointer.
Extends the iterator conversion tests to TIterator<const TImage>: it is reached from the writable iterator and converts to and from the read-only name, no writable iterator is built from or derives from a read-only one, and the iterators compare across these names. The indexed and exclusion iterators keep ImageIteratorDimension and their constructors from indexed iterators over const images. For the four frequency iterator pairs, the read-only iterator is built from an indexed iterator only explicitly, and converts to and from the writable iterator over a const image.
Checks that Set() exists for a non-const image and not for a const image, for the image, reverse, indexed, line, path, reflective, frequency, and flood-filled iterators; that an assignable Value() follows the same rule for the image, reverse, indexed, path, reflective, and frequency iterators; and that the read-only reverse iterators no longer provide Set(). The path checks are in itkPathIteratorGTest.
Checks class template argument deduction from raw and smart pointers and from a base iterator for the image, line, path, reflective, and frequency iterators; that the path and reflective read-only names convert to and from the iterator over a const image but never to a writable one; that a default-constructed indexed iterator converts without an image; that the read-only reverse iterator walks a region backward; that a reflective iterator keeps its state when converted to an iterator over a const image; and that a random non-repeating iterator over a const image owns its permutation.
Explains which iterator names now select access by the constness of the image type, what no longer compiles, and how downstream code adapts.
…Y_REMOVE ImageType of an iterator over a const image names the image without const, so XxxIterator<typename It::ImageType> gains a Set() that It lacks. With ITK_FUTURE_LEGACY_REMOVE it keeps the const of the template argument, as std::span<const T>::element_type does, so that expression keeps the access of It; the default keeps existing code compiling. detail::IteratorImageType_t holds the choice for the converted families, and the comparison constraints use std::remove_const_t<TImage> so they do not depend on it. std::remove_const_t<typename It::ImageType> names the image without const in both configurations.
The default constructor called GoToBegin(), which dereferences the null image pointer. It now only marks every dimension as in its first pass, the reflection state that GoToBegin() sets.
…st pass The constructor from ImageIteratorWithIndex left every dimension marked as reflected, so the iterator reached its end after one pixel instead of walking the region forward and back.
4b31306 to
90587a3
Compare
|
Force-push Previous approvals still hold. Code is nearly identical; only redundant "inline" keywords were removed. |
665b79d
into
InsightSoftwareConsortium:main
Part of #6141, and fixes #7003: the image iterators now take their access from the constness of the image type, so
XxxIterator<const ImageType>has noSet()and no writableValue(), and none of them needsconst_cast. Supersedes #7021 (its commits are here, authored by @mymonin) and #7117.The bug fixes and the tests that hold on
mainlanded first in #7068 and #7135; this PR extends those tests to iterators overconstimages. It leaves #6141 open: the downstream forest results (criterion 5) are in the forest comment below, with Windows still in progress, and criterion 3 does not hold for the flood-filled iterators.Commit layout for review. Most commits need no careful reading:
STYLE:commits that do not build alone: delete the writable subclass files,.hxxincluded (pure deletion),git mvtheConstfiles onto the writable names (renamed without changes), and rename the class identifiers (the body gives theperlcommand that reproduces it).BUG: Default the copy operations of the image iterators(it also fixesPathIteratorassignment, which skippedm_IsAtEndandm_ZeroOffset), a trailingSTYLE:commit removing stale comments, theENH: Test …commits, and the guide.ITK_FUTURE_LEGACY_REMOVEstaging ofImageType(below), and twoReflectiveImageRegionIteratorfixes by @dzenanz.The commits that change behavior, each small and compiling on its own:
Set()/Value(),m_Bufferconstness, the derivedImageConstIteratordetail::IsImageIteratorConvertible_voperator++/--intoImageReverseIteratordetail::IteratorImageType_t; the comparison constraints no longer depend onImageTypeGoToBegin()on a null imageDesign
ImageRegionIterator<TImage>derives fromImageIterator<TImage>, and pixel pointers areconstexactly whenTImageisconst, so writes need no cast.Set()and non-constValue()are removed forconst TImagewith SFINAE.ImageRegionConstIterator<T>is a thin class deriving fromImageRegionIterator<const T>, with deduction guides forSmartPointer,ConstPointer, and raw pointers, and from a base iterator (itk::ImageRegionIterator it(baseIterator)still deduces, as onmain), for every converted family including the line, path, reflective, and frequency iterators. Each header provides both names; theConstheaders remain as one-line includes, and the.hxxfiles of removed classes are deleted, since only their headers included them.detail::IsImageIteratorConvertible_vstates the allowed conversions: same image type, never droppingconst. Comparisons require the same image type, with or withoutconst.ImageReverseIterator<T>carries the row spans andoperator++/--(onmainit had them throughImageRegionReverseConstIterator<T>), soImageReverseConstIterator<T>gains them;ImageRegionReverseIterator<T>derives from it and adds only constructors.explicitfor a read-only one, as in ITK 5.LineIterator::Value()returns aconstreference and is available for every image type.ImageTypeof an iterator over aconstimage is the image withoutconst, as before. WithITK_FUTURE_LEGACY_REMOVEit keeps theconst(the convention ofstd::span<const T>::element_type), soXxxIterator<typename It::ImageType>keeps the access ofIt.std::remove_const_t<typename It::ImageType>names the image withoutconstin both configurations; the option is set initkConfigure.h, so ITK and its consumers always agree.SliceIterator, and theRLEImageiterator specializations.XxxConstIterator<T>is notXxxIterator<const T>for them.API changes
Listed in the ITK 6 migration guide (the DOC commit and the
ImageTypestaging commit). Removed, by design:constimage (const-qualified images can be modified viaitk::ImageRegionIterator<const ImageType>#7003), including the publicImageReverseConstIterator::Set().XxxIterator<T>toXxxConstIterator<T>base-class relation:Superclasschanges, and a non-constXxxConstIterator<T> &no longer accepts anXxxIterator<T>.Also: the FFT and shifted FFT layout iterators initialize
m_ActualXDimensionIsOdd, as the half-Hermitian one already did. The reflective copy, random non-repeating copy, andPathIterator::Value()fixes are in #7135. Kept from ITK 5:ImageIteratorDimension, construction of the exclusion and frequency iterators from a plainImageIteratorWithIndex, andexpliciton that constructor for the read-only frequency iterators.Testing
ExclusionIteratorConstructibleFromImageIteratorWithIndexcheck (confirmed by buildingITKCommonGTestDriverat that commit); the next ENH commit builds. Every other commit expected to build compiles the five iterator test sources with no warnings. The refactor commit now carries the same-image comparison constraint, so it passes BUG: Fix iterator copy and PathIterator::Value() bugs, and test iterator access #7135'sHaveConstNonConstComparisonscheck.HasSetchecks now cover every converted iterator, including the frequency, reflective,PathIterator, and flood-filled ones.Module_RLEImageON): 6663/6663 tests pass; the only warnings are the existing ones in the IsotropicWavelets and RANSAC remote modules.ITK_FUTURE_LEGACY_REMOVE, and every pointer, conversion, copy, and deduction form thatmainaccepted on a converted family still compiles (checked by compiling each form againstmainand this branch).0620b7e5fc2against the head).XxxIterator<const T>, including the Path, Reflective, and four frequency pairs. ThePathIteratorchecks are in a newitkPathIteratorGTest; the legacyitkPathIteratorTestis unchanged frommain.pre-commit run --all-filespasses at the tip,git diff --checkpasses on every commit, and every rename commit equals itsperlrule applied to its parent.AI assistance (Claude Code) rebuilt the commit series with scripts kept for review and ran the checks; the author reviewed the changes.