OGS 6.2.0-97-g4a610c866
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  std::string const& gmsh_binary)
37 {
38  if (!ply.isClosed())
39  {
40  WARN("Error in createSurface() - Polyline is not closed.");
41  return false;
42  }
43 
44  if (ply.getNumberOfPoints() <= 2)
45  {
46  WARN(
47  "Error in createSurface() - Polyline consists of less "
48  "than three points and therefore cannot be triangulated.");
49  return false;
50  }
51 
52  // create new GEOObjects and insert a copy of the polyline
53  auto polyline_points = std::make_unique<std::vector<GeoLib::Point*>>();
55  auto ply_points = ply.getPointsVec();
56  for (auto p : ply_points)
57  {
58  polyline_points->push_back(new GeoLib::Point(*p));
59  }
60  std::string ply_name = "temporary_polyline_name";
61  geo.addPointVec(std::move(polyline_points), ply_name);
62  auto polyline =
63  std::make_unique<GeoLib::Polyline>(*geo.getPointVec(ply_name));
64  for (std::size_t k(0); k < ply.getNumberOfPoints(); ++k)
65  {
66  polyline->addPoint(ply.getPointID(k));
67  }
68  auto polylines = std::make_unique<std::vector<GeoLib::Polyline*>>();
69  polylines->push_back(polyline.release());
70  geo.addPolylineVec(std::move(polylines), ply_name);
71 
72  // use GMSHInterface to create a mesh from the closed polyline
73  std::vector<std::string> geo_names;
74  geo.getGeometryNames(geo_names);
77  0.0, 0, geo_names, false, false);
78  gmsh_io.setPrecision(std::numeric_limits<double>::digits10);
79 
80  char file_base_name_c[L_tmpnam];
81  if (! std::tmpnam(file_base_name_c))
82  {
83  OGS_FATAL("Could not create unique file name.");
84  }
85  std::string const file_base_name(file_base_name_c);
86  gmsh_io.writeToFile(file_base_name + ".geo");
87  std::string gmsh_command =
88  gmsh_binary + " -2 -algo meshadapt \"" + file_base_name + ".geo\"";
89  gmsh_command += " -o \"" + file_base_name + ".msh\"";
90  int const gmsh_return_value = std::system(gmsh_command.c_str());
91  if (gmsh_return_value != 0)
92  {
93  WARN("Call to '%s' returned non-zero value %d.", gmsh_command.c_str(),
94  gmsh_return_value);
95  }
96  auto surface_mesh =
97  FileIO::GMSH::readGMSHMesh("\"" + file_base_name + ".msh\"");
98  if (!surface_mesh)
99  {
100  WARN("The surface mesh could not be created.");
101  return false;
102  }
103  if (std::remove((file_base_name + ".geo").c_str()) !=0)
104  WARN("Could not remove temporary file '%s'.",
105  (file_base_name + ".geo").c_str());
106  if (std::remove((file_base_name + ".msh").c_str()) !=0)
107  WARN("Could not remove temporary file '%s'.",
108  (file_base_name + ".msh").c_str());
109 
110  // convert the surface mesh into a geometric surface
111  if (!MeshLib::convertMeshToGeo(*surface_mesh, geometries,
112  std::numeric_limits<double>::epsilon()))
113  {
114  WARN("The surface mesh could not be converted to a geometry.");
115  return false;
116  }
117  std::string merged_geometry_name("geometry_with_surfaces");
118  geometries.mergeGeometries({geometry_name, surface_mesh->getName()},
119  merged_geometry_name);
120  geometries.removeSurfaceVec(geometry_name);
121  geometries.removePolylineVec(geometry_name);
122  geometries.removePointVec(geometry_name);
123  geometries.removeSurfaceVec(surface_mesh->getName());
124  geometries.removePolylineVec(surface_mesh->getName());
125  geometries.removePointVec(surface_mesh->getName());
126  geometries.renameGeometry(merged_geometry_name, geometry_name);
127 
128  return true;
129 }
130 
131 } // namespace FileIO
bool removePointVec(const std::string &name)
Definition: GEOObjects.cpp:89
bool createSurface(GeoLib::Polyline const &ply, GeoLib::GEOObjects &geometries, std::string const &geometry_name, std::string const &gmsh_binary)
Definition of the PolyLine class.
Definition of the Point class.
Container class for geometric objects.
Definition: GEOObjects.h:62
bool isClosed() const
Definition: Polyline.cpp:210
set the parameter with a fixed value
Definition of the Mesh class.
std::size_t getNumberOfPoints() const
Definition: Polyline.cpp:200
std::vector< Point * > const & getPointsVec() const
Definition: Polyline.cpp:279
bool convertMeshToGeo(const MeshLib::Mesh &mesh, GeoLib::GEOObjects &geo_objects, double eps)
bool removePolylineVec(const std::string &name)
Definition: GEOObjects.cpp:228
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:247
void setPrecision(unsigned int precision)
Sets the decimal precision.
Definition: Writer.cpp:69
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:310
int writeToFile(std::string const &filename)
Writes the object to the given file.
Definition: Writer.cpp:46
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
void renameGeometry(std::string const &old_name, std::string const &new_name)
Definition: GEOObjects.cpp:598
int mergeGeometries(std::vector< std::string > const &names, std::string &merged_geo_name)
Definition: GEOObjects.cpp:443
MeshLib::Mesh * readGMSHMesh(std::string const &fname)
Definition: GmshReader.cpp:163