| 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 |
* 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 <algorithm> |
| 12 |
#include <iostream> |
| 13 |
#include <ostream> |
| 14 |
|
| 15 |
#include "atlas/grid/detail/tiles/LFRicTiles.h" |
| 16 |
#include "atlas/grid/detail/tiles/Tiles.h" |
| 17 |
#include "atlas/grid/detail/tiles/TilesFactory.h" |
| 18 |
#include "atlas/projection/detail/ProjectionUtilities.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
#include "atlas/runtime/Log.h" |
| 21 |
#include "atlas/util/CoordinateEnums.h" |
| 22 |
|
| 23 |
namespace atlas { |
| 24 |
namespace grid { |
| 25 |
namespace detail { |
| 26 |
|
| 27 |
namespace { |
| 28 |
|
| 29 |
static constexpr bool debug = false; // constexpr so compiler can optimize `if ( debug ) { ... }` out |
| 30 |
static constexpr double epsilon = 1.e-12; |
| 31 |
|
| 32 |
using projection::detail::ProjectionUtilities; |
| 33 |
|
| 34 |
static bool is_tiny(const double& x) { |
| 35 |
return (std::abs(x) < epsilon); |
| 36 |
} |
| 37 |
|
| 38 |
static bool is_same(const double& x, const double& y, const double& tol = 1.0) { |
| 39 |
return (std::abs(x - y) < epsilon * tol); |
| 40 |
} |
| 41 |
|
| 42 |
static bool is_less(const double& lhs, const double& rhs) { |
| 43 |
return lhs < rhs - epsilon; |
| 44 |
} |
| 45 |
|
| 46 |
static bool is_geq(const double& lhs, const double& rhs) { |
| 47 |
return lhs >= rhs - epsilon; |
| 48 |
} |
| 49 |
|
| 50 |
static void sphericalToCartesian(const double lonlat[], double xyz[]) { |
| 51 |
auto crd_sys = ProjectionUtilities::CoordinateSystem::LEFT_HAND; |
| 52 |
constexpr double radius = 1.; |
| 53 |
ProjectionUtilities::sphericalToCartesian(lonlat, xyz, crd_sys, radius); |
| 54 |
} |
| 55 |
|
| 56 |
static PointXY rotatePlus90AboutPt(const PointXY& xy, const PointXY& origin) { |
| 57 |
return PointXY{-xy.y() + origin.x() + origin.y(), xy.x() - origin.x() + origin.y()}; |
| 58 |
} |
| 59 |
|
| 60 |
static PointXY rotateMinus90AboutPt(const PointXY& xy, const PointXY& origin) { |
| 61 |
return PointXY{xy.y() + origin.x() - origin.y(), -xy.x() + origin.x() + origin.y()}; |
| 62 |
} |
| 63 |
|
| 64 |
static PointXY rotatePlus180AboutPt(const PointXY& xy, const PointXY& origin) { |
| 65 |
return PointXY{2.0 * origin.x() - xy.x(), 2.0 * origin.y() - xy.y()}; |
| 66 |
} |
| 67 |
|
| 68 |
} // anonymous namespace |
| 69 |
|
| 70 |
// constructor |
| 71 |
LFRicCubedSphereTiles::LFRicCubedSphereTiles(const eckit::Parametrisation&) {} |
| 72 |
|
| 73 |
std::array<std::array<double, 6>, 2> LFRicCubedSphereTiles::xy2abOffsets() const { |
| 74 |
return {{{0., 1., 2., 3., 0., 0.}, {1., 1., 1., 1., 2., 0.}}}; |
| 75 |
} |
| 76 |
|
| 77 |
std::array<std::array<double, 6>, 2> LFRicCubedSphereTiles::ab2xyOffsets() const { |
| 78 |
return {{{0., 90., 180., 270., 0., 0.}, {-45., -45., -45., -45., 45., -135.}}}; |
| 79 |
} |
| 80 |
|
| 81 |
void tile0Rotate(double xyz[]) { |
| 82 |
// Face 0, no rotation. |
| 83 |
} |
| 84 |
|
| 85 |
void tile1Rotate(double xyz[]) { |
| 86 |
double xyz_in[3]; |
| 87 |
std::copy(xyz, xyz + 3, xyz_in); |
| 88 |
xyz[XX] = -xyz_in[YY]; |
| 89 |
xyz[YY] = xyz_in[XX]; |
| 90 |
} |
| 91 |
|
| 92 |
void tile2Rotate(double xyz[]) { |
| 93 |
double xyz_in[3]; |
| 94 |
std::copy(xyz, xyz + 3, xyz_in); |
| 95 |
xyz[XX] = -xyz_in[XX]; |
| 96 |
xyz[YY] = -xyz_in[YY]; |
| 97 |
} |
| 98 |
|
| 99 |
void tile3Rotate(double xyz[]) { |
| 100 |
double xyz_in[3]; |
| 101 |
std::copy(xyz, xyz + 3, xyz_in); |
| 102 |
xyz[XX] = xyz_in[YY]; |
| 103 |
xyz[YY] = -xyz_in[XX]; |
| 104 |
} |
| 105 |
|
| 106 |
void tile4Rotate(double xyz[]) { |
| 107 |
double xyz_in[3]; |
| 108 |
std::copy(xyz, xyz + 3, xyz_in); |
| 109 |
xyz[XX] = xyz_in[ZZ]; |
| 110 |
xyz[ZZ] = -xyz_in[XX]; |
| 111 |
} |
| 112 |
|
| 113 |
void tile5Rotate(double xyz[]) { |
| 114 |
double xyz_in[3]; |
| 115 |
std::copy(xyz, xyz + 3, xyz_in); |
| 116 |
xyz[XX] = -xyz_in[ZZ]; |
| 117 |
xyz[ZZ] = xyz_in[XX]; |
| 118 |
} |
| 119 |
|
| 120 |
void tile0RotateInverse(double xyz[]) { |
| 121 |
// Face 0, no rotation. |
| 122 |
} |
| 123 |
|
| 124 |
void tile1RotateInverse(double xyz[]) { |
| 125 |
double xyz_in[3]; |
| 126 |
std::copy(xyz, xyz + 3, xyz_in); |
| 127 |
xyz[XX] = xyz_in[YY]; |
| 128 |
xyz[YY] = -xyz_in[XX]; |
| 129 |
} |
| 130 |
|
| 131 |
void tile2RotateInverse(double xyz[]) { |
| 132 |
double xyz_in[3]; |
| 133 |
std::copy(xyz, xyz + 3, xyz_in); |
| 134 |
xyz[XX] = -xyz_in[XX]; |
| 135 |
xyz[YY] = -xyz_in[YY]; |
| 136 |
} |
| 137 |
|
| 138 |
void tile3RotateInverse(double xyz[]) { |
| 139 |
double xyz_in[3]; |
| 140 |
std::copy(xyz, xyz + 3, xyz_in); |
| 141 |
xyz[XX] = -xyz_in[YY]; |
| 142 |
xyz[YY] = xyz_in[XX]; |
| 143 |
} |
| 144 |
|
| 145 |
void tile4RotateInverse(double xyz[]) { |
| 146 |
double xyz_in[3]; |
| 147 |
std::copy(xyz, xyz + 3, xyz_in); |
| 148 |
xyz[XX] = -xyz_in[ZZ]; |
| 149 |
xyz[ZZ] = xyz_in[XX]; |
| 150 |
} |
| 151 |
|
| 152 |
void tile5RotateInverse(double xyz[]) { |
| 153 |
double xyz_in[3]; |
| 154 |
std::copy(xyz, xyz + 3, xyz_in); |
| 155 |
xyz[XX] = xyz_in[ZZ]; |
| 156 |
xyz[ZZ] = -xyz_in[XX]; |
| 157 |
} |
| 158 |
|
| 159 |
void LFRicCubedSphereTiles::rotate(idx_t t, double xyz[]) const { |
| 160 |
switch (t) { |
| 161 |
case 0: { |
| 162 |
tile0Rotate(xyz); |
| 163 |
break; |
| 164 |
} |
| 165 |
case 1: { |
| 166 |
tile1Rotate(xyz); |
| 167 |
break; |
| 168 |
} |
| 169 |
case 2: { |
| 170 |
tile2Rotate(xyz); |
| 171 |
break; |
| 172 |
} |
| 173 |
case 3: { |
| 174 |
tile3Rotate(xyz); |
| 175 |
break; |
| 176 |
} |
| 177 |
case 4: { |
| 178 |
tile4Rotate(xyz); |
| 179 |
break; |
| 180 |
} |
| 181 |
case 5: { |
| 182 |
tile5Rotate(xyz); |
| 183 |
break; |
| 184 |
} |
| 185 |
default: { |
| 186 |
Log::info() << "ERROR: t out of range" << std::endl; |
| 187 |
throw_OutOfRange("t", t, 6); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void LFRicCubedSphereTiles::unrotate(idx_t t, double xyz[]) const { |
| 193 |
switch (t) { |
| 194 |
case 0: { |
| 195 |
tile0RotateInverse(xyz); |
| 196 |
break; |
| 197 |
} |
| 198 |
case 1: { |
| 199 |
tile1RotateInverse(xyz); |
| 200 |
break; |
| 201 |
} |
| 202 |
case 2: { |
| 203 |
tile2RotateInverse(xyz); |
| 204 |
break; |
| 205 |
} |
| 206 |
case 3: { |
| 207 |
tile3RotateInverse(xyz); |
| 208 |
break; |
| 209 |
} |
| 210 |
case 4: { |
| 211 |
tile4RotateInverse(xyz); |
| 212 |
break; |
| 213 |
} |
| 214 |
case 5: { |
| 215 |
tile5RotateInverse(xyz); |
| 216 |
break; |
| 217 |
} |
| 218 |
default: { |
| 219 |
throw_OutOfRange("t", t, 6); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
idx_t LFRicCubedSphereTiles::indexFromXY(const double xy[]) const { |
| 225 |
// Assume one face-edge is of length 90 degrees. |
| 226 |
// |
| 227 |
// y ^ |
| 228 |
// | |
| 229 |
// 135 ---------- |
| 230 |
// | | <= | |
| 231 |
// | | | |
| 232 |
// | |=< 4 <=| |
| 233 |
// | | v | |
| 234 |
// | | <= | |
| 235 |
// 45 4----------4----------4----------4---------- |
| 236 |
// | | ^ | ^ | ^ | ^ | |
| 237 |
// | | | | | | |
| 238 |
// | |=< 0 <|=< 1 <|=< 2 <|=< 3 <| |
| 239 |
// | | v | v | v | v | |
| 240 |
// | | = | = | = | = | |
| 241 |
// -45 0 ---------1----------2----------3---------- |
| 242 |
// | | ^ | |
| 243 |
// | | | |
| 244 |
// | | < 5 <| |
| 245 |
// | | | |
| 246 |
// | | v | |
| 247 |
// -135 ----------(5 for end iterator) |
| 248 |
// ----0---------90--------180--------270--------360---> x |
| 249 |
|
| 250 |
idx_t t{-1}; // tile index |
| 251 |
|
| 252 |
if ((xy[XX] >= 0.) && (xy[YY] >= -45.) && (xy[XX] < 90.) && (xy[YY] < 45.)) { |
| 253 |
t = 0; |
| 254 |
} |
| 255 |
else if ((xy[XX] >= 90.) && (xy[YY] >= -45.) && (xy[XX] < 180.) && (xy[YY] < 45.)) { |
| 256 |
t = 1; |
| 257 |
} |
| 258 |
else if ((xy[XX] >= 180.) && (xy[YY] >= -45.) && (xy[XX] < 270.) && (xy[YY] < 45.)) { |
| 259 |
t = 2; |
| 260 |
} |
| 261 |
else if ((xy[XX] >= 270.) && (xy[YY] >= -45.) && (xy[XX] < 360.) && (xy[YY] < 45.)) { |
| 262 |
t = 3; |
| 263 |
} |
| 264 |
else if ((xy[XX] >= 0.) && (xy[YY] >= 45.) && (xy[XX] <= 90.) && (xy[YY] <= 135.)) { |
| 265 |
t = 4; |
| 266 |
} |
| 267 |
else if ((xy[XX] > 0.) && (xy[YY] > -135.) && (xy[XX] < 90.) && (xy[YY] < -45.)) { |
| 268 |
t = 5; |
| 269 |
} |
| 270 |
|
| 271 |
return t; |
| 272 |
} |
| 273 |
|
| 274 |
idx_t LFRicCubedSphereTiles::indexFromLonLat(const double crd[]) const { |
| 275 |
idx_t t{-1}; // tile index |
| 276 |
|
| 277 |
double xyz[3]; |
| 278 |
|
| 279 |
sphericalToCartesian(crd, xyz); |
| 280 |
|
| 281 |
const double& lon = crd[LON]; |
| 282 |
|
| 283 |
double zPlusAbsX = xyz[ZZ] + std::abs(xyz[XX]); |
| 284 |
double zPlusAbsY = xyz[ZZ] + std::abs(xyz[YY]); |
| 285 |
double zMinusAbsX = xyz[ZZ] - std::abs(xyz[XX]); |
| 286 |
double zMinusAbsY = xyz[ZZ] - std::abs(xyz[YY]); |
| 287 |
|
| 288 |
if (is_tiny(zPlusAbsX)) { |
| 289 |
zPlusAbsX = 0.; |
| 290 |
} |
| 291 |
if (is_tiny(zPlusAbsY)) { |
| 292 |
zPlusAbsY = 0.; |
| 293 |
} |
| 294 |
if (is_tiny(zMinusAbsX)) { |
| 295 |
zMinusAbsX = 0.; |
| 296 |
} |
| 297 |
if (is_tiny(zMinusAbsY)) { |
| 298 |
zMinusAbsY = 0.; |
| 299 |
} |
| 300 |
|
| 301 |
if ((zPlusAbsX <= 0.) && (zPlusAbsY <= 0.)) { |
| 302 |
t = 4; |
| 303 |
} |
| 304 |
else if ((zMinusAbsX > 0.) && (zMinusAbsY > 0.)) { |
| 305 |
t = 5; |
| 306 |
} |
| 307 |
else if (is_geq(lon, 315.) || is_less(lon, 45.)) { |
| 308 |
t = 0; |
| 309 |
} |
| 310 |
else if (is_geq(lon, 45.) && is_less(lon, 135.)) { |
| 311 |
t = 1; |
| 312 |
} |
| 313 |
else if (is_geq(lon, 135.) && is_less(lon, 225.)) { |
| 314 |
t = 2; |
| 315 |
} |
| 316 |
else if (is_geq(lon, 225.) && is_less(lon, 315.)) { |
| 317 |
t = 3; |
| 318 |
} |
| 319 |
|
| 320 |
return t; |
| 321 |
} |
| 322 |
|
| 323 |
void LFRicCubedSphereTiles::enforceXYdomain(double xy[]) const { |
| 324 |
// the conversion from lonlat to xy can involve some small errors and small errors |
| 325 |
// can affect whether xy that is created within a valid space |
| 326 |
// This has been tested with N = 512 with equiangular and equidistant projections. |
| 327 |
const double tol{70.0}; |
| 328 |
constexpr double epsilon = std::numeric_limits<double>::epsilon(); |
| 329 |
|
| 330 |
if (debug) { |
| 331 |
Log::info() << "enforceXYDomain before " << xy[XX] << " " << xy[YY] << std::endl; |
| 332 |
} |
| 333 |
|
| 334 |
if (is_same(xy[YY], 45.0, tol)) { |
| 335 |
xy[YY] = 45.0; |
| 336 |
} |
| 337 |
if (is_same(xy[YY], -45.0, tol)) { |
| 338 |
xy[YY] = -45.0; |
| 339 |
} |
| 340 |
if (xy[YY] < -45.0) { |
| 341 |
xy[XX] = std::min(xy[XX], 90.0 - epsilon); |
| 342 |
xy[XX] = std::max(xy[XX], 0.0 + epsilon); |
| 343 |
} |
| 344 |
else if (xy[YY] >= -45.0) { |
| 345 |
xy[XX] = std::max(xy[XX], 0.0); |
| 346 |
} |
| 347 |
if (xy[YY] >= 45.0) { |
| 348 |
xy[XX] = std::min(xy[XX], 90.0); |
| 349 |
} |
| 350 |
|
| 351 |
xy[XX] = std::max(xy[XX], 0.0); |
| 352 |
xy[XX] = std::min(xy[XX], 360.0 - epsilon); |
| 353 |
xy[YY] = std::max(xy[YY], -135.0 + epsilon); |
| 354 |
xy[YY] = std::min(xy[YY], 135.0); |
| 355 |
|
| 356 |
|
| 357 |
if (debug) { |
| 358 |
Log::info() << "enforceXYDomain after " << xy[XX] << " " << xy[YY] << std::endl; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// input is the xy value as PointXY that is a continous "cross " extension in terms of xy space from the tile in question |
| 363 |
// the output is an xy value that lives on the standard "|---" shape |
| 364 |
PointXY LFRicCubedSphereTiles::tileCubePeriodicity(const PointXY& xyExtended, const idx_t t) const { |
| 365 |
// xy space is a function of tiles--Tile 0) // xy space for Tile 1 |
| 366 |
// // |
| 367 |
// y ^ // y ^ |
| 368 |
// // | |
| 369 |
// | |
| 370 |
// 225 ---------- // 225 ---------- |
| 371 |
// | |To 2 | // | |To 3 as | |
| 372 |
// | |rotate | // | |below inst| |
| 373 |
// | |-180 | // | |and rotate| |
| 374 |
// | |about | // | |+90 about | |
| 375 |
// | |(90,90) | // | |(0,45) + |replace in [0,360) x range |
| 376 |
// 135 4----------4 // 135 4----------4 |
| 377 |
// | | <= | // | |To 4 | |
| 378 |
// | | | // | |rotate | |
| 379 |
// | |=< 4 <=| // | |+90 about | |
| 380 |
// | | v | // | |(90,45) | |
| 381 |
// | |* <= | // | | | |
| 382 |
// 45 4----------4----------4----------4---------- // 45 4----------4----------4----------4---------- |
| 383 |
// | | ^ | ^ | ^ | ^ | // | | ^ | ^ | ^ | ^ | |
| 384 |
// | | | | | | // | | | | | | |
| 385 |
// | |=< 0 <|=< 1 <|=< 2 <|=< 3 <| // | |=< 0 <|=< 1 <|=< 2 <|=< 3 <| |
| 386 |
// | | v | v | v | v | // | | v | v | v | v | |
| 387 |
// | |* = |* = |* = | * = | // | |* = |* = |* = | * = | |
| 388 |
// -45 0 ---------1----------2----------3---------- // -45 0 ---------1----------2----------3---------- |
| 389 |
// | | ^ | // | |To 5 | |
| 390 |
// | | | // | |rotate | |
| 391 |
// | |< 5 <| // | |-90 about | |
| 392 |
// | | | // | |(90,-45) | |
| 393 |
// | |* v | // | | | |
| 394 |
// -135 ---------- ---------- ---------- ---------- // -135 ---------- ---------- ---------- ---------- |
| 395 |
// ----0---------90--------180--------270--------360---> x ----0---------90--------180--------270--------360---> x |
| 396 |
|
| 397 |
// xy space for Tile 2 // xy space for Tile 3 |
| 398 |
// // |
| 399 |
// y ^ // y ^ |
| 400 |
// | // | |
| 401 |
// 225 ---------- // 225 ---------- |
| 402 |
// | |To 0 | // | |To 1 | |
| 403 |
// | |rotate-180| // | |rotate-180| |
| 404 |
// | |about | // | |about | |
| 405 |
// | |(135.,90) | // | |(225,90) | |
| 406 |
// | | | // | | | |
| 407 |
// 135 4----------4 // 135 4----------4 |
| 408 |
// | |To 4 dble | // | |To 4 | |
| 409 |
// | |+90 | // | |-90 | |
| 410 |
// | |rotations | // | |rotation | |
| 411 |
// | |about(180,45) // | |about(360,45) |
| 412 |
// | |(90,45) | // | |and | replace in x range |
| 413 |
// 45 4----------4----------4----------4---------- // 45 4----------4----------4----------4---------- |
| 414 |
// | | ^ | ^ | ^ | ^ | // | | ^ | ^ | ^ | ^ | |
| 415 |
// | | | | | | // | | | | | | |
| 416 |
// | |=< 0 <|=< 1 <|=< 2 <|=< 3 <| // | |=< 0 <|=< 1 <|=< 2 <|=< 3 <| |
| 417 |
// | | v | v | v | v | // | | v | v | v | v | |
| 418 |
// | |* = |* = |* = | * = | // | |* = |* = |* = | * = | |
| 419 |
// -45 0 ---------1----------2----------3---------- // -45 0 ---------1----------2----------3---------- |
| 420 |
// | |To 5 dble | // | |To 5 | |
| 421 |
// | |-90 | // | |90 | |
| 422 |
// | |rotations | // | |rotation | |
| 423 |
// | |about (180,-45)| // | |about (360,-45)| |
| 424 |
// | |(90,-45) | // | |and |replace in x range |
| 425 |
// -135 ---------- ---------- ---------- ---------- // -135 ---------- ---------- ---------- ---------- |
| 426 |
// ----0---------90--------180--------270--------360---> x// ----0---------90--------180--------270--------360---> x |
| 427 |
|
| 428 |
// xy space for Tile 4's // xy space Tile 5's |
| 429 |
// // |
| 430 |
// y ^ // y ^ |
| 431 |
// | // | |
| 432 |
// 225 ---------- // 225 ---------- |
| 433 |
// | |To 2 | // | |To 2 | |
| 434 |
// | |rotate | // | |rotate | |
| 435 |
// | |-180 | // | |-180 | |
| 436 |
// | |about | // | |about | |
| 437 |
// | |(135.,90) | // | |(135.,90) | |
| 438 |
// 135 4----------4----------4----------4----------| // 135 4----------4 |
| 439 |
// | | <= |To 1 |To 5 |To 3 | // | | <= | |
| 440 |
// | | |rotate |rotate 180|rotate+90 | // | | | |
| 441 |
// | |=< 4 <=|-90 about |about |about | // | |=< 4 <=| |
| 442 |
// | | v |(90,45) | | | // | | v | |
| 443 |
// | |* <= | |(135, 0) |(360, 45) | // | |* <= | |
| 444 |
// 45 4----------4----------4----------4---------- // 45 4----------4 |
| 445 |
// | | ^ | // | | ^ | |
| 446 |
// | | | // | | | |
| 447 |
// | |=< 0 <| // | |= 0 >| |
| 448 |
// | | v | // | | | |
| 449 |
// | |* = | // | |* = | |
| 450 |
// -45 0 ---------1 // -45 0 ---------1----------2----------3----------| |
| 451 |
// | | ^ | // | | ^ |To 1 |To 4 |To 3 as | |
| 452 |
// | | | // | | |rotate |rotate |left instr| |
| 453 |
// | |< 5 <| // | |< 5 <|+90 about |+180 |+ rotate | |
| 454 |
// | | | // | | |(90,-45) |(135,0) |+90 about | |
| 455 |
// | |* v | // | |* v | | |(0, 45) |+ replace in [0,360 range) |
| 456 |
// -135 ---------- ---------- ---------- ---------- // -135 ---------- ---------- ---------- ---------- |
| 457 |
// ----0---------90--------180--------270--------360---> x// ----0---------90--------180--------270--------360---> x |
| 458 |
|
| 459 |
|
| 460 |
// Step 1: wrap into range x = [ 0, 360], y = [135, 225] ensuring that we stay |
| 461 |
// on the cross associated with tile index. |
| 462 |
if (!withinCross(t, xyExtended)) { |
| 463 |
ATLAS_THROW_EXCEPTION("Point (" << xyExtended.x() << "," << xyExtended.y() << ")" |
| 464 |
<< " is not in the cross extension of tile " << t); |
| 465 |
} |
| 466 |
|
| 467 |
PointXY withinRange = xyExtended; |
| 468 |
enforceWrapAround(t, withinRange); |
| 469 |
|
| 470 |
PointXY finalXY = withinRange; |
| 471 |
PointXY tempXY = withinRange; |
| 472 |
|
| 473 |
switch (t) { |
| 474 |
case 0: |
| 475 |
finalXY = (withinRange.y() > 135.0) ? rotatePlus180AboutPt(withinRange, PointXY{135.0, 90.0}) : withinRange; |
| 476 |
break; |
| 477 |
case 1: |
| 478 |
if ((withinRange.x() >= 90.0) && (withinRange.x() <= 180.0)) { |
| 479 |
if (withinRange.y() >= 45.0) { |
| 480 |
tempXY = rotatePlus90AboutPt(withinRange, PointXY{90.0, 45.0}); |
| 481 |
|
| 482 |
if (withinRange.y() > 135.0) { |
| 483 |
finalXY = rotatePlus90AboutPt(tempXY, PointXY{0.0, 45.0}); |
| 484 |
finalXY.x() += 360.0; |
| 485 |
} |
| 486 |
else { |
| 487 |
finalXY = tempXY; |
| 488 |
} |
| 489 |
} |
| 490 |
else if (withinRange.y() < -45.0) { |
| 491 |
finalXY = rotateMinus90AboutPt(withinRange, PointXY{90.0, -45.0}); |
| 492 |
} |
| 493 |
} |
| 494 |
break; |
| 495 |
case 2: |
| 496 |
if ((withinRange.x() >= 180.0) && (withinRange.x() <= 270.0)) { |
| 497 |
if (withinRange.y() > 135.0) { |
| 498 |
finalXY = rotatePlus180AboutPt(tempXY, PointXY{135.0, 90.0}); |
| 499 |
} |
| 500 |
else if (withinRange.y() >= 45.0) { |
| 501 |
tempXY = rotatePlus90AboutPt(withinRange, PointXY{180.0, 45.0}); |
| 502 |
finalXY = rotatePlus90AboutPt(tempXY, PointXY{90.0, 45.0}); |
| 503 |
} |
| 504 |
else if (withinRange.y() < -45.0) { |
| 505 |
tempXY = rotateMinus90AboutPt(withinRange, PointXY{180.0, -45.0}); |
| 506 |
finalXY = rotateMinus90AboutPt(tempXY, PointXY{90.0, -45.0}); |
| 507 |
} |
| 508 |
} |
| 509 |
break; |
| 510 |
|
| 511 |
case 3: |
| 512 |
if (((withinRange.x() >= 270.0) && (withinRange.x() <= 360.0)) || withinRange.x() == 0.0) { |
| 513 |
if (withinRange.y() > 135.0) { |
| 514 |
finalXY = rotatePlus180AboutPt(tempXY, PointXY{225.0, 90.0}); |
| 515 |
} |
| 516 |
else if (withinRange.y() >= 45.0) { |
| 517 |
finalXY = rotateMinus90AboutPt(tempXY, PointXY{360.0, 45.0}); |
| 518 |
finalXY.x() -= 360.0; |
| 519 |
} |
| 520 |
else if (withinRange.y() < -45.0) { |
| 521 |
finalXY = rotatePlus90AboutPt(tempXY, PointXY{360.0, -45.0}); |
| 522 |
finalXY.x() -= 360.0; |
| 523 |
} |
| 524 |
} |
| 525 |
break; |
| 526 |
case 4: |
| 527 |
if (withinRange.y() > 135.0) { |
| 528 |
finalXY = rotatePlus180AboutPt(tempXY, PointXY{135.0, 90.0}); |
| 529 |
} |
| 530 |
else if ((withinRange.y() >= 45.0) && (withinRange.y() <= 135.0)) { |
| 531 |
if ((withinRange.x() > 90.0) && (withinRange.x() <= 180.0)) { |
| 532 |
finalXY = rotateMinus90AboutPt(withinRange, PointXY{90.0, 45.0}); |
| 533 |
} |
| 534 |
if ((withinRange.x() > 180.0) && (withinRange.x() <= 270.0)) { |
| 535 |
finalXY = rotatePlus180AboutPt(withinRange, PointXY{135.0, 0.0}); |
| 536 |
} |
| 537 |
if ((withinRange.x() > 270.0) && (withinRange.x() <= 360.0)) { |
| 538 |
finalXY = rotatePlus90AboutPt(withinRange, PointXY{360.0, 45.0}); |
| 539 |
} |
| 540 |
} |
| 541 |
break; |
| 542 |
case 5: |
| 543 |
if (withinRange.y() > 135.0) { |
| 544 |
finalXY = rotatePlus180AboutPt(tempXY, PointXY{135.0, 90.0}); |
| 545 |
} |
| 546 |
else if ((withinRange.y() <= -45.0) && (withinRange.y() >= -135.0)) { |
| 547 |
if ((withinRange.x() > 90.0) && (withinRange.x() <= 180.0)) { |
| 548 |
finalXY = rotatePlus90AboutPt(withinRange, PointXY{90.0, -45.0}); |
| 549 |
} |
| 550 |
if ((withinRange.x() > 180.0) && (withinRange.x() <= 270.0)) { |
| 551 |
finalXY = rotatePlus180AboutPt(withinRange, PointXY{135.0, 0.0}); |
| 552 |
} |
| 553 |
if ((withinRange.x() > 270.0) && (withinRange.x() <= 360.0)) { |
| 554 |
finalXY = rotateMinus90AboutPt(withinRange, PointXY{360.0, -45.0}); |
| 555 |
} |
| 556 |
} |
| 557 |
break; |
| 558 |
} |
| 559 |
|
| 560 |
// Now we are on the standard tiles with their default rotate orientation. |
| 561 |
// The next step is to wrap the edges and corners to the correct tiles. |
| 562 |
if (finalXY.x() > 90. && finalXY.x() < 180. && finalXY.y() == 45.0) { |
| 563 |
finalXY.y() = 45.0 + (finalXY.x() - 90.0); |
| 564 |
finalXY.x() = 90.0; |
| 565 |
} |
| 566 |
if (finalXY.x() == 180. && finalXY.y() == 45.0) { |
| 567 |
finalXY.x() = 90.; |
| 568 |
finalXY.y() = 135.0; |
| 569 |
} |
| 570 |
if (finalXY.x() > 180. && finalXY.x() < 270. && finalXY.y() == 45.0) { |
| 571 |
finalXY.x() = 90.0 - (finalXY.x() - 90.0); |
| 572 |
finalXY.y() = 135.0; |
| 573 |
} |
| 574 |
if (finalXY.x() == 270. && finalXY.y() == 45.0) { |
| 575 |
finalXY.x() = 0.; |
| 576 |
finalXY.y() = 135.0; |
| 577 |
} |
| 578 |
if (finalXY.x() > 270. && finalXY.x() <= 360. && finalXY.y() == 45.0) { |
| 579 |
finalXY.y() = 135.0 - (finalXY.x() - 270.0); |
| 580 |
finalXY.x() = 0.0; |
| 581 |
} |
| 582 |
|
| 583 |
if (finalXY.x() >= 360.) { |
| 584 |
finalXY.x() -= 360.; |
| 585 |
} |
| 586 |
|
| 587 |
if (finalXY.x() == 0. && finalXY.y() < -45. && finalXY.y() > -135.0) { |
| 588 |
finalXY.x() = 360.0 + (finalXY.y() + 45.0); |
| 589 |
finalXY.y() = -45.; |
| 590 |
} |
| 591 |
if (finalXY.x() == 90. && finalXY.y() < -45. && finalXY.y() > -135.0) { |
| 592 |
finalXY.x() = 90.0 - (finalXY.y() + 45.0); |
| 593 |
finalXY.y() = -45.; |
| 594 |
} |
| 595 |
if (finalXY.y() <= -135. && finalXY.x() >= 0. && finalXY.x() <= 90.0) { |
| 596 |
finalXY.x() = 270.0 - finalXY.x(); |
| 597 |
finalXY.y() = -45.; |
| 598 |
} |
| 599 |
|
| 600 |
return finalXY; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
void LFRicCubedSphereTiles::print(std::ostream& os) const { |
| 605 |
os << "CubedSphereTiles[" |
| 606 |
<< "]"; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
bool LFRicCubedSphereTiles::withinCross(const idx_t tiles, const PointXY& withinRange) const { |
| 611 |
std::size_t t = static_cast<std::size_t>(tiles); |
| 612 |
return !((withinRange.x() < botLeftTile(t).x() && withinRange.y() < botLeftTile(t).y()) || |
| 613 |
(withinRange.x() > botRightTile(t).x() && withinRange.y() < botRightTile(t).y()) || |
| 614 |
(withinRange.x() > topRightTile(t).x() && withinRange.y() > topRightTile(t).y()) || |
| 615 |
(withinRange.x() < topLeftTile(t).x() && withinRange.y() > topLeftTile(t).y())); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
void LFRicCubedSphereTiles::enforceWrapAround(const idx_t t, PointXY& withinRange) const { |
| 620 |
if (withinRange.x() < 0.0) { |
| 621 |
PointXY temp = withinRange; |
| 622 |
temp.x() += 360; |
| 623 |
if (withinCross(t, temp)) { |
| 624 |
withinRange = temp; |
| 625 |
} |
| 626 |
} |
| 627 |
if (withinRange.x() > 360.0) { |
| 628 |
PointXY temp = withinRange; |
| 629 |
temp.x() -= 360; |
| 630 |
if (withinCross(t, temp)) { |
| 631 |
withinRange = temp; |
| 632 |
} |
| 633 |
} |
| 634 |
if (withinRange.y() <= -135.0) { |
| 635 |
PointXY temp = withinRange; |
| 636 |
temp.y() += 360; |
| 637 |
if (withinCross(t, temp)) { |
| 638 |
withinRange = temp; |
| 639 |
} |
| 640 |
} |
| 641 |
if (withinRange.y() > 225.0) { |
| 642 |
PointXY temp = withinRange; |
| 643 |
temp.y() -= 360; |
| 644 |
if (withinCross(t, temp)) { |
| 645 |
withinRange = temp; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
return; |
| 650 |
} |
| 651 |
|
| 652 |
const PointXY& LFRicCubedSphereTiles::tileCentre(size_t t) const { |
| 653 |
return tileCentres_[t]; |
| 654 |
} |
| 655 |
|
| 656 |
const LFRicCubedSphereTiles::Jacobian& LFRicCubedSphereTiles::tileJacobian(size_t t) const { |
| 657 |
return tileJacobians_[t]; |
| 658 |
} |
| 659 |
|
| 660 |
PointXY LFRicCubedSphereTiles::botLeftTile(size_t t) { |
| 661 |
return tileCentres_[t] + PointXY{-45., -45.}; |
| 662 |
} |
| 663 |
|
| 664 |
PointXY LFRicCubedSphereTiles::botRightTile(size_t t) { |
| 665 |
return tileCentres_[t] + PointXY{45., -45.}; |
| 666 |
} |
| 667 |
|
| 668 |
PointXY LFRicCubedSphereTiles::topLeftTile(size_t t) { |
| 669 |
return tileCentres_[t] + PointXY{-45., 45.}; |
| 670 |
} |
| 671 |
|
| 672 |
PointXY LFRicCubedSphereTiles::topRightTile(size_t t) { |
| 673 |
return tileCentres_[t] + PointXY{45., 45.}; |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
// Centre of each tile in xy space. |
| 678 |
const std::array<PointXY, 6> LFRicCubedSphereTiles::tileCentres_{ |
| 679 |
PointXY{45., 0.}, PointXY{135., 0.}, PointXY{225., 0.}, PointXY{315., 0.}, PointXY{45., 90.}, PointXY{45., -90.}}; |
| 680 |
|
| 681 |
// Jacobian of xy space with respect to curvilinear coordinates for each tile. |
| 682 |
const std::array<LFRicCubedSphereTiles::Jacobian, 6> LFRicCubedSphereTiles::tileJacobians_{ |
| 683 |
Jacobian{{1., 0.}, {0., 1.}}, Jacobian{{1., 0.}, {0., 1.}}, Jacobian{{0., -1.}, {1., 0.}}, |
| 684 |
Jacobian{{0., -1.}, {1., 0.}}, Jacobian{{1., 0.}, {0., 1.}}, Jacobian{{0., 1.}, {-1., 0.}}}; |
| 685 |
|
| 686 |
|
| 687 |
namespace { |
| 688 |
static CubedSphereTilesBuilder<LFRicCubedSphereTiles> register_builder(LFRicCubedSphereTiles::static_type()); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
} // namespace detail |
| 693 |
} // namespace grid |
| 694 |
} // namespace atlas |