Skip to content

ENH: GetInverseTransform() returns concrete type for 3D transforms (resolves #6686, supersedes #6685) - #6689

Merged
hjmjohnson merged 3 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:enh-3d-transform-same-type-inverse
Jul 24, 2026
Merged

hjmjohnson merged 3 commits into
InsightSoftwareConsortium:mainfrom
hjmjohnson:enh-3d-transform-same-type-inverse

Conversation

@hjmjohnson

@hjmjohnson hjmjohnson commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

Makes GetInverseTransform() return the concrete transform type for the 3D rigid/versor/similarity family and for FixedCenterOfRotationAffineTransform and ScaleLogarithmicTransform, matching the behavior the 2D family already had. Resolves #6686 and supersedes #6685 (the failing repro by @dzenanz, whose type assertion this PR generalizes, satisfies, and extends).

Root cause and fix

Transform::InvertTransform(const TTransform&) does TTransform::New(), deducing TTransform from the static type at the call site. MatrixOffsetTransformBase::GetInverseTransform() calls InvertTransform(*this) where *this is statically the base, so every derived class that did not override GetInverseTransform() inherited a parent-typed inverse.

The 2D family (Rigid2DTransform, Euler2DTransform, Similarity2DTransform, centered variants) already overrides it with return Superclass::InvertTransform(*this);. This PR adds the same one-line override to the 3D counterparts. The inherited GetInverse() already re-extracts the constrained parameters through the virtual ComputeMatrixParameters(), so no new inverse math is needed.

Classes fixed: Euler3DTransform, QuaternionRigidTransform, Similarity3DTransform, VersorRigid3DTransform, VersorTransform, FixedCenterOfRotationAffineTransform, ScaleLogarithmicTransform.

Unlike the 2D siblings, these classes did not re-export InverseTransformBasePointer, so each override also adds using typename Superclass::InverseTransformBasePointer; (unqualified names from a template-dependent base are not found by two-phase lookup).

Intentionally exempt
  • BSplineTransform — no closed-form global inverse; GetInverseTransform() returns nullptr.
  • AzimuthElevationToCartesianTransform — nonlinear; its inverse is an affine approximation.

The test marks both as expectSameType = false.

Test and local verification

itkTestTransformGetInverse now asserts the inverse preserves the concrete type (parameterized expectSameType, default true).

  • Built ITKTransformTestDriver against main; test exits 0 with the fix, all 28 instantiations pass.
  • Non-vacuity confirmed: reverting just the Euler3DTransform override reproduces ERROR: GetInverseTransform() did not preserve concrete type for Euler3DTransform; got MatrixOffsetTransformBase and exit 1.
  • pre-commit run --all-files clean.
Migration and downstream impact

Added a section to Documentation/docs/migration_guides/itk_6_migration_guide.md.

A survey of ~280 downstream ITK-consumer repositories found no code that breaks: every consumer either uses the inverse through the base pointer or upcasts it to an ancestor type, both of which a more-derived object still satisfies. The one real downstream effect is serialization: an inverse written to disk now records the derived class name (e.g. Euler3DTransform_double_3_3), so byte/string-compared transform baselines in downstream projects may need regeneration. Factory-based readers load either form.

PR Checklist

  • No API changes were made (or the changes have been approved)
  • No major design changes were made (or the changes have been approved)
  • Added test (or behavior not changed)

@github-actions github-actions Bot added type:Enhancement Improvement of existing methods or implementation type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Documentation Issues affecting the Documentation module labels Jul 23, 2026
@hjmjohnson
hjmjohnson force-pushed the enh-3d-transform-same-type-inverse branch 2 times, most recently from 6b42cdf to d0b9c86 Compare July 23, 2026 19:12
@hjmjohnson
hjmjohnson marked this pull request as ready for review July 23, 2026 19:23

@dzenanz dzenanz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good. One possible caveat, absence of which was triggering compile errors when I was doing KernelTransform inverse.

@greptile-apps

This comment was marked as low quality.

@hjmjohnson
hjmjohnson force-pushed the enh-3d-transform-same-type-inverse branch from d0b9c86 to 4d8cd5d Compare July 23, 2026 20:06
@hjmjohnson

Copy link
Copy Markdown
Member Author

Force-push = test coverage only, no source/logic change. Added NonIdentityInverseTests exercising inverse(forward(p)) == p for all 7 classes with non-identity state (rotations, translations, anisotropic scale/shear, reciprocal scales), addressing the coverage gap noted in the Greptile review. Verified locally: builds clean, passes, and fails if any override is reverted.

@hjmjohnson hjmjohnson changed the title ENH: GetInverseTransform() returns concrete type for 3D transforms (resolves #6686) ENH: GetInverseTransform() returns concrete type for 3D transforms (resolves #6686, supersedes #6685) Jul 23, 2026
@hjmjohnson
hjmjohnson force-pushed the enh-3d-transform-same-type-inverse branch 2 times, most recently from 751d4b2 to cc6e608 Compare July 23, 2026 20:32
@hjmjohnson

Copy link
Copy Markdown
Member Author

Two force-pushes just landed: first = plain rebase onto current main (resolves the migration-guide conflict with the merged NiftiImageIO section, keeping both); second = the comment reword only (@dzenanz's note). CI re-running on the new HEAD.

@hjmjohnson
hjmjohnson force-pushed the enh-3d-transform-same-type-inverse branch from cc6e608 to 3c43e8a Compare July 23, 2026 22:47
hjmjohnson and others added 2 commits July 23, 2026 17:47
The 2D transform family (Rigid2D, Euler2D, Similarity2D, and centered
variants) already overrides GetInverseTransform() to return the same
concrete type via Superclass::InvertTransform(*this). Their 3D
counterparts inherited the MatrixOffsetTransformBase implementation,
which deduces the parent type and returns a base-typed inverse.

Add the same one-line override to the 3D rigid/versor/similarity classes
and to FixedCenterOfRotationAffineTransform and ScaleLogarithmicTransform
so the inverse preserves the concrete type. The inherited GetInverse()
already re-extracts constrained parameters through the virtual
ComputeMatrixParameters(), so no new inverse math is required.

Extend itkTestTransformGetInverse to assert the inverse preserves the
concrete type, exempting BSplineTransform (no closed-form inverse, null)
and AzimuthElevationToCartesianTransform (nonlinear, affine inverse).

Addresses InsightSoftwareConsortium#6686.

Co-Authored-By: Dženan Zukić <[email protected]>
…guide

Document the ITK 6 behavior change where GetInverseTransform() returns
the concrete transform type for the 3D rigid/versor/similarity family and
for FixedCenterOfRotationAffineTransform and ScaleLogarithmicTransform.

Addresses InsightSoftwareConsortium#6686.

Co-Authored-By: Dženan Zukić <[email protected]>
@hjmjohnson

Copy link
Copy Markdown
Member Author

Two force-pushes: first = the comment fix (@dzenanz — condensed the block, no more "this PR" phrasing); second = plain rebase onto current main.

@hjmjohnson
hjmjohnson force-pushed the enh-3d-transform-same-type-inverse branch from 3c43e8a to 13ebaff Compare July 23, 2026 22:48

@dzenanz dzenanz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not all are covered here.

@hjmjohnson
hjmjohnson merged commit 0375e07 into InsightSoftwareConsortium:main Jul 24, 2026
15 of 17 checks passed
@hjmjohnson
hjmjohnson deleted the enh-3d-transform-same-type-inverse branch August 22, 2026 15:15
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:Documentation Issues affecting the Documentation module type:Enhancement Improvement of existing methods or implementation 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.

GetInverseTransform() should not return base class type

3 participants