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

rotation_from_up_and_look: Refactor #179

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions polliwog/transform/_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ def rotation_from_up_and_look(up, look):
result is a rotation matrix that will make up along +y and look along +z
(i.e. facing towards a default opengl camera).

up: The direction you want to become `+y`.
look: The direction you want to become `+z`.
Args:
up (np.ndarray): The direction you want to become `+y`.
look (np.ndarray): The direction you want to become `+z`.

Returns:
np.ndarray: A `3x3` rotation matrix.
"""
vg.shape.check(locals(), "up", (3,))
vg.shape.check(locals(), "look", (3,))

up, look = [np.asarray(vector, dtype=np.float64) for vector in (up, look)]

if np.linalg.norm(up) == 0:
if vg.almost_zero(up):
raise ValueError("Singular up")
if np.linalg.norm(look) == 0:
if vg.almost_zero(look):
raise ValueError("Singular look")
if vg.almost_collinear(up, look):
raise ValueError("`up` and `look` are almost collinear")

y = up / np.linalg.norm(up)
z = look - np.dot(look, y) * y
if np.linalg.norm(z) == 0:
raise ValueError("up and look are collinear")
z = z / np.linalg.norm(z)
x = np.cross(y, z)
return np.array([x, y, z])
up = vg.normalize(up)
recomputed_look = vg.normalize(vg.reject(look, from_v=up))
right = np.cross(up, recomputed_look)
return np.array([right, up, recomputed_look])


def euler(xyz, order="xyz", units="deg"):
Expand Down
8 changes: 4 additions & 4 deletions polliwog/transform/test_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ def test_starting_with_canonical_reference_frame_gives_identity():


def test_raises_value_error_with_zero_length_inputs():
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Singular up"):
rotation_from_up_and_look(up=origin, look=vg.basis.z)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Singular look"):
rotation_from_up_and_look(up=vg.basis.y, look=origin)


def test_raises_value_error_with_collinear_inputs():
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="`up` and `look` are almost collinear"):
rotation_from_up_and_look(up=vg.basis.z, look=vg.basis.z)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="`up` and `look` are almost collinear"):
rotation_from_up_and_look(up=vg.basis.z, look=vg.basis.neg_z)


Expand Down