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