34 std::vector<MeshLib::Node*>
const& nodes,
35 double const& max_dist)
37 double sqr_shortest_dist(max_dist);
38 double elevation(p[2]);
41 double sqr_dist = (p[0] - (*node)[0]) * (p[0] - (*node)[0]) +
42 (p[1] - (*node)[1]) * (p[1] - (*node)[1]);
43 if (sqr_dist < sqr_shortest_dist)
45 sqr_shortest_dist = sqr_dist;
46 elevation = (*node)[2];
52int main(
int argc,
char* argv[])
56 "Changes the elevation of 2D mesh nodes based on either raster data or "
57 "another 2D mesh. In addition, a low pass filter can be applied to "
58 "node elevation based connected nodes.\n\n"
59 "OpenGeoSys-6 software, version " +
62 "Copyright (c) 2012-2024, OpenGeoSys Community "
63 "(http://www.opengeosys.org)",
65 TCLAP::SwitchArg lowpass_arg(
67 "Applies a lowpass filter to elevation over connected nodes.",
false);
69 TCLAP::ValueArg<double> map_static_arg(
71 "Static elevation to map the input file to. This can be combined with "
72 "mapping based on rasters or other meshes to deal with locations where "
73 "no corresponding data exists.",
75 cmd.add(map_static_arg);
76 TCLAP::ValueArg<double> max_dist_arg(
78 "Maximum distance to search for mesh nodes if there is no "
79 "corresponding data for input mesh nodes on the mesh it should be "
80 "mapped on. (Default value: 1)",
82 cmd.add(max_dist_arg);
83 TCLAP::ValueArg<std::string> map_mesh_arg(
84 "m",
"mesh",
"2D mesh file (*.vtu) to map the input file on.",
false,
86 cmd.add(map_mesh_arg);
87 TCLAP::ValueArg<std::string> map_raster_arg(
89 "Raster file (*.asc *.grd *.xyz) to map the input file on.",
false,
"",
91 cmd.add(map_raster_arg);
92 TCLAP::ValueArg<std::string> output_arg(
93 "o",
"output",
"Output mesh file (*.vtu)",
true,
"",
"string");
95 TCLAP::ValueArg<std::string> input_arg(
96 "i",
"input",
"Input mesh file (*.vtu, *.msh)",
true,
"",
"string");
98 cmd.parse(argc, argv);
100 std::unique_ptr<MeshLib::Mesh> mesh(
104 ERR(
"Error reading mesh file.");
108 if (!(map_static_arg.isSet() || map_raster_arg.isSet() ||
109 map_mesh_arg.isSet()))
111 ERR(
"Nothing to do. Please choose mapping based on a raster or mesh "
112 "file, or to a static value.");
116 if (map_raster_arg.isSet() && map_mesh_arg.isSet())
118 ERR(
"Please select mapping based on *either* a mesh or a raster file.");
123 if (map_static_arg.isSet())
126 *mesh, map_static_arg.getValue());
130 if (map_raster_arg.isSet())
132 std::string
const raster_path = map_raster_arg.getValue();
133 std::ifstream file_stream(raster_path, std::ifstream::in);
134 if (!file_stream.good())
136 ERR(
"Opening raster file {} failed.", raster_path);
141 std::unique_ptr<GeoLib::Raster>
const raster(
147 if (map_mesh_arg.isSet())
149 std::unique_ptr<MeshLib::Mesh> ground_truth(
151 if (ground_truth ==
nullptr)
153 ERR(
"Error reading mesh file.");
157 std::vector<MeshLib::Node*>
const& nodes = mesh->getNodes();
159 auto const edgeLengths = minMaxEdgeLength(mesh->getElements());
160 double const max_edge = edgeLengths.second;
161 double const max_dist(pow(max_dist_arg.getValue(), 2));
166 (*node)[1] - max_edge,
167 -std::numeric_limits<double>::max()}};
169 (*node)[1] + max_edge,
170 std::numeric_limits<double>::max()}};
171 std::vector<const MeshLib::Element*>
const& elems =
173 auto const* element =
176 if (element !=
nullptr)
184 *node, ground_truth->getNodes(), max_dist);
192 if (lowpass_arg.isSet())
195 const std::size_t nNodes(mesh->getNumberOfNodes());
196 std::vector<MeshLib::Node*> nodes(mesh->getNodes());
198 std::vector<double> elevation(nNodes);
199 for (std::size_t i = 0; i < nNodes; i++)
201 elevation[i] = (*nodes[i])[2];
204 auto const& connections =
206 for (std::size_t i = 0; i < nNodes; i++)
208 auto const& conn_nodes(connections[nodes[i]->getID()]);
209 const unsigned nConnNodes(conn_nodes.size());
210 elevation[i] = (2 * (*nodes[i])[2]);
211 for (std::size_t j = 0; j < nConnNodes; ++j)
213 elevation[i] += (*conn_nodes[j])[2];
215 elevation[i] /= (nConnNodes + 2);
218 for (std::size_t i = 0; i < nNodes; i++)
220 (*nodes[i])[2] = elevation[i];
229 INFO(
"Result successfully written.");