All files / engine/Source/Core Rectangle.js

98.56% Statements 412/418
90.71% Branches 166/183
100% Functions 31/31
98.54% Lines 406/412

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 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072                                                          23646x               23646x               23646x               23646x     1x                 50279x                       45030x                 1x                     1x   233x 232x     231x   231x 231x 231x 231x   231x                     1x   3016x     3015x   3015x 201x     3015x 3015x 3015x 3015x 3015x               1x   50300x   50300x 50300x 50300x 20x   50300x               1x   45051x   45051x                               1x 516x 516x 516x 516x   516x 515x     1x 1x 1x 1x   1x                               1x 87x 43x     44x 44x 44x 44x   44x                   1x   444x     443x 443x 443x 443x 443x 443x   443x 898x 898x 898x 898x 898x     898x     898x 898x     443x 2x 2x   2x 2x   2x         443x 5x     438x 438x 438x 438x 438x                     1x   43x   42x   42x 42x 42x 42x 42x 42x   42x 1239x 1239x 1239x 1239x 1239x     1239x     1239x 1239x     42x 2x 2x   2x 2x   2x 1x       42x 30x     12x 12x 12x 12x 12x     1x 1x 1x 1x 1x 1x 1x 5x                     1x   7x     6x 6x   6x 5x     6x 3x     6x 1x 1x     5x         5x         5x 5x         5x   5x 5x   5x 5x   5x     5x 5x     5x 5x     5x 5x     5x 5x   5x   5x                   1x 8792x 26x     8766x 6723x               2043x 2043x 2043x 2043x 2043x                         1x 45x   45x                                 1x 149x                   1x 11873x                     1x 28588x                                       1x 31x                           1x   637x   636x 636x         635x   634x 634x         633x   632x 632x 631x   630x 630x 629x                     1x   6845x     6844x 5236x   1608x 1608x 1608x 1608x                   1x   5229x     5228x 4837x   391x 391x 391x 391x                   1x   6843x     6842x 5234x   1608x 1608x 1608x 1608x                   1x   2946x     2945x 2548x   397x 397x 397x 397x                   1x   9926x     9925x 9925x   9925x 14x     9925x 9925x   9925x 2426x     7499x 7499x 7499x 7499x                             1x   2654x 2653x     2651x 2651x   2651x 2651x   2651x 2x 2649x 2x     2651x 2x 2649x 2x     2651x     2651x       2651x         16x     2635x 2635x   2635x 6x     2629x 202x   2427x 2427x 2427x 2427x 2427x                           1x   18387x 18387x     18387x 18387x 18387x 18387x   18387x 28x     18359x       18359x 18359x 18359x 18359x 18359x                     1x   30x 29x     29x 5x     29x 29x   29x 29x   29x   29x 1x     29x 1x 28x       29x     29x       29x 29x 29x 29x   29x                     1x   8x 7x     6x 5x     6x 6x 6x 6x   6x                   1x   70429x 70429x     70428x 70428x   70428x 70428x   70428x 86x 86x 15x     70428x                   1x                       1x   585x     584x 584x   584x 8x   584x   584x 584x 584x 584x   584x 584x   584x 584x 584x 584x   584x 584x 584x   584x 584x 584x   584x 584x 584x   584x 11x 573x 275x   298x     584x 4088x 4088x 381x 381x       584x 298x 298x 298x 298x 298x 298x   584x 584x                           1x                 83x 82x 80x 79x 77x 76x 74x 73x 71x   70x 68x     66x 7x           66x 63x 63x 63x   3x 3x 3x   66x 66x 66x     66x     66x 60x   66x     66x 61x     66x                 1x                  
import Cartesian3 from "./Cartesian3.js";
import Cartographic from "./Cartographic.js";
import Check from "./Check.js";
import defined from "./defined.js";
import Ellipsoid from "./Ellipsoid.js";
import CesiumMath from "./Math.js";
import Transforms from "./Transforms.js";
import Matrix4 from "./Matrix4.js";
 
/**
 * A two dimensional region specified as longitude and latitude coordinates.
 *
 * @alias Rectangle
 * @constructor
 *
 * @param {number} [west=0.0] The westernmost longitude, in radians, in the range [-Pi, Pi].
 * @param {number} [south=0.0] The southernmost latitude, in radians, in the range [-Pi/2, Pi/2].
 * @param {number} [east=0.0] The easternmost longitude, in radians, in the range [-Pi, Pi].
 * @param {number} [north=0.0] The northernmost latitude, in radians, in the range [-Pi/2, Pi/2].
 *
 * @see Packable
 */
function Rectangle(west, south, east, north) {
  /**
   * The westernmost longitude in radians in the range [-Pi, Pi].
   *
   * @type {number}
   * @default 0.0
   */
  this.west = west ?? 0.0;
 
  /**
   * The southernmost latitude in radians in the range [-Pi/2, Pi/2].
   *
   * @type {number}
   * @default 0.0
   */
  this.south = south ?? 0.0;
 
  /**
   * The easternmost longitude in radians in the range [-Pi, Pi].
   *
   * @type {number}
   * @default 0.0
   */
  this.east = east ?? 0.0;
 
  /**
   * The northernmost latitude in radians in the range [-Pi/2, Pi/2].
   *
   * @type {number}
   * @default 0.0
   */
  this.north = north ?? 0.0;
}
 
Object.defineProperties(Rectangle.prototype, {
  /**
   * Gets the width of the rectangle in radians.
   * @memberof Rectangle.prototype
   * @type {number}
   * @readonly
   */
  width: {
    get: function () {
      return Rectangle.computeWidth(this);
    },
  },
 
  /**
   * Gets the height of the rectangle in radians.
   * @memberof Rectangle.prototype
   * @type {number}
   * @readonly
   */
  height: {
    get: function () {
      return Rectangle.computeHeight(this);
    },
  },
});
 
/**
 * The number of elements used to pack the object into an array.
 * @type {number}
 */
Rectangle.packedLength = 4;
 
/**
 * Stores the provided instance into the provided array.
 *
 * @param {Rectangle} value The value to pack.
 * @param {number[]} array The array to pack into.
 * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
 *
 * @returns {number[]} The array that was packed into
 */
Rectangle.pack = function (value, array, startingIndex) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("value", value);
  Check.defined("array", array);
  //>>includeEnd('debug');
 
  startingIndex = startingIndex ?? 0;
 
  array[startingIndex++] = value.west;
  array[startingIndex++] = value.south;
  array[startingIndex++] = value.east;
  array[startingIndex] = value.north;
 
  return array;
};
 
/**
 * Retrieves an instance from a packed array.
 *
 * @param {number[]} array The packed array.
 * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
 * @param {Rectangle} [result] The object into which to store the result.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
 */
Rectangle.unpack = function (array, startingIndex, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("array", array);
  //>>includeEnd('debug');
 
  startingIndex = startingIndex ?? 0;
 
  if (!defined(result)) {
    result = new Rectangle();
  }
 
  result.west = array[startingIndex++];
  result.south = array[startingIndex++];
  result.east = array[startingIndex++];
  result.north = array[startingIndex];
  return result;
};
 
/**
 * Computes the width of a rectangle in radians.
 * @param {Rectangle} rectangle The rectangle to compute the width of.
 * @returns {number} The width.
 */
Rectangle.computeWidth = function (rectangle) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
  let east = rectangle.east;
  const west = rectangle.west;
  if (east < west) {
    east += CesiumMath.TWO_PI;
  }
  return east - west;
};
 
/**
 * Computes the height of a rectangle in radians.
 * @param {Rectangle} rectangle The rectangle to compute the height of.
 * @returns {number} The height.
 */
Rectangle.computeHeight = function (rectangle) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
  return rectangle.north - rectangle.south;
};
 
/**
 * Creates a rectangle given the boundary longitude and latitude in degrees.
 *
 * @param {number} [west=0.0] The westernmost longitude in degrees in the range [-180.0, 180.0].
 * @param {number} [south=0.0] The southernmost latitude in degrees in the range [-90.0, 90.0].
 * @param {number} [east=0.0] The easternmost longitude in degrees in the range [-180.0, 180.0].
 * @param {number} [north=0.0] The northernmost latitude in degrees in the range [-90.0, 90.0].
 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 *
 * @example
 * const rectangle = Cesium.Rectangle.fromDegrees(0.0, 20.0, 10.0, 30.0);
 */
Rectangle.fromDegrees = function (west, south, east, north, result) {
  west = CesiumMath.toRadians(west ?? 0.0);
  south = CesiumMath.toRadians(south ?? 0.0);
  east = CesiumMath.toRadians(east ?? 0.0);
  north = CesiumMath.toRadians(north ?? 0.0);
 
  if (!defined(result)) {
    return new Rectangle(west, south, east, north);
  }
 
  result.west = west;
  result.south = south;
  result.east = east;
  result.north = north;
 
  return result;
};
 
/**
 * Creates a rectangle given the boundary longitude and latitude in radians.
 *
 * @param {number} [west=0.0] The westernmost longitude in radians in the range [-Math.PI, Math.PI].
 * @param {number} [south=0.0] The southernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
 * @param {number} [east=0.0] The easternmost longitude in radians in the range [-Math.PI, Math.PI].
 * @param {number} [north=0.0] The northernmost latitude in radians in the range [-Math.PI/2, Math.PI/2].
 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 *
 * @example
 * const rectangle = Cesium.Rectangle.fromRadians(0.0, Math.PI/4, Math.PI/8, 3*Math.PI/4);
 */
Rectangle.fromRadians = function (west, south, east, north, result) {
  if (!defined(result)) {
    return new Rectangle(west, south, east, north);
  }
 
  result.west = west ?? 0.0;
  result.south = south ?? 0.0;
  result.east = east ?? 0.0;
  result.north = north ?? 0.0;
 
  return result;
};
 
/**
 * Creates the smallest possible Rectangle that encloses all positions in the provided array.
 *
 * @param {Cartographic[]} cartographics The list of Cartographic instances.
 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 */
Rectangle.fromCartographicArray = function (cartographics, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("cartographics", cartographics);
  //>>includeEnd('debug');
 
  let west = Number.MAX_VALUE;
  let east = -Number.MAX_VALUE;
  let westOverIDL = Number.MAX_VALUE;
  let eastOverIDL = -Number.MAX_VALUE;
  let south = Number.MAX_VALUE;
  let north = -Number.MAX_VALUE;
 
  for (let i = 0, len = cartographics.length; i < len; i++) {
    const position = cartographics[i];
    west = Math.min(west, position.longitude);
    east = Math.max(east, position.longitude);
    south = Math.min(south, position.latitude);
    north = Math.max(north, position.latitude);
 
    const lonAdjusted =
      position.longitude >= 0
        ? position.longitude
        : position.longitude + CesiumMath.TWO_PI;
    westOverIDL = Math.min(westOverIDL, lonAdjusted);
    eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  }
 
  if (east - west > eastOverIDL - westOverIDL) {
    west = westOverIDL;
    east = eastOverIDL;
 
    Eif (east > CesiumMath.PI) {
      east = east - CesiumMath.TWO_PI;
    }
    Iif (west > CesiumMath.PI) {
      west = west - CesiumMath.TWO_PI;
    }
  }
 
  if (!defined(result)) {
    return new Rectangle(west, south, east, north);
  }
 
  result.west = west;
  result.south = south;
  result.east = east;
  result.north = north;
  return result;
};
 
/**
 * Creates the smallest possible Rectangle that encloses all positions in the provided array.
 *
 * @param {Cartesian3[]} cartesians The list of Cartesian instances.
 * @param {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid the cartesians are on.
 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 */
Rectangle.fromCartesianArray = function (cartesians, ellipsoid, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("cartesians", cartesians);
  //>>includeEnd('debug');
  ellipsoid = ellipsoid ?? Ellipsoid.default;
 
  let west = Number.MAX_VALUE;
  let east = -Number.MAX_VALUE;
  let westOverIDL = Number.MAX_VALUE;
  let eastOverIDL = -Number.MAX_VALUE;
  let south = Number.MAX_VALUE;
  let north = -Number.MAX_VALUE;
 
  for (let i = 0, len = cartesians.length; i < len; i++) {
    const position = ellipsoid.cartesianToCartographic(cartesians[i]);
    west = Math.min(west, position.longitude);
    east = Math.max(east, position.longitude);
    south = Math.min(south, position.latitude);
    north = Math.max(north, position.latitude);
 
    const lonAdjusted =
      position.longitude >= 0
        ? position.longitude
        : position.longitude + CesiumMath.TWO_PI;
    westOverIDL = Math.min(westOverIDL, lonAdjusted);
    eastOverIDL = Math.max(eastOverIDL, lonAdjusted);
  }
 
  if (east - west > eastOverIDL - westOverIDL) {
    west = westOverIDL;
    east = eastOverIDL;
 
    Eif (east > CesiumMath.PI) {
      east = east - CesiumMath.TWO_PI;
    }
    if (west > CesiumMath.PI) {
      west = west - CesiumMath.TWO_PI;
    }
  }
 
  if (!defined(result)) {
    return new Rectangle(west, south, east, north);
  }
 
  result.west = west;
  result.south = south;
  result.east = east;
  result.north = north;
  return result;
};
 
const fromBoundingSphereMatrixScratch = new Cartesian3();
const fromBoundingSphereEastScratch = new Cartesian3();
const fromBoundingSphereNorthScratch = new Cartesian3();
const fromBoundingSphereWestScratch = new Cartesian3();
const fromBoundingSphereSouthScratch = new Cartesian3();
const fromBoundingSpherePositionsScratch = new Array(5);
for (let n = 0; n < fromBoundingSpherePositionsScratch.length; ++n) {
  fromBoundingSpherePositionsScratch[n] = new Cartesian3();
}
/**
 * Create a rectangle from a bounding sphere, ignoring height.
 *
 *
 * @param {BoundingSphere} boundingSphere The bounding sphere.
 * @param {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid.
 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 */
Rectangle.fromBoundingSphere = function (boundingSphere, ellipsoid, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("boundingSphere", boundingSphere);
  //>>includeEnd('debug');
 
  const center = boundingSphere.center;
  const radius = boundingSphere.radius;
 
  if (!defined(ellipsoid)) {
    ellipsoid = Ellipsoid.default;
  }
 
  if (!defined(result)) {
    result = new Rectangle();
  }
 
  if (Cartesian3.equals(center, Cartesian3.ZERO)) {
    Rectangle.clone(Rectangle.MAX_VALUE, result);
    return result;
  }
 
  const fromENU = Transforms.eastNorthUpToFixedFrame(
    center,
    ellipsoid,
    fromBoundingSphereMatrixScratch,
  );
  const east = Matrix4.multiplyByPointAsVector(
    fromENU,
    Cartesian3.UNIT_X,
    fromBoundingSphereEastScratch,
  );
  Cartesian3.normalize(east, east);
  const north = Matrix4.multiplyByPointAsVector(
    fromENU,
    Cartesian3.UNIT_Y,
    fromBoundingSphereNorthScratch,
  );
  Cartesian3.normalize(north, north);
 
  Cartesian3.multiplyByScalar(north, radius, north);
  Cartesian3.multiplyByScalar(east, radius, east);
 
  const south = Cartesian3.negate(north, fromBoundingSphereSouthScratch);
  const west = Cartesian3.negate(east, fromBoundingSphereWestScratch);
 
  const positions = fromBoundingSpherePositionsScratch;
 
  // North
  let corner = positions[0];
  Cartesian3.add(center, north, corner);
 
  // West
  corner = positions[1];
  Cartesian3.add(center, west, corner);
 
  // South
  corner = positions[2];
  Cartesian3.add(center, south, corner);
 
  // East
  corner = positions[3];
  Cartesian3.add(center, east, corner);
 
  positions[4] = center;
 
  return Rectangle.fromCartesianArray(positions, ellipsoid, result);
};
 
/**
 * Duplicates a Rectangle.
 *
 * @param {Rectangle} rectangle The rectangle to clone.
 * @param {Rectangle} [result] The object onto which to store the result, or undefined if a new instance should be created.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided. (Returns undefined if rectangle is undefined)
 */
Rectangle.clone = function (rectangle, result) {
  if (!defined(rectangle)) {
    return undefined;
  }
 
  if (!defined(result)) {
    return new Rectangle(
      rectangle.west,
      rectangle.south,
      rectangle.east,
      rectangle.north,
    );
  }
 
  result.west = rectangle.west;
  result.south = rectangle.south;
  result.east = rectangle.east;
  result.north = rectangle.north;
  return result;
};
 
/**
 * Compares the provided Rectangles componentwise and returns
 * <code>true</code> if they pass an absolute or relative tolerance test,
 * <code>false</code> otherwise.
 *
 * @param {Rectangle} [left] The first Rectangle.
 * @param {Rectangle} [right] The second Rectangle.
 * @param {number} [absoluteEpsilon=0] The absolute epsilon tolerance to use for equality testing.
 * @returns {boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
 */
Rectangle.equalsEpsilon = function (left, right, absoluteEpsilon) {
  absoluteEpsilon = absoluteEpsilon ?? 0;
 
  return (
    left === right ||
    (defined(left) &&
      defined(right) &&
      Math.abs(left.west - right.west) <= absoluteEpsilon &&
      Math.abs(left.south - right.south) <= absoluteEpsilon &&
      Math.abs(left.east - right.east) <= absoluteEpsilon &&
      Math.abs(left.north - right.north) <= absoluteEpsilon)
  );
};
 
/**
 * Duplicates this Rectangle.
 *
 * @param {Rectangle} [result] The object onto which to store the result.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 */
Rectangle.prototype.clone = function (result) {
  return Rectangle.clone(this, result);
};
 
/**
 * Compares the provided Rectangle with this Rectangle componentwise and returns
 * <code>true</code> if they are equal, <code>false</code> otherwise.
 *
 * @param {Rectangle} [other] The Rectangle to compare.
 * @returns {boolean} <code>true</code> if the Rectangles are equal, <code>false</code> otherwise.
 */
Rectangle.prototype.equals = function (other) {
  return Rectangle.equals(this, other);
};
 
/**
 * Compares the provided rectangles and returns <code>true</code> if they are equal,
 * <code>false</code> otherwise.
 *
 * @param {Rectangle} [left] The first Rectangle.
 * @param {Rectangle} [right] The second Rectangle.
 * @returns {boolean} <code>true</code> if left and right are equal; otherwise <code>false</code>.
 */
Rectangle.equals = function (left, right) {
  return (
    left === right ||
    (defined(left) &&
      defined(right) &&
      left.west === right.west &&
      left.south === right.south &&
      left.east === right.east &&
      left.north === right.north)
  );
};
 
/**
 * Compares the provided Rectangle with this Rectangle componentwise and returns
 * <code>true</code> if they are within the provided epsilon,
 * <code>false</code> otherwise.
 *
 * @param {Rectangle} [other] The Rectangle to compare.
 * @param {number} [epsilon=0] The epsilon to use for equality testing.
 * @returns {boolean} <code>true</code> if the Rectangles are within the provided epsilon, <code>false</code> otherwise.
 */
Rectangle.prototype.equalsEpsilon = function (other, epsilon) {
  return Rectangle.equalsEpsilon(this, other, epsilon);
};
 
/**
 * Checks a Rectangle's properties and throws if they are not in valid ranges.
 *
 * @param {Rectangle} rectangle The rectangle to validate
 *
 * @exception {DeveloperError} <code>north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
 * @exception {DeveloperError} <code>south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
 * @exception {DeveloperError} <code>east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
 * @exception {DeveloperError} <code>west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
 * @private
 */
Rectangle._validate = function (rectangle) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
 
  const north = rectangle.north;
  Check.typeOf.number.greaterThanOrEquals(
    "north",
    north,
    -CesiumMath.PI_OVER_TWO,
  );
  Check.typeOf.number.lessThanOrEquals("north", north, CesiumMath.PI_OVER_TWO);
 
  const south = rectangle.south;
  Check.typeOf.number.greaterThanOrEquals(
    "south",
    south,
    -CesiumMath.PI_OVER_TWO,
  );
  Check.typeOf.number.lessThanOrEquals("south", south, CesiumMath.PI_OVER_TWO);
 
  const west = rectangle.west;
  Check.typeOf.number.greaterThanOrEquals("west", west, -Math.PI);
  Check.typeOf.number.lessThanOrEquals("west", west, Math.PI);
 
  const east = rectangle.east;
  Check.typeOf.number.greaterThanOrEquals("east", east, -Math.PI);
  Check.typeOf.number.lessThanOrEquals("east", east, Math.PI);
  //>>includeEnd('debug');
};
 
/**
 * Computes the southwest corner of a rectangle.
 *
 * @param {Rectangle} rectangle The rectangle for which to find the corner
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
 */
Rectangle.southwest = function (rectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
 
  if (!defined(result)) {
    return new Cartographic(rectangle.west, rectangle.south);
  }
  result.longitude = rectangle.west;
  result.latitude = rectangle.south;
  result.height = 0.0;
  return result;
};
 
/**
 * Computes the northwest corner of a rectangle.
 *
 * @param {Rectangle} rectangle The rectangle for which to find the corner
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
 */
Rectangle.northwest = function (rectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
 
  if (!defined(result)) {
    return new Cartographic(rectangle.west, rectangle.north);
  }
  result.longitude = rectangle.west;
  result.latitude = rectangle.north;
  result.height = 0.0;
  return result;
};
 
/**
 * Computes the northeast corner of a rectangle.
 *
 * @param {Rectangle} rectangle The rectangle for which to find the corner
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
 */
Rectangle.northeast = function (rectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
 
  if (!defined(result)) {
    return new Cartographic(rectangle.east, rectangle.north);
  }
  result.longitude = rectangle.east;
  result.latitude = rectangle.north;
  result.height = 0.0;
  return result;
};
 
/**
 * Computes the southeast corner of a rectangle.
 *
 * @param {Rectangle} rectangle The rectangle for which to find the corner
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
 */
Rectangle.southeast = function (rectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
 
  if (!defined(result)) {
    return new Cartographic(rectangle.east, rectangle.south);
  }
  result.longitude = rectangle.east;
  result.latitude = rectangle.south;
  result.height = 0.0;
  return result;
};
 
/**
 * Computes the center of a rectangle.
 *
 * @param {Rectangle} rectangle The rectangle for which to find the center
 * @param {Cartographic} [result] The object onto which to store the result.
 * @returns {Cartographic} The modified result parameter or a new Cartographic instance if none was provided.
 */
Rectangle.center = function (rectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
 
  let east = rectangle.east;
  const west = rectangle.west;
 
  if (east < west) {
    east += CesiumMath.TWO_PI;
  }
 
  const longitude = CesiumMath.negativePiToPi((west + east) * 0.5);
  const latitude = (rectangle.south + rectangle.north) * 0.5;
 
  if (!defined(result)) {
    return new Cartographic(longitude, latitude);
  }
 
  result.longitude = longitude;
  result.latitude = latitude;
  result.height = 0.0;
  return result;
};
 
/**
 * Computes the intersection of two rectangles.  This function assumes that the rectangle's coordinates are
 * latitude and longitude in radians and produces a correct intersection, taking into account the fact that
 * the same angle can be represented with multiple values as well as the wrapping of longitude at the
 * anti-meridian.  For a simple intersection that ignores these factors and can be used with projected
 * coordinates, see {@link Rectangle.simpleIntersection}.
 *
 * @param {Rectangle} rectangle On rectangle to find an intersection
 * @param {Rectangle} otherRectangle Another rectangle to find an intersection
 * @param {Rectangle} [result] The object onto which to store the result.
 * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
 */
Rectangle.intersection = function (rectangle, otherRectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  Check.typeOf.object("otherRectangle", otherRectangle);
  //>>includeEnd('debug');
 
  let rectangleEast = rectangle.east;
  let rectangleWest = rectangle.west;
 
  let otherRectangleEast = otherRectangle.east;
  let otherRectangleWest = otherRectangle.west;
 
  if (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
    rectangleEast += CesiumMath.TWO_PI;
  } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
    otherRectangleEast += CesiumMath.TWO_PI;
  }
 
  if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
    otherRectangleWest += CesiumMath.TWO_PI;
  } else if (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
    rectangleWest += CesiumMath.TWO_PI;
  }
 
  const west = CesiumMath.negativePiToPi(
    Math.max(rectangleWest, otherRectangleWest),
  );
  const east = CesiumMath.negativePiToPi(
    Math.min(rectangleEast, otherRectangleEast),
  );
 
  if (
    (rectangle.west < rectangle.east ||
      otherRectangle.west < otherRectangle.east) &&
    east <= west
  ) {
    return undefined;
  }
 
  const south = Math.max(rectangle.south, otherRectangle.south);
  const north = Math.min(rectangle.north, otherRectangle.north);
 
  if (south >= north) {
    return undefined;
  }
 
  if (!defined(result)) {
    return new Rectangle(west, south, east, north);
  }
  result.west = west;
  result.south = south;
  result.east = east;
  result.north = north;
  return result;
};
 
/**
 * Computes a simple intersection of two rectangles.  Unlike {@link Rectangle.intersection}, this function
 * does not attempt to put the angular coordinates into a consistent range or to account for crossing the
 * anti-meridian.  As such, it can be used for rectangles where the coordinates are not simply latitude
 * and longitude (i.e. projected coordinates).
 *
 * @param {Rectangle} rectangle On rectangle to find an intersection
 * @param {Rectangle} otherRectangle Another rectangle to find an intersection
 * @param {Rectangle} [result] The object onto which to store the result.
 * @returns {Rectangle|undefined} The modified result parameter, a new Rectangle instance if none was provided or undefined if there is no intersection.
 */
Rectangle.simpleIntersection = function (rectangle, otherRectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  Check.typeOf.object("otherRectangle", otherRectangle);
  //>>includeEnd('debug');
 
  const west = Math.max(rectangle.west, otherRectangle.west);
  const south = Math.max(rectangle.south, otherRectangle.south);
  const east = Math.min(rectangle.east, otherRectangle.east);
  const north = Math.min(rectangle.north, otherRectangle.north);
 
  if (south >= north || west >= east) {
    return undefined;
  }
 
  Iif (!defined(result)) {
    return new Rectangle(west, south, east, north);
  }
 
  result.west = west;
  result.south = south;
  result.east = east;
  result.north = north;
  return result;
};
 
/**
 * Computes a rectangle that is the union of two rectangles.
 *
 * @param {Rectangle} rectangle A rectangle to enclose in rectangle.
 * @param {Rectangle} otherRectangle A rectangle to enclose in a rectangle.
 * @param {Rectangle} [result] The object onto which to store the result.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 */
Rectangle.union = function (rectangle, otherRectangle, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  Check.typeOf.object("otherRectangle", otherRectangle);
  //>>includeEnd('debug');
 
  if (!defined(result)) {
    result = new Rectangle();
  }
 
  let rectangleEast = rectangle.east;
  let rectangleWest = rectangle.west;
 
  let otherRectangleEast = otherRectangle.east;
  let otherRectangleWest = otherRectangle.west;
 
  Iif (rectangleEast < rectangleWest && otherRectangleEast > 0.0) {
    rectangleEast += CesiumMath.TWO_PI;
  } else if (otherRectangleEast < otherRectangleWest && rectangleEast > 0.0) {
    otherRectangleEast += CesiumMath.TWO_PI;
  }
 
  if (rectangleEast < rectangleWest && otherRectangleWest < 0.0) {
    otherRectangleWest += CesiumMath.TWO_PI;
  } else Iif (otherRectangleEast < otherRectangleWest && rectangleWest < 0.0) {
    rectangleWest += CesiumMath.TWO_PI;
  }
 
  const west = CesiumMath.negativePiToPi(
    Math.min(rectangleWest, otherRectangleWest),
  );
  const east = CesiumMath.negativePiToPi(
    Math.max(rectangleEast, otherRectangleEast),
  );
 
  result.west = west;
  result.south = Math.min(rectangle.south, otherRectangle.south);
  result.east = east;
  result.north = Math.max(rectangle.north, otherRectangle.north);
 
  return result;
};
 
/**
 * Computes a rectangle by enlarging the provided rectangle until it contains the provided cartographic.
 *
 * @param {Rectangle} rectangle A rectangle to expand.
 * @param {Cartographic} cartographic A cartographic to enclose in a rectangle.
 * @param {Rectangle} [result] The object onto which to store the result.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if one was not provided.
 */
Rectangle.expand = function (rectangle, cartographic, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  Check.typeOf.object("cartographic", cartographic);
  //>>includeEnd('debug');
 
  if (!defined(result)) {
    result = new Rectangle();
  }
 
  result.west = Math.min(rectangle.west, cartographic.longitude);
  result.south = Math.min(rectangle.south, cartographic.latitude);
  result.east = Math.max(rectangle.east, cartographic.longitude);
  result.north = Math.max(rectangle.north, cartographic.latitude);
 
  return result;
};
 
/**
 * Returns true if the cartographic is on or inside the rectangle, false otherwise.
 *
 * @param {Rectangle} rectangle The rectangle
 * @param {Cartographic} cartographic The cartographic to test.
 * @returns {boolean} true if the provided cartographic is inside the rectangle, false otherwise.
 */
Rectangle.contains = function (rectangle, cartographic) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  Check.typeOf.object("cartographic", cartographic);
  //>>includeEnd('debug');
 
  let longitude = cartographic.longitude;
  const latitude = cartographic.latitude;
 
  const west = rectangle.west;
  let east = rectangle.east;
 
  if (east < west) {
    east += CesiumMath.TWO_PI;
    if (longitude < 0.0) {
      longitude += CesiumMath.TWO_PI;
    }
  }
  return (
    (longitude > west ||
      CesiumMath.equalsEpsilon(longitude, west, CesiumMath.EPSILON14)) &&
    (longitude < east ||
      CesiumMath.equalsEpsilon(longitude, east, CesiumMath.EPSILON14)) &&
    latitude >= rectangle.south &&
    latitude <= rectangle.north
  );
};
 
const subsampleLlaScratch = new Cartographic();
/**
 * Samples a rectangle so that it includes a list of Cartesian points suitable for passing to
 * {@link BoundingSphere#fromPoints}.  Sampling is necessary to account
 * for rectangles that cover the poles or cross the equator.
 *
 * @param {Rectangle} rectangle The rectangle to subsample.
 * @param {Ellipsoid} [ellipsoid=Ellipsoid.default] The ellipsoid to use.
 * @param {number} [surfaceHeight=0.0] The height of the rectangle above the ellipsoid.
 * @param {Cartesian3[]} [result] The array of Cartesians onto which to store the result.
 * @returns {Cartesian3[]} The modified result parameter or a new Array of Cartesians instances if none was provided.
 */
Rectangle.subsample = function (rectangle, ellipsoid, surfaceHeight, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  //>>includeEnd('debug');
 
  ellipsoid = ellipsoid ?? Ellipsoid.default;
  surfaceHeight = surfaceHeight ?? 0.0;
 
  if (!defined(result)) {
    result = [];
  }
  let length = 0;
 
  const north = rectangle.north;
  const south = rectangle.south;
  const east = rectangle.east;
  const west = rectangle.west;
 
  const lla = subsampleLlaScratch;
  lla.height = surfaceHeight;
 
  lla.longitude = west;
  lla.latitude = north;
  result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  length++;
 
  lla.longitude = east;
  result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  length++;
 
  lla.latitude = south;
  result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  length++;
 
  lla.longitude = west;
  result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
  length++;
 
  if (north < 0.0) {
    lla.latitude = north;
  } else if (south > 0.0) {
    lla.latitude = south;
  } else {
    lla.latitude = 0.0;
  }
 
  for (let i = 1; i < 8; ++i) {
    lla.longitude = -Math.PI + i * CesiumMath.PI_OVER_TWO;
    if (Rectangle.contains(rectangle, lla)) {
      result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
      length++;
    }
  }
 
  if (lla.latitude === 0.0) {
    lla.longitude = west;
    result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
    length++;
    lla.longitude = east;
    result[length] = ellipsoid.cartographicToCartesian(lla, result[length]);
    length++;
  }
  result.length = length;
  return result;
};
 
/**
 * Computes a subsection of a rectangle from normalized coordinates in the range [0.0, 1.0].
 *
 * @param {Rectangle} rectangle The rectangle to subsection.
 * @param {number} westLerp The west interpolation factor in the range [0.0, 1.0]. Must be less than or equal to eastLerp.
 * @param {number} southLerp The south interpolation factor in the range [0.0, 1.0]. Must be less than or equal to northLerp.
 * @param {number} eastLerp The east interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to westLerp.
 * @param {number} northLerp The north interpolation factor in the range [0.0, 1.0]. Must be greater than or equal to southLerp.
 * @param {Rectangle} [result] The object onto which to store the result.
 * @returns {Rectangle} The modified result parameter or a new Rectangle instance if none was provided.
 */
Rectangle.subsection = function (
  rectangle,
  westLerp,
  southLerp,
  eastLerp,
  northLerp,
  result,
) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("rectangle", rectangle);
  Check.typeOf.number.greaterThanOrEquals("westLerp", westLerp, 0.0);
  Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, 1.0);
  Check.typeOf.number.greaterThanOrEquals("southLerp", southLerp, 0.0);
  Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, 1.0);
  Check.typeOf.number.greaterThanOrEquals("eastLerp", eastLerp, 0.0);
  Check.typeOf.number.lessThanOrEquals("eastLerp", eastLerp, 1.0);
  Check.typeOf.number.greaterThanOrEquals("northLerp", northLerp, 0.0);
  Check.typeOf.number.lessThanOrEquals("northLerp", northLerp, 1.0);
 
  Check.typeOf.number.lessThanOrEquals("westLerp", westLerp, eastLerp);
  Check.typeOf.number.lessThanOrEquals("southLerp", southLerp, northLerp);
  //>>includeEnd('debug');
 
  if (!defined(result)) {
    result = new Rectangle();
  }
 
  // This function doesn't use CesiumMath.lerp because it has floating point precision problems
  // when the start and end values are the same but the t changes.
 
  if (rectangle.west <= rectangle.east) {
    const width = rectangle.east - rectangle.west;
    result.west = rectangle.west + westLerp * width;
    result.east = rectangle.west + eastLerp * width;
  } else {
    const width = CesiumMath.TWO_PI + rectangle.east - rectangle.west;
    result.west = CesiumMath.negativePiToPi(rectangle.west + westLerp * width);
    result.east = CesiumMath.negativePiToPi(rectangle.west + eastLerp * width);
  }
  const height = rectangle.north - rectangle.south;
  result.south = rectangle.south + southLerp * height;
  result.north = rectangle.south + northLerp * height;
 
  // Fix floating point precision problems when t = 1
  Iif (westLerp === 1.0) {
    result.west = rectangle.east;
  }
  if (eastLerp === 1.0) {
    result.east = rectangle.east;
  }
  Iif (southLerp === 1.0) {
    result.south = rectangle.north;
  }
  if (northLerp === 1.0) {
    result.north = rectangle.north;
  }
 
  return result;
};
 
/**
 * The largest possible rectangle.
 *
 * @type {Rectangle}
 * @constant
 */
Rectangle.MAX_VALUE = Object.freeze(
  new Rectangle(
    -Math.PI,
    -CesiumMath.PI_OVER_TWO,
    Math.PI,
    CesiumMath.PI_OVER_TWO,
  ),
);
export default Rectangle;