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