workspace / tests /test_encoder /test_encoder.py
AntonioJun's picture
Replace tests with local workspace contents
e8055cf verified
Raw
History Blame Contribute Delete
2.65 kB
"""Tests for encoder/geometric.py's low-level geometry primitives (backprojection,
direction/turn classification, distance, centroid/extent math)."""
import numpy as np
import pytest
import geometric
def test_backproject_frame_applies_intrinsics_pose_and_confidence():
depth = np.array([[2.0, 2.0], [2.0, np.nan]], np.float32)
mask = np.ones((2, 2), bool)
intrinsics = np.eye(3, dtype=np.float32)
pose = np.eye(4, dtype=np.float32)
pose[0, 3] = 1.0
confidence = np.array([[0.9, 0.8], [0.1, 1.0]], np.float32)
points, kept_confidence = geometric.backproject_frame(
depth, intrinsics, pose, mask, confidence, conf_thr=0.5, return_conf=True
)
np.testing.assert_allclose(points, [[1.0, 0.0, 2.0], [3.0, 0.0, 2.0]])
np.testing.assert_allclose(kept_confidence, [0.9, 0.8])
def test_backproject_frame_returns_typed_empty_array():
points = geometric.backproject_frame(
np.zeros((2, 2), np.float32), np.eye(3), np.eye(4), np.ones((2, 2), bool)
)
assert points.shape == (0, 3)
assert points.dtype == np.float32
def test_relative_direction_modes():
origin = np.array([0.0, 0.0, 0.0])
forward = np.array([0.0, 1.0, 0.0])
front_left = np.array([-1.0, 1.0, 0.0])
up = np.array([0.0, 0.0, 1.0])
assert (
geometric.answer_rel_direction(origin, forward, front_left, up, 2)
== "front-left"
)
assert (
geometric.answer_rel_direction(origin, forward, front_left, up, 2, "medium")
== "left"
)
assert geometric.answer_rel_direction(origin, origin, front_left, up, 2) is None
def test_closest_distance_uses_point_cloud_distance():
first = [{"pts": np.array([[0.0, 0.0, 0.0]], np.float32), "n": 1}]
second = [{"pts": np.array([[0.0, 3.0, 4.0]], np.float32), "n": 1}]
assert geometric.answer_closest_distance(first, second) == pytest.approx(5.0)
def test_robust_centroid_extent_returns_sorted_dimensions():
points = np.array(
[[x, y, z] for x in (-2.0, 2.0) for y in (-1.0, 1.0) for z in (-0.5, 0.5)],
np.float32,
)
centroid, longest, dimensions = geometric.robust_centroid_extent(points, up_axis=2)
np.testing.assert_allclose(centroid, [0.0, 0.0, 0.0])
assert longest > 3.0
assert np.all(dimensions[:-1] >= dimensions[1:])
def test_depth_edges_handles_small_and_discontinuous_frames():
small = np.ones((5, 5), np.float32)
assert not geometric.depth_edges(small, np.ones_like(small, bool)).any()
depth = np.ones((20, 20), np.float32)
depth[:, 10:] = 10.0
edges = geometric.depth_edges(depth, np.ones_like(depth, bool))
assert edges[:, 9:11].any()