π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): write ASCII PLY coordinates with full float64 precision (#5048)
niukanen1fdacaabb2e51
diff --git a/changelog.d/5048.fixed.md b/changelog.d/5048.fixed.mdnew file mode 100644index 000000000..bbcf864b1--- /dev/null+++ b/changelog.d/5048.fixed.md@@ -0,0 +1 @@+`save_pointcloud_ply` (ASCII) writes every float64 coordinate with its shortest round-trip representation instead of 9 significant digits, so `double` properties no longer lose precision past the ninth digit (UTM-scale metres were off by millimetres).diff --git a/kornia/geometry/pointcloud.py b/kornia/geometry/pointcloud.pyindex 1b30b4a41..96b6c9b75 100644--- a/kornia/geometry/pointcloud.py+++ b/kornia/geometry/pointcloud.py@@ -75,9 +75,11 @@ def save_pointcloud_ply(filename: str, pointcloud: torch.Tensor) -> None:if num_points > 0:# Move to CPU, convert to float64 for matching 'double' in headerarr = xyz.detach().cpu().to(torch.float64)- # Write each row as space-separated floats+ # Write each row as space-separated floats. repr of a Python float is the+ # shortest string that round-trips it exactly, which a 'double' property+ # needs; a fixed significant-digit count silently loses float64 precision.for x, y, z in arr.tolist():- f.write(f"{x:.9g} {y:.9g} {z:.9g}\n")+ f.write(f"{x!r} {y!r} {z!r}\n")def save_pointcloud_ply_binary(filename: str, pointcloud: torch.Tensor) -> None:diff --git a/tests/geometry/test_pointcloud_io.py b/tests/geometry/test_pointcloud_io.pyindex 918e2d934..f3fddbca6 100644--- a/tests/geometry/test_pointcloud_io.py+++ b/tests/geometry/test_pointcloud_io.py@@ -58,6 +58,29 @@ class TestSaveLoadPointCloud(BaseTester):assert xyz_load.shape == (height * width, 3)self.assert_close(xyz_load, xyz_save.reshape(-1, 3))+ def test_ascii_writer_round_trips_float64(self, tmp_path):+ # The ASCII writer declares `property double` and converts to float64, so a+ # written value must read back exactly: a fixed significant-digit format+ # loses the digits past the ninth (UTM-scale metres off by millimetres),+ # while the binary writer stores the same values bit-exactly.+ pts = torch.tensor(+ [[0.1234567890123456, -4.5, 1e-7], [5123456.789012, 612345.678901, 123.456789012]],+ dtype=torch.float64,+ )++ filename = str(tmp_path / "pointcloud_f64.ply")+ kornia.geometry.save_pointcloud_ply(filename, pts)++ xyz_load = kornia.geometry.load_pointcloud_ply(filename)+ assert xyz_load.dtype == torch.float32, "the loader returns float32"+ # The loader downcasts to float32, so compare through the written text: every+ # float64 value must survive the write as its exact shortest round-trip form.+ with open(filename, encoding="utf-8") as f:+ body = f.read()+ assert body.splitlines()[0] == "ply"+ for value in (0.1234567890123456, 5123456.789012, 612345.678901):+ assert repr(value) in body+def test_invalid_filename_type(self):xyz_save = torch.rand(10, 3)with pytest.raises(TypeError):