From e2e87dbd38f3afdab71ffb35ceb9121b8e4fdf9d Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Mon, 23 Nov 2026 19:07:23 +1000 Subject: [PATCH] fix: raise when the pose estimate of ESTPOSE_P fails The block output None when CentralCamera.estpose found no solution, and the next block in the diagram received None as its input. It now raises RuntimeError, which bdsim reports with the block, the time and the input. The estpose :return: text also now says it can be None. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/Camera.py | 5 ++- src/machinevisiontoolbox/blocks/camera.py | 13 ++++++ tests/test_blocks.py | 53 +++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/machinevisiontoolbox/Camera.py b/src/machinevisiontoolbox/Camera.py index a3b911c5..9881c9d5 100644 --- a/src/machinevisiontoolbox/Camera.py +++ b/src/machinevisiontoolbox/Camera.py @@ -3568,8 +3568,9 @@ def estpose( :type method: str, optional :param frame: estimate pose with respect to frame "world" [default] or "camera" :type frame: str, optional - :return: pose of target frame relative to the world frame - :rtype: :class:`~spatialmath.pose3d.SE3` + :return: pose of target frame relative to the world frame, or ``None`` if + the algorithm finds no solution + :rtype: :class:`~spatialmath.pose3d.SE3` or None Using a set of points defining some object with respect to its own frame {B}, and a set of image-plane projections, estimate the pose of {B} with respect to the world diff --git a/src/machinevisiontoolbox/blocks/camera.py b/src/machinevisiontoolbox/blocks/camera.py index f0c93c53..75a5055e 100644 --- a/src/machinevisiontoolbox/blocks/camera.py +++ b/src/machinevisiontoolbox/blocks/camera.py @@ -167,12 +167,18 @@ def __init__( :return: a ESTPOSE_P block :rtype: EstPose_p instance :raises ValueError: if ``camera`` is None or ``frame`` is not ``"world"`` or ``"camera"`` + :raises RuntimeError: when the diagram is evaluated, if the pose estimation finds + no solution for the points. bdsim prints the block, the time and the input, + and stops the run with a "Fatal failure" error caused by this one. .. 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. + .. versionchanged:: 2.5.0 + A pose estimate that fails raises :class:`RuntimeError`. It used to + output ``None``, which the next block received as its input. """ if camera is None: raise ValueError("camera is not defined") @@ -189,6 +195,13 @@ def __init__( def output(self, t: float, inports: list[Any], x: np.ndarray) -> list[Any]: p = inports[0] T = self.camera.estpose(self.P, p, method=self.method, frame=self.frame) + if T is None: + # bdsim prints the block, the time and the input, then stops the run + # with a "Fatal failure" error caused by this one + raise RuntimeError( + f"pose estimation failed: method {self.method!r} found no pose " + f"for {p.shape[1]} points" + ) return [T] diff --git a/tests/test_blocks.py b/tests/test_blocks.py index dc1c67a5..7f584d89 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -287,3 +287,56 @@ def test_frames_differ(self): def test_bad_frame_raises(self): with pytest.raises(ValueError): EstPose_p(self.cam, self.P, frame="robot") + + +class TestEstPoseFailure: + """EstPose_p raises when the pose estimate fails, and does not output None.""" + + def setup_method(self): + self.cam = CentralCamera.Default() + self.P = mkcube(0.2) + # every image point at one position: IPPE finds no pose for this + p = self.cam.project_point(self.P, objpose=SE3.Trans(0.1, 0.2, 0.5)) + self.p_degenerate = np.tile(p[:, :1], (1, self.P.shape[1])) + self.p = p + + def test_the_camera_gives_none_for_the_degenerate_points(self): + # confirms that the points used below are a failure of the estimate + assert self.cam.estpose(self.P, self.p_degenerate, method="ippe") is None + + def test_failure_raises_not_none(self): + block = EstPose_p(self.cam, self.P, method="ippe") + with pytest.raises(RuntimeError, match="pose estimation failed"): + block.test_output(self.p_degenerate) + + def test_message_names_the_method_and_the_point_count(self): + block = EstPose_p(self.cam, self.P, method="ippe") + with pytest.raises(RuntimeError, match=r"'ippe'.*8 points"): + block.test_output(self.p_degenerate) + + def test_success_is_unchanged(self): + T = EstPose_p(self.cam, self.P).test_output(self.p)[0] + assert T is not None + + def test_in_a_simulation_bdsim_reports_the_block(self, capsys): + sim = bdsim.BDSim( + animation=False, + graphics=False, + hold=False, + progress=False, + verbose=False, + sysargs=False, + ) + bd = sim.blockdiagram() + points = bd.CONSTANT(self.p_degenerate) + estpose = bd.ESTPOSE_P(self.cam, self.P, method="ippe") + bd.connect(points, estpose) + # bdsim evaluates the diagram once while it compiles, so the failure is + # raised there, before any simulation steps + with pytest.raises(RuntimeError) as raised: + bd.compile() + # bdsim reports "Fatal failure", caused by our error, and prints which + # block and time it came from + assert "pose estimation failed" in str(raised.value.__cause__) + shown = capsys.readouterr() + assert "estpose_p.0" in shown.out + shown.err