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

Using tf.transformations quaternion_from_euler with numpy array arguments can silently change the input arguments! #238

Open
peci1 opened this issue Dec 20, 2022 · 1 comment

Comments

@peci1
Copy link

peci1 commented Dec 20, 2022

This code in quaternion_from_euler is actually dangerous when called with 1-D 1-element numpy arrays on input:

def quaternion_from_euler(ai, aj, ak, axes='sxyz'):
"""Return quaternion from Euler angles and axis sequence.
ai, aj, ak : Euler's roll, pitch and yaw angles
axes : One of 24 axis sequences as string or encoded tuple
>>> q = quaternion_from_euler(1, 2, 3, 'ryxz')
>>> numpy.allclose(q, [0.310622, -0.718287, 0.444435, 0.435953])
True
"""
try:
firstaxis, parity, repetition, frame = _AXES2TUPLE[axes.lower()]
except (AttributeError, KeyError):
_ = _TUPLE2AXES[axes]
firstaxis, parity, repetition, frame = axes
i = firstaxis
j = _NEXT_AXIS[i+parity]
k = _NEXT_AXIS[i-parity+1]
if frame:
ai, ak = ak, ai
if parity:
aj = -aj
ai /= 2.0
aj /= 2.0
ak /= 2.0

E.g.

import numpy as np
from tf.transformations import quaternion_from_euler
angles = np.array([[0], [0], [1.0]])
print(angles[2], type(angles[2]))  # prints [ 1. ], <type 'numpy.ndarray'>
q = quaternion_from_euler(0, 0, angles[2])
print(angles[2])  # prints [ 0.5 ], <type 'numpy.ndarray'>

The problem is that 1-element arrays behave like scalars, so the function works, however, different from scalars, they are passed to functions as reference as opposed to by value.

@salihmarangoz
Copy link
Contributor

Created a PR with passing checks to solve this: #241

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