| 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 element { |
| 21 |
|
| 22 |
static constexpr double BAD_WEIGHT_VALUE = -1.; |
| 23 |
|
| 24 |
//---------------------------------------------------------------------------------------------------------------------- |
| 25 |
|
| 26 |
class Triag2D { |
| 27 |
public: |
| 28 |
Triag2D(const double* x0, const double* x1, const double* x2): v00(x0), v10(x1), v11(x2) {} |
| 29 |
|
| 30 |
Triag2D(const PointXY& x0, const PointXY& x1, const PointXY& x2): Triag2D(x0.data(), x1.data(), x2.data()) {} |
| 31 |
|
| 32 |
Triag2D(const Vector2D& x0, const Vector2D& x1, const Vector2D& x2): Triag2D(x0.data(), x1.data(), x2.data()) {} |
| 33 |
|
| 34 |
method::Intersect intersects(const PointXY& r, double edgeEpsilon = 5 * std::numeric_limits<double>::epsilon(), |
| 35 |
double epsilon = 5 * std::numeric_limits<double>::epsilon()) const; |
| 36 |
|
| 37 |
bool validate() const; |
| 38 |
|
| 39 |
double area() const; |
| 40 |
|
| 41 |
void print(std::ostream&) const; |
| 42 |
|
| 43 |
friend std::ostream& operator<<(std::ostream& s, const Triag2D& p) { |
| 44 |
p.print(s); |
| 45 |
return s; |
| 46 |
} |
| 47 |
|
| 48 |
const Vector2D& p(int i) { |
| 49 |
if (i == 0) |
| 50 |
return v00; |
| 51 |
if (i == 1) |
| 52 |
return v10; |
| 53 |
if (i == 2) |
| 54 |
return v11; |
| 55 |
throw_OutOfRange("Triag2D::p(i)", i, 3, Here()); |
| 56 |
} |
| 57 |
|
| 58 |
private: // members |
| 59 |
Vector2D v00; // aka v0 |
| 60 |
Vector2D v10; // aka v1 |
| 61 |
Vector2D v11; // aka v2 |
| 62 |
|
| 63 |
static double cross2d(const Vector2D& a, const Vector2D& b) { return a.x() * b.y() - a.y() * b.x(); } |
| 64 |
|
| 65 |
bool inTriangle(const Vector2D& p, double tolerance = 5 * std::numeric_limits<double>::epsilon()) const; |
| 66 |
}; |
| 67 |
|
| 68 |
//---------------------------------------------------------------------------------------------------------------------- |
| 69 |
|
| 70 |
} // namespace element |
| 71 |
} // namespace interpolation |
| 72 |
} // namespace atlas |