OGS
RemoveMeshComponents.cpp
Go to the documentation of this file.
1
12
16#include "MeshLib/Node.h"
18
19namespace MeshToolsLib
20{
21namespace details
22{
23std::vector<MeshLib::Element*> excludeElementCopy(
24 std::vector<MeshLib::Element*> const& vec_src_eles,
25 std::vector<std::size_t> const& vec_removed)
26{
27 std::vector<MeshLib::Element*> vec_dest_eles(vec_src_eles.size() -
28 vec_removed.size());
29
30 unsigned cnt(0);
31 for (std::size_t i = 0; i < vec_removed[0]; ++i)
32 {
33 vec_dest_eles[cnt++] = vec_src_eles[i];
34 }
35 for (std::size_t i = 1; i < vec_removed.size(); ++i)
36 {
37 for (std::size_t j = vec_removed[i - 1] + 1; j < vec_removed[i]; ++j)
38 {
39 vec_dest_eles[cnt++] = vec_src_eles[j];
40 }
41 }
42 for (std::size_t i = vec_removed.back() + 1; i < vec_src_eles.size(); ++i)
43 {
44 vec_dest_eles[cnt++] = vec_src_eles[i];
45 }
46
47 return vec_dest_eles;
48}
49
50} // namespace details
51
53 const MeshLib::Mesh& mesh,
54 const std::vector<std::size_t>& removed_element_ids,
55 const std::string& new_mesh_name)
56{
57 if (removed_element_ids.empty())
58 {
59 INFO("No elements to remove");
60 return nullptr;
61 }
62
63 INFO("Removing total {:d} elements...", removed_element_ids.size());
64 std::vector<MeshLib::Element*> tmp_elems =
65 details::excludeElementCopy(mesh.getElements(), removed_element_ids);
66 INFO("{:d} elements remain in mesh.", tmp_elems.size());
67
68 // copy node and element objects
69 std::vector<MeshLib::Node*> new_nodes =
71 std::vector<MeshLib::Element*> new_elems =
72 MeshLib::copyElementVector(tmp_elems, new_nodes);
73
74 // delete unused nodes
75 MeshLib::NodeSearch ns(mesh);
76 ns.searchNodesConnectedToOnlyGivenElements(removed_element_ids);
77 auto& removed_node_ids(ns.getSearchedNodeIDs());
78 INFO("Removing total {:d} nodes...", removed_node_ids.size());
79 for (auto nodeid : removed_node_ids)
80 {
81 delete new_nodes[nodeid];
82 new_nodes[nodeid] = nullptr;
83 }
84 new_nodes.erase(std::remove(new_nodes.begin(), new_nodes.end(), nullptr),
85 new_nodes.end());
86
87 if (!new_elems.empty())
88 {
89 MeshLib::Mesh* new_mesh =
90 new MeshLib::Mesh(new_mesh_name, new_nodes, new_elems,
91 true /* compute_element_neighbors */,
93 removed_element_ids, removed_node_ids));
94 return new_mesh;
95 }
96
97 INFO("Current selection removes all elements.");
98 return nullptr;
99}
100
101std::vector<bool> markUnusedNodes(
102 std::vector<MeshLib::Element*> const& elements,
103 std::vector<MeshLib::Node*> const& nodes)
104{
105 std::vector<bool> unused_nodes(nodes.size(), true);
106 for (auto e : elements)
107 {
108 for (unsigned i = 0; i < e->getNumberOfNodes(); i++)
109 {
110 unused_nodes[getNodeIndex(*e, i)] = false;
111 }
112 }
113
114 return unused_nodes;
115}
116
117void removeMarkedNodes(std::vector<bool> const& nodes_to_delete,
118 std::vector<MeshLib::Node*>& nodes)
119{
120 assert(nodes_to_delete.size() == nodes.size());
121
122 for (std::size_t i = 0; i < nodes.size(); i++)
123 {
124 if (nodes_to_delete[i])
125 {
126 delete nodes[i];
127 nodes[i] = nullptr;
128 }
129 }
130 nodes.erase(remove(begin(nodes), end(nodes), nullptr), end(nodes));
131}
132
134 const std::vector<std::size_t>& del_nodes_idx,
135 const std::string& new_mesh_name)
136{
137 if (del_nodes_idx.empty())
138 {
139 return nullptr;
140 }
141
142 // copy node and element objects
143 std::vector<MeshLib::Node*> new_nodes =
145 std::vector<MeshLib::Element*> new_elems =
146 MeshLib::copyElementVector(mesh.getElements(), new_nodes);
147
148 // delete elements
149 MeshLib::ElementSearch es(mesh);
150 es.searchByNodeIDs(del_nodes_idx);
151 auto& removed_element_ids = es.getSearchedElementIDs();
152 for (auto eid : removed_element_ids)
153 {
154 delete new_elems[eid];
155 new_elems[eid] = nullptr;
156 }
157 new_elems.erase(std::remove(new_elems.begin(), new_elems.end(), nullptr),
158 new_elems.end());
159
160 // check unused nodes due to element deletion
161 std::vector<bool> const node_delete_flag =
162 markUnusedNodes(new_elems, new_nodes);
163
164 // delete unused nodes
165 removeMarkedNodes(node_delete_flag, new_nodes);
166
167 if (!new_elems.empty())
168 {
169 MeshLib::Mesh* new_mesh =
170 new MeshLib::Mesh(new_mesh_name, new_nodes, new_elems,
171 true /* compute_element_neighbors */,
173 removed_element_ids, del_nodes_idx));
174 return new_mesh;
175 }
176
177 return nullptr;
178}
179} // namespace MeshToolsLib
Definition of Duplicate functions.
Definition of the Element class.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
Definition of the Node class.
Element search class.
const std::vector< std::size_t > & getSearchedElementIDs() const
return marked elements
std::size_t searchByNodeIDs(const std::vector< std::size_t > &nodes)
Marks all elements connecting to any of the given nodes.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
Properties & getProperties()
Definition Mesh.h:134
Node search class.
Definition NodeSearch.h:25
std::size_t searchNodesConnectedToOnlyGivenElements(const std::vector< std::size_t > &elements)
const std::vector< std::size_t > & getSearchedNodeIDs() const
return marked node IDs
Definition NodeSearch.h:30
Properties excludeCopyProperties(std::vector< std::size_t > const &exclude_elem_ids, std::vector< std::size_t > const &exclude_node_ids) const
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)
std::vector< MeshLib::Element * > excludeElementCopy(std::vector< MeshLib::Element * > const &vec_src_eles, std::vector< std::size_t > const &vec_removed)
std::vector< bool > markUnusedNodes(std::vector< MeshLib::Element * > const &elements, std::vector< MeshLib::Node * > const &nodes)
Marks nodes not used by any of the elements.
void removeMarkedNodes(std::vector< bool > const &nodes_to_delete, std::vector< MeshLib::Node * > &nodes)
Deallocates and removes nodes marked true.
MeshLib::Mesh * removeNodes(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &del_nodes_idx, const std::string &new_mesh_name)
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)