OGS
createTetgenSmeshFromRasters.cpp
Go to the documentation of this file.
1
13#include <tclap/CmdLine.h>
14
15#include <algorithm>
16#include <iterator>
17#include <memory>
18#include <string>
19#include <vector>
20
22#include "BaseLib/FileTools.h"
24#include "BaseLib/Logging.h"
25#include "BaseLib/MPI.h"
28#include "InfoLib/GitInfo.h"
31#include "MeshLib/Mesh.h"
34
35int main(int argc, char* argv[])
36{
37 TCLAP::CmdLine cmd(
38 "Creates a boundary representation for a layered 3D mesh in "
39 "*.smesh-format. This boundary representation can be used with the "
40 "TetGen Tetrahedral Mesh Generator to create 3D meshes of the given "
41 "geometry at arbitrary resolutions and with varying properties. "
42 "Details on command line switches and possible parametrisation can be "
43 "found in the TetGen User's Manual. Supported raster formats are "
44 "ArcGIS ascii rasters (*.asc), Surfer Grids (*.grd) and XYZ raster "
45 "files (*.xyz)."
46 "Only input meshes consisting of line and triangle elements are "
47 "currently supported as mapping of quads might result in invalid mesh "
48 "elements.\n\n"
49 "OpenGeoSys-6 software, version " +
51 ".\n"
52 "Copyright (c) 2012-2025, OpenGeoSys Community "
53 "(http://www.opengeosys.org)",
55
56 TCLAP::SwitchArg use_ascii_arg("", "ascii_output",
57 "Write VTU output in ASCII format.");
58 cmd.add(use_ascii_arg);
59
60 double min_thickness(std::numeric_limits<double>::epsilon());
61 TCLAP::ValueArg<double> min_thickness_arg(
62 "t", "thickness",
63 "The minimum thickness of a layer to be integrated at any given "
64 "location (min = 0)",
65 false, min_thickness, "MIN_THICKNESS");
66 cmd.add(min_thickness_arg);
67
68 TCLAP::ValueArg<std::string> raster_path_arg(
69 "r", "raster-list",
70 "Input (.vtu). An ascii-file containing a list of raster files, "
71 "starting from top "
72 "(DEM) to bottom.",
73 true, "", "INPUT_FILE_LIST");
74 cmd.add(raster_path_arg);
75
76 TCLAP::ValueArg<std::string> mesh_out_arg(
77 "o", "output-mesh-file",
78 "Output (.smesh). The file name of the resulting 3D mesh", true, "",
79 "OUTPUT_FILE");
80 cmd.add(mesh_out_arg);
81
82 TCLAP::ValueArg<std::string> mesh_arg(
83 "i", "input-mesh-file",
84 "Input (.vtu | .msh). The file name of the 2D input mesh", true, "",
85 "INPUT_FILE");
86 cmd.add(mesh_arg);
87
88 auto log_level_arg = BaseLib::makeLogLevelArg();
89 cmd.add(log_level_arg);
90
91 cmd.parse(argc, argv);
92
93 BaseLib::MPI::Setup mpi_setup(argc, argv);
94 BaseLib::initOGSLogger(log_level_arg.getValue());
95
96 if (min_thickness_arg.isSet())
97 {
98 min_thickness = min_thickness_arg.getValue();
99 if (min_thickness < 0)
100 {
101 ERR("Minimum layer thickness must be non-negative value.");
102 return EXIT_FAILURE;
103 }
104 }
105
106 INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue());
107 std::unique_ptr<MeshLib::Mesh> const sfc_mesh(MeshLib::IO::readMeshFromFile(
108 mesh_arg.getValue(), true /* compute_element_neighbors */));
109 if (!sfc_mesh)
110 {
111 ERR("Error reading mesh '{:s}'.", mesh_arg.getValue());
112 return EXIT_FAILURE;
113 }
114 if (sfc_mesh->getDimension() != 2)
115 {
116 ERR("Input mesh must be a 2D mesh.");
117 return EXIT_FAILURE;
118 }
119 INFO("done.");
120
121 std::vector<std::string> raster_paths =
122 BaseLib::IO::readStringListFromFile(raster_path_arg.getValue());
123 if (raster_paths.size() < 2)
124 {
125 ERR("At least two raster files needed to create 3D mesh.");
126 return EXIT_FAILURE;
127 }
128 std::reverse(raster_paths.begin(), raster_paths.end());
129
130 std::string output_name(mesh_out_arg.getValue());
131 if (!BaseLib::hasFileExtension(".smesh", output_name))
132 {
133 output_name.append(".smesh");
134 }
135
136 auto const rasters = FileIO::readRasters(raster_paths);
137 LayeredVolume lv;
138 if (rasters)
139 {
140 if (!lv.createLayers(*sfc_mesh, *rasters, min_thickness))
141 {
142 ERR("Creating the layers was erroneous.");
143 return EXIT_FAILURE;
144 }
145 }
146 else
147 {
148 ERR("The raster files are not accessible.");
149 return EXIT_FAILURE;
150 }
151 std::unique_ptr<MeshLib::Mesh> tg_mesh =
152 lv.getMesh("BoundaryRepresentation");
153 if (tg_mesh != nullptr)
154 {
155 std::vector<MeshLib::Node> tg_attr(lv.getAttributePoints());
156 FileIO::TetGenInterface tetgen_interface;
157 tetgen_interface.writeTetGenSmesh(output_name, *tg_mesh, tg_attr);
158 INFO("Smesh was successfully written.");
159 }
160 else
161 {
162 ERR("The tetgen-smesh could not be created.");
163 return EXIT_FAILURE;
164 }
165
166 return EXIT_SUCCESS;
167}
Definition of the AsciiRasterInterface class.
Filename manipulation routines.
Git information.
Definition of the LayeredVolume class.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
Definition of the MeshLayerMapper class.
Definition of the Mesh class.
Definition of the TetGenInterface class.
Implementation of the VtuInterface class.
static bool writeTetGenSmesh(const std::string &file_name, const GeoLib::GEOObjects &geo_objects, const std::string &geo_name, const std::vector< GeoLib::Point > &attribute_points)
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.
Creates a volume geometry from 2D mesh layers based on raster data.
std::vector< MeshLib::Node > getAttributePoints() const
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.
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:64
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
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
Definition of readMeshFromFile function.