OGS
ElementSearch.cpp
Go to the documentation of this file.
1
11#include "ElementSearch.h"
12
13#include <range/v3/algorithm/any_of.hpp>
14#include <range/v3/range/conversion.hpp>
15#include <range/v3/view/filter.hpp>
16#include <range/v3/view/take.hpp>
17
18#include "BaseLib/Algorithm.h"
20#include "MeshLib/Node.h"
21
22namespace MeshLib
23{
24ElementSearch::ElementSearch(const MeshLib::Mesh& mesh) : _mesh(mesh) {}
25
26template <typename Container, typename Predicate>
27std::vector<std::size_t> filter(Container const& container, Predicate const& p)
28{
29 return ranges::views::filter(container, p) | views::ids |
30 ranges::to<std::vector>;
31}
32
34{
35 auto matchedIDs = filter(_mesh.getElements(), [&](MeshLib::Element const* e)
36 { return e->getGeomType() == eleType; });
37
38 this->updateUnion(matchedIDs);
39 return matchedIDs.size();
40}
41
42std::size_t ElementSearch::searchByContent(double eps)
43{
44 auto matchedIDs =
45 filter(_mesh.getElements(), [&eps](MeshLib::Element const* e)
46 { return e->getContent() < eps; });
47
48 this->updateUnion(matchedIDs);
49 return matchedIDs.size();
50}
51
53 bool const invert)
54{
55 auto matchedIDs = filter(
57 [&aabb, invert](MeshLib::Element const* e)
58 {
59 // any node of element is in aabb.
60 return ranges::any_of(
61 e->nodes() | ranges::views::take(e->getNumberOfBaseNodes()),
62 [&aabb, invert](auto const* n)
63 { return (aabb.containsPoint(*n, 0) != invert); });
64 });
65
66 this->updateUnion(matchedIDs);
67 return matchedIDs.size();
68}
69
70std::size_t ElementSearch::searchByNodeIDs(
71 const std::vector<std::size_t>& nodes)
72{
73 std::vector<std::size_t> connected_elements;
74 for (std::size_t node_id : nodes)
75 {
76 auto const& elements = _mesh.getElementsConnectedToNode(node_id);
77 std::transform(begin(elements), end(elements),
78 back_inserter(connected_elements),
79 [](Element const* const e) { return e->getID(); });
80 }
81
82 BaseLib::makeVectorUnique(connected_elements);
83
84 this->updateUnion(connected_elements);
85 return connected_elements.size();
86}
87
88void ElementSearch::updateUnion(const std::vector<std::size_t>& vec)
89{
90 std::vector<std::size_t> vec_temp(vec.size() + _marked_elements.size());
91 auto it = std::set_union(vec.begin(), vec.end(), _marked_elements.begin(),
92 _marked_elements.end(), vec_temp.begin());
93 vec_temp.resize(it - vec_temp.begin());
94 _marked_elements.assign(vec_temp.begin(), vec_temp.end());
95}
96
97} // 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:56
void updateUnion(const std::vector< std::size_t > &vec)
Updates the vector of marked elements with values from vec.
std::size_t searchByBoundingBox(GeoLib::AABB const &aabb, bool const invert=false)
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)
const MeshLib::Mesh & _mesh
The mesh from which elements should be removed.
std::size_t getID() const
Returns the ID of the element.
Definition Element.h:89
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
void makeVectorUnique(std::vector< T > &v)
Definition Algorithm.h:175
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:225
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