geometry.py
Go to the documentation of this file.
1 from .registry import converts_from_numpy, converts_to_numpy
2 from geometry_msgs.msg import Transform, Vector3, Quaternion, Point, Pose
3 from . import numpify
4 
5 import numpy as np
6 
7 # basic types
8 
9 @converts_to_numpy(Vector3)
10 def vector3_to_numpy(msg, hom=False):
11  if hom:
12  return np.array([msg.x, msg.y, msg.z, 0])
13  else:
14  return np.array([msg.x, msg.y, msg.z])
15 
16 @converts_from_numpy(Vector3)
18  if arr.shape[-1] == 4:
19  assert np.all(arr[...,-1] == 0)
20  arr = arr[...,:-1]
21 
22  if len(arr.shape) == 1:
23  return Vector3(*arr)
24  else:
25  return np.apply_along_axis(lambda v: Vector3(*v), axis=-1, arr=arr)
26 
27 @converts_to_numpy(Point)
28 def point_to_numpy(msg, hom=False):
29  if hom:
30  return np.array([msg.x, msg.y, msg.z, 1])
31  else:
32  return np.array([msg.x, msg.y, msg.z])
33 
34 @converts_from_numpy(Point)
35 def numpy_to_point(arr):
36  if arr.shape[-1] == 4:
37  arr = arr[...,:-1] / arr[...,-1]
38 
39  if len(arr.shape) == 1:
40  return Point(*arr)
41  else:
42  return np.apply_along_axis(lambda v: Point(*v), axis=-1, arr=arr)
43 
44 @converts_to_numpy(Quaternion)
45 def quat_to_numpy(msg):
46  return np.array([msg.x, msg.y, msg.z, msg.w])
47 
48 @converts_from_numpy(Quaternion)
49 def numpy_to_quat(arr):
50  assert arr.shape[-1] == 4
51 
52  if len(arr.shape) == 1:
53  return Quaternion(*arr)
54  else:
55  return np.apply_along_axis(lambda v: Quaternion(*v), axis=-1, arr=arr)
56 
57 
58 # compound types
59 # all of these take ...x4x4 homogeneous matrices
60 
61 @converts_to_numpy(Transform)
63  from tf import transformations
64 
65  return np.dot(
66  transformations.translation_matrix(numpify(msg.translation)),
67  transformations.quaternion_matrix(numpify(msg.rotation))
68  )
69 
70 @converts_from_numpy(Transform)
72  from tf import transformations
73 
74  shape, rest = arr.shape[:-2], arr.shape[-2:]
75  assert rest == (4,4)
76 
77  if len(shape) == 0:
78  trans = transformations.translation_from_matrix(arr)
79  quat = transformations.quaternion_from_matrix(arr)
80 
81  return Transform(
82  translation=Vector3(*trans),
83  rotation=Quaternion(*quat)
84  )
85  else:
86  res = np.empty(shape, dtype=np.object_)
87  for idx in np.ndindex(shape):
88  res[idx] = Transform(
89  translation=Vector3(*transformations.translation_from_matrix(arr[idx])),
90  rotation=Quaternion(*transformations.quaternion_from_matrix(arr[idx]))
91  )
92 
93 @converts_to_numpy(Pose)
94 def pose_to_numpy(msg):
95  from tf import transformations
96 
97  return np.dot(
98  transformations.translation_matrix(numpify(msg.position)),
99  transformations.quaternion_matrix(numpify(msg.orientation))
100  )
101 
102 @converts_from_numpy(Pose)
103 def numpy_to_pose(arr):
104  from tf import transformations
105 
106  shape, rest = arr.shape[:-2], arr.shape[-2:]
107  assert rest == (4,4)
108 
109  if len(shape) == 0:
110  trans = transformations.translation_from_matrix(arr)
111  quat = transformations.quaternion_from_matrix(arr)
112 
113  return Pose(
114  position=Vector3(*trans),
115  orientation=Quaternion(*quat)
116  )
117  else:
118  res = np.empty(shape, dtype=np.object_)
119  for idx in np.ndindex(shape):
120  res[idx] = Pose(
121  position=Vector3(*transformations.translation_from_matrix(arr[idx])),
122  orientation=Quaternion(*transformations.quaternion_from_matrix(arr[idx]))
123  )
124 
def numpify(msg, args, kwargs)
Definition: registry.py:25
def converts_from_numpy(msgtype, plural=False)
Definition: registry.py:17
def numpy_to_point(arr)
Definition: geometry.py:35
def quat_to_numpy(msg)
Definition: geometry.py:45
def pose_to_numpy(msg)
Definition: geometry.py:94
def converts_to_numpy(msgtype, plural=False)
Definition: registry.py:9
def numpy_to_vector3(arr)
Definition: geometry.py:17
def numpy_to_quat(arr)
Definition: geometry.py:49
def numpy_to_transform(arr)
Definition: geometry.py:71
def vector3_to_numpy(msg, hom=False)
Definition: geometry.py:10
def point_to_numpy(msg, hom=False)
Definition: geometry.py:28
def numpy_to_pose(arr)
Definition: geometry.py:103
def transform_to_numpy(msg)
Definition: geometry.py:62


ros_numpy
Author(s): Eric Wieser
autogenerated on Sat Oct 3 2020 03:25:57