Skip to content

Commit

Permalink
integrate orthographic camer depth fix from mmatl#40
Browse files Browse the repository at this point in the history
  • Loading branch information
maxbarnikol committed Dec 17, 2024
1 parent a59963e commit b2ac547
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
51 changes: 51 additions & 0 deletions pyrender/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ def get_projection_matrix(self, width=None, height=None):
"""
pass

@abc.abstractmethod
def inverse_transform_depth_ndc(self, depth_ndc):
"""
Unproject the depth in Normalized Device Coordinates
to metric values in eye (camera) reference frame.
:param
depth_ndc: np.array np.float32
depth in [-1, 1] in normalized device coordinates
:return:
depth: np.array np.float32
depth in metric units in eye (camera) reference frame.
"""
pass


class PerspectiveCamera(Camera):

Expand Down Expand Up @@ -205,6 +219,19 @@ def get_projection_matrix(self, width=None, height=None):

return P

def inverse_transform_depth_ndc(self, depth_ndc):
inf_inds = (depth_ndc == 1)
noninf = np.logical_not(inf_inds)
depth_img = depth_ndc
if self.zfar is None:
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
else:
depth_img[noninf] = ((2.0 * self.znear * self.zfar) /
(self.zfar + self.znear - depth_img[noninf] *
(self.zfar - self.znear)))
depth_img[inf_inds] = 0.0
return depth_img


class OrthographicCamera(Camera):
"""An orthographic camera for orthographic projection.
Expand Down Expand Up @@ -309,6 +336,17 @@ def get_projection_matrix(self, width=None, height=None):
P[3][3] = 1.0
return P

def inverse_transform_depth_ndc(self, depth_ndc):
inf_inds = (depth_ndc == 1)
noninf = np.logical_not(inf_inds)
depth_img = depth_ndc
if self.zfar is None:
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
else:
depth_img[noninf] = (depth_img[noninf] * (self.zfar - self.znear) + self.zfar + self.znear) / 2.0
depth_img[inf_inds] = 0.0
return depth_img


class IntrinsicsCamera(Camera):
"""A perspective camera with custom intrinsics.
Expand Down Expand Up @@ -432,6 +470,19 @@ def get_projection_matrix(self, width, height):

return P

def inverse_transform_depth_ndc(self, depth_ndc):
inf_inds = (depth_ndc == 1)
noninf = np.logical_not(inf_inds)
depth_img = depth_ndc
if self.zfar is None:
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
else:
depth_img[noninf] = ((2.0 * self.znear * self.zfar) /
(self.zfar + self.znear - depth_img[noninf] *
(self.zfar - self.znear)))
depth_img[inf_inds] = 0.0
return depth_img


__all__ = ['Camera', 'PerspectiveCamera', 'OrthographicCamera',
'IntrinsicsCamera']
12 changes: 1 addition & 11 deletions pyrender/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,18 +1150,8 @@ def _read_main_framebuffer(self, scene, flags):
depth_im = np.frombuffer(depth_buf, dtype=np.float32)
depth_im = depth_im.reshape((height, width))
depth_im = np.flip(depth_im, axis=0)
inf_inds = (depth_im == 1.0)
depth_im = 2.0 * depth_im - 1.0
z_near = scene.main_camera_node.camera.znear
z_far = scene.main_camera_node.camera.zfar
noninf = np.logical_not(inf_inds)
if z_far is None:
depth_im[noninf] = 2 * z_near / (1.0 - depth_im[noninf])
else:
depth_im[noninf] = ((2.0 * z_near * z_far) /
(z_far + z_near - depth_im[noninf] *
(z_far - z_near)))
depth_im[inf_inds] = 0.0
depth_im = scene.main_camera_node.camera.inverse_transform_depth_ndc(depth_im)

# Resize for macos if needed
if sys.platform == 'darwin':
Expand Down

0 comments on commit b2ac547

Please sign in to comment.