question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quaternion.setFromUnitVectors lacks precision for close to opposite vectors

See original GitHub issue

Describe 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:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
WestLangleycommented, Mar 18, 2021

If you feel your testing is exhaustive, give it a go. 😃 I was just opting to be a bit more conservative.

2reactions
WestLangleycommented, Mar 17, 2021

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found