8000 describe format of quaternion coefficents returned by .vec · flyinger/spatialmath-python@1ef54aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ef54aa

Browse files
committed
describe format of quaternion coefficents returned by .vec
Add variant .vec_xyzs to return in three.js friendly order
1 parent 1f85beb commit 1ef54aa

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

spatialmath/quaternion.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ def vec(self):
216216
- 1, return a NumPy array shape=(4,)
217217
- N>1, return a NumPy array shape=(N,4).
218218
219+
The quaternion coefficients are in the order (s, vx, vy, vz), ie. with
220+
the scalar (real part) first.
221+
219222
Example:
220223
221224
.. runblock:: pycon
@@ -229,6 +232,36 @@ def vec(self):
229232
else:
230233
return np.array([q._A for q in self])
231234

235+
@property
236+
def vec_xyzs(self):
237+
"""
238+
Quaternion as a vector
239+
240+
:return: quaternion expressed as a 4-vector
241+
:rtype: numpy ndarray, shape=(4,)
242+
243+
``q.vec`` is the quaternion as a vector. If `len(q)` is:
244+
245+
- 1, return a NumPy array shape=(4,)
246+
- N>1, return a NumPy array shape=(N,4).
247+
248+
The quaternion coefficients are in the order (vx, vy, vz, s), ie. with
249+
the scalar (real part) last. This is useful when exporting to other
250+
packages like three.js or pybullet.
251+
252+
Example:
253+
254+
.. runblock:: pycon
255+
256+
>>> from spatialmath import Quaternion
257+
>>> Quaternion([1,2,3,4]).vec_xyzs
258+
>>> Quaternion([np.r_[1,2,3,4], np.r_[5,6,7,8]]).vec_xyzs
259+
"""
260+
if len(self) == 1:
261+
return self._A
262+
else:
263+
return np.array([q._A for q in self])
264+
232265
@property
233266
def matrix(self):
234267
"""

0 commit comments

Comments
 (0)
0