preCICE
Loading...
Searching...
No Matches
Filter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/container/flat_map.hpp>
4
5#include "mesh/Mesh.hpp"
7
8namespace precice::mesh {
9
15template <typename UnaryPredicate>
16void filterMesh(Mesh &destination, const Mesh &source, UnaryPredicate &&p)
17{
18 if (source.hasConnectivity()) {
19 filterMeshWithConnectivity(destination, source, std::forward<UnaryPredicate>(p));
20 } else {
21 filterMeshWithoutConnectivity(destination, source, std::forward<UnaryPredicate>(p));
22 }
23}
24
25template <typename UnaryPredicate>
26void filterMeshWithConnectivity(Mesh &destination, const Mesh &source, UnaryPredicate p)
27{
28 // Create a lookup table which can contain all vertices of the original mesh.
29 std::vector<Vertex *> vertexMap(source.nVertices(), nullptr);
30
31 for (const Vertex &vertex : source.vertices()) {
32 if (p(vertex)) {
33 Vertex &v = destination.createVertex(vertex.getCoords());
34 v.setGlobalIndex(vertex.getGlobalIndex());
35 v.setTagged(vertex.isTagged());
36 v.setOwner(vertex.isOwner());
37 vertexMap[vertex.getID()] = &v;
38 }
39 }
40
41 auto fetch = [&vertexMap](int vid) -> Vertex * {
42#ifndef NDEBUG
43 return vertexMap.at(vid);
44#else
45 return vertexMap[vid];
46#endif
47 };
48
49 // Add all edges formed by the contributing vertices
50 for (const Edge &edge : source.edges()) {
51 auto vertex1 = fetch(edge.vertex(0).getID());
52 auto vertex2 = fetch(edge.vertex(1).getID());
53 if (vertex1 &&
54 vertex2) {
55 destination.createEdge(*vertex1, *vertex2);
56 }
57 }
58
59 // Add all triangles formed by the contributing vertices
60 for (const Triangle &triangle : source.triangles()) {
61 auto vertex1 = fetch(triangle.vertex(0).getID());
62 auto vertex2 = fetch(triangle.vertex(1).getID());
63 auto vertex3 = fetch(triangle.vertex(2).getID());
64 if (vertex1 &&
65 vertex2 &&
66 vertex3) {
67 destination.createTriangle(*vertex1, *vertex2, *vertex3);
68 }
69 }
70
71 // Add all tetrahedra formed by the contributing vertices
72 for (const Tetrahedron &tetra : source.tetrahedra()) {
73 auto vertex1 = fetch(tetra.vertex(0).getID());
74 auto vertex2 = fetch(tetra.vertex(1).getID());
75 auto vertex3 = fetch(tetra.vertex(2).getID());
76 auto vertex4 = fetch(tetra.vertex(3).getID());
77 if (vertex1 &&
78 vertex2 &&
79 vertex3 &&
80 vertex4) {
81 destination.createTetrahedron(*vertex1, *vertex2, *vertex3, *vertex4);
82 }
83 }
84}
85
86template <typename UnaryPredicate>
87void filterMeshWithoutConnectivity(Mesh &destination, const Mesh &source, UnaryPredicate p)
88{
89 for (const Vertex &vertex : source.vertices()) {
90 if (p(vertex)) {
91 Vertex &v = destination.createVertex(vertex.getCoords());
92 v.setGlobalIndex(vertex.getGlobalIndex());
93 v.setTagged(vertex.isTagged());
94 v.setOwner(vertex.isOwner());
95 }
96 }
97}
98
99} // namespace precice::mesh
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:15
Container and creator for meshes.
Definition Mesh.hpp:38
Triangle & createTriangle(Edge &edgeOne, Edge &edgeTwo, Edge &edgeThree)
Creates and initializes a Triangle object.
Definition Mesh.cpp:121
VertexContainer & vertices()
Returns modifieable container holding all vertices.
Definition Mesh.cpp:55
std::size_t nVertices() const
Returns the number of vertices.
Definition Mesh.cpp:65
bool hasConnectivity() const
Definition Mesh.hpp:126
TetraContainer & tetrahedra()
Returns modifiable container holding all tetrahedra.
Definition Mesh.cpp:95
Tetrahedron & createTetrahedron(Vertex &vertexOne, Vertex &vertexTwo, Vertex &vertexThree, Vertex &vertexFour)
Creates and initializes a Tetrahedron object.
Definition Mesh.cpp:143
TriangleContainer & triangles()
Returns modifiable container holding all triangles.
Definition Mesh.cpp:80
Edge & createEdge(Vertex &vertexOne, Vertex &vertexTwo)
Creates and initializes an Edge object.
Definition Mesh.cpp:113
Vertex & createVertex(const Eigen::Ref< const Eigen::VectorXd > &coords)
Creates and initializes a Vertex object.
Definition Mesh.cpp:105
EdgeContainer & edges()
Returns modifiable container holding all edges.
Definition Mesh.cpp:70
Tetrahedron of a mesh, defined by 4 vertices.
Triangle of a mesh, defined by three vertices.
Definition Triangle.hpp:24
Vertex of a mesh.
Definition Vertex.hpp:16
void setTagged(bool tagged)
Definition Vertex.cpp:37
void setGlobalIndex(int globalIndex)
Definition Vertex.cpp:17
void setOwner(bool owner)
Definition Vertex.cpp:27
provides Mesh, Data and primitives.
void filterMesh(Mesh &destination, const Mesh &source, UnaryPredicate &&p)
Definition Filter.hpp:16
void filterMeshWithConnectivity(Mesh &destination, const Mesh &source, UnaryPredicate p)
Definition Filter.hpp:26
void filterMeshWithoutConnectivity(Mesh &destination, const Mesh &source, UnaryPredicate p)
Definition Filter.hpp:87