Skip to content

Commit

Permalink
implement convert to/from euclidean/homogeneous + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarriba committed Aug 22, 2018
1 parent fe8e4df commit f13c462
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
build/
dist/
torchvision.egg-info/
torchgeometry.egg-info/
*/**/__pycache__
*/**/*.pyc
*/**/*~
*~
*.swp
docs/build
24 changes: 24 additions & 0 deletions test/test_functional.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 3 additions & 1 deletion torchgeometry/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
__version__ = '0.0.1'
__version__ = '0.0.1' # the current version of the lib

from .functional import *
41 changes: 41 additions & 0 deletions torchgeometry/functional.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f13c462

Please sign in to comment.