| 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 |
#pragma once |
| 9 |
|
| 10 |
#include <iosfwd> |
| 11 |
#include <limits> |
| 12 |
|
| 13 |
#include "atlas/interpolation/Vector2D.h" |
| 14 |
#include "atlas/interpolation/method/Intersect.h" |
| 15 |
#include "atlas/runtime/Exception.h" |
| 16 |
#include "atlas/util/Point.h" |
| 17 |
|
| 18 |
namespace atlas { |
| 19 |
namespace interpolation { |
| 20 |
namespace method { |
| 21 |
struct Ray; |
| 22 |
} |
| 23 |
namespace element { |
| 24 |
|
| 25 |
//---------------------------------------------------------------------------------------------------------------------- |
| 26 |
|
| 27 |
class Quad2D { |
| 28 |
public: |
| 29 |
Quad2D(const double* x0, const double* x1, const double* x2, const double* x3): |
| 30 |
v00(x0), v10(x1), v11(x2), v01(x3) {} |
| 31 |
|
| 32 |
Quad2D(const Point2& x0, const Point2& x1, const Point2& x2, const Point2& x3): |
| 33 |
Quad2D(x0.data(), x1.data(), x2.data(), x3.data()) {} |
| 34 |
|
| 35 |
Quad2D(const Vector2D& x0, const Vector2D& x1, const Vector2D& x2, const Vector2D& x3): |
| 36 |
Quad2D(x0.data(), x1.data(), x2.data(), x3.data()) {} |
| 37 |
|
| 38 |
method::Intersect intersects(const Point2& r, double edgeEpsilon = 5 * std::numeric_limits<double>::epsilon(), |
| 39 |
double epsilon = 5 * std::numeric_limits<double>::epsilon()) const; |
| 40 |
|
| 41 |
method::Intersect localRemap(const Point2& r, double edgeEpsilon = 5 * std::numeric_limits<double>::epsilon(), |
| 42 |
double epsilon = 5 * std::numeric_limits<double>::epsilon()) const; |
| 43 |
|
| 44 |
bool validate() const; |
| 45 |
|
| 46 |
double area() const; |
| 47 |
|
| 48 |
const Vector2D& p(int i) { |
| 49 |
if (i == 0) { |
| 50 |
return v00; |
| 51 |
} |
| 52 |
if (i == 1) { |
| 53 |
return v10; |
| 54 |
} |
| 55 |
if (i == 2) { |
| 56 |
return v11; |
| 57 |
} |
| 58 |
if (i == 3) { |
| 59 |
return v01; |
| 60 |
} |
| 61 |
throw_OutOfRange("Quad2D::p(i)", i, 4, Here()); |
| 62 |
} |
| 63 |
|
| 64 |
void print(std::ostream&) const; |
| 65 |
|
| 66 |
friend std::ostream& operator<<(std::ostream& s, const Quad2D& p) { |
| 67 |
p.print(s); |
| 68 |
return s; |
| 69 |
} |
| 70 |
|
| 71 |
private: // members |
| 72 |
Vector2D v00; // aka v0 |
| 73 |
Vector2D v10; // aka v1 |
| 74 |
Vector2D v11; // aka v2 |
| 75 |
Vector2D v01; // aka v3 |
| 76 |
|
| 77 |
bool inQuadrilateral(const Vector2D& p, double tolerance = 5 * std::numeric_limits<double>::epsilon()) const; |
| 78 |
|
| 79 |
static double cross2d(const Vector2D& a, const Vector2D& b) { return a.x() * b.y() - a.y() * b.x(); } |
| 80 |
}; |
| 81 |
|
| 82 |
//---------------------------------------------------------------------------------------------------------------------- |
| 83 |
|
| 84 |
} // namespace element |
| 85 |
} // namespace interpolation |
| 86 |
} // namespace atlas |