Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 133x 133x 1x 132x 1x 131x 131x 1x 64x 64x 1x 63x 1x 345x 343x 6x 337x 3x 334x 334x 1x 333x 1x 74x 72x 5x 67x 1x 133x 133x 1x 57x 57x 57x 57x 57x 1x 138x 137x 3x 134x 134x 7028x 134x 6894x 6894x 134x 1x 7x 7x 2x 5x 1x 4x 4x 4x 1x 2x 1x 1x 1x 1x 6x 3x 3x 3x 1x 111x 111x 111x 111x 111x 1x 210x 210x 2x 208x 208x 208x 208x 208x 1x 57x 57x 2x 55x 2x 53x 2x 51x 51x 51x 51x 51x 51x 111x 111x 208x 208x 208x 208x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 319x 1x 319x 319x 319x 319x 319x 319x 2552x 2552x 2552x 2552x 2552x 2552x 2552x 2552x 319x 111x 111x 111x 111x 111x 319x 303x 12x 1x 1x 1x 1x 1054x 653x 401x 222x 208x 208x 208x 416x 416x 416x 416x 208x 208x 832x 1364x 1024x 1024x 1024x 340x 340x 340x 340x 340x 340x 340x 340x 340x 1x 1x 1x 1x 5x 3x 1x 57x | import Cartesian3 from "./Cartesian3.js";
import Cartographic from "./Cartographic.js";
import Check from "./Check.js";
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
import Ellipsoid from "./Ellipsoid.js";
import FeatureDetection from "./FeatureDetection.js";
import RuntimeError from "./RuntimeError.js";
/**
* S2
* --
*
* This implementation is based on the S2 C++ reference implementation: https://github.com/google/s2geometry
*
*
* Overview:
* ---------
* The S2 library decomposes the unit sphere into a hierarchy of cells. A cell is a quadrilateral bounded by 4 geodesics.
* The 6 root cells are obtained by projecting the six faces of a cube on a unit sphere. Each root cell follows a quadtree
* subdivision scheme, i.e. each cell subdivides into 4 smaller cells that cover the same area as the parent cell. The S2 cell
* hierarchy extends from level 0 (root cells) to level 30 (leaf cells). The root cells are rotated to enable a continuous Hilbert
* curve to map all 6 faces of the cube.
*
*
* Cell ID:
* --------
* Each cell in S2 can be uniquely identified using a 64-bit unsigned integer, its cell ID. The first 3 bits of the cell ID are the face bits, i.e.
* they indicate which of the 6 faces of the cube a cell lies on. After the face bits are the position bits, i.e. they indicate the position
* of the cell along the Hilbert curve. After the positions bits is the sentinel bit, which is always set to 1, and it indicates the level of the
* cell. Again, the level can be between 0 and 30 in S2.
*
* Note: In the illustration below, the face bits are marked with 'f', the position bits are marked with 'p', the zero bits are marked with '-'.
*
* Cell ID (base 10): 3170534137668829184
* Cell ID (base 2) : 0010110000000000000000000000000000000000000000000000000000000000
*
* 001 0110000000000000000000000000000000000000000000000000000000000
* fff pps----------------------------------------------------------
*
* For the cell above, we can see that it lies on face 1 (01), with a Hilbert index of 1 (1).
*
*
* Cell Subdivision:
* ------------------
* Cells in S2 subdivide recursively using quadtree subdivision. For each cell, you can get a child of index [0-3]. To compute the child at index i,
* insert the base 2 representation of i to the right of the parent's position bits. Ensure that the sentinel bit is also shifted two places to the right.
*
* Parent Cell ID (base 10) : 3170534137668829184
* Parent Cell ID (base 2) : 0010110000000000000000000000000000000000000000000000000000000000
*
* 001 0110000000000000000000000000000000000000000000000000000000000
* fff pps----------------------------------------------------------
*
* To get the 3rd child of the cell above, we insert the binary representation of 3 to the right of the parent's position bits:
*
* Note: In the illustration below, the bits to be added are highlighted with '^'.
*
* 001 0111100000000000000000000000000000000000000000000000000000000
* fff pppps--------------------------------------------------------
* ^^
*
* Child(3) Cell ID (base 10) : 3386706919782612992
* Child(3) Cell ID (base 2) : 0010111100000000000000000000000000000000000000000000000000000000
*
* Cell Token:
* -----------
* To provide a more concise representation of the S2 cell ID, we can use their hexadecimal representation.
*
* Cell ID (base 10): 3170534137668829184
* Cell ID (base 2) : 0010110000000000000000000000000000000000000000000000000000000000
*
* We remove all trailing zero bits, until we reach the nybble (4 bits) that contains the sentinel bit.
*
* Note: In the illustration below, the bits to be removed are highlighted with 'X'.
*
* 0010110000000000000000000000000000000000000000000000000000000000
* fffpps--XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
*
* We convert the remaining bits to their hexadecimal representation.
*
* Base 2: 0010 1100
* Base 16: "2" "c"
*
* Cell Token: "2c"
*
* To compute the cell ID from the token, we simply add enough zeros to the right to make the ID span 64 bits.
*
* Coordinate Transforms:
* ----------------------
*
* To go from a cell in S2 to a point on the ellipsoid, the following order of transforms is applied:
*
* 1. (Cell ID): S2 cell ID
* 2. (Face, I, J): Leaf cell coordinates, where i and j are in range [0, 2^30 - 1]
* 3. (Face, S, T): Cell space coordinates, where s and t are in range [0, 1].
* 4. (Face, Si, Ti): Discrete cell space coordinates, where si and ti are in range [0, 2^31]
* 5. (Face, U, V): Cube space coordinates, where u and v are in range [-1, 1]. We apply the non-linear quadratic transform here.
* 6. (X, Y, Z): Direction vector, where vector may not be unit length. Can be normalized to obtain point on unit sphere
* 7. (Latitude, Longitude): Direction vector, where latitude is in range [-90, 90] and longitude is in range [-180, 180]
*
* @ignore
*/
// The maximum level supported within an S2 cell ID. Each level is represented by two bits in the final cell ID
const S2_MAX_LEVEL = 30;
// The maximum index of a valid leaf cell plus one. The range of valid leaf cell indices is [0..S2_LIMIT_IJ-1].
const S2_LIMIT_IJ = 1 << S2_MAX_LEVEL;
// The maximum value of an si- or ti-coordinate. The range of valid (si,ti) values is [0..S2_MAX_SITI]. Use `>>>` to convert to unsigned.
const S2_MAX_SITI = (1 << (S2_MAX_LEVEL + 1)) >>> 0;
// The number of bits in a S2 cell ID used for specifying the position along the Hilbert curve
const S2_POSITION_BITS = 2 * S2_MAX_LEVEL + 1;
// The number of bits per I and J in the lookup tables
const S2_LOOKUP_BITS = 4;
// Lookup table for mapping 10 bits of IJ + orientation to 10 bits of Hilbert curve position + orientation.
const S2_LOOKUP_POSITIONS = [];
// Lookup table for mapping 10 bits of IJ + orientation to 10 bits of Hilbert curve position + orientation.
const S2_LOOKUP_IJ = [];
// Lookup table of two bits of IJ from two bits of curve position, based also on the current curve orientation from the swap and invert bits
const S2_POSITION_TO_IJ = [
[0, 1, 3, 2], // 0: Normal order, no swap or invert
[0, 2, 3, 1], // 1: Swap bit set, swap I and J bits
[3, 2, 0, 1], // 2: Invert bit set, invert bits
[3, 1, 0, 2], // 3: Swap and invert bits set
];
// Mask that specifies the swap orientation bit for the Hilbert curve
const S2_SWAP_MASK = 1;
// Mask that specifies the invert orientation bit for the Hilbert curve
const S2_INVERT_MASK = 2;
// Lookup for the orientation update mask of one of the four sub-cells within a higher level cell.
// This mask is XOR'ed with the current orientation to get the sub-cell orientation.
const S2_POSITION_TO_ORIENTATION_MASK = [
S2_SWAP_MASK,
0,
0,
S2_SWAP_MASK | S2_INVERT_MASK,
];
/**
* Represents a cell in the S2 geometry library.
*
* @alias S2Cell
* @constructor
*
* @param {bigint} [cellId] The 64-bit S2CellId.
* @private
*/
function S2Cell(cellId) {
Iif (!FeatureDetection.supportsBigInt()) {
throw new RuntimeError("S2 required BigInt support");
}
//>>includeStart('debug', pragmas.debug);
if (!defined(cellId)) {
throw new DeveloperError("cell ID is required.");
}
if (!S2Cell.isValidId(cellId)) {
throw new DeveloperError("cell ID is invalid.");
}
//>>includeEnd('debug');
this._cellId = cellId;
this._level = S2Cell.getLevel(cellId);
}
/**
* Creates a new S2Cell from a token. A token is a hexadecimal representation of the 64-bit S2CellId.
*
* @param {string} token The token for the S2 Cell.
* @returns {S2Cell} Returns a new S2Cell.
* @private
*/
S2Cell.fromToken = function (token) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("token", token);
if (!S2Cell.isValidToken(token)) {
throw new DeveloperError("token is invalid.");
}
//>>includeEnd('debug');
return new S2Cell(S2Cell.getIdFromToken(token));
};
/**
* Validates an S2 cell ID.
*
* @param {bigint} [cellId] The S2CellId.
* @returns {boolean} Returns true if the cell ID is valid, returns false otherwise.
* @private
*/
S2Cell.isValidId = function (cellId) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.bigint("cellId", cellId);
//>>includeEnd('debug');
// Check if sentinel bit is missing.
if (cellId <= 0) {
return false;
}
// Check if face bits indicate a valid value, in range [0-5].
if (cellId >> BigInt(S2_POSITION_BITS) > 5) {
return false;
}
// Check trailing 1 bit is in one of the even bit positions allowed for the 30 levels, using a bitmask.
const lowestSetBit = cellId & (~cellId + BigInt(1));
if (!(lowestSetBit & BigInt("0x1555555555555555"))) {
return false;
}
return true;
};
/**
* Validates an S2 cell token.
*
* @param {string} [token] The hexadecimal representation of an S2CellId.
* @returns {boolean} Returns true if the token is valid, returns false otherwise.
* @private
*/
S2Cell.isValidToken = function (token) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("token", token);
//>>includeEnd('debug');
if (!/^[0-9a-fA-F]{1,16}$/.test(token)) {
return false;
}
return S2Cell.isValidId(S2Cell.getIdFromToken(token));
};
/**
* Converts an S2 cell token to a 64-bit S2 cell ID.
*
* @param {string} [token] The hexadecimal representation of an S2CellId. Expected to be a valid S2 token.
* @returns {bigint} Returns the S2 cell ID.
* @private
*/
S2Cell.getIdFromToken = function (token) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("token", token);
//>>includeEnd('debug');
return BigInt("0x" + token + "0".repeat(16 - token.length)); // eslint-disable-line
};
/**
* Converts a 64-bit S2 cell ID to an S2 cell token.
*
* @param {bigint} [cellId] The S2 cell ID.
* @returns {string} Returns hexadecimal representation of an S2CellId.
* @private
*/
S2Cell.getTokenFromId = function (cellId) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.bigint("cellId", cellId);
//>>includeEnd('debug');
const trailingZeroHexChars = Math.floor(countTrailingZeroBits(cellId) / 4);
const hexString = cellId.toString(16).replace(/0*$/, "");
const zeroString = Array(17 - trailingZeroHexChars - hexString.length).join(
"0",
);
return zeroString + hexString;
};
/**
* Gets the level of the cell from the cell ID.
*
* @param {bigint} [cellId] The S2 cell ID.
* @returns {number} Returns the level of the cell.
* @private
*/
S2Cell.getLevel = function (cellId) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.bigint("cellId", cellId);
if (!S2Cell.isValidId(cellId)) {
throw new DeveloperError();
}
//>>includeEnd('debug');
let lsbPosition = 0;
while (cellId !== BigInt(0)) {
if (cellId & BigInt(1)) {
break;
}
lsbPosition++;
cellId = cellId >> BigInt(1);
}
// We use (>> 1) because there are 2 bits per level.
return S2_MAX_LEVEL - (lsbPosition >> 1);
};
/**
* Gets the child cell of the cell at the given index.
*
* @param {number} index An integer index of the child.
* @returns {S2Cell} The child of the S2Cell.
* @private
*/
S2Cell.prototype.getChild = function (index) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("index", index);
if (index < 0 || index > 3) {
throw new DeveloperError("child index must be in the range [0-3].");
}
if (this._level === 30) {
throw new DeveloperError("cannot get child of leaf cell.");
}
//>>includeEnd('debug');
// Shift sentinel bit 2 positions to the right.
const newLsb = lsb(this._cellId) >> BigInt(2);
// Insert child index before the sentinel bit.
const childCellId = this._cellId + BigInt(2 * index + 1 - 4) * newLsb;
return new S2Cell(childCellId);
};
/**
* Gets the parent cell of an S2Cell.
*
* @returns {S2Cell} Returns the parent of the S2Cell.
* @private
*/
S2Cell.prototype.getParent = function () {
//>>includeStart('debug', pragmas.debug);
if (this._level === 0) {
throw new DeveloperError("cannot get parent of root cell.");
}
//>>includeEnd('debug');
// Shift the sentinel bit 2 positions to the left.
const newLsb = lsb(this._cellId) << BigInt(2);
// Erase the left over bits to the right of the sentinel bit.
return new S2Cell((this._cellId & (~newLsb + BigInt(1))) | newLsb);
};
/**
* Gets the parent cell at the given level.
*
* @returns {S2Cell} Returns the parent of the S2Cell.
* @private
*/
S2Cell.prototype.getParentAtLevel = function (level) {
//>>includeStart('debug', pragmas.debug);
if (this._level === 0 || level < 0 || this._level < level) {
throw new DeveloperError("cannot get parent at invalid level.");
}
//>>includeEnd('debug');
const newLsb = lsbForLevel(level);
return new S2Cell((this._cellId & -newLsb) | newLsb);
};
/**
* Get center of the S2 cell.
*
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid.
* @returns {Cartesian3} The position of center of the S2 cell.
* @private
*/
S2Cell.prototype.getCenter = function (ellipsoid) {
ellipsoid = ellipsoid ?? Ellipsoid.WGS84;
let center = getS2Center(this._cellId, this._level);
// Normalize XYZ.
center = Cartesian3.normalize(center, center);
const cartographic = new Cartographic.fromCartesian(
center,
Ellipsoid.UNIT_SPHERE,
);
// Interpret as geodetic coordinates on the ellipsoid.
return Cartographic.toCartesian(cartographic, ellipsoid, new Cartesian3());
};
/**
* Get vertex of the S2 cell. Vertices are indexed in CCW order.
*
* @param {number} index An integer index of the vertex. Must be in the range [0-3].
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid.
* @returns {Cartesian3} The position of the vertex of the S2 cell.
* @private
*/
S2Cell.prototype.getVertex = function (index, ellipsoid) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.number("index", index);
if (index < 0 || index > 3) {
throw new DeveloperError("vertex index must be in the range [0-3].");
}
//>>includeEnd('debug');
ellipsoid = ellipsoid ?? Ellipsoid.WGS84;
let vertex = getS2Vertex(this._cellId, this._level, index);
// Normalize XYZ.
vertex = Cartesian3.normalize(vertex, vertex);
const cartographic = new Cartographic.fromCartesian(
vertex,
Ellipsoid.UNIT_SPHERE,
);
// Interpret as geodetic coordinates on the ellipsoid.
return Cartographic.toCartesian(cartographic, ellipsoid, new Cartesian3());
};
/**
* Creates an S2Cell from its face, position along the Hilbert curve for a given level.
*
* @param {number} face The root face of S2 this cell is on. Must be in the range [0-5].
* @param {bigint} position The position along the Hilbert curve. Must be in the range [0-4**level).
* @param {number} level The level of the S2 curve. Must be in the range [0-30].
* @returns {S2Cell} A new S2Cell from the given parameters.
* @private
*/
S2Cell.fromFacePositionLevel = function (face, position, level) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.bigint("position", position);
if (face < 0 || face > 5) {
throw new DeveloperError("Invalid S2 Face (must be within 0-5)");
}
if (level < 0 || level > S2_MAX_LEVEL) {
throw new DeveloperError("Invalid level (must be within 0-30)");
}
if (position < 0 || position >= Math.pow(4, level)) {
throw new DeveloperError("Invalid Hilbert position for level");
}
//>>includeEnd('debug');
const faceBitString =
(face < 4 ? "0" : "") + (face < 2 ? "0" : "") + face.toString(2);
const positionBitString = position.toString(2);
const positionPrefixPadding = Array(
2 * level - positionBitString.length + 1,
).join("0");
const positionSuffixPadding = Array(S2_POSITION_BITS - 2 * level).join("0");
const cellId = BigInt(
`0b${faceBitString}${positionPrefixPadding}${positionBitString}1${
// Adding the sentinel bit that always follows the position bits.
positionSuffixPadding
}`,
);
return new S2Cell(cellId);
};
/**
* @private
*/
function getS2Center(cellId, level) {
const faceSiTi = convertCellIdToFaceSiTi(cellId, level);
return convertFaceSiTitoXYZ(faceSiTi[0], faceSiTi[1], faceSiTi[2]);
}
/**
* @private
*/
function getS2Vertex(cellId, level, index) {
const faceIJ = convertCellIdToFaceIJ(cellId, level);
const uv = convertIJLeveltoBoundUV([faceIJ[1], faceIJ[2]], level);
// Handles CCW ordering of the vertices.
const y = (index >> 1) & 1;
return convertFaceUVtoXYZ(faceIJ[0], uv[0][y ^ (index & 1)], uv[1][y]);
}
// S2 Coordinate Conversions
/**
* @private
*/
function convertCellIdToFaceSiTi(cellId, level) {
const faceIJ = convertCellIdToFaceIJ(cellId);
const face = faceIJ[0];
const i = faceIJ[1];
const j = faceIJ[2];
// We're resolving the center when we do the coordinate transform here. For the leaf cells, we're adding half the cell size
// (remember that this space has 31 levels - which allows us to pick center and edges of the leaf cells). For non leaf cells,
// we get one of either two cells diagonal to the cell center. The correction is used to make sure we pick the leaf cell edges
// that represent the parent cell center.
const isLeaf = level === 30;
const shouldCorrect =
!isLeaf && (BigInt(i) ^ (cellId >> BigInt(2))) & BigInt(1);
const correction = isLeaf ? 1 : shouldCorrect ? 2 : 0;
const si = (i << 1) + correction;
const ti = (j << 1) + correction;
return [face, si, ti];
}
/**
* @private
*/
function convertCellIdToFaceIJ(cellId) {
if (S2_LOOKUP_POSITIONS.length === 0) {
generateLookupTable();
}
const face = Number(cellId >> BigInt(S2_POSITION_BITS));
let bits = face & S2_SWAP_MASK;
const lookupMask = (1 << S2_LOOKUP_BITS) - 1;
let i = 0;
let j = 0;
for (let k = 7; k >= 0; k--) {
const numberOfBits =
k === 7 ? S2_MAX_LEVEL - 7 * S2_LOOKUP_BITS : S2_LOOKUP_BITS;
const extractMask = (1 << (2 * numberOfBits)) - 1;
bits +=
Number(
(cellId >> BigInt(k * 2 * S2_LOOKUP_BITS + 1)) & BigInt(extractMask),
) << 2;
bits = S2_LOOKUP_IJ[bits];
const offset = k * S2_LOOKUP_BITS;
i += (bits >> (S2_LOOKUP_BITS + 2)) << offset;
j += ((bits >> 2) & lookupMask) << offset;
bits &= S2_SWAP_MASK | S2_INVERT_MASK;
}
return [face, i, j];
}
/**
* @private
*/
function convertFaceSiTitoXYZ(face, si, ti) {
const s = convertSiTitoST(si);
const t = convertSiTitoST(ti);
const u = convertSTtoUV(s);
const v = convertSTtoUV(t);
return convertFaceUVtoXYZ(face, u, v);
}
/**
* @private
*/
function convertFaceUVtoXYZ(face, u, v) {
switch (face) {
case 0:
return new Cartesian3(1, u, v);
case 1:
return new Cartesian3(-u, 1, v);
case 2:
return new Cartesian3(-u, -v, 1);
case 3:
return new Cartesian3(-1, -v, -u);
case 4:
return new Cartesian3(v, -1, -u);
default:
return new Cartesian3(v, u, -1);
}
}
/**
* S2 provides 3 methods for the non-linear transform: linear, quadratic and tangential.
* This implementation uses the quadratic method because it provides a good balance of
* accuracy and speed.
*
* For a more detailed comparison of these transform methods, see
* {@link https://github.com/google/s2geometry/blob/0c4c460bdfe696da303641771f9def900b3e440f/src/s2/s2metrics.cc}
* @private
*/
function convertSTtoUV(s) {
if (s >= 0.5) {
return (1 / 3) * (4 * s * s - 1);
}
return (1 / 3) * (1 - 4 * (1 - s) * (1 - s));
}
/**
* @private
*/
function convertSiTitoST(si) {
return (1.0 / S2_MAX_SITI) * si;
}
/**
* @private
*/
function convertIJLeveltoBoundUV(ij, level) {
const result = [[], []];
const cellSize = getSizeIJ(level);
for (let d = 0; d < 2; ++d) {
const ijLow = ij[d] & -cellSize;
const ijHigh = ijLow + cellSize;
result[d][0] = convertSTtoUV(convertIJtoSTMinimum(ijLow));
result[d][1] = convertSTtoUV(convertIJtoSTMinimum(ijHigh));
}
return result;
}
/**
* @private
*/
function getSizeIJ(level) {
return (1 << (S2_MAX_LEVEL - level)) >>> 0;
}
/**
* @private
*/
function convertIJtoSTMinimum(i) {
return (1.0 / S2_LIMIT_IJ) * i;
}
// Utility Functions
/**
* This function generates 4 variations of a Hilbert curve of level 4, based on the S2_POSITION_TO_IJ table, for fast lookups of (i, j)
* to position along Hilbert curve. The reference C++ implementation uses an iterative approach, however, this function is implemented
* recursively.
*
* See {@link https://github.com/google/s2geometry/blob/c59d0ca01ae3976db7f8abdc83fcc871a3a95186/src/s2/s2cell_id.cc#L75-L109}
* @private
*/
function generateLookupCell(
level,
i,
j,
originalOrientation,
position,
orientation,
) {
if (level === S2_LOOKUP_BITS) {
const ij = (i << S2_LOOKUP_BITS) + j;
S2_LOOKUP_POSITIONS[(ij << 2) + originalOrientation] =
(position << 2) + orientation;
S2_LOOKUP_IJ[(position << 2) + originalOrientation] =
(ij << 2) + orientation;
} else {
level++;
i <<= 1;
j <<= 1;
position <<= 2;
const r = S2_POSITION_TO_IJ[orientation];
generateLookupCell(
level,
i + (r[0] >> 1),
j + (r[0] & 1),
originalOrientation,
position,
orientation ^ S2_POSITION_TO_ORIENTATION_MASK[0],
);
generateLookupCell(
level,
i + (r[1] >> 1),
j + (r[1] & 1),
originalOrientation,
position + 1,
orientation ^ S2_POSITION_TO_ORIENTATION_MASK[1],
);
generateLookupCell(
level,
i + (r[2] >> 1),
j + (r[2] & 1),
originalOrientation,
position + 2,
orientation ^ S2_POSITION_TO_ORIENTATION_MASK[2],
);
generateLookupCell(
level,
i + (r[3] >> 1),
j + (r[3] & 1),
originalOrientation,
position + 3,
orientation ^ S2_POSITION_TO_ORIENTATION_MASK[3],
);
}
}
/**
* @private
*/
function generateLookupTable() {
generateLookupCell(0, 0, 0, 0, 0, 0);
generateLookupCell(0, 0, 0, S2_SWAP_MASK, 0, S2_SWAP_MASK);
generateLookupCell(0, 0, 0, S2_INVERT_MASK, 0, S2_INVERT_MASK);
generateLookupCell(
0,
0,
0,
S2_SWAP_MASK | S2_INVERT_MASK,
0,
S2_SWAP_MASK | S2_INVERT_MASK,
);
}
/**
* Return the lowest-numbered bit that is on for this cell id
* @private
*/
function lsb(cellId) {
return cellId & (~cellId + BigInt(1));
}
/**
* Return the lowest-numbered bit that is on for cells at the given level.
* @private
*/
function lsbForLevel(level) {
return BigInt(1) << BigInt(2 * (S2_MAX_LEVEL - level));
}
// Lookup table for getting trailing zero bits.
// https://graphics.stanford.edu/~seander/bithacks.html
const Mod67BitPosition = [
64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54, 4, 64, 13, 10, 17,
62, 60, 28, 42, 30, 20, 51, 25, 44, 55, 47, 5, 32, 65, 38, 14, 22, 11, 58, 18,
53, 63, 9, 61, 27, 29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
7, 48, 35, 6, 34, 33, 0,
];
/**
* Return the number of trailing zeros in number.
* @private
*/
function countTrailingZeroBits(x) {
return Mod67BitPosition[(-x & x) % BigInt(67)];
}
export default S2Cell;
|