preCICE v3.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <mesh/Edge.hpp>
6#include <mesh/Mesh.hpp>
7#include <optional>
8#include <utility>
9
10namespace precice::mapping {
11struct Sample;
12}
13
14namespace precice::mesh {
15
26{
27 Vertex *a0 = &a.vertex(0);
28 Vertex *a1 = &a.vertex(1);
29 Vertex *b0 = &b.vertex(0);
30 Vertex *b1 = &b.vertex(1);
31 if (a0 == b0 || a0 == b1) {
32 return a0;
33 }
34 if (a1 == b0 || a1 == b1) {
35 return a1;
36 }
37 return nullptr;
38}
39
46inline double edgeLength(const Edge &e)
47{
48 return (e.vertex(0).getCoords() - e.vertex(1).getCoords()).norm();
49}
50
51template <std::size_t n>
60
71template <std::size_t n>
73{
74 static_assert(n > 1, "You already know the answer.");
75 Chain<n> chain;
76 chain.connected = false;
77
78 // the edge 0 is the starting point
79 // find connected edges 1 ... n-2
80 for (std::size_t i = 1; i < n - 1; ++i) {
81 bool found = false;
82 for (std::size_t j = i; j < n; ++j) {
83 if (edges[i - 1]->connectedTo(*edges[j])) {
84 std::swap(edges[i], edges[j]);
85 found = true;
86 break;
87 }
88 }
89 if (found == false) {
90 return chain;
91 }
92 }
93 // the last edge just needs to be checked
94 if (!edges[n - 1]->connectedTo(*edges[n - 2]) ||
95 !edges[n - 1]->connectedTo(*edges[0])) {
96 return chain;
97 }
98 chain.edges = edges;
99
100 // now find common vertices
101 for (std::size_t i = 0; i < n - 1; ++i) {
102 chain.vertices[i] = sharedVertex(*edges[i], *edges[i + 1]);
103 }
104 chain.vertices[n - 1] = sharedVertex(*edges[0], *edges[n - 1]);
105 chain.connected = true;
106 return chain;
107}
108
110template <std::size_t n>
112{
113 static_assert(n > 0, "Cannot handle nothing.");
115 std::transform(vertexIDs.begin(), vertexIDs.end(), vptrs.begin(),
116 [&mesh](int id) { return &(mesh.vertex(id)); });
117 return vptrs;
118}
119
121template <std::size_t n>
123{
125 std::transform(vertexIDs.begin(), vertexIDs.end(), coords.begin(),
126 [&mesh](int id) { return mesh.vertex(id).getCoords(); });
127 return coords;
128}
129
131template <std::size_t n>
133{
135 std::transform(vertexPtrs.begin(), vertexPtrs.end(), coords.begin(),
136 [](Vertex *v) { return v->getCoords(); });
137 return coords;
138}
139
141Eigen::VectorXd integrateSurface(const PtrMesh &mesh, const Eigen::VectorXd &input);
142
144Eigen::VectorXd integrateVolume(const PtrMesh &mesh, const Eigen::VectorXd &input);
145
146template <typename Container>
147std::optional<std::size_t> locateInvalidVertexID(const Mesh &mesh, const Container &container)
148{
149 if (const auto invalidIter = std::find_if(container.begin(), container.end(), [&mesh](VertexID id) { return !mesh.isValidVertexID(id); });
150 invalidIter != container.end()) {
151 return {std::distance(container.begin(), invalidIter)};
152 }
153 return std::nullopt;
154}
155
158
159} // namespace precice::mesh
T begin(T... args)
An axis-aligned bounding box around a (partition of a) 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
Vertex of a mesh.
Definition Vertex.hpp:16
Eigen::VectorXd getCoords() const
Returns the coordinates of the vertex.
Definition Vertex.hpp:114
T distance(T... args)
T end(T... args)
T find_if(T... args)
T make_unique(T... args)
contains data mapping from points to meshes.
provides Mesh, Data and primitives.
Eigen::VectorXd integrateSurface(const PtrMesh &mesh, const Eigen::VectorXd &input)
Given the data and the mesh, this function returns the surface integral. Assumes no overlap exists fo...
Definition Utils.cpp:11
Eigen::VectorXd integrateVolume(const PtrMesh &mesh, const Eigen::VectorXd &input)
Given the data and the mesh, this function returns the volume integral. Assumes no overlap exists for...
Definition Utils.cpp:41
std::array< Eigen::VectorXd, n > coordsFor(const Mesh &mesh, const std::array< int, n > &vertexIDs)
Given a mesh and an array of vertexIDS, this function returns an array of coordinates of the vertices...
Definition Utils.hpp:122
std::shared_ptr< Mesh > PtrMesh
Vertex * sharedVertex(Edge &a, Edge &b)
Definition Utils.hpp:25
std::optional< std::size_t > locateInvalidVertexID(const Mesh &mesh, const Container &container)
Definition Utils.hpp:147
std::array< Vertex *, n > vertexPtrsFor(Mesh &mesh, const std::array< int, n > &vertexIDs)
Given a mesh and an array of vertexIDS, this function returns an array of pointers to vertices.
Definition Utils.hpp:111
std::size_t countVerticesInBoundingBox(mesh::PtrMesh mesh, const mesh::BoundingBox &bb)
Given a Mesh and a bounding box, counts all vertices within the bounding box.
Definition Utils.cpp:72
double edgeLength(const Edge &e)
Definition Utils.hpp:46
Chain< n > asChain(std::array< mesh::Edge *, n > edges)
Definition Utils.hpp:72
int VertexID
Definition Types.hpp:13
std::array< Edge *, n > edges
undefined if not connected
Definition Utils.hpp:58
bool connected
true if the chain is connected or closed and thus valid
Definition Utils.hpp:54
std::array< Vertex *, n > vertices
undefined if not connected
Definition Utils.hpp:56
T swap(T... args)
T transform(T... args)