# 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
  flat_world_xyz = tf.matmul(
      flat_camera_xyzw, camera_to_world_mat, transpose_b=True)
  world_xyz = tf.reshape(flat_world_xyz, [batch_size, height, width, 4])
  world_xyz = world_xyz[:, :, :, :3]
  return world_xyz, flat_camera_xyzw[:, :, :3], flat_nic_xyz


def interpolate_from_grid_coordinates(samples, grid):
  """Performs trilinear interpolation to estimate the value of a grid function.

  This function makes several assumptions to do the lookup:
  1) The grid is LHW and has evenly spaced samples in the range (0, 1), which
    is really the screen space range [0.5, {L, H, W}-0.5].

  Args:
    samples: Tensor with shape [batch_size, sample_count, 3].
    grid: Tensor with shape [batch_size, length, height, width, 1].

  Returns:
    sample: Tensor with shape [batch_size, sample_count, 1] and type float32.
    mask: Tensor with shape [batch_size, sample_count, 1] and type float32
  """
  batch_size, length, height, width = grid.get_shape().as_list()[:4]
  # These asserts aren't required by the algorithm, but they are currently
  # true for the pipeline:
  assert length == height
  assert length == width
  sample_count = samples.get_shape().as_list()[1]
  tf_util.assert_shape(samples, [batch_size, sample_count, 3],
                       'interpolate_from_grid:samples')

# 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

  flat_world_xyz = tf.matmul(
      flat_camera_xyzw, camera_to_world_mat, transpose_b=True)
  world_xyz = tf.reshape(flat_world_xyz, [batch_size, height, width, 4])
  world_xyz = world_xyz[:, :, :, :3]
  return world_xyz, flat_camera_xyzw[:, :, :3], flat_nic_xyz


def interpolate_from_grid_coordinates(samples, grid):
  """Performs trilinear interpolation to estimate the value of a grid function.

  This function makes several assumptions to do the lookup:
  1) The grid is LHW and has evenly spaced samples in the range (0, 1), which
    is really the screen space range [0.5, {L, H, W}-0.5].

  Args:
    samples: Tensor with shape [batch_size, sample_count, 3].
    grid: Tensor with shape [batch_size, length, height, width, 1].

  Returns:
    sample: Tensor with shape [batch_size, sample_count, 1] and type float32.
    mask: Tensor with shape [batch_size, sample_count, 1] and type float32
  """
  batch_size, length, height, width = grid.get_shape().as_list()[:4]
  # These asserts aren't required by the algorithm, but they are currently
  # true for the pipeline:
  assert length == height
  assert length == width
  sample_count = samples.get_shape().as_list()[1]
  tf_util.assert_shape(samples, [batch_size, sample_count, 3],
                       'interpolate_from_grid:samples')

  tf_util.assert_shape(grid, [batch_size, length, height, width, 1],