diff --git a/src/machinevisiontoolbox/PointCloud.py b/src/machinevisiontoolbox/PointCloud.py index 4e6273ee..c417b76c 100644 --- a/src/machinevisiontoolbox/PointCloud.py +++ b/src/machinevisiontoolbox/PointCloud.py @@ -18,6 +18,37 @@ _open3d = False +def _show_in_notebook( + geometry: Any, title: str, file: str | None, kwargs: dict[str, Any] +) -> None: + """Draw an Open3D geometry in the output of the notebook cell, using Plotly + + :param geometry: an Open3D point cloud or triangle mesh + :param title: title of the figure + :param file: ``disp``'s ``file`` argument, which cannot be used here + :param kwargs: view options passed to + :func:`open3d.visualization.get_plotly_fig` + :raises ValueError: ``file`` is given + :raises ImportError: Plotly is not installed, it is not a dependency of the + Toolbox + """ + 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([geometry], **kwargs) + fig.update_layout(title=dict(text=title), margin=dict(t=40)) + fig.show() + + class PointCloud: def __init__(self, arg: Any, depth_scale: float = 1.0, **kwargs) -> None: """ @@ -396,21 +427,7 @@ def disp( :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() + _show_in_notebook(self._pcd, title, file, kwargs) return if block: @@ -741,6 +758,43 @@ def __init__(self, pcd: PointCloud, voxel_size: float) -> None: pcd._pcd, voxel_size=voxel_size ) + def _cubes(self) -> Any: + """The voxels as a triangle mesh, one cube of 12 triangles per voxel + + Open3D's Plotly drawing does not handle a voxel grid, it draws nothing. The + voxels keep their colors, or are gray if none has a color, so that the + lighting shows their shape. + """ + voxels = self._voxels.get_voxels() + index = np.array([v.grid_index for v in voxels], dtype=float).reshape(-1, 3) + colors = np.array([v.color for v in voxels], dtype=float).reshape(-1, 3) + if not np.any(colors): + colors = np.full_like(colors, 0.7) + n = len(voxels) + + # the unit cube: 8 corners, and 12 triangles which are 2 on each face + corners = np.array( + [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]] + + [[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]], + dtype=float, + ) + faces = np.array( + [[0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7]] + + [[0, 1, 5], [0, 5, 4], [3, 6, 2], [3, 7, 6]] + + [[0, 4, 7], [0, 7, 3], [1, 2, 6], [1, 6, 5]] + ) + + # the grid index is of the voxel's minimum corner, from the grid's origin + minimum = np.asarray(self._voxels.origin) + index * self._voxels.voxel_size + vertices = minimum[:, None, :] + corners[None, :, :] * self._voxels.voxel_size + triangles = faces[None, :, :] + 8 * np.arange(n)[:, None, None] + + mesh = o3d.geometry.TriangleMesh() + mesh.vertices = o3d.utility.Vector3dVector(vertices.reshape(-1, 3)) + mesh.triangles = o3d.utility.Vector3iVector(triangles.reshape(-1, 3)) + mesh.vertex_colors = o3d.utility.Vector3dVector(np.repeat(colors, 8, axis=0)) + return mesh + def write(self, filename: str) -> None: """ Write voxel grid to file @@ -757,6 +811,7 @@ def disp( block: bool = True, file: str | None = None, title: str = "Voxel Grid", + notebook: bool = False, **kwargs: Any, ) -> None: """ @@ -768,7 +823,26 @@ def disp( :type title: str, optional :param file: save display to this filename, 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 + + With ``notebook=True`` the voxels are drawn in the output of the notebook + cell, as a Plotly figure that can be rotated and zoomed, in the same way as + :meth:`PointCloud.disp`, which describes the options. Each voxel is drawn + as a cube of 12 triangles, so a grid of many thousands of voxels is slow. + + .. versionchanged:: 2.5.0 + Added the ``notebook`` option. + + :seealso: :meth:`PointCloud.disp` """ + if notebook: + _show_in_notebook(self._cubes(), title, file, kwargs) + return + if block: o3d.visualization.draw_geometries( [self._voxels], window_name=title, **kwargs diff --git a/tests/test_pointcloud.py b/tests/test_pointcloud.py index 0679f7ab..cf30d967 100644 --- a/tests/test_pointcloud.py +++ b/tests/test_pointcloud.py @@ -118,6 +118,61 @@ def test_file_cannot_be_used(self): self.pc.disp(notebook=True, file="x.png") +@unittest.skipUnless(open3d_available, "open3d not installed") +class TestVoxelGridNotebookDisplay(unittest.TestCase): + def setUp(self): + self.vg = PointCloud(np.random.rand(3, 2000)).voxel_grid(0.25) + + def test_one_cube_per_voxel_with_the_bounds_of_the_grid(self): + n = len(self.vg._voxels.get_voxels()) + mesh = self.vg._cubes() + self.assertEqual(len(mesh.triangles), 12 * n) + self.assertEqual(len(mesh.vertices), 8 * n) + nt.assert_allclose(mesh.get_min_bound(), self.vg._voxels.get_min_bound()) + nt.assert_allclose(mesh.get_max_bound(), self.vg._voxels.get_max_bound()) + + def test_cubes_are_gray_when_the_voxels_have_no_color(self): + colors = np.asarray(self.vg._cubes().vertex_colors) + nt.assert_allclose(colors, 0.7) + + def test_cubes_keep_the_colors_of_the_voxels(self): + import open3d as o3d + + pcd = o3d.geometry.PointCloud() + pcd.points = o3d.utility.Vector3dVector(np.random.rand(500, 3)) + pcd.colors = o3d.utility.Vector3dVector(np.tile([1.0, 0.0, 0.0], (500, 1))) + colors = np.asarray(PointCloud(pcd).voxel_grid(0.25)._cubes().vertex_colors) + nt.assert_allclose(colors, np.tile([1.0, 0.0, 0.0], (len(colors), 1))) + + @unittest.skipUnless(plotly_available, "plotly not installed") + def test_shown_once_with_a_mesh_of_the_voxels(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), + ): + result = self.vg.disp(notebook=True, title="Voxels") + self.assertIsNone(result) + self.assertEqual(len(shown), 1) + self.assertEqual(shown[0].layout.title.text, "Voxels") + self.assertEqual(len(shown[0].data), 1) + self.assertEqual(len(shown[0].data[0].x), len(self.vg._cubes().vertices)) + + def test_plotly_missing_says_how_to_install_it(self): + from unittest.mock import patch + + with patch.dict("sys.modules", {"plotly": None}): + with self.assertRaisesRegex(ImportError, "pip install plotly"): + self.vg.disp(notebook=True) + + def test_file_cannot_be_used(self): + with self.assertRaisesRegex(ValueError, "file"): + self.vg.disp(notebook=True, file="x.png") + + # ----------------------------------------------------------------------- # if __name__ == "__main__": unittest.main()