OGS
TriangulatePolyline.cpp
Go to the documentation of this file.
1 
14 #include <tclap/CmdLine.h>
15 
16 #include <string>
17 
20 #include "GeoLib/GEOObjects.h"
22 #include "GeoLib/Polyline.h"
23 #include "InfoLib/GitInfo.h"
24 
25 std::string output_question()
26 {
27  WARN("Given polyline is not closed. Close polyline now?");
28  WARN("Enter (Y)es for closing the line or (N)o for abort and press ENTER.");
29  std::string input;
30  std::cin >> input;
31  return input;
32 }
33 
34 int main(int argc, char* argv[])
35 {
36  TCLAP::CmdLine cmd(
37  "Triangulates the specified polyline in the given geometry file.\n\n"
38  "OpenGeoSys-6 software, version " +
40  ".\n"
41  "Copyright (c) 2012-2021, OpenGeoSys Community "
42  "(http://www.opengeosys.org)",
44  TCLAP::ValueArg<std::string> input_arg(
45  "i", "input", "GML input file (*.gml)", true, "", "string");
46  TCLAP::ValueArg<std::string> output_arg(
47  "o", "output", "GML output file (*.gml)", true, "", "string");
48  TCLAP::ValueArg<std::string> name_arg(
49  "n", "name", "Name of polyline in given file", true, "", "string");
50  TCLAP::ValueArg<std::string> gmsh_path_arg("g", "gmsh-path",
51  "the path to the gmsh binary",
52  false, "", "path as string");
53  cmd.add(input_arg);
54  cmd.add(name_arg);
55  cmd.add(output_arg);
56  cmd.add(gmsh_path_arg);
57  cmd.parse(argc, argv);
58 
59  std::string const& file_name(input_arg.getValue());
60  std::string const& polyline_name(name_arg.getValue());
61 
62  GeoLib::GEOObjects geo_objects;
63  GeoLib::IO::BoostXmlGmlInterface xml(geo_objects);
64  try
65  {
66  if (!xml.readFile(file_name))
67  {
68  ERR("Failed to load geometry file.");
69  return EXIT_FAILURE;
70  }
71  }
72  catch (std::runtime_error const& err)
73  {
74  ERR("Failed to read file `{:s}'.", file_name);
75  ERR("{:s}", err.what());
76  return EXIT_FAILURE;
77  }
78 
79  auto const geo_name = geo_objects.getGeometryNames()[0];
80  GeoLib::PolylineVec const* const line_vec(
81  geo_objects.getPolylineVecObj(geo_name));
82  GeoLib::Polyline* line = const_cast<GeoLib::Polyline*>(
83  line_vec->getElementByName(polyline_name));
84 
85  // check if line exists
86  if (line == nullptr)
87  {
88  ERR("No polyline found with name '{:s}'. Aborting...", polyline_name);
89  return EXIT_FAILURE;
90  }
91 
92  // check if polyline can be triangulated (i.e. closed + coplanar)
93  if (!line->isCoplanar())
94  {
95  ERR("Polyline is not coplanar, no unambiguous triangulation possible. "
96  "Aborting...");
97  return EXIT_FAILURE;
98  }
99 
100  if (!line->isClosed())
101  {
102  std::string input;
103  while (input != "y" && input != "Y" && input != "n" && input != "N")
104  {
105  input = output_question();
106  }
107 
108  if (input == "y" || input == "Y")
109  {
110  line->closePolyline();
111  INFO("Polyline closed.");
112  }
113  else
114  {
115  return EXIT_FAILURE;
116  }
117  }
118 
119  INFO("Creating a surface by triangulation of the polyline ...");
120  if (FileIO::createSurface(*line, geo_objects, geo_name,
121  gmsh_path_arg.getValue()))
122  {
123  INFO("\t done");
124  }
125  else
126  {
127  WARN("\t Creating a surface by triangulation of the polyline failed.");
128  }
129  GeoLib::SurfaceVec* sfc_vec(geo_objects.getSurfaceVecObj(geo_name));
130  std::size_t const sfc_id = geo_objects.getSurfaceVec(geo_name)->size() - 1;
131  std::string const surface_name(polyline_name + "_surface");
132  for (std::size_t i = 1;; ++i)
133  {
134  std::string const new_surface_name =
135  (i > 1) ? (surface_name + std::to_string(i)) : surface_name;
136  if (sfc_vec->getElementByName(new_surface_name) == nullptr)
137  {
138  sfc_vec->setNameForElement(sfc_id, new_surface_name);
139  break;
140  }
141  }
142 
143  // write new file
144  xml.export_name = geo_name;
145  BaseLib::IO::writeStringToFile(xml.writeToString(), output_arg.getValue());
146  INFO("...done.");
147 
148  return EXIT_SUCCESS;
149 }
Definition of analytical geometry functions.
Definition of the BoostXmlGmlInterface class.
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
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
Definition of the PolyLine class.
int main(int argc, char *argv[])
std::string output_question()
std::string writeToString()
Writes the object to a string.
Definition: Writer.cpp:31
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< Surface * > * getSurfaceVec(const std::string &name) const
Returns the surface vector with the given name as a const.
Definition: GEOObjects.cpp:307
SurfaceVec * getSurfaceVecObj(const std::string &name)
Returns the surface vector with the given name.
Definition: GEOObjects.h:205
const PolylineVec * getPolylineVecObj(const std::string &name) const
Definition: GEOObjects.cpp:227
bool readFile(const std::string &fname) override
Reads an xml-file containing OGS geometry.
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition: Polyline.h:51
bool isClosed() const
Definition: Polyline.cpp:109
void closePolyline()
Definition: Polyline.cpp:299
bool isCoplanar() const
Definition: Polyline.cpp:119
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
Definition: TemplateVec.h:40
virtual void setNameForElement(std::size_t id, std::string const &name)
Sets the given name for the element of the given ID.
Definition: TemplateVec.h:214
const T * getElementByName(const std::string &name) const
Returns an element with the given name.
Definition: TemplateVec.h:132
int writeStringToFile(std::string content, std::filesystem::path const &file_path)
Definition: Writer.cpp:45
bool createSurface(GeoLib::Polyline const &ply, GeoLib::GEOObjects &geometries, std::string const &geometry_name, std::string const &gmsh_binary)
GITINFOLIB_EXPORT const std::string ogs_version