π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
implement convert to/from euclidean/homogeneous + tests
edgarriba48133f287cbb
diff --git a/.gitignore b/.gitignoreindex 2abd33a85..af4efcdde 100644--- a/.gitignore+++ b/.gitignore@@ -1,8 +1,9 @@build/dist/-torchvision.egg-info/+torchgeometry.egg-info/*/**/__pycache__*/**/*.pyc*/**/*~*~+*.swpdocs/builddiff --git a/test/test_functional.py b/test/test_functional.pynew file mode 100644index 000000000..30892ebaf--- /dev/null+++ b/test/test_functional.py@@ -0,0 +1,24 @@+import unittest++import torch+import torchgeometry as dgm+++class Tester(unittest.TestCase):++ def test_convert_points_to_homogeneous(self):+ points = torch.rand(1, 2, 3)++ points_h = dgm.convert_points_to_homogeneous(points)+ assert (points_h[..., -1] == torch.ones(1, 2, 1)).all()++ def test_convert_points_from_homogeneous(self):+ points_h = torch.rand(1, 2, 3)+ points_h[..., -1] = 1.0++ points = dgm.convert_points_from_homogeneous(points_h)+ assert (points_h[..., :2] == points[..., :2]).all()+++if __name__ == '__main__':+ unittest.main()diff --git a/torchgeometry/__init__.py b/torchgeometry/__init__.pyindex b8023d8bc..4a1db3585 100644--- a/torchgeometry/__init__.py+++ b/torchgeometry/__init__.py@@ -1 +1,3 @@-__version__ = '0.0.1'+__version__ = '0.0.1' # the current version of the lib++from .functional import *diff --git a/torchgeometry/functional.py b/torchgeometry/functional.pynew file mode 100644index 000000000..8f5eef633--- /dev/null+++ b/torchgeometry/functional.py@@ -0,0 +1,41 @@+import torch+++def convert_points_from_homogeneous(points, eps=1e-6):+ """Converts points from homogeneous to Euclidean space.++ Args:+ points (Tensor): tensor of N-dimensional points of size (B, D, N).++ Returns:+ Tensor: tensor of N-1-dimensional points of size (B, D, N-1).+ """+ if not torch.is_tensor(points):+ raise TypeError("Input type is not a torch.Tensor. Got {}"+ .format(type(points)))++ if len(points.shape) != 3:+ raise ValueError("Input size must be a three dimensional tensor. Got {}"+ .format(points.shape))++ return points[..., :-1] / (points[..., -1:] + eps)+++def convert_points_to_homogeneous(points):+ """Converts points from Euclidean to homogeneous space.++ Args:+ points (Tensor): tensor of N-dimensional points of size (B, D, N).++ Returns:+ Tensor: tensor of N+1-dimensional points of size (B, D, N+1).+ """+ if not torch.is_tensor(points):+ raise TypeError("Input ype is not a torch.Tensor. Got {}"+ .format(type(points)))++ if len(points.shape) != 3:+ raise ValueError("Input size must be a three dimensional tensor. Got {}"+ .format(points.shape))++ return torch.cat([points, torch.ones_like(points)[..., :1]], dim=-1)