37 std::vector<MeshLib::Node*>
const& nodes,
38 double const& max_dist)
40 double sqr_shortest_dist(max_dist);
41 double elevation(p[2]);
44 double sqr_dist = (p[0] - (*node)[0]) * (p[0] - (*node)[0]) +
45 (p[1] - (*node)[1]) * (p[1] - (*node)[1]);
46 if (sqr_dist < sqr_shortest_dist)
48 sqr_shortest_dist = sqr_dist;
49 elevation = (*node)[2];
55int main(
int argc,
char* argv[])
58 MPI_Init(&argc, &argv);
61 "Changes the elevation of 2D mesh nodes based on either raster data or "
62 "another 2D mesh. In addition, a low pass filter can be applied to "
63 "node elevation based connected nodes.\n\n"
64 "OpenGeoSys-6 software, version " +
67 "Copyright (c) 2012-2024, OpenGeoSys Community "
68 "(http://www.opengeosys.org)",
70 TCLAP::SwitchArg lowpass_arg(
72 "Applies a lowpass filter to elevation over connected nodes.",
false);
74 TCLAP::ValueArg<double> map_static_arg(
76 "Static elevation to map the input file to. This can be combined with "
77 "mapping based on rasters or other meshes to deal with locations where "
78 "no corresponding data exists.",
80 cmd.add(map_static_arg);
81 TCLAP::ValueArg<double> max_dist_arg(
83 "Maximum distance to search for mesh nodes if there is no "
84 "corresponding data for input mesh nodes on the mesh it should be "
85 "mapped on. (Default value: 1)",
87 cmd.add(max_dist_arg);
88 TCLAP::ValueArg<std::string> map_mesh_arg(
89 "m",
"mesh",
"2D mesh file (*.vtu) to map the input file on.",
false,
91 cmd.add(map_mesh_arg);
92 TCLAP::ValueArg<std::string> map_raster_arg(
94 "Raster file (*.asc *.grd *.xyz) to map the input file on.",
false,
"",
96 cmd.add(map_raster_arg);
97 TCLAP::ValueArg<std::string> output_arg(
98 "o",
"output",
"Output mesh file (*.vtu)",
true,
"",
"string");
100 TCLAP::ValueArg<std::string> input_arg(
101 "i",
"input",
"Input mesh file (*.vtu, *.msh)",
true,
"",
"string");
103 cmd.parse(argc, argv);
105 std::unique_ptr<MeshLib::Mesh> mesh(
109 ERR(
"Error reading mesh file.");
116 if (!(map_static_arg.isSet() || map_raster_arg.isSet() ||
117 map_mesh_arg.isSet()))
119 ERR(
"Nothing to do. Please choose mapping based on a raster or mesh "
120 "file, or to a static value.");
127 if (map_raster_arg.isSet() && map_mesh_arg.isSet())
129 ERR(
"Please select mapping based on *either* a mesh or a raster file.");
137 if (map_static_arg.isSet())
140 *mesh, map_static_arg.getValue());
144 if (map_raster_arg.isSet())
146 std::string
const raster_path = map_raster_arg.getValue();
147 std::ifstream file_stream(raster_path, std::ifstream::in);
148 if (!file_stream.good())
150 ERR(
"Opening raster file {} failed.", raster_path);
158 std::unique_ptr<GeoLib::Raster>
const raster(
164 if (map_mesh_arg.isSet())
166 std::unique_ptr<MeshLib::Mesh> ground_truth(
168 if (ground_truth ==
nullptr)
170 ERR(
"Error reading mesh file.");
177 std::vector<MeshLib::Node*>
const& nodes = mesh->getNodes();
179 auto const edgeLengths = minMaxEdgeLength(mesh->getElements());
180 double const max_edge = edgeLengths.second;
181 double const max_dist(pow(max_dist_arg.getValue(), 2));
186 (*node)[1] - max_edge,
187 -std::numeric_limits<double>::max()}};
189 (*node)[1] + max_edge,
190 std::numeric_limits<double>::max()}};
191 std::vector<const MeshLib::Element*>
const& elems =
193 auto const* element =
196 if (element !=
nullptr)
204 *node, ground_truth->getNodes(), max_dist);
212 if (lowpass_arg.isSet())
215 const std::size_t nNodes(mesh->getNumberOfNodes());
216 std::vector<MeshLib::Node*> nodes(mesh->getNodes());
218 std::vector<double> elevation(nNodes);
219 for (std::size_t i = 0; i < nNodes; i++)
221 elevation[i] = (*nodes[i])[2];
224 auto const& connections =
226 for (std::size_t i = 0; i < nNodes; i++)
228 auto const& conn_nodes(connections[nodes[i]->getID()]);
229 const unsigned nConnNodes(conn_nodes.size());
230 elevation[i] = (2 * (*nodes[i])[2]);
231 for (std::size_t j = 0; j < nConnNodes; ++j)
233 elevation[i] += (*conn_nodes[j])[2];
235 elevation[i] /= (nConnNodes + 2);
238 for (std::size_t i = 0; i < nNodes; i++)
240 (*nodes[i])[2] = elevation[i];
252 INFO(
"Result successfully written.");