This repository was archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
capture bad_alloc if it ever happens #46
Open
adrpar
wants to merge
1
commit into
heremaps:master
Choose a base branch
from
adrpar:nice_error_on_oom
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,21 @@ namespace tntn { | |
|
|
||
| //forward decl for use in preset commands | ||
| int tin_terrain_commandline_action(std::vector<std::string> args); | ||
| static int main_loop_dem2tintiles(const std::string input_file, | ||
| const std::string meshing_method, | ||
| const int min_zoom, | ||
| const int max_zoom, | ||
| double max_error, | ||
| bool max_error_given, | ||
| const std::string output_basedir, | ||
| std::unique_ptr<MeshWriter> w); | ||
| static int main_loop_dem2tin(const std::string input_file, | ||
| const std::string method, | ||
| double max_error, | ||
| bool max_error_given, | ||
| int step, | ||
| const std::string output_file, | ||
| const FileFormat output_file_format); | ||
|
|
||
| typedef int (*subcommand_function_t)(bool need_help, | ||
| const po::variables_map&, | ||
|
|
@@ -140,40 +155,52 @@ static int subcommand_dem2tintiles(bool need_help, | |
|
|
||
| const std::string meshing_method = local_varmap["method"].as<std::string>(); | ||
|
|
||
| auto input_raster = std::make_unique<RasterDouble>(); | ||
|
|
||
| if(!load_raster_file(input_file.c_str(), *input_raster)) | ||
| if("zemlya" != meshing_method && "terra" != meshing_method) | ||
| { | ||
| return false; | ||
| throw po::error(std::string("unknown method ") + meshing_method); | ||
| } | ||
|
|
||
| if("zemlya" == meshing_method || "terra" == meshing_method) | ||
| std::unique_ptr<char[]> emergency_memory(new char[16384]); | ||
|
|
||
| try | ||
| { | ||
| max_error = input_raster->get_cell_size(); | ||
| if(local_varmap.count("max-error")) | ||
| { | ||
| max_error_given = true; | ||
| max_error = local_varmap["max-error"].as<double>(); | ||
| } | ||
| return main_loop_dem2tintiles(input_file, | ||
| meshing_method, | ||
| min_zoom, | ||
| max_zoom, | ||
| max_error, | ||
| max_error_given, | ||
| output_basedir, | ||
| std::move(w)); | ||
| } | ||
| catch(std::bad_alloc& exception) | ||
| { | ||
| emergency_memory.reset(); | ||
|
|
||
| if(max_error < 0.0) | ||
| { | ||
| throw po::error("max-error must be positive"); | ||
| } | ||
| TNTN_LOG_ERROR("tin-terrain has run out of memory."); | ||
| exit(1); | ||
| } | ||
| #if defined(TNTN_USE_ADDONS) && TNTN_USE_ADDONS | ||
| else if("curvature") | ||
| } | ||
|
|
||
| static int main_loop_dem2tintiles(const std::string input_file, | ||
| const std::string meshing_method, | ||
| const int min_zoom, | ||
| const int max_zoom, | ||
| double max_error, | ||
| bool max_error_given, | ||
| const std::string output_basedir, | ||
| std::unique_ptr<MeshWriter> w) | ||
| { | ||
| auto input_raster = std::make_unique<RasterDouble>(); | ||
|
|
||
| if(!load_raster_file(input_file.c_str(), *input_raster)) | ||
| { | ||
| double threshold = 1; | ||
| if(local_varmap.count("threshold")) | ||
| { | ||
| threshold = local_varmap["threshold"].as<double>(); | ||
| } | ||
| return false; | ||
| } | ||
| #endif | ||
| else | ||
|
|
||
| if(!max_error_given && ("zemlya" == meshing_method || "terra" == meshing_method)) | ||
| { | ||
| throw po::error(std::string("unknown method ") + meshing_method); | ||
| max_error = input_raster->get_cell_size(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that this line will always erase a max_error value passed throught CLI
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that was the case in the original code. So I guess that was on purpose? I cannot tell if that was intentional or not.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, completely messed this up. fixed. |
||
| } | ||
|
|
||
| RasterOverviews overviews(std::move(input_raster), min_zoom, max_zoom); | ||
|
|
@@ -278,17 +305,20 @@ static int subcommand_dem2tin(bool need_help, | |
|
|
||
| // clang-format off | ||
| subdesc.add_options() | ||
| ("input", po::value<std::string>(), "input filename") | ||
| ("input-format",po::value<std::string>()->default_value("auto"), "input file format, can be any of: auto, asc, xyz, tiff") | ||
| ("output", po::value<std::string>(), "output filename") | ||
| ("output-format", po::value<std::string>()->default_value("auto"), "output file format, can be any of: auto, obj, off, terrain (quantized mesh), json/geojson") | ||
| ("max-error", po::value<double>(), "max error parameter when using terra or zemlya method") | ||
| ("step", po::value<int>(), "grid spacing in pixels when using dense method") | ||
| ("input", po::value<std::string>(), "input filename") | ||
| ("input-format", po::value<std::string>()->default_value("auto"), | ||
| "input file format, can be any of: auto, asc, xyz, tiff") | ||
| ("output", po::value<std::string>(), "output filename") | ||
| ("output-format", po::value<std::string>()->default_value("auto"), | ||
| "output file format, can be any of: auto, obj, off, terrain (quantized mesh), json/geojson") | ||
| ("max-error", po::value<double>(), "max error parameter when using terra or zemlya method") | ||
| ("step", po::value<int>(), "grid spacing in pixels when using dense method") | ||
| #if defined(TNTN_USE_ADDONS) && TNTN_USE_ADDONS | ||
| ("threshold", po::value<double>(), "threshold when using curvature method") | ||
| ("method", po::value<std::string>()->default_value("terra"), "meshing method, valid values are: dense, terra, zemlya, curvature"); | ||
| ("threshold", po::value<double>(), "threshold when using curvature method") | ||
| ("method", po::value<std::string>()->default_value("terra"), "meshing method, valid values are: dense, terra, zemlya, curvature"); | ||
| #else | ||
| ("method", po::value<std::string>()->default_value("terra"), "meshing method, valid values are: dense, terra, zemlya"); | ||
| ("method", po::value<std::string>()->default_value("terra"), | ||
| "meshing method, valid values are: dense, terra, zemlya"); | ||
| #endif | ||
| // clang-format on | ||
|
|
||
|
|
@@ -344,6 +374,51 @@ static int subcommand_dem2tin(bool need_help, | |
|
|
||
| const std::string method = local_varmap["method"].as<std::string>(); | ||
|
|
||
| bool max_error_given = false; | ||
| double max_error = 0.0; | ||
|
|
||
| if(local_varmap.count("max-error")) | ||
| { | ||
| max_error_given = true; | ||
| max_error = local_varmap["max-error"].as<double>(); | ||
| } | ||
|
|
||
| int step = 1; | ||
| if(local_varmap.count("step")) | ||
| { | ||
| step = local_varmap["step"].as<int>(); | ||
| } | ||
|
|
||
| std::unique_ptr<char[]> emergency_memory(new char[16384]); | ||
|
|
||
| try | ||
| { | ||
| return main_loop_dem2tin(input_file, | ||
| method, | ||
| max_error, | ||
| max_error_given, | ||
| step, | ||
| output_file, | ||
| output_file_format); | ||
| } | ||
| catch(std::bad_alloc& exception) | ||
| { | ||
| emergency_memory.reset(); | ||
|
|
||
| TNTN_LOG_ERROR("tin-terrain has run out of memory."); | ||
| exit(1); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| static int main_loop_dem2tin(const std::string input_file, | ||
| const std::string method, | ||
| double max_error, | ||
| bool max_error_given, | ||
| int step, | ||
| const std::string output_file, | ||
| const FileFormat output_file_format) | ||
| { | ||
| auto raster = std::make_unique<RasterDouble>(); | ||
|
|
||
| // Import raster file without projection validation | ||
|
|
@@ -361,10 +436,9 @@ static int subcommand_dem2tin(bool need_help, | |
|
|
||
| if(method == "terra" || method == "zemlya") | ||
| { | ||
| double max_error = raster->get_cell_size(); | ||
| if(local_varmap.count("max-error")) | ||
| if(!max_error_given) | ||
| { | ||
| max_error = local_varmap["max-error"].as<double>(); | ||
| max_error = raster->get_cell_size(); | ||
| } | ||
|
|
||
| if("terra" == method) | ||
|
|
@@ -380,34 +454,9 @@ static int subcommand_dem2tin(bool need_help, | |
| } | ||
| else if(method == "dense") | ||
| { | ||
| int step = 1; | ||
| if(local_varmap.count("step")) | ||
| { | ||
| step = local_varmap["step"].as<int>(); | ||
| } | ||
|
|
||
| TNTN_LOG_INFO("generating dense mesh grid ..."); | ||
| mesh = generate_tin_dense_quadwalk(*raster, step); | ||
| } | ||
| #if defined(TNTN_USE_ADDONS) && TNTN_USE_ADDONS | ||
| else if(method == "curvature") | ||
| { | ||
| if(!local_varmap.count("threshold")) | ||
| { | ||
| throw po::error("no --threshold given, required for this method"); | ||
| } | ||
|
|
||
| double gradient_threshold = local_varmap["threshold"].as<double>(); | ||
|
|
||
| if(gradient_threshold <= 0.0) | ||
| { | ||
| throw po::error("threshold must be larger than zero"); | ||
| } | ||
|
|
||
| TNTN_LOG_INFO("performing curverture integral meshing..."); | ||
| mesh = generate_tin_curvature(*raster, gradient_threshold); | ||
| } | ||
| #endif | ||
| else | ||
| { | ||
| throw po::error(std::string("unknown method ") + method); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wonder if this has performance cost in our case https://stackoverflow.com/questions/1897940/in-what-ways-do-c-exceptions-slow-down-code-when-there-are-no-exceptions-thown