OGS
Utils.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <Eigen/Geometry>
14#include <algorithm>
15#include <vector>
16
17#include "BaseLib/Algorithm.h"
18#include "Element.h"
19#include "FaceRule.h"
20#include "MeshLib/Node.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
38
39 base_nodes.shrink_to_fit();
40 return base_nodes;
41}
42
43inline Eigen::Vector3d calculateNormalizedSurfaceNormal(
44 MeshLib::Element const& surface_element,
45 MeshLib::Element const& bulk_element)
46{
47 Eigen::Vector3d surface_element_normal;
48
49 switch (surface_element.getDimension())
50 {
51 case 2:
52 surface_element_normal =
54 break;
55 case 1:
56 {
57 auto const bulk_element_normal =
59 auto const& v0 = surface_element.getNode(0)->asEigenVector3d();
60 auto const& v1 = surface_element.getNode(1)->asEigenVector3d();
61 Eigen::Vector3d const edge_vector = v1 - v0;
62 surface_element_normal = -bulk_element_normal.cross(edge_vector);
63 break;
64 }
65 case 0:
66 {
67 assert(surface_element.getCellType() == CellType::POINT1);
68 assert(bulk_element.getCellType() == CellType::LINE2 ||
69 bulk_element.getCellType() == CellType::LINE3);
70
71 auto const& x = surface_element.getNode(0)->asEigenVector3d();
72
73 // The start of the line element.
74 auto const& a = bulk_element.getNode(0)->asEigenVector3d();
75
76 // The end of the line element is the 2nd base node of the line,
77 // which is the 2nd node.
78 auto const& b = bulk_element.getNode(1)->asEigenVector3d();
79
80 // x coincides either with the start or with the end of the line.
81 // a + b - 2 * x evaluates to a - b or to b - a, respectively.
82 // The formula assumes that the line is perfectly straight, even in
83 // the LINE3 case.
84 surface_element_normal = a + b - 2 * x;
85 }
86 }
87
88 surface_element_normal.normalize();
89 // At the moment (2018-04-26) the surface normal is not oriented
90 // according to the right hand rule
91 // for correct results it is necessary to multiply the normal with
92 // -1
93 surface_element_normal *= -1;
94
95 return surface_element_normal;
96}
97
98} // namespace MeshLib
Definition of the Element class.
Definition of the Node class.
Eigen::Vector3d const & asEigenVector3d() const
Definition Point3d.h:64
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:40
void makeVectorUnique(std::vector< T > &v)
Definition Algorithm.h:180
std::vector< Node * > getBaseNodes(std::vector< Element * > const &elements)
Definition Utils.h:26
bool idsComparator(T const a, T const b)
Definition Mesh.h:206
Eigen::Vector3d calculateNormalizedSurfaceNormal(MeshLib::Element const &surface_element, MeshLib::Element const &bulk_element)
Definition Utils.h:43