Skip to content

Fix overlapping branches in batch quaternion conversion - #400

Merged
AlexanderFabisch merged 1 commit into
dfki-ric:mainfrom
vera-rubin:fix/quaternion-branch-overlap
Nov 20, 2026
Merged

AlexanderFabisch merged 1 commit into
dfki-ric:mainfrom
vera-rubin:fix/quaternion-branch-overlap

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

quaternions_from_matrices could return a non-unit quaternion near an x-axis half-turn because the y branch overwrote the dominant x result.

We exclude matrices already handled by the x branch from the y branch.

We've verified the regression and the full test suite.

Fixes #399

— ChipLab

@AlexanderFabisch

Copy link
Copy Markdown
Member

Hi, could you explain in mathematical terms what this is fixing?

@vera-rubin

Copy link
Copy Markdown
Contributor Author

This is Shepperd's method. For a unit quaternion $q=(w,x,y,z)$ the diagonal of $R$ gives

$$4w^2 = 1+\mathrm{tr}\,R,\quad 4x^2 = 1+R_{00}-R_{11}-R_{22},\quad 4y^2 = 1-R_{00}+R_{11}-R_{22},\quad 4z^2 = 1-R_{00}-R_{11}+R_{22}$$

One component comes from its square root, and the others are off-diagonal sums divided by it (e.g. $4xy = R_{10}+R_{01}$). Each matrix should take exactly one branch, chosen so that divisor is large. ind3 didn't exclude ind2, so when $\mathrm{tr},R \le 0$ and $R_{00} > R_{11} > R_{22}$ the y branch overwrote the x result. That divides by $4|y|$, taken from $1-R_{00}+R_{11}-R_{22}$, which cancels badly when the axis is close to x. For a half-turn about normalize(1, 2e-8, 0), $|q|$ comes out about 2% off.

With ind2 excluded, the x branch is used. There $x^2-y^2=(R_{00}-R_{11})/2\ge 0$, likewise for $z$, and $w^2\le 1/4$, so $x^2\ge 1/4$ and the divisor $4x$ is at least 2. This matches the if/elif in quaternion_from_matrix. In that region the output may now be $-q$ rather than $q$ ($x>0$ instead of $y\ge 0$), which is the same rotation.

@AlexanderFabisch
AlexanderFabisch merged commit ad1a286 into dfki-ric:main Nov 20, 2026
@AlexanderFabisch

Copy link
Copy Markdown
Member

Thanks, merged to develop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

quaternions_from_matrices returns a non-unit quaternion near an x-axis half-turn

2 participants