KITTI-C2L-Dataset / code /tests /test_geometry.py
mcocheteux's picture
Release KITTI-C2L v1.0.0: documented self-contained benchmark, loader and baselines
db613b6 verified
Raw History Blame Contribute Delete
2.92 kB
from __future__ import annotations
import numpy as np
from kitti_c2l.geometry import (
euler_deg_to_matrix,
matrix_to_euler_deg,
matrix_to_rotation_6d,
project_to_image,
rotation_6d_to_matrix,
se3_compose,
se3_inverse,
transform_points,
)
def test_euler_round_trip():
euler_deg = np.array([12.0, -7.0, 33.0])
matrix = euler_deg_to_matrix(euler_deg)
recovered = matrix_to_euler_deg(matrix)
np.testing.assert_allclose(recovered, euler_deg, atol=1e-4)
def test_euler_identity_at_zero():
matrix = euler_deg_to_matrix(np.zeros(3))
np.testing.assert_allclose(matrix, np.eye(3), atol=1e-10)
def test_rotation_6d_round_trip():
matrix = euler_deg_to_matrix(np.array([5.0, 10.0, -15.0]))
d6 = matrix_to_rotation_6d(matrix)
recovered = rotation_6d_to_matrix(d6)
np.testing.assert_allclose(recovered, matrix, atol=1e-6)
def test_transform_points_identity():
points = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
transformed = transform_points(points, np.eye(3), np.zeros(3))
np.testing.assert_allclose(transformed, points)
def test_transform_points_translation_only():
points = np.array([[1.0, 2.0, 3.0]])
transformed = transform_points(points, np.eye(3), np.array([10.0, 0.0, 0.0]))
np.testing.assert_allclose(transformed, [[11.0, 2.0, 3.0]])
def test_project_to_image_principal_point():
# A point on the optical axis (relative to principal point offset) projects to (cx, cy).
intrinsics = np.array([[700.0, 0.0, 16.0], [0.0, 700.0, 16.0], [0.0, 0.0, 1.0]])
points_cam = np.array([[0.0, 0.0, 5.0]])
pixels = project_to_image(points_cam, intrinsics)
np.testing.assert_allclose(pixels, [[16.0, 16.0]], atol=1e-6)
def test_se3_inverse_round_trip():
rotation = euler_deg_to_matrix(np.array([10.0, 20.0, 30.0]))
translation = np.array([1.0, -2.0, 3.0])
rotation_inv, translation_inv = se3_inverse(rotation, translation)
rotation_id, translation_id = se3_compose(rotation, translation, rotation_inv, translation_inv)
np.testing.assert_allclose(rotation_id, np.eye(3), atol=1e-10)
np.testing.assert_allclose(translation_id, np.zeros(3), atol=1e-10)
def test_se3_compose_matches_matrix_multiplication():
rng = np.random.default_rng(0)
r_b_a = euler_deg_to_matrix(rng.uniform(-30, 30, size=3))
t_b_a = rng.uniform(-10, 10, size=3)
r_c_b = euler_deg_to_matrix(rng.uniform(-30, 30, size=3))
t_c_b = rng.uniform(-10, 10, size=3)
r_c_a, t_c_a = se3_compose(r_b_a, t_b_a, r_c_b, t_c_b)
# Cross-check via explicit 4x4 homogeneous matrices.
def homogeneous(r, t):
m = np.eye(4)
m[:3, :3] = r
m[:3, 3] = t
return m
expected = homogeneous(r_c_b, t_c_b) @ homogeneous(r_b_a, t_b_a)
np.testing.assert_allclose(r_c_a, expected[:3, :3], atol=1e-10)
np.testing.assert_allclose(t_c_a, expected[:3, 3], atol=1e-10)