Skip to content

Commit f109e3b

Browse files
authored
Merge pull request #9 from vortexntnu/refactor/euler-and-quat-conversions
Refactor euler and quaternion conversion functions
2 parents 99c4718 + 8d62084 commit f109e3b

2 files changed

Lines changed: 27 additions & 33 deletions

File tree

tests/test_utils.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_euler_to_quat_z():
5656

5757
def test_euler_to_quat_xyz():
5858
quat = euler_to_quat(1, 1, 1)
59-
assert quat == pytest.approx([0.5709, 0.167, 0.5709, 0.565], abs=0.01)
59+
assert quat == pytest.approx([0.1675, 0.5709, 0.1675, 0.7861], abs=0.01)
6060

6161

6262
def test_quat_to_euler_identity():
@@ -76,12 +76,12 @@ def test_quat_to_euler_z():
7676

7777
def test_quat_to_euler_xyz():
7878
euler = quat_to_euler(0.4207, 0.4207, 0.229, 0.770)
79-
assert euler == pytest.approx([1, 1, 0], abs=0.01)
79+
assert euler == pytest.approx([1.237, 0.4729, 0.9179], abs=0.01)
8080

8181

8282
def test_quat_to_euler_xyz_negative():
8383
euler = quat_to_euler(0.4207, -0.4207, -0.229, 0.770)
84-
assert euler == pytest.approx([1, -1, 0], abs=0.01)
84+
assert euler == pytest.approx([1.237, -0.4729, -0.9179], abs=0.01)
8585

8686

8787
def test_pose_addition():
@@ -122,29 +122,23 @@ def test_pose_rotation_matrix_shape():
122122
def test_pose_rotation_matrix_values():
123123
pose = PoseData(1, 2, 3, 0.1, 0.2, 0.3)
124124
rotation_matrix = pose.as_rotation_matrix()
125-
assert rotation_matrix[0, 0] == pytest.approx(0.935, abs=0.05)
126-
assert rotation_matrix[0, 1] == pytest.approx(-0.283, abs=0.05)
127-
assert rotation_matrix[0, 2] == pytest.approx(0.2101, abs=0.05)
128-
assert rotation_matrix[1, 0] == pytest.approx(0.302, abs=0.05)
129-
assert rotation_matrix[1, 1] == pytest.approx(0.9505, abs=0.05)
130-
assert rotation_matrix[1, 2] == pytest.approx(-0.068, abs=0.05)
131-
assert rotation_matrix[2, 0] == pytest.approx(-0.1805, abs=0.05)
132-
assert rotation_matrix[2, 1] == pytest.approx(0.127, abs=0.05)
133-
assert rotation_matrix[2, 2] == pytest.approx(0.975, abs=0.05)
125+
assert rotation_matrix[0, 0] == pytest.approx(0.9363, abs=0.05)
126+
assert rotation_matrix[0, 1] == pytest.approx(-0.2751, abs=0.05)
127+
assert rotation_matrix[0, 2] == pytest.approx(0.2184, abs=0.05)
128+
assert rotation_matrix[1, 0] == pytest.approx(0.2896, abs=0.05)
129+
assert rotation_matrix[1, 1] == pytest.approx(0.9564, abs=0.05)
130+
assert rotation_matrix[1, 2] == pytest.approx(-0.0370, abs=0.05)
131+
assert rotation_matrix[2, 0] == pytest.approx(-0.1987, abs=0.05)
132+
assert rotation_matrix[2, 1] == pytest.approx(0.0978, abs=0.05)
133+
assert rotation_matrix[2, 2] == pytest.approx(0.9752, abs=0.05)
134134

135135

136136
def test_pose_rotation_matrix_values_different_pose():
137137
pose = PoseData(1, 2, 3, 0.5, -0.5, 0.9)
138138
rotation_matrix = pose.as_rotation_matrix()
139-
assert rotation_matrix[0] == pytest.approx(
140-
[0.54551407, -0.68743404, -0.47942554], abs=0.001
141-
)
142-
assert rotation_matrix[1] == pytest.approx(
143-
[0.5445577, 0.72556086, -0.42073549], abs=0.001
144-
)
145-
assert rotation_matrix[2] == pytest.approx(
146-
[0.6370803, -0.03155774, 0.77015115], abs=0.001
147-
)
139+
assert rotation_matrix[0] == pytest.approx([0.5455, -0.8303, 0.1140], abs=0.001)
140+
assert rotation_matrix[1] == pytest.approx([0.6874, 0.3655, -0.6276], abs=0.001)
141+
assert rotation_matrix[2] == pytest.approx([0.4794, 0.4207, 0.7702], abs=0.001)
148142

149143

150144
def test_twist_addition():

vortex_utils/python_utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ def ssa(angle: float) -> float:
88
return (angle + np.pi) % (2 * np.pi) - np.pi
99

1010

11-
def euler_to_quat(roll: float, pitch: float, yaw: float) -> np.ndarray:
12-
"""Converts euler angles to quaternion (x, y, z, w)."""
13-
rotation_matrix = Rotation.from_euler("XYZ", [roll, pitch, yaw]).as_matrix()
14-
quaternion = Rotation.from_matrix(rotation_matrix).as_quat()
15-
return quaternion
16-
17-
18-
def quat_to_euler(x: float, y: float, z: float, w: float) -> np.ndarray:
19-
"""Converts quaternion (x, y, z, w) to euler angles."""
20-
rotation_matrix = Rotation.from_quat([x, y, z, w]).as_matrix()
21-
euler_angles = Rotation.from_matrix(rotation_matrix).as_euler("XYZ")
22-
return euler_angles
11+
def euler_to_quat(
12+
roll: float, pitch: float, yaw: float, *, degrees: bool = False
13+
) -> np.ndarray:
14+
"""RPY (intrinsic x-y-z) -> quaternion (x, y, z, w)."""
15+
return Rotation.from_euler('xyz', [roll, pitch, yaw], degrees=degrees).as_quat()
16+
17+
18+
def quat_to_euler(
19+
x: float, y: float, z: float, w: float, *, degrees: bool = False
20+
) -> np.ndarray:
21+
"""Quaternion (x, y, z, w) -> RPY (intrinsic x-y-z)."""
22+
return Rotation.from_quat([x, y, z, w]).as_euler('xyz', degrees=degrees)
2323

2424

2525
@dataclass(slots=True)

0 commit comments

Comments
 (0)