# Path: ldif/util/np_util.py
# def batch_np(arr, batch_size):
# def make_coordinate_grid(height, width, is_screen_space, is_homogeneous):
# def make_coordinate_grid_3d(length, height, width, is_screen_space,
#                             is_homogeneous):
# def filter_valid(mask, vals):
# def zero_by_mask(mask, vals, replace_with=0.0):
# def make_mask(im, thresh=0.0):
# def make_pixel_mask(im):
# def thresh_and_radius_to_distance(radius, thresh):
# def plot_rbfs_at_thresh(centers, radii, thresh=0.5):
# def plot_rbfs(centers, radii, scale=10.0):
# def cube_and_render(volume, thresh):
# def sample_surface(quadrics, centers, radii, length, height, width,
#                    renormalize):
# def visualize_prediction(quadrics,
#                          centers,
#                          radii,
#                          renormalize,
#                          thresh=0.0,
#                          input_volumes=None):
# 
# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):


import numpy as np
from ldif.util import np_util
from ldif.util.file_util import log
  return xyz_image, mask


def depth_to_point_cloud(depth_image, intrinsics, extrinsics):
  xyz_image, mask = depth_to_xyz_image(depth_image, intrinsics, extrinsics)
  log.info(xyz_image.shape, mask.shape)
  xyz = xyz_image.reshape([-1, 3])[mask.ravel()]
  return xyz


def depth_image_to_cam_image(depth_image,
                             fx=585.0,
                             fy=585.0,
                             cx=255.5,
                             cy=255.5):
  """Converts a GAPS depth image to a camera-space image.

  Args:
    depth_image: Numpy array with shape [height, width] or [height, width, 1].
      The depth in units (not in 1000ths of units, which is what GAPS writes).
    fx: The x focal-length. Float.
    fy: The y focal-length. Float.
    cx: The x center. Float.
    cy: The y center. Float.
  Returns:
    cam_image: array with shape [height, width, 3].
  """
  is_invalid = np.squeeze(depth_image == 0.0)
  height, width = depth_image.shape[0:2]
  depth_image = np.reshape(depth_image, [height, width])

# Path: ldif/util/np_util.py
# def batch_np(arr, batch_size):
# def make_coordinate_grid(height, width, is_screen_space, is_homogeneous):
# def make_coordinate_grid_3d(length, height, width, is_screen_space,
#                             is_homogeneous):
# def filter_valid(mask, vals):
# def zero_by_mask(mask, vals, replace_with=0.0):
# def make_mask(im, thresh=0.0):
# def make_pixel_mask(im):
# def thresh_and_radius_to_distance(radius, thresh):
# def plot_rbfs_at_thresh(centers, radii, thresh=0.5):
# def plot_rbfs(centers, radii, scale=10.0):
# def cube_and_render(volume, thresh):
# def sample_surface(quadrics, centers, radii, length, height, width,
#                    renormalize):
# def visualize_prediction(quadrics,
#                          centers,
#                          radii,
#                          renormalize,
#                          thresh=0.0,
#                          input_volumes=None):
# 
# Path: ldif/util/file_util.py
# def readlines(p):
# def readbin(p):
# def writebin(p, s):
# def writetxt(p, s):
# def write_np(path, arr):
# def read_grd(path):
# def read_sif_v1(path, verbose=False):
# def read_lines(p):
# def read_image(p):
# def read_npz(p):
# def read_np(p):
# def read_txt_to_np(p):
# def read_py2_pkl(p):
# def write_mesh(path, mesh):
# def read_mesh(path):
# def read_csv(path):
# def read_normals(path_to_dir, im_count=20, leading='_depth'):
# def write_points(path_ext_optional, points):
# def write_grd(path, volume, world2grid=None):
# def write_depth_image(path, depth_image):


# Path: ldif/util/geom_util_np.py
import numpy as np
from ldif.util import np_util
from ldif.util.file_util import log

  return xyz_image, mask


def depth_to_point_cloud(depth_image, intrinsics, extrinsics):
  xyz_image, mask = depth_to_xyz_image(depth_image, intrinsics, extrinsics)
  log.info(xyz_image.shape, mask.shape)
  xyz = xyz_image.reshape([-1, 3])[mask.ravel()]
  return xyz


def depth_image_to_cam_image(depth_image,
                             fx=585.0,
                             fy=585.0,
                             cx=255.5,
                             cy=255.5):
  """Converts a GAPS depth image to a camera-space image.

  Args:
    depth_image: Numpy array with shape [height, width] or [height, width, 1].
      The depth in units (not in 1000ths of units, which is what GAPS writes).
    fx: The x focal-length. Float.
    fy: The y focal-length. Float.
    cx: The x center. Float.
    cy: The y center. Float.
  Returns:
    cam_image: array with shape [height, width, 3].
  """
  is_invalid = np.squeeze(depth_image == 0.0)
  height, width = depth_image.shape[0:2]
  depth_image = np.reshape(depth_image, [height, width])

  pixel_coords = np_util.make_coordinate_grid(