| 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/Triag3D.h" |
| 15 |
#include "atlas/interpolation/method/Intersect.h" |
| 16 |
#include "atlas/interpolation/method/Ray.h" |
| 17 |
|
| 18 |
//---------------------------------------------------------------------------------------------------------------------- |
| 19 |
|
| 20 |
namespace atlas { |
| 21 |
namespace interpolation { |
| 22 |
namespace element { |
| 23 |
|
| 24 |
method::Intersect Triag3D::intersects(const method::Ray& r, double edgeEpsilon, double epsilon) const { |
| 25 |
method::Intersect isect; |
| 26 |
|
| 27 |
Vector3D edge1 = v1 - v0; |
| 28 |
Vector3D edge2 = v2 - v0; |
| 29 |
Vector3D pvec = r.dir.cross(edge2); |
| 30 |
|
| 31 |
// ray is parallel to triangle (check?) |
| 32 |
const double det = edge1.dot(pvec); |
| 33 |
if (fabs(det) < epsilon) { |
| 34 |
return isect.fail(); |
| 35 |
} |
| 36 |
|
| 37 |
const double invDet = 1. / det; |
| 38 |
|
| 39 |
Vector3D tvec = r.orig - v0; |
| 40 |
Vector3D qvec = tvec.cross(edge1); |
| 41 |
|
| 42 |
isect.u = tvec.dot(pvec) * invDet; |
| 43 |
isect.v = r.dir.dot(qvec) * invDet; |
| 44 |
isect.t = edge2.dot(qvec) * invDet; |
| 45 |
|
| 46 |
const double w = 1 - (isect.u + isect.v); |
| 47 |
|
| 48 |
if (w < 0) { |
| 49 |
// check if far outside of triangle, in respect to diagonal edge |
| 50 |
if (w < -edgeEpsilon) { |
| 51 |
return isect.fail(); |
| 52 |
} |
| 53 |
|
| 54 |
// snap to diagonal edge |
| 55 |
// Note: we may still be to the left of vertical edge or below the |
| 56 |
// horizontal edge |
| 57 |
isect.u += 0.5 * w; |
| 58 |
isect.v += 0.5 * w; |
| 59 |
} |
| 60 |
|
| 61 |
if (isect.u < 0) { |
| 62 |
// check if far outside of triangle, in respect to vertical edge |
| 63 |
if ((isect.u < -edgeEpsilon) || (isect.v < -edgeEpsilon) || (isect.v > 1 + edgeEpsilon)) { |
| 64 |
return isect.fail(); |
| 65 |
} |
| 66 |
|
| 67 |
// snap to lower/upper left corners |
| 68 |
isect.u = 0; |
| 69 |
if (isect.v < 0) { |
| 70 |
isect.v = 0; |
| 71 |
} |
| 72 |
else if (isect.v > 1) { |
| 73 |
isect.v = 1; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (isect.v < 0) { |
| 78 |
// check if far outside of triangle, in respect to horizontal edge |
| 79 |
if ((isect.v < -edgeEpsilon) || (isect.u < -edgeEpsilon) || (isect.u > 1 + edgeEpsilon)) { |
| 80 |
return isect.fail(); |
| 81 |
} |
| 82 |
|
| 83 |
// snap to lower left/right corners |
| 84 |
isect.v = 0; |
| 85 |
if (isect.u < 0) { |
| 86 |
isect.u = 0; |
| 87 |
} |
| 88 |
else if (isect.u > 1) { |
| 89 |
isect.u = 1; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return isect.success(); |
| 94 |
} |
| 95 |
|
| 96 |
double Triag3D::area() const { |
| 97 |
Vector3D edge1 = v1 - v0; |
| 98 |
Vector3D edge2 = v2 - v0; |
| 99 |
|
| 100 |
Vector3D cross = edge1.cross(edge2); |
| 101 |
|
| 102 |
return 0.5 * cross.norm(); |
| 103 |
} |
| 104 |
|
| 105 |
void Triag3D::print(std::ostream& s) const { |
| 106 |
s << "Triag3D[" |
| 107 |
<< "v0=(" << v0[0] << ", " << v0[1] << ", " << v0[2] << "), v1=(" << v1[0] << ", " << v1[1] << ", " << v1[2] |
| 108 |
<< "), v2=(" << v2[0] << ", " << v2[1] << ", " << v2[2] << ")]"; |
| 109 |
} |
| 110 |
|
| 111 |
//---------------------------------------------------------------------------------------------------------------------- |
| 112 |
|
| 113 |
} // namespace element |
| 114 |
} // namespace interpolation |
| 115 |
} // namespace atlas |