| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 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 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <cmath> |
| 12 |
#include <iostream> |
| 13 |
|
| 14 |
#include "atlas/interpolation/element/Quad3D.h" |
| 15 |
#include "atlas/interpolation/element/Triag3D.h" |
| 16 |
#include "atlas/interpolation/method/Ray.h" |
| 17 |
#include "atlas/runtime/Log.h" |
| 18 |
|
| 19 |
namespace atlas { |
| 20 |
namespace interpolation { |
| 21 |
namespace element { |
| 22 |
|
| 23 |
//---------------------------------------------------------------------------------------------------------------------- |
| 24 |
|
| 25 |
method::Intersect Quad3D::intersects(const method::Ray& r, double edgeEpsilon, double epsilon) const { |
| 26 |
method::Intersect isect; // intersection is false |
| 27 |
|
| 28 |
Triag3D T013(v00, v10, v01); |
| 29 |
isect = T013.intersects(r, edgeEpsilon, epsilon); |
| 30 |
if (isect) { |
| 31 |
return isect; |
| 32 |
} |
| 33 |
|
| 34 |
Triag3D T231(v11, v01, v10); |
| 35 |
isect = T231.intersects(r, edgeEpsilon, epsilon); |
| 36 |
if (isect) { |
| 37 |
isect.u = 1 - isect.u; |
| 38 |
isect.v = 1 - isect.v; |
| 39 |
return isect; |
| 40 |
} |
| 41 |
|
| 42 |
return isect.fail(); |
| 43 |
} |
| 44 |
|
| 45 |
bool Quad3D::validate() const { |
| 46 |
// normal for sub-triangle T231 |
| 47 |
|
| 48 |
Vector3D E23 = v01 - v11; |
| 49 |
Vector3D E21 = v10 - v11; |
| 50 |
|
| 51 |
Vector3D N231 = E23.cross(E21); |
| 52 |
|
| 53 |
// normal for sub-triangle T013 |
| 54 |
|
| 55 |
Vector3D E01 = v10 - v00; |
| 56 |
Vector3D E03 = v01 - v00; |
| 57 |
|
| 58 |
Vector3D N013 = E01.cross(E03); |
| 59 |
|
| 60 |
// normal for sub-triangle T120 |
| 61 |
|
| 62 |
Vector3D E12 = -E21; |
| 63 |
Vector3D E10 = -E01; |
| 64 |
|
| 65 |
Vector3D N120 = E12.cross(E10); |
| 66 |
|
| 67 |
// normal for sub-triangle T302 |
| 68 |
|
| 69 |
Vector3D E30 = -E03; |
| 70 |
Vector3D E32 = -E23; |
| 71 |
|
| 72 |
Vector3D N302 = E30.cross(E32); |
| 73 |
|
| 74 |
// all normals must point same way |
| 75 |
|
| 76 |
double dot02 = N231.dot(N013); |
| 77 |
double dot23 = N013.dot(N120); |
| 78 |
double dot31 = N120.dot(N302); |
| 79 |
double dot10 = N302.dot(N231); |
| 80 |
|
| 81 |
// all normals must point same way |
| 82 |
bool is_inside = ((dot02 >= 0. && dot23 >= 0. && dot31 >= 0. && dot10 >= 0.) || |
| 83 |
(dot02 <= 0. && dot23 <= 0. && dot31 <= 0. && dot10 <= 0.)); |
| 84 |
return is_inside; |
| 85 |
} |
| 86 |
|
| 87 |
double Quad3D::area() const { |
| 88 |
Triag3D T013(v00, v10, v01); |
| 89 |
Triag3D T231(v11, v01, v10); |
| 90 |
|
| 91 |
return T013.area() + T231.area(); |
| 92 |
} |
| 93 |
|
| 94 |
void Quad3D::print(std::ostream& s) const { |
| 95 |
auto printVector3D = [&s](const Vector3D& v) { s << "[" << v[0] << "," << v[1] << "," << v[2] << "]"; }; |
| 96 |
s << "Quad3D["; |
| 97 |
printVector3D(v00); |
| 98 |
s << ", "; |
| 99 |
printVector3D(v10); |
| 100 |
s << ", "; |
| 101 |
printVector3D(v11); |
| 102 |
s << ", "; |
| 103 |
printVector3D(v01); |
| 104 |
s << "]"; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
//---------------------------------------------------------------------------------------------------------------------- |
| 109 |
|
| 110 |
} // namespace element |
| 111 |
} // namespace interpolation |
| 112 |
} // namespace atlas |