Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix3.asQuaternion() returns wrong result #4

Open
jpasserin opened this issue Dec 16, 2021 · 2 comments
Open

Matrix3.asQuaternion() returns wrong result #4

jpasserin opened this issue Dec 16, 2021 · 2 comments

Comments

@jpasserin
Copy link

from math3d.matrixN import Matrix3
m = Matrix3([1,0,0,0,-1,0,0,0,-1])
print(m.asQuaternion())
# [0.000000e+00  0.000000e+00  0.000000e+00  6.123234e-17]
# This is basically (0,0,0,0) which is not a valid quaternion

I found an alternate algorithm for Matrix3 to Quaternion which gave me the correct result

from math import sqrt

m = [[1,0,0],[0,-1,0],[0,0,-1]]

tr = m[0][0] + m[1][1] + m[2][2]

if tr > 0:
    S = sqrt(tr+1.0) * 2
    qw = 0.25 * S
    qx = (m[2][1] - m[1][2]) / S
    qy = (m[0][2] - m[2][0]) / S
    qz = (m[1][0] - m[0][1]) / S
elif (m[0][0] > m[1][1]) and (m[0][0] > m[2][2]):
    S = sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]) * 2
    qw = (m[2][1] - m[1][2]) / S
    qx = 0.25 * S
    qy = (m[0][1] + m[1][0]) / S
    qz = (m[0][2] + m[2][0]) / S
elif m[1][1] > m[2][2]:
    S = sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]) * 2
    qw = (m[0][2] - m[2][0]) / S
    qx = (m[0][1] + m[1][0]) / S
    qy = 0.25 * S
    qz = (m[1][2] + m[2][1]) / S
else: 
    S = sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]) * 2
    qw = (m[1][0] - m[0][1]) / S
    qx = (m[0][2] + m[2][0]) / S
    qy = (m[1][2] + m[2][1]) / S
    qz = 0.25 * S

print (qx, qy, qz, qw)
# (1,0,0,0)
@jpasserin
Copy link
Author

Looks like there is still an issue.
it seems that when passing an Array of Matrix3 the output quaternion array is an array of the last item.

from math3d import Matrix3, Matrix3Array

m = Matrix3Array(
    [Matrix3([1,0,0,0,1,0,0,0,1])]
)

print(m)
# [[[1. 0. 0.]
#  [0. 1. 0.]
#  [0. 0. 1.]]]

print(m.asQuaternionArray())
# Correct Result, when passing only one matrix3
# [[0. 0. 0. 1.]]

But when passing two matrices

from math3d import Matrix3, Matrix3Array

m = Matrix3Array([
    Matrix3([1,0,0,0,1,0,0,0,1]),
    Matrix3([.707,0,.707,0,1,0,-.707,0,.707])]
)

print(m)
# [[[ 1.     0.     0.   ]
#  [ 0.     1.     0.   ]
#  [ 0.     0.     1.   ]]
# [[ 0.707  0.     0.707]
#  [ 0.     1.     0.   ]
#  [-0.707  0.     0.707]]]

print(m.asQuaternionArray())
# This is seems to be the second matrix twice
# [[0.         0.38263761 0.         0.92385064]
# [0.         0.38263761 0.         0.92385064]]

# Should be 
# [[0. 0. 0. 1.]
# [0.         0.38263761 0.         0.92385064]]

@tbttfox
Copy link
Owner

tbttfox commented Jan 5, 2022

OK, should work now.

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

No branches or pull requests

2 participants