OGS
createLayeredMeshFromRasters.cpp File Reference
#include <tclap/CmdLine.h>
#include <algorithm>
#include <iterator>
#include <memory>
#include <string>
#include <vector>
#include "Applications/FileIO/AsciiRasterInterface.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/IO/readStringListFromFile.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/VtkIO/VtuInterface.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/MeshGenerators/MeshLayerMapper.h"
Include dependency graph for createLayeredMeshFromRasters.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 29 of file createLayeredMeshFromRasters.cpp.

30 {
31  TCLAP::CmdLine cmd(
32  "Creates a layered 3D OGS mesh from an existing 2D OGS mesh and a list "
33  "of raster files representing subsurface layers. Supported raster "
34  "formats are ArcGIS ascii rasters (*.asc) and Surfer Grids (*.grd). "
35  "Only input meshes consisting of line and triangle elements are "
36  "currently supported as mapping of quads might result in invalid mesh "
37  "elements.\n\n"
38  "OpenGeoSys-6 software, version " +
40  ".\n"
41  "Copyright (c) 2012-2021, OpenGeoSys Community "
42  "(http://www.opengeosys.org)",
44 
45  TCLAP::SwitchArg use_ascii_arg("", "ascii_output",
46  "Write VTU output in ASCII format.");
47  cmd.add(use_ascii_arg);
48 
49  double min_thickness(std::numeric_limits<double>::epsilon());
50  TCLAP::ValueArg<double> min_thickness_arg(
51  "t", "thickness",
52  "The minimum thickness of a layer to be integrated at any given "
53  "location.",
54  false, min_thickness, "floating point number");
55  cmd.add(min_thickness_arg);
56 
57  TCLAP::ValueArg<std::string> raster_path_arg(
58  "r", "raster-list",
59  "An ascii-file containing a list of raster files, starting from top "
60  "(DEM) to bottom.",
61  true, "", "file name");
62  cmd.add(raster_path_arg);
63 
64  TCLAP::ValueArg<std::string> mesh_out_arg(
65  "o", "output-mesh-file", "The file name of the resulting 3D mesh.",
66  true, "", "file name");
67  cmd.add(mesh_out_arg);
68 
69  TCLAP::ValueArg<std::string> mesh_arg("i", "input-mesh-file",
70  "The file name of the 2D input mesh.",
71  true, "", "file name");
72  cmd.add(mesh_arg);
73 
74  cmd.parse(argc, argv);
75 
76  if (min_thickness_arg.isSet())
77  {
78  min_thickness = min_thickness_arg.getValue();
79  if (min_thickness < 0)
80  {
81  ERR("Minimum layer thickness must be non-negative value.");
82  return EXIT_FAILURE;
83  }
84  }
85 
86  INFO("Reading mesh '{:s}' ... ", mesh_arg.getValue());
87  std::unique_ptr<MeshLib::Mesh> const sfc_mesh(
88  MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
89  if (!sfc_mesh)
90  {
91  ERR("Error reading mesh '{:s}'.", mesh_arg.getValue());
92  return EXIT_FAILURE;
93  }
94  if (sfc_mesh->getDimension() != 2)
95  {
96  ERR("Input mesh must be a 2D mesh.");
97  return EXIT_FAILURE;
98  }
99  INFO("done.");
100 
101  std::vector<std::string> raster_paths =
102  BaseLib::IO::readStringListFromFile(raster_path_arg.getValue());
103  if (raster_paths.size() < 2)
104  {
105  ERR("At least two raster files needed to create 3D mesh.");
106  return EXIT_FAILURE;
107  }
108  std::reverse(raster_paths.begin(), raster_paths.end());
109 
111  if (auto const rasters = FileIO::readRasters(raster_paths))
112  {
113  if (!mapper.createLayers(*sfc_mesh, *rasters, min_thickness))
114  {
115  return EXIT_FAILURE;
116  }
117  }
118  else
119  {
120  return EXIT_FAILURE;
121  }
122 
123  std::string output_name(mesh_out_arg.getValue());
124  if (!BaseLib::hasFileExtension(".vtu", output_name))
125  {
126  output_name.append(".vtu");
127  }
128 
129  INFO("Writing mesh '{:s}' ... ", output_name);
130  auto const result_mesh = mapper.getMesh("SubsurfaceMesh");
131  if (result_mesh == nullptr)
132  {
133  ERR("Mapper returned empty result for 'SubsurfaceMesh'.");
134  return EXIT_FAILURE;
135  }
136 
137  auto const data_mode =
138  use_ascii_arg.getValue() ? vtkXMLWriter::Ascii : vtkXMLWriter::Binary;
139 
140  MeshLib::IO::writeVtu(*result_mesh, output_name, data_mode);
141  INFO("done.");
142 
143  return EXIT_SUCCESS;
144 }
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
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.
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)
Definition: FileTools.cpp:191
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)

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