Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/multicam_sim/cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,15 @@ def centre(self) -> FloatArray:
"""Camera centre ``C = -R^T @ t`` in world coordinates."""
return camera_centre(self.rotation(), self.translation())

def forward(self) -> FloatArray:
"""World-space unit viewing direction.

``R``'s rows are the camera axes in world coordinates in the order
``[right, down, forward]`` (OpenCV, +z forward), so the forward axis is
the third row — already unit length for an orthonormal ``R``.
"""
return np.asarray(self.rotation()[2], dtype=np.float64)

def projection_matrix(self) -> FloatArray:
"""The ``3x4`` matrix ``P = K [R | t]``."""
return projection_matrix(self.intrinsics.matrix(), self.rotation(), self.translation())
Expand Down
17 changes: 17 additions & 0 deletions tests/test_cameras.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,20 @@ def test_intrinsics_rejects_non_positive_size(width: int, height: int) -> None:
def test_intrinsics_direct_construction_still_works() -> None:
intr = Intrinsics.from_focal(100.0, 640, 480)
assert (intr.width, intr.height) == (640, 480)


def test_camera_forward_is_unit_length() -> None:
intr = Intrinsics.from_focal(100.0, 640, 480)
eye = np.array([1.0, 2.0, 3.0])
target = np.array([4.0, 0.0, 1.0])
cam = Camera.look_at(0, intr, eye, target)
assert np.linalg.norm(cam.forward()) == pytest.approx(1.0)


def test_camera_forward_points_from_eye_to_target() -> None:
intr = Intrinsics.from_focal(100.0, 640, 480)
eye = np.array([1.0, 2.0, 3.0])
target = np.array([4.0, 0.0, 1.0])
cam = Camera.look_at(0, intr, eye, target)
expected = (target - eye) / np.linalg.norm(target - eye)
np.testing.assert_allclose(cam.forward(), expected, atol=1e-9)
Loading