Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix save_pointcloud_ply fn counting point with inf coordinates (#1263)

Abdelrhman-Hosny33dd828036a2
diff --git a/kornia/utils/pointcloud_io.py b/kornia/utils/pointcloud_io.py
index 8a4af8258..1875ab8e3 100644
--- a/kornia/utils/pointcloud_io.py
+++ b/kornia/utils/pointcloud_io.py
@@ -31,6 +31,7 @@ def save_pointcloud_ply(filename: str, pointcloud: torch.Tensor) -> None:
for idx in range(num_points):
xyz = xyz_vec[idx]
if not bool(torch.isfinite(xyz).any()):
+ num_points -= 1
continue
x: float = xyz[0].item()
y: float = xyz[1].item()
diff --git a/test/utils/test_pointcloud_io.py b/test/utils/test_pointcloud_io.py
index e77e963f3..e3deabbaa 100644
--- a/test/utils/test_pointcloud_io.py
+++ b/test/utils/test_pointcloud_io.py
@@ -22,3 +22,31 @@ class TestSaveLoadPointCloud:
# remove the temporal file
if os.path.exists(filename):
os.remove(filename)
+
+ @staticmethod
+ def test_inf_coordinates_save_pointcloud():
+ # create the tensor to save
+ height, width = 10, 8
+ xyz_save = torch.rand(height, width, 3)
+
+ # add nans or infinite values
+ # point data (inf, inf, inf) skipped
+ xyz_save[0, 0, :] = float('inf')
+ # point data (inf, number, number) counted
+ xyz_save[0, 1, 0] = float('inf')
+ # point data (inf, inf, number) counted
+ xyz_save[1, 0, :-1] = float('inf')
+
+ # save to file
+ filename = "pointcloud.ply"
+ kornia.save_pointcloud_ply(filename, xyz_save)
+
+ # same as xyz_save but with dropping the first point
+ xyz_correct = xyz_save.reshape(-1, 3)[1:, :]
+
+ # load file
+ xyz_load = kornia.load_pointcloud_ply(filename)
+ assert_close(xyz_correct, xyz_load)
+
+ if os.path.exists(filename):
+ os.remove(filename)