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

Adds Pose equivalency functions #130

Merged
merged 11 commits into from
Jul 25, 2023
49 changes: 48 additions & 1 deletion pyrobosim/pyrobosim/utils/pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np
from transforms3d.euler import euler2quat, quat2euler
from transforms3d.quaternions import mat2quat, qnorm, quat2mat
from transforms3d.quaternions import mat2quat, nearly_equivalent, qnorm, quat2mat


class Pose:
Expand Down Expand Up @@ -185,6 +185,15 @@ def get_transform_matrix(self):
tf_mat[:3, :3] = self.get_rotation_matrix()
return tf_mat

def get_translation(self):
"""
Gets the pose x y and z of the pose as an array.

:return: Pose x y and z as an array
:rtype: :class: numpy.ndarray
"""
return np.array([self.x, self.y, self.z])

def __repr__(self):
"""
Representation for printing a Pose object.
Expand All @@ -199,6 +208,44 @@ def __repr__(self):
)
return f"Pose: [{pos_str}, {quat_str}]"

def is_approx(self, other, rel_tol=1e-09, abs_tol=0.0):
sea-bass marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the default abs_tol=0, then is_approx() will always return False for non-identical poses with default values. I changed these both to default to 1e-6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I misunderstood how the abs_tol works then : ) thanks

"""
Check if two poses are approximately equal with a tolerance.

:param other: Pose with which to check approximate equality.
:type other: :class:`pyrobosim.utils.pose.Pose`
:param rel_tol: Relative tolerance
:type rel_tol: float
:param abs_tol: Absolute tolerance
:type abs_tol: float

:return: True if the Poses are approximately equal, else False
:rtype: bool
"""
if not (isinstance(other, Pose)):
raise TypeError("Expected a Pose")

return np.allclose(
self.get_translation, other.get_translation, rel_tol, abs_tol
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're supposed to actually call the function

Suggested change
self.get_translation, other.get_translation, rel_tol, abs_tol
self.get_translation(), other.get_translation(), rel_tol, abs_tol

) and nearly_equivalent(self.q, other.q, rel_tol, abs_tol)

def __eq__(self, other):
sea-bass marked this conversation as resolved.
Show resolved Hide resolved
"""
Check if two poses are equal

:param other: Pose with which to check equality.
:type other: :class:`pyrobosim.utils.pose.Pose`

:return: True if the poses are equal, else False
:rtype: bool
"""
if not (isinstance(other, Pose)):
raise TypeError("Expected a Pose")

return np.all(self.get_translation == other.get_translation) and np.all(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and same here

Suggested change
return np.all(self.get_translation == other.get_translation) and np.all(
return np.all(self.get_translation() == other.get_translation()) and np.all(

self.q == other.q
)


def get_angle(p1, p2):
"""
Expand Down