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 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 | 2398x 2397x 2397x 2397x 2396x 2396x 2396x 2396x 7x 2389x 1x 1262x 1x 2236x 2236x 2236x 2236x 352x 3x 352x 352x 352x 164x 3x 164x 139x 3x 139x 1145x 3x 1145x 1x 1338x 1338x 1250x 11x 1239x 209x 1030x 1x 898x 898x 898x 1x 78x 78x 78x 1x 187x 1x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 19x 97x 39x 58x 19x 39x 19x 20x 19x 1x 4x 33x 7x 5x 2x 2x 26x 4x 22x 4x 18x 4x 14x 4x 10x 2x 19x 11x 5x 6x 2x 4x 4x 12x 12x 2x 10x 2x 8x 2x 6x 4x 1x 3x 1x 2x 1x 1x 1x 4x 1x 3x 1x 2x 1x 1x 1x 5x 1x 4x 1x 3x 1x 2x 1x 1x 5x 1x 4x 1x 3x 1x 2x 1x 1x 4x 3x 1x 6686x 6686x 6686x 6686x 6686x 6686x 6686x 2397x 1998x 399x 431x 431x 431x 431x 431x 399x 2397x 674x 2397x 2397x 2397x 2397x 507x 507x 507x 18x 18x 18x 18x 489x 11x 11x 11x 11x 478x 478x 478x 1x 477x 477x 477x 2396x 2396x 3882x 3882x 5x 3877x 417x 3460x 2812x 648x 648x 18x 630x 1372x 1372x 1372x 36x 36x 36x 24x 2x 22x 2x 1x 1x 20x 20x 20x 12x 11x 11x 1x 1336x 1336x 355x 16x 339x 339x 19x 19x 320x 981x 37x 2x 35x 35x 944x 116x 2x 114x 114x 828x 462x 462x 1242x 462x 366x 18x 2x 1x 1x 16x 16x 348x 10x 4x 6x 6x 338x 3x 1x 2x 335x 166x 41x 125x 125x 169x 68x 14x 54x 54x 54x 101x 29x 8x 21x 21x 21x 21x 72x 4x 1x 3x 3x 68x 5x 1x 4x 4x 63x 6x 1x 5x 5x 57x 56x 1x 56x 56x 2x 54x 54x 11x 11x 9x 9x 1x 8x 2x 43x 35x 35x 35x 8x 488x 476x 476x 3x 473x 12x 3x 9x 3x 6x 3x 3x 32x 32x 27x 5x 5x 1x 1x 1x 161x 32x 129x 1x 128x 126x 69x 69x 57x 57x 64x 488x 476x 6720x 3882x 2838x 1372x 1466x 488x 978x 137x 137x 137x 136x 1x 841x 640x 640x 640x 640x 635x 5x 201x 201x 29x 29x 29x 29x 172x 161x 11x 8x 8x 23x 8x 3x 2x 1x 6629x 6686x 29x 6657x 31x 10x 21x 10x 11x 11x 6626x 297x 6x 291x 123x 168x 7x 161x 8x 153x 8x 145x 3x 142x 3x 139x 2x 137x 3x 134x 4x 130x 5x 125x 125x 6329x 689x 76x 613x 14x 599x 69x 530x 70x 460x 11x 449x 14x 435x 10x 425x 25x 400x 9x 391x 219x 172x 75x 97x 16x 81x 7x 74x 10x 64x 10x 54x 54x 5640x 21x 5619x 126x 69x 57x 5493x 8x 5485x 473x 5012x 18x 4994x 504x 4490x 462x 4028x 688x 3340x 10x 3330x 3x 3x 3327x 3x 1x 2x 125x 125x 105x 105x 54x 54x 47x 47x 47x 21x 21x 19x 19x 19x 19x 406x 399x 1x 4249x 1x 992x 992x 992x 405x 2x 403x 95x 95x 308x 587x 206x 381x 371x 371x 10x 5x 5x 5x 992x 1x 352x 352x 352x 352x 352x 944x 944x 923x 21x 9x 12x 5x 7x 4x 3x 349x 349x 349x 3x 346x 4x 342x 6x 336x 37x 37x 336x 125x 211x 101x 110x 110x 1x 561x 1x 12x 12x 12x 13x 13x 13x 13x 3x 13x 13x 13x 12x 1x 378x 170x 1x 41x 8x 33x 33x 2x 31x 31x 22x 2x 20x 2x 18x 2x 16x 2x 23x 1x 49x 7x 42x 42x 2x 40x 40x 28x 4x 24x 5x 19x 4x 15x 4x 23x 1x 2x 2x 8x 2x 1x 5x 5x 1x 4x 1x 70x 70x 1x 69x 1x 68x 1x 67x 66x 1x 1x 6x 6x 1x 5x 1x 81x 81x 81x 4x 77x 1x 8x 8x 8x 5x 3x 1x 126x 126x 126x 5x 121x 1x 59x 59x 59x 5x 54x 1x 5x 5x 1x 4x 1x 3x 3x 1x 2x 1x 17x 17x 2x 15x 8x 7x 7x 1x 6x 1x 45x 45x 45x 1x 44x 1x 43x 2x 41x 17x 24x 22x 2x 1x 13x 13x 13x 1x 12x 1x 11x 2x 9x 6x 3x 1x 63x 63x 63x 1x 62x 1x 61x 1x 60x 1x 59x 1x 58x 1x 57x 2x 55x 2x 53x 2x 51x 49x 2x 1x 65x 65x 65x 1x 64x 1x 63x 1x 62x 1x 61x 2x 59x 2x 57x 54x 3x 1x 10x 10x 10x 1x 9x 1x 8x 2x 6x 3x 3x 1x 15x 15x 15x 4x 11x 1x 9x 9x 9x 4x 5x 1x 30x 30x 30x 16x 14x 1x 7x 1x 7x 1x 3x 2x 1x 1x 3x 2x 1x 1x 2x 1x 1x 2x 1x 2x 1x 5x 1x 10x 10x 10x 2x 10x 10x 1x 9x 1x 10x 10x 10x 2x 8x 1x 9x 9x 9x 4x 5x 2x 3x 1x 9x 9x 9x 4x 5x 2x 3x 1x 10x 10x 10x 2x 8x 8x 1x 7x 1x 10x 10x 9x 1x 4x 4x 4x 11x 2x 2x 2x 2x 2x 2x 6x 6x 6x 19x 2x 4x 4x 4x 4x 4x 4x 353x 322x 31x 37x 37x 37x 37x 6x 6x 6x 6x 6x 78x 78x 78x 144x 78x 30x 3x 27x 1x 1x 546x 546x 546x 197x 72x 125x 540x 87x 538x 3x 538x 6x 538x 30x 2x 28x 32x 1x 31x 2x 29x 1x 28x 19x 9x 1x 8x 1x 7x 2x 5x 50x 1x 49x 1x 48x 1x 47x 1x 46x 6x 40x 2x 2x 1x 32x 2x 30x 14x 16x 5x 11x 5x 6x 5x 1x 1x 6x 1x 5x 1x 4x 1x 3x 1x 1x 14x 218x 57x 17x 15x 42x 42x 37x 5x 61x 61x 51x 14x 37x 1x 1x 1x 1x 1x 36x 10x 2x 2x 1x 1x 8x 4x 4x 4x 4x 3x 1x 4x 2x 2x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 55x 55x 30x 25x 25x 4x 1x 2x 2x 1x 39x 39x 39x 14x 2x 2x 2x 2x 12x 39x 12x 39x 39x 39x 12x 10x 12x 1x 1x 1x 1x 1x 4x 2x 4x | import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Cartesian4 from "../Core/Cartesian4.js";
import Check from "../Core/Check.js";
import Color from "../Core/Color.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import CesiumMath from "../Core/Math.js";
import RuntimeError from "../Core/RuntimeError.js";
import jsep from "jsep";
import ExpressionNodeType from "./ExpressionNodeType.js";
/**
* An expression for a style applied to a {@link Cesium3DTileset}.
* <p>
* Evaluates an expression defined using the
* {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Styling|3D Tiles Styling language}.
* </p>
* <p>
* Implements the {@link StyleExpression} interface.
* </p>
*
* @alias Expression
* @constructor
*
* @param {string} [expression] The expression defined using the 3D Tiles Styling language.
* @param {object} [defines] Defines in the style.
*
* @example
* const expression = new Cesium.Expression('(regExp("^Chest").test(${County})) && (${YearBuilt} >= 1970)');
* expression.evaluate(feature); // returns true or false depending on the feature's properties
*
* @example
* const expression = new Cesium.Expression('(${Temperature} > 90) ? color("red") : color("white")');
* expression.evaluateColor(feature, result); // returns a Cesium.Color object
*/
function Expression(expression, defines) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.string("expression", expression);
//>>includeEnd('debug');
this._expression = expression;
expression = replaceDefines(expression, defines);
expression = replaceVariables(removeBackslashes(expression));
// customize jsep operators
jsep.addBinaryOp("=~", 0);
jsep.addBinaryOp("!~", 0);
let ast;
try {
ast = jsep(expression);
} catch (e) {
throw new RuntimeError(e);
}
this._runtimeAst = createRuntimeAst(this, ast);
}
Object.defineProperties(Expression.prototype, {
/**
* Gets the expression defined in the 3D Tiles Styling language.
*
* @memberof Expression.prototype
*
* @type {string}
* @readonly
*
* @default undefined
*/
expression: {
get: function () {
return this._expression;
},
},
});
// Scratch storage manager while evaluating deep expressions.
// For example, an expression like dot(vec4(${red}), vec4(${green}) * vec4(${blue}) requires 3 scratch Cartesian4's
const scratchStorage = {
arrayIndex: 0,
arrayArray: [[]],
cartesian2Index: 0,
cartesian3Index: 0,
cartesian4Index: 0,
cartesian2Array: [new Cartesian2()],
cartesian3Array: [new Cartesian3()],
cartesian4Array: [new Cartesian4()],
reset: function () {
this.arrayIndex = 0;
this.cartesian2Index = 0;
this.cartesian3Index = 0;
this.cartesian4Index = 0;
},
getArray: function () {
if (this.arrayIndex >= this.arrayArray.length) {
this.arrayArray.push([]);
}
const array = this.arrayArray[this.arrayIndex++];
array.length = 0;
return array;
},
getCartesian2: function () {
if (this.cartesian2Index >= this.cartesian2Array.length) {
this.cartesian2Array.push(new Cartesian2());
}
return this.cartesian2Array[this.cartesian2Index++];
},
getCartesian3: function () {
if (this.cartesian3Index >= this.cartesian3Array.length) {
this.cartesian3Array.push(new Cartesian3());
}
return this.cartesian3Array[this.cartesian3Index++];
},
getCartesian4: function () {
if (this.cartesian4Index >= this.cartesian4Array.length) {
this.cartesian4Array.push(new Cartesian4());
}
return this.cartesian4Array[this.cartesian4Index++];
},
};
/**
* Evaluates the result of an expression, optionally using the provided feature's properties. If the result of
* the expression in the
* {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Styling|3D Tiles Styling language}
* is of type <code>Boolean</code>, <code>Number</code>, or <code>String</code>, the corresponding JavaScript
* primitive type will be returned. If the result is a <code>RegExp</code>, a Javascript <code>RegExp</code>
* object will be returned. If the result is a <code>Cartesian2</code>, <code>Cartesian3</code>, or <code>Cartesian4</code>,
* a {@link Cartesian2}, {@link Cartesian3}, or {@link Cartesian4} object will be returned. If the <code>result</code> argument is
* a {@link Color}, the {@link Cartesian4} value is converted to a {@link Color} and then returned.
*
* @param {Cesium3DTileFeature} feature The feature whose properties may be used as variables in the expression.
* @param {object} [result] The object onto which to store the result.
* @returns {boolean|number|string|RegExp|Cartesian2|Cartesian3|Cartesian4|Color} The result of evaluating the expression.
*/
Expression.prototype.evaluate = function (feature, result) {
scratchStorage.reset();
const value = this._runtimeAst.evaluate(feature);
if (result instanceof Color && value instanceof Cartesian4) {
return Color.fromCartesian4(value, result);
}
if (
value instanceof Cartesian2 ||
value instanceof Cartesian3 ||
value instanceof Cartesian4
) {
return value.clone(result);
}
return value;
};
/**
* Evaluates the result of a Color expression, optionally using the provided feature's properties.
* <p>
* This is equivalent to {@link Expression#evaluate} but always returns a {@link Color} object.
* </p>
*
* @param {Cesium3DTileFeature} feature The feature whose properties may be used as variables in the expression.
* @param {Color} [result] The object in which to store the result
* @returns {Color} The modified result parameter or a new Color instance if one was not provided.
*/
Expression.prototype.evaluateColor = function (feature, result) {
scratchStorage.reset();
const color = this._runtimeAst.evaluate(feature);
return Color.fromCartesian4(color, result);
};
/**
* Gets the shader function for this expression.
* Returns undefined if the shader function can't be generated from this expression.
*
* @param {string} functionSignature Signature of the generated function.
* @param {object} variableSubstitutionMap Maps variable names to shader variable names.
* @param {object} shaderState Stores information about the generated shader function, including whether it is translucent.
* @param {string} returnType The return type of the generated function.
*
* @returns {string} The shader function.
*
* @private
*/
Expression.prototype.getShaderFunction = function (
functionSignature,
variableSubstitutionMap,
shaderState,
returnType,
) {
let shaderExpression = this.getShaderExpression(
variableSubstitutionMap,
shaderState,
);
shaderExpression =
`${returnType} ${functionSignature}\n` +
`{\n` +
` return ${shaderExpression};\n` +
`}\n`;
return shaderExpression;
};
/**
* Gets the shader expression for this expression.
* Returns undefined if the shader expression can't be generated from this expression.
*
* @param {object} variableSubstitutionMap Maps variable names to shader variable names.
* @param {object} shaderState Stores information about the generated shader function, including whether it is translucent.
*
* @returns {string} The shader expression.
*
* @private
*/
Expression.prototype.getShaderExpression = function (
variableSubstitutionMap,
shaderState,
) {
return this._runtimeAst.getShaderExpression(
variableSubstitutionMap,
shaderState,
);
};
/**
* Gets the variables used by the expression.
*
* @returns {string[]} The variables used by the expression.
*
* @private
*/
Expression.prototype.getVariables = function () {
let variables = [];
this._runtimeAst.getVariables(variables);
// Remove duplicates
variables = variables.filter(function (variable, index, variables) {
return variables.indexOf(variable) === index;
});
return variables;
};
const unaryOperators = ["!", "-", "+"];
const binaryOperators = [
"+",
"-",
"*",
"/",
"%",
"===",
"!==",
">",
">=",
"<",
"<=",
"&&",
"||",
"!~",
"=~",
];
const variableRegex = /\${(.*?)}/g; // Matches ${variable_name}
const backslashRegex = /\\/g;
const backslashReplacement = "@#%";
const replacementRegex = /@#%/g;
const scratchColor = new Color();
const unaryFunctions = {
abs: getEvaluateUnaryComponentwise(Math.abs),
sqrt: getEvaluateUnaryComponentwise(Math.sqrt),
cos: getEvaluateUnaryComponentwise(Math.cos),
sin: getEvaluateUnaryComponentwise(Math.sin),
tan: getEvaluateUnaryComponentwise(Math.tan),
acos: getEvaluateUnaryComponentwise(Math.acos),
asin: getEvaluateUnaryComponentwise(Math.asin),
atan: getEvaluateUnaryComponentwise(Math.atan),
radians: getEvaluateUnaryComponentwise(CesiumMath.toRadians),
degrees: getEvaluateUnaryComponentwise(CesiumMath.toDegrees),
sign: getEvaluateUnaryComponentwise(CesiumMath.sign),
floor: getEvaluateUnaryComponentwise(Math.floor),
ceil: getEvaluateUnaryComponentwise(Math.ceil),
round: getEvaluateUnaryComponentwise(Math.round),
exp: getEvaluateUnaryComponentwise(Math.exp),
exp2: getEvaluateUnaryComponentwise(exp2),
log: getEvaluateUnaryComponentwise(Math.log),
log2: getEvaluateUnaryComponentwise(log2),
fract: getEvaluateUnaryComponentwise(fract),
length: length,
normalize: normalize,
};
const binaryFunctions = {
atan2: getEvaluateBinaryComponentwise(Math.atan2, false),
pow: getEvaluateBinaryComponentwise(Math.pow, false),
min: getEvaluateBinaryComponentwise(Math.min, true),
max: getEvaluateBinaryComponentwise(Math.max, true),
distance: distance,
dot: dot,
cross: cross,
};
const ternaryFunctions = {
clamp: getEvaluateTernaryComponentwise(CesiumMath.clamp, true),
mix: getEvaluateTernaryComponentwise(CesiumMath.lerp, true),
};
function fract(number) {
return number - Math.floor(number);
}
function exp2(exponent) {
return Math.pow(2.0, exponent);
}
function log2(number) {
return CesiumMath.log2(number);
}
function getEvaluateUnaryComponentwise(operation) {
return function (call, left) {
if (typeof left === "number") {
return operation(left);
} else if (left instanceof Cartesian2) {
return Cartesian2.fromElements(
operation(left.x),
operation(left.y),
scratchStorage.getCartesian2(),
);
} else if (left instanceof Cartesian3) {
return Cartesian3.fromElements(
operation(left.x),
operation(left.y),
operation(left.z),
scratchStorage.getCartesian3(),
);
} else if (left instanceof Cartesian4) {
return Cartesian4.fromElements(
operation(left.x),
operation(left.y),
operation(left.z),
operation(left.w),
scratchStorage.getCartesian4(),
);
}
throw new RuntimeError(
`Function "${call}" requires a vector or number argument. Argument is ${left}.`,
);
};
}
function getEvaluateBinaryComponentwise(operation, allowScalar) {
return function (call, left, right) {
if (allowScalar && typeof right === "number") {
if (typeof left === "number") {
return operation(left, right);
} else if (left instanceof Cartesian2) {
return Cartesian2.fromElements(
operation(left.x, right),
operation(left.y, right),
scratchStorage.getCartesian2(),
);
} else Eif (left instanceof Cartesian3) {
return Cartesian3.fromElements(
operation(left.x, right),
operation(left.y, right),
operation(left.z, right),
scratchStorage.getCartesian3(),
);
} else if (left instanceof Cartesian4) {
return Cartesian4.fromElements(
operation(left.x, right),
operation(left.y, right),
operation(left.z, right),
operation(left.w, right),
scratchStorage.getCartesian4(),
);
}
}
if (typeof left === "number" && typeof right === "number") {
return operation(left, right);
} else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
return Cartesian2.fromElements(
operation(left.x, right.x),
operation(left.y, right.y),
scratchStorage.getCartesian2(),
);
} else if (left instanceof Cartesian3 && right instanceof Cartesian3) {
return Cartesian3.fromElements(
operation(left.x, right.x),
operation(left.y, right.y),
operation(left.z, right.z),
scratchStorage.getCartesian3(),
);
} else if (left instanceof Cartesian4 && right instanceof Cartesian4) {
return Cartesian4.fromElements(
operation(left.x, right.x),
operation(left.y, right.y),
operation(left.z, right.z),
operation(left.w, right.w),
scratchStorage.getCartesian4(),
);
}
throw new RuntimeError(
`Function "${call}" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`,
);
};
}
function getEvaluateTernaryComponentwise(operation, allowScalar) {
return function (call, left, right, test) {
if (allowScalar && typeof test === "number") {
if (typeof left === "number" && typeof right === "number") {
return operation(left, right, test);
} else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
return Cartesian2.fromElements(
operation(left.x, right.x, test),
operation(left.y, right.y, test),
scratchStorage.getCartesian2(),
);
} else Iif (left instanceof Cartesian3 && right instanceof Cartesian3) {
return Cartesian3.fromElements(
operation(left.x, right.x, test),
operation(left.y, right.y, test),
operation(left.z, right.z, test),
scratchStorage.getCartesian3(),
);
} else Iif (left instanceof Cartesian4 && right instanceof Cartesian4) {
return Cartesian4.fromElements(
operation(left.x, right.x, test),
operation(left.y, right.y, test),
operation(left.z, right.z, test),
operation(left.w, right.w, test),
scratchStorage.getCartesian4(),
);
}
}
Iif (
typeof left === "number" &&
typeof right === "number" &&
typeof test === "number"
) {
return operation(left, right, test);
} else if (
left instanceof Cartesian2 &&
right instanceof Cartesian2 &&
test instanceof Cartesian2
) {
return Cartesian2.fromElements(
operation(left.x, right.x, test.x),
operation(left.y, right.y, test.y),
scratchStorage.getCartesian2(),
);
} else if (
left instanceof Cartesian3 &&
right instanceof Cartesian3 &&
test instanceof Cartesian3
) {
return Cartesian3.fromElements(
operation(left.x, right.x, test.x),
operation(left.y, right.y, test.y),
operation(left.z, right.z, test.z),
scratchStorage.getCartesian3(),
);
} else if (
left instanceof Cartesian4 &&
right instanceof Cartesian4 &&
test instanceof Cartesian4
) {
return Cartesian4.fromElements(
operation(left.x, right.x, test.x),
operation(left.y, right.y, test.y),
operation(left.z, right.z, test.z),
operation(left.w, right.w, test.w),
scratchStorage.getCartesian4(),
);
}
throw new RuntimeError(
`Function "${call}" requires vector or number arguments of matching types. Arguments are ${left}, ${right}, and ${test}.`,
);
};
}
function length(call, left) {
if (typeof left === "number") {
return Math.abs(left);
} else if (left instanceof Cartesian2) {
return Cartesian2.magnitude(left);
} else if (left instanceof Cartesian3) {
return Cartesian3.magnitude(left);
} else Eif (left instanceof Cartesian4) {
return Cartesian4.magnitude(left);
}
throw new RuntimeError(
`Function "${call}" requires a vector or number argument. Argument is ${left}.`,
);
}
function normalize(call, left) {
if (typeof left === "number") {
return 1.0;
} else if (left instanceof Cartesian2) {
return Cartesian2.normalize(left, scratchStorage.getCartesian2());
} else if (left instanceof Cartesian3) {
return Cartesian3.normalize(left, scratchStorage.getCartesian3());
} else Eif (left instanceof Cartesian4) {
return Cartesian4.normalize(left, scratchStorage.getCartesian4());
}
throw new RuntimeError(
`Function "${call}" requires a vector or number argument. Argument is ${left}.`,
);
}
function distance(call, left, right) {
if (typeof left === "number" && typeof right === "number") {
return Math.abs(left - right);
} else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
return Cartesian2.distance(left, right);
} else if (left instanceof Cartesian3 && right instanceof Cartesian3) {
return Cartesian3.distance(left, right);
} else if (left instanceof Cartesian4 && right instanceof Cartesian4) {
return Cartesian4.distance(left, right);
}
throw new RuntimeError(
`Function "${call}" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`,
);
}
function dot(call, left, right) {
if (typeof left === "number" && typeof right === "number") {
return left * right;
} else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
return Cartesian2.dot(left, right);
} else if (left instanceof Cartesian3 && right instanceof Cartesian3) {
return Cartesian3.dot(left, right);
} else if (left instanceof Cartesian4 && right instanceof Cartesian4) {
return Cartesian4.dot(left, right);
}
throw new RuntimeError(
`Function "${call}" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`,
);
}
function cross(call, left, right) {
if (left instanceof Cartesian3 && right instanceof Cartesian3) {
return Cartesian3.cross(left, right, scratchStorage.getCartesian3());
}
throw new RuntimeError(
`Function "${call}" requires vec3 arguments. Arguments are ${left} and ${right}.`,
);
}
function Node(type, value, left, right, test) {
this._type = type;
this._value = value;
this._left = left;
this._right = right;
this._test = test;
this.evaluate = undefined;
setEvaluateFunction(this);
}
function replaceDefines(expression, defines) {
if (!defined(defines)) {
return expression;
}
for (const key in defines) {
Eif (defines.hasOwnProperty(key)) {
const definePlaceholder = new RegExp(`\\$\\{${key}\\}`, "g");
const defineReplace = `(${defines[key]})`;
Eif (defined(defineReplace)) {
expression = expression.replace(definePlaceholder, defineReplace);
}
}
}
return expression;
}
function removeBackslashes(expression) {
return expression.replace(backslashRegex, backslashReplacement);
}
function replaceBackslashes(expression) {
return expression.replace(replacementRegex, "\\");
}
function replaceVariables(expression) {
let exp = expression;
let result = "";
let i = exp.indexOf("${");
while (i >= 0) {
// Check if string is inside quotes
const openSingleQuote = exp.indexOf("'");
const openDoubleQuote = exp.indexOf('"');
let closeQuote;
if (openSingleQuote >= 0 && openSingleQuote < i) {
closeQuote = exp.indexOf("'", openSingleQuote + 1);
result += exp.substr(0, closeQuote + 1);
exp = exp.substr(closeQuote + 1);
i = exp.indexOf("${");
} else if (openDoubleQuote >= 0 && openDoubleQuote < i) {
closeQuote = exp.indexOf('"', openDoubleQuote + 1);
result += exp.substr(0, closeQuote + 1);
exp = exp.substr(closeQuote + 1);
i = exp.indexOf("${");
} else {
result += exp.substr(0, i);
const j = exp.indexOf("}");
if (j < 0) {
throw new RuntimeError("Unmatched {.");
}
result += `czm_${exp.substr(i + 2, j - (i + 2))}`;
exp = exp.substr(j + 1);
i = exp.indexOf("${");
}
}
result += exp;
return result;
}
function parseLiteral(ast) {
const type = typeof ast.value;
if (ast.value === null) {
return new Node(ExpressionNodeType.LITERAL_NULL, null);
} else if (type === "boolean") {
return new Node(ExpressionNodeType.LITERAL_BOOLEAN, ast.value);
} else if (type === "number") {
return new Node(ExpressionNodeType.LITERAL_NUMBER, ast.value);
} else Eif (type === "string") {
if (ast.value.indexOf("${") >= 0) {
return new Node(ExpressionNodeType.VARIABLE_IN_STRING, ast.value);
}
return new Node(
ExpressionNodeType.LITERAL_STRING,
replaceBackslashes(ast.value),
);
}
}
function parseCall(expression, ast) {
const args = ast.arguments;
const argsLength = args.length;
let call;
let val, left, right;
// Member function calls
if (ast.callee.type === "MemberExpression") {
call = ast.callee.property.name;
const object = ast.callee.object;
if (call === "test" || call === "exec") {
// Make sure this is called on a valid type
if (!defined(object.callee) || object.callee.name !== "regExp") {
throw new RuntimeError(`${call} is not a function.`);
}
if (argsLength === 0) {
if (call === "test") {
return new Node(ExpressionNodeType.LITERAL_BOOLEAN, false);
}
return new Node(ExpressionNodeType.LITERAL_NULL, null);
}
left = createRuntimeAst(expression, object);
right = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.FUNCTION_CALL, call, left, right);
} else if (call === "toString") {
val = createRuntimeAst(expression, object);
return new Node(ExpressionNodeType.FUNCTION_CALL, call, val);
}
throw new RuntimeError(`Unexpected function call "${call}".`);
}
// Non-member function calls
call = ast.callee.name;
if (call === "color") {
if (argsLength === 0) {
return new Node(ExpressionNodeType.LITERAL_COLOR, call);
}
val = createRuntimeAst(expression, args[0]);
if (defined(args[1])) {
const alpha = createRuntimeAst(expression, args[1]);
return new Node(ExpressionNodeType.LITERAL_COLOR, call, [val, alpha]);
}
return new Node(ExpressionNodeType.LITERAL_COLOR, call, [val]);
} else if (call === "rgb" || call === "hsl") {
if (argsLength < 3) {
throw new RuntimeError(`${call} requires three arguments.`);
}
val = [
createRuntimeAst(expression, args[0]),
createRuntimeAst(expression, args[1]),
createRuntimeAst(expression, args[2]),
];
return new Node(ExpressionNodeType.LITERAL_COLOR, call, val);
} else if (call === "rgba" || call === "hsla") {
if (argsLength < 4) {
throw new RuntimeError(`${call} requires four arguments.`);
}
val = [
createRuntimeAst(expression, args[0]),
createRuntimeAst(expression, args[1]),
createRuntimeAst(expression, args[2]),
createRuntimeAst(expression, args[3]),
];
return new Node(ExpressionNodeType.LITERAL_COLOR, call, val);
} else if (call === "vec2" || call === "vec3" || call === "vec4") {
// Check for invalid constructors at evaluation time
val = new Array(argsLength);
for (let i = 0; i < argsLength; ++i) {
val[i] = createRuntimeAst(expression, args[i]);
}
return new Node(ExpressionNodeType.LITERAL_VECTOR, call, val);
} else if (call === "isNaN" || call === "isFinite") {
if (argsLength === 0) {
if (call === "isNaN") {
return new Node(ExpressionNodeType.LITERAL_BOOLEAN, true);
}
return new Node(ExpressionNodeType.LITERAL_BOOLEAN, false);
}
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (call === "isExactClass" || call === "isClass") {
if (argsLength < 1 || argsLength > 1) {
throw new RuntimeError(`${call} requires exactly one argument.`);
}
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (call === "getExactClassName") {
if (argsLength > 0) {
throw new RuntimeError(`${call} does not take any argument.`);
}
return new Node(ExpressionNodeType.UNARY, call);
} else if (defined(unaryFunctions[call])) {
if (argsLength !== 1) {
throw new RuntimeError(`${call} requires exactly one argument.`);
}
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (defined(binaryFunctions[call])) {
if (argsLength !== 2) {
throw new RuntimeError(`${call} requires exactly two arguments.`);
}
left = createRuntimeAst(expression, args[0]);
right = createRuntimeAst(expression, args[1]);
return new Node(ExpressionNodeType.BINARY, call, left, right);
} else if (defined(ternaryFunctions[call])) {
if (argsLength !== 3) {
throw new RuntimeError(`${call} requires exactly three arguments.`);
}
left = createRuntimeAst(expression, args[0]);
right = createRuntimeAst(expression, args[1]);
const test = createRuntimeAst(expression, args[2]);
return new Node(ExpressionNodeType.TERNARY, call, left, right, test);
} else if (call === "Boolean") {
if (argsLength === 0) {
return new Node(ExpressionNodeType.LITERAL_BOOLEAN, false);
}
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (call === "Number") {
if (argsLength === 0) {
return new Node(ExpressionNodeType.LITERAL_NUMBER, 0);
}
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (call === "String") {
if (argsLength === 0) {
return new Node(ExpressionNodeType.LITERAL_STRING, "");
}
val = createRuntimeAst(expression, args[0]);
return new Node(ExpressionNodeType.UNARY, call, val);
} else if (call === "regExp") {
return parseRegex(expression, ast);
}
throw new RuntimeError(`Unexpected function call "${call}".`);
}
function parseRegex(expression, ast) {
const args = ast.arguments;
// no arguments, return default regex
if (args.length === 0) {
return new Node(ExpressionNodeType.LITERAL_REGEX, new RegExp());
}
const pattern = createRuntimeAst(expression, args[0]);
let exp;
// optional flag argument supplied
if (args.length > 1) {
const flags = createRuntimeAst(expression, args[1]);
if (isLiteralType(pattern) && isLiteralType(flags)) {
try {
exp = new RegExp(
replaceBackslashes(String(pattern._value)),
flags._value,
);
} catch (e) {
throw new RuntimeError(e);
}
return new Node(ExpressionNodeType.LITERAL_REGEX, exp);
}
return new Node(ExpressionNodeType.REGEX, pattern, flags);
}
// only pattern argument supplied
if (isLiteralType(pattern)) {
try {
exp = new RegExp(replaceBackslashes(String(pattern._value)));
} catch (e) {
throw new RuntimeError(e);
}
return new Node(ExpressionNodeType.LITERAL_REGEX, exp);
}
return new Node(ExpressionNodeType.REGEX, pattern);
}
function parseKeywordsAndVariables(ast) {
if (isVariable(ast.name)) {
const name = getPropertyName(ast.name);
if (name.substr(0, 8) === "tiles3d_") {
return new Node(ExpressionNodeType.BUILTIN_VARIABLE, name);
}
return new Node(ExpressionNodeType.VARIABLE, name);
} else if (ast.name === "NaN") {
return new Node(ExpressionNodeType.LITERAL_NUMBER, NaN);
} else if (ast.name === "Infinity") {
return new Node(ExpressionNodeType.LITERAL_NUMBER, Infinity);
} else if (ast.name === "undefined") {
return new Node(ExpressionNodeType.LITERAL_UNDEFINED, undefined);
}
throw new RuntimeError(`${ast.name} is not defined.`);
}
function parseMathConstant(ast) {
const name = ast.property.name;
if (name === "PI") {
return new Node(ExpressionNodeType.LITERAL_NUMBER, Math.PI);
} else Eif (name === "E") {
return new Node(ExpressionNodeType.LITERAL_NUMBER, Math.E);
}
}
function parseNumberConstant(ast) {
const name = ast.property.name;
Eif (name === "POSITIVE_INFINITY") {
return new Node(
ExpressionNodeType.LITERAL_NUMBER,
Number.POSITIVE_INFINITY,
);
}
}
function parseMemberExpression(expression, ast) {
if (ast.object.name === "Math") {
return parseMathConstant(ast);
} else if (ast.object.name === "Number") {
return parseNumberConstant(ast);
}
let val;
const obj = createRuntimeAst(expression, ast.object);
if (ast.computed) {
val = createRuntimeAst(expression, ast.property);
return new Node(ExpressionNodeType.MEMBER, "brackets", obj, val);
}
val = new Node(ExpressionNodeType.LITERAL_STRING, ast.property.name);
return new Node(ExpressionNodeType.MEMBER, "dot", obj, val);
}
function isLiteralType(node) {
return node._type >= ExpressionNodeType.LITERAL_NULL;
}
function isVariable(name) {
return name.substr(0, 4) === "czm_";
}
function getPropertyName(variable) {
return variable.substr(4);
}
function createRuntimeAst(expression, ast) {
let node;
let op;
let left;
let right;
if (ast.type === "Literal") {
node = parseLiteral(ast);
} else if (ast.type === "CallExpression") {
node = parseCall(expression, ast);
} else if (ast.type === "Identifier") {
node = parseKeywordsAndVariables(ast);
} else if (ast.type === "UnaryExpression") {
op = ast.operator;
const child = createRuntimeAst(expression, ast.argument);
if (unaryOperators.indexOf(op) > -1) {
node = new Node(ExpressionNodeType.UNARY, op, child);
} else {
throw new RuntimeError(`Unexpected operator "${op}".`);
}
} else if (ast.type === "BinaryExpression") {
op = ast.operator;
left = createRuntimeAst(expression, ast.left);
right = createRuntimeAst(expression, ast.right);
if (binaryOperators.indexOf(op) > -1) {
node = new Node(ExpressionNodeType.BINARY, op, left, right);
} else {
throw new RuntimeError(`Unexpected operator "${op}".`);
}
} else Iif (ast.type === "LogicalExpression") {
op = ast.operator;
left = createRuntimeAst(expression, ast.left);
right = createRuntimeAst(expression, ast.right);
if (binaryOperators.indexOf(op) > -1) {
node = new Node(ExpressionNodeType.BINARY, op, left, right);
}
} else if (ast.type === "ConditionalExpression") {
const test = createRuntimeAst(expression, ast.test);
left = createRuntimeAst(expression, ast.consequent);
right = createRuntimeAst(expression, ast.alternate);
node = new Node(ExpressionNodeType.CONDITIONAL, "?", left, right, test);
} else if (ast.type === "MemberExpression") {
node = parseMemberExpression(expression, ast);
} else if (ast.type === "ArrayExpression") {
const val = [];
for (let i = 0; i < ast.elements.length; i++) {
val[i] = createRuntimeAst(expression, ast.elements[i]);
}
node = new Node(ExpressionNodeType.ARRAY, val);
} else if (ast.type === "Compound") {
// empty expression or multiple expressions
throw new RuntimeError("Provide exactly one expression.");
} else {
throw new RuntimeError("Cannot parse expression.");
}
return node;
}
function setEvaluateFunction(node) {
if (node._type === ExpressionNodeType.CONDITIONAL) {
node.evaluate = node._evaluateConditional;
} else if (node._type === ExpressionNodeType.FUNCTION_CALL) {
if (node._value === "test") {
node.evaluate = node._evaluateRegExpTest;
} else if (node._value === "exec") {
node.evaluate = node._evaluateRegExpExec;
} else Eif (node._value === "toString") {
node.evaluate = node._evaluateToString;
}
} else if (node._type === ExpressionNodeType.UNARY) {
if (node._value === "!") {
node.evaluate = node._evaluateNot;
} else if (node._value === "-") {
node.evaluate = node._evaluateNegative;
} else if (node._value === "+") {
node.evaluate = node._evaluatePositive;
} else if (node._value === "isNaN") {
node.evaluate = node._evaluateNaN;
} else if (node._value === "isFinite") {
node.evaluate = node._evaluateIsFinite;
} else if (node._value === "isExactClass") {
node.evaluate = node._evaluateIsExactClass;
} else if (node._value === "isClass") {
node.evaluate = node._evaluateIsClass;
} else if (node._value === "getExactClassName") {
node.evaluate = node._evaluateGetExactClassName;
} else if (node._value === "Boolean") {
node.evaluate = node._evaluateBooleanConversion;
} else if (node._value === "Number") {
node.evaluate = node._evaluateNumberConversion;
} else if (node._value === "String") {
node.evaluate = node._evaluateStringConversion;
} else Eif (defined(unaryFunctions[node._value])) {
node.evaluate = getEvaluateUnaryFunction(node._value);
}
} else if (node._type === ExpressionNodeType.BINARY) {
if (node._value === "+") {
node.evaluate = node._evaluatePlus;
} else if (node._value === "-") {
node.evaluate = node._evaluateMinus;
} else if (node._value === "*") {
node.evaluate = node._evaluateTimes;
} else if (node._value === "/") {
node.evaluate = node._evaluateDivide;
} else if (node._value === "%") {
node.evaluate = node._evaluateMod;
} else if (node._value === "===") {
node.evaluate = node._evaluateEqualsStrict;
} else if (node._value === "!==") {
node.evaluate = node._evaluateNotEqualsStrict;
} else if (node._value === "<") {
node.evaluate = node._evaluateLessThan;
} else if (node._value === "<=") {
node.evaluate = node._evaluateLessThanOrEquals;
} else if (node._value === ">") {
node.evaluate = node._evaluateGreaterThan;
} else if (node._value === ">=") {
node.evaluate = node._evaluateGreaterThanOrEquals;
} else if (node._value === "&&") {
node.evaluate = node._evaluateAnd;
} else if (node._value === "||") {
node.evaluate = node._evaluateOr;
} else if (node._value === "=~") {
node.evaluate = node._evaluateRegExpMatch;
} else if (node._value === "!~") {
node.evaluate = node._evaluateRegExpNotMatch;
} else Eif (defined(binaryFunctions[node._value])) {
node.evaluate = getEvaluateBinaryFunction(node._value);
}
} else if (node._type === ExpressionNodeType.TERNARY) {
node.evaluate = getEvaluateTernaryFunction(node._value);
} else if (node._type === ExpressionNodeType.MEMBER) {
if (node._value === "brackets") {
node.evaluate = node._evaluateMemberBrackets;
} else {
node.evaluate = node._evaluateMemberDot;
}
} else if (node._type === ExpressionNodeType.ARRAY) {
node.evaluate = node._evaluateArray;
} else if (node._type === ExpressionNodeType.VARIABLE) {
node.evaluate = node._evaluateVariable;
} else if (node._type === ExpressionNodeType.VARIABLE_IN_STRING) {
node.evaluate = node._evaluateVariableString;
} else if (node._type === ExpressionNodeType.LITERAL_COLOR) {
node.evaluate = node._evaluateLiteralColor;
} else if (node._type === ExpressionNodeType.LITERAL_VECTOR) {
node.evaluate = node._evaluateLiteralVector;
} else if (node._type === ExpressionNodeType.LITERAL_STRING) {
node.evaluate = node._evaluateLiteralString;
} else if (node._type === ExpressionNodeType.REGEX) {
node.evaluate = node._evaluateRegExp;
} else if (node._type === ExpressionNodeType.BUILTIN_VARIABLE) {
Eif (node._value === "tiles3d_tileset_time") {
node.evaluate = evaluateTilesetTime;
}
} else {
node.evaluate = node._evaluateLiteral;
}
}
function evaluateTilesetTime(feature) {
if (!defined(feature)) {
return 0.0;
}
return feature.content.tileset.timeSinceLoad;
}
function getEvaluateUnaryFunction(call) {
const evaluate = unaryFunctions[call];
return function (feature) {
const left = this._left.evaluate(feature);
return evaluate(call, left);
};
}
function getEvaluateBinaryFunction(call) {
const evaluate = binaryFunctions[call];
return function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
return evaluate(call, left, right);
};
}
function getEvaluateTernaryFunction(call) {
const evaluate = ternaryFunctions[call];
return function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
const test = this._test.evaluate(feature);
return evaluate(call, left, right, test);
};
}
function getFeatureProperty(feature, name) {
// Returns undefined if the feature is not defined or the property name is not defined for that feature
if (defined(feature)) {
return feature.getPropertyInherited(name);
}
}
Node.prototype._evaluateLiteral = function () {
return this._value;
};
Node.prototype._evaluateLiteralColor = function (feature) {
const color = scratchColor;
const args = this._left;
if (this._value === "color") {
if (!defined(args)) {
Color.fromBytes(255, 255, 255, 255, color);
} else if (args.length > 1) {
Color.fromCssColorString(args[0].evaluate(feature), color);
color.alpha = args[1].evaluate(feature);
} else {
Color.fromCssColorString(args[0].evaluate(feature), color);
}
} else if (this._value === "rgb") {
Color.fromBytes(
args[0].evaluate(feature),
args[1].evaluate(feature),
args[2].evaluate(feature),
255,
color,
);
} else if (this._value === "rgba") {
// convert between css alpha (0 to 1) and cesium alpha (0 to 255)
const a = args[3].evaluate(feature) * 255;
Color.fromBytes(
args[0].evaluate(feature),
args[1].evaluate(feature),
args[2].evaluate(feature),
a,
color,
);
} else if (this._value === "hsl") {
Color.fromHsl(
args[0].evaluate(feature),
args[1].evaluate(feature),
args[2].evaluate(feature),
1.0,
color,
);
} else Eif (this._value === "hsla") {
Color.fromHsl(
args[0].evaluate(feature),
args[1].evaluate(feature),
args[2].evaluate(feature),
args[3].evaluate(feature),
color,
);
}
return Cartesian4.fromColor(color, scratchStorage.getCartesian4());
};
Node.prototype._evaluateLiteralVector = function (feature) {
// Gather the components that make up the vector, which includes components from interior vectors.
// For example vec3(1, 2, 3) or vec3(vec2(1, 2), 3) are both valid.
//
// If the number of components does not equal the vector's size, then a RuntimeError is thrown - with two exceptions:
// 1. A vector may be constructed from a larger vector and drop the extra components.
// 2. A vector may be constructed from a single component - vec3(1) will become vec3(1, 1, 1).
//
// Examples of invalid constructors include:
// vec4(1, 2) // not enough components
// vec3(vec2(1, 2)) // not enough components
// vec3(1, 2, 3, 4) // too many components
// vec2(vec4(1), 1) // too many components
const components = scratchStorage.getArray();
const call = this._value;
const args = this._left;
const argsLength = args.length;
for (let i = 0; i < argsLength; ++i) {
const value = args[i].evaluate(feature);
if (typeof value === "number") {
components.push(value);
} else if (value instanceof Cartesian2) {
components.push(value.x, value.y);
} else if (value instanceof Cartesian3) {
components.push(value.x, value.y, value.z);
} else if (value instanceof Cartesian4) {
components.push(value.x, value.y, value.z, value.w);
} else {
throw new RuntimeError(
`${call} argument must be a vector or number. Argument is ${value}.`,
);
}
}
const componentsLength = components.length;
const vectorLength = parseInt(call.charAt(3));
if (componentsLength === 0) {
throw new RuntimeError(`Invalid ${call} constructor. No valid arguments.`);
} else if (componentsLength < vectorLength && componentsLength > 1) {
throw new RuntimeError(
`Invalid ${call} constructor. Not enough arguments.`,
);
} else if (componentsLength > vectorLength && argsLength > 1) {
throw new RuntimeError(`Invalid ${call} constructor. Too many arguments.`);
}
if (componentsLength === 1) {
// Add the same component 3 more times
const component = components[0];
components.push(component, component, component);
}
if (call === "vec2") {
return Cartesian2.fromArray(components, 0, scratchStorage.getCartesian2());
} else if (call === "vec3") {
return Cartesian3.fromArray(components, 0, scratchStorage.getCartesian3());
} else Eif (call === "vec4") {
return Cartesian4.fromArray(components, 0, scratchStorage.getCartesian4());
}
};
Node.prototype._evaluateLiteralString = function () {
return this._value;
};
Node.prototype._evaluateVariableString = function (feature) {
let result = this._value;
let match = variableRegex.exec(result);
while (match !== null) {
const placeholder = match[0];
const variableName = match[1];
let property = getFeatureProperty(feature, variableName);
if (!defined(property)) {
property = "";
}
result = result.replace(placeholder, property);
variableRegex.lastIndex += property.length - placeholder.length;
match = variableRegex.exec(result);
}
return result;
};
Node.prototype._evaluateVariable = function (feature) {
// evaluates to undefined if the property name is not defined for that feature
return getFeatureProperty(feature, this._value);
};
function checkFeature(ast) {
return ast._value === "feature";
}
// PERFORMANCE_IDEA: Determine if parent property needs to be computed before runtime
Node.prototype._evaluateMemberDot = function (feature) {
if (checkFeature(this._left)) {
return getFeatureProperty(feature, this._right.evaluate(feature));
}
const property = this._left.evaluate(feature);
if (!defined(property)) {
return undefined;
}
const member = this._right.evaluate(feature);
if (
property instanceof Cartesian2 ||
property instanceof Cartesian3 ||
property instanceof Cartesian4
) {
// Vector components may be accessed with .r, .g, .b, .a and implicitly with .x, .y, .z, .w
if (member === "r") {
return property.x;
} else if (member === "g") {
return property.y;
} else if (member === "b") {
return property.z;
} else if (member === "a") {
return property.w;
}
}
return property[member];
};
Node.prototype._evaluateMemberBrackets = function (feature) {
if (checkFeature(this._left)) {
return getFeatureProperty(feature, this._right.evaluate(feature));
}
const property = this._left.evaluate(feature);
if (!defined(property)) {
return undefined;
}
const member = this._right.evaluate(feature);
if (
property instanceof Cartesian2 ||
property instanceof Cartesian3 ||
property instanceof Cartesian4
) {
// Vector components may be accessed with [0][1][2][3], ['r']['g']['b']['a'] and implicitly with ['x']['y']['z']['w']
// For Cartesian2 and Cartesian3 out-of-range components will just return undefined
if (member === 0 || member === "r") {
return property.x;
} else if (member === 1 || member === "g") {
return property.y;
} else if (member === 2 || member === "b") {
return property.z;
} else if (member === 3 || member === "a") {
return property.w;
}
}
return property[member];
};
Node.prototype._evaluateArray = function (feature) {
const array = [];
for (let i = 0; i < this._value.length; i++) {
array[i] = this._value[i].evaluate(feature);
}
return array;
};
// PERFORMANCE_IDEA: Have "fast path" functions that deal only with specific types
// that we can assign if we know the types before runtime
Node.prototype._evaluateNot = function (feature) {
const left = this._left.evaluate(feature);
if (typeof left !== "boolean") {
throw new RuntimeError(
`Operator "!" requires a boolean argument. Argument is ${left}.`,
);
}
return !left;
};
Node.prototype._evaluateNegative = function (feature) {
const left = this._left.evaluate(feature);
if (left instanceof Cartesian2) {
return Cartesian2.negate(left, scratchStorage.getCartesian2());
} else if (left instanceof Cartesian3) {
return Cartesian3.negate(left, scratchStorage.getCartesian3());
} else if (left instanceof Cartesian4) {
return Cartesian4.negate(left, scratchStorage.getCartesian4());
} else if (typeof left === "number") {
return -left;
}
throw new RuntimeError(
`Operator "-" requires a vector or number argument. Argument is ${left}.`,
);
};
Node.prototype._evaluatePositive = function (feature) {
const left = this._left.evaluate(feature);
if (
!(
left instanceof Cartesian2 ||
left instanceof Cartesian3 ||
left instanceof Cartesian4 ||
typeof left === "number"
)
) {
throw new RuntimeError(
`Operator "+" requires a vector or number argument. Argument is ${left}.`,
);
}
return left;
};
Node.prototype._evaluateLessThan = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (typeof left !== "number" || typeof right !== "number") {
throw new RuntimeError(
`Operator "<" requires number arguments. Arguments are ${left} and ${right}.`,
);
}
return left < right;
};
Node.prototype._evaluateLessThanOrEquals = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (typeof left !== "number" || typeof right !== "number") {
throw new RuntimeError(
`Operator "<=" requires number arguments. Arguments are ${left} and ${right}.`,
);
}
return left <= right;
};
Node.prototype._evaluateGreaterThan = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (typeof left !== "number" || typeof right !== "number") {
throw new RuntimeError(
`Operator ">" requires number arguments. Arguments are ${left} and ${right}.`,
);
}
return left > right;
};
Node.prototype._evaluateGreaterThanOrEquals = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (typeof left !== "number" || typeof right !== "number") {
throw new RuntimeError(
`Operator ">=" requires number arguments. Arguments are ${left} and ${right}.`,
);
}
return left >= right;
};
Node.prototype._evaluateOr = function (feature) {
const left = this._left.evaluate(feature);
if (typeof left !== "boolean") {
throw new RuntimeError(
`Operator "||" requires boolean arguments. First argument is ${left}.`,
);
}
// short circuit the expression
if (left) {
return true;
}
const right = this._right.evaluate(feature);
if (typeof right !== "boolean") {
throw new RuntimeError(
`Operator "||" requires boolean arguments. Second argument is ${right}.`,
);
}
return left || right;
};
Node.prototype._evaluateAnd = function (feature) {
const left = this._left.evaluate(feature);
if (typeof left !== "boolean") {
throw new RuntimeError(
`Operator "&&" requires boolean arguments. First argument is ${left}.`,
);
}
// short circuit the expression
if (!left) {
return false;
}
const right = this._right.evaluate(feature);
if (typeof right !== "boolean") {
throw new RuntimeError(
`Operator "&&" requires boolean arguments. Second argument is ${right}.`,
);
}
return left && right;
};
Node.prototype._evaluatePlus = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (right instanceof Cartesian2 && left instanceof Cartesian2) {
return Cartesian2.add(left, right, scratchStorage.getCartesian2());
} else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
return Cartesian3.add(left, right, scratchStorage.getCartesian3());
} else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
return Cartesian4.add(left, right, scratchStorage.getCartesian4());
} else if (typeof left === "string" || typeof right === "string") {
// If only one argument is a string the other argument calls its toString function.
return left + right;
} else if (typeof left === "number" && typeof right === "number") {
return left + right;
}
throw new RuntimeError(
`Operator "+" requires vector or number arguments of matching types, or at least one string argument. Arguments are ${left} and ${right}.`,
);
};
Node.prototype._evaluateMinus = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (right instanceof Cartesian2 && left instanceof Cartesian2) {
return Cartesian2.subtract(left, right, scratchStorage.getCartesian2());
} else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
return Cartesian3.subtract(left, right, scratchStorage.getCartesian3());
} else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
return Cartesian4.subtract(left, right, scratchStorage.getCartesian4());
} else if (typeof left === "number" && typeof right === "number") {
return left - right;
}
throw new RuntimeError(
`Operator "-" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`,
);
};
Node.prototype._evaluateTimes = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (right instanceof Cartesian2 && left instanceof Cartesian2) {
return Cartesian2.multiplyComponents(
left,
right,
scratchStorage.getCartesian2(),
);
} else if (right instanceof Cartesian2 && typeof left === "number") {
return Cartesian2.multiplyByScalar(
right,
left,
scratchStorage.getCartesian2(),
);
} else if (left instanceof Cartesian2 && typeof right === "number") {
return Cartesian2.multiplyByScalar(
left,
right,
scratchStorage.getCartesian2(),
);
} else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
return Cartesian3.multiplyComponents(
left,
right,
scratchStorage.getCartesian3(),
);
} else if (right instanceof Cartesian3 && typeof left === "number") {
return Cartesian3.multiplyByScalar(
right,
left,
scratchStorage.getCartesian3(),
);
} else if (left instanceof Cartesian3 && typeof right === "number") {
return Cartesian3.multiplyByScalar(
left,
right,
scratchStorage.getCartesian3(),
);
} else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
return Cartesian4.multiplyComponents(
left,
right,
scratchStorage.getCartesian4(),
);
} else if (right instanceof Cartesian4 && typeof left === "number") {
return Cartesian4.multiplyByScalar(
right,
left,
scratchStorage.getCartesian4(),
);
} else if (left instanceof Cartesian4 && typeof right === "number") {
return Cartesian4.multiplyByScalar(
left,
right,
scratchStorage.getCartesian4(),
);
} else if (typeof left === "number" && typeof right === "number") {
return left * right;
}
throw new RuntimeError(
`Operator "*" requires vector or number arguments. If both arguments are vectors they must be matching types. Arguments are ${left} and ${right}.`,
);
};
Node.prototype._evaluateDivide = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (right instanceof Cartesian2 && left instanceof Cartesian2) {
return Cartesian2.divideComponents(
left,
right,
scratchStorage.getCartesian2(),
);
} else if (left instanceof Cartesian2 && typeof right === "number") {
return Cartesian2.divideByScalar(
left,
right,
scratchStorage.getCartesian2(),
);
} else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
return Cartesian3.divideComponents(
left,
right,
scratchStorage.getCartesian3(),
);
} else if (left instanceof Cartesian3 && typeof right === "number") {
return Cartesian3.divideByScalar(
left,
right,
scratchStorage.getCartesian3(),
);
} else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
return Cartesian4.divideComponents(
left,
right,
scratchStorage.getCartesian4(),
);
} else if (left instanceof Cartesian4 && typeof right === "number") {
return Cartesian4.divideByScalar(
left,
right,
scratchStorage.getCartesian4(),
);
} else if (typeof left === "number" && typeof right === "number") {
return left / right;
}
throw new RuntimeError(
`Operator "/" requires vector or number arguments of matching types, or a number as the second argument. Arguments are ${left} and ${right}.`,
);
};
Node.prototype._evaluateMod = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (right instanceof Cartesian2 && left instanceof Cartesian2) {
return Cartesian2.fromElements(
left.x % right.x,
left.y % right.y,
scratchStorage.getCartesian2(),
);
} else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
return Cartesian3.fromElements(
left.x % right.x,
left.y % right.y,
left.z % right.z,
scratchStorage.getCartesian3(),
);
} else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
return Cartesian4.fromElements(
left.x % right.x,
left.y % right.y,
left.z % right.z,
left.w % right.w,
scratchStorage.getCartesian4(),
);
} else if (typeof left === "number" && typeof right === "number") {
return left % right;
}
throw new RuntimeError(
`Operator "%" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`,
);
};
Node.prototype._evaluateEqualsStrict = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (
(right instanceof Cartesian2 && left instanceof Cartesian2) ||
(right instanceof Cartesian3 && left instanceof Cartesian3) ||
(right instanceof Cartesian4 && left instanceof Cartesian4)
) {
return left.equals(right);
}
return left === right;
};
Node.prototype._evaluateNotEqualsStrict = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (
(right instanceof Cartesian2 && left instanceof Cartesian2) ||
(right instanceof Cartesian3 && left instanceof Cartesian3) ||
(right instanceof Cartesian4 && left instanceof Cartesian4)
) {
return !left.equals(right);
}
return left !== right;
};
Node.prototype._evaluateConditional = function (feature) {
const test = this._test.evaluate(feature);
Iif (typeof test !== "boolean") {
throw new RuntimeError(
`Conditional argument of conditional expression must be a boolean. Argument is ${test}.`,
);
}
if (test) {
return this._left.evaluate(feature);
}
return this._right.evaluate(feature);
};
Node.prototype._evaluateNaN = function (feature) {
return isNaN(this._left.evaluate(feature));
};
Node.prototype._evaluateIsFinite = function (feature) {
return isFinite(this._left.evaluate(feature));
};
Node.prototype._evaluateIsExactClass = function (feature) {
if (defined(feature)) {
return feature.isExactClass(this._left.evaluate(feature));
}
return false;
};
Node.prototype._evaluateIsClass = function (feature) {
if (defined(feature)) {
return feature.isClass(this._left.evaluate(feature));
}
return false;
};
Node.prototype._evaluateGetExactClassName = function (feature) {
if (defined(feature)) {
return feature.getExactClassName();
}
};
Node.prototype._evaluateBooleanConversion = function (feature) {
return Boolean(this._left.evaluate(feature));
};
Node.prototype._evaluateNumberConversion = function (feature) {
return Number(this._left.evaluate(feature));
};
Node.prototype._evaluateStringConversion = function (feature) {
return String(this._left.evaluate(feature));
};
Node.prototype._evaluateRegExp = function (feature) {
const pattern = this._value.evaluate(feature);
let flags = "";
if (defined(this._left)) {
flags = this._left.evaluate(feature);
}
let exp;
try {
exp = new RegExp(pattern, flags);
} catch (e) {
throw new RuntimeError(e);
}
return exp;
};
Node.prototype._evaluateRegExpTest = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (!(left instanceof RegExp && typeof right === "string")) {
throw new RuntimeError(
`RegExp.test requires the first argument to be a RegExp and the second argument to be a string. Arguments are ${left} and ${right}.`,
);
}
return left.test(right);
};
Node.prototype._evaluateRegExpMatch = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (left instanceof RegExp && typeof right === "string") {
return left.test(right);
} else if (right instanceof RegExp && typeof left === "string") {
return right.test(left);
}
throw new RuntimeError(
`Operator "=~" requires one RegExp argument and one string argument. Arguments are ${left} and ${right}.`,
);
};
Node.prototype._evaluateRegExpNotMatch = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (left instanceof RegExp && typeof right === "string") {
return !left.test(right);
} else if (right instanceof RegExp && typeof left === "string") {
return !right.test(left);
}
throw new RuntimeError(
`Operator "!~" requires one RegExp argument and one string argument. Arguments are ${left} and ${right}.`,
);
};
Node.prototype._evaluateRegExpExec = function (feature) {
const left = this._left.evaluate(feature);
const right = this._right.evaluate(feature);
if (!(left instanceof RegExp && typeof right === "string")) {
throw new RuntimeError(
`RegExp.exec requires the first argument to be a RegExp and the second argument to be a string. Arguments are ${left} and ${right}.`,
);
}
const exec = left.exec(right);
if (!defined(exec)) {
return null;
}
return exec[1];
};
Node.prototype._evaluateToString = function (feature) {
const left = this._left.evaluate(feature);
if (
left instanceof RegExp ||
left instanceof Cartesian2 ||
left instanceof Cartesian3 ||
left instanceof Cartesian4
) {
return String(left);
}
throw new RuntimeError(`Unexpected function call "${this._value}".`);
};
function convertHSLToRGB(ast) {
// Check if the color contains any nested expressions to see if the color can be converted here.
// E.g. "hsl(0.9, 0.6, 0.7)" is able to convert directly to rgb, "hsl(0.9, 0.6, ${Height})" is not.
const channels = ast._left;
const length = channels.length;
for (let i = 0; i < length; ++i) {
if (channels[i]._type !== ExpressionNodeType.LITERAL_NUMBER) {
return undefined;
}
}
const h = channels[0]._value;
const s = channels[1]._value;
const l = channels[2]._value;
const a = length === 4 ? channels[3]._value : 1.0;
return Color.fromHsl(h, s, l, a, scratchColor);
}
function convertRGBToColor(ast) {
// Check if the color contains any nested expressions to see if the color can be converted here.
// E.g. "rgb(255, 255, 255)" is able to convert directly to Color, "rgb(255, 255, ${Height})" is not.
const channels = ast._left;
const length = channels.length;
for (let i = 0; i < length; ++i) {
if (channels[i]._type !== ExpressionNodeType.LITERAL_NUMBER) {
return undefined;
}
}
const color = scratchColor;
color.red = channels[0]._value / 255.0;
color.green = channels[1]._value / 255.0;
color.blue = channels[2]._value / 255.0;
color.alpha = length === 4 ? channels[3]._value : 1.0;
return color;
}
function numberToString(number) {
if (number % 1 === 0) {
// Add a .0 to whole numbers
return number.toFixed(1);
}
return number.toString();
}
function colorToVec3(color) {
const r = numberToString(color.red);
const g = numberToString(color.green);
const b = numberToString(color.blue);
return `vec3(${r}, ${g}, ${b})`;
}
function colorToVec4(color) {
const r = numberToString(color.red);
const g = numberToString(color.green);
const b = numberToString(color.blue);
const a = numberToString(color.alpha);
return `vec4(${r}, ${g}, ${b}, ${a})`;
}
function getExpressionArray(
array,
variableSubstitutionMap,
shaderState,
parent,
) {
const length = array.length;
const expressions = new Array(length);
for (let i = 0; i < length; ++i) {
expressions[i] = array[i].getShaderExpression(
variableSubstitutionMap,
shaderState,
parent,
);
}
return expressions;
}
function getVariableName(variableName, variableSubstitutionMap) {
if (!defined(variableSubstitutionMap[variableName])) {
return Expression.NULL_SENTINEL;
}
return variableSubstitutionMap[variableName];
}
/**
* @private
*/
Expression.NULL_SENTINEL = "czm_infinity"; // null just needs to be some sentinel value that will cause "[expression] === null" to be false in nearly all cases. GLSL doesn't have a NaN constant so use czm_infinity.
Node.prototype.getShaderExpression = function (
variableSubstitutionMap,
shaderState,
parent,
) {
let color;
let left;
let right;
let test;
const type = this._type;
let value = this._value;
if (defined(this._left)) {
if (Array.isArray(this._left)) {
// Left can be an array if the type is LITERAL_COLOR or LITERAL_VECTOR
left = getExpressionArray(
this._left,
variableSubstitutionMap,
shaderState,
this,
);
} else {
left = this._left.getShaderExpression(
variableSubstitutionMap,
shaderState,
this,
);
}
}
if (defined(this._right)) {
right = this._right.getShaderExpression(
variableSubstitutionMap,
shaderState,
this,
);
}
if (defined(this._test)) {
test = this._test.getShaderExpression(
variableSubstitutionMap,
shaderState,
this,
);
}
if (Array.isArray(this._value)) {
// For ARRAY type
value = getExpressionArray(
this._value,
variableSubstitutionMap,
shaderState,
this,
);
}
let args;
let length;
let vectorExpression;
switch (type) {
case ExpressionNodeType.VARIABLE:
if (checkFeature(this)) {
return undefined;
}
return getVariableName(value, variableSubstitutionMap);
case ExpressionNodeType.UNARY:
// Supported types: +, -, !, Boolean, Number
if (value === "Boolean") {
return `bool(${left})`;
} else if (value === "Number") {
return `float(${left})`;
} else if (value === "round") {
return `floor(${left} + 0.5)`;
} else if (defined(unaryFunctions[value])) {
return `${value}(${left})`;
} else if (value === "isNaN") {
// In GLSL 2.0 use isnan instead
return `(${left} != ${left})`;
} else if (value === "isFinite") {
// In GLSL 2.0 use isinf instead. GLSL doesn't have an infinity constant so use czm_infinity which is an arbitrarily big enough number.
return `(abs(${left}) < czm_infinity)`;
} else if (
value === "String" ||
value === "isExactClass" ||
value === "isClass" ||
value === "getExactClassName"
) {
throw new RuntimeError(
`Error generating style shader: "${value}" is not supported.`,
);
}
return value + left;
case ExpressionNodeType.BINARY:
// Supported types: ||, &&, ===, !==, <, >, <=, >=, +, -, *, /, %
if (value === "%") {
return `mod(${left}, ${right})`;
} else if (value === "===") {
return `(${left} == ${right})`;
} else if (value === "!==") {
return `(${left} != ${right})`;
} else if (value === "atan2") {
return `atan(${left}, ${right})`;
} else if (defined(binaryFunctions[value])) {
return `${value}(${left}, ${right})`;
}
return `(${left} ${value} ${right})`;
case ExpressionNodeType.TERNARY:
Eif (defined(ternaryFunctions[value])) {
return `${value}(${left}, ${right}, ${test})`;
}
break;
case ExpressionNodeType.CONDITIONAL:
return `(${test} ? ${left} : ${right})`;
case ExpressionNodeType.MEMBER:
if (checkFeature(this._left)) {
return getVariableName(right, variableSubstitutionMap);
}
// This is intended for accessing the components of vector properties. String members aren't supported.
// Check for 0.0 rather than 0 because all numbers are previously converted to decimals.
if (right === "r" || right === "x" || right === "0.0") {
return `${left}[0]`;
} else if (right === "g" || right === "y" || right === "1.0") {
return `${left}[1]`;
} else if (right === "b" || right === "z" || right === "2.0") {
return `${left}[2]`;
} else if (right === "a" || right === "w" || right === "3.0") {
return `${left}[3]`;
}
return `${left}[int(${right})]`;
case ExpressionNodeType.FUNCTION_CALL:
throw new RuntimeError(
`Error generating style shader: "${value}" is not supported.`,
);
case ExpressionNodeType.ARRAY:
if (value.length === 4) {
return `vec4(${value[0]}, ${value[1]}, ${value[2]}, ${value[3]})`;
} else if (value.length === 3) {
return `vec3(${value[0]}, ${value[1]}, ${value[2]})`;
} else if (value.length === 2) {
return `vec2(${value[0]}, ${value[1]})`;
}
throw new RuntimeError(
"Error generating style shader: Invalid array length. Array length should be 2, 3, or 4.",
);
case ExpressionNodeType.REGEX:
throw new RuntimeError(
"Error generating style shader: Regular expressions are not supported.",
);
case ExpressionNodeType.VARIABLE_IN_STRING:
throw new RuntimeError(
"Error generating style shader: Converting a variable to a string is not supported.",
);
case ExpressionNodeType.LITERAL_NULL:
return Expression.NULL_SENTINEL;
case ExpressionNodeType.LITERAL_BOOLEAN:
return value ? "true" : "false";
case ExpressionNodeType.LITERAL_NUMBER:
return numberToString(value);
case ExpressionNodeType.LITERAL_STRING:
if (defined(parent) && parent._type === ExpressionNodeType.MEMBER) {
if (
value === "r" ||
value === "g" ||
value === "b" ||
value === "a" ||
value === "x" ||
value === "y" ||
value === "z" ||
value === "w" ||
checkFeature(parent._left)
) {
return value;
}
}
// Check for css color strings
color = Color.fromCssColorString(value, scratchColor);
if (defined(color)) {
return colorToVec3(color);
}
throw new RuntimeError(
"Error generating style shader: String literals are not supported.",
);
case ExpressionNodeType.LITERAL_COLOR:
args = left;
if (value === "color") {
if (!defined(args)) {
return "vec4(1.0)";
} else if (args.length > 1) {
const rgb = args[0];
const alpha = args[1];
Eif (alpha !== "1.0") {
shaderState.translucent = true;
}
return `vec4(${rgb}, ${alpha})`;
}
return `vec4(${args[0]}, 1.0)`;
} else if (value === "rgb") {
color = convertRGBToColor(this);
if (defined(color)) {
return colorToVec4(color);
}
return `vec4(${args[0]} / 255.0, ${args[1]} / 255.0, ${args[2]} / 255.0, 1.0)`;
} else if (value === "rgba") {
Eif (args[3] !== "1.0") {
shaderState.translucent = true;
}
color = convertRGBToColor(this);
if (defined(color)) {
return colorToVec4(color);
}
return `vec4(${args[0]} / 255.0, ${args[1]} / 255.0, ${args[2]} / 255.0, ${args[3]})`;
} else if (value === "hsl") {
color = convertHSLToRGB(this);
if (defined(color)) {
return colorToVec4(color);
}
return `vec4(czm_HSLToRGB(vec3(${args[0]}, ${args[1]}, ${args[2]})), 1.0)`;
} else Eif (value === "hsla") {
color = convertHSLToRGB(this);
if (defined(color)) {
Eif (color.alpha !== 1.0) {
shaderState.translucent = true;
}
return colorToVec4(color);
}
Eif (args[3] !== "1.0") {
shaderState.translucent = true;
}
return `vec4(czm_HSLToRGB(vec3(${args[0]}, ${args[1]}, ${args[2]})), ${args[3]})`;
}
break;
case ExpressionNodeType.LITERAL_VECTOR:
//>>includeStart('debug', pragmas.debug);
Iif (!defined(left)) {
throw new DeveloperError(
"left should always be defined for type ExpressionNodeType.LITERAL_VECTOR",
);
}
//>>includeEnd('debug');
length = left.length;
vectorExpression = `${value}(`;
for (let i = 0; i < length; ++i) {
vectorExpression += left[i];
if (i < length - 1) {
vectorExpression += ", ";
}
}
vectorExpression += ")";
return vectorExpression;
case ExpressionNodeType.LITERAL_REGEX:
throw new RuntimeError(
"Error generating style shader: Regular expressions are not supported.",
);
case ExpressionNodeType.LITERAL_UNDEFINED:
return Expression.NULL_SENTINEL;
case ExpressionNodeType.BUILTIN_VARIABLE:
Eif (value === "tiles3d_tileset_time") {
return value;
}
}
};
Node.prototype.getVariables = function (variables, parent) {
let array;
let length;
let i;
const type = this._type;
const value = this._value;
if (defined(this._left)) {
if (Array.isArray(this._left)) {
// Left can be an array if the type is LITERAL_COLOR or LITERAL_VECTOR
array = this._left;
length = array.length;
for (i = 0; i < length; ++i) {
array[i].getVariables(variables, this);
}
} else {
this._left.getVariables(variables, this);
}
}
if (defined(this._right)) {
this._right.getVariables(variables, this);
}
Iif (defined(this._test)) {
this._test.getVariables(variables, this);
}
Iif (Array.isArray(this._value)) {
// For ARRAY type
array = this._value;
length = array.length;
for (i = 0; i < length; ++i) {
array[i].getVariables(variables, this);
}
}
let match;
switch (type) {
case ExpressionNodeType.VARIABLE:
if (!checkFeature(this)) {
variables.push(value);
}
break;
case ExpressionNodeType.VARIABLE_IN_STRING:
match = variableRegex.exec(value);
while (match !== null) {
variables.push(match[1]);
match = variableRegex.exec(value);
}
break;
case ExpressionNodeType.LITERAL_STRING:
if (
defined(parent) &&
parent._type === ExpressionNodeType.MEMBER &&
checkFeature(parent._left)
) {
variables.push(value);
}
break;
}
};
export default Expression;
|