All files / engine/Source/Core GroundPolylineGeometry.js

96.85% Statements 585/604
84.61% Branches 121/143
100% Functions 19/19
96.83% Lines 581/600

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 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660                                                    1x 1x   1x 1x                       1x 1x                                                             78x 78x     78x 1x   77x                             77x   77x               77x               77x             77x   77x     77x 77x     77x     1x                   1x                                           1x       3x 3x 6x 3x 3x       3x 3x     1x 1x 1x   40x 40x 40x   40x 40x   40x 40x     1x 1x 1x 1x                           37x 28x       9x 6x 3x 3x     9x 9x 3x       6x               6x 6x 6x 6x 6x 6x   400x       400x           400x             400x 400x 400x 400x 400x   400x 400x       1x   1074x 1074x 1074x                               1x   7x 6x     5x   5x 5x   5x   5x 13x 13x 13x     5x 5x 5x   5x 5x   5x 5x   5x                   1x   6x     5x 5x 5x   5x 13x 13x     5x 5x 5x   5x 5x   5x 5x   5x 4x         5x 5x 5x 5x 5x 5x 5x   5x       1059x 1059x 1059x       40x     40x 40x 40x 40x     1x 1x 1x 1x 1x 1x               20x     20x           20x     20x             7x 7x 7x       13x 13x     13x 13x 9x     13x     1x   1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 21x 21x 21x 21x 21x 21x       21x 21x         21x 21x   21x 12x                   21x       21x 21x 32x 32x 32x           32x         4x 4x                                                   32x     21x 3x 3x 3x           3x         1x 1x                                                     21x   21x 21x 58x       58x 58x     21x       21x   21x 1x               20x 20x 20x 20x   20x 20x 20x 20x 20x     20x 20x   20x 20x           20x 20x           20x   20x 3x               17x                 20x 20x 20x 20x 20x   20x                             20x 14x 14x 14x 14x 14x   14x               14x 14x 14x 14x 14x 14x   14x                               20x 20x   20x           20x   20x 3x 3x           3x             3x               17x                 20x 20x 20x 20x 20x 20x   20x 3x                         3x 3x 9x 9x 9x   3x 3x     20x                           1x 1x 1x   440x   440x 440x 5x   5x     5x         5x 5x         5x   435x     1x 1x 1x               865x         865x 865x   865x 865x               865x       1x 1x         1x           865x 865x       865x         865x 865x 865x 1x   865x     1x 1x                   874x         874x   874x 874x         874x   874x 874x         874x     1x   1748x 1748x 1748x   1748x 20x 20x 20x 1728x     18x 18x 18x               436x 436x 436x     4x 4x 4x 432x     4x 4x 4x   428x     1x 1x   1x 1x 1x 1x 1x 1x   1x       1x   1x 1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x   1x     1x                                                                           1x                                 20x     20x 20x 20x 20x     20x     20x   20x 20x 20x 20x     20x                 20x 19x 19x 19x 19x         20x 20x   20x 20x 20x 20x   20x 20x   20x 19x 19x   436x 436x 436x 436x   436x       436x       436x       436x         20x 20x         20x   20x 20x 437x       437x         437x 437x         20x 20x 20x 20x 20x 20x   20x         20x 20x           20x 3x         3x   1x             20x 20x     20x   20x 437x 437x 437x         437x 3x           437x         437x 437x           437x     437x 437x 437x 437x           437x 436x 436x 436x 436x 436x   436x 436x 436x             428x             428x             8x   4x             4x     4x     4x     4x             4x     4x     4x                           437x   437x       437x         437x   437x 437x 437x 437x   437x         437x   437x 437x 437x         437x   437x 437x     437x       437x 437x 437x 436x   436x       436x               436x 436x 436x 436x   436x 436x     437x 3496x 3496x 3496x       3496x   3496x     3496x 3496x   3496x 3496x   3496x         3496x   3496x         3496x     3496x           3496x 3496x 80x   3496x       3496x 3488x 3488x 3488x 3488x   3488x 3488x 3488x 3488x   3488x 3488x 3488x 3488x   3488x     3488x 3488x 76x   3488x         437x 437x 437x 437x   437x       437x       437x 437x     437x 437x   437x               437x                   437x         437x         437x 437x 437x     437x 437x   437x 437x 437x 437x   437x         437x         437x 437x 437x   437x 437x   437x         437x         437x 437x   437x 437x   437x 437x 437x   437x 437x     20x 20x 20x 437x 15732x   437x 437x     20x 20x           20x           20x     20x   20x                                               20x 19x 19x 19x 19x               20x               157x                                       1x    
import ApproximateTerrainHeights from "./ApproximateTerrainHeights.js";
import ArcType from "./ArcType.js";
import arrayRemoveDuplicates from "./arrayRemoveDuplicates.js";
import BoundingSphere from "./BoundingSphere.js";
import Cartesian3 from "./Cartesian3.js";
import Cartographic from "./Cartographic.js";
import Check from "./Check.js";
import ComponentDatatype from "./ComponentDatatype.js";
import Frozen from "./Frozen.js";
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
import Ellipsoid from "./Ellipsoid.js";
import EllipsoidGeodesic from "./EllipsoidGeodesic.js";
import EllipsoidRhumbLine from "./EllipsoidRhumbLine.js";
import EncodedCartesian3 from "./EncodedCartesian3.js";
import GeographicProjection from "./GeographicProjection.js";
import Geometry from "./Geometry.js";
import GeometryAttribute from "./GeometryAttribute.js";
import IntersectionTests from "./IntersectionTests.js";
import CesiumMath from "./Math.js";
import Matrix3 from "./Matrix3.js";
import Plane from "./Plane.js";
import Quaternion from "./Quaternion.js";
import Rectangle from "./Rectangle.js";
import WebMercatorProjection from "./WebMercatorProjection.js";
 
const PROJECTIONS = [GeographicProjection, WebMercatorProjection];
const PROJECTION_COUNT = PROJECTIONS.length;
 
const MITER_BREAK_SMALL = Math.cos(CesiumMath.toRadians(30.0));
const MITER_BREAK_LARGE = Math.cos(CesiumMath.toRadians(150.0));
 
// Initial heights for constructing the wall.
// Keeping WALL_INITIAL_MIN_HEIGHT near the ellipsoid surface helps
// prevent precision problems with planes in the shader.
// Putting the start point of a plane at ApproximateTerrainHeights._defaultMinTerrainHeight,
// which is a highly conservative bound, usually puts the plane origin several thousands
// of meters away from the actual terrain, causing floating point problems when checking
// fragments on terrain against the plane.
// Ellipsoid height is generally much closer.
// The initial max height is arbitrary.
// Both heights are corrected using ApproximateTerrainHeights for computing the actual volume geometry.
const WALL_INITIAL_MIN_HEIGHT = 0.0;
const WALL_INITIAL_MAX_HEIGHT = 1000.0;
 
/**
 * A description of a polyline on terrain or 3D Tiles. Only to be used with {@link GroundPolylinePrimitive}.
 *
 * @alias GroundPolylineGeometry
 * @constructor
 *
 * @param {object} options Options with the following properties:
 * @param {Cartesian3[]} options.positions An array of {@link Cartesian3} defining the polyline's points. Heights above the ellipsoid will be ignored.
 * @param {number} [options.width=1.0] The screen space width in pixels.
 * @param {number} [options.granularity=9999.0] The distance interval in meters used for interpolating options.points. Defaults to 9999.0 meters. Zero indicates no interpolation.
 * @param {boolean} [options.loop=false] Whether during geometry creation a line segment will be added between the last and first line positions to make this Polyline a loop.
 * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of line the polyline segments must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
 *
 * @exception {DeveloperError} At least two positions are required.
 *
 * @see GroundPolylinePrimitive
 *
 * @example
 * const positions = Cesium.Cartesian3.fromDegreesArray([
 *   -112.1340164450331, 36.05494287836128,
 *   -112.08821010582645, 36.097804071380715,
 *   -112.13296079730024, 36.168769146801104
 * ]);
 *
 * const geometry = new Cesium.GroundPolylineGeometry({
 *   positions : positions
 * });
 */
function GroundPolylineGeometry(options) {
  options = options ?? Frozen.EMPTY_OBJECT;
  const positions = options.positions;
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(positions) || positions.length < 2) {
    throw new DeveloperError("At least two positions are required.");
  }
  Iif (
    defined(options.arcType) &&
    options.arcType !== ArcType.GEODESIC &&
    options.arcType !== ArcType.RHUMB
  ) {
    throw new DeveloperError(
      "Valid options for arcType are ArcType.GEODESIC and ArcType.RHUMB.",
    );
  }
  //>>includeEnd('debug');
 
  /**
   * The screen space width in pixels.
   * @type {number}
   */
  this.width = options.width ?? 1.0; // Doesn't get packed, not necessary for computing geometry.
 
  this._positions = positions;
 
  /**
   * The distance interval used for interpolating options.points. Zero indicates no interpolation.
   * Default of 9999.0 allows centimeter accuracy with 32 bit floating point.
   * @type {boolean}
   * @default 9999.0
   */
  this.granularity = options.granularity ?? 9999.0;
 
  /**
   * Whether during geometry creation a line segment will be added between the last and first line positions to make this Polyline a loop.
   * If the geometry has two positions this parameter will be ignored.
   * @type {boolean}
   * @default false
   */
  this.loop = options.loop ?? false;
 
  /**
   * The type of path the polyline must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
   * @type {ArcType}
   * @default ArcType.GEODESIC
   */
  this.arcType = options.arcType ?? ArcType.GEODESIC;
 
  this._ellipsoid = Ellipsoid.default;
 
  // MapProjections can't be packed, so store the index to a known MapProjection.
  this._projectionIndex = 0;
  this._workerName = "createGroundPolylineGeometry";
 
  // Used by GroundPolylinePrimitive to signal worker that scenemode is 3D only.
  this._scene3DOnly = false;
}
 
Object.defineProperties(GroundPolylineGeometry.prototype, {
  /**
   * The number of elements used to pack the object into an array.
   * @memberof GroundPolylineGeometry.prototype
   * @type {number}
   * @readonly
   * @private
   */
  packedLength: {
    get: function () {
      return (
        1.0 +
        this._positions.length * 3 +
        1.0 +
        1.0 +
        1.0 +
        Ellipsoid.packedLength +
        1.0 +
        1.0
      );
    },
  },
});
 
/**
 * Set the GroundPolylineGeometry's projection and ellipsoid.
 * Used by GroundPolylinePrimitive to signal scene information to the geometry for generating 2D attributes.
 *
 * @param {GroundPolylineGeometry} groundPolylineGeometry GroundPolylinGeometry describing a polyline on terrain or 3D Tiles.
 * @param {Projection} mapProjection A MapProjection used for projecting cartographic coordinates to 2D.
 * @private
 */
GroundPolylineGeometry.setProjectionAndEllipsoid = function (
  groundPolylineGeometry,
  mapProjection,
) {
  let projectionIndex = 0;
  for (let i = 0; i < PROJECTION_COUNT; i++) {
    if (mapProjection instanceof PROJECTIONS[i]) {
      projectionIndex = i;
      break;
    }
  }
 
  groundPolylineGeometry._projectionIndex = projectionIndex;
  groundPolylineGeometry._ellipsoid = mapProjection.ellipsoid;
};
 
const cart3Scratch1 = new Cartesian3();
const cart3Scratch2 = new Cartesian3();
const cart3Scratch3 = new Cartesian3();
function computeRightNormal(start, end, maxHeight, ellipsoid, result) {
  const startBottom = getPosition(ellipsoid, start, 0.0, cart3Scratch1);
  const startTop = getPosition(ellipsoid, start, maxHeight, cart3Scratch2);
  const endBottom = getPosition(ellipsoid, end, 0.0, cart3Scratch3);
 
  const up = direction(startTop, startBottom, cart3Scratch2);
  const forward = direction(endBottom, startBottom, cart3Scratch3);
 
  Cartesian3.cross(forward, up, result);
  return Cartesian3.normalize(result, result);
}
 
const interpolatedCartographicScratch = new Cartographic();
const interpolatedBottomScratch = new Cartesian3();
const interpolatedTopScratch = new Cartesian3();
const interpolatedNormalScratch = new Cartesian3();
function interpolateSegment(
  start,
  end,
  minHeight,
  maxHeight,
  granularity,
  arcType,
  ellipsoid,
  normalsArray,
  bottomPositionsArray,
  topPositionsArray,
  cartographicsArray,
) {
  if (granularity === 0.0) {
    return;
  }
 
  let ellipsoidLine;
  if (arcType === ArcType.GEODESIC) {
    ellipsoidLine = new EllipsoidGeodesic(start, end, ellipsoid);
  } else Eif (arcType === ArcType.RHUMB) {
    ellipsoidLine = new EllipsoidRhumbLine(start, end, ellipsoid);
  }
 
  const surfaceDistance = ellipsoidLine.surfaceDistance;
  if (surfaceDistance < granularity) {
    return;
  }
 
  // Compute rightwards normal applicable at all interpolated points
  const interpolatedNormal = computeRightNormal(
    start,
    end,
    maxHeight,
    ellipsoid,
    interpolatedNormalScratch,
  );
 
  const segments = Math.ceil(surfaceDistance / granularity);
  const interpointDistance = surfaceDistance / segments;
  let distanceFromStart = interpointDistance;
  const pointsToAdd = segments - 1;
  let packIndex = normalsArray.length;
  for (let i = 0; i < pointsToAdd; i++) {
    const interpolatedCartographic =
      ellipsoidLine.interpolateUsingSurfaceDistance(
        distanceFromStart,
        interpolatedCartographicScratch,
      );
    const interpolatedBottom = getPosition(
      ellipsoid,
      interpolatedCartographic,
      minHeight,
      interpolatedBottomScratch,
    );
    const interpolatedTop = getPosition(
      ellipsoid,
      interpolatedCartographic,
      maxHeight,
      interpolatedTopScratch,
    );
 
    Cartesian3.pack(interpolatedNormal, normalsArray, packIndex);
    Cartesian3.pack(interpolatedBottom, bottomPositionsArray, packIndex);
    Cartesian3.pack(interpolatedTop, topPositionsArray, packIndex);
    cartographicsArray.push(interpolatedCartographic.latitude);
    cartographicsArray.push(interpolatedCartographic.longitude);
 
    packIndex += 3;
    distanceFromStart += interpointDistance;
  }
}
 
const heightlessCartographicScratch = new Cartographic();
function getPosition(ellipsoid, cartographic, height, result) {
  Cartographic.clone(cartographic, heightlessCartographicScratch);
  heightlessCartographicScratch.height = height;
  return Cartographic.toCartesian(
    heightlessCartographicScratch,
    ellipsoid,
    result,
  );
}
 
/**
 * Stores the provided instance into the provided array.
 *
 * @param {PolygonGeometry} 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
 */
GroundPolylineGeometry.pack = function (value, array, startingIndex) {
  //>>includeStart('debug', pragmas.debug);
  Check.typeOf.object("value", value);
  Check.defined("array", array);
  //>>includeEnd('debug');
 
  let index = startingIndex ?? 0;
 
  const positions = value._positions;
  const positionsLength = positions.length;
 
  array[index++] = positionsLength;
 
  for (let i = 0; i < positionsLength; ++i) {
    const cartesian = positions[i];
    Cartesian3.pack(cartesian, array, index);
    index += 3;
  }
 
  array[index++] = value.granularity;
  array[index++] = value.loop ? 1.0 : 0.0;
  array[index++] = value.arcType;
 
  Ellipsoid.pack(value._ellipsoid, array, index);
  index += Ellipsoid.packedLength;
 
  array[index++] = value._projectionIndex;
  array[index++] = value._scene3DOnly ? 1.0 : 0.0;
 
  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 {PolygonGeometry} [result] The object into which to store the result.
 */
GroundPolylineGeometry.unpack = function (array, startingIndex, result) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("array", array);
  //>>includeEnd('debug');
 
  let index = startingIndex ?? 0;
  const positionsLength = array[index++];
  const positions = new Array(positionsLength);
 
  for (let i = 0; i < positionsLength; i++) {
    positions[i] = Cartesian3.unpack(array, index);
    index += 3;
  }
 
  const granularity = array[index++];
  const loop = array[index++] === 1.0;
  const arcType = array[index++];
 
  const ellipsoid = Ellipsoid.unpack(array, index);
  index += Ellipsoid.packedLength;
 
  const projectionIndex = array[index++];
  const scene3DOnly = array[index++] === 1.0;
 
  if (!defined(result)) {
    result = new GroundPolylineGeometry({
      positions: positions,
    });
  }
 
  result._positions = positions;
  result.granularity = granularity;
  result.loop = loop;
  result.arcType = arcType;
  result._ellipsoid = ellipsoid;
  result._projectionIndex = projectionIndex;
  result._scene3DOnly = scene3DOnly;
 
  return result;
};
 
function direction(target, origin, result) {
  Cartesian3.subtract(target, origin, result);
  Cartesian3.normalize(result, result);
  return result;
}
 
function tangentDirection(target, origin, up, result) {
  result = direction(target, origin, result);
 
  // orthogonalize
  result = Cartesian3.cross(result, up, result);
  result = Cartesian3.normalize(result, result);
  result = Cartesian3.cross(up, result, result);
  return result;
}
 
const toPreviousScratch = new Cartesian3();
const toNextScratch = new Cartesian3();
const forwardScratch = new Cartesian3();
const vertexUpScratch = new Cartesian3();
const cosine90 = 0.0;
const cosine180 = -1.0;
function computeVertexMiterNormal(
  previousBottom,
  vertexBottom,
  vertexTop,
  nextBottom,
  result,
) {
  const up = direction(vertexTop, vertexBottom, vertexUpScratch);
 
  // Compute vectors pointing towards neighboring points but tangent to this point on the ellipsoid
  const toPrevious = tangentDirection(
    previousBottom,
    vertexBottom,
    up,
    toPreviousScratch,
  );
  const toNext = tangentDirection(nextBottom, vertexBottom, up, toNextScratch);
 
  // Check if tangents are almost opposite - if so, no need to miter.
  if (
    CesiumMath.equalsEpsilon(
      Cartesian3.dot(toPrevious, toNext),
      cosine180,
      CesiumMath.EPSILON5,
    )
  ) {
    result = Cartesian3.cross(up, toPrevious, result);
    result = Cartesian3.normalize(result, result);
    return result;
  }
 
  // Average directions to previous and to next in the plane of Up
  result = Cartesian3.add(toNext, toPrevious, result);
  result = Cartesian3.normalize(result, result);
 
  // Flip the normal if it isn't pointing roughly bound right (aka if forward is pointing more "backwards")
  const forward = Cartesian3.cross(up, result, forwardScratch);
  if (Cartesian3.dot(toNext, forward) < cosine90) {
    result = Cartesian3.negate(result, result);
  }
 
  return result;
}
 
const XZ_PLANE = Plane.fromPointNormal(Cartesian3.ZERO, Cartesian3.UNIT_Y);
 
const previousBottomScratch = new Cartesian3();
const vertexBottomScratch = new Cartesian3();
const vertexTopScratch = new Cartesian3();
const nextBottomScratch = new Cartesian3();
const vertexNormalScratch = new Cartesian3();
const intersectionScratch = new Cartesian3();
const cartographicScratch0 = new Cartographic();
const cartographicScratch1 = new Cartographic();
const cartographicIntersectionScratch = new Cartographic();
/**
 * Computes shadow volumes for the ground polyline, consisting of its vertices, indices, and a bounding sphere.
 * Vertices are "fat," packing all the data needed in each volume to describe a line on terrain or 3D Tiles.
 * Should not be called independent of {@link GroundPolylinePrimitive}.
 *
 * @param {GroundPolylineGeometry} groundPolylineGeometry
 * @private
 */
GroundPolylineGeometry.createGeometry = function (groundPolylineGeometry) {
  const compute2dAttributes = !groundPolylineGeometry._scene3DOnly;
  let loop = groundPolylineGeometry.loop;
  const ellipsoid = groundPolylineGeometry._ellipsoid;
  const granularity = groundPolylineGeometry.granularity;
  const arcType = groundPolylineGeometry.arcType;
  const projection = new PROJECTIONS[groundPolylineGeometry._projectionIndex](
    ellipsoid,
  );
 
  const minHeight = WALL_INITIAL_MIN_HEIGHT;
  const maxHeight = WALL_INITIAL_MAX_HEIGHT;
 
  let index;
  let i;
 
  const positions = groundPolylineGeometry._positions;
  const positionsLength = positions.length;
 
  if (positionsLength === 2) {
    loop = false;
  }
 
  // Split positions across the IDL and the Prime Meridian as well.
  // Split across prime meridian because very large geometries crossing the Prime Meridian but not the IDL
  // may get split by the plane of IDL + Prime Meridian.
  let p0;
  let p1;
  let c0;
  let c1;
  const rhumbLine = new EllipsoidRhumbLine(undefined, undefined, ellipsoid);
  let intersection;
  let intersectionCartographic;
  let intersectionLongitude;
  const splitPositions = [positions[0]];
  for (i = 0; i < positionsLength - 1; i++) {
    p0 = positions[i];
    p1 = positions[i + 1];
    intersection = IntersectionTests.lineSegmentPlane(
      p0,
      p1,
      XZ_PLANE,
      intersectionScratch,
    );
    if (
      defined(intersection) &&
      !Cartesian3.equalsEpsilon(intersection, p0, CesiumMath.EPSILON7) &&
      !Cartesian3.equalsEpsilon(intersection, p1, CesiumMath.EPSILON7)
    ) {
      if (groundPolylineGeometry.arcType === ArcType.GEODESIC) {
        splitPositions.push(Cartesian3.clone(intersection));
      } else Eif (groundPolylineGeometry.arcType === ArcType.RHUMB) {
        intersectionLongitude = ellipsoid.cartesianToCartographic(
          intersection,
          cartographicScratch0,
        ).longitude;
        c0 = ellipsoid.cartesianToCartographic(p0, cartographicScratch0);
        c1 = ellipsoid.cartesianToCartographic(p1, cartographicScratch1);
        rhumbLine.setEndPoints(c0, c1);
        intersectionCartographic = rhumbLine.findIntersectionWithLongitude(
          intersectionLongitude,
          cartographicIntersectionScratch,
        );
        intersection = ellipsoid.cartographicToCartesian(
          intersectionCartographic,
          intersectionScratch,
        );
        if (
          defined(intersection) &&
          !Cartesian3.equalsEpsilon(intersection, p0, CesiumMath.EPSILON7) &&
          !Cartesian3.equalsEpsilon(intersection, p1, CesiumMath.EPSILON7)
        ) {
          splitPositions.push(Cartesian3.clone(intersection));
        }
      }
    }
    splitPositions.push(p1);
  }
 
  if (loop) {
    p0 = positions[positionsLength - 1];
    p1 = positions[0];
    intersection = IntersectionTests.lineSegmentPlane(
      p0,
      p1,
      XZ_PLANE,
      intersectionScratch,
    );
    if (
      defined(intersection) &&
      !Cartesian3.equalsEpsilon(intersection, p0, CesiumMath.EPSILON7) &&
      !Cartesian3.equalsEpsilon(intersection, p1, CesiumMath.EPSILON7)
    ) {
      if (groundPolylineGeometry.arcType === ArcType.GEODESIC) {
        splitPositions.push(Cartesian3.clone(intersection));
      } else Eif (groundPolylineGeometry.arcType === ArcType.RHUMB) {
        intersectionLongitude = ellipsoid.cartesianToCartographic(
          intersection,
          cartographicScratch0,
        ).longitude;
        c0 = ellipsoid.cartesianToCartographic(p0, cartographicScratch0);
        c1 = ellipsoid.cartesianToCartographic(p1, cartographicScratch1);
        rhumbLine.setEndPoints(c0, c1);
        intersectionCartographic = rhumbLine.findIntersectionWithLongitude(
          intersectionLongitude,
          cartographicIntersectionScratch,
        );
        intersection = ellipsoid.cartographicToCartesian(
          intersectionCartographic,
          intersectionScratch,
        );
        if (
          defined(intersection) &&
          !Cartesian3.equalsEpsilon(intersection, p0, CesiumMath.EPSILON7) &&
          !Cartesian3.equalsEpsilon(intersection, p1, CesiumMath.EPSILON7)
        ) {
          splitPositions.push(Cartesian3.clone(intersection));
        }
      }
    }
  }
  let cartographicsLength = splitPositions.length;
 
  let cartographics = new Array(cartographicsLength);
  for (i = 0; i < cartographicsLength; i++) {
    const cartographic = Cartographic.fromCartesian(
      splitPositions[i],
      ellipsoid,
    );
    cartographic.height = 0.0;
    cartographics[i] = cartographic;
  }
 
  cartographics = arrayRemoveDuplicates(
    cartographics,
    Cartographic.equalsEpsilon,
  );
  cartographicsLength = cartographics.length;
 
  if (cartographicsLength < 2) {
    return undefined;
  }
 
  /**** Build heap-side arrays for positions, interpolated cartographics, and normals from which to compute vertices ****/
  // We build a "wall" and then decompose it into separately connected component "volumes" because we need a lot
  // of information about the wall. Also, this simplifies interpolation.
  // Convention: "next" and "end" are locally forward to each segment of the wall,
  // and we are computing normals pointing towards the local right side of the vertices in each segment.
  const cartographicsArray = [];
  const normalsArray = [];
  const bottomPositionsArray = [];
  const topPositionsArray = [];
 
  let previousBottom = previousBottomScratch;
  let vertexBottom = vertexBottomScratch;
  let vertexTop = vertexTopScratch;
  let nextBottom = nextBottomScratch;
  let vertexNormal = vertexNormalScratch;
 
  // First point - either loop or attach a "perpendicular" normal
  const startCartographic = cartographics[0];
  const nextCartographic = cartographics[1];
 
  const prestartCartographic = cartographics[cartographicsLength - 1];
  previousBottom = getPosition(
    ellipsoid,
    prestartCartographic,
    minHeight,
    previousBottom,
  );
  nextBottom = getPosition(ellipsoid, nextCartographic, minHeight, nextBottom);
  vertexBottom = getPosition(
    ellipsoid,
    startCartographic,
    minHeight,
    vertexBottom,
  );
  vertexTop = getPosition(ellipsoid, startCartographic, maxHeight, vertexTop);
 
  if (loop) {
    vertexNormal = computeVertexMiterNormal(
      previousBottom,
      vertexBottom,
      vertexTop,
      nextBottom,
      vertexNormal,
    );
  } else {
    vertexNormal = computeRightNormal(
      startCartographic,
      nextCartographic,
      maxHeight,
      ellipsoid,
      vertexNormal,
    );
  }
 
  Cartesian3.pack(vertexNormal, normalsArray, 0);
  Cartesian3.pack(vertexBottom, bottomPositionsArray, 0);
  Cartesian3.pack(vertexTop, topPositionsArray, 0);
  cartographicsArray.push(startCartographic.latitude);
  cartographicsArray.push(startCartographic.longitude);
 
  interpolateSegment(
    startCartographic,
    nextCartographic,
    minHeight,
    maxHeight,
    granularity,
    arcType,
    ellipsoid,
    normalsArray,
    bottomPositionsArray,
    topPositionsArray,
    cartographicsArray,
  );
 
  // All inbetween points
  for (i = 1; i < cartographicsLength - 1; ++i) {
    previousBottom = Cartesian3.clone(vertexBottom, previousBottom);
    vertexBottom = Cartesian3.clone(nextBottom, vertexBottom);
    const vertexCartographic = cartographics[i];
    getPosition(ellipsoid, vertexCartographic, maxHeight, vertexTop);
    getPosition(ellipsoid, cartographics[i + 1], minHeight, nextBottom);
 
    computeVertexMiterNormal(
      previousBottom,
      vertexBottom,
      vertexTop,
      nextBottom,
      vertexNormal,
    );
 
    index = normalsArray.length;
    Cartesian3.pack(vertexNormal, normalsArray, index);
    Cartesian3.pack(vertexBottom, bottomPositionsArray, index);
    Cartesian3.pack(vertexTop, topPositionsArray, index);
    cartographicsArray.push(vertexCartographic.latitude);
    cartographicsArray.push(vertexCartographic.longitude);
 
    interpolateSegment(
      cartographics[i],
      cartographics[i + 1],
      minHeight,
      maxHeight,
      granularity,
      arcType,
      ellipsoid,
      normalsArray,
      bottomPositionsArray,
      topPositionsArray,
      cartographicsArray,
    );
  }
 
  // Last point - either loop or attach a normal "perpendicular" to the wall.
  const endCartographic = cartographics[cartographicsLength - 1];
  const preEndCartographic = cartographics[cartographicsLength - 2];
 
  vertexBottom = getPosition(
    ellipsoid,
    endCartographic,
    minHeight,
    vertexBottom,
  );
  vertexTop = getPosition(ellipsoid, endCartographic, maxHeight, vertexTop);
 
  if (loop) {
    const postEndCartographic = cartographics[0];
    previousBottom = getPosition(
      ellipsoid,
      preEndCartographic,
      minHeight,
      previousBottom,
    );
    nextBottom = getPosition(
      ellipsoid,
      postEndCartographic,
      minHeight,
      nextBottom,
    );
 
    vertexNormal = computeVertexMiterNormal(
      previousBottom,
      vertexBottom,
      vertexTop,
      nextBottom,
      vertexNormal,
    );
  } else {
    vertexNormal = computeRightNormal(
      preEndCartographic,
      endCartographic,
      maxHeight,
      ellipsoid,
      vertexNormal,
    );
  }
 
  index = normalsArray.length;
  Cartesian3.pack(vertexNormal, normalsArray, index);
  Cartesian3.pack(vertexBottom, bottomPositionsArray, index);
  Cartesian3.pack(vertexTop, topPositionsArray, index);
  cartographicsArray.push(endCartographic.latitude);
  cartographicsArray.push(endCartographic.longitude);
 
  if (loop) {
    interpolateSegment(
      endCartographic,
      startCartographic,
      minHeight,
      maxHeight,
      granularity,
      arcType,
      ellipsoid,
      normalsArray,
      bottomPositionsArray,
      topPositionsArray,
      cartographicsArray,
    );
    index = normalsArray.length;
    for (i = 0; i < 3; ++i) {
      normalsArray[index + i] = normalsArray[i];
      bottomPositionsArray[index + i] = bottomPositionsArray[i];
      topPositionsArray[index + i] = topPositionsArray[i];
    }
    cartographicsArray.push(startCartographic.latitude);
    cartographicsArray.push(startCartographic.longitude);
  }
 
  return generateGeometryAttributes(
    loop,
    projection,
    bottomPositionsArray,
    topPositionsArray,
    normalsArray,
    cartographicsArray,
    compute2dAttributes,
  );
};
 
// If the end normal angle is too steep compared to the direction of the line segment,
// "break" the miter by rotating the normal 90 degrees around the "up" direction at the point
// For ultra precision we would want to project into a plane, but in practice this is sufficient.
const lineDirectionScratch = new Cartesian3();
const matrix3Scratch = new Matrix3();
const quaternionScratch = new Quaternion();
function breakMiter(endGeometryNormal, startBottom, endBottom, endTop) {
  const lineDirection = direction(endBottom, startBottom, lineDirectionScratch);
 
  const dot = Cartesian3.dot(lineDirection, endGeometryNormal);
  if (dot > MITER_BREAK_SMALL || dot < MITER_BREAK_LARGE) {
    const vertexUp = direction(endTop, endBottom, vertexUpScratch);
    const angle =
      dot < MITER_BREAK_LARGE
        ? CesiumMath.PI_OVER_TWO
        : -CesiumMath.PI_OVER_TWO;
    const quaternion = Quaternion.fromAxisAngle(
      vertexUp,
      angle,
      quaternionScratch,
    );
    const rotationMatrix = Matrix3.fromQuaternion(quaternion, matrix3Scratch);
    Matrix3.multiplyByVector(
      rotationMatrix,
      endGeometryNormal,
      endGeometryNormal,
    );
    return true;
  }
  return false;
}
 
const endPosCartographicScratch = new Cartographic();
const normalStartpointScratch = new Cartesian3();
const normalEndpointScratch = new Cartesian3();
function projectNormal(
  projection,
  cartographic,
  normal,
  projectedPosition,
  result,
) {
  const position = Cartographic.toCartesian(
    cartographic,
    projection._ellipsoid,
    normalStartpointScratch,
  );
  let normalEndpoint = Cartesian3.add(position, normal, normalEndpointScratch);
  let flipNormal = false;
 
  const ellipsoid = projection._ellipsoid;
  let normalEndpointCartographic = ellipsoid.cartesianToCartographic(
    normalEndpoint,
    endPosCartographicScratch,
  );
  // If normal crosses the IDL, go the other way and flip the result.
  // In practice this almost never happens because the cartographic start
  // and end points of each segment are "nudged" to be on the same side
  // of the IDL and slightly away from the IDL.
  if (
    Math.abs(cartographic.longitude - normalEndpointCartographic.longitude) >
    CesiumMath.PI_OVER_TWO
  ) {
    flipNormal = true;
    normalEndpoint = Cartesian3.subtract(
      position,
      normal,
      normalEndpointScratch,
    );
    normalEndpointCartographic = ellipsoid.cartesianToCartographic(
      normalEndpoint,
      endPosCartographicScratch,
    );
  }
 
  normalEndpointCartographic.height = 0.0;
  const normalEndpointProjected = projection.project(
    normalEndpointCartographic,
    result,
  );
  result = Cartesian3.subtract(
    normalEndpointProjected,
    projectedPosition,
    result,
  );
  result.z = 0.0;
  result = Cartesian3.normalize(result, result);
  if (flipNormal) {
    Cartesian3.negate(result, result);
  }
  return result;
}
 
const adjustHeightNormalScratch = new Cartesian3();
const adjustHeightOffsetScratch = new Cartesian3();
function adjustHeights(
  bottom,
  top,
  minHeight,
  maxHeight,
  adjustHeightBottom,
  adjustHeightTop,
) {
  // bottom and top should be at WALL_INITIAL_MIN_HEIGHT and WALL_INITIAL_MAX_HEIGHT, respectively
  const adjustHeightNormal = Cartesian3.subtract(
    top,
    bottom,
    adjustHeightNormalScratch,
  );
  Cartesian3.normalize(adjustHeightNormal, adjustHeightNormal);
 
  const distanceForBottom = minHeight - WALL_INITIAL_MIN_HEIGHT;
  let adjustHeightOffset = Cartesian3.multiplyByScalar(
    adjustHeightNormal,
    distanceForBottom,
    adjustHeightOffsetScratch,
  );
  Cartesian3.add(bottom, adjustHeightOffset, adjustHeightBottom);
 
  const distanceForTop = maxHeight - WALL_INITIAL_MAX_HEIGHT;
  adjustHeightOffset = Cartesian3.multiplyByScalar(
    adjustHeightNormal,
    distanceForTop,
    adjustHeightOffsetScratch,
  );
  Cartesian3.add(top, adjustHeightOffset, adjustHeightTop);
}
 
const nudgeDirectionScratch = new Cartesian3();
function nudgeXZ(start, end) {
  const startToXZdistance = Plane.getPointDistance(XZ_PLANE, start);
  const endToXZdistance = Plane.getPointDistance(XZ_PLANE, end);
  let offset = nudgeDirectionScratch;
  // Larger epsilon than what's used in GeometryPipeline, a centimeter in world space
  if (CesiumMath.equalsEpsilon(startToXZdistance, 0.0, CesiumMath.EPSILON2)) {
    offset = direction(end, start, offset);
    Cartesian3.multiplyByScalar(offset, CesiumMath.EPSILON2, offset);
    Cartesian3.add(start, offset, start);
  } else if (
    CesiumMath.equalsEpsilon(endToXZdistance, 0.0, CesiumMath.EPSILON2)
  ) {
    offset = direction(start, end, offset);
    Cartesian3.multiplyByScalar(offset, CesiumMath.EPSILON2, offset);
    Cartesian3.add(end, offset, end);
  }
}
 
// "Nudge" cartographic coordinates so start and end are on the same side of the IDL.
// Nudge amounts are tiny, basically just an IDL flip.
// Only used for 2D/CV.
function nudgeCartographic(start, end) {
  const absStartLon = Math.abs(start.longitude);
  const absEndLon = Math.abs(end.longitude);
  if (
    CesiumMath.equalsEpsilon(absStartLon, CesiumMath.PI, CesiumMath.EPSILON11)
  ) {
    const endSign = CesiumMath.sign(end.longitude);
    start.longitude = endSign * (absStartLon - CesiumMath.EPSILON11);
    return 1;
  } else if (
    CesiumMath.equalsEpsilon(absEndLon, CesiumMath.PI, CesiumMath.EPSILON11)
  ) {
    const startSign = CesiumMath.sign(start.longitude);
    end.longitude = startSign * (absEndLon - CesiumMath.EPSILON11);
    return 2;
  }
  return 0;
}
 
const startCartographicScratch = new Cartographic();
const endCartographicScratch = new Cartographic();
 
const segmentStartTopScratch = new Cartesian3();
const segmentEndTopScratch = new Cartesian3();
const segmentStartBottomScratch = new Cartesian3();
const segmentEndBottomScratch = new Cartesian3();
const segmentStartNormalScratch = new Cartesian3();
const segmentEndNormalScratch = new Cartesian3();
 
const getHeightCartographics = [
  startCartographicScratch,
  endCartographicScratch,
];
const getHeightRectangleScratch = new Rectangle();
 
const adjustHeightStartTopScratch = new Cartesian3();
const adjustHeightEndTopScratch = new Cartesian3();
const adjustHeightStartBottomScratch = new Cartesian3();
const adjustHeightEndBottomScratch = new Cartesian3();
 
const segmentStart2DScratch = new Cartesian3();
const segmentEnd2DScratch = new Cartesian3();
const segmentStartNormal2DScratch = new Cartesian3();
const segmentEndNormal2DScratch = new Cartesian3();
 
const offsetScratch = new Cartesian3();
const startUpScratch = new Cartesian3();
const endUpScratch = new Cartesian3();
const rightScratch = new Cartesian3();
const startPlaneNormalScratch = new Cartesian3();
const endPlaneNormalScratch = new Cartesian3();
const encodeScratch = new EncodedCartesian3();
 
const encodeScratch2D = new EncodedCartesian3();
const forwardOffset2DScratch = new Cartesian3();
const right2DScratch = new Cartesian3();
 
const normalNudgeScratch = new Cartesian3();
 
const scratchBoundingSpheres = [new BoundingSphere(), new BoundingSphere()];
 
// Winding order is reversed so each segment's volume is inside-out
const REFERENCE_INDICES = [
  0,
  2,
  1,
  0,
  3,
  2, // right
  0,
  7,
  3,
  0,
  4,
  7, // start
  0,
  5,
  4,
  0,
  1,
  5, // bottom
  5,
  7,
  4,
  5,
  6,
  7, // left
  5,
  2,
  6,
  5,
  1,
  2, // end
  3,
  6,
  2,
  3,
  7,
  6, // top
];
const REFERENCE_INDICES_LENGTH = REFERENCE_INDICES.length;
 
// Decompose the "wall" into a series of shadow volumes.
// Each shadow volume's vertices encode a description of the line it contains,
// including mitering planes at the end points, a plane along the line itself,
// and attributes for computing length-wise texture coordinates.
function generateGeometryAttributes(
  loop,
  projection,
  bottomPositionsArray,
  topPositionsArray,
  normalsArray,
  cartographicsArray,
  compute2dAttributes,
) {
  let i;
  let index;
  const ellipsoid = projection._ellipsoid;
 
  // Each segment will have 8 vertices
  const segmentCount = bottomPositionsArray.length / 3 - 1;
  const vertexCount = segmentCount * 8;
  const arraySizeVec4 = vertexCount * 4;
  const indexCount = segmentCount * 36;
 
  const indices =
    vertexCount > 65535
      ? new Uint32Array(indexCount)
      : new Uint16Array(indexCount);
  const positionsArray = new Float64Array(vertexCount * 3);
 
  const startHiAndForwardOffsetX = new Float32Array(arraySizeVec4);
  const startLoAndForwardOffsetY = new Float32Array(arraySizeVec4);
  const startNormalAndForwardOffsetZ = new Float32Array(arraySizeVec4);
  const endNormalAndTextureCoordinateNormalizationX = new Float32Array(
    arraySizeVec4,
  );
  const rightNormalAndTextureCoordinateNormalizationY = new Float32Array(
    arraySizeVec4,
  );
 
  let startHiLo2D;
  let offsetAndRight2D;
  let startEndNormals2D;
  let texcoordNormalization2D;
 
  if (compute2dAttributes) {
    startHiLo2D = new Float32Array(arraySizeVec4);
    offsetAndRight2D = new Float32Array(arraySizeVec4);
    startEndNormals2D = new Float32Array(arraySizeVec4);
    texcoordNormalization2D = new Float32Array(vertexCount * 2);
  }
 
  /*** Compute total lengths for texture coordinate normalization ***/
  // 2D
  const cartographicsLength = cartographicsArray.length / 2;
  let length2D = 0.0;
 
  const startCartographic = startCartographicScratch;
  startCartographic.height = 0.0;
  const endCartographic = endCartographicScratch;
  endCartographic.height = 0.0;
 
  let segmentStartCartesian = segmentStartTopScratch;
  let segmentEndCartesian = segmentEndTopScratch;
 
  if (compute2dAttributes) {
    index = 0;
    for (i = 1; i < cartographicsLength; i++) {
      // Don't clone anything from previous segment b/c possible IDL touch
      startCartographic.latitude = cartographicsArray[index];
      startCartographic.longitude = cartographicsArray[index + 1];
      endCartographic.latitude = cartographicsArray[index + 2];
      endCartographic.longitude = cartographicsArray[index + 3];
 
      segmentStartCartesian = projection.project(
        startCartographic,
        segmentStartCartesian,
      );
      segmentEndCartesian = projection.project(
        endCartographic,
        segmentEndCartesian,
      );
      length2D += Cartesian3.distance(
        segmentStartCartesian,
        segmentEndCartesian,
      );
      index += 2;
    }
  }
 
  // 3D
  const positionsLength = topPositionsArray.length / 3;
  segmentEndCartesian = Cartesian3.unpack(
    topPositionsArray,
    0,
    segmentEndCartesian,
  );
  let length3D = 0.0;
 
  index = 3;
  for (i = 1; i < positionsLength; i++) {
    segmentStartCartesian = Cartesian3.clone(
      segmentEndCartesian,
      segmentStartCartesian,
    );
    segmentEndCartesian = Cartesian3.unpack(
      topPositionsArray,
      index,
      segmentEndCartesian,
    );
    length3D += Cartesian3.distance(segmentStartCartesian, segmentEndCartesian);
    index += 3;
  }
 
  /*** Generate segments ***/
  let j;
  index = 3;
  let cartographicsIndex = 0;
  let vec2sWriteIndex = 0;
  let vec3sWriteIndex = 0;
  let vec4sWriteIndex = 0;
  let miterBroken = false;
 
  let endBottom = Cartesian3.unpack(
    bottomPositionsArray,
    0,
    segmentEndBottomScratch,
  );
  let endTop = Cartesian3.unpack(topPositionsArray, 0, segmentEndTopScratch);
  let endGeometryNormal = Cartesian3.unpack(
    normalsArray,
    0,
    segmentEndNormalScratch,
  );
 
  if (loop) {
    const preEndBottom = Cartesian3.unpack(
      bottomPositionsArray,
      bottomPositionsArray.length - 6,
      segmentStartBottomScratch,
    );
    if (breakMiter(endGeometryNormal, preEndBottom, endBottom, endTop)) {
      // Miter broken as if for the last point in the loop, needs to be inverted for first point (clone of endBottom)
      endGeometryNormal = Cartesian3.negate(
        endGeometryNormal,
        endGeometryNormal,
      );
    }
  }
 
  let lengthSoFar3D = 0.0;
  let lengthSoFar2D = 0.0;
 
  // For translating bounding volume
  let sumHeights = 0.0;
 
  for (i = 0; i < segmentCount; i++) {
    const startBottom = Cartesian3.clone(endBottom, segmentStartBottomScratch);
    const startTop = Cartesian3.clone(endTop, segmentStartTopScratch);
    let startGeometryNormal = Cartesian3.clone(
      endGeometryNormal,
      segmentStartNormalScratch,
    );
 
    if (miterBroken) {
      startGeometryNormal = Cartesian3.negate(
        startGeometryNormal,
        startGeometryNormal,
      );
    }
 
    endBottom = Cartesian3.unpack(
      bottomPositionsArray,
      index,
      segmentEndBottomScratch,
    );
    endTop = Cartesian3.unpack(topPositionsArray, index, segmentEndTopScratch);
    endGeometryNormal = Cartesian3.unpack(
      normalsArray,
      index,
      segmentEndNormalScratch,
    );
 
    miterBroken = breakMiter(endGeometryNormal, startBottom, endBottom, endTop);
 
    // 2D - don't clone anything from previous segment b/c possible IDL touch
    startCartographic.latitude = cartographicsArray[cartographicsIndex];
    startCartographic.longitude = cartographicsArray[cartographicsIndex + 1];
    endCartographic.latitude = cartographicsArray[cartographicsIndex + 2];
    endCartographic.longitude = cartographicsArray[cartographicsIndex + 3];
    let start2D;
    let end2D;
    let startGeometryNormal2D;
    let endGeometryNormal2D;
 
    if (compute2dAttributes) {
      const nudgeResult = nudgeCartographic(startCartographic, endCartographic);
      start2D = projection.project(startCartographic, segmentStart2DScratch);
      end2D = projection.project(endCartographic, segmentEnd2DScratch);
      const direction2D = direction(end2D, start2D, forwardOffset2DScratch);
      direction2D.y = Math.abs(direction2D.y);
 
      startGeometryNormal2D = segmentStartNormal2DScratch;
      endGeometryNormal2D = segmentEndNormal2DScratch;
      if (
        nudgeResult === 0 ||
        Cartesian3.dot(direction2D, Cartesian3.UNIT_Y) > MITER_BREAK_SMALL
      ) {
        // No nudge - project the original normal
        // Or, if the line's angle relative to the IDL is very acute,
        // in which case snapping will produce oddly shaped volumes.
        startGeometryNormal2D = projectNormal(
          projection,
          startCartographic,
          startGeometryNormal,
          start2D,
          segmentStartNormal2DScratch,
        );
        endGeometryNormal2D = projectNormal(
          projection,
          endCartographic,
          endGeometryNormal,
          end2D,
          segmentEndNormal2DScratch,
        );
      } else if (nudgeResult === 1) {
        // Start is close to IDL - snap start normal to align with IDL
        endGeometryNormal2D = projectNormal(
          projection,
          endCartographic,
          endGeometryNormal,
          end2D,
          segmentEndNormal2DScratch,
        );
        startGeometryNormal2D.x = 0.0;
        // If start longitude is negative and end longitude is less negative, relative right is unit -Y
        // If start longitude is positive and end longitude is less positive, relative right is unit +Y
        startGeometryNormal2D.y = CesiumMath.sign(
          startCartographic.longitude - Math.abs(endCartographic.longitude),
        );
        startGeometryNormal2D.z = 0.0;
      } else {
        // End is close to IDL - snap end normal to align with IDL
        startGeometryNormal2D = projectNormal(
          projection,
          startCartographic,
          startGeometryNormal,
          start2D,
          segmentStartNormal2DScratch,
        );
        endGeometryNormal2D.x = 0.0;
        // If end longitude is negative and start longitude is less negative, relative right is unit Y
        // If end longitude is positive and start longitude is less positive, relative right is unit -Y
        endGeometryNormal2D.y = CesiumMath.sign(
          startCartographic.longitude - endCartographic.longitude,
        );
        endGeometryNormal2D.z = 0.0;
      }
    }
 
    /****************************************
     * Geometry descriptors of a "line on terrain,"
     * as opposed to the "shadow volume used to draw
     * the line on terrain":
     * - position of start + offset to end
     * - start, end, and right-facing planes
     * - encoded texture coordinate offsets
     ****************************************/
 
    /* 3D */
    const segmentLength3D = Cartesian3.distance(startTop, endTop);
 
    const encodedStart = EncodedCartesian3.fromCartesian(
      startBottom,
      encodeScratch,
    );
    const forwardOffset = Cartesian3.subtract(
      endBottom,
      startBottom,
      offsetScratch,
    );
    const forward = Cartesian3.normalize(forwardOffset, rightScratch);
 
    let startUp = Cartesian3.subtract(startTop, startBottom, startUpScratch);
    startUp = Cartesian3.normalize(startUp, startUp);
    let rightNormal = Cartesian3.cross(forward, startUp, rightScratch);
    rightNormal = Cartesian3.normalize(rightNormal, rightNormal);
 
    let startPlaneNormal = Cartesian3.cross(
      startUp,
      startGeometryNormal,
      startPlaneNormalScratch,
    );
    startPlaneNormal = Cartesian3.normalize(startPlaneNormal, startPlaneNormal);
 
    let endUp = Cartesian3.subtract(endTop, endBottom, endUpScratch);
    endUp = Cartesian3.normalize(endUp, endUp);
    let endPlaneNormal = Cartesian3.cross(
      endGeometryNormal,
      endUp,
      endPlaneNormalScratch,
    );
    endPlaneNormal = Cartesian3.normalize(endPlaneNormal, endPlaneNormal);
 
    const texcoordNormalization3DX = segmentLength3D / length3D;
    const texcoordNormalization3DY = lengthSoFar3D / length3D;
 
    /* 2D */
    let segmentLength2D = 0.0;
    let encodedStart2D;
    let forwardOffset2D;
    let right2D;
    let texcoordNormalization2DX = 0.0;
    let texcoordNormalization2DY = 0.0;
    if (compute2dAttributes) {
      segmentLength2D = Cartesian3.distance(start2D, end2D);
 
      encodedStart2D = EncodedCartesian3.fromCartesian(
        start2D,
        encodeScratch2D,
      );
      forwardOffset2D = Cartesian3.subtract(
        end2D,
        start2D,
        forwardOffset2DScratch,
      );
 
      // Right direction is just forward direction rotated by -90 degrees around Z
      // Similarly with plane normals
      right2D = Cartesian3.normalize(forwardOffset2D, right2DScratch);
      const swap = right2D.x;
      right2D.x = right2D.y;
      right2D.y = -swap;
 
      texcoordNormalization2DX = segmentLength2D / length2D;
      texcoordNormalization2DY = lengthSoFar2D / length2D;
    }
    /** Pack **/
    for (j = 0; j < 8; j++) {
      const vec4Index = vec4sWriteIndex + j * 4;
      const vec2Index = vec2sWriteIndex + j * 2;
      const wIndex = vec4Index + 3;
 
      // Encode sidedness of vertex relative to right plane in texture coordinate normalization X,
      // whether vertex is top or bottom of volume in sign/magnitude of normalization Y.
      const rightPlaneSide = j < 4 ? 1.0 : -1.0;
      const topBottomSide =
        j === 2 || j === 3 || j === 6 || j === 7 ? 1.0 : -1.0;
 
      // 3D
      Cartesian3.pack(encodedStart.high, startHiAndForwardOffsetX, vec4Index);
      startHiAndForwardOffsetX[wIndex] = forwardOffset.x;
 
      Cartesian3.pack(encodedStart.low, startLoAndForwardOffsetY, vec4Index);
      startLoAndForwardOffsetY[wIndex] = forwardOffset.y;
 
      Cartesian3.pack(
        startPlaneNormal,
        startNormalAndForwardOffsetZ,
        vec4Index,
      );
      startNormalAndForwardOffsetZ[wIndex] = forwardOffset.z;
 
      Cartesian3.pack(
        endPlaneNormal,
        endNormalAndTextureCoordinateNormalizationX,
        vec4Index,
      );
      endNormalAndTextureCoordinateNormalizationX[wIndex] =
        texcoordNormalization3DX * rightPlaneSide;
 
      Cartesian3.pack(
        rightNormal,
        rightNormalAndTextureCoordinateNormalizationY,
        vec4Index,
      );
 
      let texcoordNormalization = texcoordNormalization3DY * topBottomSide;
      if (texcoordNormalization === 0.0 && topBottomSide < 0.0) {
        texcoordNormalization = 9.0; // some value greater than 1.0
      }
      rightNormalAndTextureCoordinateNormalizationY[wIndex] =
        texcoordNormalization;
 
      // 2D
      if (compute2dAttributes) {
        startHiLo2D[vec4Index] = encodedStart2D.high.x;
        startHiLo2D[vec4Index + 1] = encodedStart2D.high.y;
        startHiLo2D[vec4Index + 2] = encodedStart2D.low.x;
        startHiLo2D[vec4Index + 3] = encodedStart2D.low.y;
 
        startEndNormals2D[vec4Index] = -startGeometryNormal2D.y;
        startEndNormals2D[vec4Index + 1] = startGeometryNormal2D.x;
        startEndNormals2D[vec4Index + 2] = endGeometryNormal2D.y;
        startEndNormals2D[vec4Index + 3] = -endGeometryNormal2D.x;
 
        offsetAndRight2D[vec4Index] = forwardOffset2D.x;
        offsetAndRight2D[vec4Index + 1] = forwardOffset2D.y;
        offsetAndRight2D[vec4Index + 2] = right2D.x;
        offsetAndRight2D[vec4Index + 3] = right2D.y;
 
        texcoordNormalization2D[vec2Index] =
          texcoordNormalization2DX * rightPlaneSide;
 
        texcoordNormalization = texcoordNormalization2DY * topBottomSide;
        if (texcoordNormalization === 0.0 && topBottomSide < 0.0) {
          texcoordNormalization = 9.0; // some value greater than 1.0
        }
        texcoordNormalization2D[vec2Index + 1] = texcoordNormalization;
      }
    }
 
    // Adjust height of volume in 3D
    const adjustHeightStartBottom = adjustHeightStartBottomScratch;
    const adjustHeightEndBottom = adjustHeightEndBottomScratch;
    const adjustHeightStartTop = adjustHeightStartTopScratch;
    const adjustHeightEndTop = adjustHeightEndTopScratch;
 
    const getHeightsRectangle = Rectangle.fromCartographicArray(
      getHeightCartographics,
      getHeightRectangleScratch,
    );
    const minMaxHeights = ApproximateTerrainHeights.getMinimumMaximumHeights(
      getHeightsRectangle,
      ellipsoid,
    );
    const minHeight = minMaxHeights.minimumTerrainHeight;
    const maxHeight = minMaxHeights.maximumTerrainHeight;
 
    // Sum using abs() to properly account for negative eleavtions in calculating bounding sphere radius
    sumHeights += Math.abs(minHeight);
    sumHeights += Math.abs(maxHeight);
 
    adjustHeights(
      startBottom,
      startTop,
      minHeight,
      maxHeight,
      adjustHeightStartBottom,
      adjustHeightStartTop,
    );
    adjustHeights(
      endBottom,
      endTop,
      minHeight,
      maxHeight,
      adjustHeightEndBottom,
      adjustHeightEndTop,
    );
 
    // Nudge the positions away from the "polyline" a little bit to prevent errors in GeometryPipeline
    let normalNudge = Cartesian3.multiplyByScalar(
      rightNormal,
      CesiumMath.EPSILON5,
      normalNudgeScratch,
    );
    Cartesian3.add(
      adjustHeightStartBottom,
      normalNudge,
      adjustHeightStartBottom,
    );
    Cartesian3.add(adjustHeightEndBottom, normalNudge, adjustHeightEndBottom);
    Cartesian3.add(adjustHeightStartTop, normalNudge, adjustHeightStartTop);
    Cartesian3.add(adjustHeightEndTop, normalNudge, adjustHeightEndTop);
 
    // If the segment is very close to the XZ plane, nudge the vertices slightly to avoid touching it.
    nudgeXZ(adjustHeightStartBottom, adjustHeightEndBottom);
    nudgeXZ(adjustHeightStartTop, adjustHeightEndTop);
 
    Cartesian3.pack(adjustHeightStartBottom, positionsArray, vec3sWriteIndex);
    Cartesian3.pack(adjustHeightEndBottom, positionsArray, vec3sWriteIndex + 3);
    Cartesian3.pack(adjustHeightEndTop, positionsArray, vec3sWriteIndex + 6);
    Cartesian3.pack(adjustHeightStartTop, positionsArray, vec3sWriteIndex + 9);
 
    normalNudge = Cartesian3.multiplyByScalar(
      rightNormal,
      -2.0 * CesiumMath.EPSILON5,
      normalNudgeScratch,
    );
    Cartesian3.add(
      adjustHeightStartBottom,
      normalNudge,
      adjustHeightStartBottom,
    );
    Cartesian3.add(adjustHeightEndBottom, normalNudge, adjustHeightEndBottom);
    Cartesian3.add(adjustHeightStartTop, normalNudge, adjustHeightStartTop);
    Cartesian3.add(adjustHeightEndTop, normalNudge, adjustHeightEndTop);
 
    nudgeXZ(adjustHeightStartBottom, adjustHeightEndBottom);
    nudgeXZ(adjustHeightStartTop, adjustHeightEndTop);
 
    Cartesian3.pack(
      adjustHeightStartBottom,
      positionsArray,
      vec3sWriteIndex + 12,
    );
    Cartesian3.pack(
      adjustHeightEndBottom,
      positionsArray,
      vec3sWriteIndex + 15,
    );
    Cartesian3.pack(adjustHeightEndTop, positionsArray, vec3sWriteIndex + 18);
    Cartesian3.pack(adjustHeightStartTop, positionsArray, vec3sWriteIndex + 21);
 
    cartographicsIndex += 2;
    index += 3;
 
    vec2sWriteIndex += 16;
    vec3sWriteIndex += 24;
    vec4sWriteIndex += 32;
 
    lengthSoFar3D += segmentLength3D;
    lengthSoFar2D += segmentLength2D;
  }
 
  index = 0;
  let indexOffset = 0;
  for (i = 0; i < segmentCount; i++) {
    for (j = 0; j < REFERENCE_INDICES_LENGTH; j++) {
      indices[index + j] = REFERENCE_INDICES[j] + indexOffset;
    }
    indexOffset += 8;
    index += REFERENCE_INDICES_LENGTH;
  }
 
  const boundingSpheres = scratchBoundingSpheres;
  BoundingSphere.fromVertices(
    bottomPositionsArray,
    Cartesian3.ZERO,
    3,
    boundingSpheres[0],
  );
  BoundingSphere.fromVertices(
    topPositionsArray,
    Cartesian3.ZERO,
    3,
    boundingSpheres[1],
  );
  const boundingSphere = BoundingSphere.fromBoundingSpheres(boundingSpheres);
 
  // Adjust bounding sphere height and radius to cover more of the volume
  boundingSphere.radius += sumHeights / (segmentCount * 2.0);
 
  const attributes = {
    position: new GeometryAttribute({
      componentDatatype: ComponentDatatype.DOUBLE,
      componentsPerAttribute: 3,
      normalize: false,
      values: positionsArray,
    }),
    startHiAndForwardOffsetX: getVec4GeometryAttribute(
      startHiAndForwardOffsetX,
    ),
    startLoAndForwardOffsetY: getVec4GeometryAttribute(
      startLoAndForwardOffsetY,
    ),
    startNormalAndForwardOffsetZ: getVec4GeometryAttribute(
      startNormalAndForwardOffsetZ,
    ),
    endNormalAndTextureCoordinateNormalizationX: getVec4GeometryAttribute(
      endNormalAndTextureCoordinateNormalizationX,
    ),
    rightNormalAndTextureCoordinateNormalizationY: getVec4GeometryAttribute(
      rightNormalAndTextureCoordinateNormalizationY,
    ),
  };
 
  if (compute2dAttributes) {
    attributes.startHiLo2D = getVec4GeometryAttribute(startHiLo2D);
    attributes.offsetAndRight2D = getVec4GeometryAttribute(offsetAndRight2D);
    attributes.startEndNormals2D = getVec4GeometryAttribute(startEndNormals2D);
    attributes.texcoordNormalization2D = new GeometryAttribute({
      componentDatatype: ComponentDatatype.FLOAT,
      componentsPerAttribute: 2,
      normalize: false,
      values: texcoordNormalization2D,
    });
  }
 
  return new Geometry({
    attributes: attributes,
    indices: indices,
    boundingSphere: boundingSphere,
  });
}
 
function getVec4GeometryAttribute(typedArray) {
  return new GeometryAttribute({
    componentDatatype: ComponentDatatype.FLOAT,
    componentsPerAttribute: 4,
    normalize: false,
    values: typedArray,
  });
}
 
/**
 * Approximates an ellipsoid-tangent vector in 2D by projecting the end point into 2D.
 * Exposed for testing.
 *
 * @param {MapProjection} projection Map Projection for projecting coordinates to 2D.
 * @param {Cartographic} cartographic The cartographic origin point of the normal.
 *   Used to check if the normal crosses the IDL during projection.
 * @param {Cartesian3} normal The normal in 3D.
 * @param {Cartesian3} projectedPosition The projected origin point of the normal in 2D.
 * @param {Cartesian3} result Result parameter on which to store the projected normal.
 * @private
 */
GroundPolylineGeometry._projectNormal = projectNormal;
export default GroundPolylineGeometry;