Skip to content

Commit c606906

Browse files
committed
(fix) Fixes geometry transformation utils
1 parent 315f0db commit c606906

File tree

2 files changed

+57
-59
lines changed

2 files changed

+57
-59
lines changed

src/kompass_core/mapping/local_mapper.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
OCCUPANCY_TYPE,
1010
)
1111

12-
from ..utils.geometry import from_frame1_to_frame2, get_pose_target_in_reference_frame
12+
from ..utils.geometry import transform_point_from_local_to_global, get_relative_pose
1313

1414
from .laserscan_model import LaserScanModelConfig
1515
from ..utils.common import BaseAttrs, base_validators
@@ -221,8 +221,8 @@ def _calculate_grid_shift(self, current_robot_pose: PoseData):
221221
# i.e. we have a t+1 state
222222
# get current shift in translation and orientation of the new center
223223
# with respect to the previous old center
224-
pose_current_robot_in_previous_robot = get_pose_target_in_reference_frame(
225-
reference_pose=self._pose_robot_in_world, target_pose=current_robot_pose
224+
pose_current_robot_in_previous_robot = get_relative_pose(
225+
pose_1_in_ref=self._pose_robot_in_world, pose_2_in_ref=current_robot_pose
226226
)
227227
# new position and orientation with respect to the previous pose
228228
_position_in_previous_pose = pose_current_robot_in_previous_robot.get_position()
@@ -264,8 +264,8 @@ def update_from_scan(
264264

265265
# Calculate new grid pose
266266
self._pose_robot_in_world = robot_pose
267-
self.lower_right_corner_pose = from_frame1_to_frame2(
268-
robot_pose, self._local_lower_right_corner_point
267+
self.lower_right_corner_pose = transform_point_from_local_to_global(
268+
self._local_lower_right_corner_point, robot_pose
269269
)
270270

271271
if self.config.baysian_update:

src/kompass_core/utils/geometry.py

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -116,74 +116,72 @@ def _rotate_vector_by_quaternion(q: np.ndarray, v: List[float]) -> List[float]:
116116
return rotated_vq[1:].tolist()
117117

118118

119-
def get_pose_target_in_reference_frame(
120-
reference_pose: PoseData, target_pose: PoseData
121-
) -> PoseData:
119+
def _transform_pose(pose: PoseData, reference_pose: PoseData) -> PoseData:
122120
"""
123-
Computes a target pose with respect to the frame (coordinate system) defined by a reference.
124-
target and reference should be given first in a common frame
125-
126-
:param reference_pose: Pose of reference frame in a common frame
127-
:type reference_pose: PoseData
128-
:param target_pose: Pose of target frame in a common frame
129-
:type target_pose: PoseData
121+
Transforms a pose from a local frame to a global frame using a reference pose.
122+
Equivalent to: global_pose = reference_pose * local_pose
130123
131-
:return: The pose of target in the reference frame
132-
:rtype: PoseData
124+
:param pose: PoseData in local frame
125+
:type pose: PoseData
126+
:param reference_pose: PoseData of local frame in global frame
127+
:type reference_pose: PoseData
128+
:return: PoseData in global frame
129+
:rtype: PoseData
133130
"""
134-
position_target = target_pose.get_position()
135-
orientation_target = target_pose.get_orientation()
136-
137-
reference_position = reference_pose.get_position()
138-
reference_rotation = reference_pose.get_orientation()
139-
140-
orientation_target_in_ref = (
141-
_np_quaternion_conjugate(reference_rotation) * orientation_target
142-
)
131+
rotated_position = _rotate_vector_by_quaternion(reference_pose.get_orientation(), pose.get_position().tolist())
132+
translated_position = reference_pose.get_position() + np.array(rotated_position)
143133

144-
position_target_in_ref = _rotate_vector_by_quaternion(
145-
_np_quaternion_conjugate(reference_rotation),
146-
(position_target - reference_position).tolist(),
147-
)
134+
combined_orientation = _np_quaternion_multiply(reference_pose.get_orientation(), pose.get_orientation())
148135

149-
target_pose_in_ref = PoseData()
150-
target_pose_in_ref.set_pose(
151-
x=position_target_in_ref[0],
152-
y=position_target_in_ref[1],
153-
z=position_target_in_ref[2],
154-
qw=orientation_target_in_ref[0],
155-
qx=orientation_target_in_ref[1],
156-
qy=orientation_target_in_ref[2],
157-
qz=orientation_target_in_ref[3],
136+
result = PoseData()
137+
result.set_pose(
138+
*translated_position,
139+
*combined_orientation
158140
)
141+
return result
159142

160-
return target_pose_in_ref
161143

144+
def _inverse_pose(pose: PoseData) -> PoseData:
145+
"""
146+
Computes the inverse of a pose (converts pose_in_frame to frame_in_pose)
162147
163-
def from_frame1_to_frame2(
164-
pose_1_in_2: PoseData, pose_target_in_1: PoseData
165-
) -> PoseData:
148+
:param pose: PoseData to invert
149+
:type pose: PoseData
150+
:return: Inverse pose
151+
:rtype: PoseData
166152
"""
167-
get the pose of a target in frame 2 instead of frame 1
153+
inv_orientation = _np_quaternion_conjugate(pose.get_orientation())
154+
inv_position = _rotate_vector_by_quaternion(inv_orientation, (-pose.get_position()).tolist())
155+
156+
result = PoseData()
157+
result.set_pose(
158+
*inv_position,
159+
*inv_orientation
160+
)
161+
return result
168162

169-
:param pose_1_in_2: pose of frame 1 in frame 2
170-
:type pose_1_in_2: PoseData
171-
:param pose_target_in_1: pose of target in frame 1
172-
:type pose_target_in_1: PoseData
163+
def transform_point_from_local_to_global(point_pose_in_local: PoseData, robot_pose_in_global: PoseData) -> PoseData:
164+
"""Transforms a point from the robot frame to the global frame.
173165
174-
:return: pose of target in frame 2
175-
:rtype: PoseData
166+
:param point_pose_in_local: Target point in robot frame
167+
:type point_pose_in_local: PoseData
168+
:param robot_pose_in_global: Robot pose in global frame
169+
:type robot_pose_in_global: PoseData
170+
:return: Target point in global frame
171+
:rtype: PoseData
176172
"""
177-
pose_2_origin = PoseData()
178-
pose_2_in_1 = get_pose_target_in_reference_frame(
179-
reference_pose=pose_1_in_2, target_pose=pose_2_origin
180-
)
173+
return _transform_pose(point_pose_in_local, robot_pose_in_global)
181174

182-
pose_target_in_2 = get_pose_target_in_reference_frame(
183-
reference_pose=pose_2_in_1, target_pose=pose_target_in_1
184-
)
175+
def get_relative_pose(pose_1_in_ref: PoseData, pose_2_in_ref: PoseData) -> PoseData:
176+
"""
177+
Computes the pose of pose_2 in the coordinate frame of pose_1.
185178
186-
return pose_target_in_2
179+
:param pose_1_in_ref: PoseData of frame 1 in reference frame
180+
:param pose_2_in_ref: PoseData of frame 2 in reference frame
181+
:return: PoseData of pose_2 in frame 1
182+
"""
183+
pose_ref_in_1 = _inverse_pose(pose_1_in_ref)
184+
return _transform_pose(pose_2_in_ref, pose_ref_in_1)
187185

188186

189187
def from_euler_to_quaternion(yaw: float, pitch: float, roll: float) -> np.ndarray:
@@ -275,7 +273,7 @@ def from_frame1_to_frame2_2d(
275273
)
276274

277275
# Get transformd pose
278-
pose_target_in_2: PoseData = from_frame1_to_frame2(pose_1_in_2, pose_target_in_1)
276+
pose_target_in_2: PoseData = get_relative_pose(pose_1_in_2, pose_target_in_1)
279277

280278
return pose_target_in_2
281279

0 commit comments

Comments
 (0)