OGS
computeSurfaceNodeIDsInPolygonalRegion.cpp
Go to the documentation of this file.
1
13#include <tclap/CmdLine.h>
14
15#include <algorithm>
16#include <fstream>
17#include <memory>
18#include <string>
19#include <vector>
20
22#include "BaseLib/Error.h"
23#include "BaseLib/FileTools.h"
24#include "BaseLib/MPI.h"
25#include "GeoLib/GEOObjects.h"
26#include "GeoLib/Polygon.h"
27#include "InfoLib/GitInfo.h"
29#include "MeshLib/Mesh.h"
30#include "MeshLib/Node.h"
32
34 std::string const& id_area_fname, std::string const& csv_fname,
35 std::vector<std::pair<std::size_t, double>> const& ids_and_areas,
36 std::vector<MeshLib::Node*> const& mesh_nodes)
37{
38 std::ofstream ids_and_area_out(id_area_fname);
39 if (!ids_and_area_out)
40 {
41 OGS_FATAL("Unable to open the file '{:s}' - aborting.", id_area_fname);
42 }
43 std::ofstream csv_out(csv_fname);
44 if (!csv_out)
45 {
46 OGS_FATAL("Unable to open the file '{:s}' - aborting.", csv_fname);
47 }
48
49 ids_and_area_out << std::setprecision(20);
50 csv_out << std::setprecision(20);
51
52 ids_and_area_out << ids_and_areas[0].first << " "
53 << ids_and_areas[0].second;
54 csv_out << "ID x y z area node_id\n"; // CSV header
55 csv_out << 0 << " " << *mesh_nodes[ids_and_areas[0].first]
56 << ids_and_areas[0].second << " " << ids_and_areas[0].first;
57 for (std::size_t k(1); k < ids_and_areas.size(); k++)
58 {
59 ids_and_area_out << "\n"
60 << ids_and_areas[k].first << " "
61 << ids_and_areas[k].second;
62 csv_out << "\n"
63 << k << " " << *mesh_nodes[ids_and_areas[k].first]
64 << ids_and_areas[k].second << " " << ids_and_areas[k].first;
65 }
66 ids_and_area_out << "\n";
67 csv_out << "\n";
68}
69
70int main(int argc, char* argv[])
71{
72 TCLAP::CmdLine cmd(
73 "Computes ids of mesh nodes that are in polygonal regions and resides "
74 "on the top surface. The polygonal regions have to be given in a gml- "
75 "or gli-file. The found mesh nodes and the associated area are written "
76 "as txt and csv data. The documentation is available at "
77 "https://docs.opengeosys.org/docs/tools/model-preparation/"
78 "computesurfacenodeidsinpolygonalregion.\n\n"
79 "OpenGeoSys-6 software, version " +
81 ".\n"
82 "Copyright (c) 2012-2024, OpenGeoSys Community "
83 "(http://www.opengeosys.org)",
85 TCLAP::ValueArg<std::string> mesh_in(
86 "m", "mesh-input-file",
87 "the name of the file containing the input mesh", true, "",
88 "file name of input mesh");
89 cmd.add(mesh_in);
90 TCLAP::ValueArg<std::string> geo_in(
91 "g", "geo-file", "the name of the gml file containing the polygons",
92 true, "", "file name of input geometry");
93 cmd.add(geo_in);
94
95 TCLAP::ValueArg<std::string> gmsh_path_arg("", "gmsh-path",
96 "the path to the gmsh binary",
97 false, "", "path as string");
98 cmd.add(gmsh_path_arg);
99 cmd.parse(argc, argv);
100
101 BaseLib::MPI::Setup mpi_setup(argc, argv);
102
103 std::unique_ptr<MeshLib::Mesh const> mesh(
104 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
105 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
106 mesh->getNumberOfElements());
107
108 GeoLib::GEOObjects geo_objs;
109 FileIO::readGeometryFromFile(geo_in.getValue(), geo_objs,
110 gmsh_path_arg.getValue());
111 auto const geo_name = geo_objs.getGeometryNames()[0];
112 INFO("Geometry '{:s}' read: {:d} points, {:d} polylines.",
113 geo_name,
114 geo_objs.getPointVec(geo_name)->size(),
115 geo_objs.getPolylineVec(geo_name)->size());
116
117 Eigen::Vector3d const dir({0.0, 0.0, -1.0});
118 double angle(90);
119
120 auto computeElementTopSurfaceAreas =
121 [](MeshLib::Mesh const& mesh, Eigen::Vector3d const& d, double angle)
122 {
123 std::unique_ptr<MeshLib::Mesh> surface_mesh(
125 angle));
127 *surface_mesh);
128 };
129
130 std::vector<double> areas(computeElementTopSurfaceAreas(*mesh, dir, angle));
131 std::vector<MeshLib::Node*> all_sfc_nodes(
133 angle));
134
135 std::for_each(all_sfc_nodes.begin(), all_sfc_nodes.end(),
136 [](MeshLib::Node* p) { (*p)[2] = 0.0; });
137
138 std::vector<MeshLib::Node*> const& mesh_nodes(mesh->getNodes());
139 GeoLib::PolylineVec const* ply_vec(geo_objs.getPolylineVecObj(geo_name));
140 auto const& plys(ply_vec->getVector());
141
142 for (std::size_t j(0); j < plys.size(); j++)
143 {
144 if (!plys[j]->isClosed())
145 {
146 continue;
147 }
148 std::string polygon_name;
149 ply_vec->getNameOfElement(plys[j], polygon_name);
150 if (polygon_name.empty())
151 {
152 polygon_name = "Polygon-" + std::to_string(j);
153 }
154 // create Polygon from Polyline
155 GeoLib::Polygon const polygon{*plys[j]};
156 // ids of mesh nodes on surface that are within the given polygon
157 std::vector<std::pair<std::size_t, double>> ids_and_areas;
158 for (std::size_t k(0); k < all_sfc_nodes.size(); k++)
159 {
160 MeshLib::Node const& surface_node(*(all_sfc_nodes[k]));
161 if (polygon.isPntInPolygon(surface_node))
162 {
163 ids_and_areas.emplace_back(surface_node.getID(), areas[k]);
164 }
165 }
166 if (ids_and_areas.empty())
167 {
168 ERR("Polygonal part of surface '{:s}' doesn't contains nodes. No "
169 "output will be generated.",
170 polygon_name);
171 continue;
172 }
173
174 std::string const out_path(BaseLib::extractPath(geo_in.getValue()));
175 std::string id_and_area_fname(out_path + polygon_name);
176 std::string csv_fname(out_path + polygon_name);
177 id_and_area_fname += std::to_string(j) + ".txt";
178 csv_fname += std::to_string(j) + ".csv";
179 INFO(
180 "Polygonal part of surface '{}' contains {} nodes. Writing to "
181 "files '{}' and '{}'.",
182 polygon_name,
183 ids_and_areas.size(),
184 id_and_area_fname,
185 csv_fname);
186 writeToFile(id_and_area_fname, csv_fname, ids_and_areas, mesh_nodes);
187 }
188
189 std::for_each(all_sfc_nodes.begin(), all_sfc_nodes.end(),
190 std::default_delete<MeshLib::Node>());
191
192 return EXIT_SUCCESS;
193}
#define OGS_FATAL(...)
Definition Error.h:26
Filename manipulation routines.
Definition of the GEOObjects class.
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 MeshSurfaceExtraction class.
Definition of the Mesh class.
Definition of the Node class.
Definition of the Polygon class.
Container class for geometric objects.
Definition GEOObjects.h:57
std::vector< std::string > getGeometryNames() const
Returns the names of all geometry vectors.
const std::vector< Point * > * getPointVec(const std::string &name) const
const PolylineVec * getPolylineVecObj(const std::string &name) const
const std::vector< Polyline * > * getPolylineVec(const std::string &name) const
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
bool getNameOfElement(const T *data, std::string &name) const
std::vector< T * > const & getVector() const
std::size_t getID() const
static std::vector< double > getSurfaceAreaForNodes(const MeshLib::Mesh &mesh)
Returns a vector of the areas assigned to each node on a surface mesh.
static MeshLib::Mesh * getMeshSurface(const MeshLib::Mesh &subsfc_mesh, Eigen::Vector3d const &dir, double angle, std::string_view subsfc_node_id_prop_name="", std::string_view subsfc_element_id_prop_name="", std::string_view face_id_prop_name="")
static std::vector< MeshLib::Node * > getSurfaceNodes(const MeshLib::Mesh &mesh, Eigen::Vector3d const &dir, double angle)
Returns the surface nodes of a mesh.
int main(int argc, char *argv[])
void writeToFile(std::string const &id_area_fname, std::string const &csv_fname, std::vector< std::pair< std::size_t, double > > const &ids_and_areas, std::vector< MeshLib::Node * > const &mesh_nodes)
std::string extractPath(std::string const &pathname)
void readGeometryFromFile(std::string const &fname, GeoLib::GEOObjects &geo_objs, std::string const &gmsh_path)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
Definition of readMeshFromFile function.