OGS
AppendLinesAlongPolyline.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <range/v3/view/any_view.hpp>
7#include <range/v3/view/common.hpp>
8#include <range/v3/view/repeat_n.hpp>
9
10#include "BaseLib/Logging.h"
11#include "GeoLib/Polyline.h"
12#include "GeoLib/PolylineVec.h"
16#include "MeshLib/Mesh.h"
17#include "MeshLib/MeshEnums.h"
18#include "MeshLib/Node.h"
20
22{
23std::unique_ptr<MeshLib::Mesh> appendLinesAlongPolylines(
24 const MeshLib::Mesh& mesh, const GeoLib::PolylineVec& ply_vec)
25{
26 // copy existing nodes and elements
27 std::vector<MeshLib::Node*> vec_new_nodes =
29 std::vector<MeshLib::Element*> vec_new_eles =
30 MeshLib::copyElementVector(mesh.getElements(), vec_new_nodes);
31
32 auto const material_ids = materialIDs(mesh);
33 assert(material_ids != nullptr);
34 int const max_matID = material_ids ? ranges::max(*material_ids) : 0;
35
36 auto const edgeLengths = minMaxEdgeLength(mesh.getElements());
37 double const min_edge = edgeLengths.first;
38
39 std::vector<int> new_mat_ids;
40 const std::size_t n_ply(ply_vec.size());
41 // for each polyline
42 for (std::size_t k(0); k < n_ply; k++)
43 {
44 auto const* const ply = ply_vec.getVector()[k];
45
46 // search nodes on the polyline
48 mesh, *ply, min_edge * 0.5, MeshGeoToolsLib::SearchAllNodes::Yes);
49 auto& vec_nodes_on_ply = mshNodesAlongPoly.getNodeIDs();
50 if (vec_nodes_on_ply.empty())
51 {
52 std::string ply_name;
53 ply_vec.getNameOfElementByID(k, ply_name);
54 INFO("No nodes found on polyline {:s}", ply_name);
55 continue;
56 }
57
58 // add line elements
59 for (std::size_t i = 0; i < vec_nodes_on_ply.size() - 1; i++)
60 {
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 true /* compute_element_neighbors */);
75 auto new_material_ids =
76 new_mesh->getProperties().createNewPropertyVector<int>(
77 "MaterialIDs", MeshLib::MeshItemType::Cell);
78 if (!new_material_ids)
79 {
80 OGS_FATAL("Could not create MaterialIDs cell vector in new mesh.");
81 }
82
83 auto initial_values =
84 material_ids ? ranges::any_view<int const>(*material_ids)
85 : ranges::views::repeat_n(0, mesh.getNumberOfElements());
86
87 new_material_ids->assign(ranges::views::common(
88 ranges::views::concat(initial_values, new_mat_ids)));
89
90 return new_mesh;
91}
92
93} // namespace MeshGeoToolsLib
#define OGS_FATAL(...)
Definition Error.h:19
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:28
bool getNameOfElementByID(std::size_t id, std::string &element_name) const
std::size_t size() const
Definition TemplateVec.h:88
std::vector< T * > const & getVector() const
Definition TemplateVec.h:94
std::vector< std::size_t > const & getNodeIDs() const
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:97
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:94
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:88
TemplateVec< GeoLib::Polyline > PolylineVec
class PolylineVec encapsulate a std::vector of Polylines additional one can give the vector of polyli...
Definition PolylineVec.h:16
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.
TemplateElement< MeshLib::LineRule2 > Line
Definition Line.h:14
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)