Skip to content
29 changes: 22 additions & 7 deletions src/machinevisiontoolbox/PointCloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

import difflib
import inspect
from pathlib import Path
from typing import Any, Callable
from warnings import warn
Expand Down Expand Up @@ -90,6 +91,15 @@ def _show_in_notebook(
"""
if file is not None:
raise ValueError("file cannot be used with notebook=True")
# Plotly draws a subset of the options of the Open3D window, so name an option
# that it does not accept, instead of leaving it to fail in get_plotly_fig
supported = set(inspect.signature(o3d.visualization.get_plotly_fig).parameters)
unsupported = sorted(set(kwargs) - supported)
if unsupported:
raise TypeError(
f"disp(notebook=True) does not support the option(s) {unsupported}; "
f"the options it accepts are {sorted(supported - {'geometry_list'})}"
)
try:
__import__("plotly")
except ImportError:
Expand Down Expand Up @@ -533,6 +543,7 @@ def disp(
:type notebook: bool, optional
:raises ValueError: ``file`` is given with ``notebook=True``
:raises ImportError: ``notebook=True`` and Plotly is not installed
:raises TypeError: ``notebook=True`` and an option that Plotly cannot draw
:param front: set front vector
:type front: array_like(3)
:param lookat: set lookat vector
Expand All @@ -549,12 +560,14 @@ def disp(
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 <https://plotly.com/python/>`_, 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`. The three axes have the same
scale, so shapes have their true proportions, as in the Open3D window.
Toolbox: ``pip install plotly``. ``block`` and ``file`` do not apply. The
view options above are passed on, along with any other option that
:func:`open3d.visualization.draw_plotly` accepts, such as ``width``,
``height`` and ``point_sample_factor`` (the fraction of the points drawn,
0 to 1, which keeps a large cloud responsive). The three axes have the same
scale, so shapes have their true proportions, as in the Open3D window. An
option of the Open3D window that Plotly cannot draw raises
:class:`TypeError`, which names it and lists the options that are accepted.

.. versionchanged:: 2.5.0
Added the ``notebook`` option.
Expand Down Expand Up @@ -1162,12 +1175,14 @@ def disp(
:type notebook: bool, optional
:raises ValueError: ``file`` is given with ``notebook=True``
:raises ImportError: ``notebook=True`` and Plotly is not installed
:raises TypeError: ``notebook=True`` and an option that Plotly cannot draw

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, and with the same
scale on the three axes. Each voxel is drawn as a cube of 12 triangles, so a
grid of many thousands of voxels is slow.
grid of many thousands of voxels is slow. Options of the Open3D window that
Plotly cannot draw raise :class:`TypeError`.

.. versionchanged:: 2.5.0
Added the ``notebook`` option.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,17 @@ def test_missing_wrapped_object_does_not_recurse(self):


# ----------------------------------------------------------------------- #
@unittest.skipUnless(open3d_available, "open3d not installed")
class TestDispNotebookOptions(unittest.TestCase):
"""disp(notebook=True) names an option that Plotly cannot draw"""

def test_unsupported_option_is_named(self):
pc = PointCloud(np.random.default_rng(0).random((3, 20)))
with self.assertRaises(TypeError) as cm:
pc.disp(notebook=True, point_show_normal=True)
self.assertIn("point_show_normal", str(cm.exception))
self.assertIn("notebook=True", str(cm.exception))


if __name__ == "__main__":
unittest.main()