OGS
createTetgenSmeshFromRasters.cpp File Reference

Detailed Description

Creates a boundary representation for a layered 3D mesh.

Date
2023-04-25

Definition in file createTetgenSmeshFromRasters.cpp.

#include <tclap/CmdLine.h>
#include <algorithm>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
#include "Applications/FileIO/TetGenInterface.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/IO/readStringListFromFile.h"
#include "BaseLib/MPI.h"
#include "GeoLib/IO/AsciiRasterInterface.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshToolsLib/MeshGenerators/LayeredVolume.h"
#include "MeshToolsLib/MeshGenerators/MeshLayerMapper.h"
Include dependency graph for createTetgenSmeshFromRasters.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 33 of file createTetgenSmeshFromRasters.cpp.

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

References LayeredMeshGenerator::createLayers(), ERR(), LayeredVolume::getAttributePoints(), LayeredMeshGenerator::getMesh(), BaseLib::hasFileExtension(), INFO(), GitInfoLib::GitInfo::ogs_version, MeshLib::IO::readMeshFromFile(), FileIO::readRasters(), BaseLib::IO::readStringListFromFile(), and FileIO::TetGenInterface::writeTetGenSmesh().