All files / engine/Source/Scene VoxelEllipsoidShape.js

93.15% Statements 286/307
78.89% Branches 86/109
100% Functions 15/15
93.13% Lines 285/306

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 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175                                                        39x 39x 39x 39x           39x           39x           39x           39x           39x           39x   39x                                   39x                                             39x     1x                     17x                           31x                           2x                           36x                       33x                       48x                         14x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                       1x             39x 39x 39x   39x 39x 39x     39x 39x 39x 39x 39x     39x 39x       39x   39x           39x           39x           39x           39x         39x             39x                     39x                         39x                             39x           39x 39x 39x 39x 39x   39x               39x                   39x           39x           39x           39x 39x 39x   39x     39x   39x     39x     39x       39x   39x       39x     39x     39x   39x         39x   39x     39x     39x       39x     39x   39x   39x     39x   39x 39x   39x     39x     39x       39x     39x   39x   39x     39x   39x 39x     39x 782x 782x       39x       39x 39x 39x 39x             39x                     39x     39x 39x 39x 39x   39x             39x 39x 39x         39x 33x 33x   33x       33x 33x   33x             33x             39x 33x     33x   33x   33x     33x 33x     33x 33x         33x             33x 33x   33x 33x               39x 33x           33x             33x       33x         33x   33x     33x   33x   33x   33x               39x   33x 33x 33x     33x       33x 33x   33x                 33x 33x 33x     33x       33x       33x 33x   33x       33x             39x 33x   33x             33x 33x   33x 33x               39x   39x     1x 1x 1x 1x           1x 61x 61x   61x 61x       61x               61x               61x         61x         61x 61x 61x           61x           1x                       1x               62x 61x 60x 59x 58x     57x 57x 57x 57x 57x 57x 57x   57x                 57x           57x           57x                     1x 1x 1x 1x 1x 1x                         14x 14x               14x             14x   14x         14x   42x     42x       42x   42x         42x                 42x 42x               14x               1x 1x 1x 1x               1x         14x 14x     14x                   14x   14x 14x         14x                     14x               14x     14x       14x         14x         14x   14x 14x 14x 14x   14x         14x 14x   14x         14x   14x     1x 1x 1x                     1x             5x 4x 3x 2x     1x 1x         1x           1x                   1x           1x               1x         1x           1x                                                                       97x             97x 97x         97x                   1x                             1x                  
import defined from "../Core/defined.js";
import BoundingSphere from "../Core/BoundingSphere.js";
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Cartographic from "../Core/Cartographic.js";
import Check from "../Core/Check.js";
import Ellipsoid from "../Core/Ellipsoid.js";
import CesiumMath from "../Core/Math.js";
import Matrix3 from "../Core/Matrix3.js";
import Matrix4 from "../Core/Matrix4.js";
import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
import Rectangle from "../Core/Rectangle.js";
import Transforms from "../Core/Transforms.js";
 
/**
 * An ellipsoid {@link VoxelShape}.
 *
 * @alias VoxelEllipsoidShape
 * @constructor
 *
 * @see VoxelShape
 * @see VoxelBoxShape
 * @see VoxelCylinderShape
 * @see VoxelShapeType
 *
 * @private
 */
function VoxelEllipsoidShape() {
  this._orientedBoundingBox = new OrientedBoundingBox();
  this._boundingSphere = new BoundingSphere();
  this._boundTransform = new Matrix4();
  this._shapeTransform = new Matrix4();
 
  /**
   * @type {Rectangle}
   * @private
   */
  this._rectangle = new Rectangle();
 
  /**
   * @type {number}
   * @private
   */
  this._minimumHeight = VoxelEllipsoidShape.DefaultMinBounds.z;
 
  /**
   * @type {number}
   * @private
   */
  this._maximumHeight = VoxelEllipsoidShape.DefaultMaxBounds.z;
 
  /**
   * @type {Ellipsoid}
   * @private
   */
  this._ellipsoid = new Ellipsoid();
 
  /**
   * @type {Cartesian3}
   * @private
   */
  this._translation = new Cartesian3();
 
  /**
   * @type {Matrix3}
   * @private
   */
  this._rotation = new Matrix3();
 
  this._shaderUniforms = {
    cameraPositionCartographic: new Cartesian3(),
    ellipsoidEcToEastNorthUp: new Matrix3(),
    ellipsoidRadii: new Cartesian3(),
    eccentricitySquared: 0.0,
    evoluteScale: new Cartesian2(),
    ellipsoidCurvatureAtLatitude: new Cartesian2(),
    ellipsoidInverseRadiiSquared: new Cartesian3(),
    ellipsoidRenderLongitudeMinMax: new Cartesian2(),
    ellipsoidShapeUvLongitudeRangeOrigin: 0.0,
    ellipsoidShapeUvLongitudeMinMaxMid: new Cartesian3(),
    ellipsoidLocalToShapeUvLongitude: new Cartesian2(),
    ellipsoidLocalToShapeUvLatitude: new Cartesian2(),
    ellipsoidRenderLatitudeSinMinMax: new Cartesian2(),
    ellipsoidInverseHeightDifference: 0.0,
    clipMinMaxHeight: new Cartesian2(),
  };
 
  this._shaderDefines = {
    ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_RANGE_EQUAL_ZERO: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_RANGE_UNDER_HALF: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_RANGE_OVER_HALF: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_MIN_DISCONTINUITY: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_MAX_DISCONTINUITY: undefined,
    ELLIPSOID_HAS_SHAPE_BOUNDS_LONGITUDE: undefined,
    ELLIPSOID_HAS_SHAPE_BOUNDS_LONGITUDE_MIN_MAX_REVERSED: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MAX_UNDER_HALF: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MAX_EQUAL_HALF: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MAX_OVER_HALF: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MIN_UNDER_HALF: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MIN_EQUAL_HALF: undefined,
    ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MIN_OVER_HALF: undefined,
    ELLIPSOID_HAS_SHAPE_BOUNDS_LATITUDE: undefined,
    ELLIPSOID_INTERSECTION_INDEX_LONGITUDE: undefined,
    ELLIPSOID_INTERSECTION_INDEX_LATITUDE_MAX: undefined,
    ELLIPSOID_INTERSECTION_INDEX_LATITUDE_MIN: undefined,
    ELLIPSOID_INTERSECTION_INDEX_HEIGHT_MAX: undefined,
    ELLIPSOID_INTERSECTION_INDEX_HEIGHT_MIN: undefined,
  };
 
  this._shaderMaximumIntersectionsLength = 0; // not known until update
}
 
Object.defineProperties(VoxelEllipsoidShape.prototype, {
  /**
   * An oriented bounding box containing the bounded shape.
   *
   * @memberof VoxelEllipsoidShape.prototype
   * @type {OrientedBoundingBox}
   * @readonly
   * @private
   */
  orientedBoundingBox: {
    get: function () {
      return this._orientedBoundingBox;
    },
  },
 
  /**
   * A bounding sphere containing the bounded shape.
   *
   * @memberof VoxelEllipsoidShape.prototype
   * @type {BoundingSphere}
   * @readonly
   * @private
   */
  boundingSphere: {
    get: function () {
      return this._boundingSphere;
    },
  },
 
  /**
   * A transformation matrix containing the bounded shape.
   *
   * @memberof VoxelEllipsoidShape.prototype
   * @type {Matrix4}
   * @readonly
   * @private
   */
  boundTransform: {
    get: function () {
      return this._boundTransform;
    },
  },
 
  /**
   * A transformation matrix containing the shape, ignoring the bounds.
   *
   * @memberof VoxelEllipsoidShape.prototype
   * @type {Matrix4}
   * @readonly
   * @private
   */
  shapeTransform: {
    get: function () {
      return this._shapeTransform;
    },
  },
 
  /**
   * @memberof VoxelEllipsoidShape.prototype
   * @type {Object<string, any>}
   * @readonly
   * @private
   */
  shaderUniforms: {
    get: function () {
      return this._shaderUniforms;
    },
  },
 
  /**
   * @memberof VoxelEllipsoidShape.prototype
   * @type {Object<string, any>}
   * @readonly
   * @private
   */
  shaderDefines: {
    get: function () {
      return this._shaderDefines;
    },
  },
 
  /**
   * The maximum number of intersections against the shape for any ray direction.
   * @memberof VoxelEllipsoidShape.prototype
   * @type {number}
   * @readonly
   * @private
   */
  shaderMaximumIntersectionsLength: {
    get: function () {
      return this._shaderMaximumIntersectionsLength;
    },
  },
});
 
const scratchActualMinBounds = new Cartesian3();
const scratchShapeMinBounds = new Cartesian3();
const scratchShapeMaxBounds = new Cartesian3();
const scratchClipMinBounds = new Cartesian3();
const scratchClipMaxBounds = new Cartesian3();
const scratchRenderMinBounds = new Cartesian3();
const scratchRenderMaxBounds = new Cartesian3();
const scratchScale = new Cartesian3();
const scratchShapeOuterExtent = new Cartesian3();
const scratchRenderOuterExtent = new Cartesian3();
const scratchRenderRectangle = new Rectangle();
 
/**
 * Update the shape's state.
 * @private
 * @param {Matrix4} modelMatrix The model matrix.
 * @param {Cartesian3} minBounds The minimum bounds.
 * @param {Cartesian3} maxBounds The maximum bounds.
 * @param {Cartesian3} [clipMinBounds=VoxelEllipsoidShape.DefaultMinBounds] The minimum clip bounds.
 * @param {Cartesian3} [clipMaxBounds=VoxelEllipsoidShape.DefaultMaxBounds] The maximum clip bounds.
 * @returns {boolean} Whether the shape is visible.
 */
VoxelEllipsoidShape.prototype.update = function (
  modelMatrix,
  minBounds,
  maxBounds,
  clipMinBounds,
  clipMaxBounds,
) {
  const { DefaultMinBounds, DefaultMaxBounds } = VoxelEllipsoidShape;
  clipMinBounds = clipMinBounds ?? DefaultMinBounds;
  clipMaxBounds = clipMaxBounds ?? DefaultMaxBounds;
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("modelMatrix", modelMatrix);
  Check.typeOf.object("minBounds", minBounds);
  Check.typeOf.object("maxBounds", maxBounds);
  //>>includeEnd('debug');
 
  const epsilonZeroScale = CesiumMath.EPSILON10;
  const epsilonLongitudeDiscontinuity = CesiumMath.EPSILON3; // 0.001 radians = 0.05729578 degrees
  const epsilonLongitude = CesiumMath.EPSILON10;
  const epsilonLatitude = CesiumMath.EPSILON10;
  const epsilonLatitudeFlat = CesiumMath.EPSILON3; // 0.001 radians = 0.05729578 degrees
 
  // Don't let the height go below the center of the ellipsoid.
  const radii = Matrix4.getScale(modelMatrix, scratchScale);
  const actualMinBounds = Cartesian3.clone(
    DefaultMinBounds,
    scratchActualMinBounds,
  );
  actualMinBounds.z = -Cartesian3.minimumComponent(radii);
 
  const shapeMinBounds = Cartesian3.clamp(
    minBounds,
    actualMinBounds,
    DefaultMaxBounds,
    scratchShapeMinBounds,
  );
  const shapeMaxBounds = Cartesian3.clamp(
    maxBounds,
    actualMinBounds,
    DefaultMaxBounds,
    scratchShapeMaxBounds,
  );
  const clampedClipMinBounds = Cartesian3.clamp(
    clipMinBounds,
    actualMinBounds,
    DefaultMaxBounds,
    scratchClipMinBounds,
  );
  const clampedClipMaxBounds = Cartesian3.clamp(
    clipMaxBounds,
    actualMinBounds,
    DefaultMaxBounds,
    scratchClipMaxBounds,
  );
  const renderMinBounds = Cartesian3.maximumByComponent(
    shapeMinBounds,
    clampedClipMinBounds,
    scratchRenderMinBounds,
  );
  const renderMaxBounds = Cartesian3.minimumByComponent(
    shapeMaxBounds,
    clampedClipMaxBounds,
    scratchRenderMaxBounds,
  );
 
  // Compute the farthest a point can be from the center of the ellipsoid.
  const shapeOuterExtent = Cartesian3.add(
    radii,
    Cartesian3.fromElements(
      shapeMaxBounds.z,
      shapeMaxBounds.z,
      shapeMaxBounds.z,
      scratchShapeOuterExtent,
    ),
    scratchShapeOuterExtent,
  );
 
  const renderOuterExtent = Cartesian3.add(
    radii,
    Cartesian3.fromElements(
      renderMaxBounds.z,
      renderMaxBounds.z,
      renderMaxBounds.z,
      scratchRenderOuterExtent,
    ),
    scratchRenderOuterExtent,
  );
 
  // Exit early if the shape is not visible.
  // Note that minLongitude may be greater than maxLongitude when crossing the 180th meridian.
  Iif (
    renderMinBounds.y > renderMaxBounds.y ||
    renderMinBounds.y === DefaultMaxBounds.y ||
    renderMaxBounds.y === DefaultMinBounds.y ||
    renderMinBounds.z > renderMaxBounds.z ||
    CesiumMath.equalsEpsilon(
      renderOuterExtent,
      Cartesian3.ZERO,
      undefined,
      epsilonZeroScale,
    )
  ) {
    return false;
  }
 
  this._rectangle = Rectangle.fromRadians(
    shapeMinBounds.x,
    shapeMinBounds.y,
    shapeMaxBounds.x,
    shapeMaxBounds.y,
  );
  this._translation = Matrix4.getTranslation(modelMatrix, this._translation);
  this._rotation = Matrix4.getRotation(modelMatrix, this._rotation);
  this._ellipsoid = Ellipsoid.fromCartesian3(radii, this._ellipsoid);
  this._minimumHeight = shapeMinBounds.z;
  this._maximumHeight = shapeMaxBounds.z;
 
  const renderRectangle = Rectangle.fromRadians(
    renderMinBounds.x,
    renderMinBounds.y,
    renderMaxBounds.x,
    renderMaxBounds.y,
    scratchRenderRectangle,
  );
 
  this._orientedBoundingBox = getEllipsoidChunkObb(
    renderRectangle,
    renderMinBounds.z,
    renderMaxBounds.z,
    this._ellipsoid,
    this._translation,
    this._rotation,
    this._orientedBoundingBox,
  );
 
  this._shapeTransform = Matrix4.fromRotationTranslation(
    this._rotation,
    this._translation,
    this._shapeTransform,
  );
 
  this._boundTransform = Matrix4.fromRotationTranslation(
    this._orientedBoundingBox.halfAxes,
    this._orientedBoundingBox.center,
    this._boundTransform,
  );
 
  this._boundingSphere = BoundingSphere.fromOrientedBoundingBox(
    this._orientedBoundingBox,
    this._boundingSphere,
  );
 
  // Longitude
  const defaultLongitudeRange = DefaultMaxBounds.x - DefaultMinBounds.x;
  const defaultLongitudeRangeHalf = 0.5 * defaultLongitudeRange;
  const renderIsLongitudeReversed = renderMaxBounds.x < renderMinBounds.x;
  const renderLongitudeRange =
    renderMaxBounds.x -
    renderMinBounds.x +
    renderIsLongitudeReversed * defaultLongitudeRange;
  const renderIsLongitudeRangeZero = renderLongitudeRange <= epsilonLongitude;
  const renderIsLongitudeRangeUnderHalf =
    renderLongitudeRange >= defaultLongitudeRangeHalf - epsilonLongitude &&
    renderLongitudeRange < defaultLongitudeRange - epsilonLongitude;
  const renderIsLongitudeRangeOverHalf =
    renderLongitudeRange > epsilonLongitude &&
    renderLongitudeRange < defaultLongitudeRangeHalf - epsilonLongitude;
  const renderHasLongitude =
    renderIsLongitudeRangeZero ||
    renderIsLongitudeRangeUnderHalf ||
    renderIsLongitudeRangeOverHalf;
 
  const shapeIsLongitudeReversed = shapeMaxBounds.x < shapeMinBounds.x;
  const shapeLongitudeRange =
    shapeMaxBounds.x -
    shapeMinBounds.x +
    shapeIsLongitudeReversed * defaultLongitudeRange;
  const shapeIsLongitudeRangeUnderHalf =
    shapeLongitudeRange > defaultLongitudeRangeHalf + epsilonLongitude &&
    shapeLongitudeRange < defaultLongitudeRange - epsilonLongitude;
  const shapeIsLongitudeRangeHalf =
    shapeLongitudeRange >= defaultLongitudeRangeHalf - epsilonLongitude &&
    shapeLongitudeRange <= defaultLongitudeRangeHalf + epsilonLongitude;
  const shapeIsLongitudeRangeOverHalf =
    shapeLongitudeRange < defaultLongitudeRangeHalf - epsilonLongitude;
  const shapeHasLongitude =
    shapeIsLongitudeRangeUnderHalf ||
    shapeIsLongitudeRangeHalf ||
    shapeIsLongitudeRangeOverHalf;
 
  // Latitude
  const renderIsLatitudeMaxUnderHalf = renderMaxBounds.y < -epsilonLatitudeFlat;
  const renderIsLatitudeMaxHalf =
    renderMaxBounds.y >= -epsilonLatitudeFlat &&
    renderMaxBounds.y <= +epsilonLatitudeFlat;
  const renderIsLatitudeMaxOverHalf =
    renderMaxBounds.y > +epsilonLatitudeFlat &&
    renderMaxBounds.y < DefaultMaxBounds.y - epsilonLatitude;
  const renderHasLatitudeMax =
    renderIsLatitudeMaxUnderHalf ||
    renderIsLatitudeMaxHalf ||
    renderIsLatitudeMaxOverHalf;
  const renderIsLatitudeMinUnderHalf =
    renderMinBounds.y > DefaultMinBounds.y + epsilonLatitude &&
    renderMinBounds.y < -epsilonLatitudeFlat;
  const renderIsLatitudeMinHalf =
    renderMinBounds.y >= -epsilonLatitudeFlat &&
    renderMinBounds.y <= +epsilonLatitudeFlat;
  const renderIsLatitudeMinOverHalf = renderMinBounds.y > +epsilonLatitudeFlat;
  const renderHasLatitudeMin =
    renderIsLatitudeMinUnderHalf ||
    renderIsLatitudeMinHalf ||
    renderIsLatitudeMinOverHalf;
  const renderHasLatitude = renderHasLatitudeMax || renderHasLatitudeMin;
 
  const shapeLatitudeRange = shapeMaxBounds.y - shapeMinBounds.y;
  const shapeIsLatitudeMaxUnderHalf = shapeMaxBounds.y < -epsilonLatitudeFlat;
  const shapeIsLatitudeMaxHalf =
    shapeMaxBounds.y >= -epsilonLatitudeFlat &&
    shapeMaxBounds.y <= +epsilonLatitudeFlat;
  const shapeIsLatitudeMaxOverHalf =
    shapeMaxBounds.y > +epsilonLatitudeFlat &&
    shapeMaxBounds.y < DefaultMaxBounds.y - epsilonLatitude;
  const shapeHasLatitudeMax =
    shapeIsLatitudeMaxUnderHalf ||
    shapeIsLatitudeMaxHalf ||
    shapeIsLatitudeMaxOverHalf;
  const shapeIsLatitudeMinUnderHalf =
    shapeMinBounds.y > DefaultMinBounds.y + epsilonLatitude &&
    shapeMinBounds.y < -epsilonLatitudeFlat;
  const shapeIsLatitudeMinHalf =
    shapeMinBounds.y >= -epsilonLatitudeFlat &&
    shapeMinBounds.y <= +epsilonLatitudeFlat;
  const shapeIsLatitudeMinOverHalf = shapeMinBounds.y > +epsilonLatitudeFlat;
  const shapeHasLatitudeMin =
    shapeIsLatitudeMinUnderHalf ||
    shapeIsLatitudeMinHalf ||
    shapeIsLatitudeMinOverHalf;
  const shapeHasLatitude = shapeHasLatitudeMax || shapeHasLatitudeMin;
 
  const shaderUniforms = this._shaderUniforms;
  const shaderDefines = this._shaderDefines;
 
  // To keep things simple, clear the defines every time
  for (const key in shaderDefines) {
    Eif (shaderDefines.hasOwnProperty(key)) {
      shaderDefines[key] = undefined;
    }
  }
 
  shaderUniforms.ellipsoidRadii = Cartesian3.clone(
    shapeOuterExtent,
    shaderUniforms.ellipsoidRadii,
  );
  const { x: radiiX, z: radiiZ } = shaderUniforms.ellipsoidRadii;
  const axisRatio = radiiZ / radiiX;
  shaderUniforms.eccentricitySquared = 1.0 - axisRatio * axisRatio;
  shaderUniforms.evoluteScale = Cartesian2.fromElements(
    (radiiX * radiiX - radiiZ * radiiZ) / radiiX,
    (radiiZ * radiiZ - radiiX * radiiX) / radiiZ,
    shaderUniforms.evoluteScale,
  );
 
  // Used to compute geodetic surface normal.
  shaderUniforms.ellipsoidInverseRadiiSquared = Cartesian3.divideComponents(
    Cartesian3.ONE,
    Cartesian3.multiplyComponents(
      shaderUniforms.ellipsoidRadii,
      shaderUniforms.ellipsoidRadii,
      shaderUniforms.ellipsoidInverseRadiiSquared,
    ),
    shaderUniforms.ellipsoidInverseRadiiSquared,
  );
 
  // Keep track of how many intersections there are going to be.
  let intersectionCount = 0;
 
  // Intersects outer and inner ellipsoid for the max and min height.
  shaderDefines["ELLIPSOID_INTERSECTION_INDEX_HEIGHT_MAX"] = intersectionCount;
  intersectionCount += 1;
  shaderDefines["ELLIPSOID_INTERSECTION_INDEX_HEIGHT_MIN"] = intersectionCount;
  intersectionCount += 1;
 
  shaderUniforms.clipMinMaxHeight = Cartesian2.fromElements(
    renderMinBounds.z - shapeMaxBounds.z,
    renderMaxBounds.z - shapeMaxBounds.z,
    shaderUniforms.clipMinMaxHeight,
  );
 
  // The percent of space that is between the inner and outer ellipsoid.
  const thickness = shapeMaxBounds.z - shapeMinBounds.z;
  shaderUniforms.ellipsoidInverseHeightDifference = 1.0 / thickness;
  Iif (shapeMinBounds.z === shapeMaxBounds.z) {
    shaderUniforms.ellipsoidInverseHeightDifference = 0.0;
  }
 
  // Intersects a wedge for the min and max longitude.
  if (renderHasLongitude) {
    shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE"] = true;
    shaderDefines["ELLIPSOID_INTERSECTION_INDEX_LONGITUDE"] = intersectionCount;
 
    Iif (renderIsLongitudeRangeUnderHalf) {
      shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_RANGE_UNDER_HALF"] =
        true;
      intersectionCount += 1;
    } else if (renderIsLongitudeRangeOverHalf) {
      shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_RANGE_OVER_HALF"] =
        true;
      intersectionCount += 2;
    } else Eif (renderIsLongitudeRangeZero) {
      shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_RANGE_EQUAL_ZERO"] =
        true;
      intersectionCount += 2;
    }
 
    shaderUniforms.ellipsoidRenderLongitudeMinMax = Cartesian2.fromElements(
      renderMinBounds.x,
      renderMaxBounds.x,
      shaderUniforms.ellipsoidRenderLongitudeMinMax,
    );
  }
 
  if (shapeHasLongitude) {
    shaderDefines["ELLIPSOID_HAS_SHAPE_BOUNDS_LONGITUDE"] = true;
 
    const uvShapeMinLongitude =
      (shapeMinBounds.x - DefaultMinBounds.x) / defaultLongitudeRange;
    const uvShapeMaxLongitude =
      (shapeMaxBounds.x - DefaultMinBounds.x) / defaultLongitudeRange;
    const uvLongitudeRangeZero =
      1.0 - shapeLongitudeRange / defaultLongitudeRange;
    // Translate the origin of UV angles (in [0,1]) to the center of the unoccupied space
    const uvLongitudeRangeOrigin =
      (uvShapeMaxLongitude + 0.5 * uvLongitudeRangeZero) % 1.0;
    shaderUniforms.ellipsoidShapeUvLongitudeRangeOrigin =
      uvLongitudeRangeOrigin;
 
    const shapeIsLongitudeReversed = shapeMaxBounds.x < shapeMinBounds.x;
    Iif (shapeIsLongitudeReversed) {
      shaderDefines["ELLIPSOID_HAS_SHAPE_BOUNDS_LONGITUDE_MIN_MAX_REVERSED"] =
        true;
    }
 
    Iif (shapeLongitudeRange <= epsilonLongitude) {
      shaderUniforms.ellipsoidLocalToShapeUvLongitude = Cartesian2.fromElements(
        0.0,
        1.0,
        shaderUniforms.ellipsoidLocalToShapeUvLongitude,
      );
    } else {
      const scale = defaultLongitudeRange / shapeLongitudeRange;
      const shiftedMinLongitude = uvShapeMinLongitude - uvLongitudeRangeOrigin;
      const offset =
        -scale * (shiftedMinLongitude - Math.floor(shiftedMinLongitude));
      shaderUniforms.ellipsoidLocalToShapeUvLongitude = Cartesian2.fromElements(
        scale,
        offset,
        shaderUniforms.ellipsoidLocalToShapeUvLongitude,
      );
    }
  }
 
  if (renderHasLongitude) {
    const renderIsMinLongitudeDiscontinuity = CesiumMath.equalsEpsilon(
      renderMinBounds.x,
      DefaultMinBounds.x,
      undefined,
      epsilonLongitudeDiscontinuity,
    );
    const renderIsMaxLongitudeDiscontinuity = CesiumMath.equalsEpsilon(
      renderMaxBounds.x,
      DefaultMaxBounds.x,
      undefined,
      epsilonLongitudeDiscontinuity,
    );
 
    Iif (renderIsMinLongitudeDiscontinuity) {
      shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_MIN_DISCONTINUITY"] =
        true;
    }
    Iif (renderIsMaxLongitudeDiscontinuity) {
      shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LONGITUDE_MAX_DISCONTINUITY"] =
        true;
    }
    const uvShapeMinLongitude =
      (shapeMinBounds.x - DefaultMinBounds.x) / defaultLongitudeRange;
    const uvShapeMaxLongitude =
      (shapeMaxBounds.x - DefaultMinBounds.x) / defaultLongitudeRange;
 
    const uvRenderMaxLongitude =
      (renderMaxBounds.x - DefaultMinBounds.x) / defaultLongitudeRange;
    const uvRenderLongitudeRangeZero =
      1.0 - renderLongitudeRange / defaultLongitudeRange;
    const uvRenderLongitudeRangeZeroMid =
      (uvRenderMaxLongitude + 0.5 * uvRenderLongitudeRangeZero) % 1.0;
 
    shaderUniforms.ellipsoidShapeUvLongitudeMinMaxMid = Cartesian3.fromElements(
      uvShapeMinLongitude,
      uvShapeMaxLongitude,
      uvRenderLongitudeRangeZeroMid,
      shaderUniforms.ellipsoidShapeUvLongitudeMinMaxMid,
    );
  }
 
  if (renderHasLatitude) {
    // Intersects a cone for min latitude
    Eif (renderHasLatitudeMin) {
      shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MIN"] = true;
      shaderDefines["ELLIPSOID_INTERSECTION_INDEX_LATITUDE_MIN"] =
        intersectionCount;
 
      Iif (renderIsLatitudeMinUnderHalf) {
        shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MIN_UNDER_HALF"] =
          true;
        intersectionCount += 1;
      } else if (renderIsLatitudeMinHalf) {
        shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MIN_EQUAL_HALF"] =
          true;
        intersectionCount += 1;
      } else Eif (renderIsLatitudeMinOverHalf) {
        shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MIN_OVER_HALF"] =
          true;
        intersectionCount += 2;
      }
    }
 
    // Intersects a cone for max latitude
    Eif (renderHasLatitudeMax) {
      shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MAX"] = true;
      shaderDefines["ELLIPSOID_INTERSECTION_INDEX_LATITUDE_MAX"] =
        intersectionCount;
 
      Iif (renderIsLatitudeMaxUnderHalf) {
        shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MAX_UNDER_HALF"] =
          true;
        intersectionCount += 2;
      } else Iif (renderIsLatitudeMaxHalf) {
        shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MAX_EQUAL_HALF"] =
          true;
        intersectionCount += 1;
      } else Eif (renderIsLatitudeMaxOverHalf) {
        shaderDefines["ELLIPSOID_HAS_RENDER_BOUNDS_LATITUDE_MAX_OVER_HALF"] =
          true;
        intersectionCount += 1;
      }
    }
 
    shaderUniforms.ellipsoidRenderLatitudeSinMinMax = Cartesian2.fromElements(
      Math.sin(renderMinBounds.y),
      Math.sin(renderMaxBounds.y),
      shaderUniforms.ellipsoidRenderLatitudeSinMinMax,
    );
  }
 
  if (shapeHasLatitude) {
    shaderDefines["ELLIPSOID_HAS_SHAPE_BOUNDS_LATITUDE"] = true;
 
    Iif (shapeLatitudeRange < epsilonLatitude) {
      shaderUniforms.ellipsoidLocalToShapeUvLatitude = Cartesian2.fromElements(
        0.0,
        1.0,
        shaderUniforms.ellipsoidLocalToShapeUvLatitude,
      );
    } else {
      const defaultLatitudeRange = DefaultMaxBounds.y - DefaultMinBounds.y;
      const scale = defaultLatitudeRange / shapeLatitudeRange;
      const offset =
        (DefaultMinBounds.y - shapeMinBounds.y) / shapeLatitudeRange;
      shaderUniforms.ellipsoidLocalToShapeUvLatitude = Cartesian2.fromElements(
        scale,
        offset,
        shaderUniforms.ellipsoidLocalToShapeUvLatitude,
      );
    }
  }
 
  this._shaderMaximumIntersectionsLength = intersectionCount;
 
  return true;
};
 
const scratchCameraPositionCartographic = new Cartographic();
const surfacePositionScratch = new Cartesian3();
const enuTransformScratch = new Matrix4();
const enuRotationScratch = new Matrix3();
/**
 * Update any view-dependent transforms.
 * @private
 * @param {FrameState} frameState The frame state.
 */
VoxelEllipsoidShape.prototype.updateViewTransforms = function (frameState) {
  const shaderUniforms = this._shaderUniforms;
  const ellipsoid = this._ellipsoid;
  // TODO: incorporate modelMatrix or shapeTransform here?
  const cameraWC = frameState.camera.positionWC;
  const cameraPositionCartographic = ellipsoid.cartesianToCartographic(
    cameraWC,
    scratchCameraPositionCartographic,
  );
  Cartesian3.fromElements(
    cameraPositionCartographic.longitude,
    cameraPositionCartographic.latitude,
    cameraPositionCartographic.height,
    shaderUniforms.cameraPositionCartographic,
  );
 
  // TODO: incorporate modelMatrix here?
  const surfacePosition = Cartesian3.fromRadians(
    cameraPositionCartographic.longitude,
    cameraPositionCartographic.latitude,
    0.0,
    ellipsoid,
    surfacePositionScratch,
  );
 
  shaderUniforms.ellipsoidCurvatureAtLatitude = ellipsoid.getLocalCurvature(
    surfacePosition,
    shaderUniforms.ellipsoidCurvatureAtLatitude,
  );
 
  const enuToWorld = Transforms.eastNorthUpToFixedFrame(
    surfacePosition,
    ellipsoid,
    enuTransformScratch,
  );
  const rotateEnuToWorld = Matrix4.getRotation(enuToWorld, enuRotationScratch);
  const rotateWorldToView = frameState.context.uniformState.viewRotation;
  const rotateEnuToView = Matrix3.multiply(
    rotateWorldToView,
    rotateEnuToWorld,
    enuRotationScratch,
  );
  // Inverse is the transpose since it's a pure rotation.
  shaderUniforms.ellipsoidEcToEastNorthUp = Matrix3.transpose(
    rotateEnuToView,
    shaderUniforms.ellipsoidEcToEastNorthUp,
  );
};
 
const scratchRectangle = new Rectangle();
 
/**
 * Computes an oriented bounding box for a specified tile.
 * @private
 * @param {number} tileLevel The tile's level.
 * @param {number} tileX The tile's x coordinate.
 * @param {number} tileY The tile's y coordinate.
 * @param {number} tileZ The tile's z coordinate.
 * @param {OrientedBoundingBox} result The oriented bounding box that will be set to enclose the specified tile
 * @returns {OrientedBoundingBox} The oriented bounding box.
 */
VoxelEllipsoidShape.prototype.computeOrientedBoundingBoxForTile = function (
  tileLevel,
  tileX,
  tileY,
  tileZ,
  result,
) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.number("tileLevel", tileLevel);
  Check.typeOf.number("tileX", tileX);
  Check.typeOf.number("tileY", tileY);
  Check.typeOf.number("tileZ", tileZ);
  Check.typeOf.object("result", result);
  //>>includeEnd('debug');
 
  const sizeAtLevel = 1.0 / Math.pow(2.0, tileLevel);
  const minLongitudeLerp = tileX * sizeAtLevel;
  const maxLongitudeLerp = (tileX + 1) * sizeAtLevel;
  const minLatitudeLerp = tileY * sizeAtLevel;
  const maxLatitudeLerp = (tileY + 1) * sizeAtLevel;
  const minHeightLerp = tileZ * sizeAtLevel;
  const maxHeightLerp = (tileZ + 1) * sizeAtLevel;
 
  const rectangle = Rectangle.subsection(
    this._rectangle,
    minLongitudeLerp,
    minLatitudeLerp,
    maxLongitudeLerp,
    maxLatitudeLerp,
    scratchRectangle,
  );
 
  const minHeight = CesiumMath.lerp(
    this._minimumHeight,
    this._maximumHeight,
    minHeightLerp,
  );
 
  const maxHeight = CesiumMath.lerp(
    this._minimumHeight,
    this._maximumHeight,
    maxHeightLerp,
  );
 
  return getEllipsoidChunkObb(
    rectangle,
    minHeight,
    maxHeight,
    this._ellipsoid,
    this._translation,
    this._rotation,
    result,
  );
};
 
const scratchQuadrantPosition = new Cartesian2();
const scratchInverseRadii = new Cartesian2();
const scratchEllipseTrigs = new Cartesian2();
const scratchEllipseGuess = new Cartesian2();
const scratchEvolute = new Cartesian2();
const scratchQ = new Cartesian2();
 
/**
 * Find the nearest point on an ellipse and its radius.
 * @param {Cartesian2} position
 * @param {Cartesian2} radii
 * @param {Cartesian2} evoluteScale
 * @param {Cartesian3} result The Cartesian3 to store the result in. .x and .y components contain the nearest point on the ellipse, .z contains the local radius of curvature.
 * @returns {Cartesian3} The nearest point on the ellipse and its radius.
 * @private
 */
function nearestPointAndRadiusOnEllipse(position, radii, evoluteScale, result) {
  // Map to the first quadrant
  const p = Cartesian2.abs(position, scratchQuadrantPosition);
  const inverseRadii = Cartesian2.fromElements(
    1.0 / radii.x,
    1.0 / radii.y,
    scratchInverseRadii,
  );
  // We describe the ellipse parametrically: v = radii * vec2(cos(t), sin(t))
  // but store the cos and sin of t in a vec2 for efficiency.
  // Initial guess: t = pi/4
  let tTrigs = Cartesian2.fromElements(
    Math.SQRT1_2,
    Math.SQRT1_2,
    scratchEllipseTrigs,
  );
  // TODO: too much duplication. Move v and evolute declarations inside loop?
  // Initial guess of point on ellipsoid
  let v = Cartesian2.multiplyComponents(radii, tTrigs, scratchEllipseGuess);
  // Center of curvature of the ellipse at v
  let evolute = Cartesian2.fromElements(
    evoluteScale.x * tTrigs.x * tTrigs.x * tTrigs.x,
    evoluteScale.y * tTrigs.y * tTrigs.y * tTrigs.y,
    scratchEvolute,
  );
  for (let i = 0; i < 3; ++i) {
    // Find the (approximate) intersection of p - evolute with the ellipsoid.
    const distance = Cartesian2.magnitude(
      Cartesian2.subtract(v, evolute, scratchQ),
    );
    const direction = Cartesian2.normalize(
      Cartesian2.subtract(p, evolute, scratchQ),
      scratchQ,
    );
    const q = Cartesian2.multiplyByScalar(direction, distance, scratchQ);
    // Update the estimate of t
    tTrigs = Cartesian2.multiplyComponents(
      Cartesian2.add(q, evolute, scratchEllipseTrigs),
      inverseRadii,
      scratchEllipseTrigs,
    );
    tTrigs = Cartesian2.normalize(
      Cartesian2.clamp(
        tTrigs,
        Cartesian2.ZERO,
        Cartesian2.ONE,
        scratchEllipseTrigs,
      ),
      scratchEllipseTrigs,
    );
    v = Cartesian2.multiplyComponents(radii, tTrigs, scratchEllipseGuess);
    evolute = Cartesian2.fromElements(
      evoluteScale.x * tTrigs.x * tTrigs.x * tTrigs.x,
      evoluteScale.y * tTrigs.y * tTrigs.y * tTrigs.y,
      scratchEvolute,
    );
  }
 
  // Map back to the original quadrant
  return Cartesian3.fromElements(
    Math.sign(position.x) * v.x,
    Math.sign(position.y) * v.y,
    Cartesian2.magnitude(Cartesian2.subtract(v, evolute, scratchQ)),
    result,
  );
}
 
const scratchEllipseRadii = new Cartesian2();
const scratchEllipsePosition = new Cartesian2();
const scratchSurfacePointAndRadius = new Cartesian3();
const scratchNormal2d = new Cartesian2();
/**
 * Convert a UV coordinate to the shape's UV space.
 * @private
 * @param {Cartesian3} positionLocal The local position to convert.
 * @param {Cartesian3} result The Cartesian3 to store the result in.
 * @returns {Cartesian3} The converted UV coordinate.
 */
VoxelEllipsoidShape.prototype.convertLocalToShapeUvSpace = function (
  positionLocal,
  result,
) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("positionLocal", positionLocal);
  Check.typeOf.object("result", result);
  //>>includeEnd('debug');
 
  let longitude = Math.atan2(positionLocal.y, positionLocal.x);
 
  const {
    ellipsoidRadii,
    evoluteScale,
    ellipsoidInverseRadiiSquared,
    ellipsoidInverseHeightDifference,
    ellipsoidShapeUvLongitudeRangeOrigin,
    ellipsoidLocalToShapeUvLongitude,
    ellipsoidLocalToShapeUvLatitude,
  } = this._shaderUniforms;
 
  const distanceFromZAxis = Math.hypot(positionLocal.x, positionLocal.y);
  const posEllipse = Cartesian2.fromElements(
    distanceFromZAxis,
    positionLocal.z,
    scratchEllipsePosition,
  );
  const surfacePointAndRadius = nearestPointAndRadiusOnEllipse(
    posEllipse,
    Cartesian2.fromElements(
      ellipsoidRadii.x,
      ellipsoidRadii.z,
      scratchEllipseRadii,
    ),
    evoluteScale,
    scratchSurfacePointAndRadius,
  );
 
  const normal2d = Cartesian2.normalize(
    Cartesian2.fromElements(
      surfacePointAndRadius.x * ellipsoidInverseRadiiSquared.x,
      surfacePointAndRadius.y * ellipsoidInverseRadiiSquared.z,
      scratchNormal2d,
    ),
    scratchNormal2d,
  );
  let latitude = Math.atan2(normal2d.y, normal2d.x);
 
  const heightSign =
    Cartesian2.magnitude(posEllipse) <
    Cartesian2.magnitude(surfacePointAndRadius)
      ? -1.0
      : 1.0;
  const heightVector = Cartesian2.subtract(
    posEllipse,
    surfacePointAndRadius,
    scratchEllipsePosition,
  );
  let height = heightSign * Cartesian2.magnitude(heightVector);
 
  const {
    ELLIPSOID_HAS_SHAPE_BOUNDS_LONGITUDE,
    ELLIPSOID_HAS_SHAPE_BOUNDS_LATITUDE,
  } = this._shaderDefines;
 
  longitude = (longitude + Math.PI) / (2.0 * Math.PI);
  Eif (defined(ELLIPSOID_HAS_SHAPE_BOUNDS_LONGITUDE)) {
    longitude -= ellipsoidShapeUvLongitudeRangeOrigin;
    longitude = longitude - Math.floor(longitude);
    // Scale and shift so [0, 1] covers the occupied space.
    longitude =
      longitude * ellipsoidLocalToShapeUvLongitude.x +
      ellipsoidLocalToShapeUvLongitude.y;
  }
 
  latitude = (latitude + Math.PI / 2.0) / Math.PI;
  Eif (defined(ELLIPSOID_HAS_SHAPE_BOUNDS_LATITUDE)) {
    // Scale and shift so [0, 1] covers the occupied space.
    latitude =
      latitude * ellipsoidLocalToShapeUvLatitude.x +
      ellipsoidLocalToShapeUvLatitude.y;
  }
 
  height = 1.0 + height * ellipsoidInverseHeightDifference;
 
  return Cartesian3.fromElements(longitude, latitude, height, result);
};
 
const sampleSizeScratch = new Cartesian3();
const scratchTileMinBounds = new Cartesian3();
const scratchTileMaxBounds = new Cartesian3();
 
/**
 * Computes an oriented bounding box for a specified sample within a specified tile.
 * @private
 * @param {SpatialNode} spatialNode The spatial node containing the sample
 * @param {Cartesian3} tileDimensions The size of the tile in number of samples, before padding
 * @param {Cartesian3} tileUv The sample coordinate within the tile
 * @param {OrientedBoundingBox} result The oriented bounding box that will be set to enclose the specified sample
 * @returns {OrientedBoundingBox} The oriented bounding box.
 */
VoxelEllipsoidShape.prototype.computeOrientedBoundingBoxForSample = function (
  spatialNode,
  tileDimensions,
  tileUv,
  result,
) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("spatialNode", spatialNode);
  Check.typeOf.object("tileDimensions", tileDimensions);
  Check.typeOf.object("tileUv", tileUv);
  Check.typeOf.object("result", result);
  //>>includeEnd('debug');
 
  const tileSizeAtLevel = 1.0 / Math.pow(2.0, spatialNode.level);
  const sampleSize = Cartesian3.divideComponents(
    Cartesian3.ONE,
    tileDimensions,
    sampleSizeScratch,
  );
  const sampleSizeAtLevel = Cartesian3.multiplyByScalar(
    sampleSize,
    tileSizeAtLevel,
    sampleSizeScratch,
  );
 
  const minLerp = Cartesian3.multiplyByScalar(
    Cartesian3.fromElements(
      spatialNode.x + tileUv.x,
      spatialNode.y + tileUv.y,
      spatialNode.z + tileUv.z,
      scratchTileMinBounds,
    ),
    tileSizeAtLevel,
    scratchTileMinBounds,
  );
  const maxLerp = Cartesian3.add(
    minLerp,
    sampleSizeAtLevel,
    scratchTileMaxBounds,
  );
 
  const rectangle = Rectangle.subsection(
    this._rectangle,
    minLerp.x,
    minLerp.y,
    maxLerp.x,
    maxLerp.y,
    scratchRectangle,
  );
  const minHeight = CesiumMath.lerp(
    this._minimumHeight,
    this._maximumHeight,
    minLerp.z,
  );
  const maxHeight = CesiumMath.lerp(
    this._minimumHeight,
    this._maximumHeight,
    maxLerp.z,
  );
 
  return getEllipsoidChunkObb(
    rectangle,
    minHeight,
    maxHeight,
    this._ellipsoid,
    this._translation,
    this._rotation,
    result,
  );
};
 
/**
 * Computes an {@link OrientedBoundingBox} for a subregion of the shape.
 *
 * @function
 *
 * @param {Rectangle} rectangle The rectangle.
 * @param {number} minHeight The minimumZ.
 * @param {number} maxHeight The maximumZ.
 * @param {Ellipsoid} ellipsoid The ellipsoid.
 * @param {Cartesian3} translation The translation applied to the shape
 * @param {Matrix3} rotation The rotation applied to the shape
 * @param {OrientedBoundingBox} result The object onto which to store the result.
 * @returns {OrientedBoundingBox} The oriented bounding box that contains this subregion.
 *
 * @private
 */
function getEllipsoidChunkObb(
  rectangle,
  minHeight,
  maxHeight,
  ellipsoid,
  translation,
  rotation,
  result,
) {
  result = OrientedBoundingBox.fromRectangle(
    rectangle,
    minHeight,
    maxHeight,
    ellipsoid,
    result,
  );
  result.center = Cartesian3.add(result.center, translation, result.center);
  result.halfAxes = Matrix3.multiply(
    result.halfAxes,
    rotation,
    result.halfAxes,
  );
  return result;
}
 
/**
 * Defines the minimum bounds of the shape. Corresponds to minimum longitude, latitude, height.
 * @private
 * @type {Cartesian3}
 * @constant
 * @readonly
 */
VoxelEllipsoidShape.DefaultMinBounds = Object.freeze(
  new Cartesian3(
    -CesiumMath.PI,
    -CesiumMath.PI_OVER_TWO,
    -Ellipsoid.WGS84.minimumRadius,
  ),
);
 
/**
 * Defines the maximum bounds of the shape. Corresponds to maximum longitude, latitude, height.
 * @private
 * @type {Cartesian3}
 * @constant
 * @readonly
 */
VoxelEllipsoidShape.DefaultMaxBounds = Object.freeze(
  new Cartesian3(
    CesiumMath.PI,
    CesiumMath.PI_OVER_TWO,
    10.0 * Ellipsoid.WGS84.maximumRadius,
  ),
);
 
export default VoxelEllipsoidShape;