|
8 | 8 |
|
9 | 9 | @converts_to_numpy(Vector3) |
10 | 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]) |
| 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 | 15 |
|
16 | 16 | @converts_from_numpy(Vector3) |
17 | 17 | def numpy_to_vector3(arr): |
18 | | - if arr.shape[-1] == 4: |
19 | | - assert np.all(arr[...,-1] == 0) |
20 | | - arr = arr[...,:-1] |
| 18 | + if arr.shape[-1] == 4: |
| 19 | + assert np.all(arr[...,-1] == 0) |
| 20 | + arr = arr[...,:-1] |
21 | 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) |
| 22 | + if len(arr.shape) == 1: |
| 23 | + return Vector3(**dict(zip(['x', 'y', 'z'], arr))) |
| 24 | + else: |
| 25 | + return np.apply_along_axis( |
| 26 | + lambda v: Vector3(**dict(zip(['x', 'y', 'z'], v))), axis=-1, |
| 27 | + arr=arr) |
26 | 28 |
|
27 | 29 | @converts_to_numpy(Point) |
28 | 30 | 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]) |
| 31 | + if hom: |
| 32 | + return np.array([msg.x, msg.y, msg.z, 1]) |
| 33 | + else: |
| 34 | + return np.array([msg.x, msg.y, msg.z]) |
33 | 35 |
|
34 | 36 | @converts_from_numpy(Point) |
35 | 37 | def numpy_to_point(arr): |
36 | | - if arr.shape[-1] == 4: |
37 | | - arr = arr[...,:-1] / arr[...,-1] |
| 38 | + if arr.shape[-1] == 4: |
| 39 | + arr = arr[...,:-1] / arr[...,-1] |
38 | 40 |
|
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) |
| 41 | + if len(arr.shape) == 1: |
| 42 | + return Point(**dict(zip(['x', 'y', 'z'], arr))) |
| 43 | + else: |
| 44 | + return np.apply_along_axis( |
| 45 | + lambda v: Point(**dict(zip(['x', 'y', 'z'], v))), axis=-1, arr=arr) |
43 | 46 |
|
44 | 47 | @converts_to_numpy(Quaternion) |
45 | 48 | def quat_to_numpy(msg): |
46 | | - return np.array([msg.x, msg.y, msg.z, msg.w]) |
| 49 | + return np.array([msg.x, msg.y, msg.z, msg.w]) |
47 | 50 |
|
48 | 51 | @converts_from_numpy(Quaternion) |
49 | 52 | def numpy_to_quat(arr): |
50 | | - assert arr.shape[-1] == 4 |
| 53 | + assert arr.shape[-1] == 4 |
51 | 54 |
|
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) |
| 55 | + if len(arr.shape) == 1: |
| 56 | + return Quaternion(**dict(zip(['x', 'y', 'z', 'w'], arr))) |
| 57 | + else: |
| 58 | + return np.apply_along_axis( |
| 59 | + lambda v: Quaternion(**dict(zip(['x', 'y', 'z', 'w'], v))), |
| 60 | + axis=-1, arr=arr) |
56 | 61 |
|
57 | 62 |
|
58 | 63 | # compound types |
59 | 64 | # all of these take ...x4x4 homogeneous matrices |
60 | 65 |
|
61 | 66 | @converts_to_numpy(Transform) |
62 | 67 | def transform_to_numpy(msg): |
63 | | - from tf import transformations |
| 68 | + from . import transformations |
64 | 69 |
|
65 | | - return np.dot( |
| 70 | + return np.dot( |
66 | 71 | transformations.translation_matrix(numpify(msg.translation)), |
67 | 72 | transformations.quaternion_matrix(numpify(msg.rotation)) |
68 | 73 | ) |
69 | 74 |
|
70 | 75 | @converts_from_numpy(Transform) |
71 | 76 | def numpy_to_transform(arr): |
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 | | - ) |
| 77 | + from . import transformations |
| 78 | + |
| 79 | + shape, rest = arr.shape[:-2], arr.shape[-2:] |
| 80 | + assert rest == (4,4) |
| 81 | + |
| 82 | + if len(shape) == 0: |
| 83 | + trans = transformations.translation_from_matrix(arr) |
| 84 | + quat = transformations.quaternion_from_matrix(arr) |
| 85 | + |
| 86 | + return Transform( |
| 87 | + translation=Vector3(**dict(zip(['x', 'y', 'z'], trans))), |
| 88 | + rotation=Quaternion(**dict(zip(['x', 'y', 'z', 'w'], quat))) |
| 89 | + ) |
| 90 | + else: |
| 91 | + res = np.empty(shape, dtype=np.object_) |
| 92 | + for idx in np.ndindex(shape): |
| 93 | + res[idx] = Transform( |
| 94 | + translation=Vector3( |
| 95 | + **dict( |
| 96 | + zip(['x', 'y', 'z'], |
| 97 | + transformations.translation_from_matrix(arr[idx])))), |
| 98 | + rotation=Quaternion( |
| 99 | + **dict( |
| 100 | + zip(['x', 'y', 'z', 'w'], |
| 101 | + transformations.quaternion_from_matrix(arr[idx])))) |
| 102 | + ) |
92 | 103 |
|
93 | 104 | @converts_to_numpy(Pose) |
94 | 105 | def pose_to_numpy(msg): |
95 | | - from tf import transformations |
| 106 | + from . import transformations |
96 | 107 |
|
97 | | - return np.dot( |
| 108 | + return np.dot( |
98 | 109 | transformations.translation_matrix(numpify(msg.position)), |
99 | 110 | transformations.quaternion_matrix(numpify(msg.orientation)) |
100 | 111 | ) |
101 | 112 |
|
102 | 113 | @converts_from_numpy(Pose) |
103 | 114 | 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 | | - |
| 115 | + from . import transformations |
| 116 | + |
| 117 | + shape, rest = arr.shape[:-2], arr.shape[-2:] |
| 118 | + assert rest == (4,4) |
| 119 | + |
| 120 | + if len(shape) == 0: |
| 121 | + trans = transformations.translation_from_matrix(arr) |
| 122 | + quat = transformations.quaternion_from_matrix(arr) |
| 123 | + |
| 124 | + return Pose( |
| 125 | + position=Point(**dict(zip(['x', 'y', 'z'], trans))), |
| 126 | + orientation=Quaternion(**dict(zip(['x', 'y', 'z', 'w'], quat))) |
| 127 | + ) |
| 128 | + else: |
| 129 | + res = np.empty(shape, dtype=np.object_) |
| 130 | + for idx in np.ndindex(shape): |
| 131 | + res[idx] = Pose( |
| 132 | + position=Point( |
| 133 | + **dict( |
| 134 | + zip(['x', 'y', 'z'], |
| 135 | + transformations.translation_from_matrix(arr[idx])))), |
| 136 | + orientation=Quaternion( |
| 137 | + **dict( |
| 138 | + zip(['x', 'y', 'z', 'w'], |
| 139 | + transformations.quaternion_from_matrix(arr[idx])))) |
| 140 | + ) |
0 commit comments