Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/machinevisiontoolbox/PointCloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Thin wrapper around Open3D point-cloud objects with MVTB look and feel.
"""

from pathlib import Path
from warnings import warn
from typing import Any, Callable

Expand Down Expand Up @@ -244,10 +245,22 @@ def Read(cls, filename: str, *args: Any, **kwargs: Any) -> "PointCloud":
the ``data`` folder of the
`mvtb_data package <https://github.com/petercorke/machinevision-toolbox-python/tree/master/packages/mvtb-data>`_.

.. note:: ``filename`` may be given as a bare name (``'bunny.ply'``)
or with a redundant leading ``data/`` (``'data/bunny.ply'``, the
older convention some book examples still use) -- both resolve
to the same file.

"""

from machinevisiontoolbox import mvtb_path_to_datafile

# accept a caller-supplied redundant "data/" prefix (older
# convention) without double-joining it against the "data" this
# method already prepends
parts = Path(filename).parts
if parts and parts[0] == "data":
filename = str(Path(*parts[1:])) if len(parts) > 1 else ""

filename = mvtb_path_to_datafile("data", filename, string=True)
pcd = o3d.io.read_point_cloud(filename, *args, **kwargs)
return cls(pcd)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ def test_constructor(self):
rgbd = Image.Pstack((d, rgb.astype("uint16")), colororder="DRGB")
pc = PointCloud.DepthImage(rgbd, camera)

def test_read(self):
# bare filename -- the documented convention
pc = PointCloud.Read("bunny.ply")
self.assertIsInstance(pc, PointCloud)
self.assertEqual(len(pc), 35947)

# a caller-supplied redundant "data/" prefix (the older convention
# some book examples still use) must resolve to the same file,
# not double-join to data/data/bunny.ply
pc2 = PointCloud.Read("data/bunny.ply")
self.assertEqual(len(pc2), len(pc))


# ----------------------------------------------------------------------- #
if __name__ == "__main__":
Expand Down