OGS
ElementSearch.cpp
Go to the documentation of this file.
1 
11 #include "ElementSearch.h"
12 
13 #include "BaseLib/Algorithm.h"
15 #include "MeshLib/Node.h"
16 
17 namespace MeshLib
18 {
19 ElementSearch::ElementSearch(const MeshLib::Mesh& mesh) : _mesh(mesh) {}
20 
21 template <typename Container, typename Predicate>
22 std::vector<std::size_t> filter(Container const& container, Predicate const& p)
23 {
24  std::vector<std::size_t> matchedIDs;
25  std::size_t i = 0;
26  for (auto value : container)
27  {
28  if (p(value))
29  {
30  matchedIDs.push_back(i);
31  }
32  i++;
33  }
34  return matchedIDs;
35 }
36 
38 {
39  auto matchedIDs = filter(_mesh.getElements(), [&](MeshLib::Element* e)
40  { return e->getGeomType() == eleType; });
41 
42  this->updateUnion(matchedIDs);
43  return matchedIDs.size();
44 }
45 
46 std::size_t ElementSearch::searchByContent(double eps)
47 {
48  auto matchedIDs = filter(_mesh.getElements(), [&eps](MeshLib::Element* e)
49  { return e->getContent() < eps; });
50 
51  this->updateUnion(matchedIDs);
52  return matchedIDs.size();
53 }
54 
56 {
57  auto matchedIDs =
59  [&aabb](MeshLib::Element* e)
60  {
61  std::size_t const nElemNodes(e->getNumberOfBaseNodes());
62  for (std::size_t n = 0; n < nElemNodes; ++n)
63  {
64  if (aabb.containsPoint(*e->getNode(n), 0))
65  {
66  return true; // any node of element is in aabb.
67  }
68  }
69  return false; // no nodes of element are in aabb.
70  });
71 
72  this->updateUnion(matchedIDs);
73  return matchedIDs.size();
74 }
75 
76 std::size_t ElementSearch::searchByNodeIDs(
77  const std::vector<std::size_t>& nodes)
78 {
79  std::vector<std::size_t> connected_elements;
80  for (std::size_t node_id : nodes)
81  {
82  auto const& elements = _mesh.getElementsConnectedToNode(node_id);
83  std::transform(begin(elements), end(elements),
84  back_inserter(connected_elements),
85  [](Element const* const e) { return e->getID(); });
86  }
87 
88  BaseLib::makeVectorUnique(connected_elements);
89 
90  this->updateUnion(connected_elements);
91  return connected_elements.size();
92 }
93 
94 void ElementSearch::updateUnion(const std::vector<std::size_t>& vec)
95 {
96  std::vector<std::size_t> vec_temp(vec.size() + _marked_elements.size());
97  auto it = std::set_union(vec.begin(), vec.end(), _marked_elements.begin(),
98  _marked_elements.end(), vec_temp.begin());
99  vec_temp.resize(it - vec_temp.begin());
100  _marked_elements.assign(vec_temp.begin(), vec_temp.end());
101 }
102 
103 } // end namespace MeshLib
Definition of the Element class.
Definition of the Node class.
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition: AABB.h:49
void updateUnion(const std::vector< std::size_t > &vec)
Updates the vector of marked elements with values from vec.
std::size_t searchByElementType(MeshElemType eleType)
Marks all elements of the given element type.
std::size_t searchByContent(double eps=std::numeric_limits< double >::epsilon())
Marks all elements with a volume smaller than eps.
ElementSearch(const MeshLib::Mesh &mesh)
std::size_t searchByBoundingBox(GeoLib::AABB const &aabb)
Marks all elements with at least one node outside the bounding box spanned by x1 and x2;.
const MeshLib::Mesh & _mesh
The mesh from which elements should be removed.
virtual std::size_t getID() const final
Returns the ID of the element.
Definition: Element.h:82
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
void makeVectorUnique(std::vector< T > &v)
Definition: Algorithm.h:209
std::vector< std::size_t > filter(Container const &container, Predicate const &p)
MeshElemType
Types of mesh elements supported by OpenGeoSys. Values are from VTKCellType enum.
Definition: MeshEnums.h:27