| 1 |
#include "QhullSphericalTriangulation.h" |
| 2 |
|
| 3 |
#include "atlas/library/defines.h" |
| 4 |
|
| 5 |
#include <cmath> |
| 6 |
#include <random> |
| 7 |
#include <algorithm> |
| 8 |
#include <utility> |
| 9 |
|
| 10 |
#if ATLAS_HAVE_QHULL |
| 11 |
|
| 12 |
// Suppress a few warnings present in third-party libqhullcpp |
| 13 |
ATLAS_SUPPRESS_WARNINGS_PUSH |
| 14 |
ATLAS_SUPPRESS_WARNINGS_INTEGER_SIGN_CHANGE |
| 15 |
ATLAS_SUPPRESS_WARNINGS_CODE_IS_UNREACHABLE |
| 16 |
ATLAS_SUPPRESS_WARNINGS_TEMPLATE_ID_CDTOR |
| 17 |
|
| 18 |
#include <libqhullcpp/Qhull.h> |
| 19 |
#include <libqhullcpp/QhullFacet.h> |
| 20 |
#include <libqhullcpp/QhullFacetList.h> |
| 21 |
#include <libqhullcpp/QhullVertexSet.h> |
| 22 |
#include <libqhullcpp/QhullPoints.h> |
| 23 |
|
| 24 |
ATLAS_SUPPRESS_WARNINGS_POP |
| 25 |
|
| 26 |
#endif |
| 27 |
|
| 28 |
#include "atlas/runtime/Exception.h" |
| 29 |
|
| 30 |
namespace atlas{ |
| 31 |
namespace util{ |
| 32 |
|
| 33 |
#if ATLAS_HAVE_QHULL |
| 34 |
struct QhullSphericalTriangulation::Qhull : public orgQhull::Qhull { |
| 35 |
using orgQhull::Qhull::Qhull; |
| 36 |
}; |
| 37 |
#else |
| 38 |
struct QhullSphericalTriangulation::Qhull { |
| 39 |
template<typename... Args> |
| 40 |
Qhull(Args...) { |
| 41 |
throw_Exception("Atlas has not been compiled with Qhull",Here()); |
| 42 |
} |
| 43 |
}; |
| 44 |
#endif |
| 45 |
|
| 46 |
QhullSphericalTriangulation::~QhullSphericalTriangulation() = default; |
| 47 |
|
| 48 |
QhullSphericalTriangulation::QhullSphericalTriangulation(size_t N, const double lonlat[]) : |
| 49 |
QhullSphericalTriangulation(N, lonlat, lonlat+1, 2, 2) { |
| 50 |
} |
| 51 |
|
| 52 |
QhullSphericalTriangulation::QhullSphericalTriangulation(size_t N, const double lon[], const double lat[]) : |
| 53 |
QhullSphericalTriangulation(N, lon, lat, 1, 1) { |
| 54 |
} |
| 55 |
|
| 56 |
QhullSphericalTriangulation::QhullSphericalTriangulation(size_t N, const double lon[], const double lat[], int lon_stride, int lat_stride) { |
| 57 |
auto lonlat2xyz = [](double lon, double lat, auto& xyz) { |
| 58 |
constexpr double deg2rad = M_PI / 180.; |
| 59 |
const double lambda = deg2rad * lon; |
| 60 |
const double phi = deg2rad * lat; |
| 61 |
|
| 62 |
const double sin_phi = std::sin(phi); |
| 63 |
const double cos_phi = std::cos(phi); |
| 64 |
const double sin_lambda = std::sin(lambda); |
| 65 |
const double cos_lambda = std::cos(lambda); |
| 66 |
|
| 67 |
xyz[0] = cos_phi * cos_lambda; |
| 68 |
xyz[1] = cos_phi * sin_lambda; |
| 69 |
xyz[2] = sin_phi; |
| 70 |
}; |
| 71 |
|
| 72 |
points_xyz_.resize(N); |
| 73 |
|
| 74 |
for (size_t i = 0; i < N; ++i) { |
| 75 |
lonlat2xyz(lon[i*lon_stride], lat[i*lat_stride], points_xyz_[i]); |
| 76 |
} |
| 77 |
|
| 78 |
constexpr int dim = 3; |
| 79 |
constexpr const char* command = "Qt"; |
| 80 |
constexpr const char* comment = ""; |
| 81 |
const double* coordinates = reinterpret_cast<const double*>(points_xyz_.data()); |
| 82 |
qhull_ = std::make_unique<Qhull>(comment, dim, N, coordinates, command); |
| 83 |
} |
| 84 |
|
| 85 |
size_t QhullSphericalTriangulation::size() const { |
| 86 |
#if ATLAS_HAVE_QHULL |
| 87 |
return qhull_->facetList().size(); |
| 88 |
#else |
| 89 |
return 0; |
| 90 |
#endif |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
template <typename Qhull, typename Points, typename Value> |
| 95 |
static inline void qhull_get_triangles(const Qhull& qhull, const Points& points_xyz_, std::array<Value,3> triangles[]) { |
| 96 |
#if ATLAS_HAVE_QHULL |
| 97 |
auto ensure_outward_normal = [&](auto& tri) { |
| 98 |
|
| 99 |
auto dot = [](const auto& p1, const auto& p2) { |
| 100 |
return p1[0]*p2[0] + p1[1]*p2[1] + p1[2]*p2[2]; |
| 101 |
}; |
| 102 |
auto cross = [](const auto& p1, const auto& p2) { |
| 103 |
return std::array<double,3>{ |
| 104 |
p1[1] * p2[2] - p1[2] * p2[1], p1[2] * p2[0] - p1[0] * p2[2], |
| 105 |
p1[0] * p2[1] - p1[1] * p2[0] |
| 106 |
}; |
| 107 |
}; |
| 108 |
|
| 109 |
const std::array<double,3>& a = points_xyz_[tri[0]]; |
| 110 |
const std::array<double,3>& b = points_xyz_[tri[1]]; |
| 111 |
const std::array<double,3>& c = points_xyz_[tri[2]]; |
| 112 |
std::array<double,3> ba {a[0]-b[0], a[1]-b[1], a[2]-b[2]}; |
| 113 |
std::array<double,3> bc {c[0]-b[0], c[1]-b[1], c[2]-b[2]}; |
| 114 |
|
| 115 |
bool outward = dot(b, cross(bc,ba)) > 0; |
| 116 |
|
| 117 |
if (not outward) { |
| 118 |
std::swap(tri[1], tri[2]); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
size_t jtri{0}; |
| 123 |
for (const auto& facet : qhull.facetList()){ |
| 124 |
auto& tri = triangles[jtri++]; |
| 125 |
size_t jvrt{0}; |
| 126 |
for( const auto& vertex: facet.vertices()){ |
| 127 |
tri[jvrt++] = vertex.point().id(); |
| 128 |
} |
| 129 |
|
| 130 |
ensure_outward_normal(tri); |
| 131 |
} |
| 132 |
#endif |
| 133 |
} |
| 134 |
|
| 135 |
void QhullSphericalTriangulation::triangles(std::array<int,3> triangles[]) const { |
| 136 |
qhull_get_triangles(*qhull_,points_xyz_,triangles); |
| 137 |
} |
| 138 |
|
| 139 |
void QhullSphericalTriangulation::triangles(std::array<long,3> triangles[]) const { |
| 140 |
qhull_get_triangles(*qhull_,points_xyz_,triangles); |
| 141 |
} |
| 142 |
|
| 143 |
void QhullSphericalTriangulation::triangles(std::array<long long,3> triangles[]) const { |
| 144 |
qhull_get_triangles(*qhull_,points_xyz_,triangles); |
| 145 |
} |
| 146 |
|
| 147 |
void QhullSphericalTriangulation::triangles(std::array<size_t,3> triangles[]) const { |
| 148 |
qhull_get_triangles(*qhull_,points_xyz_,triangles); |
| 149 |
} |
| 150 |
|
| 151 |
std::vector<std::array<idx_t,3>> QhullSphericalTriangulation::triangles() const { |
| 152 |
return triangles<idx_t>(); |
| 153 |
} |
| 154 |
|
| 155 |
} // namespace util |
| 156 |
} // namespace atlas |