OGS
RemoveMeshComponents.cpp
Go to the documentation of this file.
1
12
17#include "MeshLib/Node.h"
18
19namespace MeshLib
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 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,
92 removed_element_ids, removed_node_ids));
93 return new_mesh;
94 }
95
96 INFO("Current selection removes all elements.");
97 return nullptr;
98}
99
100std::vector<bool> markUnusedNodes(std::vector<Element*> const& elements,
101 std::vector<Node*> const& nodes)
102{
103 std::vector<bool> unused_nodes(nodes.size(), true);
104 for (auto e : elements)
105 {
106 for (unsigned i = 0; i < e->getNumberOfNodes(); i++)
107 {
108 unused_nodes[getNodeIndex(*e, i)] = false;
109 }
110 }
111
112 return unused_nodes;
113}
114
115void removeMarkedNodes(std::vector<bool> const& nodes_to_delete,
116 std::vector<Node*>& nodes)
117{
118 assert(nodes_to_delete.size() == nodes.size());
119
120 for (std::size_t i = 0; i < nodes.size(); i++)
121 {
122 if (nodes_to_delete[i])
123 {
124 delete nodes[i];
125 nodes[i] = nullptr;
126 }
127 }
128 nodes.erase(remove(begin(nodes), end(nodes), nullptr), end(nodes));
129}
130
132 const std::vector<std::size_t>& del_nodes_idx,
133 const std::string& new_mesh_name)
134{
135 if (del_nodes_idx.empty())
136 {
137 return nullptr;
138 }
139
140 // copy node and element objects
141 std::vector<MeshLib::Node*> new_nodes =
143 std::vector<MeshLib::Element*> new_elems =
144 MeshLib::copyElementVector(mesh.getElements(), new_nodes);
145
146 // delete elements
147 MeshLib::ElementSearch es(mesh);
148 es.searchByNodeIDs(del_nodes_idx);
149 auto& removed_element_ids = es.getSearchedElementIDs();
150 for (auto eid : removed_element_ids)
151 {
152 delete new_elems[eid];
153 new_elems[eid] = nullptr;
154 }
155 new_elems.erase(std::remove(new_elems.begin(), new_elems.end(), nullptr),
156 new_elems.end());
157
158 // check unused nodes due to element deletion
159 std::vector<bool> const node_delete_flag =
160 markUnusedNodes(new_elems, new_nodes);
161
162 // delete unused nodes
163 removeMarkedNodes(node_delete_flag, new_nodes);
164
165 if (!new_elems.empty())
166 {
167 MeshLib::Mesh* new_mesh =
168 new MeshLib::Mesh(new_mesh_name, new_nodes, new_elems,
170 removed_element_ids, del_nodes_idx));
171 return new_mesh;
172 }
173
174 return nullptr;
175}
176} // end namespace MeshLib
Definition of Duplicate functions.
Definition of the Element class.
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:34
Definition of the Node class.
Element search class.
Definition: ElementSearch.h:28
const std::vector< std::size_t > & getSearchedElementIDs() const
return marked elements
Definition: ElementSearch.h:33
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:95
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
Properties & getProperties()
Definition: Mesh.h:123
Node search class.
Definition: NodeSearch.h:25
std::size_t searchNodesConnectedToOnlyGivenElements(const std::vector< std::size_t > &elements)
Definition: NodeSearch.cpp:24
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
Definition: Properties.cpp:58
std::vector< MeshLib::Element * > excludeElementCopy(std::vector< MeshLib::Element * > const &vec_src_eles, std::vector< std::size_t > const &vec_removed)
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
std::size_t getNodeIndex(Element const &element, unsigned const idx)
Definition: Element.cpp:223
std::vector< bool > markUnusedNodes(std::vector< Element * > const &elements, std::vector< Node * > const &nodes)
Marks nodes not used by any of the elements.
MeshLib::Mesh * removeNodes(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &del_nodes_idx, const std::string &new_mesh_name)
void removeMarkedNodes(std::vector< bool > const &nodes_to_delete, std::vector< Node * > &nodes)
Deallocates and removes nodes marked true.
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)