Skip to content

Commit

Permalink
Add a class to handle Pinhole camera. (#280)
Browse files Browse the repository at this point in the history
This allows to easily handle transformations between 3D "real" space
and the "screen" space.
  • Loading branch information
yakutovicha committed Jan 25, 2022
1 parent b225f00 commit 3fb4821
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions aiidalab_widgets_base/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import wraps

import more_itertools as mit
import numpy as np
from ase.io import read


Expand Down Expand Up @@ -115,3 +116,20 @@ def next_i(change):
return inner

return f


class PinholeCamera:
def __init__(self, matrix):
self.matrix = np.reshape(matrix, (4, 4)).transpose()

def screen_to_vector(self, move_vector):
"""Converts vector from the screen coordinates to the normalized vector in 3D."""
move_vector[0] = -move_vector[0] # the x axis seem to be reverted in nglview.
res = np.append(np.array(move_vector), [0])
res = self.inverse_matrix.dot(res)
res /= np.linalg.norm(res)
return res[0:3]

@property
def inverse_matrix(self):
return np.linalg.inv(self.matrix)

0 comments on commit 3fb4821

Please sign in to comment.