# Path: s2p/common.py
# def remove(target):
# def garbage_cleanup():
# def tmpfile(ext=''):
# def run(cmd, env=os.environ):
# def mkdir_p(path):
# def shellquote(s):
# def matrix_translation(x, y):
# def image_size_gdal(im):
# def gdal_read_as_array_with_nans(im):
# def image_zoom_out_morpho(im, f):
# def rasterio_write(path, array, profile={}, tags={}):
# def image_apply_homography(out, im, H, w, h):
# def image_qauto(im, out=None):
# def points_apply_homography(H, pts):
# def bounding_box2D(pts):
# def image_crop_gdal(im, x, y, w, h, out=None):
# def run_binary_on_list_of_points(points, binary, option=None, env_var=None):
# def get_rectangle_coordinates(im):
# def get_roi_coordinates(img, preview):
# def cargarse_basura(inputf, outputf):
# def print_elapsed_time(since_first_call=False):
# class RunFailure(Exception):


import os
import json
import argparse
import s2p
    import tempfile
from s2p import common
    tiles_file = os.path.join(s2poutdir, 'tiles.txt')

    # Read the tiles file
    tiles = s2p.read_tiles(tiles_file)
    print(str(len(tiles))+' tiles found')

    # collect all plys
    plys = [os.path.join(os.path.abspath(os.path.dirname(t)), 'cloud.ply') for t in tiles]


    nthreads = 4
    plys = ' '.join(plys)
    common.run("LidarPreprocessor -to %s.LidarO -tp %s.LidarP -nt %d %s -o %s" % (output,
                                                                           output,
                                                                           nthreads,
                                                                           plys,
                                                                           output))


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=('S2P: lidarviewer generation tool'))

    parser.add_argument('s2pout',metavar='s2poutdir',
                        help=('path to the s2p output directory'))
    parser.add_argument('outdir',metavar='potreeoutdir', default='',nargs='?',
                        help=('path to output lidarviewer (default: current dir)'))
    args = parser.parse_args()

    try:
    	produce_lidarviewer(args.s2pout,args.outdir)

# Path: s2p/common.py
# def remove(target):
# def garbage_cleanup():
# def tmpfile(ext=''):
# def run(cmd, env=os.environ):
# def mkdir_p(path):
# def shellquote(s):
# def matrix_translation(x, y):
# def image_size_gdal(im):
# def gdal_read_as_array_with_nans(im):
# def image_zoom_out_morpho(im, f):
# def rasterio_write(path, array, profile={}, tags={}):
# def image_apply_homography(out, im, H, w, h):
# def image_qauto(im, out=None):
# def points_apply_homography(H, pts):
# def bounding_box2D(pts):
# def image_crop_gdal(im, x, y, w, h, out=None):
# def run_binary_on_list_of_points(points, binary, option=None, env_var=None):
# def get_rectangle_coordinates(im):
# def get_roi_coordinates(img, preview):
# def cargarse_basura(inputf, outputf):
# def print_elapsed_time(since_first_call=False):
# class RunFailure(Exception):


# Path: utils/s2p_to_lidarviewer.py
import os
import json
import argparse
import s2p
    import tempfile
from s2p import common

    tiles_file = os.path.join(s2poutdir, 'tiles.txt')

    # Read the tiles file
    tiles = s2p.read_tiles(tiles_file)
    print(str(len(tiles))+' tiles found')

    # collect all plys
    plys = [os.path.join(os.path.abspath(os.path.dirname(t)), 'cloud.ply') for t in tiles]


    nthreads = 4
    plys = ' '.join(plys)
    common.run("LidarPreprocessor -to %s.LidarO -tp %s.LidarP -nt %d %s -o %s" % (output,
                                                                           output,
                                                                           nthreads,
                                                                           plys,
                                                                           output))


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=('S2P: lidarviewer generation tool'))

    parser.add_argument('s2pout',metavar='s2poutdir',
                        help=('path to the s2p output directory'))
    parser.add_argument('outdir',metavar='potreeoutdir', default='',nargs='?',
                        help=('path to output lidarviewer (default: current dir)'))
    args = parser.parse_args()

    try:
    	produce_lidarviewer(args.s2pout,args.outdir)

    except common.RunFailure: