KieranWynn / pyquaternion

A fully featured, pythonic library for representing and using quaternions
http://kieranwynn.github.io/pyquaternion/
MIT License
339 stars 68 forks source link

What to do when matrix is not quite within orthogonality tolerance #66

Open fishbotics opened 3 years ago

fishbotics commented 3 years ago

Hi,

Thanks for writing this library. I use it all the time and love its simplicity for 3D representations. I have lately been dealing with an issue pretty commonly where I am getting a transformation matrix from somewhere and it's very close to being orthogonal, but not quite within the tolerance. These transformations should be valid, but I'm guessing there's some lost precision when using floats vs double. Here is an example:

import numpy as np
from pyquaternion import Quaternion

a = np.array([[-7.0710677e-01, -7.0710677e-01,  0.0000000e+00], 
    [-7.0710677e-01,  7.0710677e-01, -9.7931776e-12], 
    [ 6.9248223e-12, -6.9248223e-12, -1.0000000e+00], 
    [ 0.0000000e+00,  0.0000000e+00,  0.0000000e+00]], 
    dtype=np.float32) 

 print(np.dot(a, a.conj().transpose()))

Gives

 array([[ 9.99999940e-01, -1.26880515e-08, -8.15222941e-20],
       [-1.26880515e-08,  9.99999940e-01,  0.00000000e+00],
       [-8.15222941e-20,  0.00000000e+00,  1.00000000e+00]], dtype=float32)

This looks really close to the identity, but not quite. Any tips on how to project this back onto SO3 so I can produce the corresponding quaternion?

Thanks!

fishbotics commented 3 years ago

I found the solution in one of the old PRs and in docs/index.md (you can set atol and rtol when initializing). This is missing from the online docs. I can submit a PR to add it.

DanielTakeshi commented 1 year ago

@fishbotics I think that would be here? https://github.com/KieranWynn/pyquaternion/pull/44