| 1 |
/* |
| 2 |
* (C) Crown Copyright 2022 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 |
#include "atlas/interpolation/method/cubedsphere/CellFinder.h" |
| 9 |
#include "atlas/grid/CubedSphereGrid.h" |
| 10 |
#include "atlas/interpolation/Vector2D.h" |
| 11 |
#include "atlas/interpolation/element/Quad2D.h" |
| 12 |
#include "atlas/interpolation/element/Triag2D.h" |
| 13 |
#include "atlas/mesh/HybridElements.h" |
| 14 |
#include "atlas/mesh/Nodes.h" |
| 15 |
#include "atlas/util/Topology.h" |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
namespace interpolation { |
| 19 |
namespace method { |
| 20 |
namespace cubedsphere { |
| 21 |
|
| 22 |
CellFinder::CellFinder(const Mesh& mesh, const util::Config& config): mesh_{mesh} { |
| 23 |
// Check mesh and get projection. |
| 24 |
const auto csGrid = CubedSphereGrid(mesh_.grid()); |
| 25 |
ATLAS_ASSERT_MSG(csGrid, "cubedsphere::CellFinder requires a cubed sphere mesh."); |
| 26 |
projection_ = &(csGrid.cubedSphereProjection()); |
| 27 |
|
| 28 |
// Get views to cell data. |
| 29 |
const auto lonlatView = array::make_view<double, 2>(mesh_.cells().field("lonlat")); |
| 30 |
const auto haloView = array::make_view<int, 1>(mesh_.cells().halo()); |
| 31 |
|
| 32 |
// make points and payloads vectors. |
| 33 |
auto points = std::vector<PointLonLat>{}; |
| 34 |
auto payloads = std::vector<idx_t>{}; |
| 35 |
|
| 36 |
// Iterate over cells. |
| 37 |
auto halo = config.getInt("halo", 0); |
| 38 |
for (idx_t i = 0; i < mesh_.cells().size(); ++i) { |
| 39 |
if (haloView(i) <= halo) { |
| 40 |
points.emplace_back(lonlatView(i, LON), lonlatView(i, LAT)); |
| 41 |
payloads.emplace_back(i); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// build cell kd-tree. |
| 46 |
tree_.build(points, payloads); |
| 47 |
} |
| 48 |
|
| 49 |
CellFinder::Cell CellFinder::getCell(const PointLonLat& lonlat, size_t listSize, double edgeEpsilon, double epsilon) const { |
| 50 |
// Convert xy to alphabeta and t; |
| 51 |
// const auto& tiles = projection_->getCubedSphereTiles(); |
| 52 |
|
| 53 |
// Get mesh nodes and connectivity table. |
| 54 |
const auto nodeXyView = array::make_view<double, 2>(mesh_.nodes().xy()); |
| 55 |
const auto& nodeConnectivity = mesh_.cells().node_connectivity(); |
| 56 |
|
| 57 |
// Get four nearest cell-centres to xy. |
| 58 |
if (tree_.size() == 0) { |
| 59 |
return Cell{-1, {}, Intersect{}.fail()}; |
| 60 |
} |
| 61 |
|
| 62 |
const auto valueList = tree_.closestPoints(lonlat, std::min(listSize, tree_.size())); |
| 63 |
|
| 64 |
// Get view to cell flags. |
| 65 |
const auto cellFlagsView = array::make_view<int, 1>(mesh_.cells().flags()); |
| 66 |
const auto cellTijView = array::make_view<idx_t, 2>(mesh_.cells().field("tij")); |
| 67 |
|
| 68 |
for (const auto& value : valueList) { |
| 69 |
const auto i = value.payload(); |
| 70 |
|
| 71 |
// Ignore invalid cells. |
| 72 |
if ((cellFlagsView(i) & Topology::INVALID)) { |
| 73 |
break; |
| 74 |
} |
| 75 |
|
| 76 |
const auto t = cellTijView(i,0); |
| 77 |
const auto row = nodeConnectivity.row(i); |
| 78 |
|
| 79 |
if (row.size() == 4) { |
| 80 |
auto quadAlphabeta = std::array<Vector2D, 4>{}; |
| 81 |
for (size_t k = 0; k < 4; ++k) { |
| 82 |
const auto xyNode = PointXY(nodeXyView(row(k), XX), nodeXyView(row(k), YY)); |
| 83 |
quadAlphabeta[k] = Vector2D(projection_->xy2alphabeta(xyNode, t).data()); |
| 84 |
} |
| 85 |
|
| 86 |
const auto quad = |
| 87 |
element::Quad2D(quadAlphabeta[0], quadAlphabeta[1], quadAlphabeta[2], quadAlphabeta[3]); |
| 88 |
|
| 89 |
const auto alphabeta = projection_->lonlat2alphabeta(lonlat, t); |
| 90 |
const auto isect = quad.localRemap(alphabeta, edgeEpsilon, epsilon); |
| 91 |
|
| 92 |
if (isect) { |
| 93 |
return Cell{i, {row(0), row(1), row(2), row(3)}, isect}; |
| 94 |
} |
| 95 |
} |
| 96 |
else { |
| 97 |
// Cell is triangle. |
| 98 |
auto triagAlphabeta = std::array<Vector2D, 3>{}; |
| 99 |
for (size_t k = 0; k < 3; ++k) { |
| 100 |
const auto xyNode = PointXY(nodeXyView(row(k), XX), nodeXyView(row(k), YY)); |
| 101 |
triagAlphabeta[k] = Vector2D(projection_->xy2alphabeta(xyNode, t).data()); |
| 102 |
} |
| 103 |
|
| 104 |
const auto triag = element::Triag2D(triagAlphabeta[0], triagAlphabeta[1], triagAlphabeta[2]); |
| 105 |
|
| 106 |
const auto alphabeta = projection_->lonlat2alphabeta(lonlat, t); |
| 107 |
const auto isect = triag.intersects(alphabeta, edgeEpsilon, epsilon); |
| 108 |
|
| 109 |
if (isect) { |
| 110 |
return Cell{i, {row(0), row(1), row(2)}, isect}; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Couldn't find a cell. |
| 116 |
return Cell{-1, {}, Intersect{}.fail()}; |
| 117 |
} |
| 118 |
|
| 119 |
CellFinder::Cell CellFinder::getCell(const PointXY& xy, size_t listSize, double edgeEpsilon, |
| 120 |
double epsilon) const { |
| 121 |
const auto lonlat = projection_->lonlat(xy); |
| 122 |
return getCell(lonlat, listSize, edgeEpsilon, epsilon); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
} // namespace cubedsphere |
| 127 |
} // namespace method |
| 128 |
} // namespace interpolation |
| 129 |
} // namespace atlas |