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"
18#include "MeshLib/Mesh.h"
19#include "MeshLib/MeshEnums.h"
20#include "MeshLib/Node.h"
22
24{
25std::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 auto const edgeLengths = minMaxEdgeLength(mesh.getElements());
41 double const min_edge = edgeLengths.first;
42
43 std::vector<int> new_mat_ids;
44 const std::size_t n_ply(ply_vec.size());
45 // for each polyline
46 for (std::size_t k(0); k < n_ply; k++)
47 {
48 auto const* const ply = ply_vec.getVector()[k];
49
50 // search nodes on the polyline
52 mesh, *ply, min_edge * 0.5, MeshGeoToolsLib::SearchAllNodes::Yes);
53 auto& vec_nodes_on_ply = mshNodesAlongPoly.getNodeIDs();
54 if (vec_nodes_on_ply.empty())
55 {
56 std::string ply_name;
57 ply_vec.getNameOfElementByID(k, ply_name);
58 INFO("No nodes found on polyline {:s}", ply_name);
59 continue;
60 }
61
62 // add line elements
63 for (std::size_t i = 0; i < vec_nodes_on_ply.size() - 1; i++)
64 {
65 std::array<MeshLib::Node*, 2> element_nodes;
66 element_nodes[0] = vec_new_nodes[vec_nodes_on_ply[i]];
67 element_nodes[1] = vec_new_nodes[vec_nodes_on_ply[i + 1]];
68 vec_new_eles.push_back(
69 new MeshLib::Line(element_nodes, vec_new_eles.size()));
70 new_mat_ids.push_back(max_matID + k + 1);
71 }
72 }
73
74 // generate a mesh
75 const std::string name = mesh.getName() + "_with_lines";
76 auto new_mesh =
77 std::make_unique<MeshLib::Mesh>(name, vec_new_nodes, vec_new_eles,
78 true /* compute_element_neighbors */);
79 auto new_material_ids =
80 new_mesh->getProperties().createNewPropertyVector<int>(
81 "MaterialIDs", MeshLib::MeshItemType::Cell);
82 if (!new_material_ids)
83 {
84 OGS_FATAL("Could not create MaterialIDs cell vector in new mesh.");
85 }
86 new_material_ids->reserve(new_mesh->getNumberOfElements());
87 if (material_ids != nullptr)
88 {
89 std::copy(begin(*material_ids), end(*material_ids),
90 std::back_inserter(*new_material_ids));
91 }
92 else
93 {
94 new_material_ids->resize(mesh.getNumberOfElements());
95 }
96 std::copy(begin(new_mat_ids), end(new_mat_ids),
97 std::back_inserter(*new_material_ids));
98 return new_mesh;
99}
100
101} // 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(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
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.
The class TemplateVec takes a unique name and manages a std::vector of pointers to data elements of t...
Definition TemplateVec.h:38
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
std::size_t size() const
Definition TemplateVec.h:99
std::vector< T * > const & getVector() const
std::vector< std::size_t > const & getNodeIDs() const
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:103
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
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.
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)