# 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/camera_util.py
# def look_at(eye, center, world_up):
# def roll_pitch_yaw_to_rotation_matrices(roll_pitch_yaw):
# 
# Path: ldif/util/tf_util.py
# def assert_shape(tensor, shape, name):
# def log(msg, t):
# def tile_new_axis(t, axis, length):
# def zero_by_mask(mask, vals, replace_with=0.0):
# def remove_element(t, elt, axis):


import math
import numpy as np
import tensorflow as tf
from ldif.util import np_util
from ldif.util import camera_util
from ldif.util import tf_util
def transform_depth_dodeca_to_xyz_dodeca_np(depth_dodeca):
  graph = tf.Graph()
  with graph.as_default():
    depth_in = tf.constant(depth_dodeca)
    xyz_out = transform_depth_dodeca_to_xyz_dodeca(depth_in)
    with tf.Session() as session:
      out_np = session.run(xyz_out)
  return out_np


def _unbatch(arr):
  if arr.shape[0] == 1:
    return arr.reshape(arr.shape[1:])
  return arr


def to_homogenous_np(arr, is_point=True):
  assert arr.shape[-1] in [2, 3]
  homogeneous_shape = list(arr.shape[:-1]) + [1]
  if is_point:
    coord = np.ones(homogeneous_shape, dtype=np.float32)
  else:
    coord = np.zeros(homogeneous_shape, dtype=np.float32)
  return np.concatenate([arr, coord], axis=-1)


def depth_to_cam_np(im, xfov=0.5):
  """Converts a gaps depth image to camera space."""
  im = _unbatch(im)
  height, width, _ = im.shape

# 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/camera_util.py
# def look_at(eye, center, world_up):
# def roll_pitch_yaw_to_rotation_matrices(roll_pitch_yaw):
# 
# Path: ldif/util/tf_util.py
# def assert_shape(tensor, shape, name):
# def log(msg, t):
# def tile_new_axis(t, axis, length):
# def zero_by_mask(mask, vals, replace_with=0.0):
# def remove_element(t, elt, axis):


# Path: ldif/util/geom_util.py
import math
import numpy as np
import tensorflow as tf
from ldif.util import np_util
from ldif.util import camera_util
from ldif.util import tf_util

def transform_depth_dodeca_to_xyz_dodeca_np(depth_dodeca):
  graph = tf.Graph()
  with graph.as_default():
    depth_in = tf.constant(depth_dodeca)
    xyz_out = transform_depth_dodeca_to_xyz_dodeca(depth_in)
    with tf.Session() as session:
      out_np = session.run(xyz_out)
  return out_np


def _unbatch(arr):
  if arr.shape[0] == 1:
    return arr.reshape(arr.shape[1:])
  return arr


def to_homogenous_np(arr, is_point=True):
  assert arr.shape[-1] in [2, 3]
  homogeneous_shape = list(arr.shape[:-1]) + [1]
  if is_point:
    coord = np.ones(homogeneous_shape, dtype=np.float32)
  else:
    coord = np.zeros(homogeneous_shape, dtype=np.float32)
  return np.concatenate([arr, coord], axis=-1)


def depth_to_cam_np(im, xfov=0.5):
  """Converts a gaps depth image to camera space."""
  im = _unbatch(im)
  height, width, _ = im.shape

  pixel_coords = np_util.make_coordinate_grid(