-
Notifications
You must be signed in to change notification settings - Fork 4
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
Fix order argument for quat_to_euler, quat_from_euler & mat_from_eulet #94
Merged
+242
−62
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
64172f5
add failing tests
Korijn 00ed71a
fix quat_to_euler and quat_from_euler
Korijn c3c3a3a
self review
Korijn 7b9eb27
Merge branch 'main' of github.com:pygfx/pylinalg into fix-quat-to-euler
Korijn 7a87f84
bump version because its a breaking change
Korijn 096a222
also align mat_from_euler
Korijn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
from math import cos, sin | ||
|
||
import numpy as np | ||
from numpy.lib.stride_tricks import as_strided | ||
|
||
|
@@ -129,8 +127,9 @@ def mat_from_euler(angles, /, *, order="xyz", out=None, dtype=None): | |
angles : ndarray, [3] | ||
The euler angles. | ||
order : string, optional | ||
The order in which the rotations should be applied. Default | ||
is "xyz". | ||
The rotation order as a string. Can include 'X', 'Y', 'Z' for intrinsic | ||
rotation (uppercase) or 'x', 'y', 'z' for extrinsic rotation (lowercase). | ||
Default is "xyz". | ||
out : ndarray, optional | ||
A location into which the result is stored. If provided, it | ||
must have a shape that the inputs broadcast to. If not provided or | ||
|
@@ -155,31 +154,42 @@ def mat_from_euler(angles, /, *, order="xyz", out=None, dtype=None): | |
ccw around the y-axis, and finally 0° around the x axis. | ||
""" | ||
angles = np.asarray(angles, dtype=float) | ||
order = order.lower() | ||
|
||
if angles.ndim == 0: | ||
# add dimension to allow zip | ||
angles = angles[None] | ||
|
||
matrices = [] | ||
fill_out_first = out is not None | ||
angles = np.atleast_1d(np.asarray(angles)) | ||
extrinsic = order.islower() | ||
order = order.upper() | ||
axis_lookup = {"X": 0, "Y": 1, "Z": 2} | ||
if extrinsic: | ||
angles = reversed(angles) | ||
order = reversed(order) | ||
for angle, axis in zip(angles, order): | ||
axis_idx = {"x": 0, "y": 1, "z": 2}[axis] | ||
|
||
matrix = np.array([[cos(angle), -sin(angle)], [sin(angle), cos(angle)]]) | ||
if axis_idx == 1: | ||
matrix = matrix.T | ||
matrix = np.insert(matrix, axis_idx, 0, axis=0) | ||
matrix = np.insert(matrix, axis_idx, 0, axis=1) | ||
matrix[axis_idx, axis_idx] = 1 | ||
|
||
axis_idx = axis_lookup[axis] | ||
affine_matrix = np.identity(4, dtype=dtype) | ||
affine_matrix[:3, :3] = matrix | ||
|
||
matrices.append(affine_matrix) | ||
|
||
# note: combining in the loop would save time and memory usage | ||
return mat_combine([x for x in reversed(matrices)], out=out, dtype=dtype) | ||
if axis_idx == 0: | ||
affine_matrix[1, 1] = np.cos(angle) | ||
affine_matrix[1, 2] = -np.sin(angle) | ||
affine_matrix[2, 1] = np.sin(angle) | ||
affine_matrix[2, 2] = np.cos(angle) | ||
elif axis_idx == 1: | ||
affine_matrix[0, 0] = np.cos(angle) | ||
affine_matrix[0, 2] = np.sin(angle) | ||
affine_matrix[2, 0] = -np.sin(angle) | ||
affine_matrix[2, 2] = np.cos(angle) | ||
elif axis_idx == 2: | ||
affine_matrix[0, 0] = np.cos(angle) | ||
affine_matrix[0, 1] = -np.sin(angle) | ||
affine_matrix[1, 0] = np.sin(angle) | ||
affine_matrix[1, 1] = np.cos(angle) | ||
|
||
if fill_out_first: | ||
out[:] = affine_matrix | ||
fill_out_first = False | ||
elif out is None: | ||
out = affine_matrix | ||
else: | ||
out @= affine_matrix | ||
return out | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably just echoing into the void here but this was the first time in my life I used this operator. For some reason it feels special. :) |
||
|
||
|
||
def mat_from_axis_angle(axis, angle, /, *, out=None, dtype=None): | ||
|
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, can a user also pass (or think he can pass) a mix of lowercase and uppercase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah they can but it will not give well defined results. I could validate inputs better.