Quaternion.setFromUnitVectors lacks precision for close to opposite vectors
See original GitHub issueDescribe the bug
A Quaternion calculated using setFromUnitVectors
doesn’t align the two unit vectors precisely if it get’s applied.
To Reproduce
run the following code using node (I’ve tested with 14.7.0) or check the live example on JSFiddle.
Code
const three = require("three");
const zAxis = new three.Vector3(0, 0, 1);
// just 3 example Vectors that fail
const normals = [
new three.Vector3(0.00001630279256671349, 0.00021101192465278993, -0.9999999776040931).normalize(),
new three.Vector3(-0.00011854270500476976, 0.00027142141201982574, -0.9999999561390212).normalize(),
new three.Vector3(0.000042675541323141565, -0.00040869364504498944, -0.9999999155741478).normalize(),
];
normals.forEach(normal => {
const alignNormalsRotation = new three.Quaternion().setFromUnitVectors(normal, zAxis);
console.log(normal.clone().applyQuaternion(alignNormalsRotation).angleTo(zAxis));
});
Live example
Expected behavior I would expect the calculations to have higher precision. I’ve only encounterd this amount of error with Vectors that are close to being opposite.
Result of the code:
0.00021164076687348634
0.000296178929600093
0.00041091569275236077
Expected result:
0
0
0
Platform: Tested locally with:
- Device: Desktop
- OS: Windows 10
- NodeJS: v14.7.0
- Three.js version: three@0.110.0
and in the browser with:
- Device: Desktop
- OS: Windows 10
- Browser: Chromium, Firefox, Edge (via jsfiddle see link above)
- Three.js version: “https://threejs.org/build/three.module.js” (17.03.2021)
Issue Analytics
- State:
- Created 3 years ago
- Comments:11
Top Results From Across the Web
Quaternion#setFromUnitVectors – three.js docs
Quaternions are used in three.js to represent rotations. ... The conjugate of a quaternion represents the same rotation in the opposite direction about...
Read more >Throw error when vectors aren't normailzed in Quaternion ...
I was wondering if it would make sense to check that passed unit vectors really are normalized in Quaternion 's function setFromUnitVectors ......
Read more >Finding quaternion representing the rotation from one vector ...
Yes, this works. See source code. L61 handles if the vectors face opposite directions (return PI, otherwise it'd return identity per @jpa's remark)....
Read more >pyquaternion
A fully featured python module for quaternion representation, manipulation, 3D rotation and animation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you feel your testing is exhaustive, give it a go. 😃 I was just opting to be a bit more conservative.
@Mugen87 Thanks for working on it!
The links you are looking for are hidden in here: https://github.com/mrdoob/three.js/pull/4580/files.
You can also check out a glm implementation: https://github.com/g-truc/glm/blob/ace16e47780dcef815294715237f51e9129b6eb3/glm/gtx/quaternion.inl#L121-L158