Zuzu-Typ / PyGLM

Fast OpenGL Mathematics (GLM) for Python
zlib License
214 stars 29 forks source link

Invalid quaternion for valid but mirrored rotation matrix #214

Closed Wasserwecken closed 1 year ago

Wasserwecken commented 1 year ago

I've tried to convert numpy matrices to PyGLM. THey work until i tried to mirror them. The provided example shows that after converting the matrices to quaternion only works for certain matrices. But the mirrored matrix is valid too and should return the mirrored quaternion.

Did I do something wrong, or may is this a problem by glm? (How to mirror on an axis: https://gamedev.stackexchange.com/questions/149062/how-to-mirror-reflect-flip-a-4d-transformation-matrix)

import glm
import numpy

dats = [
    # original rotation
    numpy.array([[ 0.8566732 , -0.05504142,  0.5129149 ],
                 [ 0.06778137,  0.99768126, -0.00614664],
                 [-0.5113872 ,  0.04003172,  0.8584176 ]]),
    # mirrored on X-Axis
    numpy.array([[-0.8566732 , -0.05504142,  0.5129149 ],
                 [-0.06778137,  0.99768126, -0.00614664],
                 [ 0.5113872 ,  0.04003172,  0.8584176 ]]),
]

for dat in dats:
    # check if vetors are unit length
    print(numpy.linalg.norm(dat, axis=-2))

    # convert to glm matrix and check Z-Axis
    mat = glm.mat3x3(*dat.flatten())
    print(mat * (0, 0, 1))

    # convert to quat and check Z-Axis
    qat = glm.quat_cast(mat)
    print(qat * (0, 0, 1), end='\n\n')

Output:

[1.00000008 1.         1.00000013]
vec3(    -0.511387,    0.0400317,     0.858418 )
vec3(    -0.511387,    0.0400317,     0.858418 )

[1.00000008 1.         1.00000013]
vec3(     0.511387,    0.0400317,     0.858418 )
vec3( -0.000910953,    0.0230843,     0.999466 )

The last line should be the same as the second last

Wasserwecken commented 1 year ago

Okay, it was my bad. I cannot mirror a rotation matrix withen given approach from the link. Flipping matrix in the way i did, actually mirrored the rotation BUT also converted into from right handed to left handed.