OGS
computeSurfaceNodeIDsInPolygonalRegion.cpp File Reference
#include <tclap/CmdLine.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <memory>
#include <string>
#include <vector>
#include "Applications/FileIO/readGeometryFromFile.h"
#include "BaseLib/Error.h"
#include "BaseLib/FileTools.h"
#include "BaseLib/Logging.h"
#include "BaseLib/MPI.h"
#include "BaseLib/TCLAPArguments.h"
#include "GeoLib/GEOObjects.h"
#include "GeoLib/Polygon.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Node.h"
#include "MeshToolsLib/MeshSurfaceExtraction.h"
Include dependency graph for computeSurfaceNodeIDsInPolygonalRegion.cpp:

Go to the source code of this file.

Functions

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)
int main (int argc, char *argv[])

Function Documentation

◆ main()

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

Definition at line 64 of file computeSurfaceNodeIDsInPolygonalRegion.cpp.

65{
66 TCLAP::CmdLine cmd(
67 "Computes ids of mesh nodes that are in polygonal regions and resides "
68 "on the top surface. The polygonal regions have to be given in a gml- "
69 "or gli-file. The found mesh nodes and the associated area are written "
70 "as txt and csv data. The documentation is available at "
71 "https://docs.opengeosys.org/docs/tools/model-preparation/"
72 "computesurfacenodeidsinpolygonalregion.\n\n"
73 "OpenGeoSys-6 software, version " +
75 ".\n"
76 "Copyright (c) 2012-2026, OpenGeoSys Community "
77 "(http://www.opengeosys.org)",
79 TCLAP::ValueArg<std::string> mesh_in(
80 "m", "mesh-input-file",
81 "Input (.vtu). The name of the file containing the input mesh", true,
82 "", "INPUT_FILE");
83 cmd.add(mesh_in);
84 TCLAP::ValueArg<std::string> geo_in(
85 "g", "geo-file",
86 "Input (.gml). The name of the input file containing the polygons",
87 true, "", "INPUT_FILE");
88 cmd.add(geo_in);
89
90 TCLAP::ValueArg<std::string> gmsh_path_arg(
91 "", "gmsh-path", "Input (.msh). The path to the input binary file",
92 false, "", "INPUT_FILE");
93 cmd.add(gmsh_path_arg);
94 auto log_level_arg = BaseLib::makeLogLevelArg();
95 cmd.add(log_level_arg);
96 cmd.parse(argc, argv);
97
98 BaseLib::MPI::Setup mpi_setup(argc, argv);
99 BaseLib::initOGSLogger(log_level_arg.getValue());
100
101 std::unique_ptr<MeshLib::Mesh const> mesh(
102 MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
103 INFO("Mesh read: {:d} nodes, {:d} elements.", mesh->getNumberOfNodes(),
104 mesh->getNumberOfElements());
105
106 GeoLib::GEOObjects geo_objs;
107 FileIO::readGeometryFromFile(geo_in.getValue(), geo_objs,
108 gmsh_path_arg.getValue());
109 auto const geo_name = geo_objs.getGeometryNames()[0];
110 INFO("Geometry '{:s}' read: {:d} points, {:d} polylines.",
111 geo_name,
112 geo_objs.getPointVec(geo_name)->size(),
113 geo_objs.getPolylineVec(geo_name)->size());
114
115 Eigen::Vector3d const dir({0.0, 0.0, -1.0});
116 double angle(90);
117
118 auto computeElementTopSurfaceAreas =
119 [](MeshLib::Mesh const& mesh, Eigen::Vector3d const& d, double angle)
120 {
121 std::unique_ptr<MeshLib::Mesh> surface_mesh(
123 angle));
125 *surface_mesh);
126 };
127
128 std::vector<double> areas(computeElementTopSurfaceAreas(*mesh, dir, angle));
129 std::vector<MeshLib::Node*> all_sfc_nodes(
131 angle));
132
133 std::for_each(all_sfc_nodes.begin(), all_sfc_nodes.end(),
134 [](MeshLib::Node* p) { (*p)[2] = 0.0; });
135
136 std::vector<MeshLib::Node*> const& mesh_nodes(mesh->getNodes());
137 GeoLib::PolylineVec const* ply_vec(geo_objs.getPolylineVecObj(geo_name));
138 auto const& plys(ply_vec->getVector());
139
140 for (std::size_t j(0); j < plys.size(); j++)
141 {
142 if (!plys[j]->isClosed())
143 {
144 continue;
145 }
146 std::string polygon_name;
147 ply_vec->getNameOfElement(plys[j], polygon_name);
148 if (polygon_name.empty())
149 {
150 polygon_name = "Polygon-" + std::to_string(j);
151 }
152 // create Polygon from Polyline
153 GeoLib::Polygon const polygon{*plys[j]};
154 // ids of mesh nodes on surface that are within the given polygon
155 std::vector<std::pair<std::size_t, double>> ids_and_areas;
156 for (std::size_t k(0); k < all_sfc_nodes.size(); k++)
157 {
158 MeshLib::Node const& surface_node(*(all_sfc_nodes[k]));
159 if (polygon.isPntInPolygon(surface_node))
160 {
161 ids_and_areas.emplace_back(surface_node.getID(), areas[k]);
162 }
163 }
164 if (ids_and_areas.empty())
165 {
166 ERR("Polygonal part of surface '{:s}' doesn't contains nodes. No "
167 "output will be generated.",
168 polygon_name);
169 continue;
170 }
171
172 std::string const out_path(BaseLib::extractPath(geo_in.getValue()));
173 std::string id_and_area_fname(out_path + polygon_name);
174 std::string csv_fname(out_path + polygon_name);
175 id_and_area_fname += std::to_string(j) + ".txt";
176 csv_fname += std::to_string(j) + ".csv";
177 INFO(
178 "Polygonal part of surface '{}' contains {} nodes. Writing to "
179 "files '{}' and '{}'.",
180 polygon_name,
181 ids_and_areas.size(),
182 id_and_area_fname,
183 csv_fname);
184 writeToFile(id_and_area_fname, csv_fname, ids_and_areas, mesh_nodes);
185 }
186
187 std::for_each(all_sfc_nodes.begin(), all_sfc_nodes.end(),
188 std::default_delete<MeshLib::Node>());
189
190 return EXIT_SUCCESS;
191}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Container class for geometric objects.
Definition GEOObjects.h:46
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
bool isPntInPolygon(MathLib::Point3d const &pnt) const
Definition Polygon.cpp:194
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:97
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.
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)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
std::string extractPath(std::string const &pathname)
void readGeometryFromFile(std::string const &fname, GeoLib::GEOObjects &geo_objs, std::string const &gmsh_path)
TemplateVec< GeoLib::Polyline > PolylineVec
class PolylineVec encapsulate a std::vector of Polylines additional one can give the vector of polyli...
Definition PolylineVec.h:16
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)

References ERR(), BaseLib::extractPath(), GeoLib::GEOObjects::getGeometryNames(), MathLib::Point3dWithID::getID(), MeshToolsLib::MeshSurfaceExtraction::getMeshSurface(), GeoLib::TemplateVec< T >::getNameOfElement(), GeoLib::GEOObjects::getPointVec(), GeoLib::GEOObjects::getPolylineVec(), GeoLib::GEOObjects::getPolylineVecObj(), MeshToolsLib::MeshSurfaceExtraction::getSurfaceAreaForNodes(), MeshToolsLib::MeshSurfaceExtraction::getSurfaceNodes(), GeoLib::TemplateVec< T >::getVector(), INFO(), BaseLib::initOGSLogger(), GeoLib::Polygon::isPntInPolygon(), BaseLib::makeLogLevelArg(), GitInfoLib::GitInfo::ogs_version, FileIO::readGeometryFromFile(), MeshLib::IO::readMeshFromFile(), and writeToFile().

◆ writeToFile()

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 )

Definition at line 27 of file computeSurfaceNodeIDsInPolygonalRegion.cpp.

31{
32 std::ofstream ids_and_area_out(id_area_fname);
33 if (!ids_and_area_out)
34 {
35 OGS_FATAL("Unable to open the file '{:s}' - aborting.", id_area_fname);
36 }
37 std::ofstream csv_out(csv_fname);
38 if (!csv_out)
39 {
40 OGS_FATAL("Unable to open the file '{:s}' - aborting.", csv_fname);
41 }
42
43 ids_and_area_out << std::setprecision(20);
44 csv_out << std::setprecision(20);
45
46 ids_and_area_out << ids_and_areas[0].first << " "
47 << ids_and_areas[0].second;
48 csv_out << "ID x y z area node_id\n"; // CSV header
49 csv_out << 0 << " " << *mesh_nodes[ids_and_areas[0].first]
50 << ids_and_areas[0].second << " " << ids_and_areas[0].first;
51 for (std::size_t k(1); k < ids_and_areas.size(); k++)
52 {
53 ids_and_area_out << "\n"
54 << ids_and_areas[k].first << " "
55 << ids_and_areas[k].second;
56 csv_out << "\n"
57 << k << " " << *mesh_nodes[ids_and_areas[k].first]
58 << ids_and_areas[k].second << " " << ids_and_areas[k].first;
59 }
60 ids_and_area_out << "\n";
61 csv_out << "\n";
62}
#define OGS_FATAL(...)
Definition Error.h:19

References OGS_FATAL.

Referenced by GeoTabWidget::GeoTabWidget(), MeshTabWidget::MeshTabWidget(), StationTabWidget::StationTabWidget(), and main().