# 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
import tensorflow as tf
from ldif.util.file_util import log

  Returns:
    sdf: Tensor with shape [batch_size, sample_count, 1]. The ground truth
      sdf at the sample locations. Differentiable w.r.t. samples.
  """
  xyzw_samples = np.pad(
      samples, [[0, 0], [0, 1]], mode='constant', constant_values=1)
  grid_frame_samples = np.matmul(xyzw_samples, world2grid.T)[..., :3]
  lower_coords = np.floor(grid_frame_samples).astype(np.int32)
  upper_coords = np.ceil(grid_frame_samples).astype(np.int32)
  alpha = grid_frame_samples - lower_coords.astype(np.float32)

  lca = np.split(lower_coords, 3, axis=-1)[::-1]
  uca = np.split(upper_coords, 3, axis=-1)[::-1]
  aca = np.split(alpha, 3, axis=-1)[::-1]  # ?

  c00 = grid[lca[0], lca[1], lca[2]] * (1 - aca[0]) + grid[uca[0], lca[1],
                                                           lca[2]] * aca[0]
  c01 = grid[lca[0], lca[1], uca[2]] * (1 - aca[0]) + grid[uca[0], lca[1],
                                                           uca[2]] * aca[0]
  c10 = grid[lca[0], uca[1], lca[2]] * (1 - aca[0]) + grid[uca[0], uca[1],
                                                           lca[2]] * aca[0]
  c11 = grid[lca[0], uca[1], uca[2]] * (1 - aca[0]) + grid[uca[0], uca[1],
                                                           uca[2]] * aca[0]

  c0 = c00 * (1 - aca[1]) + c10 * aca[1]
  c1 = c01 * (1 - aca[1]) + c11 * aca[1]

  interp = c0 * (1 - aca[2]) + c1 * aca[2]


# 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/interpolate_util.py
import numpy as np
import tensorflow as tf
from ldif.util.file_util import log


  Returns:
    sdf: Tensor with shape [batch_size, sample_count, 1]. The ground truth
      sdf at the sample locations. Differentiable w.r.t. samples.
  """
  xyzw_samples = np.pad(
      samples, [[0, 0], [0, 1]], mode='constant', constant_values=1)
  grid_frame_samples = np.matmul(xyzw_samples, world2grid.T)[..., :3]
  lower_coords = np.floor(grid_frame_samples).astype(np.int32)
  upper_coords = np.ceil(grid_frame_samples).astype(np.int32)
  alpha = grid_frame_samples - lower_coords.astype(np.float32)

  lca = np.split(lower_coords, 3, axis=-1)[::-1]
  uca = np.split(upper_coords, 3, axis=-1)[::-1]
  aca = np.split(alpha, 3, axis=-1)[::-1]  # ?

  c00 = grid[lca[0], lca[1], lca[2]] * (1 - aca[0]) + grid[uca[0], lca[1],
                                                           lca[2]] * aca[0]
  c01 = grid[lca[0], lca[1], uca[2]] * (1 - aca[0]) + grid[uca[0], lca[1],
                                                           uca[2]] * aca[0]
  c10 = grid[lca[0], uca[1], lca[2]] * (1 - aca[0]) + grid[uca[0], uca[1],
                                                           lca[2]] * aca[0]
  c11 = grid[lca[0], uca[1], uca[2]] * (1 - aca[0]) + grid[uca[0], uca[1],
                                                           uca[2]] * aca[0]

  c0 = c00 * (1 - aca[1]) + c10 * aca[1]
  c1 = c01 * (1 - aca[1]) + c11 * aca[1]

  interp = c0 * (1 - aca[2]) + c1 * aca[2]


  log.info('interpolated:')