diff --git a/src/machinevisiontoolbox/PointCloud.py b/src/machinevisiontoolbox/PointCloud.py index c417b76c..3b8f5fd4 100644 --- a/src/machinevisiontoolbox/PointCloud.py +++ b/src/machinevisiontoolbox/PointCloud.py @@ -45,7 +45,13 @@ def _show_in_notebook( # 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)) + # the three axes have the same scale, so shapes have their true proportions as + # in the Open3D window. Plotly's default, "auto", does that unless one axis is + # more than four times longer than the other two, when it draws a cube instead + # and squashes a long or flat scene + fig.update_layout( + title=dict(text=title), margin=dict(t=40), scene_aspectmode="data" + ) fig.show() @@ -419,7 +425,8 @@ def disp( 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`. + :func:`open3d.visualization.draw_plotly`. The three axes have the same + scale, so shapes have their true proportions, as in the Open3D window. .. versionchanged:: 2.5.0 Added the ``notebook`` option. @@ -831,8 +838,9 @@ def disp( 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. + :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. .. versionchanged:: 2.5.0 Added the ``notebook`` option. diff --git a/tests/test_pointcloud.py b/tests/test_pointcloud.py index cf30d967..683e4eb0 100644 --- a/tests/test_pointcloud.py +++ b/tests/test_pointcloud.py @@ -76,6 +76,21 @@ def test_shown_once_as_a_figure_with_points(self): # 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_axes_have_the_same_scale(self): + from unittest.mock import patch + + # a long thin cloud, for which Plotly's default would draw a cube + pc = PointCloud(np.random.rand(3, 500) * np.array([[10.0], [0.5], [0.5]])) + shown = [] + with patch( + "plotly.graph_objects.Figure.show", + autospec=True, + side_effect=lambda fig, *a, **k: shown.append(fig), + ): + pc.disp(notebook=True) + self.assertEqual(shown[0].layout.scene.aspectmode, "data") + @unittest.skipUnless(plotly_available, "plotly not installed") def test_figure_has_the_points_and_title(self): from unittest.mock import patch @@ -161,6 +176,19 @@ def test_shown_once_with_a_mesh_of_the_voxels(self): self.assertEqual(len(shown[0].data), 1) self.assertEqual(len(shown[0].data[0].x), len(self.vg._cubes().vertices)) + @unittest.skipUnless(plotly_available, "plotly not installed") + def test_axes_have_the_same_scale(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.vg.disp(notebook=True) + self.assertEqual(shown[0].layout.scene.aspectmode, "data") + def test_plotly_missing_says_how_to_install_it(self): from unittest.mock import patch