# 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 trimesh
from skimage import measure
from ldif.util.file_util import log
  """Maps from a voxel grid of implicit surface samples to a Trimesh mesh."""
  volume = np.squeeze(volume)
  length, height, width = volume.shape
  resolution = length
  # This function doesn't support non-cube volumes:
  assert resolution == height and resolution == width
  thresh = -0.07
  try:
    vertices, faces, normals, _ = measure.marching_cubes_lewiner(volume, thresh)
    del normals
    x, y, z = [np.array(x) for x in zip(*vertices)]
    xyzw = np.stack([x, y, z, np.ones_like(x)], axis=1)
    # Center the volume around the origin:
    xyzw += np.array(
        [[-resolution / 2.0, -resolution / 2.0, -resolution / 2.0, 0.]])
    # This assumes the world is right handed with y up; matplotlib's renderer
    # has z up and is left handed:
    # Reflect across z, rotate about x, and rescale to [-0.5, 0.5].
    xyzw *= np.array([[(2.0 * mcubes_extent) / resolution,
                       (2.0 * mcubes_extent) / resolution,
                       -1.0 * (2.0 * mcubes_extent) / resolution, 1]])
    y_up_to_z_up = np.array([[0., 0., -1., 0.], [0., 1., 0., 0.],
                             [1., 0., 0., 0.], [0., 0., 0., 1.]])
    xyzw = np.matmul(y_up_to_z_up, xyzw.T).T
    faces = np.stack([faces[..., 0], faces[..., 2], faces[..., 1]], axis=-1)
    world_space_xyz = np.copy(xyzw[:, :3])
    mesh = trimesh.Trimesh(vertices=world_space_xyz, faces=faces)
    log.verbose('Generated mesh successfully.')
    return True, mesh
  except (ValueError, RuntimeError) as e:

# 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/inference/extract_mesh.py
import numpy as np
import trimesh
from skimage import measure
from ldif.util.file_util import log

  """Maps from a voxel grid of implicit surface samples to a Trimesh mesh."""
  volume = np.squeeze(volume)
  length, height, width = volume.shape
  resolution = length
  # This function doesn't support non-cube volumes:
  assert resolution == height and resolution == width
  thresh = -0.07
  try:
    vertices, faces, normals, _ = measure.marching_cubes_lewiner(volume, thresh)
    del normals
    x, y, z = [np.array(x) for x in zip(*vertices)]
    xyzw = np.stack([x, y, z, np.ones_like(x)], axis=1)
    # Center the volume around the origin:
    xyzw += np.array(
        [[-resolution / 2.0, -resolution / 2.0, -resolution / 2.0, 0.]])
    # This assumes the world is right handed with y up; matplotlib's renderer
    # has z up and is left handed:
    # Reflect across z, rotate about x, and rescale to [-0.5, 0.5].
    xyzw *= np.array([[(2.0 * mcubes_extent) / resolution,
                       (2.0 * mcubes_extent) / resolution,
                       -1.0 * (2.0 * mcubes_extent) / resolution, 1]])
    y_up_to_z_up = np.array([[0., 0., -1., 0.], [0., 1., 0., 0.],
                             [1., 0., 0., 0.], [0., 0., 0., 1.]])
    xyzw = np.matmul(y_up_to_z_up, xyzw.T).T
    faces = np.stack([faces[..., 0], faces[..., 2], faces[..., 1]], axis=-1)
    world_space_xyz = np.copy(xyzw[:, :3])
    mesh = trimesh.Trimesh(vertices=world_space_xyz, faces=faces)
    log.verbose('Generated mesh successfully.')
    return True, mesh
  except (ValueError, RuntimeError) as e:

    log.warning(