From d2dd62e5fb1eb86fb81b50771b853e4d6a661ea2 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sun, 31 Oct 2021 22:50:11 +0800 Subject: [PATCH] complete tools --- src/CMakeLists.txt | 4 +- src/tool/display_help.h | 384 ++++++++++++++++++++++++++++++++++++++++ src/tool/grd2tin.cpp | 278 ++++++++++++++++++++++++++--- src/tool/rnd2tin.cpp | 241 ++++++++++++++++++++++--- 4 files changed, 856 insertions(+), 51 deletions(-) create mode 100644 src/tool/display_help.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f4e4fa9..8d5cf6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -49,7 +49,7 @@ else() DESTINATION ${CONFIG_FILE_PATH}) endif() # 头文件安装命令 -install(FILES lib/tin.h DESTINATION include/tin) +install(FILES lib/tin.h DESTINATION include) # 以下部分为例子程序的编译 # 设置可执行文件的输出地址 @@ -60,7 +60,7 @@ macro(add_demo name) # 添加可执行文件 命令行 add_executable(${name} demo/${name}.cpp) # 为安装文件添加动态库的搜索地址 在Windows下并没有什么用 直接忽略 - set_target_properties(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib) + set_target_properties(${name} PROPERTIES INSTALL_RPATH /usr/local/lib) # 链接动态库 target_link_libraries(${name} PUBLIC tin) endmacro() diff --git a/src/tool/display_help.h b/src/tool/display_help.h new file mode 100644 index 0000000..673c4a1 --- /dev/null +++ b/src/tool/display_help.h @@ -0,0 +1,384 @@ +#ifndef _DISPHELP_H +#define _DISPHELP_H +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "vector" + +using namespace std; + +typedef vector strArray; + +struct my_option +{ + string flag_s,flag_l; + string message; + strArray sec_message; + my_option() + { + flag_s = flag_l = message = ""; + } +}; +typedef vector opArray; + +class dispHelp +{ +public: + dispHelp(){ + front_space = 0; + back_space = 10; + ex_name = "Execuable"; + version = "0.0.1"; + descript = "Brief information about this command."; + author = "Author's information."; + } + ~dispHelp(){} + void addHeadInfo(string,string,string,string); + void addUsage(string); + void addOption(string,string,string); + void addOptionSec(string,int); + void addExample(string); + void changeLayerOut(int,int); + void show(); +private: + string ex_name,version,descript,author; + int front_space,back_space; + opArray options; + strArray examples; + strArray usages; +}; + +void dispHelp::addHeadInfo(string s1,string s2,string s3,string s4) +{ + ex_name = s1; version = s2; descript = s3; author = s4; + return; +} + +void dispHelp::addUsage(string usg) +{ + usages.push_back(usg); + return; +} + +void dispHelp::addOption(string msg,string sflag,string lflag = "") +{ + my_option tmp_option; + tmp_option.message = msg; tmp_option.flag_s = sflag; tmp_option.flag_l = lflag; + options.push_back(tmp_option); + return; +} + +void dispHelp::addOptionSec(string msg,int index = -1) +{ + if (index < 0) + { + options.back().sec_message.push_back(msg); + } + else options[index].sec_message.push_back(msg); + return; +} + +void dispHelp::addExample(string ex) +{ + examples.push_back(ex); + return; +} + +void dispHelp::changeLayerOut(int left,int right) +{ + front_space = left; back_space = right; + return; +} + +void dispHelp::show() +{ + int line_length; + string segment,full_message; + stringstream ss_message; + //获取终端窗口的行列数 + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + //显示头信息 + full_message = ex_name + " " + version + " - " + descript; + ss_message.clear(); ss_message.str(full_message); + + line_length = front_space + back_space; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space + back_space)) + { + for (int i = 0; i < front_space; i++) clog << " "; + clog << segment << " "; + line_length += (segment.length()+1); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+4; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+9+front_space+back_space); + } + } + clog << endl; + + ss_message.clear(); ss_message.str(author); + line_length = front_space + back_space;; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space + back_space)) + { + for (int i = 0; i < front_space; i++) clog << " "; + clog << "Author: " << segment << " "; + line_length += (segment.length()+9); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+4; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+9+front_space+back_space); + } + } + clog << endl; + + if (!usages.empty()) + { + for (int i = 0; i < front_space; i++) clog << " "; + clog << "Usage:" << endl; + for (int i = 0; i < usages.size(); i++) + { + ss_message.clear(); ss_message.str(usages[i]); + + line_length = front_space + back_space + 4; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space+back_space+4)) + { + for (int i = 0; i < front_space+4; i++) clog << " "; + clog << segment << " "; + line_length += (segment.length()+1); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+9; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+10+front_space+back_space); + } + } + clog << endl; + } + } + + if (!options.empty()) + { + for (int i = 0; i < front_space; i++) clog << " "; + clog << "Options:" << endl; + for (int i = 0; i < options.size(); i++) + { + if (options[i].flag_l == "") + { + full_message = options[i].flag_s+" : "+options[i].message; + ss_message.clear(); ss_message.str(full_message); + + line_length = front_space + back_space + 4; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space+back_space+4)) + { + for (int i = 0; i < front_space+4; i++) clog << " "; + clog << segment << " "; + line_length += (segment.length()+1); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+9; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+10+front_space+back_space); + } + } + clog << endl; + + if (!options[i].sec_message.empty()) + { + for (int j = 0; j < options[i].sec_message.size(); j++) + { + ss_message.clear(); ss_message.str(options[i].sec_message[j]); + + line_length = front_space + back_space + 9; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space+back_space+9)) + { + for (int i = 0; i < front_space+9; i++) clog << " "; + clog << segment << " "; + line_length += (segment.length()+1); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+13; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+14+front_space+back_space); + } + } + clog << endl; + } + } + } + else + { + full_message = options[i].flag_s+" "+options[i].flag_l+" "+options[i].message; + ss_message.clear(); ss_message.str(full_message); + + line_length = front_space + back_space + 4; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space+back_space+4)) + { + for (int i = 0; i < front_space+4; i++) clog << " "; + clog << segment << " "; + line_length += (segment.length()+1); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+9; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+10+front_space+back_space); + } + } + clog << endl; + + if (!options[i].sec_message.empty()) + { + for (int j = 0; j < options[i].sec_message.size(); j++) + { + ss_message.clear(); ss_message.str(options[i].sec_message[j]); + + line_length = front_space + back_space + 9; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space+back_space+9)) + { + for (int i = 0; i < front_space+9; i++) clog << " "; + clog << segment << " "; + line_length += (segment.length()+1); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+13; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+14+front_space+back_space); + } + } + clog << endl; + } + } + } + } + } + + if (!examples.empty()) + { + for (int i = 0; i < front_space; i++) clog << " "; + clog << "Examples:" << endl; + for (int i = 0; i < examples.size(); i++) + { + ss_message.clear(); ss_message.str(examples[i]); + + line_length = front_space + back_space + 4; + while(ss_message >> segment) + { + if ((line_length+segment.length()+1) <= w.ws_col) + { + if (line_length == (front_space+back_space+4)) + { + for (int i = 0; i < front_space+4; i++) clog << " "; + clog << segment << " "; + line_length += (segment.length()+1); + } + else + { + clog << segment << " "; + line_length += (segment.length()+1); + } + } + else + { + clog << endl; + for (int i = 0; i < front_space+9; i++) clog << " "; + clog << segment << " "; + line_length = (segment.length()+10+front_space+back_space); + } + } + clog << endl; + } + } + return; +} +#endif \ No newline at end of file diff --git a/src/tool/grd2tin.cpp b/src/tool/grd2tin.cpp index 6e50cb8..5378966 100644 --- a/src/tool/grd2tin.cpp +++ b/src/tool/grd2tin.cpp @@ -1,4 +1,5 @@ #include "../lib/tin.h" +#include "display_help.h" #include "iostream" #include "fstream" @@ -13,39 +14,264 @@ void display_help(std::string exe_name) { - std::clog << exe_name << " - Generating the Triangular Irregular Networks (TIN) from regular DEM grids\n\n"; - std::clog << "Usage: -z -r/// -i/ -m [-p] [-t] [-l] [-n]\n\n"; - std::clog << "Options:\n"; - std::clog << " -z --xyz-file\tinput DEM grid file.\n"; - std::clog << " -r --range\tinput DEM grid's range.\n"; - std::clog << " -i --interval\tinput DEM grid's interval.\n"; - std::clog << " -m --mesh-file\toutput a Gmsh's .msh file of the generated TIN.\n"; - std::clog << " -p --poly-file\tinput a polygon file to control the outline shape of the generated TIN.\n"; - std::clog << " -t --threshold\tthreshold of the maximal error of the generated TIN with respect to the input DEM grid.\n"; - std::clog << " -l --log-file\toutput a .log file of the maximal error of the generated TIN.\n"; - std::clog << " -n --neighbor-file\toutput a .neigh file of the maximal error of the generated TIN.\n"; + dispHelp dh; + dh.changeLayerOut(0, 10); + dh.addHeadInfo(exe_name, "1.0", "Generating the Triangular Irregular Networks (TIN) from regular DEM grids.", + "Yi Zhang (zhangyiss@icloud.com)"); + dh.addUsage(exe_name+" -f -r/// -i/ -m [-p] [-t] [-l] [-n] [-z]"); + dh.addOption("Input DEM grid file.", "-f", "--grid-file"); + dh.addOption("Input DEM grid's range.", "-r", "--range"); + dh.addOption("Input DEM grid's interval.", "-i", "--interval"); + dh.addOption("Output Gmsh file (.msh) of the generated TIN.", "-m", "--mesh-file"); + dh.addOption("Input text file of a polygon to control the outline shape of the generated TIN.", "-p", "--polygon-file"); + dh.addOption("Threshold of the maximal error of the generated TIN with respect to the input DEM grid. (Default is 1.0)", "-t", "--threshold"); + dh.addOption("Output text file of a log file of the maximal error of the generated TIN.", "-l", "--log-file"); + dh.addOption("Output text file of neighborships of the generated TIN.", "-n", "--neighbor-file"); + dh.addOption("The input DEM grid is a 1-column z-table.", "-z", "--z-table"); + dh.addOption("Display help information.", "-h", "--help"); + dh.show(); return; } -int main(int argc, char const *argv[]) +int main(int argc, char* argv[]) { - static struct option long_opts[] = + try { - {"xyz-file", required_argument, NULL, 'z'}, - {"range", required_argument, NULL, 'r'}, - {"interval", required_argument, NULL, 'i'}, - {"mesh-file", required_argument, NULL, 'm'}, - {"poly-file", required_argument, NULL, 'p'}, - {"threshold", required_argument, NULL, 't'}, - {"log-file", required_argument, NULL, 'l'}, - {"neighbor-file", required_argument, NULL, 'n'}, - {0, 0, 0, 0}, - }; + static struct option long_opts[] = + { + {"grid-file", required_argument, NULL, 'f'}, + {"range", required_argument, NULL, 'r'}, + {"interval", required_argument, NULL, 'i'}, + {"mesh-file", required_argument, NULL, 'm'}, + {"polygon-file", required_argument, NULL, 'p'}, + {"threshold", required_argument, NULL, 't'}, + {"log-file", required_argument, NULL, 'l'}, + {"neighbor-file", required_argument, NULL, 'n'}, + {"z-table", no_argument, NULL, 'z'}, + {"help", no_argument, NULL, 'h'}, + {0, 0, 0, 0}, + }; - if (argc == 1) + if (argc == 1) + { + display_help(argv[0]); + return 0; + } + + double range[4] = {0, 1000, 0, 1000}; + double interval[2] = {10, 10}; + double threshold = 1.0; + std::string grid_file, mesh_file, poly_file, log_file, neigh_file; + grid_file = mesh_file = "NULL"; + poly_file = log_file = neigh_file = "NULL"; + bool z_table = false; + + int curr; + while (1) + { + int optIndex = 0; + + curr = getopt_long(argc, argv, "zhf:r:i:m:p:t:l:n:", long_opts, &optIndex); + + if (curr == -1) break; + + switch (curr) + { + case 'h': + display_help(argv[0]); + return 0; + case 'z': + z_table = true; + break; + case 'f': + grid_file = optarg; break; + case 'r': + if (4 != sscanf(optarg, "%lf/%lf/%lf/%lf", &range[0], &range[1], &range[2], &range[3])) + { + throw "Invalid range parameters"; + } + break; + case 'i': + if (2 != sscanf(optarg, "%lf/%lf", &interval[0], &interval[1])) //格式化读入参数 + { + throw "Invalid interval parameters"; + } + break; + case 'm': + mesh_file = optarg; break; + case 'p': + poly_file = optarg; break; + case 't': + if (1 != sscanf(optarg,"%lf", &threshold)) //格式化读入参数 + { + throw "Invalid threshold"; + } + break; + case 'l': + log_file = optarg; break; + case 'n': + neigh_file = optarg; break; + case '?': + display_help(argv[0]); + return 0; + default: + abort(); + } + } + + if (grid_file == "NULL" || mesh_file == "NULL") + { + throw "No input grid text file or Gmsh mesh file"; + } + + // Prepare DEM parameters + int xnum = round((range[1] - range[0])/interval[0]) + 1; + int ynum = round((range[3] - range[2])/interval[1]) + 1; + std::vector topo(xnum*ynum); + + // Read DEM grid + double tmp_d; + std::ifstream infile; + + infile.open(grid_file); + if (z_table) + { + for (int i = 0; i < xnum*ynum; i++) + { + infile >> topo[i]; + } + } + else + { + for (int i = 0; i < xnum*ynum; i++) + { + infile >> tmp_d >> tmp_d >> topo[i]; + } + } + infile.close(); + + // Read Polygon file + int tmp_count; + double tmp_x, tmp_y; + std::vector poly_vert; + if (poly_file != "NULL") + { + infile.open(poly_file); + infile >> tmp_count; + poly_vert.resize(tmp_count); + for (int i = 0; i < tmp_count; i++) + { + infile >> tmp_x >> tmp_y; + poly_vert[i].set(tmp_x, tmp_y, 0.0, i); + } + infile.close(); + } + + std::vector err_records; + std::vector tin_vert; + std::vector tin_ele; + if (poly_file == "NULL" && log_file == "NULL") + { + grd2tin(topo, range[0], range[1], range[2], range[3], interval[0], interval[1], + tin_vert, tin_ele, threshold, nullptr, nullptr); + } + else if (poly_file == "NULL") + { + grd2tin(topo, range[0], range[1], range[2], range[3], interval[0], interval[1], + tin_vert, tin_ele, threshold, nullptr, &err_records); + } + else if (log_file == "NULL") + { + grd2tin(topo, range[0], range[1], range[2], range[3], interval[0], interval[1], + tin_vert, tin_ele, threshold, &poly_vert, nullptr); + } + else + { + grd2tin(topo, range[0], range[1], range[2], range[3], interval[0], interval[1], + tin_vert, tin_ele, threshold, &poly_vert, &err_records); + } + + // Write a log file + if (log_file != "NULL") + { + std::ofstream logfile(log_file); + logfile << "# Insertion Maxi-Error\n"; + for (int i = 0; i < err_records.size(); ++i) + { + logfile << i+1 << " " << err_records[i] << std::endl; + } + logfile.close(); + } + + // Write a Gmsh's .msh file + std::ofstream outfile(mesh_file); + outfile << "$MeshFormat" << std::endl << "2.2 0 8" << std::endl << "$EndMeshFormat "<id + 1 << " " << std::setprecision(16) + << tin_vert[i]->x << " " << tin_vert[i]->y << " " << tin_vert[i]->elev << std::endl; + } + outfile<<"$EndNodes"<vert[j]->id + 1; + } + outfile << std::endl; + } + outfile << "$EndElements"<< std::endl; + outfile<<"$NodeData"<id + 1 << " " << std::setprecision(16) << tin_vert[i]->elev << std::endl; + } + outfile << "$EndNodeData" << std::endl; + outfile.close(); + + // write a neighbor file + if (neigh_file != "NULL") + { + outfile.open(neigh_file); + outfile << tin_ele.size() << std::endl; + for (int i = 0; i < tin_ele.size(); i++) + { + outfile << i + 1; + for (int j = 0; j < 3; j++) + { + if (tin_ele[i]->neigh[j] != nullptr) + { + outfile << " " << tin_ele[i]->neigh[j]->id + 1; + } + else outfile << " -1"; + } + outfile << std::endl; + } + outfile.close(); + } + + // Destroy memories allocated by the dem2tin function + for (int i = 0; i < tin_vert.size(); ++i) + { + delete tin_vert[i]; + } + + for (int i = 0; i < tin_ele.size(); ++i) + { + delete tin_ele[i]; + } + } + catch(const char* err_char) { - display_help(argv[0]); - return 0; + std::cerr << err_char << '\n'; } return 0; } diff --git a/src/tool/rnd2tin.cpp b/src/tool/rnd2tin.cpp index 2111da7..0b14fb6 100644 --- a/src/tool/rnd2tin.cpp +++ b/src/tool/rnd2tin.cpp @@ -1,4 +1,5 @@ #include "../lib/tin.h" +#include "display_help.h" #include "iostream" #include "fstream" @@ -13,35 +14,229 @@ void display_help(std::string exe_name) { - std::clog << exe_name << " - Generating the Triangular Irregular Networks (TIN) from random DEM points\n\n"; - std::clog << "Usage: -z -m [-p] [-t] [-l] [-n]\n\n"; - std::clog << "Options:\n"; - std::clog << " -z --xyz-file\tinput .xyz file.\n"; - std::clog << " -m --mesh-file\toutput a Gmsh's .msh file of the generated TIN.\n"; - std::clog << " -p --poly-file\tinput a polygon file to control the outline shape of the generated TIN.\n"; - std::clog << " -t --threshold\tthreshold of the maximal error of the generated TIN with respect to the input DEM grid.\n"; - std::clog << " -l --log-file\toutput a .log file of the maximal error of the generated TIN.\n"; - std::clog << " -n --neighbor-file\toutput a .neigh file of the maximal error of the generated TIN.\n"; + dispHelp dh; + dh.changeLayerOut(0, 10); + dh.addHeadInfo(exe_name, "1.0", "Generating the Triangular Irregular Networks (TIN) from random DEM points.", + "Yi Zhang (zhangyiss@icloud.com)"); + dh.addUsage(exe_name+" -f -m [-p] [-t] [-l] [-n] [-z]"); + dh.addOption("Input DEM xyz file.", "-f", "--xyz-file"); + dh.addOption("Output Gmsh file (.msh) of the generated TIN.", "-m", "--mesh-file"); + dh.addOption("Input text file of a polygon to control the outline shape of the generated TIN.", "-p", "--polygon-file"); + dh.addOption("Threshold of the maximal error of the generated TIN with respect to the input DEM grid. (Default is 1.0)", "-t", "--threshold"); + dh.addOption("Output text file of a log file of the maximal error of the generated TIN.", "-l", "--log-file"); + dh.addOption("Output text file of neighborships of the generated TIN.", "-n", "--neighbor-file"); + dh.addOption("Display help information.", "-h", "--help"); + dh.show(); return; } -int main(int argc, char const *argv[]) +int main(int argc, char* argv[]) { - static struct option long_opts[] = + try { - {"xyz-file", required_argument, NULL, 'z'}, - {"mesh-file", required_argument, NULL, 'm'}, - {"poly-file", required_argument, NULL, 'p'}, - {"threshold", required_argument, NULL, 't'}, - {"log-file", required_argument, NULL, 'l'}, - {"neighbor-file", required_argument, NULL, 'n'}, - {0, 0, 0, 0}, - }; + static struct option long_opts[] = + { + {"xyz-file", required_argument, NULL, 'f'}, + {"mesh-file", required_argument, NULL, 'm'}, + {"polygon-file", required_argument, NULL, 'p'}, + {"threshold", required_argument, NULL, 't'}, + {"log-file", required_argument, NULL, 'l'}, + {"neighbor-file", required_argument, NULL, 'n'}, + {"help", no_argument, NULL, 'h'}, + {0, 0, 0, 0}, + }; - if (argc == 1) + if (argc == 1) + { + display_help(argv[0]); + return 0; + } + + double threshold = 1.0; + std::string xyz_file, mesh_file, poly_file, log_file, neigh_file; + xyz_file = mesh_file = "NULL"; + poly_file = log_file = neigh_file = "NULL"; + bool z_table = false; + + int curr; + while (1) + { + int optIndex = 0; + + curr = getopt_long(argc, argv, "hf:m:p:t:l:n:", long_opts, &optIndex); + + if (curr == -1) break; + + switch (curr) + { + case 'h': + display_help(argv[0]); + return 0; + case 'z': + z_table = true; + break; + case 'f': + xyz_file = optarg; break; + case 'm': + mesh_file = optarg; break; + case 'p': + poly_file = optarg; break; + case 't': + if (1 != sscanf(optarg,"%lf", &threshold)) //格式化读入参数 + { + throw "Invalid threshold"; + } + break; + case 'l': + log_file = optarg; break; + case 'n': + neigh_file = optarg; break; + case '?': + display_help(argv[0]); + return 0; + default: + abort(); + } + } + + if (xyz_file == "NULL" || mesh_file == "NULL") + { + throw "No input grid text file or Gmsh mesh file"; + } + + // Prepare DEM parameters + + + // Read DEM grid + dem_point tmp_dem; + std::vector topo; + std::ifstream infile; + + infile.open(xyz_file); + while (infile >> tmp_dem.x >> tmp_dem.y >> tmp_dem.elev) + { + topo.push_back(tmp_dem); + } + infile.close(); + + // Read Polygon file + int tmp_count; + double tmp_x, tmp_y; + std::vector poly_vert; + if (poly_file != "NULL") + { + infile.open(poly_file); + infile >> tmp_count; + poly_vert.resize(tmp_count); + for (int i = 0; i < tmp_count; i++) + { + infile >> tmp_x >> tmp_y; + poly_vert[i].set(tmp_x, tmp_y, 0.0, i); + } + infile.close(); + } + + std::vector err_records; + std::vector tin_vert; + std::vector tin_ele; + if (poly_file == "NULL" && log_file == "NULL") + { + rnd2tin(topo, tin_vert, tin_ele, threshold, nullptr, nullptr); + } + else if (poly_file == "NULL") + { + rnd2tin(topo, tin_vert, tin_ele, threshold, nullptr, &err_records); + } + else if (log_file == "NULL") + { + rnd2tin(topo, tin_vert, tin_ele, threshold, &poly_vert, nullptr); + } + else + { + rnd2tin(topo, tin_vert, tin_ele, threshold, &poly_vert, &err_records); + } + + // Write a log file + if (log_file != "NULL") + { + std::ofstream logfile(log_file); + logfile << "# Insertion Maxi-Error\n"; + for (int i = 0; i < err_records.size(); ++i) + { + logfile << i+1 << " " << err_records[i] << std::endl; + } + logfile.close(); + } + + // Write a Gmsh's .msh file + std::ofstream outfile(mesh_file); + outfile << "$MeshFormat" << std::endl << "2.2 0 8" << std::endl << "$EndMeshFormat "<id + 1 << " " << std::setprecision(16) + << tin_vert[i]->x << " " << tin_vert[i]->y << " " << tin_vert[i]->elev << std::endl; + } + outfile<<"$EndNodes"<vert[j]->id + 1; + } + outfile << std::endl; + } + outfile << "$EndElements"<< std::endl; + outfile<<"$NodeData"<id + 1 << " " << std::setprecision(16) << tin_vert[i]->elev << std::endl; + } + outfile << "$EndNodeData" << std::endl; + outfile.close(); + + // write a neighbor file + if (neigh_file != "NULL") + { + outfile.open(neigh_file); + outfile << tin_ele.size() << std::endl; + for (int i = 0; i < tin_ele.size(); i++) + { + outfile << i + 1; + for (int j = 0; j < 3; j++) + { + if (tin_ele[i]->neigh[j] != nullptr) + { + outfile << " " << tin_ele[i]->neigh[j]->id + 1; + } + else outfile << " -1"; + } + outfile << std::endl; + } + outfile.close(); + } + + // Destroy memories allocated by the dem2tin function + for (int i = 0; i < tin_vert.size(); ++i) + { + delete tin_vert[i]; + } + + for (int i = 0; i < tin_ele.size(); ++i) + { + delete tin_ele[i]; + } + } + catch(const char* err_char) { - display_help(argv[0]); - return 0; + std::cerr << err_char << '\n'; } return 0; -} +} \ No newline at end of file