import os
from typing import Optional

import torch


def save_pointcloud_ply(filename: str, pointcloud: torch.Tensor) -> None:
    r"""Utility function to save to disk a pointcloud in PLY format.

    Args:
        filename: the path to save the pointcloud.
        pointcloud: tensor containing the pointcloud to save.
          The tensor must be in the shape of :math:`(*, 3)` where the last
          component is assumed to be a 3d point coordinate :math:`(X, Y, Z)`.
    """
    if not isinstance(filename, str) and filename[-3:] == '.ply':
        raise TypeError("Input filename must be a string in with the .ply  " "extension. Got {}".format(filename))

    if not torch.is_tensor(pointcloud):
        raise TypeError(f"Input pointcloud type is not a torch.Tensor. Got {type(pointcloud)}")

    if not len(pointcloud.shape) == 3 and pointcloud.shape[-1] == 3:
        raise TypeError("Input pointcloud must be in the following shape " "HxWx3. Got {}.".format(pointcloud.shape))

    # flatten the input pointcloud in a vector to iterate points
    xyz_vec: torch.Tensor = pointcloud.reshape(-1, 3)

    with open(filename, 'w') as f:
        data_str: str = ''
        num_points: int = xyz_vec.shape[0]
        for idx in range(num_points):
            xyz = xyz_vec[idx]
            if not bool(torch.isfinite(xyz).any()):
                continue
            x: float = xyz[0].item()
            y: float = xyz[1].item()
            z: float = xyz[2].item()
            data_str += f'{x} {y} {z}\n'

        f.write("ply\n")
        f.write("format ascii 1.0\n")
        f.write("comment arraiy generated\n")
        f.write("element vertex %d\n" % num_points)
        f.write("property double x\n")
        f.write("property double y\n")
        f.write("property double z\n")
        f.write("end_header\n")
        f.write(data_str)


def load_pointcloud_ply(filename: str, header_size: int = 8) -> torch.Tensor:
    r"""Utility function to load from disk a pointcloud in PLY format.

    Args:
        filename: the path to the pointcloud.
        header_size: the size of the ply file header that will
          be skipped during loading.

    Return:
        tensor containing the loaded point with shape :math:`(*, 3)` where
        :math:`*` represents the number of points.
    """
    if not isinstance(filename, str) and filename[-3:] == '.ply':
        raise TypeError("Input filename must be a string in with the .ply  " "extension. Got {}".format(filename))
    if not os.path.isfile(filename):
        raise ValueError("Input filename is not an existing file.")
    if not (isinstance(header_size, int) and header_size > 0):
        raise TypeError(f"Input header_size must be a positive integer. Got {header_size}.")
    # open the file and populate tensor
    with open(filename) as f:
        points = []

        # skip header
        lines = f.readlines()[header_size:]

        # iterate over the points
        for line in lines:
            x_str, y_str, z_str = line.split()
            points.append((torch.tensor(float(x_str)), torch.tensor(float(y_str)), torch.tensor(float(z_str))))

        # create tensor from list
        pointcloud: torch.Tensor = torch.tensor(points)
        return pointcloud

kornia/utils/pointcloud_io.py
import os
from typing import Optional

import torch


def save_pointcloud_ply(filename: str, pointcloud: torch.Tensor) -> None:
    r"""Utility function to save to disk a pointcloud in PLY format.

    Args:
        filename: the path to save the pointcloud.
        pointcloud: tensor containing the pointcloud to save.
          The tensor must be in the shape of :math:`(*, 3)` where the last
          component is assumed to be a 3d point coordinate :math:`(X, Y, Z)`.
    """
    if not isinstance(filename, str) and filename[-3:] == '.ply':
        raise TypeError("Input filename must be a string in with the .ply  " "extension. Got {}".format(filename))

    if not torch.is_tensor(pointcloud):
        raise TypeError(f"Input pointcloud type is not a torch.Tensor. Got {type(pointcloud)}")

    if not len(pointcloud.shape) == 3 and pointcloud.shape[-1] == 3:
        raise TypeError("Input pointcloud must be in the following shape " "HxWx3. Got {}.".format(pointcloud.shape))

    # flatten the input pointcloud in a vector to iterate points
    xyz_vec: torch.Tensor = pointcloud.reshape(-1, 3)

    with open(filename, 'w') as f:
        data_str: str = ''
        num_points: int = xyz_vec.shape[0]
        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()
            z: float = xyz[2].item()
            data_str += f'{x} {y} {z}\n'

        f.write("ply\n")
        f.write("format ascii 1.0\n")
        f.write("comment arraiy generated\n")
        f.write("element vertex %d\n" % num_points)
        f.write("property double x\n")
        f.write("property double y\n")
        f.write("property double z\n")
        f.write("end_header\n")
        f.write(data_str)


def load_pointcloud_ply(filename: str, header_size: int = 8) -> torch.Tensor:
    r"""Utility function to load from disk a pointcloud in PLY format.

    Args:
        filename: the path to the pointcloud.
        header_size: the size of the ply file header that will
          be skipped during loading.

    Return:
        tensor containing the loaded point with shape :math:`(*, 3)` where
        :math:`*` represents the number of points.
    """
    if not isinstance(filename, str) and filename[-3:] == '.ply':
        raise TypeError("Input filename must be a string in with the .ply  " "extension. Got {}".format(filename))
    if not os.path.isfile(filename):
        raise ValueError("Input filename is not an existing file.")
    if not (isinstance(header_size, int) and header_size > 0):
        raise TypeError(f"Input header_size must be a positive integer. Got {header_size}.")
    # open the file and populate tensor
    with open(filename) as f:
        points = []

        # skip header
        lines = f.readlines()[header_size:]

        # iterate over the points
        for line in lines:
            x_str, y_str, z_str = line.split()
            points.append((torch.tensor(float(x_str)), torch.tensor(float(y_str)), torch.tensor(float(z_str))))

        # create tensor from list
        pointcloud: torch.Tensor = torch.tensor(points)
        return pointcloud

kornia/utils/pointcloud_io.py
diff --git a/kornia/utils/pointcloud_io.py b/kornia/utils/pointcloud_io.py
index 8a4af82588..1875ab8e3e 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 e77e963f39..e3deabbaa9 100644
--- a/test/utils/test_pointcloud_io.py
+++ b/test/utils/test_pointcloud_io.py
@@ -22,3 +22,31 @@ def test_save_pointcloud(self):
         # 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)

[Bug] save pointcloud not updates num_points when inf
## 🐛 Bug

The function `K.utils.save_pointcloud_ply` doesn't update the final number of points to be serialized when one of the values contain an infinite value.

How to  fix:

update this line https://github.com/kornia/kornia/blob/master/kornia/utils/pointcloud_io.py#L34

```python
            if not bool(torch.isfinite(xyz).any()):
                continue
```
by

```python
            if not bool(torch.isfinite(xyz).any()):
                num_points -= 1
                continue
```
