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
11 changes: 10 additions & 1 deletion src/machinevisiontoolbox/blocks/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,29 @@ def __init__(self, camera, P, frame="world", method="iterative", **blockargs):
:type blockargs: dict
:return: a ESTPOSE_P block
:rtype: EstPose_p instance
:raises ValueError: if ``camera`` is None or ``frame`` is not ``"world"`` or ``"camera"``

.. versionchanged:: 2.5.0
``frame`` is now passed to :meth:`~machinevisiontoolbox.Camera.CentralCamera.estpose`.
Previously it was accepted but ignored, so the pose was always given with
respect to the world frame.

"""
if camera is None:
raise ValueError("camera is not defined")
if frame not in ("world", "camera"):
raise ValueError(f"bad frame value {frame}, must be 'world' or 'camera'")

super().__init__(**blockargs)

self.camera = camera
self.P = P
self.frame = frame
self.method = method

def output(self, t, inports, x):
p = inports[0]
T = self.camera.estpose(self.P, p, method=self.method)
T = self.camera.estpose(self.P, p, method=self.method, frame=self.frame)
return [T]


Expand Down
47 changes: 47 additions & 0 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
"""Tests for the bdsim blocks defined in MVTB (``machinevisiontoolbox.blocks``).

bdsim is an optional dependency, so these tests are skipped when it is not installed.
Only blocks defined in this package are tested here, not bdsim's own blocks.
"""

import numpy as np
import numpy.testing as nt
import pytest
from spatialmath import SE3

pytest.importorskip("bdsim")

from machinevisiontoolbox import CentralCamera, mkcube # noqa: E402
from machinevisiontoolbox.blocks import EstPose_p # noqa: E402


class TestEstPoseFrame:
"""EstPose_p passes its ``frame`` argument on to ``CentralCamera.estpose``."""

def setup_method(self):
# a camera that is not at the world origin, so that the two frames differ
self.cam = CentralCamera.Default(pose=SE3.Trans(0.1, -0.2, -1.0))
self.P = mkcube(0.2)
self.T_world = SE3.Trans(0.1, 0.2, 0.5) * SE3.RPY(0.1, 0.2, 0.3)
self.p = self.cam.project_point(self.P, objpose=self.T_world)

def test_default_is_world(self):
block = EstPose_p(self.cam, self.P)
T = block.test_output(self.p)[0]
nt.assert_array_almost_equal(T.A, self.T_world.A, decimal=4)

def test_camera_frame(self):
block = EstPose_p(self.cam, self.P, frame="camera")
T = block.test_output(self.p)[0]
T_camera = self.cam.pose.inv() * self.T_world
nt.assert_array_almost_equal(T.A, T_camera.A, decimal=4)

def test_frames_differ(self):
T_w = EstPose_p(self.cam, self.P, frame="world").test_output(self.p)[0]
T_c = EstPose_p(self.cam, self.P, frame="camera").test_output(self.p)[0]
assert not np.allclose(T_w.A, T_c.A)

def test_bad_frame_raises(self):
with pytest.raises(ValueError):
EstPose_p(self.cam, self.P, frame="robot")
Loading