OGS
AppendLinesAlongPolyline.cpp
Go to the documentation of this file.
1 
11 
12 #include "BaseLib/Logging.h"
13 #include "GeoLib/Polyline.h"
14 #include "GeoLib/PolylineVec.h"
17 #include "MeshLib/Elements/Line.h"
18 #include "MeshLib/Mesh.h"
20 #include "MeshLib/MeshEnums.h"
21 #include "MeshLib/Node.h"
22 
23 namespace MeshGeoToolsLib
24 {
25 std::unique_ptr<MeshLib::Mesh> appendLinesAlongPolylines(
26  const MeshLib::Mesh& mesh, const GeoLib::PolylineVec& ply_vec)
27 {
28  // copy existing nodes and elements
29  std::vector<MeshLib::Node*> vec_new_nodes =
31  std::vector<MeshLib::Element*> vec_new_eles =
32  MeshLib::copyElementVector(mesh.getElements(), vec_new_nodes);
33 
34  auto const material_ids = materialIDs(mesh);
35  int const max_matID =
36  material_ids
37  ? *(std::max_element(begin(*material_ids), end(*material_ids)))
38  : 0;
39 
40  std::vector<int> new_mat_ids;
41  const std::size_t n_ply(ply_vec.size());
42  // for each polyline
43  for (std::size_t k(0); k < n_ply; k++)
44  {
45  const GeoLib::Polyline* ply = (*ply_vec.getVector())[k];
46 
47  // search nodes on the polyline
49  mesh, *ply, mesh.getMinEdgeLength() * 0.5,
51  auto& vec_nodes_on_ply = mshNodesAlongPoly.getNodeIDs();
52  if (vec_nodes_on_ply.empty())
53  {
54  std::string ply_name;
55  ply_vec.getNameOfElementByID(k, ply_name);
56  INFO("No nodes found on polyline {:s}", ply_name);
57  continue;
58  }
59 
60  // add line elements
61  for (std::size_t i = 0; i < vec_nodes_on_ply.size() - 1; i++)
62  {
63  std::array<MeshLib::Node*, 2> element_nodes;
64  element_nodes[0] = vec_new_nodes[vec_nodes_on_ply[i]];
65  element_nodes[1] = vec_new_nodes[vec_nodes_on_ply[i + 1]];
66  vec_new_eles.push_back(
67  new MeshLib::Line(element_nodes, vec_new_eles.size()));
68  new_mat_ids.push_back(max_matID + k + 1);
69  }
70  }
71 
72  // generate a mesh
73  const std::string name = mesh.getName() + "_with_lines";
74  auto new_mesh =
75  std::make_unique<MeshLib::Mesh>(name, vec_new_nodes, vec_new_eles);
76  auto new_material_ids =
77  new_mesh->getProperties().createNewPropertyVector<int>(
78  "MaterialIDs", MeshLib::MeshItemType::Cell);
79  if (!new_material_ids)
80  {
81  OGS_FATAL("Could not create MaterialIDs cell vector in new mesh.");
82  }
83  new_material_ids->reserve(new_mesh->getNumberOfElements());
84  if (material_ids != nullptr)
85  {
86  std::copy(begin(*material_ids), end(*material_ids),
87  std::back_inserter(*new_material_ids));
88  }
89  else
90  {
91  new_material_ids->resize(mesh.getNumberOfElements());
92  }
93  std::copy(begin(new_mat_ids), end(new_mat_ids),
94  std::back_inserter(*new_material_ids));
95  return new_mesh;
96 }
97 
98 } // namespace MeshGeoToolsLib
Definition of Duplicate functions.
Definition of the Element class.
#define OGS_FATAL(...)
Definition: Error.h:26
Definition of the Line class.
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
Definition of mesh-related Enumerations.
Definition of the Mesh class.
Definition of the Node class.
Definition of the PolylineVec class.
Definition of the PolyLine class.
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition: Polyline.h:51
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 getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:153
std::size_t size() const
Definition: TemplateVec.h:106
std::vector< std::size_t > const & getNodeIDs() const
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:95
double getMinEdgeLength() const
Get the minimum edge length over all elements of the mesh.
Definition: Mesh.h:80
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:92
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:86
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
std::unique_ptr< MeshLib::Mesh > appendLinesAlongPolylines(const MeshLib::Mesh &mesh, const GeoLib::PolylineVec &ply_vec)
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:258
std::vector< Element * > copyElementVector(std::vector< Element * > const &elements, std::vector< Node * > const &new_nodes, std::vector< std::size_t > const *const node_id_map)