Debian Codegraph @

src/atlas/interpolation/element/Triag2D.cc

raw
1 /*
2 * (C) Crown Copyright 2021 Met Office
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 */
7
8 #include <cmath>
9 #include <iostream>
10
11 #include "atlas/interpolation/Vector2D.h"
12 #include "atlas/interpolation/element/Triag2D.h"
13 #include "atlas/runtime/Log.h"
14
15 namespace atlas {
16 namespace interpolation {
17 namespace element {
18
19 //----------------------------------------------------------------------------------------------------------------------
20
21 method::Intersect Triag2D::intersects(const PointXY& r, double edgeEpsilon, double epsilon) const {
22 method::Intersect isect; // intersection is false
23
24 /*
25 * ,v11
26 * e2 , ^ `.
27 * , ^ r `.
28 * , ^ `.
29 * v00-------------------v10
30 * e1
31 */
32
33 Vector2D rvec{r.data()};
34
35
36 if (!inTriangle(rvec, epsilon * area())) {
37 return isect.fail();
38 }
39
40 Vector2D e1{v10 - v00};
41 Vector2D e2{v11 - v00};
42 Vector2D pvec{rvec - v00};
43
44 // solve u e1 + v e2 = pvec for u and v
45 double invDet = 1. / (e1.x() * e2.y() - e2.x() * e1.y());
46 isect.u = (pvec.x() * e2.y() - e2.x() * pvec.y()) * invDet;
47 isect.v = (e1.x() * pvec.y() - pvec.x() * e1.y()) * invDet;
48
49 // make sure weights are valid
50 if (isect.u > -edgeEpsilon || isect.u < 1. + edgeEpsilon || isect.v > -edgeEpsilon || isect.v < 1. + edgeEpsilon) {
51 return isect.success();
52 }
53 else {
54 return isect.fail();
55 }
56 }
57
58 bool Triag2D::validate() const {
59 // normal for sub-triangle T012
60
61 Vector2D E01 = v10 - v00;
62 Vector2D E02 = v11 - v00;
63
64 double N012 = cross2d(E01, E02);
65
66 // normal for sub-triangle T120
67
68 Vector2D E12 = v11 - v10;
69 Vector2D E10 = v00 - v10;
70
71 double N120 = cross2d(E12, E10);
72
73 // normal for sub-triangle T201
74
75 Vector2D E20 = -E02;
76 Vector2D E21 = -E12;
77
78 double N201 = cross2d(E20, E21);
79
80 // all normals must point same way
81
82 double dot1 = N120 * N012;
83 double dot2 = N201 * N120;
84 double dot3 = N201 * N012;
85
86 // all normals must point same way
87 bool is_inside = ((dot1 >= 0. && dot2 >= 0. && dot3 >= 0.) || (dot1 <= 0. && dot2 <= 0. && dot3 <= 0.));
88 return is_inside;
89 }
90
91 double Triag2D::area() const {
92 return std::abs(0.5 * cross2d((v10 - v00), (v11 - v00)));
93 }
94
95 bool Triag2D::inTriangle(const Vector2D& p, double tolerance) const {
96 // point p must be on the inside of all triangle edges to be inside the triangle.
97 return cross2d(p - v00, p - v10) > -tolerance && cross2d(p - v10, p - v11) > -tolerance &&
98 cross2d(p - v11, p - v00) > -tolerance;
99 }
100
101 void Triag2D::print(std::ostream& s) const {
102 auto printVector2D = [&s](const Vector2D& v) { s << "[" << v[0] << "," << v[1] << "]"; };
103 s << "Triag2D[";
104 printVector2D(v00);
105 s << ", ";
106 printVector2D(v11);
107 s << ", ";
108 printVector2D(v10);
109 s << "]";
110 }
111
112
113 //----------------------------------------------------------------------------------------------------------------------
114
115 } // namespace element
116 } // namespace interpolation
117 } // namespace atlas