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
37 BaseLib::makeVectorUnique(base_nodes, MeshLib::idsComparator<Node*>);
38
39 return base_nodes;
40}
41
42inline Eigen::Vector3d calculateNormalizedSurfaceNormal(
43 MeshLib::Element const& surface_element,
44 MeshLib::Element const& bulk_element)
45{
46 Eigen::Vector3d surface_element_normal;
47
48 switch (surface_element.getDimension())
49 {
50 case 2:
51 surface_element_normal =
53 break;
54 case 1:
55 {
56 auto const bulk_element_normal =
58 auto const& v0 = surface_element.getNode(0)->asEigenVector3d();
59 auto const& v1 = surface_element.getNode(1)->asEigenVector3d();
60 Eigen::Vector3d const edge_vector = v1 - v0;
61 surface_element_normal = -bulk_element_normal.cross(edge_vector);
62 break;
63 }
64 case 0:
65 {
66 assert(surface_element.getCellType() == CellType::POINT1);
67 assert(bulk_element.getCellType() == CellType::LINE2 ||
68 bulk_element.getCellType() == CellType::LINE3);
69
70 auto const& x = surface_element.getNode(0)->asEigenVector3d();
71
72 // The start of the line element.
73 auto const& a = bulk_element.getNode(0)->asEigenVector3d();
74
75 // The end of the line element is the 2nd base node of the line,
76 // which is the 2nd node.
77 auto const& b = bulk_element.getNode(1)->asEigenVector3d();
78
79 // x coincides either with the start or with the end of the line.
80 // a + b - 2 * x evaluates to a - b or to b - a, respectively.
81 // The formula assumes that the line is perfectly straight, even in
82 // the LINE3 case.
83 surface_element_normal = a + b - 2 * x;
84 }
85 }
86
87 surface_element_normal.normalize();
88 // At the moment (2018-04-26) the surface normal is not oriented
89 // according to the right hand rule
90 // for correct results it is necessary to multiply the normal with
91 // -1
92 surface_element_normal *= -1;
93
94 return surface_element_normal;
95}
96
97} // namespace MeshLib
Definition of the Element class.
Definition of the Node class.
Eigen::Vector3d const & asEigenVector3d() const
Definition Point3d.h:63
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:175
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:42