OGS
MeshMapping.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <tclap/CmdLine.h>
5
6#include <fstream>
7#include <memory>
8#include <string>
9
10#include "BaseLib/FileTools.h"
11#include "BaseLib/Logging.h"
12#include "BaseLib/MPI.h"
14#include "GeoLib/AABB.h"
16#include "GeoLib/Raster.h"
17#include "InfoLib/GitInfo.h"
18#include "MathLib/MathTools.h"
21#include "MeshLib/Mesh.h"
23#include "MeshLib/Node.h"
26
28 std::vector<MeshLib::Node*> const& nodes,
29 double const& max_dist)
30{
31 double sqr_shortest_dist(max_dist);
32 double elevation(p[2]);
33 for (MeshLib::Node* node : nodes)
34 {
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)
38 {
39 sqr_shortest_dist = sqr_dist;
40 elevation = (*node)[2];
41 }
42 }
43 return elevation;
44}
45
46int main(int argc, char* argv[])
47{
48 TCLAP::CmdLine cmd(
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 " +
54 ".\n"
55 "Copyright (c) 2012-2026, OpenGeoSys Community "
56 "(http://www.opengeosys.org)",
58 auto log_level_arg = BaseLib::makeLogLevelArg();
59 cmd.add(log_level_arg);
60 TCLAP::SwitchArg lowpass_arg(
61 "", "lowpass",
62 "Applies a lowpass filter to elevation over connected nodes.", false);
63 cmd.add(lowpass_arg);
64 TCLAP::ValueArg<double> map_static_arg(
65 "s", "static",
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(
72 "d", "distance",
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(
83 "r", "raster",
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");
89 cmd.add(output_arg);
90 TCLAP::ValueArg<std::string> input_arg(
91 "i", "input", "Input (.vtu | .msh) mesh file", true, "", "INPUT_FILE");
92 cmd.add(input_arg);
93 cmd.parse(argc, argv);
94
95 BaseLib::MPI::Setup mpi_setup(argc, argv);
96 BaseLib::initOGSLogger(log_level_arg.getValue());
97
98 std::unique_ptr<MeshLib::Mesh> mesh(
99 MeshLib::IO::readMeshFromFile(input_arg.getValue()));
100 if (mesh == nullptr)
101 {
102 ERR("Error reading mesh file.");
103 return EXIT_FAILURE;
104 }
105
106 if (!(map_static_arg.isSet() || map_raster_arg.isSet() ||
107 map_mesh_arg.isSet()))
108 {
109 ERR("Nothing to do. Please choose mapping based on a raster or mesh "
110 "file, or to a static value.");
111 return EXIT_FAILURE;
112 }
113
114 if (map_raster_arg.isSet() && map_mesh_arg.isSet())
115 {
116 ERR("Please select mapping based on *either* a mesh or a raster file.");
117 return EXIT_FAILURE;
118 }
119
120 // Maps the elevation of mesh nodes to static value
121 if (map_static_arg.isSet())
122 {
124 *mesh, map_static_arg.getValue());
125 }
126
127 // Maps the elevation of mesh nodes according to raster
128 if (map_raster_arg.isSet())
129 {
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())
133 {
134 ERR("Opening raster file {} failed.", raster_path);
135 return EXIT_FAILURE;
136 }
137 file_stream.close();
138
139 std::unique_ptr<GeoLib::Raster> const raster(
141 MeshToolsLib::MeshLayerMapper::layerMapping(*mesh, *raster, 0, true);
142 }
143
144 // Maps the elevation of mesh nodes according to a ground truth mesh
145 if (map_mesh_arg.isSet())
146 {
147 std::unique_ptr<MeshLib::Mesh> ground_truth(
148 MeshLib::IO::readMeshFromFile(map_mesh_arg.getValue()));
149 if (ground_truth == nullptr)
150 {
151 ERR("Error reading mesh file.");
152 return EXIT_FAILURE;
153 }
154
155 std::vector<MeshLib::Node*> const& nodes = mesh->getNodes();
156 MeshLib::MeshElementGrid const grid(*ground_truth);
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));
160
161 for (MeshLib::Node* node : nodes)
162 {
163 MathLib::Point3d min_vol{{(*node)[0] - max_edge,
164 (*node)[1] - max_edge,
165 -std::numeric_limits<double>::max()}};
166 MathLib::Point3d max_vol{{(*node)[0] + max_edge,
167 (*node)[1] + max_edge,
168 std::numeric_limits<double>::max()}};
169 std::vector<const MeshLib::Element*> const& elems =
170 grid.getElementsInVolume(min_vol, max_vol);
171 auto const* element =
173 *node);
174 if (element != nullptr)
175 {
177 *element, *node);
178 }
179 else
180 {
181 (*node)[2] = getClosestPointElevation(
182 *node, ground_truth->getNodes(), max_dist);
183 }
184 }
185 }
186
187 // a simple lowpass filter for the elevation of mesh nodes using the
188 // elevation of each node weighted by 2 and the elevation of each connected
189 // node weighted by 1
190 if (lowpass_arg.isSet())
191 {
192 INFO("lowpass");
193 const std::size_t nNodes(mesh->getNumberOfNodes());
194 std::vector<MeshLib::Node*> nodes(mesh->getNodes());
195
196 std::vector<double> elevation(nNodes);
197 for (std::size_t i = 0; i < nNodes; i++)
198 {
199 elevation[i] = (*nodes[i])[2];
200 }
201
202 auto const& connections =
204 for (std::size_t i = 0; i < nNodes; i++)
205 {
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)
210 {
211 elevation[i] += (*conn_nodes[j])[2];
212 }
213 elevation[i] /= (nConnNodes + 2);
214 }
215
216 for (std::size_t i = 0; i < nNodes; i++)
217 {
218 (*nodes[i])[2] = elevation[i];
219 }
220 }
221
222 if (MeshLib::IO::writeMeshToFile(*mesh, output_arg.getValue()) != 0)
223 {
224 return EXIT_FAILURE;
225 }
226
227 INFO("Result successfully written.");
228 return EXIT_SUCCESS;
229}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
int main(int argc, char *argv[])
double getClosestPointElevation(MeshLib::Node const &p, std::vector< MeshLib::Node * > const &nodes, double const &max_dist)
static GeoLib::Raster * readRaster(std::string const &fname)
std::vector< MeshLib::Element const * > getElementsInVolume(POINT const &min, POINT const &max) const
static bool layerMapping(MeshLib::Mesh const &mesh, const GeoLib::Raster &raster, double nodata_replacement=0.0, bool const ignore_nodata=false)
static bool mapToStaticValue(MeshLib::Mesh const &mesh, double value)
Maps the elevation of all mesh nodes to the specified static value.
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
std::vector< std::vector< Node * > > calculateNodesConnectedByElements(Mesh const &mesh)
Definition Mesh.cpp:298
double getElevation(MeshLib::Element const &element, MathLib::Point3d const &node)
MeshLib::Element const * getProjectedElement(std::vector< const MeshLib::Element * > const &elements, MathLib::Point3d const &node)