OGS
createSurface.cpp
Go to the documentation of this file.
1 
11 #include "createSurface.h"
12 
13 #include <cstdio>
14 #include <list>
15 
17 #include "BaseLib/Logging.h"
18 #include "BaseLib/StringTools.h"
20 #include "GeoLib/GEOObjects.h"
21 #include "GeoLib/Point.h"
22 #include "GeoLib/Polygon.h"
23 #include "GeoLib/Polyline.h"
24 #include "GeoLib/Surface.h"
25 #include "GeoLib/Triangle.h"
27 #include "MeshLib/Mesh.h"
29 #include "filesystem.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  std::transform(ply_points.begin(), ply_points.end(),
57  std::back_inserter(*polyline_points),
58  [](auto const* p) { return new GeoLib::Point(*p); });
59  std::string ply_name = "temporary_polyline_name";
60  geo.addPointVec(std::move(polyline_points), ply_name);
61  auto polyline =
62  std::make_unique<GeoLib::Polyline>(*geo.getPointVec(ply_name));
63  for (std::size_t k(0); k < ply.getNumberOfPoints(); ++k)
64  {
65  polyline->addPoint(ply.getPointID(k));
66  }
67  auto polylines = std::make_unique<std::vector<GeoLib::Polyline*>>();
68  polylines->push_back(polyline.release());
69  geo.addPolylineVec(std::move(polylines), ply_name);
70 
71  // use GMSHInterface to create a mesh from the closed polyline
72  auto const geo_names = geo.getGeometryNames();
75  0.0, 0, geo_names, false, false);
76 
77  // write to random file in temp directory
78  auto geo_file = fs::temp_directory_path() /= BaseLib::randomString(32);
79  auto msh_file = fs::temp_directory_path() /= BaseLib::randomString(32);
80 
81  BaseLib::IO::writeStringToFile(gmsh_io.writeToString(), geo_file);
82  // Using GMSH's vtk output here so we don't have to deal with GMSH and it's
83  // various file format versions here
84  std::string gmsh_command = "\"" + gmsh_binary +
85  "\" -2 -algo meshadapt -format vtk -o " +
86  msh_file.string() + " " + geo_file.string();
87 
88  int const gmsh_return_value = std::system(gmsh_command.c_str());
89  if (gmsh_return_value != 0)
90  {
91  WARN("Call to '{:s}' returned non-zero value {:d}.", gmsh_command,
92  gmsh_return_value);
93  }
94  auto surface_mesh = MeshLib::IO::readMeshFromFile(msh_file.string());
95  if (!surface_mesh)
96  {
97  WARN("The surface mesh could not be created.");
98  return false;
99  }
100  if (!(fs::remove(geo_file) && fs::remove(msh_file)))
101  {
102  WARN("Could not remove temporary files in createSurface.");
103  }
104 
105  // convert the surface mesh into a geometric surface
106  if (!MeshLib::convertMeshToGeo(*surface_mesh, geometries,
107  std::numeric_limits<double>::epsilon()))
108  {
109  WARN("The surface mesh could not be converted to a geometry.");
110  return false;
111  }
112  std::string merged_geometry_name("geometry_with_surfaces");
113  geometries.mergeGeometries({geometry_name, surface_mesh->getName()},
114  merged_geometry_name);
115  geometries.removeSurfaceVec(geometry_name);
116  geometries.removePolylineVec(geometry_name);
117  geometries.removePointVec(geometry_name);
118  geometries.removeSurfaceVec(surface_mesh->getName());
119  geometries.removePolylineVec(surface_mesh->getName());
120  geometries.removePointVec(surface_mesh->getName());
121  geometries.renameGeometry(merged_geometry_name, geometry_name);
122 
123  return true;
124 }
125 
126 std::unique_ptr<GeoLib::Surface> createSurfaceWithEarClipping(
127  GeoLib::Polyline const& line)
128 {
129  if (!line.isClosed())
130  {
131  WARN("Error in Surface::createSurface() - Polyline is not closed.");
132  return nullptr;
133  }
134 
135  if (line.getNumberOfPoints() <= 2)
136  {
137  WARN(
138  "Error in Surface::createSurface() - Polyline consists of less "
139  "than three points and therefore cannot be triangulated.");
140  return nullptr;
141  }
142 
143  // create empty surface
144  auto sfc = std::make_unique<GeoLib::Surface>(line.getPointsVec());
145  auto polygon = std::make_unique<GeoLib::Polygon>(GeoLib::Polygon(line));
146  std::list<GeoLib::Polygon*> const& list_of_simple_polygons =
147  polygon->computeListOfSimplePolygons();
148 
149  for (auto const& simple_polygon : list_of_simple_polygons)
150  {
151  std::list<GeoLib::Triangle> triangles;
152  GeoLib::EarClippingTriangulation(*simple_polygon, triangles);
153 
154  // add Triangles to Surface
155  for (auto const& t : triangles)
156  {
157  sfc->addTriangle(t[0], t[1], t[2]);
158  }
159  }
160  if (sfc->getNumberOfTriangles() == 0)
161  {
162  WARN(
163  "Surface::createSurface(): Triangulation does not contain any "
164  "triangles.");
165  return nullptr;
166  }
167  return sfc;
168 }
169 
170 } // namespace FileIO
Definition of the EarClippingTriangulation class.
Definition of the GEOObjects class.
Definition of the Point class.
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
Definition of the Mesh class.
Definition of the Polygon class.
Definition of the PolyLine class.
Definition of string helper functions.
std::string writeToString()
Writes the object to a string.
Definition: Writer.cpp:31
Reads and writes GMSH-files to and from OGS data structures.
Definition: GMSHInterface.h:46
Container class for geometric objects.
Definition: GEOObjects.h:61
void renameGeometry(std::string const &old_name, std::string const &new_name)
Definition: GEOObjects.cpp:630
bool removePointVec(const std::string &name)
Definition: GEOObjects.cpp:97
bool removeSurfaceVec(const std::string &name)
Definition: GEOObjects.cpp:323
int mergeGeometries(std::vector< std::string > const &geo_names, std::string &merged_geo_name)
Definition: GEOObjects.cpp:435
bool removePolylineVec(const std::string &name)
Definition: GEOObjects.cpp:243
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition: Polyline.h:51
std::size_t getPointID(std::size_t i) const
Definition: Polyline.cpp:150
std::size_t getNumberOfPoints() const
Definition: Polyline.cpp:99
bool isClosed() const
Definition: Polyline.cpp:109
std::vector< Point * > const & getPointsVec() const
Definition: Polyline.cpp:182
Definition of mesh to geometry conversion.
int writeStringToFile(std::string content, std::filesystem::path const &file_path)
Definition: Writer.cpp:45
std::string randomString(std::size_t const length)
Returns a random string of the given length containing just a-z,A-Z,0-9.
@ FixedMeshDensity
set the parameter with a fixed value
bool createSurface(GeoLib::Polyline const &ply, GeoLib::GEOObjects &geometries, std::string const &geometry_name, std::string const &gmsh_binary)
std::unique_ptr< GeoLib::Surface > createSurfaceWithEarClipping(GeoLib::Polyline const &line)
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
bool convertMeshToGeo(const MeshLib::Mesh &mesh, GeoLib::GEOObjects &geo_objects, double const eps)
Definition of readMeshFromFile function.