Files

225 lines
7.3 KiB
Python

import numpy as np
import pytest
from geometry_msgs.msg import Pose, Point, Quaternion
from scene_graph_generation_pipeline.object_localization.geometry_utils import GeometryUtils
def test_project_mask_to_3d():
# Setup test inputs
depth_scale = 0.001
intrinsics = {
'fx': 500.0, 'fy': 500.0,
'cx': 320.0, 'cy': 240.0
}
# 640x480 depth image of constant depth 1000 (which corresponds to 1.0 meter)
depth_img = np.full((480, 640), 1000, dtype=np.uint16)
# Simple 2D mask of 2x2 pixels at the center
mask = np.zeros((480, 640), dtype=bool)
mask[240, 320] = True
mask[240, 321] = True
# Project
points_3d = GeometryUtils.project_mask_to_3d(depth_img, mask, intrinsics, depth_scale)
assert points_3d.shape[0] == 2
assert points_3d.shape[1] == 3
# Point 1 check: mask[240, 320]
# u = 320, v = 240.
# z = 1000 * 0.001 = 1.0
# x = (320 - 320) * 1.0 / 500 = 0.0
# y = (240 - 240) * 1.0 / 500 = 0.0
# So point 1 should be approx [0, 0, 1]
np.testing.assert_allclose(points_3d[0], [0.0, 0.0, 1.0], atol=1e-5)
# Point 2 check: mask[240, 321]
# u = 321, v = 240
# z = 1.0
# x = (321 - 320) * 1.0 / 500 = 0.002
np.testing.assert_allclose(points_3d[1], [0.002, 0.0, 1.0], atol=1e-5)
def test_project_mask_to_3d_out_of_range():
# Setup test inputs
depth_scale = 0.001
intrinsics = {
'fx': 500.0, 'fy': 500.0,
'cx': 320.0, 'cy': 240.0
}
# Depth values that are filtered out (e.g. 50 meaning 0.05m (<0.1m) or 15000 meaning 15.0m (>10.0m))
depth_img = np.full((480, 640), 50, dtype=np.uint16)
mask = np.zeros((480, 640), dtype=bool)
mask[240, 320] = True
points_3d = GeometryUtils.project_mask_to_3d(depth_img, mask, intrinsics, depth_scale)
assert points_3d.shape == (0, 3) or len(points_3d) == 0
def test_project_bbox_to_3d():
depth_scale = 0.001
intrinsics = {
'fx': 500.0, 'fy': 500.0,
'cx': 320.0, 'cy': 240.0
}
# 640x480 depth image of constant depth 1000 (1.0 meter)
depth_img = np.full((480, 640), 1000, dtype=np.uint16)
# Bbox centered
bbox = [300, 220, 340, 260] # w=40, h=40
# shrink_ratio=0.15 => dx = 6, dy = 6. Shrunk bbox: [306, 226, 334, 254]
# width = 334 - 306 + 1 = 29
# height = 254 - 226 + 1 = 29
# Total points = 29 * 29 = 841
points_3d = GeometryUtils.project_bbox_to_3d(depth_img, bbox, intrinsics, depth_scale, shrink_ratio=0.15)
assert points_3d.shape[0] == 841
assert points_3d.shape[1] == 3
# Check that points are indeed in the shrunk bounds
# center is u=320, v=240 => x=0, y=0, z=1
# Check min/max values
# sx1=306 => x_min = (306 - 320) * 1.0 / 500 = -0.028
# sx2=334 => x_max = (334 - 320) * 1.0 / 500 = 0.028
# sy1=226 => y_min = (226 - 240) * 1.0 / 500 = -0.028
# sy2=254 => y_max = (254 - 240) * 1.0 / 500 = 0.028
np.testing.assert_allclose(np.min(points_3d, axis=0), [-0.028, -0.028, 1.0], atol=1e-5)
np.testing.assert_allclose(np.max(points_3d, axis=0), [0.028, 0.028, 1.0], atol=1e-5)
def test_clean_outliers():
# 10 points close to origin, 1 point far away
points = np.array([
[0.0, 0.0, 0.0],
[0.1, 0.0, 0.0],
[0.0, 0.1, 0.0],
[0.0, 0.0, 0.1],
[-0.1, 0.0, 0.0],
[0.0, -0.1, 0.0],
[0.0, 0.0, -0.1],
[0.05, 0.05, 0.05],
[-0.05, -0.05, -0.05],
[0.02, -0.02, 0.01],
[10.0, 10.0, 10.0] # Outlier
])
clean = GeometryUtils.clean_outliers(points, std_ratio=1.0)
# The outlier at [10.0, 10.0, 10.0] should be filtered out
assert len(clean) == 10
for pt in clean:
assert np.linalg.norm(pt) < 1.0
def test_compute_3d_bbox():
# Set of 3D points
points = np.array([
[-1.0, -2.0, -3.0],
[1.0, 2.0, 3.0],
[0.0, 0.0, 0.0]
])
bbox = GeometryUtils.compute_3d_bbox(points)
assert bbox is not None
np.testing.assert_allclose(bbox['min'], [-1.0, -2.0, -3.0])
np.testing.assert_allclose(bbox['max'], [1.0, 2.0, 3.0])
np.testing.assert_allclose(bbox['center'], [0.0, 0.0, 0.0])
np.testing.assert_allclose(bbox['size'], [2.0, 4.0, 6.0])
# Empty points check
assert GeometryUtils.compute_3d_bbox(np.empty((0, 3))) is None
def test_compute_3d_obb():
# Let's generate a rectangular box of points that is rotated by 30 degrees around Z axis.
theta = np.radians(30.0)
c, s = np.cos(theta), np.sin(theta)
R = np.array([
[c, -s, 0.0],
[s, c, 0.0],
[0.0, 0.0, 1.0]
])
# Base box of size (2, 4, 1) centered at (5, 6, 2)
# local coordinates of 8 corners:
local_corners = np.array([
[-1.0, -2.0, -0.5],
[1.0, -2.0, -0.5],
[1.0, 2.0, -0.5],
[-1.0, 2.0, -0.5],
[-1.0, -2.0, 0.5],
[1.0, -2.0, 0.5],
[1.0, 2.0, 0.5],
[-1.0, 2.0, 0.5]
])
global_corners = (R @ local_corners.T).T + np.array([5.0, 6.0, 2.0])
obb = GeometryUtils.compute_3d_obb(global_corners)
assert obb is not None
# The center should be close to [5, 6, 2]
np.testing.assert_allclose(obb['center'], [5.0, 6.0, 2.0], atol=1e-2)
# The size should be close to 2 and 4 (order can depend on cv2 minAreaRect orientation)
sorted_obb_size = sorted(obb['size'][:2])
sorted_expected_size = sorted([2.0, 4.0])
np.testing.assert_allclose(sorted_obb_size, sorted_expected_size, atol=1e-2)
np.testing.assert_allclose(obb['size'][2], 1.0, atol=1e-2)
# 8 corners check
assert len(obb['corners']) == 8
# Empty points check
assert GeometryUtils.compute_3d_obb(np.empty((0, 3))) is None
def test_transform_points_camera_to_map():
# Setup camera point along Z-axis (optical axis)
points_cam = np.array([
[0.0, 0.0, 1.0]
])
# Identity pose (zero translation, identity rotation q = [0, 0, 0, 1])
pose = Pose()
pose.position = Point(x=0.0, y=0.0, z=0.0)
pose.orientation = Quaternion(x=0.0, y=0.0, z=0.0, w=1.0)
transformed = GeometryUtils.transform_points_camera_to_map(points_cam, pose)
# Check shape
assert transformed.shape == (1, 3)
assert np.all(np.isfinite(transformed))
def test_transform_points():
# Setup test points
points = np.array([
[1.0, 2.0, 3.0]
])
# Test 1: Identity transformation
translation = [0.0, 0.0, 0.0]
rotation = [0.0, 0.0, 0.0, 1.0] # x,y,z,w
transformed = GeometryUtils.transform_points(points, translation, rotation)
np.testing.assert_allclose(transformed, points, atol=1e-5)
# Test 2: Simple translation only
translation = [10.0, -5.0, 2.5]
transformed = GeometryUtils.transform_points(points, translation, rotation)
np.testing.assert_allclose(transformed, [[11.0, -3.0, 5.5]], atol=1e-5)
# Test 3: Simple 90-degree rotation around Z-axis
# Yaw = 90 deg => q = [0, 0, sin(45), cos(45)] = [0, 0, 0.707107, 0.707107]
# Rotate [1, 2, 3] by 90 deg around Z:
# x' = x*cos(90) - y*sin(90) = 1*0 - 2*1 = -2
# y' = x*sin(90) + y*cos(90) = 1*1 + 2*0 = 1
# z' = z = 3
translation = [0.0, 0.0, 0.0]
rotation = [0.0, 0.0, np.sin(np.pi/4), np.cos(np.pi/4)]
transformed = GeometryUtils.transform_points(points, translation, rotation)
np.testing.assert_allclose(transformed, [[-2.0, 1.0, 3.0]], atol=1e-5)