-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow numpy to compute the angle of a quaternion (#38)
If `q = np.exp(v̂ * θ/2)` for some unit vector `v̂` and an angle `θ` ∈[-2π,2π], then `np.angle` returns `abs(θ)`. This equals `2*abs(log(q))`, but is more efficient.
- Loading branch information
Showing
3 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,6 +259,25 @@ def log(q, qout): | |
qout[3] = f * q[3] | ||
|
||
|
||
@attach_typelist_and_signature([(float64[:], float64[:])], '(n)->()') | ||
def angle(q, qout): | ||
"""Return angle (in radians) through which input quaternion rotates a vector | ||
If `q = np.exp(v̂ * θ/2)` for some unit vector `v̂` and an angle `θ` ∈[-2π,2π], | ||
then this function returns `abs(θ)`. This equals 2*abs(log(q)), but is more | ||
efficient. | ||
""" | ||
b = np.sqrt(q[1]**2 + q[2]**2 + q[3]**2) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
moble
Author
Owner
|
||
if b <= _quaternion_resolution * np.abs(q[0]): | ||
if q[0] < 0.0: | ||
qout[0] = 2*np.pi | ||
else: | ||
qout[0] = 0.0 | ||
else: | ||
qout[0] = 2*np.abs(np.arctan2(b, q[0])) | ||
|
||
|
||
@attach_typelist_and_signature([(float64[:], float64[:])], '(n)->(n)') | ||
def sqrt(q, qout): | ||
"""Return square-root of input quaternion √q. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Wouldn't it be better to calculate this as
np.linalg.norm(q[1:])
instead ofnp.sqrt(q[1]**2 + q[2]**2 + q[3]**2)
?