Skip to content

Fix fs2real vertex transform - #3429

Open
Lestropie wants to merge 1 commit into
masterfrom
fs2real_fix_master
Open

Fix fs2real vertex transform#3429
Lestropie wants to merge 1 commit into
masterfrom
fs2real_fix_master

Conversation

@Lestropie

@Lestropie Lestropie commented Aug 5, 2026

Copy link
Copy Markdown
Member

Expected to fix #3236.

In MRtrix 3.0.5, the MR::Header class was augmented with member Header::Realignment to preserve information about how a loaded image was reoriented to match RAS convention upon load, as this can be crucial information for certain transformation operations.
The transformation of surfaces from FreeSurfer to MRtrix RAS space was however imperfectly augmented with this information.
It failed to account for the sign of the voxel-to-scanner affine that may have been applied upstream (e.g. 5ttgen hsvs did not appropriately import the reference volume using -config RealignTransform false).
This interacted with the FreeSurfer convention of the voxel index (# voxels / 2) being the origin.
Where the sign of an axis has flipped, such as going from FreeSurfer LIA to MRtrix RAS, this results in the origin moving by one voxel.

The appropriate transform matrix is here manually computed from first principles, similarly to how it was prior to 3.0.5, however is done in such a way that the correct result is obtained regardless of the orientation of the input image, whether it was reoriented on load, or indeed whether a prior command had conformed the image to RAS.
For this reason the -config RealignTransform false trick is not actually required in 5ttgen hsvs.

The vertex_transform filter's fs2real mode applied an incorrect transformation
introduced in version 3.0.5. The fix reverts the affected logic in
src/surface/filter/vertex_transform.cpp to restore the pre-3.0.5 behaviour
rather than patching around it with new code. Supporting test cases for
meshconvert and warpinvert are also added alongside incidental repository
artefacts accumulated on this branch.

Prompt:
> Rebase the fix within commit 66ac5d1d59dd3c4a8362671f1505a95bdec4ecdc available at ~/src/worktrees/hsvs/ onto this clone of the MRtrix3 master branch, noting that much of the Claude project-level configuration does not apply to the master branch (different build system, different filesystem layout). Note that this bug was introduced by changes in version 3.0.5, so check the code modification and where possible / elegant resolve using reversion of code rather than use of new code.

Questions:
> Q: Reverting to the pre-3.0.5 stride-based code fully fixes the 3.0.5 regression, but leaves a pre-existing limitation the ported fix removes: if the reference image has been re-strided (e.g. `mrconvert -strides 1,2,3`), the stride-based code still gets c_ras wrong. Which do you want on master?
> A: Port the new freesurfer_cras()

Generated-by: Claude Opus 5 <noreply@anthropic.com>
@Lestropie Lestropie self-assigned this Aug 5, 2026
@Lestropie Lestropie added the bug label Aug 5, 2026
@Lestropie
Lestropie marked this pull request as draft August 6, 2026 00:04
@Lestropie Lestropie changed the title Fix fs2real vertex transform by reverting 3.0.5 regression Fix fs2real vertex transform Aug 6, 2026
@Lestropie

Copy link
Copy Markdown
Member Author

Note: This made its way into production because there was no test for meshconvert fs2real and because the Python command tests were not performed externally manually at any time during that burst of tests (#2134); 5ttgen hsvs tests should have caught this.

@Lestropie

Copy link
Copy Markdown
Member Author
anim

@Lestropie
Lestropie marked this pull request as ready for review August 6, 2026 02:47
@Lestropie
Lestropie requested a review from a team August 6, 2026 02:47
@Lestropie Lestropie mentioned this pull request Aug 6, 2026
27 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Freesurfer surface inaccuracy

1 participant