text: #!/usr/bin/env python3
  # Copyright 2004-present Facebook. All Rights Reserved.
  
  import argparse
  import concurrent.futures
  import json
  import logging
  import os
  import subprocess
  import glob 
  
  import deep_sdf
  import deep_sdf.workspace as ws
  
  
  def filter_classes_glob(patterns, classes):
      import fnmatch
  
      passed_classes = set()
      for pattern in patterns:
  
          passed_classes = passed_classes.union(
              set(filter(lambda x: fnmatch.fnmatch(x, pattern), classes))
          )
  
      return list(passed_classes)
  
  
  def filter_classes_regex(patterns, classes):
      import re
  
      passed_classes = set()
      for pattern in patterns:
          regex = re.compile(pattern)
          passed_classes = passed_classes.union(set(filter(regex.match, classes)))
  
      return list(passed_classes)
  
  
  def filter_classes(patterns, classes):
      if patterns[0] == "glob":
          return filter_classes_glob(patterns, classes[1:])
      elif patterns[0] == "regex":
          return filter_classes_regex(patterns, classes[1:])
      else:
          return filter_classes_glob(patterns, classes)
  
  
  def process_mesh(mesh_filepath, target_filepath, executable, additional_args):
      logging.info(mesh_filepath + " --> " + target_filepath)
      command = [executable, "-m", mesh_filepath, "-o", target_filepath] + additional_args
  
      subproc = subprocess.Popen(command, stdout=subprocess.DEVNULL)
      subproc.wait()
  
  
  def append_data_source_map(data_dir, name, source):
  
      data_source_map_filename = ws.get_data_source_map_filename(data_dir)
  
      print("data sources stored to " + data_source_map_filename)
  
      data_source_map = {}
  
      if os.path.isfile(data_source_map_filename):
          with open(data_source_map_filename, "r") as f:
              data_source_map = json.load(f)
  
      if name in data_source_map:
          if not data_source_map[name] == os.path.abspath(source):
              raise RuntimeError(
                  "Cannot add data with the same name and a different source."
              )
  
      else:
          data_source_map[name] = os.path.abspath(source)
  
          with open(data_source_map_filename, "w") as f:
              json.dump(data_source_map, f, indent=2)
  
  
  if __name__ == "__main__":
  
      arg_parser = argparse.ArgumentParser(
          formatter_class=argparse.RawTextHelpFormatter,
          description="Pre-processes data from a data source and append the results to "
          + "a dataset.",
      )
      arg_parser.add_argument(
          "--dest_dir",
          "-d",
          dest="dest_dir",
          required=True,
          help="The directory which holds all preprocessed data.",
      )
      arg_parser.add_argument(
          "--source",
          "-s",
          dest="source_dir",
          required=True,
          help="The directory which holds the data to preprocess and append.",
      )
      
      arg_parser.add_argument(
          "--skip",
          dest="skip",
          default=False,
          action="store_true",
          help="If set, previously-processed shapes will be skipped",
      )
      
      arg_parser.add_argument(
          "--threads",
          dest="num_threads",
          default=8,
          help="The number of threads to use to process the data.",
      )
      
  
      deep_sdf.add_common_args(arg_parser)
  
      args = arg_parser.parse_args()
  
      deep_sdf.configure_logging(args)
  
      
      deepsdf_dir = os.path.dirname(os.path.abspath(__file__)) 
      
      executable  = os.path.join(deepsdf_dir, "bin/PreprocessMesh_v2")
      
      assert os.path.exists(executable), executable
      
      extension = ".npz"    
      
      # save directory
      dest_dir = args.dest_dir
  
      logging.info(
          "Preprocessing data from "
          + args.source_dir
          + " and placing the results in "
          + dest_dir
      )
  
      if not os.path.isdir(dest_dir):
          os.makedirs(dest_dir)
  
      #if args.surface_sampling:
      #    normalization_param_dir = os.path.join(
      #        args.data_dir, ws.normalization_param_subdir, args.source_name
      #    )
      #    if not os.path.isdir(normalization_param_dir):
      #        os.makedirs(normalization_param_dir)
      #append_data_source_map(args.data_dir, args.source_name, args.source_dir)
      #class_directories = split[args.source_name]
      # 
      
      meshes_targets_and_specific_args = []
      
      #for class_dir in class_directories:
      if 1:
          specific_args = [ '-s', '100000' , '--ft']
          
          flist = glob.glob( os.path.join(args.source_dir,'*.ply'))
          flist.sort()
  
          logging.debug( "Processing " + str(len(flist)) )
          
  
          for inst_fp in flist:
              
              mid = os.path.basename(inst_fp).split('.')[0]
              
              processed_filepath = os.path.join(dest_dir, mid+'.npz')
              
              meshes_targets_and_specific_args.append(
                      (  inst_fp,
                         processed_filepath,
                         specific_args,
                      )
                  )
                  
      with concurrent.futures.ThreadPoolExecutor(
          max_workers=int(args.num_threads)  ) as executor:
          
          for ( mesh_filepath,
                target_filepath,
                specific_args,
          ) in meshes_targets_and_specific_args:
              executor.submit(
                  process_mesh,
                  mesh_filepath,
                  target_filepath,
                  executable,
                  specific_args ,
              )
          
          executor.shutdown()
          
  