|
19 | 19 | from dimos_lcm.geometry_msgs import Transform as LCMTransform |
20 | 20 | from dimos_lcm.geometry_msgs import TransformStamped as LCMTransformStamped |
21 | 21 |
|
22 | | -from dimos.msgs.geometry_msgs import Pose, Quaternion, Transform, Vector3 |
| 22 | +from dimos.msgs.geometry_msgs import Pose, PoseStamped, Quaternion, Transform, Vector3 |
23 | 23 |
|
24 | 24 |
|
25 | 25 | def test_transform_initialization(): |
@@ -228,3 +228,156 @@ def test_lcm_encode_decode(): |
228 | 228 | decoded_transform = Transform.lcm_decode(data) |
229 | 229 |
|
230 | 230 | assert decoded_transform == transform |
| 231 | + |
| 232 | + |
| 233 | +def test_transform_addition(): |
| 234 | + # Test 1: Simple translation addition (no rotation) |
| 235 | + t1 = Transform( |
| 236 | + translation=Vector3(1, 0, 0), |
| 237 | + rotation=Quaternion(0, 0, 0, 1), # identity rotation |
| 238 | + ) |
| 239 | + t2 = Transform( |
| 240 | + translation=Vector3(2, 0, 0), |
| 241 | + rotation=Quaternion(0, 0, 0, 1), # identity rotation |
| 242 | + ) |
| 243 | + t3 = t1 + t2 |
| 244 | + assert t3.translation == Vector3(3, 0, 0) |
| 245 | + assert t3.rotation == Quaternion(0, 0, 0, 1) |
| 246 | + |
| 247 | + # Test 2: 90-degree rotation composition |
| 248 | + # First transform: move 1 unit in X |
| 249 | + t1 = Transform( |
| 250 | + translation=Vector3(1, 0, 0), |
| 251 | + rotation=Quaternion(0, 0, 0, 1), # identity |
| 252 | + ) |
| 253 | + # Second transform: move 1 unit in X with 90-degree rotation around Z |
| 254 | + angle = np.pi / 2 |
| 255 | + t2 = Transform( |
| 256 | + translation=Vector3(1, 0, 0), |
| 257 | + rotation=Quaternion(0, 0, np.sin(angle / 2), np.cos(angle / 2)), |
| 258 | + ) |
| 259 | + t3 = t1 + t2 |
| 260 | + assert t3.translation == Vector3(2, 0, 0) |
| 261 | + # Rotation should be 90 degrees around Z |
| 262 | + assert np.isclose(t3.rotation.x, 0.0, atol=1e-10) |
| 263 | + assert np.isclose(t3.rotation.y, 0.0, atol=1e-10) |
| 264 | + assert np.isclose(t3.rotation.z, np.sin(angle / 2), atol=1e-10) |
| 265 | + assert np.isclose(t3.rotation.w, np.cos(angle / 2), atol=1e-10) |
| 266 | + |
| 267 | + # Test 3: Rotation affects translation |
| 268 | + # First transform: 90-degree rotation around Z |
| 269 | + t1 = Transform( |
| 270 | + translation=Vector3(0, 0, 0), |
| 271 | + rotation=Quaternion(0, 0, np.sin(angle / 2), np.cos(angle / 2)), # 90° around Z |
| 272 | + ) |
| 273 | + # Second transform: move 1 unit in X |
| 274 | + t2 = Transform( |
| 275 | + translation=Vector3(1, 0, 0), |
| 276 | + rotation=Quaternion(0, 0, 0, 1), # identity |
| 277 | + ) |
| 278 | + t3 = t1 + t2 |
| 279 | + # X direction rotated 90° becomes Y direction |
| 280 | + assert np.isclose(t3.translation.x, 0.0, atol=1e-10) |
| 281 | + assert np.isclose(t3.translation.y, 1.0, atol=1e-10) |
| 282 | + assert np.isclose(t3.translation.z, 0.0, atol=1e-10) |
| 283 | + # Rotation remains 90° around Z |
| 284 | + assert np.isclose(t3.rotation.z, np.sin(angle / 2), atol=1e-10) |
| 285 | + assert np.isclose(t3.rotation.w, np.cos(angle / 2), atol=1e-10) |
| 286 | + |
| 287 | + # Test 4: Frame tracking |
| 288 | + t1 = Transform( |
| 289 | + translation=Vector3(1, 0, 0), |
| 290 | + rotation=Quaternion(0, 0, 0, 1), |
| 291 | + frame_id="world", |
| 292 | + child_frame_id="robot", |
| 293 | + ) |
| 294 | + t2 = Transform( |
| 295 | + translation=Vector3(2, 0, 0), |
| 296 | + rotation=Quaternion(0, 0, 0, 1), |
| 297 | + frame_id="robot", |
| 298 | + child_frame_id="sensor", |
| 299 | + ) |
| 300 | + t3 = t1 + t2 |
| 301 | + assert t3.frame_id == "world" |
| 302 | + assert t3.child_frame_id == "sensor" |
| 303 | + |
| 304 | + # Test 5: Type error |
| 305 | + with pytest.raises(TypeError): |
| 306 | + t1 + "not a transform" |
| 307 | + |
| 308 | + |
| 309 | +def test_transform_from_pose(): |
| 310 | + """Test converting Pose to Transform""" |
| 311 | + # Create a Pose with position and orientation |
| 312 | + pose = Pose( |
| 313 | + position=Vector3(1.0, 2.0, 3.0), |
| 314 | + orientation=Quaternion(0.0, 0.0, 0.707, 0.707), # 90 degrees around Z |
| 315 | + ) |
| 316 | + |
| 317 | + # Convert to Transform |
| 318 | + transform = Transform.from_pose("base_link", pose) |
| 319 | + |
| 320 | + # Check that translation and rotation match |
| 321 | + assert transform.translation == pose.position |
| 322 | + assert transform.rotation == pose.orientation |
| 323 | + assert transform.frame_id == "world" # default frame_id |
| 324 | + assert transform.child_frame_id == "base_link" # passed as first argument |
| 325 | + |
| 326 | + |
| 327 | +def test_transform_from_pose_stamped(): |
| 328 | + """Test converting PoseStamped to Transform""" |
| 329 | + # Create a PoseStamped with position, orientation, timestamp and frame |
| 330 | + test_time = time.time() |
| 331 | + pose_stamped = PoseStamped( |
| 332 | + ts=test_time, |
| 333 | + frame_id="map", |
| 334 | + position=Vector3(4.0, 5.0, 6.0), |
| 335 | + orientation=Quaternion(0.0, 0.707, 0.0, 0.707), # 90 degrees around Y |
| 336 | + ) |
| 337 | + |
| 338 | + # Convert to Transform |
| 339 | + transform = Transform.from_pose("robot_base", pose_stamped) |
| 340 | + |
| 341 | + # Check that all fields match |
| 342 | + assert transform.translation == pose_stamped.position |
| 343 | + assert transform.rotation == pose_stamped.orientation |
| 344 | + assert transform.frame_id == pose_stamped.frame_id |
| 345 | + assert transform.ts == pose_stamped.ts |
| 346 | + assert transform.child_frame_id == "robot_base" # passed as first argument |
| 347 | + |
| 348 | + |
| 349 | +def test_transform_from_pose_variants(): |
| 350 | + """Test from_pose with different Pose initialization methods""" |
| 351 | + # Test with Pose created from x,y,z |
| 352 | + pose1 = Pose(1.0, 2.0, 3.0) |
| 353 | + transform1 = Transform.from_pose("base_link", pose1) |
| 354 | + assert transform1.translation.x == 1.0 |
| 355 | + assert transform1.translation.y == 2.0 |
| 356 | + assert transform1.translation.z == 3.0 |
| 357 | + assert transform1.rotation.w == 1.0 # Identity quaternion |
| 358 | + |
| 359 | + # Test with Pose created from tuple |
| 360 | + pose2 = Pose(([7.0, 8.0, 9.0], [0.0, 0.0, 0.0, 1.0])) |
| 361 | + transform2 = Transform.from_pose("base_link", pose2) |
| 362 | + assert transform2.translation.x == 7.0 |
| 363 | + assert transform2.translation.y == 8.0 |
| 364 | + assert transform2.translation.z == 9.0 |
| 365 | + |
| 366 | + # Test with Pose created from dict |
| 367 | + pose3 = Pose({"position": [10.0, 11.0, 12.0], "orientation": [0.0, 0.0, 0.0, 1.0]}) |
| 368 | + transform3 = Transform.from_pose("base_link", pose3) |
| 369 | + assert transform3.translation.x == 10.0 |
| 370 | + assert transform3.translation.y == 11.0 |
| 371 | + assert transform3.translation.z == 12.0 |
| 372 | + |
| 373 | + |
| 374 | +def test_transform_from_pose_invalid_type(): |
| 375 | + """Test that from_pose raises TypeError for invalid types""" |
| 376 | + with pytest.raises(TypeError): |
| 377 | + Transform.from_pose("not a pose") |
| 378 | + |
| 379 | + with pytest.raises(TypeError): |
| 380 | + Transform.from_pose(42) |
| 381 | + |
| 382 | + with pytest.raises(TypeError): |
| 383 | + Transform.from_pose(None) |
0 commit comments