From c9760204be1195b308647418a26f6707832c34f2 Mon Sep 17 00:00:00 2001 From: Piero Toffanin Date: Mon, 31 Aug 2020 13:07:08 -0400 Subject: [PATCH] Add support for maxConcurrency in dem2mesh Former-commit-id: 7ef5946d3812303e8f14c832663dee3f7e812b25 --- opendm/mesh.py | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/opendm/mesh.py b/opendm/mesh.py index 2416a835..d08b8ed3 100644 --- a/opendm/mesh.py +++ b/opendm/mesh.py @@ -36,7 +36,7 @@ def create_25dmesh(inPointCloud, outMesh, dsm_radius=0.07, dsm_resolution=0.05, ) if method == 'gridded': - mesh = dem_to_mesh_gridded(os.path.join(tmp_directory, 'mesh_dsm.tif'), outMesh, maxVertexCount, verbose) + mesh = dem_to_mesh_gridded(os.path.join(tmp_directory, 'mesh_dsm.tif'), outMesh, maxVertexCount, verbose, maxConcurrency=max(1, available_cores)) elif method == 'poisson': dsm_points = dem_to_points(os.path.join(tmp_directory, 'mesh_dsm.tif'), os.path.join(tmp_directory, 'dsm_points.ply'), verbose) mesh = screened_poisson_reconstruction(dsm_points, outMesh, depth=depth, @@ -74,7 +74,7 @@ def dem_to_points(inGeotiff, outPointCloud, verbose=False): return outPointCloud -def dem_to_mesh_gridded(inGeotiff, outMesh, maxVertexCount, verbose=False): +def dem_to_mesh_gridded(inGeotiff, outMesh, maxVertexCount, verbose=False, maxConcurrency=1): log.ODM_INFO('Creating mesh from DSM: %s' % inGeotiff) mesh_path, mesh_filename = os.path.split(outMesh) @@ -87,19 +87,32 @@ def dem_to_mesh_gridded(inGeotiff, outMesh, maxVertexCount, verbose=False): outMeshDirty = os.path.join(mesh_path, "{}.dirty{}".format(basename, ext)) - kwargs = { - 'bin': context.dem2mesh_path, - 'outfile': outMeshDirty, - 'infile': inGeotiff, - 'maxVertexCount': maxVertexCount, - 'verbose': '-verbose' if verbose else '' - } + # This should work without issues most of the times, + # but just in case we lower maxConcurrency if it fails. + while True: + try: + kwargs = { + 'bin': context.dem2mesh_path, + 'outfile': outMeshDirty, + 'infile': inGeotiff, + 'maxVertexCount': maxVertexCount, + 'maxConcurrency': maxConcurrency, + 'verbose': '-verbose' if verbose else '' + } + system.run('{bin} -inputFile {infile} ' + '-outputFile {outfile} ' + '-maxTileLength 2000 ' + '-maxVertexCount {maxVertexCount} ' + '-maxConcurrency {maxConcurrency} ' + ' {verbose} '.format(**kwargs)) + break + except Exception as e: + maxConcurrency = math.floor(maxConcurrency / 2) + if maxConcurrency >= 1: + log.ODM_WARNING("dem2mesh failed, retrying with lower concurrency (%s) in case this is a memory issue" % maxConcurrency) + else: + raise e - system.run('{bin} -inputFile {infile} ' - '-outputFile {outfile} ' - '-maxTileLength 4000 ' - '-maxVertexCount {maxVertexCount} ' - ' {verbose} '.format(**kwargs)) # Cleanup and reduce vertex count if necessary # (as dem2mesh cannot guarantee that we'll have the target vertex count)