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
43 changes: 42 additions & 1 deletion src/machinevisiontoolbox/PointCloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@
_open3d = False


class _Normals(np.ndarray):
"""The point normals, which can also be called as the old ``normals()`` method

Up to MVTB release 0.9 ``PointCloud.normals(radius=..., max_nn=...)`` was the
method that estimated the normals, and the printed book uses it. Since MVTB
v1.0.1 it is the property :attr:`PointCloud.normals`, and the method is
:meth:`PointCloud.estimate_normals`, which are the names that Open3D uses for
the same things (the array is 3xN, the MVTB convention, where Open3D's is Nx3).
This array subclass lets the old call still work, with a deprecation warning,
without changing what the property returns.
"""

_pcd: "PointCloud | None" = None

def __new__(cls, array: np.ndarray, pcd: "PointCloud") -> "_Normals":
obj = np.asarray(array).view(cls)
obj._pcd = pcd
return obj

def __array_finalize__(self, obj: Any) -> None:
self._pcd = getattr(obj, "_pcd", None)

def __call__(self, **kwargs: Any) -> None:
warn(
"Deprecated in MVTB 2.5.0: normals(...) estimates the normals, use "
"estimate_normals(...) instead, as in Open3D. normals is now a property "
"that returns them.",
DeprecationWarning,
stacklevel=2,
)
if self._pcd is None:
raise TypeError("'numpy.ndarray' object is not callable")
self._pcd.estimate_normals(**kwargs)


def _show_in_notebook(
geometry: Any, title: str, file: str | None, kwargs: dict[str, Any]
) -> None:
Expand Down Expand Up @@ -799,9 +834,15 @@ def normals(self) -> np.ndarray:
Point normals are returned as columns of the array. The columns correspond
to the same columns in the :attr:`points` and :attr:`colors` properties.

.. deprecated:: 2.5.0
Calling the result, ``pcd.normals(radius=0.1, max_nn=30)``, estimates the
normals as the method of this name did up to MVTB release 0.9. It still
works, with a warning, use :meth:`estimate_normals` instead. Since MVTB
v1.0.1 these are the names that Open3D uses.

.. seealso:: :meth:`estimate_normals` :attr:`points` :attr:`colors`
"""
return np.asarray(self._pcd.normals).T
return _Normals(np.asarray(self._pcd.normals).T, self)

def remove_outlier(self, **kwargs: Any) -> "PointCloud":
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,42 @@ def test_file_cannot_be_used(self):
self.pc.disp(notebook=True, file="x.png")


@unittest.skipUnless(open3d_available, "open3d not installed")
class TestNormals(unittest.TestCase):
"""``normals`` is a property, and also still the old method of that name"""

def setUp(self):
self.pc = PointCloud.Read("bunny.ply")

def test_property_is_an_array_of_columns(self):
self.assertEqual(self.pc.normals.shape, (3, 0)) # none yet
self.pc.estimate_normals(radius=0.1, max_nn=30)
n = self.pc.normals
self.assertEqual(n.shape, (3, len(self.pc)))
nt.assert_allclose(np.linalg.norm(n, axis=0), 1)
self.assertIsInstance(n, np.ndarray)

def test_old_call_estimates_the_normals_and_warns(self):
# the printed book has pcd.normals(radius=0.1, max_nn=30)
with self.assertWarnsRegex(DeprecationWarning, "estimate_normals") as w:
result = self.pc.normals(radius=0.1, max_nn=30)
self.assertIsNone(result)
# the warning is reported at the caller's line, not inside the Toolbox
self.assertEqual(w.filename, __file__)

other = PointCloud.Read("bunny.ply")
other.estimate_normals(radius=0.1, max_nn=30)
nt.assert_allclose(self.pc.normals, other.normals)

def test_array_behaves_as_an_array(self):
self.pc.estimate_normals(radius=0.1, max_nn=30)
n = self.pc.normals
self.assertEqual(n.T.shape, (len(self.pc), 3))
self.assertEqual(n[:, 0].shape, (3,))
nt.assert_allclose(np.sum(n * n, axis=0), 1)
self.assertEqual(type(np.asarray(n)), np.ndarray)


@unittest.skipUnless(open3d_available, "open3d not installed")
class TestVoxelGridNotebookDisplay(unittest.TestCase):
def setUp(self):
Expand Down
Loading