Loading [MathJax]/extensions/tex2jax.js
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 assert(material_ids != nullptr);
36 int const max_matID = material_ids ? ranges::max(*material_ids) : 0;
37
38 auto const edgeLengths = minMaxEdgeLength(mesh.getElements());
39 double const min_edge = edgeLengths.first;
40
41 std::vector<int> new_mat_ids;
42 const std::size_t n_ply(ply_vec.size());
43 // for each polyline
44 for (std::size_t k(0); k < n_ply; k++)
45 {
46 auto const* const ply = ply_vec.getVector()[k];
47
48 // search nodes on the polyline
50 mesh, *ply, min_edge * 0.5, MeshGeoToolsLib::SearchAllNodes::Yes);
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 true /* compute_element_neighbors */);
77 auto new_material_ids =
78 new_mesh->getProperties().createNewPropertyVector<int>(
79 "MaterialIDs", MeshLib::MeshItemType::Cell);
80 if (!new_material_ids)
81 {
82 OGS_FATAL("Could not create MaterialIDs cell vector in new mesh.");
83 }
84 new_material_ids->reserve(new_mesh->getNumberOfElements());
85 if (material_ids != nullptr)
86 {
87 std::copy(begin(*material_ids), end(*material_ids),
88 std::back_inserter(*new_material_ids));
89 }
90 else
91 {
92 new_material_ids->resize(mesh.getNumberOfElements());
93 }
94 std::copy(begin(new_mat_ids), end(new_mat_ids),
95 std::back_inserter(*new_material_ids));
96 return new_mesh;
97}
98
99} // 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...
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:108
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:105
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:99
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)