# Path: ldif/datasets/process_element.py
# def load_example_dict(example_directory, log_level=None):
# def _float_feature(value):
# def _bytes_feature(value):
# def make_tf_example(d):
# def full_featurespec():
# def parse_tf_example(example_proto):
# def _example_dict_tf_func_wrapper(mesh_orig_path):
# def parse_example(filename):
# 
# Path: ldif/scripts/make_example.py
# def remove_png_dir(d):
# def write_depth_and_normals_npz(dirpath, path_out):
# def mesh_to_example(codebase_root_dir, mesh_path, dirpath, skip_existing, log_level):
# 
# 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/path_util.py
# def get_path_to_ldif_root():
# def get_path_to_ldif_parent():
# def package_to_abs(path):
# def gaps_path():
# def test_data_path():
# def util_test_data_path():
# def create_test_output_dir():
# 
# 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 glob
import random
import os
import tqdm
import tensorflow as tf
from absl import app
from absl import flags
from joblib import Parallel, delayed
from ldif.datasets import process_element
from ldif.scripts import make_example
from ldif.util import file_util
from ldif.util import path_util
from ldif.util.file_util import log

flags.DEFINE_boolean(
    'optimize_only', False, 'Whether to skip dataset creation '
    'and only write tfrecords files.')


def process_one(f, mesh_directory, dataset_directory, skip_existing, log_level):
  """Processes a single mesh, adding it to the dataset."""
  relpath = f.replace(mesh_directory, '')
  assert relpath[0] == '/'
  relpath = relpath[1:]
  split, synset = relpath.split('/')[:2]
  log.verbose(f'The split is {split} and the synset is {synset}')
  name = os.path.basename(f)
  name, extension = os.path.splitext(name)
  valid_extensions = ['.ply']
  if extension not in valid_extensions:
    raise ValueError(f'File with unsupported extension {extension} found: {f}.'
                     f' Only {valid_extensions} are supported.')
  output_dir = f'{dataset_directory}/{split}/{synset}/{name}/'
  # This is the last file the processing writes, if it already exists the
  # example has already been processed.
  final_file_written = f'{output_dir}/depth_and_normals.npz'
  make_example.mesh_to_example(
      os.path.join(path_util.get_path_to_ldif_parent(), 'ldif'), f,
      f'{dataset_directory}/{split}/{synset}/{name}/', skip_existing, log_level)
  return output_dir

def serialize(example_dir, log_level):
  d = process_element.load_example_dict(example_dir, log_level)

# Path: ldif/datasets/process_element.py
# def load_example_dict(example_directory, log_level=None):
# def _float_feature(value):
# def _bytes_feature(value):
# def make_tf_example(d):
# def full_featurespec():
# def parse_tf_example(example_proto):
# def _example_dict_tf_func_wrapper(mesh_orig_path):
# def parse_example(filename):
# 
# Path: ldif/scripts/make_example.py
# def remove_png_dir(d):
# def write_depth_and_normals_npz(dirpath, path_out):
# def mesh_to_example(codebase_root_dir, mesh_path, dirpath, skip_existing, log_level):
# 
# 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/path_util.py
# def get_path_to_ldif_root():
# def get_path_to_ldif_parent():
# def package_to_abs(path):
# def gaps_path():
# def test_data_path():
# def util_test_data_path():
# def create_test_output_dir():
# 
# 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: meshes2dataset.py
import glob
import random
import os
import tqdm
import tensorflow as tf
from absl import app
from absl import flags
from joblib import Parallel, delayed
from ldif.datasets import process_element
from ldif.scripts import make_example
from ldif.util import file_util
from ldif.util import path_util
from ldif.util.file_util import log


flags.DEFINE_boolean(
    'optimize_only', False, 'Whether to skip dataset creation '
    'and only write tfrecords files.')


def process_one(f, mesh_directory, dataset_directory, skip_existing, log_level):
  """Processes a single mesh, adding it to the dataset."""
  relpath = f.replace(mesh_directory, '')
  assert relpath[0] == '/'
  relpath = relpath[1:]
  split, synset = relpath.split('/')[:2]
  log.verbose(f'The split is {split} and the synset is {synset}')
  name = os.path.basename(f)
  name, extension = os.path.splitext(name)
  valid_extensions = ['.ply']
  if extension not in valid_extensions:
    raise ValueError(f'File with unsupported extension {extension} found: {f}.'
                     f' Only {valid_extensions} are supported.')
  output_dir = f'{dataset_directory}/{split}/{synset}/{name}/'
  # This is the last file the processing writes, if it already exists the
  # example has already been processed.
  final_file_written = f'{output_dir}/depth_and_normals.npz'
  make_example.mesh_to_example(
      os.path.join(path_util.get_path_to_ldif_parent(), 'ldif'), f,
      f'{dataset_directory}/{split}/{synset}/{name}/', skip_existing, log_level)
  return output_dir

def serialize(example_dir, log_level):
  d = process_element.load_example_dict(example_dir, log_level)

  s = process_element.make_tf_example(d)