preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // Create a flat_map which can contain all vertices of the original mesh.
19 // This prevents resizes during the map build-up.
20 boost::container::flat_map<VertexID, Vertex *> vertexMap;
21 vertexMap.reserve(source.nVertices());
22
23 for (const Vertex &vertex : source.vertices()) {
24 if (p(vertex)) {
25 Vertex &v = destination.createVertex(vertex.getCoords());
26 v.setGlobalIndex(vertex.getGlobalIndex());
27 if (vertex.isTagged())
28 v.tag();
29 v.setOwner(vertex.isOwner());
30 vertexMap[vertex.getID()] = &v;
31 }
32 }
33
34 // Add all edges formed by the contributing vertices
35 for (const Edge &edge : source.edges()) {
36 VertexID vertexIndex1 = edge.vertex(0).getID();
37 VertexID vertexIndex2 = edge.vertex(1).getID();
38 if (vertexMap.count(vertexIndex1) == 1 &&
39 vertexMap.count(vertexIndex2) == 1) {
40 destination.createEdge(*vertexMap[vertexIndex1], *vertexMap[vertexIndex2]);
41 }
42 }
43
44 // Add all triangles formed by the contributing vertices
45 for (const Triangle &triangle : source.triangles()) {
46 VertexID vertexIndex1 = triangle.vertex(0).getID();
47 VertexID vertexIndex2 = triangle.vertex(1).getID();
48 VertexID vertexIndex3 = triangle.vertex(2).getID();
49 if (vertexMap.count(vertexIndex1) == 1 &&
50 vertexMap.count(vertexIndex2) == 1 &&
51 vertexMap.count(vertexIndex3) == 1) {
52 destination.createTriangle(*vertexMap[vertexIndex1], *vertexMap[vertexIndex2], *vertexMap[vertexIndex3]);
53 }
54 }
55
56 // Add all tetrahedra formed by the contributing vertices
57 for (const Tetrahedron &tetra : source.tetrahedra()) {
58 VertexID vertexIndex1 = tetra.vertex(0).getID();
59 VertexID vertexIndex2 = tetra.vertex(1).getID();
60 VertexID vertexIndex3 = tetra.vertex(2).getID();
61 VertexID vertexIndex4 = tetra.vertex(3).getID();
62 if (vertexMap.count(vertexIndex1) == 1 &&
63 vertexMap.count(vertexIndex2) == 1 &&
64 vertexMap.count(vertexIndex3) == 1 &&
65 vertexMap.count(vertexIndex4) == 1) {
66 destination.createTetrahedron(*vertexMap[vertexIndex1], *vertexMap[vertexIndex2], *vertexMap[vertexIndex3], *vertexMap[vertexIndex4]);
67 }
68 }
69}
70
71} // namespace precice::mesh
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:15
Vertex & vertex(int i)
Returns the edge's vertex with index 0 or 1.
Definition Edge.hpp:76
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
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 & vertex(int i)
Returns triangle vertex with index 0, 1 or 2.
Definition Triangle.hpp:140
Vertex of a mesh.
Definition Vertex.hpp:16
VertexID getID() const
Returns the unique (among vertices of one mesh on one processor) ID of the vertex.
Definition Vertex.hpp:109
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
int VertexID
Definition Types.hpp:13