helper.py
Go to the documentation of this file.
1 import json
2 import math
3 
4 from ensenso_camera_test.ros2_testing import import_tf_transformation_function
5 
6 quaternion_about_axis = import_tf_transformation_function("quaternion_about_axis")
7 translation_matrix = import_tf_transformation_function("translation_matrix")
8 quaternion_matrix = import_tf_transformation_function("quaternion_matrix")
9 concatenate_matrices = import_tf_transformation_function("concatenate_matrices")
10 inverse_matrix = import_tf_transformation_function("inverse_matrix")
11 translation_from_matrix = import_tf_transformation_function("translation_from_matrix")
12 quaternion_from_matrix = import_tf_transformation_function("quaternion_from_matrix")
13 
14 
15 class Pose:
16  def __init__(self, position, orientation):
17  self.position = position
18  self.orientation = orientation
19 
20  @classmethod
21  def from_json(cls, path):
22  with open(path) as f:
23  transform = json.load(f)
24 
25  # The NxLib transformation in the JSON file is in millimeters. We
26  # represent it in meters like everything in ROS.
27  position = [
28  transform["Translation"][0] / 1000,
29  transform["Translation"][1] / 1000,
30  transform["Translation"][2] / 1000,
31  ]
32  axis = (transform["Rotation"]["Axis"][0], transform["Rotation"]["Axis"][1], transform["Rotation"]["Axis"][2])
33  orientation = quaternion_about_axis(transform["Rotation"]["Angle"], axis)
34 
35  return cls(position, orientation)
36 
37  @classmethod
38  def from_message(cls, message):
39  position = [message.position.x, message.position.y, message.position.z]
40  orientation = [message.orientation.x, message.orientation.y, message.orientation.z, message.orientation.w]
41 
42  return cls(position, orientation)
43 
44  def inverse(self):
45  """
46  Get the inverse of the pose.
47  """
48  translation = translation_matrix(self.position)
49  rotation = quaternion_matrix(self.orientation)
50  transform = concatenate_matrices(translation, rotation)
51 
52  inverse_transform = inverse_matrix(transform)
53  translation = translation_from_matrix(inverse_transform)
54  rotation = quaternion_from_matrix(inverse_transform)
55 
56  return Pose(translation, rotation)
57 
58  def equals(self, other, tolerance=0.002):
59  """
60  Check whether this pose is equal to another one. The poses are
61  considered equal, when all of the entries are within the given tolerance
62  of each other.
63  """
64  for a, b in zip(self.position, other.position):
65  if abs(a - b) > tolerance:
66  return False
67  for a, b in zip(self.orientation, other.orientation):
68  if abs(a - b) > tolerance:
69  return False
70  return True
71 
72 
73 class ImagePoint:
74  def __init__(self, x, y):
75  self.x = x
76  self.y = y
77 
78  @classmethod
79  def from_message(cls, message):
80  return cls(message.x, message.y)
81 
82  def distance_to(self, other):
83  return math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
84 
85  def equals(self, other, tolerance=1):
86  """
87  Check whether the points are the same, up to the given distance.
88  """
89  return self.distance_to(other) < tolerance
90 
91 
92 class Point:
93  def __init__(self, x=0, y=0, z=0):
94  self.x = x
95  self.y = y
96  self.z = z
97 
98  def dot_prod(self, other_point):
99  return self.x * other_point.x + self.y * other_point.y + self.z * other_point.z
100 
101 
103  def __init__(self, lower, upper):
104  self.lower = lower
105  self.upper = upper
106 
107  @classmethod
108  def from_message(cls, message):
109  lower = [message.lower.x, message.lower.y, message.lower.z]
110  upper = [message.upper.x, message.upper.y, message.upper.z]
111 
112  return cls(lower, upper)
113 
114  def write_to_message(self, message):
115  message.lower.x = float(self.lower[0])
116  message.lower.y = float(self.lower[1])
117  message.lower.z = float(self.lower[2])
118 
119  message.upper.x = float(self.upper[0])
120  message.upper.y = float(self.upper[1])
121  message.upper.z = float(self.upper[2])
122 
123  def is_empty(self):
124  for lower, upper in zip(self.lower, self.upper):
125  if lower >= upper:
126  return True
127  return False
128 
129  def equals(self, other, tolerance=0):
130  for a, b in zip(self.lower, other.lower):
131  if abs(a - b) > tolerance:
132  return False
133  for a, b in zip(self.upper, other.upper):
134  if abs(a - b) > tolerance:
135  return False
136  return True
ensenso_camera_test.helper.concatenate_matrices
concatenate_matrices
Definition: helper.py:9
ensenso_camera_test.helper.ImagePoint
Definition: helper.py:73
ensenso_camera_test.helper.translation_matrix
translation_matrix
Definition: helper.py:7
ensenso_camera_test.helper.Pose.__init__
def __init__(self, position, orientation)
Definition: helper.py:16
ensenso_camera_test.helper.RegionOfInterest.lower
lower
Definition: helper.py:104
ensenso_camera_test.helper.ImagePoint.__init__
def __init__(self, x, y)
Definition: helper.py:74
ensenso_camera_test.helper.Pose.position
position
Definition: helper.py:17
ensenso_camera_test.helper.ImagePoint.y
y
Definition: helper.py:76
ensenso_camera_test.helper.quaternion_from_matrix
quaternion_from_matrix
Definition: helper.py:12
ensenso_camera_test.helper.quaternion_about_axis
quaternion_about_axis
Definition: helper.py:6
ensenso_camera_test.helper.ImagePoint.distance_to
def distance_to(self, other)
Definition: helper.py:82
ensenso_camera_test.helper.RegionOfInterest
Definition: helper.py:102
ensenso_camera_test.helper.translation_from_matrix
translation_from_matrix
Definition: helper.py:11
ensenso_camera_test.helper.Point.z
z
Definition: helper.py:96
ensenso_camera_test.helper.Point.__init__
def __init__(self, x=0, y=0, z=0)
Definition: helper.py:93
ensenso_camera_test.helper.Point
Definition: helper.py:92
ensenso_camera_test.helper.ImagePoint.x
x
Definition: helper.py:75
ensenso_camera_test.helper.Point.x
x
Definition: helper.py:94
ensenso_camera_test.helper.RegionOfInterest.is_empty
def is_empty(self)
Definition: helper.py:123
ensenso_camera_test.helper.Pose.from_json
def from_json(cls, path)
Definition: helper.py:21
ensenso_camera_test.helper.RegionOfInterest.equals
def equals(self, other, tolerance=0)
Definition: helper.py:129
ensenso_camera_test.helper.Pose.inverse
def inverse(self)
Definition: helper.py:44
ensenso_camera_test.helper.Pose.from_message
def from_message(cls, message)
Definition: helper.py:38
ensenso_camera_test.helper.Pose.equals
def equals(self, other, tolerance=0.002)
Definition: helper.py:58
ensenso_camera_test.helper.RegionOfInterest.from_message
def from_message(cls, message)
Definition: helper.py:108
ensenso_camera_test.helper.RegionOfInterest.upper
upper
Definition: helper.py:105
ensenso_camera_test.helper.RegionOfInterest.write_to_message
def write_to_message(self, message)
Definition: helper.py:114
ensenso_camera_test.helper.inverse_matrix
inverse_matrix
Definition: helper.py:10
ensenso_camera_test.helper.Point.dot_prod
def dot_prod(self, other_point)
Definition: helper.py:98
ensenso_camera_test.helper.ImagePoint.from_message
def from_message(cls, message)
Definition: helper.py:79
ensenso_camera_test.helper.quaternion_matrix
quaternion_matrix
Definition: helper.py:8
ensenso_camera_test.ros2_testing
Definition: ros2_testing.py:1
ensenso_camera_test.helper.ImagePoint.equals
def equals(self, other, tolerance=1)
Definition: helper.py:85
ensenso_camera_test.helper.RegionOfInterest.__init__
def __init__(self, lower, upper)
Definition: helper.py:103
ensenso_camera_test.helper.Pose
Definition: helper.py:15
ensenso_camera_test.helper.Pose.orientation
orientation
Definition: helper.py:18
ensenso_camera_test.helper.Point.y
y
Definition: helper.py:95
ensenso_camera_test.ros2_testing.import_tf_transformation_function
def import_tf_transformation_function(function_name)
Definition: ros2_testing.py:16


ensenso_camera_test
Author(s): Ensenso
autogenerated on Wed Apr 2 2025 02:37:52