OGS
AppendLinesAlongPolyline.cpp
Go to the documentation of this file.
1
11
12#include <range/v3/view/any_view.hpp>
13#include <range/v3/view/common.hpp>
14#include <range/v3/view/repeat_n.hpp>
15
16#include "BaseLib/Logging.h"
17#include "GeoLib/Polyline.h"
18#include "GeoLib/PolylineVec.h"
22#include "MeshLib/Mesh.h"
23#include "MeshLib/MeshEnums.h"
24#include "MeshLib/Node.h"
26
28{
29std::unique_ptr<MeshLib::Mesh> appendLinesAlongPolylines(
30 const MeshLib::Mesh& mesh, const GeoLib::PolylineVec& ply_vec)
31{
32 // copy existing nodes and elements
33 std::vector<MeshLib::Node*> vec_new_nodes =
35 std::vector<MeshLib::Element*> vec_new_eles =
36 MeshLib::copyElementVector(mesh.getElements(), vec_new_nodes);
37
38 auto const material_ids = materialIDs(mesh);
39 assert(material_ids != nullptr);
40 int const max_matID = material_ids ? ranges::max(*material_ids) : 0;
41
42 auto const edgeLengths = minMaxEdgeLength(mesh.getElements());
43 double const min_edge = edgeLengths.first;
44
45 std::vector<int> new_mat_ids;
46 const std::size_t n_ply(ply_vec.size());
47 // for each polyline
48 for (std::size_t k(0); k < n_ply; k++)
49 {
50 auto const* const ply = ply_vec.getVector()[k];
51
52 // search nodes on the polyline
54 mesh, *ply, min_edge * 0.5, MeshGeoToolsLib::SearchAllNodes::Yes);
55 auto& vec_nodes_on_ply = mshNodesAlongPoly.getNodeIDs();
56 if (vec_nodes_on_ply.empty())
57 {
58 std::string ply_name;
59 ply_vec.getNameOfElementByID(k, ply_name);
60 INFO("No nodes found on polyline {:s}", ply_name);
61 continue;
62 }
63
64 // add line elements
65 for (std::size_t i = 0; i < vec_nodes_on_ply.size() - 1; i++)
66 {
67 std::array<MeshLib::Node*, 2> element_nodes;
68 element_nodes[0] = vec_new_nodes[vec_nodes_on_ply[i]];
69 element_nodes[1] = vec_new_nodes[vec_nodes_on_ply[i + 1]];
70 vec_new_eles.push_back(
71 new MeshLib::Line(element_nodes, vec_new_eles.size()));
72 new_mat_ids.push_back(max_matID + k + 1);
73 }
74 }
75
76 // generate a mesh
77 const std::string name = mesh.getName() + "_with_lines";
78 auto new_mesh =
79 std::make_unique<MeshLib::Mesh>(name, vec_new_nodes, vec_new_eles,
80 true /* compute_element_neighbors */);
81 auto new_material_ids =
82 new_mesh->getProperties().createNewPropertyVector<int>(
83 "MaterialIDs", MeshLib::MeshItemType::Cell);
84 if (!new_material_ids)
85 {
86 OGS_FATAL("Could not create MaterialIDs cell vector in new mesh.");
87 }
88
89 auto initial_values =
90 material_ids ? ranges::any_view<int const>(*material_ids)
91 : ranges::views::repeat_n(0, mesh.getNumberOfElements());
92
93 new_material_ids->assign(ranges::views::common(
94 ranges::views::concat(initial_values, new_mat_ids)));
95
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)