diff --git a/src/machinevisiontoolbox/PointCloud.py b/src/machinevisiontoolbox/PointCloud.py index 89f197a7..4e6273ee 100644 --- a/src/machinevisiontoolbox/PointCloud.py +++ b/src/machinevisiontoolbox/PointCloud.py @@ -351,6 +351,7 @@ def disp( block: bool = False, file: str | None = None, title: str = "Point Cloud", + notebook: bool = False, **kwargs: Any, ) -> None: """ @@ -362,6 +363,11 @@ def disp( :type title: str, optional :param file: save display as an image, defaults to None :type file: str, optional + :param notebook: draw in the output of the notebook cell, rather than in an + Open3D window, defaults to False + :type notebook: bool, optional + :raises ValueError: ``file`` is given with ``notebook=True`` + :raises ImportError: ``notebook=True`` and Plotly is not installed :param front: set front vector :type front: array_like(3) :param lookat: set lookat vector @@ -375,8 +381,38 @@ def disp( Typing "h" in the display window will print help information to the console. + With ``notebook=True`` the point cloud is drawn in the output of the notebook + cell, as a Plotly figure that can be rotated and zoomed. This needs + `Plotly `_, which is not installed with the + Toolbox: ``pip install plotly``. ``block`` and ``file`` do not apply, and + besides the view options above, ``width``, ``height`` and + ``point_sample_factor`` (the fraction of the points drawn, 0 to 1, which + keeps a large cloud responsive) are passed to + :func:`open3d.visualization.draw_plotly`. + + .. versionchanged:: 2.5.0 + Added the ``notebook`` option. + :seealso: :class:`open3d.visualization.ViewControl` """ + if notebook: + if file is not None: + raise ValueError("file cannot be used with notebook=True") + try: + import plotly # noqa: F401 + except ImportError: + raise ImportError( + "disp(notebook=True) draws with Plotly, which is not installed. " + "Install it with: pip install plotly" + ) from None + + # get_plotly_fig rather than draw_plotly, which shows the figure and also + # returns it, so that a cell ending in the call shows it twice + fig = o3d.visualization.get_plotly_fig([self._pcd], **kwargs) + fig.update_layout(title=dict(text=title), margin=dict(t=40)) + fig.show() + return + if block: o3d.visualization.draw_geometries([self._pcd], window_name=title, **kwargs) else: diff --git a/tests/test_pointcloud.py b/tests/test_pointcloud.py index 5fef3b52..0679f7ab 100644 --- a/tests/test_pointcloud.py +++ b/tests/test_pointcloud.py @@ -53,6 +53,71 @@ def test_read(self): self.assertEqual(len(pc2), len(pc)) +try: + import plotly # noqa: F401 + + plotly_available = True +except ImportError: + plotly_available = False + + +@unittest.skipUnless(open3d_available, "open3d not installed") +class TestPointCloudNotebookDisplay(unittest.TestCase): + def setUp(self): + self.pc = PointCloud(np.random.rand(3, 500)) + + @unittest.skipUnless(plotly_available, "plotly not installed") + def test_shown_once_as_a_figure_with_points(self): + from unittest.mock import patch + + with patch("plotly.graph_objects.Figure.show") as show: + result = self.pc.disp(notebook=True, title="My cloud") + self.assertEqual(show.call_count, 1) + # nothing is returned, or a cell ending in the call would show it again + self.assertIsNone(result) + + @unittest.skipUnless(plotly_available, "plotly not installed") + def test_figure_has_the_points_and_title(self): + from unittest.mock import patch + + shown = [] + with patch( + "plotly.graph_objects.Figure.show", + autospec=True, + side_effect=lambda fig, *a, **k: shown.append(fig), + ): + self.pc.disp(notebook=True, title="My cloud", width=300, height=200) + fig = shown[0] + self.assertEqual(len(fig.data[0].x), 500) + self.assertEqual(fig.layout.title.text, "My cloud") + self.assertEqual((fig.layout.width, fig.layout.height), (300, 200)) + + @unittest.skipUnless(plotly_available, "plotly not installed") + def test_point_sample_factor_thins_the_cloud(self): + from unittest.mock import patch + + shown = [] + with patch( + "plotly.graph_objects.Figure.show", + autospec=True, + side_effect=lambda fig, *a, **k: shown.append(fig), + ): + self.pc.disp(notebook=True, point_sample_factor=0.2) + self.assertLess(len(shown[0].data[0].x), 500) + + def test_plotly_missing_says_how_to_install_it(self): + from unittest.mock import patch + + # a None entry in sys.modules makes the import raise ImportError + with patch.dict("sys.modules", {"plotly": None}): + with self.assertRaisesRegex(ImportError, "pip install plotly"): + self.pc.disp(notebook=True) + + def test_file_cannot_be_used(self): + with self.assertRaisesRegex(ValueError, "file"): + self.pc.disp(notebook=True, file="x.png") + + # ----------------------------------------------------------------------- # if __name__ == "__main__": unittest.main()