OGS
Utils.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <algorithm>
14#include <vector>
15
16#include "BaseLib/Algorithm.h"
17#include "MeshLib/Node.h"
18
19#include "Element.h"
20#include "FaceRule.h"
21
22namespace MeshLib
23{
26inline std::vector<Node*> getBaseNodes(std::vector<Element*> const& elements)
27{
28 std::vector<Node*> base_nodes;
29 base_nodes.reserve(elements.size() * 2); // Save some of the realloctions.
30
31 for (auto* const e : elements)
32 {
33 std::copy(e->getNodes(), e->getNodes() + e->getNumberOfBaseNodes(),
34 std::back_inserter(base_nodes));
35 }
36
37 BaseLib::makeVectorUnique(base_nodes, [](Node const* a, Node* b) {
38 return a->getID() < b->getID();
39 });
40
41 return base_nodes;
42}
43
44inline Eigen::Vector3d calculateNormalizedSurfaceNormal(
45 MeshLib::Element const& surface_element,
46 MeshLib::Element const& bulk_element)
47{
48 Eigen::Vector3d surface_element_normal;
49
50 switch (surface_element.getDimension())
51 {
52 case 2:
53 surface_element_normal =
55 break;
56 case 1:
57 {
58 auto const bulk_element_normal =
60 auto const& v0 = surface_element.getNode(0)->asEigenVector3d();
61 auto const& v1 = surface_element.getNode(1)->asEigenVector3d();
62 Eigen::Vector3d const edge_vector = v1 - v0;
63 surface_element_normal = -bulk_element_normal.cross(edge_vector);
64 break;
65 }
66 case 0:
67 {
68 assert(surface_element.getCellType() == CellType::POINT1);
69 assert(bulk_element.getCellType() == CellType::LINE2 ||
70 bulk_element.getCellType() == CellType::LINE3);
71
72 auto const& x = surface_element.getNode(0)->asEigenVector3d();
73
74 // The start of the line element.
75 auto const& a = bulk_element.getNode(0)->asEigenVector3d();
76
77 // The end of the line element is the 2nd base node of the line,
78 // which is the 2nd node.
79 auto const& b = bulk_element.getNode(1)->asEigenVector3d();
80
81 // x coincides either with the start or with the end of the line.
82 // a + b - 2 * x evaluates to a - b or to b - a, respectively.
83 // The formula assumes that the line is perfectly straight, even in
84 // the LINE3 case.
85 surface_element_normal = a + b - 2 * x;
86 }
87 }
88
89 surface_element_normal.normalize();
90 // At the moment (2018-04-26) the surface normal is not oriented
91 // according to the right hand rule
92 // for correct results it is necessary to multiply the normal with
93 // -1
94 surface_element_normal *= -1;
95
96 return surface_element_normal;
97}
98
99} // namespace MeshLib
Definition of the Element class.
Definition of the Node class.
std::size_t getID() const
Definition: Point3dWithID.h:62
Eigen::Vector3d const & asEigenVector3d() const
Definition: Point3d.h:67
virtual CellType getCellType() const =0
virtual const Node * getNode(unsigned idx) const =0
virtual constexpr unsigned getDimension() const =0
Get dimension of the mesh element.
static Eigen::Vector3d getSurfaceNormal(Element const &e)
Returns the surface normal of a 2D element.
Definition: FaceRule.cpp:38
void makeVectorUnique(std::vector< T > &v)
Definition: Algorithm.h:209
void copy(PETScVector const &x, PETScVector &y)
Definition: LinAlg.cpp:37
std::vector< Node * > getBaseNodes(std::vector< Element * > const &elements)
Definition: Utils.h:26
Eigen::Vector3d calculateNormalizedSurfaceNormal(MeshLib::Element const &surface_element, MeshLib::Element const &bulk_element)
Definition: Utils.h:44