OGS
computeSurfaceNodeIDsInPolygonalRegion.cpp File Reference

Detailed Description

Computes mesh node ids of mesh nodes within a polygonal region, that resides on the surface.

Definition in file computeSurfaceNodeIDsInPolygonalRegion.cpp.

#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/MPI.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 71 of file computeSurfaceNodeIDsInPolygonalRegion.cpp.

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

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(), 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 34 of file computeSurfaceNodeIDsInPolygonalRegion.cpp.

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

References OGS_FATAL.

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