OGS
RemoveMeshComponents.cpp
Go to the documentation of this file.
1
12
16#include "MeshLib/Node.h"
18
19namespace MeshToolsLib
20{
22 const MeshLib::Mesh& mesh,
23 const std::vector<std::size_t>& removed_element_ids,
24 const std::string& new_mesh_name)
25{
26 if (removed_element_ids.empty())
27 {
28 INFO("No elements to remove");
29 return nullptr;
30 }
31
32 INFO("Removing total {:d} elements...", removed_element_ids.size());
33 std::vector<MeshLib::Element*> tmp_elems =
34 BaseLib::excludeObjectCopy(mesh.getElements(), removed_element_ids);
35 INFO("{:d} elements remain in mesh.", tmp_elems.size());
36
37 // copy node and element objects
38 std::vector<MeshLib::Node*> new_nodes =
40 std::vector<MeshLib::Element*> new_elems =
41 MeshLib::copyElementVector(tmp_elems, new_nodes);
42
43 // delete unused nodes
44 MeshLib::NodeSearch ns(mesh);
45 ns.searchNodesConnectedToOnlyGivenElements(removed_element_ids);
46 auto& removed_node_ids(ns.getSearchedNodeIDs());
47 INFO("Removing total {:d} nodes...", removed_node_ids.size());
48 for (auto nodeid : removed_node_ids)
49 {
50 delete new_nodes[nodeid];
51 new_nodes[nodeid] = nullptr;
52 }
53 new_nodes.erase(std::remove(new_nodes.begin(), new_nodes.end(), nullptr),
54 new_nodes.end());
55
56 if (!new_elems.empty())
57 {
58 MeshLib::Mesh* new_mesh =
59 new MeshLib::Mesh(new_mesh_name, new_nodes, new_elems,
60 true /* compute_element_neighbors */,
62 removed_element_ids, removed_node_ids));
63 return new_mesh;
64 }
65
66 INFO("Current selection removes all elements.");
67 return nullptr;
68}
69
70std::vector<bool> markUnusedNodes(
71 std::vector<MeshLib::Element*> const& elements,
72 std::vector<MeshLib::Node*> const& nodes)
73{
74 std::vector<bool> unused_nodes(nodes.size(), true);
75 for (auto e : elements)
76 {
77 for (unsigned i = 0; i < e->getNumberOfNodes(); i++)
78 {
79 unused_nodes[getNodeIndex(*e, i)] = false;
80 }
81 }
82
83 return unused_nodes;
84}
85
86void removeMarkedNodes(std::vector<bool> const& nodes_to_delete,
87 std::vector<MeshLib::Node*>& nodes)
88{
89 assert(nodes_to_delete.size() == nodes.size());
90
91 for (std::size_t i = 0; i < nodes.size(); i++)
92 {
93 if (nodes_to_delete[i])
94 {
95 delete nodes[i];
96 nodes[i] = nullptr;
97 }
98 }
99 nodes.erase(remove(begin(nodes), end(nodes), nullptr), end(nodes));
100}
101
103 const std::vector<std::size_t>& del_nodes_idx,
104 const std::string& new_mesh_name)
105{
106 if (del_nodes_idx.empty())
107 {
108 return nullptr;
109 }
110
111 // copy node and element objects
112 std::vector<MeshLib::Node*> new_nodes =
114 std::vector<MeshLib::Element*> new_elems =
115 MeshLib::copyElementVector(mesh.getElements(), new_nodes);
116
117 // delete elements
118 MeshLib::ElementSearch es(mesh);
119 es.searchByNodeIDs(del_nodes_idx);
120 auto& removed_element_ids = es.getSearchedElementIDs();
121 for (auto eid : removed_element_ids)
122 {
123 delete new_elems[eid];
124 new_elems[eid] = nullptr;
125 }
126 new_elems.erase(std::remove(new_elems.begin(), new_elems.end(), nullptr),
127 new_elems.end());
128
129 // check unused nodes due to element deletion
130 std::vector<bool> const node_delete_flag =
131 markUnusedNodes(new_elems, new_nodes);
132
133 // delete unused nodes
134 removeMarkedNodes(node_delete_flag, new_nodes);
135
136 if (!new_elems.empty())
137 {
138 MeshLib::Mesh* new_mesh =
139 new MeshLib::Mesh(new_mesh_name, new_nodes, new_elems,
140 true /* compute_element_neighbors */,
142 removed_element_ids, del_nodes_idx));
143 return new_mesh;
144 }
145
146 return nullptr;
147}
148} // 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:108
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
Properties & getProperties()
Definition Mesh.h:136
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< T > excludeObjectCopy(std::vector< T > const &src_vec, std::vector< std::size_t > const &exclude_positions)
Definition Algorithm.h:42
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< 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)