OGS 6.2.1-97-g73d1aeda3
AppendLinesAlongPolyline.cpp
Go to the documentation of this file.
1 
10 
11 #include <logog/include/logog.hpp>
12 
13 #include "GeoLib/Polyline.h"
14 #include "GeoLib/PolylineVec.h"
15 
16 #include "MeshLib/Mesh.h"
17 #include "MeshLib/Node.h"
18 #include "MeshLib/Elements/Line.h"
20 #include "MeshLib/MeshEnums.h"
22 
24 
25 namespace MeshGeoToolsLib
26 {
27 std::unique_ptr<MeshLib::Mesh> appendLinesAlongPolylines(
28  const MeshLib::Mesh& mesh, const GeoLib::PolylineVec& ply_vec)
29 {
30  // copy existing nodes and elements
31  std::vector<MeshLib::Node*> vec_new_nodes = MeshLib::copyNodeVector(mesh.getNodes());
32  std::vector<MeshLib::Element*> vec_new_eles = 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  std::string ply_name;
54  ply_vec.getNameOfElementByID(k, ply_name);
55  INFO("No nodes found on polyline %s", ply_name.c_str());
56  continue;
57  }
58 
59  // add line elements
60  for (std::size_t i=0; i<vec_nodes_on_ply.size()-1; i++) {
61  std::array<MeshLib::Node*, 2> element_nodes;
62  element_nodes[0] = vec_new_nodes[vec_nodes_on_ply[i]];
63  element_nodes[1] = vec_new_nodes[vec_nodes_on_ply[i+1]];
64  vec_new_eles.push_back(
65  new MeshLib::Line(element_nodes, vec_new_eles.size()));
66  new_mat_ids.push_back(max_matID+k+1);
67  }
68  }
69 
70  // generate a mesh
71  const std::string name = mesh.getName() + "_with_lines";
72  auto new_mesh =
73  std::make_unique<MeshLib::Mesh>(name, vec_new_nodes, vec_new_eles);
74  auto new_material_ids =
75  new_mesh->getProperties().createNewPropertyVector<int>(
76  "MaterialIDs", MeshLib::MeshItemType::Cell);
77  if (!new_material_ids)
78  {
79  OGS_FATAL("Could not create MaterialIDs cell vector in new mesh.");
80  }
81  new_material_ids->reserve(new_mesh->getNumberOfElements());
82  if (material_ids != nullptr)
83  {
84  std::copy(begin(*material_ids), end(*material_ids),
85  std::back_inserter(*new_material_ids));
86  }
87  else
88  {
89  new_material_ids->resize(mesh.getNumberOfElements());
90  }
91  std::copy(begin(new_mat_ids), end(new_mat_ids),
92  std::back_inserter(*new_material_ids));
93  return new_mesh;
94 }
95 
96 } // namespace MeshGeoToolsLib
Implementation of heuristic search length strategy.
Definition of the PolyLine class.
std::vector< MeshLib::Element * > copyElementVector(const std::vector< MeshLib::Element *> &elements, const std::vector< MeshLib::Node *> &nodes)
Definition of the Line class.
std::size_t size() const
Definition: TemplateVec.h:107
Definition of the Node class.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:105
double getMinEdgeLength() const
Get the minimum edge length over all elements of the mesh.
Definition: Mesh.h:90
Definition of the Mesh class.
const std::string getName() const
Get name of the mesh.
Definition: Mesh.h:102
Definition of the PolylineVec class.
Definition of Duplicate functions.
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition: Polyline.h:50
std::vector< MeshLib::Node * > copyNodeVector(const std::vector< MeshLib::Node *> &nodes)
Creates a deep copy of a Node vector.
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition: Mesh.cpp:403
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:108
std::unique_ptr< MeshLib::Mesh > appendLinesAlongPolylines(const MeshLib::Mesh &mesh, const GeoLib::PolylineVec &ply_vec)
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
Definition: TemplateVec.h:154
Definition of mesh-related Enumerations.
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
Definition: TemplateVec.h:40
std::vector< std::size_t > const & getNodeIDs() const
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:96
const std::vector< T * > * getVector() const
Definition: TemplateVec.h:113
#define OGS_FATAL(fmt,...)
Definition: Error.h:63
void copy(MatrixOrVector const &x, MatrixOrVector &y)
Copies x to y.
Definition: LinAlg.h:36
Definition of the Element class.