OGS
createMeshFromElementSelection.cpp
Go to the documentation of this file.
1
13
14#include <range/v3/numeric.hpp>
15#include <range/v3/range/conversion.hpp>
16#include <range/v3/view/enumerate.hpp>
17#include <range/v3/view/indirect.hpp>
18#include <range/v3/view/map.hpp>
19#include <unordered_map>
20
22#include "MeshLib/Mesh.h"
23#include "MeshLib/Node.h"
25
26namespace MeshLib
27{
28std::unique_ptr<MeshLib::Mesh> createMeshFromElementSelection(
29 std::string mesh_name, std::vector<MeshLib::Element*> const& elements)
30{
31 auto ids_vector = views::ids | ranges::to<std::vector>();
32
33 DBUG("Found {:d} elements in the mesh", elements.size());
34
35 // Store bulk element ids for each of the new elements.
36 auto bulk_element_ids = elements | ids_vector;
37
38 // original node ids to newly created nodes.
39 std::unordered_map<std::size_t, MeshLib::Node*> id_node_hash_map;
40 id_node_hash_map.reserve(
41 elements.size()); // There will be at least one node per element.
42
43 for (auto& e : elements)
44 {
45 // For each node find a cloned node in map or create if there is none.
46 unsigned const n_nodes = e->getNumberOfNodes();
47 for (unsigned i = 0; i < n_nodes; ++i)
48 {
49 const MeshLib::Node* n = e->getNode(i);
50 auto const it = id_node_hash_map.find(n->getID());
51 if (it == id_node_hash_map.end())
52 {
53 auto new_node_in_map = id_node_hash_map[n->getID()] =
54 new MeshLib::Node(*n);
55 e->setNode(i, new_node_in_map);
56 }
57 else
58 {
59 e->setNode(i, it->second);
60 }
61 }
62 }
63
64 std::map<std::size_t, MeshLib::Node*> nodes_map;
65 for (const auto& n : id_node_hash_map)
66 {
67 nodes_map[n.first] = n.second;
68 }
69
70 // Copy the unique nodes pointers.
71 auto element_nodes =
72 nodes_map | ranges::views::values | ranges::to<std::vector>;
73
74 // Store bulk node ids for each of the new nodes.
75 auto bulk_node_ids =
76 nodes_map | ranges::views::keys | ranges::to<std::vector>;
77
78 auto mesh = std::make_unique<MeshLib::Mesh>(
79 std::move(mesh_name), std::move(element_nodes), std::move(elements),
80 true /* compute_element_neighbors */);
81 assert(mesh != nullptr);
82
84 MeshLib::MeshItemType::Cell, 1, bulk_element_ids);
86 MeshLib::MeshItemType::Node, 1, bulk_node_ids);
87
88 return mesh;
89}
90
91} // namespace MeshLib
Definition of the Element class.
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of the Mesh class.
Definition of the Node class.
std::size_t getID() const
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:225
std::unique_ptr< MeshLib::Mesh > createMeshFromElementSelection(std::string mesh_name, std::vector< MeshLib::Element * > const &elements)
constexpr std::string_view getBulkIDString(MeshItemType mesh_item_type)
Definition Properties.h:188
void addPropertyToMesh(Mesh &mesh, std::string_view name, MeshItemType item_type, std::size_t number_of_components, std::vector< T > const &values)