-
-
Notifications
You must be signed in to change notification settings - Fork 947
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement convert to/from euclidean/homogeneous + tests
- Loading branch information
Showing
4 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |