Skip to content

Commit 9e67783

Browse files
committed
option for quaternion element order, issue #42
1 parent 7caf0a9 commit 9e67783

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

spatialmath/base/quaternions.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ def conj(q):
470470
return np.r_[q[0], -q[1:4]]
471471

472472

473-
def q2r(q):
473+
def q2r(q, order="sxyz"):
474474
"""
475475
Convert unit-quaternion to SO(3) rotation matrix
476476
@@ -493,10 +493,13 @@ def q2r(q):
493493
494494
"""
495495
q = base.getvector(q, 4)
496-
s = q[0]
497-
x = q[1]
498-
y = q[2]
499-
z = q[3]
496+
if order == "sxyz":
497+
s, x, y, z = q
498+
elif order == "xyzs":
499+
x, y, z, s = q
500+
else:
501+
raise ValueError("order is invalid, must be 'sxyz' or 'xyzs'")
502+
500503
return np.array(
501504
[
502505
[1 - 2 * (y ** 2 + z ** 2), 2 * (x * y - s * z), 2 * (x * z + s * y)],

0 commit comments

Comments
 (0)