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


ensenso_camera_test
Author(s): Ensenso
autogenerated on Thu May 6 2021 02:50:10