OGS 6.1.0-1699-ge946d4c5f
createSurface.cpp
Go to the documentation of this file.
1 
11 #include <cstdio>
12 #include <list>
13 #include <memory>
14 
15 #include <logog/include/logog.hpp>
16 
17 #include "createSurface.h"
18 
21 
22 #include "GeoLib/GEOObjects.h"
23 #include "GeoLib/Point.h"
24 #include "GeoLib/Polygon.h"
25 #include "GeoLib/Polyline.h"
26 #include "GeoLib/Surface.h"
27 
29 #include "MeshLib/Mesh.h"
30 
31 namespace FileIO
32 {
34  GeoLib::GEOObjects& geometries,
35  std::string const& geometry_name)
36 {
37  if (!ply.isClosed())
38  {
39  WARN("Error in createSurface() - Polyline is not closed.");
40  return false;
41  }
42 
43  if (ply.getNumberOfPoints() <= 2)
44  {
45  WARN(
46  "Error in createSurface() - Polyline consists of less "
47  "than three points and therefore cannot be triangulated.");
48  return false;
49  }
50 
51  // create new GEOObjects and insert a copy of the polyline
52  auto polyline_points = std::make_unique<std::vector<GeoLib::Point*>>();
54  auto ply_points = ply.getPointsVec();
55  for (auto p : ply_points)
56  polyline_points->push_back(new GeoLib::Point(*p));
57  std::string ply_name = "temporary_polyline_name";
58  geo.addPointVec(std::move(polyline_points), ply_name);
59  auto polyline =
60  std::make_unique<GeoLib::Polyline>(*geo.getPointVec(ply_name));
61  for (std::size_t k(0); k < ply.getNumberOfPoints(); ++k)
62  polyline->addPoint(ply.getPointID(k));
63  auto polylines = std::make_unique<std::vector<GeoLib::Polyline*>>();
64  polylines->push_back(polyline.release());
65  geo.addPolylineVec(std::move(polylines), ply_name);
66 
67  // use GMSHInterface to create a mesh from the closed polyline
68  std::vector<std::string> geo_names;
69  geo.getGeometryNames(geo_names);
72  0.0, 0, geo_names, false, false);
73  gmsh_io.setPrecision(std::numeric_limits<double>::digits10);
74 
75  char file_base_name_c[L_tmpnam];
76  if (! std::tmpnam(file_base_name_c))
77  {
78  OGS_FATAL("Could not create unique file name.");
79  }
80  std::string const file_base_name(file_base_name_c);
81  gmsh_io.writeToFile(file_base_name + ".geo");
82  std::string gmsh_command =
83  "gmsh -2 -algo meshadapt \"" + file_base_name + ".geo\"";
84  gmsh_command += " -o \"" + file_base_name + ".msh\"";
85  // Temporarilly disable unused result warning
86  #pragma GCC diagnostic push
87  #pragma GCC diagnostic ignored "-Wunused-result"
88  std::system(gmsh_command.c_str());
89  #pragma GCC diagnostic pop
90  auto surface_mesh =
91  FileIO::GMSH::readGMSHMesh("\"" + file_base_name + ".msh\"");
92  if (!surface_mesh)
93  {
94  WARN("The surface mesh could not be created.");
95  return false;
96  }
97  if (std::remove((file_base_name + ".geo").c_str()) !=0)
98  WARN("Could not remove temporary file '%s'.",
99  (file_base_name + ".geo").c_str());
100  if (std::remove((file_base_name + ".msh").c_str()) !=0)
101  WARN("Could not remove temporary file '%s'.",
102  (file_base_name + ".msh").c_str());
103 
104  // convert the surface mesh into a geometric surface
105  if (!MeshLib::convertMeshToGeo(*surface_mesh, geometries,
106  std::numeric_limits<double>::epsilon()))
107  {
108  WARN("The surface mesh could not be converted to a geometry.");
109  return false;
110  }
111  std::string merged_geometry_name("geometry_with_surfaces");
112  geometries.mergeGeometries({geometry_name, surface_mesh->getName()},
113  merged_geometry_name);
114  geometries.removeSurfaceVec(geometry_name);
115  geometries.removePolylineVec(geometry_name);
116  geometries.removePointVec(geometry_name);
117  geometries.removeSurfaceVec(surface_mesh->getName());
118  geometries.removePolylineVec(surface_mesh->getName());
119  geometries.removePointVec(surface_mesh->getName());
120  geometries.renameGeometry(merged_geometry_name, geometry_name);
121 
122  return true;
123 }
124 
125 } // end namespace
bool removePointVec(const std::string &name)
Definition: GEOObjects.cpp:79
Definition of the PolyLine class.
Definition of the Point class.
Container class for geometric objects.
Definition: GEOObjects.h:62
bool createSurface(GeoLib::Polyline const &ply, GeoLib::GEOObjects &geometries, std::string const &geometry_name)
bool isClosed() const
Definition: Polyline.cpp:190
set the parameter with a fixed value
Definition of the Mesh class.
std::size_t getNumberOfPoints() const
Definition: Polyline.cpp:180
std::vector< Point * > const & getPointsVec() const
Definition: Polyline.cpp:255
bool convertMeshToGeo(const MeshLib::Mesh &mesh, GeoLib::GEOObjects &geo_objects, double eps)
bool removePolylineVec(const std::string &name)
Definition: GEOObjects.cpp:198
Reads and writes GMSH-files to and from OGS data structures.
Definition: GMSHInterface.h:43
std::size_t getPointID(std::size_t i) const
Definition: Polyline.cpp:223
void setPrecision(unsigned int precision)
Sets the decimal precision.
Definition: Writer.cpp:67
Definition of mesh to geometry conversion.
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition: Polyline.h:50
Definition of the Polygon class.
bool removeSurfaceVec(const std::string &name)
Definition: GEOObjects.cpp:266
int writeToFile(std::string const &filename)
Writes the object to the given file.
Definition: Writer.cpp:44
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
void renameGeometry(std::string const &old_name, std::string const &new_name)
Definition: GEOObjects.cpp:524
int mergeGeometries(std::vector< std::string > const &names, std::string &merged_geo_name)
Definition: GEOObjects.cpp:375
MeshLib::Mesh * readGMSHMesh(std::string const &fname)
Definition: GmshReader.cpp:151