preCICE v3.1.2
Loading...
Searching...
No Matches
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 {
15namespace mesh {
16
27{
28 Vertex *a0 = &a.vertex(0);
29 Vertex *a1 = &a.vertex(1);
30 Vertex *b0 = &b.vertex(0);
31 Vertex *b1 = &b.vertex(1);
32 if (a0 == b0 || a0 == b1) {
33 return a0;
34 }
35 if (a1 == b0 || a1 == b1) {
36 return a1;
37 }
38 return nullptr;
39}
40
47inline double edgeLength(const Edge &e)
48{
49 return (e.vertex(0).getCoords() - e.vertex(1).getCoords()).norm();
50}
51
52template <std::size_t n>
61
72template <std::size_t n>
74{
75 static_assert(n > 1, "You already know the answer.");
76 Chain<n> chain;
77 chain.connected = false;
78
79 // the edge 0 is the starting point
80 // find connected edges 1 ... n-2
81 for (std::size_t i = 1; i < n - 1; ++i) {
82 bool found = false;
83 for (std::size_t j = i; j < n; ++j) {
84 if (edges[i - 1]->connectedTo(*edges[j])) {
85 std::swap(edges[i], edges[j]);
86 found = true;
87 break;
88 }
89 }
90 if (found == false) {
91 return chain;
92 }
93 }
94 // the last edge just needs to be checked
95 if (!edges[n - 1]->connectedTo(*edges[n - 2]) ||
96 !edges[n - 1]->connectedTo(*edges[0])) {
97 return chain;
98 }
99 chain.edges = edges;
100
101 // now find common vertices
102 for (std::size_t i = 0; i < n - 1; ++i) {
103 chain.vertices[i] = sharedVertex(*edges[i], *edges[i + 1]);
104 }
105 chain.vertices[n - 1] = sharedVertex(*edges[0], *edges[n - 1]);
106 chain.connected = true;
107 return chain;
108}
109
111template <std::size_t n>
113{
114 static_assert(n > 0, "Cannot handle nothing.");
116 std::transform(vertexIDs.begin(), vertexIDs.end(), vptrs.begin(),
117 [&mesh](int id) { return &(mesh.vertex(id)); });
118 return vptrs;
119}
120
122template <std::size_t n>
124{
126 std::transform(vertexIDs.begin(), vertexIDs.end(), coords.begin(),
127 [&mesh](int id) { return mesh.vertex(id).getCoords(); });
128 return coords;
129}
130
132template <std::size_t n>
134{
136 std::transform(vertexPtrs.begin(), vertexPtrs.end(), coords.begin(),
137 [](Vertex *v) { return v->getCoords(); });
138 return coords;
139}
140
142Eigen::VectorXd integrateSurface(const PtrMesh &mesh, const Eigen::VectorXd &input);
143
145Eigen::VectorXd integrateVolume(const PtrMesh &mesh, const Eigen::VectorXd &input);
146
147template <typename Container>
148std::optional<std::size_t> locateInvalidVertexID(const Mesh &mesh, const Container &container)
149{
150 if (const auto invalidIter = std::find_if(container.begin(), container.end(), [&mesh](VertexID id) { return !mesh.isValidVertexID(id); });
151 invalidIter != container.end()) {
152 return {std::distance(container.begin(), invalidIter)};
153 }
154 return std::nullopt;
155}
156
157} // namespace mesh
158} // namespace precice
T begin(T... args)
Linear edge of a mesh, defined by two Vertex objects.
Definition Edge.hpp:16
Vertex & vertex(int i)
Returns the edge's vertex with index 0 or 1.
Definition Edge.hpp:77
Container and creator for meshes.
Definition Mesh.hpp:39
Vertex of a mesh.
Definition Vertex.hpp:16
Eigen::VectorXd getCoords() const
Returns the coordinates of the vertex.
Definition Vertex.hpp:116
T distance(T... args)
T end(T... args)
T find_if(T... args)
contains data mapping from points to meshes.
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:123
std::shared_ptr< Mesh > PtrMesh
Vertex * sharedVertex(Edge &a, Edge &b)
Definition Utils.hpp:26
std::optional< std::size_t > locateInvalidVertexID(const Mesh &mesh, const Container &container)
Definition Utils.hpp:148
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:112
double edgeLength(const Edge &e)
Definition Utils.hpp:47
Chain< n > asChain(std::array< mesh::Edge *, n > edges)
Definition Utils.hpp:73
Main namespace of the precice library.
int VertexID
Definition Types.hpp:13
std::array< Edge *, n > edges
undefined if not connected
Definition Utils.hpp:59
bool connected
true if the chain is connected or closed and thus valid
Definition Utils.hpp:55
std::array< Vertex *, n > vertices
undefined if not connected
Definition Utils.hpp:57
T swap(T... args)
T transform(T... args)