OGS
Layers2Grid.cpp
Go to the documentation of this file.
1
10#include <algorithm>
11#include <memory>
12#include <string>
13#include <vector>
14
15// ThirdParty
16#include <tclap/CmdLine.h>
17
18#ifdef USE_PETSC
19#include <mpi.h>
20#endif
21
23#include "GeoLib/AABB.h"
24#include "InfoLib/GitInfo.h"
25#include "MathLib/Point3d.h"
30
31int main(int argc, char* argv[])
32{
33 TCLAP::CmdLine cmd(
34 "Reads a list of 2D unstructured mesh layers and samples them onto a "
35 "structured grid of the same extent. Note, that a large cube size may "
36 "result in an undersampling of the original structure.\nCube sizes are "
37 "defines by x/y/z-parameters. For equilateral cubes, only the "
38 "x-parameter needs to be set.\n\n"
39 "OpenGeoSys-6 software, version " +
41 ".\n"
42 "Copyright (c) 2012-2024, OpenGeoSys Community "
43 "(http://www.opengeosys.org)",
45 TCLAP::SwitchArg dilate_arg(
46 "d", "dilate",
47 "assign mat IDs based on single nodes instead of a majority of nodes, "
48 "which can result in a slightly increased voxel grid extent",
49 false);
50 cmd.add(dilate_arg);
51
52 TCLAP::ValueArg<double> z_arg("z", "cellsize-z",
53 "edge length of cubes in z-direction (depth)",
54 false, 1000, "floating point number");
55 cmd.add(z_arg);
56
57 TCLAP::ValueArg<double> y_arg(
58 "y", "cellsize-y", "edge length of cubes in y-direction (latitude)",
59 false, 1000, "floating point number");
60 cmd.add(y_arg);
61
62 TCLAP::ValueArg<double> x_arg(
63 "x", "cellsize-x",
64 "edge length of cubes in x-direction (longitude) or all directions, if "
65 "y and z are not set",
66 true, 1000, "floating point number");
67 cmd.add(x_arg);
68
69 TCLAP::ValueArg<std::string> output_arg(
70 "o", "output", "name of output mesh (*.vtu)", true, "", "string");
71 cmd.add(output_arg);
72
73 TCLAP::ValueArg<std::string> input_arg(
74 "i", "input",
75 "name of the input file list containing the paths the all input layers "
76 "in correct order from top to bottom",
77 true, "", "string");
78 cmd.add(input_arg);
79 cmd.parse(argc, argv);
80
81#ifdef USE_PETSC
82 MPI_Init(&argc, &argv);
83#endif
84
85 if ((y_arg.isSet() && !z_arg.isSet()) ||
86 ((!y_arg.isSet() && z_arg.isSet())))
87 {
88 ERR("For equilateral cubes, only x needs to be set. For unequal "
89 "cuboids, all three edge lengths (x/y/z) need to be specified.");
90#ifdef USE_PETSC
91 MPI_Finalize();
92#endif
93 return EXIT_FAILURE;
94 }
95
96 double const x_size = x_arg.getValue();
97 double const y_size = (y_arg.isSet()) ? y_arg.getValue() : x_arg.getValue();
98 double const z_size = (z_arg.isSet()) ? z_arg.getValue() : x_arg.getValue();
99 std::array<double, 3> const cellsize = {x_size, y_size, z_size};
100
101 std::string const input_name = input_arg.getValue();
102 std::string const output_name = output_arg.getValue();
103 auto const layer_names = BaseLib::IO::readStringListFromFile(input_name);
104 if (layer_names.size() < 2)
105 {
106 ERR("At least two layers are required to create a 3D Mesh");
107#ifdef USE_PETSC
108 MPI_Finalize();
109#endif
110 return EXIT_FAILURE;
111 }
112
113 std::vector<std::unique_ptr<MeshLib::Mesh>> layers;
114 layers.reserve(layer_names.size());
115 constexpr double minval = std::numeric_limits<double>::max();
116 constexpr double maxval = std::numeric_limits<double>::lowest();
117 std::pair<MathLib::Point3d, MathLib::Point3d> extent(
118 MathLib::Point3d{{minval, minval, minval}},
119 MathLib::Point3d{{maxval, maxval, maxval}});
120
121 for (auto const& layer : layer_names)
122 {
123 auto mesh(MeshLib::IO::readMeshFromFile(layer));
124 if (mesh == nullptr)
125 {
126 ERR("Input layer '{:s}' not found. Aborting...", layer);
127#ifdef USE_PETSC
128 MPI_Finalize();
129#endif
130 return EXIT_FAILURE;
131 }
132 layers.emplace_back(mesh);
133 }
134 std::vector<MeshLib::Mesh const*> layers_ptr;
135 std::transform(std::begin(layers), std::end(layers),
136 std::back_inserter(layers_ptr),
137 [](auto const& layer) { return layer.get(); });
138 bool const dilate = dilate_arg.getValue();
140 createVoxelFromLayeredMesh(extent, layers_ptr, cellsize, dilate);
141 if (mesh == nullptr)
142 {
143 ERR("The VoxelGrid could not be created.");
144#ifdef USE_PETSC
145 MPI_Finalize();
146#endif
147 return EXIT_FAILURE;
148 }
149 MeshLib::IO::VtuInterface vtu(mesh.get());
150 vtu.writeToFile(output_name);
151#ifdef USE_PETSC
152 MPI_Finalize();
153#endif
154 return EXIT_SUCCESS;
155}
Definition of the AABB class.
Definition of the Element class.
Git information.
int main(int argc, char *argv[])
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the Point3d class.
Implementation of the VtuInterface class.
Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures....
bool writeToFile(std::filesystem::path const &file_path)
std::vector< std::string > readStringListFromFile(std::string const &filename)
Reads non-empty lines from a list of strings from a file into a vector.
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
std::unique_ptr< MeshLib::Mesh > createVoxelFromLayeredMesh(std::pair< MathLib::Point3d, MathLib::Point3d > &extent, std::vector< MeshLib::Mesh const * > const &layers, std::array< double, 3 > const cellsize, bool const dilate)
Definition of readMeshFromFile function.