From 40731ef54e181c727ed2a55b99070d39a121456d Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Wed, 25 Nov 2026 07:03:52 +1000 Subject: [PATCH] fix: PointCloud.normals(radius=..., max_nn=...) still works, with a deprecation warning Up to MVTB release 0.9, normals(...) was the method that estimated the normals, and the printed book uses it (chapter 14). In MVTB v1.0.1 it became a property that returns the normals and the method became estimate_normals, which are the names Open3D uses, so the book code raised TypeError. The array that the property returns can now be called, which estimates the normals as before and warns that estimate_normals is the name to use. What the property returns is unchanged. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/PointCloud.py | 43 +++++++++++++++++++++++++- tests/test_pointcloud.py | 36 +++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/machinevisiontoolbox/PointCloud.py b/src/machinevisiontoolbox/PointCloud.py index 4e6273ee..ae66a92b 100644 --- a/src/machinevisiontoolbox/PointCloud.py +++ b/src/machinevisiontoolbox/PointCloud.py @@ -18,6 +18,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) + + class PointCloud: def __init__(self, arg: Any, depth_scale: float = 1.0, **kwargs) -> None: """ @@ -593,9 +628,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": """ diff --git a/tests/test_pointcloud.py b/tests/test_pointcloud.py index 0679f7ab..5342985a 100644 --- a/tests/test_pointcloud.py +++ b/tests/test_pointcloud.py @@ -118,6 +118,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) + + # ----------------------------------------------------------------------- # if __name__ == "__main__": unittest.main()