Skip to content

[SolidMechanics.Spring] RestShapeSpringsForceField on Rigid: Fix torque of small rotations - #6463

Open
fredroy wants to merge 2 commits into
sofa-framework:masterfrom
fredroy:fix_restssff_small_rot
Open

fredroy wants to merge 2 commits into
sofa-framework:masterfrom
fredroy:fix_restssff_small_rot

Conversation

@fredroy

@fredroy fredroy commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Rotation and quaternion are not my cup of tea so I let 🤖 explaining:

The bug

The rotation away from rest is stored as a quaternion dq = (x, y, z, w). For a rotation of angle θ, w = cos(θ/2) and (x, y, z) = axis × sin(θ/2).
The old code worked out the angle only if (w < 1.0).
For tiny angles (below about 3e-8 rad), cos(θ/2) is so close to 1 that it rounds to exactly 1.0 in double precision. The test failed, so the angle stayed 0 and the torque was 0.
The stiffness matrix still said −angularStiffness. The force and its derivative disagreed.
The fix
The rotation is now computed from the imaginary part (x, y, z), which stays precise for small angles:

s = |(x, y, z)|            // = sin(θ/2)
θ = 2 · atan2(s, w)        // correct for every angle
rotation = (x, y, z) · θ / s   // axis × angle

Bonus: As a side effect, the old code no longer reads an uninitialized axis when it skipped quatToAxis

Included: test

Bad thing is that it will certainly change some tests using RestShapeSpringsForceField

[with-all-tests]


By submitting this pull request, I acknowledge that
I have read, understand, and agree SOFA Developer Certificate of Origin (DCO).


Reviewers will merge this pull-request only if

  • it builds with SUCCESS for all platforms on the CI.
  • it does not generate new warnings.
  • it does not generate new unit test failures.
  • it does not generate new scene test failures.
  • it does not break API compatibility.
  • it is more than 1 week old (or has fast-merge label).

@fredroy fredroy added pr: fix Fix a bug pr: status to review To notify reviewers to review this pull-request pr: test PR adding test(s) in SOFA pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI labels Nov 20, 2026
@fredroy
fredroy force-pushed the fix_restssff_small_rot branch from 49a6ff5 to abe355e Compare November 20, 2026 08:22
…e and stiffness for small rotations

The torque must be -angularStiffness * rotation vector also for rotations
below ~3e-8 rad, whose quaternion has a real part rounding to 1. This test
currently fails: the torque is zero in that range.
… small rotations

The angle of the rotation from the rest orientation was computed only when
the real part of the quaternion was below 1. For rotations below ~3e-8 rad,
it rounds to 1, so the torque was zero in this range while the stiffness
is -angularStiffness. With a large angularStiffness (e.g. 1e8 to hold the
base of an instrument), the equilibrium angle falls in this range: Newton
iterations stalled at a residual equal to the torque applied to the spring.

The rotation vector is now computed from the imaginary part of the
quaternion, with atan2, accurate for all angles. This also removes the
use of an uninitialized axis.
@fredroy
fredroy force-pushed the fix_restssff_small_rot branch from abe355e to 9b51cf2 Compare November 22, 2026 23:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr: AI-aided Label notifying the reviewers that part or all of the PR has been generated with the help of an AI pr: fix Fix a bug pr: status to review To notify reviewers to review this pull-request pr: test PR adding test(s) in SOFA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant