Skip to content

Commit

Permalink
[SymForce-External] Add to_angle for the Rot2 class
Browse files Browse the repository at this point in the history
Add `to_angle` for the `Rot2` class

I suggest adding a convenience function to get the angle from a 2D rotation. It complements the existing `from_angle`, and is intuitive to use when working with 2D rotation angles.

Closes #358

GitOrigin-RevId: 04bfe2a4ac1e658b218287e96cd1d2f7505dedf1
  • Loading branch information
hflemmen authored and aaron-skydio committed Aug 23, 2023
1 parent 50dcff6 commit ef86220
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions symforce/geo/rot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ def from_angle(cls, theta: T.Scalar) -> Rot2:
"""
return cls.from_tangent([theta])

def to_angle(self, epsilon: T.Scalar = sf.epsilon()) -> T.Scalar:
"""
Get the angle of this Rot2 in radians
This is equivalent to ``to_tangent()[0]``
"""
return self.to_tangent(epsilon)[0]

def to_rotation_matrix(self) -> Matrix22:
"""
A matrix representation of this element in the Euclidean space that contains it.
Expand Down
13 changes: 13 additions & 0 deletions test/geo_rot2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def test_angle_constructor(self) -> None:
rot2 = sf.Rot2.from_tangent([1.5])
self.assertEqual(rot1, rot2)

def test_from_to_angle(self) -> None:
"""
Tests:
Rot2.from_angle
Rot2.to_angle
"""
for angle, angle_gt in zip(
[0.0, np.pi / 2, np.pi, 3 * np.pi / 2, 2 * np.pi],
[0.0, np.pi / 2, np.pi, -np.pi / 2, 0.0],
):
rot = sf.Rot2.from_angle(angle).evalf()
self.assertLess(abs(angle_gt - rot.to_angle()), 1e-8)

def test_lie_exponential(self) -> None:
"""
Tests:
Expand Down

0 comments on commit ef86220

Please sign in to comment.