OGS
AngleSkewMetric.cpp
Go to the documentation of this file.
1
15#include "AngleSkewMetric.h"
16
17#include <cmath>
18#include <numbers>
19
20#include "MathLib/MathTools.h"
21#include "MeshLib/Node.h"
22
23namespace MeshToolsLib
24{
25namespace
26{
27template <unsigned long N>
28std::tuple<double, double> getMinMaxAngle(
29 std::array<MeshLib::Node, N> const& nodes)
30{
31 double min_angle(2 * std::numbers::pi);
32 double max_angle(0.0);
33
34 for (decltype(N) i = 0; i < N; ++i)
35 {
36 const double angle(MathLib::getAngle(nodes[i], nodes[(i + 1) % N],
37 nodes[(i + 2) % N]));
38 min_angle = std::min(angle, min_angle);
39 max_angle = std::max(angle, max_angle);
40 }
41 return {min_angle, max_angle};
42}
43
45{
46 using namespace std::numbers;
47 std::array const nodes = {*elem.getNode(0), *elem.getNode(1),
48 *elem.getNode(2)};
49 auto const& [min_angle, max_angle] = getMinMaxAngle(nodes);
50 return std::max((max_angle - pi / 3) / 2, (pi / 3 - min_angle)) * 3 / pi;
51}
52
53double checkQuad(MeshLib::Element const& elem)
54{
55 std::array const nodes = {*elem.getNode(0), *elem.getNode(1),
56 *elem.getNode(2), *elem.getNode(3)};
57 auto const& [min_angle, max_angle] = getMinMaxAngle(nodes);
58
59 using namespace std::numbers;
60 return std::max((max_angle - pi / 2), (pi / 2 - min_angle)) * 2 / pi;
61}
62
64{
65 std::array<double, 4> min;
66 std::array<double, 4> max;
67 for (auto face_number = 0; face_number < 4; ++face_number)
68 {
69 std::unique_ptr<MeshLib::Element const> face{elem.getFace(face_number)};
70 std::array const nodes = {*face->getNode(0), *face->getNode(1),
71 *face->getNode(2)};
72 std::tie(min[face_number], max[face_number]) = getMinMaxAngle(nodes);
73 }
74
75 double const min_angle = *std::min_element(min.begin(), min.end());
76 double const max_angle = *std::max_element(max.begin(), max.end());
77
78 using namespace std::numbers;
79 return std::max((max_angle - pi / 3) / 2, (pi / 3 - min_angle)) * 3 / pi;
80}
81
83{
84 std::array<double, 6> min;
85 std::array<double, 6> max;
86 for (auto face_number = 0; face_number < 6; ++face_number)
87 {
88 std::unique_ptr<MeshLib::Element const> face{elem.getFace(face_number)};
89 std::array const nodes = {*face->getNode(0), *face->getNode(1),
90 *face->getNode(2), *face->getNode(3)};
91 std::tie(min[face_number], max[face_number]) = getMinMaxAngle(nodes);
92 }
93
94 double const min_angle = *std::min_element(min.begin(), min.end());
95 double const max_angle = *std::max_element(max.begin(), max.end());
96
97 using namespace std::numbers;
98 return std::max((max_angle - pi / 2), (pi / 2 - min_angle)) * 2 / pi;
99}
100
101double checkPrism(MeshLib::Element const& elem)
102{
103 // face 0: triangle (0,1,2)
104 std::unique_ptr<MeshLib::Element const> f0{elem.getFace(0)};
105 std::array const nodes_f0 = {*f0->getNode(0), *f0->getNode(1),
106 *f0->getNode(2)};
107 auto const& [min_angle_tri0, max_angle_tri0] = getMinMaxAngle(nodes_f0);
108
109 // face 4: triangle (3,4,5)
110 std::unique_ptr<MeshLib::Element const> f4{elem.getFace(4)};
111 std::array const nodes_f4 = {*f4->getNode(0), *f4->getNode(1),
112 *f4->getNode(2)};
113 auto const& [min_angle_tri1, max_angle_tri1] = getMinMaxAngle(nodes_f4);
114
115 auto const min_angle_tri = std::min(min_angle_tri0, min_angle_tri1);
116 auto const max_angle_tri = std::max(max_angle_tri0, max_angle_tri1);
117
118 using namespace std::numbers;
119 double const tri_criterion =
120 std::max((max_angle_tri - pi / 3) / 2, (pi / 3 - min_angle_tri)) * 3 /
121 pi;
122
123 std::array<double, 3> min;
124 std::array<double, 3> max;
125 for (int i = 1; i < 4; ++i)
126 {
127 std::unique_ptr<MeshLib::Element const> f{elem.getFace(i)};
128 std::array const nodes = {*f->getNode(0), *f->getNode(1),
129 *f->getNode(2), *f->getNode(3)};
130 std::tie(min[i - 1], max[i - 1]) = getMinMaxAngle(nodes);
131 }
132
133 double const min_angle_quad = *std::min_element(min.begin(), min.end());
134 double const max_angle_quad = *std::max_element(max.begin(), max.end());
135
136 using namespace std::numbers;
137 double const quad_criterion =
138 std::max((max_angle_quad - pi / 2), (pi / 2 - min_angle_quad)) * 2 / pi;
139
140 return std::min(tri_criterion, quad_criterion);
141}
142
143} // end unnamed namespace
144
146{
147 for (auto const e : _mesh.getElements())
148 {
149 switch (e->getGeomType())
150 {
152 _element_quality_metric[e->getID()] = -1.0;
153 break;
155 _element_quality_metric[e->getID()] = checkTriangle(*e);
156 break;
158 _element_quality_metric[e->getID()] = checkQuad(*e);
159 break;
161 _element_quality_metric[e->getID()] = checkTetrahedron(*e);
162 break;
164 _element_quality_metric[e->getID()] = checkHexahedron(*e);
165 break;
167 _element_quality_metric[e->getID()] = checkPrism(*e);
168 break;
169 default:
170 break;
171 }
172 }
173}
174
175} // namespace MeshToolsLib
Definition of the AngleSkewMetric class.
Definition of the Node class.
virtual const Element * getFace(unsigned i) const =0
Returns the i-th face of the element.
virtual const Node * getNode(unsigned idx) const =0
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
std::vector< double > _element_quality_metric
double getAngle(Point3d const &p0, Point3d const &p1, Point3d const &p2)
Definition MathTools.cpp:42
double checkTriangle(MeshLib::Element const &elem)
std::tuple< double, double > getMinMaxAngle(std::array< MeshLib::Node, N > const &nodes)
double checkHexahedron(MeshLib::Element const &elem)
double checkTetrahedron(MeshLib::Element const &elem)
void calculateQuality() override
Calculates the quality metric for each element of the mesh.