OGS
createLayeredMeshFromRasters.cpp
Go to the documentation of this file.
1/*
2 * \file
3 * \date 2016-02-11
4 * \brief Creates a layered mesh from a 2D mesh and a bunch of raster files.
5 *
6 * \copyright
7 * Copyright (c) 2012-2024, OpenGeoSys Community (http://www.opengeosys.org)
8 * Distributed under a Modified BSD License.
9 * See accompanying file LICENSE.txt or
10 * http://www.opengeosys.org/project/license
11 */
12
13#include <tclap/CmdLine.h>
14
15#include <algorithm>
16#include <iterator>
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "BaseLib/FileTools.h"
23#include "BaseLib/MPI.h"
25#include "InfoLib/GitInfo.h"
28#include "MeshLib/Mesh.h"
30
31int main(int argc, char* argv[])
32{
33 TCLAP::CmdLine cmd(
34 "Creates a layered 3D OGS mesh from an existing 2D OGS mesh and a list "
35 "of raster files representing subsurface layers. Supported raster "
36 "formats are ArcGIS ascii rasters (*.asc), Surfer Grids (*.grd), or "
37 "gridded XYZ rasters (*.xyz)."
38 "Only input meshes consisting of line and triangle elements are "
39 "currently supported as mapping of quads might result in invalid mesh "
40 "elements.\n\n"
41 "OpenGeoSys-6 software, version " +
43 ".\n"
44 "Copyright (c) 2012-2024, OpenGeoSys Community "
45 "(http://www.opengeosys.org)",
47
48 TCLAP::SwitchArg use_ascii_arg("", "ascii_output",
49 "Write VTU output in ASCII format.");
50 cmd.add(use_ascii_arg);
51
52 double min_thickness(std::numeric_limits<double>::epsilon());
53 TCLAP::ValueArg<double> min_thickness_arg(
54 "t", "thickness",
55 "The minimum thickness of a layer to be integrated at any given "
56 "location.",
57 false, min_thickness, "floating point number");
58 cmd.add(min_thickness_arg);
59
60 TCLAP::ValueArg<std::string> raster_path_arg(
61 "r", "raster-list",
62 "An ascii-file containing a list of raster files, starting from top "
63 "(DEM) to bottom.",
64 true, "", "file name");
65 cmd.add(raster_path_arg);
66
67 TCLAP::ValueArg<std::string> mesh_out_arg(
68 "o", "output-mesh-file", "The file name of the resulting 3D mesh.",
69 true, "", "file name");
70 cmd.add(mesh_out_arg);
71
72 TCLAP::ValueArg<std::string> mesh_arg("i", "input-mesh-file",
73 "The file name of the 2D input mesh.",
74 true, "", "file name");
75 cmd.add(mesh_arg);
76
77 cmd.parse(argc, argv);
78
79 BaseLib::MPI::Setup mpi_setup(argc, argv);
80
81 if (min_thickness_arg.isSet())
82 {
83 min_thickness = min_thickness_arg.getValue();
84 if (min_thickness < 0)
85 {
86 ERR("Minimum layer thickness must be non-negative value.");
87 return EXIT_FAILURE;
88 }
89 }
90
91 INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue());
92 std::unique_ptr<MeshLib::Mesh> const sfc_mesh(
93 MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
94 if (!sfc_mesh)
95 {
96 ERR("Error reading mesh '{:s}'.", mesh_arg.getValue());
97 return EXIT_FAILURE;
98 }
99 if (sfc_mesh->getDimension() != 2)
100 {
101 ERR("Input mesh must be a 2D mesh.");
102 return EXIT_FAILURE;
103 }
104 INFO("done.");
105
106 std::vector<std::string> raster_paths =
107 BaseLib::IO::readStringListFromFile(raster_path_arg.getValue());
108 if (raster_paths.size() < 2)
109 {
110 ERR("At least two raster files needed to create 3D mesh.");
111 return EXIT_FAILURE;
112 }
113 std::reverse(raster_paths.begin(), raster_paths.end());
114
116 if (auto const rasters = FileIO::readRasters(raster_paths))
117 {
118 if (!mapper.createLayers(*sfc_mesh, *rasters, min_thickness))
119 {
120 return EXIT_FAILURE;
121 }
122 }
123 else
124 {
125 ERR("Reading raster files.");
126 return EXIT_FAILURE;
127 }
128
129 std::string output_name(mesh_out_arg.getValue());
130 if (!BaseLib::hasFileExtension(".vtu", output_name))
131 {
132 output_name.append(".vtu");
133 }
134
135 INFO("Writing mesh '{:s}' ... ", output_name);
136 auto const result_mesh = mapper.getMesh("SubsurfaceMesh");
137 if (result_mesh == nullptr)
138 {
139 ERR("Mapper returned empty result for 'SubsurfaceMesh'.");
140 return EXIT_FAILURE;
141 }
142
143 auto const data_mode =
144 use_ascii_arg.getValue() ? vtkXMLWriter::Ascii : vtkXMLWriter::Binary;
145
146 MeshLib::IO::writeVtu(*result_mesh, output_name, data_mode);
147 INFO("done.");
148
149 return EXIT_SUCCESS;
150}
Definition of the AsciiRasterInterface class.
Filename manipulation routines.
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the MeshLayerMapper class.
Definition of the Mesh class.
Implementation of the VtuInterface class.
virtual bool createLayers(MeshLib::Mesh const &mesh, std::vector< GeoLib::Raster const * > const &rasters, double minimum_thickness, double noDataReplacementValue=0.0) final
std::unique_ptr< MeshLib::Mesh > getMesh(std::string const &mesh_name) const
Returns a mesh of the subsurface representation.
Manipulating and adding prism element layers to an existing 2D mesh.
int main(int argc, char *argv[])
std::vector< std::string > readStringListFromFile(std::string const &filename)
Reads non-empty lines from a list of strings from a file into a vector.
bool hasFileExtension(std::string const &extension, std::string const &filename)
std::optional< std::vector< GeoLib::Raster const * > > readRasters(std::vector< std::string > const &raster_paths)
GITINFOLIB_EXPORT const std::string ogs_version
int writeVtu(MeshLib::Mesh const &mesh, std::string const &file_name, int const data_mode)
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
Definition of readMeshFromFile function.