28 std::vector<MeshLib::Node*>
const& nodes,
29 double const& max_dist)
31 double sqr_shortest_dist(max_dist);
32 double elevation(p[2]);
35 double sqr_dist = (p[0] - (*node)[0]) * (p[0] - (*node)[0]) +
36 (p[1] - (*node)[1]) * (p[1] - (*node)[1]);
37 if (sqr_dist < sqr_shortest_dist)
39 sqr_shortest_dist = sqr_dist;
40 elevation = (*node)[2];
46int main(
int argc,
char* argv[])
49 "Changes the elevation of 2D mesh nodes based on either raster data or "
50 "another 2D mesh. In addition, a low pass filter can be applied to "
51 "node elevation based connected nodes.\n\n"
52 "OpenGeoSys-6 software, version " +
55 "Copyright (c) 2012-2026, OpenGeoSys Community "
56 "(http://www.opengeosys.org)",
59 cmd.add(log_level_arg);
60 TCLAP::SwitchArg lowpass_arg(
62 "Applies a lowpass filter to elevation over connected nodes.",
false);
64 TCLAP::ValueArg<double> map_static_arg(
66 "Static elevation to map the input file to. This can be combined with "
67 "mapping based on rasters or other meshes to deal with locations where "
68 "no corresponding data exists.",
69 false, 0,
"MAP_STATIC");
70 cmd.add(map_static_arg);
71 TCLAP::ValueArg<double> max_dist_arg(
73 "Maximum distance to search for mesh nodes if there is no "
74 "corresponding data for input mesh nodes on the mesh it should be "
75 "mapped on. (min = 0)",
76 false, 1,
"MAX_DISTANCE");
77 cmd.add(max_dist_arg);
78 TCLAP::ValueArg<std::string> map_mesh_arg(
79 "m",
"mesh",
"Input (.vtu). 2D mesh file to map the input file on",
80 false,
"",
"INPUT_FILE");
81 cmd.add(map_mesh_arg);
82 TCLAP::ValueArg<std::string> map_raster_arg(
84 "Input (.asc | .grd | .xyz) Raster file to map the input file on",
85 false,
"",
"INPUT_FILE");
86 cmd.add(map_raster_arg);
87 TCLAP::ValueArg<std::string> output_arg(
88 "o",
"output",
"Output (.vtu) mesh file",
true,
"",
"OUTPUT_FILE");
90 TCLAP::ValueArg<std::string> input_arg(
91 "i",
"input",
"Input (.vtu | .msh) mesh file",
true,
"",
"INPUT_FILE");
93 cmd.parse(argc, argv);
98 std::unique_ptr<MeshLib::Mesh> mesh(
102 ERR(
"Error reading mesh file.");
106 if (!(map_static_arg.isSet() || map_raster_arg.isSet() ||
107 map_mesh_arg.isSet()))
109 ERR(
"Nothing to do. Please choose mapping based on a raster or mesh "
110 "file, or to a static value.");
114 if (map_raster_arg.isSet() && map_mesh_arg.isSet())
116 ERR(
"Please select mapping based on *either* a mesh or a raster file.");
121 if (map_static_arg.isSet())
124 *mesh, map_static_arg.getValue());
128 if (map_raster_arg.isSet())
130 std::string
const raster_path = map_raster_arg.getValue();
131 std::ifstream file_stream(raster_path, std::ifstream::in);
132 if (!file_stream.good())
134 ERR(
"Opening raster file {} failed.", raster_path);
139 std::unique_ptr<GeoLib::Raster>
const raster(
145 if (map_mesh_arg.isSet())
147 std::unique_ptr<MeshLib::Mesh> ground_truth(
149 if (ground_truth ==
nullptr)
151 ERR(
"Error reading mesh file.");
155 std::vector<MeshLib::Node*>
const& nodes = mesh->getNodes();
157 auto const edgeLengths = minMaxEdgeLength(mesh->getElements());
158 double const max_edge = edgeLengths.second;
159 double const max_dist(pow(max_dist_arg.getValue(), 2));
164 (*node)[1] - max_edge,
165 -std::numeric_limits<double>::max()}};
167 (*node)[1] + max_edge,
168 std::numeric_limits<double>::max()}};
169 std::vector<const MeshLib::Element*>
const& elems =
171 auto const* element =
174 if (element !=
nullptr)
182 *node, ground_truth->getNodes(), max_dist);
190 if (lowpass_arg.isSet())
193 const std::size_t nNodes(mesh->getNumberOfNodes());
194 std::vector<MeshLib::Node*> nodes(mesh->getNodes());
196 std::vector<double> elevation(nNodes);
197 for (std::size_t i = 0; i < nNodes; i++)
199 elevation[i] = (*nodes[i])[2];
202 auto const& connections =
204 for (std::size_t i = 0; i < nNodes; i++)
206 auto const& conn_nodes(connections[nodes[i]->getID()]);
207 const unsigned nConnNodes(conn_nodes.size());
208 elevation[i] = (2 * (*nodes[i])[2]);
209 for (std::size_t j = 0; j < nConnNodes; ++j)
211 elevation[i] += (*conn_nodes[j])[2];
213 elevation[i] /= (nConnNodes + 2);
216 for (std::size_t i = 0; i < nNodes; i++)
218 (*nodes[i])[2] = elevation[i];
227 INFO(
"Result successfully written.");