Expression.js 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Cartesian4 from "../Core/Cartesian4.js";
  4. import Check from "../Core/Check.js";
  5. import Color from "../Core/Color.js";
  6. import defined from "../Core/defined.js";
  7. import DeveloperError from "../Core/DeveloperError.js";
  8. import CesiumMath from "../Core/Math.js";
  9. import RuntimeError from "../Core/RuntimeError.js";
  10. import jsep from "jsep";
  11. import ExpressionNodeType from "./ExpressionNodeType.js";
  12. /**
  13. * An expression for a style applied to a {@link Cesium3DTileset}.
  14. * <p>
  15. * Evaluates an expression defined using the
  16. * {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Styling|3D Tiles Styling language}.
  17. * </p>
  18. * <p>
  19. * Implements the {@link StyleExpression} interface.
  20. * </p>
  21. *
  22. * @alias Expression
  23. * @constructor
  24. *
  25. * @param {string} [expression] The expression defined using the 3D Tiles Styling language.
  26. * @param {object} [defines] Defines in the style.
  27. *
  28. * @example
  29. * const expression = new Cesium.Expression('(regExp("^Chest").test(${County})) && (${YearBuilt} >= 1970)');
  30. * expression.evaluate(feature); // returns true or false depending on the feature's properties
  31. *
  32. * @example
  33. * const expression = new Cesium.Expression('(${Temperature} > 90) ? color("red") : color("white")');
  34. * expression.evaluateColor(feature, result); // returns a Cesium.Color object
  35. */
  36. function Expression(expression, defines) {
  37. //>>includeStart('debug', pragmas.debug);
  38. Check.typeOf.string("expression", expression);
  39. //>>includeEnd('debug');
  40. this._expression = expression;
  41. expression = replaceDefines(expression, defines);
  42. expression = replaceVariables(removeBackslashes(expression));
  43. // customize jsep operators
  44. jsep.addBinaryOp("=~", 0);
  45. jsep.addBinaryOp("!~", 0);
  46. let ast;
  47. try {
  48. ast = jsep(expression);
  49. } catch (e) {
  50. throw new RuntimeError(e);
  51. }
  52. this._runtimeAst = createRuntimeAst(this, ast);
  53. }
  54. Object.defineProperties(Expression.prototype, {
  55. /**
  56. * Gets the expression defined in the 3D Tiles Styling language.
  57. *
  58. * @memberof Expression.prototype
  59. *
  60. * @type {string}
  61. * @readonly
  62. *
  63. * @default undefined
  64. */
  65. expression: {
  66. get: function () {
  67. return this._expression;
  68. },
  69. },
  70. });
  71. // Scratch storage manager while evaluating deep expressions.
  72. // For example, an expression like dot(vec4(${red}), vec4(${green}) * vec4(${blue}) requires 3 scratch Cartesian4's
  73. const scratchStorage = {
  74. arrayIndex: 0,
  75. arrayArray: [[]],
  76. cartesian2Index: 0,
  77. cartesian3Index: 0,
  78. cartesian4Index: 0,
  79. cartesian2Array: [new Cartesian2()],
  80. cartesian3Array: [new Cartesian3()],
  81. cartesian4Array: [new Cartesian4()],
  82. reset: function () {
  83. this.arrayIndex = 0;
  84. this.cartesian2Index = 0;
  85. this.cartesian3Index = 0;
  86. this.cartesian4Index = 0;
  87. },
  88. getArray: function () {
  89. if (this.arrayIndex >= this.arrayArray.length) {
  90. this.arrayArray.push([]);
  91. }
  92. const array = this.arrayArray[this.arrayIndex++];
  93. array.length = 0;
  94. return array;
  95. },
  96. getCartesian2: function () {
  97. if (this.cartesian2Index >= this.cartesian2Array.length) {
  98. this.cartesian2Array.push(new Cartesian2());
  99. }
  100. return this.cartesian2Array[this.cartesian2Index++];
  101. },
  102. getCartesian3: function () {
  103. if (this.cartesian3Index >= this.cartesian3Array.length) {
  104. this.cartesian3Array.push(new Cartesian3());
  105. }
  106. return this.cartesian3Array[this.cartesian3Index++];
  107. },
  108. getCartesian4: function () {
  109. if (this.cartesian4Index >= this.cartesian4Array.length) {
  110. this.cartesian4Array.push(new Cartesian4());
  111. }
  112. return this.cartesian4Array[this.cartesian4Index++];
  113. },
  114. };
  115. /**
  116. * Evaluates the result of an expression, optionally using the provided feature's properties. If the result of
  117. * the expression in the
  118. * {@link https://github.com/CesiumGS/3d-tiles/tree/main/specification/Styling|3D Tiles Styling language}
  119. * is of type <code>Boolean</code>, <code>Number</code>, or <code>String</code>, the corresponding JavaScript
  120. * primitive type will be returned. If the result is a <code>RegExp</code>, a Javascript <code>RegExp</code>
  121. * object will be returned. If the result is a <code>Cartesian2</code>, <code>Cartesian3</code>, or <code>Cartesian4</code>,
  122. * a {@link Cartesian2}, {@link Cartesian3}, or {@link Cartesian4} object will be returned. If the <code>result</code> argument is
  123. * a {@link Color}, the {@link Cartesian4} value is converted to a {@link Color} and then returned.
  124. *
  125. * @param {Cesium3DTileFeature} feature The feature whose properties may be used as variables in the expression.
  126. * @param {object} [result] The object onto which to store the result.
  127. * @returns {boolean|number|string|RegExp|Cartesian2|Cartesian3|Cartesian4|Color} The result of evaluating the expression.
  128. */
  129. Expression.prototype.evaluate = function (feature, result) {
  130. scratchStorage.reset();
  131. const value = this._runtimeAst.evaluate(feature);
  132. if (result instanceof Color && value instanceof Cartesian4) {
  133. return Color.fromCartesian4(value, result);
  134. }
  135. if (
  136. value instanceof Cartesian2 ||
  137. value instanceof Cartesian3 ||
  138. value instanceof Cartesian4
  139. ) {
  140. return value.clone(result);
  141. }
  142. return value;
  143. };
  144. /**
  145. * Evaluates the result of a Color expression, optionally using the provided feature's properties.
  146. * <p>
  147. * This is equivalent to {@link Expression#evaluate} but always returns a {@link Color} object.
  148. * </p>
  149. *
  150. * @param {Cesium3DTileFeature} feature The feature whose properties may be used as variables in the expression.
  151. * @param {Color} [result] The object in which to store the result
  152. * @returns {Color} The modified result parameter or a new Color instance if one was not provided.
  153. */
  154. Expression.prototype.evaluateColor = function (feature, result) {
  155. scratchStorage.reset();
  156. const color = this._runtimeAst.evaluate(feature);
  157. return Color.fromCartesian4(color, result);
  158. };
  159. /**
  160. * Gets the shader function for this expression.
  161. * Returns undefined if the shader function can't be generated from this expression.
  162. *
  163. * @param {string} functionSignature Signature of the generated function.
  164. * @param {object} variableSubstitutionMap Maps variable names to shader variable names.
  165. * @param {object} shaderState Stores information about the generated shader function, including whether it is translucent.
  166. * @param {string} returnType The return type of the generated function.
  167. *
  168. * @returns {string} The shader function.
  169. *
  170. * @private
  171. */
  172. Expression.prototype.getShaderFunction = function (
  173. functionSignature,
  174. variableSubstitutionMap,
  175. shaderState,
  176. returnType
  177. ) {
  178. let shaderExpression = this.getShaderExpression(
  179. variableSubstitutionMap,
  180. shaderState
  181. );
  182. shaderExpression =
  183. `${returnType} ${functionSignature}\n` +
  184. `{\n` +
  185. ` return ${shaderExpression};\n` +
  186. `}\n`;
  187. return shaderExpression;
  188. };
  189. /**
  190. * Gets the shader expression for this expression.
  191. * Returns undefined if the shader expression can't be generated from this expression.
  192. *
  193. * @param {object} variableSubstitutionMap Maps variable names to shader variable names.
  194. * @param {object} shaderState Stores information about the generated shader function, including whether it is translucent.
  195. *
  196. * @returns {string} The shader expression.
  197. *
  198. * @private
  199. */
  200. Expression.prototype.getShaderExpression = function (
  201. variableSubstitutionMap,
  202. shaderState
  203. ) {
  204. return this._runtimeAst.getShaderExpression(
  205. variableSubstitutionMap,
  206. shaderState
  207. );
  208. };
  209. /**
  210. * Gets the variables used by the expression.
  211. *
  212. * @returns {string[]} The variables used by the expression.
  213. *
  214. * @private
  215. */
  216. Expression.prototype.getVariables = function () {
  217. let variables = [];
  218. this._runtimeAst.getVariables(variables);
  219. // Remove duplicates
  220. variables = variables.filter(function (variable, index, variables) {
  221. return variables.indexOf(variable) === index;
  222. });
  223. return variables;
  224. };
  225. const unaryOperators = ["!", "-", "+"];
  226. const binaryOperators = [
  227. "+",
  228. "-",
  229. "*",
  230. "/",
  231. "%",
  232. "===",
  233. "!==",
  234. ">",
  235. ">=",
  236. "<",
  237. "<=",
  238. "&&",
  239. "||",
  240. "!~",
  241. "=~",
  242. ];
  243. const variableRegex = /\${(.*?)}/g; // Matches ${variable_name}
  244. const backslashRegex = /\\/g;
  245. const backslashReplacement = "@#%";
  246. const replacementRegex = /@#%/g;
  247. const scratchColor = new Color();
  248. const unaryFunctions = {
  249. abs: getEvaluateUnaryComponentwise(Math.abs),
  250. sqrt: getEvaluateUnaryComponentwise(Math.sqrt),
  251. cos: getEvaluateUnaryComponentwise(Math.cos),
  252. sin: getEvaluateUnaryComponentwise(Math.sin),
  253. tan: getEvaluateUnaryComponentwise(Math.tan),
  254. acos: getEvaluateUnaryComponentwise(Math.acos),
  255. asin: getEvaluateUnaryComponentwise(Math.asin),
  256. atan: getEvaluateUnaryComponentwise(Math.atan),
  257. radians: getEvaluateUnaryComponentwise(CesiumMath.toRadians),
  258. degrees: getEvaluateUnaryComponentwise(CesiumMath.toDegrees),
  259. sign: getEvaluateUnaryComponentwise(CesiumMath.sign),
  260. floor: getEvaluateUnaryComponentwise(Math.floor),
  261. ceil: getEvaluateUnaryComponentwise(Math.ceil),
  262. round: getEvaluateUnaryComponentwise(Math.round),
  263. exp: getEvaluateUnaryComponentwise(Math.exp),
  264. exp2: getEvaluateUnaryComponentwise(exp2),
  265. log: getEvaluateUnaryComponentwise(Math.log),
  266. log2: getEvaluateUnaryComponentwise(log2),
  267. fract: getEvaluateUnaryComponentwise(fract),
  268. length: length,
  269. normalize: normalize,
  270. };
  271. const binaryFunctions = {
  272. atan2: getEvaluateBinaryComponentwise(Math.atan2, false),
  273. pow: getEvaluateBinaryComponentwise(Math.pow, false),
  274. min: getEvaluateBinaryComponentwise(Math.min, true),
  275. max: getEvaluateBinaryComponentwise(Math.max, true),
  276. distance: distance,
  277. dot: dot,
  278. cross: cross,
  279. };
  280. const ternaryFunctions = {
  281. clamp: getEvaluateTernaryComponentwise(CesiumMath.clamp, true),
  282. mix: getEvaluateTernaryComponentwise(CesiumMath.lerp, true),
  283. };
  284. function fract(number) {
  285. return number - Math.floor(number);
  286. }
  287. function exp2(exponent) {
  288. return Math.pow(2.0, exponent);
  289. }
  290. function log2(number) {
  291. return CesiumMath.log2(number);
  292. }
  293. function getEvaluateUnaryComponentwise(operation) {
  294. return function (call, left) {
  295. if (typeof left === "number") {
  296. return operation(left);
  297. } else if (left instanceof Cartesian2) {
  298. return Cartesian2.fromElements(
  299. operation(left.x),
  300. operation(left.y),
  301. scratchStorage.getCartesian2()
  302. );
  303. } else if (left instanceof Cartesian3) {
  304. return Cartesian3.fromElements(
  305. operation(left.x),
  306. operation(left.y),
  307. operation(left.z),
  308. scratchStorage.getCartesian3()
  309. );
  310. } else if (left instanceof Cartesian4) {
  311. return Cartesian4.fromElements(
  312. operation(left.x),
  313. operation(left.y),
  314. operation(left.z),
  315. operation(left.w),
  316. scratchStorage.getCartesian4()
  317. );
  318. }
  319. throw new RuntimeError(
  320. `Function "${call}" requires a vector or number argument. Argument is ${left}.`
  321. );
  322. };
  323. }
  324. function getEvaluateBinaryComponentwise(operation, allowScalar) {
  325. return function (call, left, right) {
  326. if (allowScalar && typeof right === "number") {
  327. if (typeof left === "number") {
  328. return operation(left, right);
  329. } else if (left instanceof Cartesian2) {
  330. return Cartesian2.fromElements(
  331. operation(left.x, right),
  332. operation(left.y, right),
  333. scratchStorage.getCartesian2()
  334. );
  335. } else if (left instanceof Cartesian3) {
  336. return Cartesian3.fromElements(
  337. operation(left.x, right),
  338. operation(left.y, right),
  339. operation(left.z, right),
  340. scratchStorage.getCartesian3()
  341. );
  342. } else if (left instanceof Cartesian4) {
  343. return Cartesian4.fromElements(
  344. operation(left.x, right),
  345. operation(left.y, right),
  346. operation(left.z, right),
  347. operation(left.w, right),
  348. scratchStorage.getCartesian4()
  349. );
  350. }
  351. }
  352. if (typeof left === "number" && typeof right === "number") {
  353. return operation(left, right);
  354. } else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
  355. return Cartesian2.fromElements(
  356. operation(left.x, right.x),
  357. operation(left.y, right.y),
  358. scratchStorage.getCartesian2()
  359. );
  360. } else if (left instanceof Cartesian3 && right instanceof Cartesian3) {
  361. return Cartesian3.fromElements(
  362. operation(left.x, right.x),
  363. operation(left.y, right.y),
  364. operation(left.z, right.z),
  365. scratchStorage.getCartesian3()
  366. );
  367. } else if (left instanceof Cartesian4 && right instanceof Cartesian4) {
  368. return Cartesian4.fromElements(
  369. operation(left.x, right.x),
  370. operation(left.y, right.y),
  371. operation(left.z, right.z),
  372. operation(left.w, right.w),
  373. scratchStorage.getCartesian4()
  374. );
  375. }
  376. throw new RuntimeError(
  377. `Function "${call}" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`
  378. );
  379. };
  380. }
  381. function getEvaluateTernaryComponentwise(operation, allowScalar) {
  382. return function (call, left, right, test) {
  383. if (allowScalar && typeof test === "number") {
  384. if (typeof left === "number" && typeof right === "number") {
  385. return operation(left, right, test);
  386. } else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
  387. return Cartesian2.fromElements(
  388. operation(left.x, right.x, test),
  389. operation(left.y, right.y, test),
  390. scratchStorage.getCartesian2()
  391. );
  392. } else if (left instanceof Cartesian3 && right instanceof Cartesian3) {
  393. return Cartesian3.fromElements(
  394. operation(left.x, right.x, test),
  395. operation(left.y, right.y, test),
  396. operation(left.z, right.z, test),
  397. scratchStorage.getCartesian3()
  398. );
  399. } else if (left instanceof Cartesian4 && right instanceof Cartesian4) {
  400. return Cartesian4.fromElements(
  401. operation(left.x, right.x, test),
  402. operation(left.y, right.y, test),
  403. operation(left.z, right.z, test),
  404. operation(left.w, right.w, test),
  405. scratchStorage.getCartesian4()
  406. );
  407. }
  408. }
  409. if (
  410. typeof left === "number" &&
  411. typeof right === "number" &&
  412. typeof test === "number"
  413. ) {
  414. return operation(left, right, test);
  415. } else if (
  416. left instanceof Cartesian2 &&
  417. right instanceof Cartesian2 &&
  418. test instanceof Cartesian2
  419. ) {
  420. return Cartesian2.fromElements(
  421. operation(left.x, right.x, test.x),
  422. operation(left.y, right.y, test.y),
  423. scratchStorage.getCartesian2()
  424. );
  425. } else if (
  426. left instanceof Cartesian3 &&
  427. right instanceof Cartesian3 &&
  428. test instanceof Cartesian3
  429. ) {
  430. return Cartesian3.fromElements(
  431. operation(left.x, right.x, test.x),
  432. operation(left.y, right.y, test.y),
  433. operation(left.z, right.z, test.z),
  434. scratchStorage.getCartesian3()
  435. );
  436. } else if (
  437. left instanceof Cartesian4 &&
  438. right instanceof Cartesian4 &&
  439. test instanceof Cartesian4
  440. ) {
  441. return Cartesian4.fromElements(
  442. operation(left.x, right.x, test.x),
  443. operation(left.y, right.y, test.y),
  444. operation(left.z, right.z, test.z),
  445. operation(left.w, right.w, test.w),
  446. scratchStorage.getCartesian4()
  447. );
  448. }
  449. throw new RuntimeError(
  450. `Function "${call}" requires vector or number arguments of matching types. Arguments are ${left}, ${right}, and ${test}.`
  451. );
  452. };
  453. }
  454. function length(call, left) {
  455. if (typeof left === "number") {
  456. return Math.abs(left);
  457. } else if (left instanceof Cartesian2) {
  458. return Cartesian2.magnitude(left);
  459. } else if (left instanceof Cartesian3) {
  460. return Cartesian3.magnitude(left);
  461. } else if (left instanceof Cartesian4) {
  462. return Cartesian4.magnitude(left);
  463. }
  464. throw new RuntimeError(
  465. `Function "${call}" requires a vector or number argument. Argument is ${left}.`
  466. );
  467. }
  468. function normalize(call, left) {
  469. if (typeof left === "number") {
  470. return 1.0;
  471. } else if (left instanceof Cartesian2) {
  472. return Cartesian2.normalize(left, scratchStorage.getCartesian2());
  473. } else if (left instanceof Cartesian3) {
  474. return Cartesian3.normalize(left, scratchStorage.getCartesian3());
  475. } else if (left instanceof Cartesian4) {
  476. return Cartesian4.normalize(left, scratchStorage.getCartesian4());
  477. }
  478. throw new RuntimeError(
  479. `Function "${call}" requires a vector or number argument. Argument is ${left}.`
  480. );
  481. }
  482. function distance(call, left, right) {
  483. if (typeof left === "number" && typeof right === "number") {
  484. return Math.abs(left - right);
  485. } else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
  486. return Cartesian2.distance(left, right);
  487. } else if (left instanceof Cartesian3 && right instanceof Cartesian3) {
  488. return Cartesian3.distance(left, right);
  489. } else if (left instanceof Cartesian4 && right instanceof Cartesian4) {
  490. return Cartesian4.distance(left, right);
  491. }
  492. throw new RuntimeError(
  493. `Function "${call}" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`
  494. );
  495. }
  496. function dot(call, left, right) {
  497. if (typeof left === "number" && typeof right === "number") {
  498. return left * right;
  499. } else if (left instanceof Cartesian2 && right instanceof Cartesian2) {
  500. return Cartesian2.dot(left, right);
  501. } else if (left instanceof Cartesian3 && right instanceof Cartesian3) {
  502. return Cartesian3.dot(left, right);
  503. } else if (left instanceof Cartesian4 && right instanceof Cartesian4) {
  504. return Cartesian4.dot(left, right);
  505. }
  506. throw new RuntimeError(
  507. `Function "${call}" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`
  508. );
  509. }
  510. function cross(call, left, right) {
  511. if (left instanceof Cartesian3 && right instanceof Cartesian3) {
  512. return Cartesian3.cross(left, right, scratchStorage.getCartesian3());
  513. }
  514. throw new RuntimeError(
  515. `Function "${call}" requires vec3 arguments. Arguments are ${left} and ${right}.`
  516. );
  517. }
  518. function Node(type, value, left, right, test) {
  519. this._type = type;
  520. this._value = value;
  521. this._left = left;
  522. this._right = right;
  523. this._test = test;
  524. this.evaluate = undefined;
  525. setEvaluateFunction(this);
  526. }
  527. function replaceDefines(expression, defines) {
  528. if (!defined(defines)) {
  529. return expression;
  530. }
  531. for (const key in defines) {
  532. if (defines.hasOwnProperty(key)) {
  533. const definePlaceholder = new RegExp(`\\$\\{${key}\\}`, "g");
  534. const defineReplace = `(${defines[key]})`;
  535. if (defined(defineReplace)) {
  536. expression = expression.replace(definePlaceholder, defineReplace);
  537. }
  538. }
  539. }
  540. return expression;
  541. }
  542. function removeBackslashes(expression) {
  543. return expression.replace(backslashRegex, backslashReplacement);
  544. }
  545. function replaceBackslashes(expression) {
  546. return expression.replace(replacementRegex, "\\");
  547. }
  548. function replaceVariables(expression) {
  549. let exp = expression;
  550. let result = "";
  551. let i = exp.indexOf("${");
  552. while (i >= 0) {
  553. // Check if string is inside quotes
  554. const openSingleQuote = exp.indexOf("'");
  555. const openDoubleQuote = exp.indexOf('"');
  556. let closeQuote;
  557. if (openSingleQuote >= 0 && openSingleQuote < i) {
  558. closeQuote = exp.indexOf("'", openSingleQuote + 1);
  559. result += exp.substr(0, closeQuote + 1);
  560. exp = exp.substr(closeQuote + 1);
  561. i = exp.indexOf("${");
  562. } else if (openDoubleQuote >= 0 && openDoubleQuote < i) {
  563. closeQuote = exp.indexOf('"', openDoubleQuote + 1);
  564. result += exp.substr(0, closeQuote + 1);
  565. exp = exp.substr(closeQuote + 1);
  566. i = exp.indexOf("${");
  567. } else {
  568. result += exp.substr(0, i);
  569. const j = exp.indexOf("}");
  570. if (j < 0) {
  571. throw new RuntimeError("Unmatched {.");
  572. }
  573. result += `czm_${exp.substr(i + 2, j - (i + 2))}`;
  574. exp = exp.substr(j + 1);
  575. i = exp.indexOf("${");
  576. }
  577. }
  578. result += exp;
  579. return result;
  580. }
  581. function parseLiteral(ast) {
  582. const type = typeof ast.value;
  583. if (ast.value === null) {
  584. return new Node(ExpressionNodeType.LITERAL_NULL, null);
  585. } else if (type === "boolean") {
  586. return new Node(ExpressionNodeType.LITERAL_BOOLEAN, ast.value);
  587. } else if (type === "number") {
  588. return new Node(ExpressionNodeType.LITERAL_NUMBER, ast.value);
  589. } else if (type === "string") {
  590. if (ast.value.indexOf("${") >= 0) {
  591. return new Node(ExpressionNodeType.VARIABLE_IN_STRING, ast.value);
  592. }
  593. return new Node(
  594. ExpressionNodeType.LITERAL_STRING,
  595. replaceBackslashes(ast.value)
  596. );
  597. }
  598. }
  599. function parseCall(expression, ast) {
  600. const args = ast.arguments;
  601. const argsLength = args.length;
  602. let call;
  603. let val, left, right;
  604. // Member function calls
  605. if (ast.callee.type === "MemberExpression") {
  606. call = ast.callee.property.name;
  607. const object = ast.callee.object;
  608. if (call === "test" || call === "exec") {
  609. // Make sure this is called on a valid type
  610. if (!defined(object.callee) || object.callee.name !== "regExp") {
  611. throw new RuntimeError(`${call} is not a function.`);
  612. }
  613. if (argsLength === 0) {
  614. if (call === "test") {
  615. return new Node(ExpressionNodeType.LITERAL_BOOLEAN, false);
  616. }
  617. return new Node(ExpressionNodeType.LITERAL_NULL, null);
  618. }
  619. left = createRuntimeAst(expression, object);
  620. right = createRuntimeAst(expression, args[0]);
  621. return new Node(ExpressionNodeType.FUNCTION_CALL, call, left, right);
  622. } else if (call === "toString") {
  623. val = createRuntimeAst(expression, object);
  624. return new Node(ExpressionNodeType.FUNCTION_CALL, call, val);
  625. }
  626. throw new RuntimeError(`Unexpected function call "${call}".`);
  627. }
  628. // Non-member function calls
  629. call = ast.callee.name;
  630. if (call === "color") {
  631. if (argsLength === 0) {
  632. return new Node(ExpressionNodeType.LITERAL_COLOR, call);
  633. }
  634. val = createRuntimeAst(expression, args[0]);
  635. if (defined(args[1])) {
  636. const alpha = createRuntimeAst(expression, args[1]);
  637. return new Node(ExpressionNodeType.LITERAL_COLOR, call, [val, alpha]);
  638. }
  639. return new Node(ExpressionNodeType.LITERAL_COLOR, call, [val]);
  640. } else if (call === "rgb" || call === "hsl") {
  641. if (argsLength < 3) {
  642. throw new RuntimeError(`${call} requires three arguments.`);
  643. }
  644. val = [
  645. createRuntimeAst(expression, args[0]),
  646. createRuntimeAst(expression, args[1]),
  647. createRuntimeAst(expression, args[2]),
  648. ];
  649. return new Node(ExpressionNodeType.LITERAL_COLOR, call, val);
  650. } else if (call === "rgba" || call === "hsla") {
  651. if (argsLength < 4) {
  652. throw new RuntimeError(`${call} requires four arguments.`);
  653. }
  654. val = [
  655. createRuntimeAst(expression, args[0]),
  656. createRuntimeAst(expression, args[1]),
  657. createRuntimeAst(expression, args[2]),
  658. createRuntimeAst(expression, args[3]),
  659. ];
  660. return new Node(ExpressionNodeType.LITERAL_COLOR, call, val);
  661. } else if (call === "vec2" || call === "vec3" || call === "vec4") {
  662. // Check for invalid constructors at evaluation time
  663. val = new Array(argsLength);
  664. for (let i = 0; i < argsLength; ++i) {
  665. val[i] = createRuntimeAst(expression, args[i]);
  666. }
  667. return new Node(ExpressionNodeType.LITERAL_VECTOR, call, val);
  668. } else if (call === "isNaN" || call === "isFinite") {
  669. if (argsLength === 0) {
  670. if (call === "isNaN") {
  671. return new Node(ExpressionNodeType.LITERAL_BOOLEAN, true);
  672. }
  673. return new Node(ExpressionNodeType.LITERAL_BOOLEAN, false);
  674. }
  675. val = createRuntimeAst(expression, args[0]);
  676. return new Node(ExpressionNodeType.UNARY, call, val);
  677. } else if (call === "isExactClass" || call === "isClass") {
  678. if (argsLength < 1 || argsLength > 1) {
  679. throw new RuntimeError(`${call} requires exactly one argument.`);
  680. }
  681. val = createRuntimeAst(expression, args[0]);
  682. return new Node(ExpressionNodeType.UNARY, call, val);
  683. } else if (call === "getExactClassName") {
  684. if (argsLength > 0) {
  685. throw new RuntimeError(`${call} does not take any argument.`);
  686. }
  687. return new Node(ExpressionNodeType.UNARY, call);
  688. } else if (defined(unaryFunctions[call])) {
  689. if (argsLength !== 1) {
  690. throw new RuntimeError(`${call} requires exactly one argument.`);
  691. }
  692. val = createRuntimeAst(expression, args[0]);
  693. return new Node(ExpressionNodeType.UNARY, call, val);
  694. } else if (defined(binaryFunctions[call])) {
  695. if (argsLength !== 2) {
  696. throw new RuntimeError(`${call} requires exactly two arguments.`);
  697. }
  698. left = createRuntimeAst(expression, args[0]);
  699. right = createRuntimeAst(expression, args[1]);
  700. return new Node(ExpressionNodeType.BINARY, call, left, right);
  701. } else if (defined(ternaryFunctions[call])) {
  702. if (argsLength !== 3) {
  703. throw new RuntimeError(`${call} requires exactly three arguments.`);
  704. }
  705. left = createRuntimeAst(expression, args[0]);
  706. right = createRuntimeAst(expression, args[1]);
  707. const test = createRuntimeAst(expression, args[2]);
  708. return new Node(ExpressionNodeType.TERNARY, call, left, right, test);
  709. } else if (call === "Boolean") {
  710. if (argsLength === 0) {
  711. return new Node(ExpressionNodeType.LITERAL_BOOLEAN, false);
  712. }
  713. val = createRuntimeAst(expression, args[0]);
  714. return new Node(ExpressionNodeType.UNARY, call, val);
  715. } else if (call === "Number") {
  716. if (argsLength === 0) {
  717. return new Node(ExpressionNodeType.LITERAL_NUMBER, 0);
  718. }
  719. val = createRuntimeAst(expression, args[0]);
  720. return new Node(ExpressionNodeType.UNARY, call, val);
  721. } else if (call === "String") {
  722. if (argsLength === 0) {
  723. return new Node(ExpressionNodeType.LITERAL_STRING, "");
  724. }
  725. val = createRuntimeAst(expression, args[0]);
  726. return new Node(ExpressionNodeType.UNARY, call, val);
  727. } else if (call === "regExp") {
  728. return parseRegex(expression, ast);
  729. }
  730. throw new RuntimeError(`Unexpected function call "${call}".`);
  731. }
  732. function parseRegex(expression, ast) {
  733. const args = ast.arguments;
  734. // no arguments, return default regex
  735. if (args.length === 0) {
  736. return new Node(ExpressionNodeType.LITERAL_REGEX, new RegExp());
  737. }
  738. const pattern = createRuntimeAst(expression, args[0]);
  739. let exp;
  740. // optional flag argument supplied
  741. if (args.length > 1) {
  742. const flags = createRuntimeAst(expression, args[1]);
  743. if (isLiteralType(pattern) && isLiteralType(flags)) {
  744. try {
  745. exp = new RegExp(
  746. replaceBackslashes(String(pattern._value)),
  747. flags._value
  748. );
  749. } catch (e) {
  750. throw new RuntimeError(e);
  751. }
  752. return new Node(ExpressionNodeType.LITERAL_REGEX, exp);
  753. }
  754. return new Node(ExpressionNodeType.REGEX, pattern, flags);
  755. }
  756. // only pattern argument supplied
  757. if (isLiteralType(pattern)) {
  758. try {
  759. exp = new RegExp(replaceBackslashes(String(pattern._value)));
  760. } catch (e) {
  761. throw new RuntimeError(e);
  762. }
  763. return new Node(ExpressionNodeType.LITERAL_REGEX, exp);
  764. }
  765. return new Node(ExpressionNodeType.REGEX, pattern);
  766. }
  767. function parseKeywordsAndVariables(ast) {
  768. if (isVariable(ast.name)) {
  769. const name = getPropertyName(ast.name);
  770. if (name.substr(0, 8) === "tiles3d_") {
  771. return new Node(ExpressionNodeType.BUILTIN_VARIABLE, name);
  772. }
  773. return new Node(ExpressionNodeType.VARIABLE, name);
  774. } else if (ast.name === "NaN") {
  775. return new Node(ExpressionNodeType.LITERAL_NUMBER, NaN);
  776. } else if (ast.name === "Infinity") {
  777. return new Node(ExpressionNodeType.LITERAL_NUMBER, Infinity);
  778. } else if (ast.name === "undefined") {
  779. return new Node(ExpressionNodeType.LITERAL_UNDEFINED, undefined);
  780. }
  781. throw new RuntimeError(`${ast.name} is not defined.`);
  782. }
  783. function parseMathConstant(ast) {
  784. const name = ast.property.name;
  785. if (name === "PI") {
  786. return new Node(ExpressionNodeType.LITERAL_NUMBER, Math.PI);
  787. } else if (name === "E") {
  788. return new Node(ExpressionNodeType.LITERAL_NUMBER, Math.E);
  789. }
  790. }
  791. function parseNumberConstant(ast) {
  792. const name = ast.property.name;
  793. if (name === "POSITIVE_INFINITY") {
  794. return new Node(
  795. ExpressionNodeType.LITERAL_NUMBER,
  796. Number.POSITIVE_INFINITY
  797. );
  798. }
  799. }
  800. function parseMemberExpression(expression, ast) {
  801. if (ast.object.name === "Math") {
  802. return parseMathConstant(ast);
  803. } else if (ast.object.name === "Number") {
  804. return parseNumberConstant(ast);
  805. }
  806. let val;
  807. const obj = createRuntimeAst(expression, ast.object);
  808. if (ast.computed) {
  809. val = createRuntimeAst(expression, ast.property);
  810. return new Node(ExpressionNodeType.MEMBER, "brackets", obj, val);
  811. }
  812. val = new Node(ExpressionNodeType.LITERAL_STRING, ast.property.name);
  813. return new Node(ExpressionNodeType.MEMBER, "dot", obj, val);
  814. }
  815. function isLiteralType(node) {
  816. return node._type >= ExpressionNodeType.LITERAL_NULL;
  817. }
  818. function isVariable(name) {
  819. return name.substr(0, 4) === "czm_";
  820. }
  821. function getPropertyName(variable) {
  822. return variable.substr(4);
  823. }
  824. function createRuntimeAst(expression, ast) {
  825. let node;
  826. let op;
  827. let left;
  828. let right;
  829. if (ast.type === "Literal") {
  830. node = parseLiteral(ast);
  831. } else if (ast.type === "CallExpression") {
  832. node = parseCall(expression, ast);
  833. } else if (ast.type === "Identifier") {
  834. node = parseKeywordsAndVariables(ast);
  835. } else if (ast.type === "UnaryExpression") {
  836. op = ast.operator;
  837. const child = createRuntimeAst(expression, ast.argument);
  838. if (unaryOperators.indexOf(op) > -1) {
  839. node = new Node(ExpressionNodeType.UNARY, op, child);
  840. } else {
  841. throw new RuntimeError(`Unexpected operator "${op}".`);
  842. }
  843. } else if (ast.type === "BinaryExpression") {
  844. op = ast.operator;
  845. left = createRuntimeAst(expression, ast.left);
  846. right = createRuntimeAst(expression, ast.right);
  847. if (binaryOperators.indexOf(op) > -1) {
  848. node = new Node(ExpressionNodeType.BINARY, op, left, right);
  849. } else {
  850. throw new RuntimeError(`Unexpected operator "${op}".`);
  851. }
  852. } else if (ast.type === "LogicalExpression") {
  853. op = ast.operator;
  854. left = createRuntimeAst(expression, ast.left);
  855. right = createRuntimeAst(expression, ast.right);
  856. if (binaryOperators.indexOf(op) > -1) {
  857. node = new Node(ExpressionNodeType.BINARY, op, left, right);
  858. }
  859. } else if (ast.type === "ConditionalExpression") {
  860. const test = createRuntimeAst(expression, ast.test);
  861. left = createRuntimeAst(expression, ast.consequent);
  862. right = createRuntimeAst(expression, ast.alternate);
  863. node = new Node(ExpressionNodeType.CONDITIONAL, "?", left, right, test);
  864. } else if (ast.type === "MemberExpression") {
  865. node = parseMemberExpression(expression, ast);
  866. } else if (ast.type === "ArrayExpression") {
  867. const val = [];
  868. for (let i = 0; i < ast.elements.length; i++) {
  869. val[i] = createRuntimeAst(expression, ast.elements[i]);
  870. }
  871. node = new Node(ExpressionNodeType.ARRAY, val);
  872. } else if (ast.type === "Compound") {
  873. // empty expression or multiple expressions
  874. throw new RuntimeError("Provide exactly one expression.");
  875. } else {
  876. throw new RuntimeError("Cannot parse expression.");
  877. }
  878. return node;
  879. }
  880. function setEvaluateFunction(node) {
  881. if (node._type === ExpressionNodeType.CONDITIONAL) {
  882. node.evaluate = node._evaluateConditional;
  883. } else if (node._type === ExpressionNodeType.FUNCTION_CALL) {
  884. if (node._value === "test") {
  885. node.evaluate = node._evaluateRegExpTest;
  886. } else if (node._value === "exec") {
  887. node.evaluate = node._evaluateRegExpExec;
  888. } else if (node._value === "toString") {
  889. node.evaluate = node._evaluateToString;
  890. }
  891. } else if (node._type === ExpressionNodeType.UNARY) {
  892. if (node._value === "!") {
  893. node.evaluate = node._evaluateNot;
  894. } else if (node._value === "-") {
  895. node.evaluate = node._evaluateNegative;
  896. } else if (node._value === "+") {
  897. node.evaluate = node._evaluatePositive;
  898. } else if (node._value === "isNaN") {
  899. node.evaluate = node._evaluateNaN;
  900. } else if (node._value === "isFinite") {
  901. node.evaluate = node._evaluateIsFinite;
  902. } else if (node._value === "isExactClass") {
  903. node.evaluate = node._evaluateIsExactClass;
  904. } else if (node._value === "isClass") {
  905. node.evaluate = node._evaluateIsClass;
  906. } else if (node._value === "getExactClassName") {
  907. node.evaluate = node._evaluateGetExactClassName;
  908. } else if (node._value === "Boolean") {
  909. node.evaluate = node._evaluateBooleanConversion;
  910. } else if (node._value === "Number") {
  911. node.evaluate = node._evaluateNumberConversion;
  912. } else if (node._value === "String") {
  913. node.evaluate = node._evaluateStringConversion;
  914. } else if (defined(unaryFunctions[node._value])) {
  915. node.evaluate = getEvaluateUnaryFunction(node._value);
  916. }
  917. } else if (node._type === ExpressionNodeType.BINARY) {
  918. if (node._value === "+") {
  919. node.evaluate = node._evaluatePlus;
  920. } else if (node._value === "-") {
  921. node.evaluate = node._evaluateMinus;
  922. } else if (node._value === "*") {
  923. node.evaluate = node._evaluateTimes;
  924. } else if (node._value === "/") {
  925. node.evaluate = node._evaluateDivide;
  926. } else if (node._value === "%") {
  927. node.evaluate = node._evaluateMod;
  928. } else if (node._value === "===") {
  929. node.evaluate = node._evaluateEqualsStrict;
  930. } else if (node._value === "!==") {
  931. node.evaluate = node._evaluateNotEqualsStrict;
  932. } else if (node._value === "<") {
  933. node.evaluate = node._evaluateLessThan;
  934. } else if (node._value === "<=") {
  935. node.evaluate = node._evaluateLessThanOrEquals;
  936. } else if (node._value === ">") {
  937. node.evaluate = node._evaluateGreaterThan;
  938. } else if (node._value === ">=") {
  939. node.evaluate = node._evaluateGreaterThanOrEquals;
  940. } else if (node._value === "&&") {
  941. node.evaluate = node._evaluateAnd;
  942. } else if (node._value === "||") {
  943. node.evaluate = node._evaluateOr;
  944. } else if (node._value === "=~") {
  945. node.evaluate = node._evaluateRegExpMatch;
  946. } else if (node._value === "!~") {
  947. node.evaluate = node._evaluateRegExpNotMatch;
  948. } else if (defined(binaryFunctions[node._value])) {
  949. node.evaluate = getEvaluateBinaryFunction(node._value);
  950. }
  951. } else if (node._type === ExpressionNodeType.TERNARY) {
  952. node.evaluate = getEvaluateTernaryFunction(node._value);
  953. } else if (node._type === ExpressionNodeType.MEMBER) {
  954. if (node._value === "brackets") {
  955. node.evaluate = node._evaluateMemberBrackets;
  956. } else {
  957. node.evaluate = node._evaluateMemberDot;
  958. }
  959. } else if (node._type === ExpressionNodeType.ARRAY) {
  960. node.evaluate = node._evaluateArray;
  961. } else if (node._type === ExpressionNodeType.VARIABLE) {
  962. node.evaluate = node._evaluateVariable;
  963. } else if (node._type === ExpressionNodeType.VARIABLE_IN_STRING) {
  964. node.evaluate = node._evaluateVariableString;
  965. } else if (node._type === ExpressionNodeType.LITERAL_COLOR) {
  966. node.evaluate = node._evaluateLiteralColor;
  967. } else if (node._type === ExpressionNodeType.LITERAL_VECTOR) {
  968. node.evaluate = node._evaluateLiteralVector;
  969. } else if (node._type === ExpressionNodeType.LITERAL_STRING) {
  970. node.evaluate = node._evaluateLiteralString;
  971. } else if (node._type === ExpressionNodeType.REGEX) {
  972. node.evaluate = node._evaluateRegExp;
  973. } else if (node._type === ExpressionNodeType.BUILTIN_VARIABLE) {
  974. if (node._value === "tiles3d_tileset_time") {
  975. node.evaluate = evaluateTilesetTime;
  976. }
  977. } else {
  978. node.evaluate = node._evaluateLiteral;
  979. }
  980. }
  981. function evaluateTilesetTime(feature) {
  982. if (!defined(feature)) {
  983. return 0.0;
  984. }
  985. return feature.content.tileset.timeSinceLoad;
  986. }
  987. function getEvaluateUnaryFunction(call) {
  988. const evaluate = unaryFunctions[call];
  989. return function (feature) {
  990. const left = this._left.evaluate(feature);
  991. return evaluate(call, left);
  992. };
  993. }
  994. function getEvaluateBinaryFunction(call) {
  995. const evaluate = binaryFunctions[call];
  996. return function (feature) {
  997. const left = this._left.evaluate(feature);
  998. const right = this._right.evaluate(feature);
  999. return evaluate(call, left, right);
  1000. };
  1001. }
  1002. function getEvaluateTernaryFunction(call) {
  1003. const evaluate = ternaryFunctions[call];
  1004. return function (feature) {
  1005. const left = this._left.evaluate(feature);
  1006. const right = this._right.evaluate(feature);
  1007. const test = this._test.evaluate(feature);
  1008. return evaluate(call, left, right, test);
  1009. };
  1010. }
  1011. function getFeatureProperty(feature, name) {
  1012. // Returns undefined if the feature is not defined or the property name is not defined for that feature
  1013. if (defined(feature)) {
  1014. return feature.getPropertyInherited(name);
  1015. }
  1016. }
  1017. Node.prototype._evaluateLiteral = function () {
  1018. return this._value;
  1019. };
  1020. Node.prototype._evaluateLiteralColor = function (feature) {
  1021. const color = scratchColor;
  1022. const args = this._left;
  1023. if (this._value === "color") {
  1024. if (!defined(args)) {
  1025. Color.fromBytes(255, 255, 255, 255, color);
  1026. } else if (args.length > 1) {
  1027. Color.fromCssColorString(args[0].evaluate(feature), color);
  1028. color.alpha = args[1].evaluate(feature);
  1029. } else {
  1030. Color.fromCssColorString(args[0].evaluate(feature), color);
  1031. }
  1032. } else if (this._value === "rgb") {
  1033. Color.fromBytes(
  1034. args[0].evaluate(feature),
  1035. args[1].evaluate(feature),
  1036. args[2].evaluate(feature),
  1037. 255,
  1038. color
  1039. );
  1040. } else if (this._value === "rgba") {
  1041. // convert between css alpha (0 to 1) and cesium alpha (0 to 255)
  1042. const a = args[3].evaluate(feature) * 255;
  1043. Color.fromBytes(
  1044. args[0].evaluate(feature),
  1045. args[1].evaluate(feature),
  1046. args[2].evaluate(feature),
  1047. a,
  1048. color
  1049. );
  1050. } else if (this._value === "hsl") {
  1051. Color.fromHsl(
  1052. args[0].evaluate(feature),
  1053. args[1].evaluate(feature),
  1054. args[2].evaluate(feature),
  1055. 1.0,
  1056. color
  1057. );
  1058. } else if (this._value === "hsla") {
  1059. Color.fromHsl(
  1060. args[0].evaluate(feature),
  1061. args[1].evaluate(feature),
  1062. args[2].evaluate(feature),
  1063. args[3].evaluate(feature),
  1064. color
  1065. );
  1066. }
  1067. return Cartesian4.fromColor(color, scratchStorage.getCartesian4());
  1068. };
  1069. Node.prototype._evaluateLiteralVector = function (feature) {
  1070. // Gather the components that make up the vector, which includes components from interior vectors.
  1071. // For example vec3(1, 2, 3) or vec3(vec2(1, 2), 3) are both valid.
  1072. //
  1073. // If the number of components does not equal the vector's size, then a RuntimeError is thrown - with two exceptions:
  1074. // 1. A vector may be constructed from a larger vector and drop the extra components.
  1075. // 2. A vector may be constructed from a single component - vec3(1) will become vec3(1, 1, 1).
  1076. //
  1077. // Examples of invalid constructors include:
  1078. // vec4(1, 2) // not enough components
  1079. // vec3(vec2(1, 2)) // not enough components
  1080. // vec3(1, 2, 3, 4) // too many components
  1081. // vec2(vec4(1), 1) // too many components
  1082. const components = scratchStorage.getArray();
  1083. const call = this._value;
  1084. const args = this._left;
  1085. const argsLength = args.length;
  1086. for (let i = 0; i < argsLength; ++i) {
  1087. const value = args[i].evaluate(feature);
  1088. if (typeof value === "number") {
  1089. components.push(value);
  1090. } else if (value instanceof Cartesian2) {
  1091. components.push(value.x, value.y);
  1092. } else if (value instanceof Cartesian3) {
  1093. components.push(value.x, value.y, value.z);
  1094. } else if (value instanceof Cartesian4) {
  1095. components.push(value.x, value.y, value.z, value.w);
  1096. } else {
  1097. throw new RuntimeError(
  1098. `${call} argument must be a vector or number. Argument is ${value}.`
  1099. );
  1100. }
  1101. }
  1102. const componentsLength = components.length;
  1103. const vectorLength = parseInt(call.charAt(3));
  1104. if (componentsLength === 0) {
  1105. throw new RuntimeError(`Invalid ${call} constructor. No valid arguments.`);
  1106. } else if (componentsLength < vectorLength && componentsLength > 1) {
  1107. throw new RuntimeError(
  1108. `Invalid ${call} constructor. Not enough arguments.`
  1109. );
  1110. } else if (componentsLength > vectorLength && argsLength > 1) {
  1111. throw new RuntimeError(`Invalid ${call} constructor. Too many arguments.`);
  1112. }
  1113. if (componentsLength === 1) {
  1114. // Add the same component 3 more times
  1115. const component = components[0];
  1116. components.push(component, component, component);
  1117. }
  1118. if (call === "vec2") {
  1119. return Cartesian2.fromArray(components, 0, scratchStorage.getCartesian2());
  1120. } else if (call === "vec3") {
  1121. return Cartesian3.fromArray(components, 0, scratchStorage.getCartesian3());
  1122. } else if (call === "vec4") {
  1123. return Cartesian4.fromArray(components, 0, scratchStorage.getCartesian4());
  1124. }
  1125. };
  1126. Node.prototype._evaluateLiteralString = function () {
  1127. return this._value;
  1128. };
  1129. Node.prototype._evaluateVariableString = function (feature) {
  1130. let result = this._value;
  1131. let match = variableRegex.exec(result);
  1132. while (match !== null) {
  1133. const placeholder = match[0];
  1134. const variableName = match[1];
  1135. let property = getFeatureProperty(feature, variableName);
  1136. if (!defined(property)) {
  1137. property = "";
  1138. }
  1139. result = result.replace(placeholder, property);
  1140. match = variableRegex.exec(result);
  1141. }
  1142. return result;
  1143. };
  1144. Node.prototype._evaluateVariable = function (feature) {
  1145. // evaluates to undefined if the property name is not defined for that feature
  1146. return getFeatureProperty(feature, this._value);
  1147. };
  1148. function checkFeature(ast) {
  1149. return ast._value === "feature";
  1150. }
  1151. // PERFORMANCE_IDEA: Determine if parent property needs to be computed before runtime
  1152. Node.prototype._evaluateMemberDot = function (feature) {
  1153. if (checkFeature(this._left)) {
  1154. return getFeatureProperty(feature, this._right.evaluate(feature));
  1155. }
  1156. const property = this._left.evaluate(feature);
  1157. if (!defined(property)) {
  1158. return undefined;
  1159. }
  1160. const member = this._right.evaluate(feature);
  1161. if (
  1162. property instanceof Cartesian2 ||
  1163. property instanceof Cartesian3 ||
  1164. property instanceof Cartesian4
  1165. ) {
  1166. // Vector components may be accessed with .r, .g, .b, .a and implicitly with .x, .y, .z, .w
  1167. if (member === "r") {
  1168. return property.x;
  1169. } else if (member === "g") {
  1170. return property.y;
  1171. } else if (member === "b") {
  1172. return property.z;
  1173. } else if (member === "a") {
  1174. return property.w;
  1175. }
  1176. }
  1177. return property[member];
  1178. };
  1179. Node.prototype._evaluateMemberBrackets = function (feature) {
  1180. if (checkFeature(this._left)) {
  1181. return getFeatureProperty(feature, this._right.evaluate(feature));
  1182. }
  1183. const property = this._left.evaluate(feature);
  1184. if (!defined(property)) {
  1185. return undefined;
  1186. }
  1187. const member = this._right.evaluate(feature);
  1188. if (
  1189. property instanceof Cartesian2 ||
  1190. property instanceof Cartesian3 ||
  1191. property instanceof Cartesian4
  1192. ) {
  1193. // Vector components may be accessed with [0][1][2][3], ['r']['g']['b']['a'] and implicitly with ['x']['y']['z']['w']
  1194. // For Cartesian2 and Cartesian3 out-of-range components will just return undefined
  1195. if (member === 0 || member === "r") {
  1196. return property.x;
  1197. } else if (member === 1 || member === "g") {
  1198. return property.y;
  1199. } else if (member === 2 || member === "b") {
  1200. return property.z;
  1201. } else if (member === 3 || member === "a") {
  1202. return property.w;
  1203. }
  1204. }
  1205. return property[member];
  1206. };
  1207. Node.prototype._evaluateArray = function (feature) {
  1208. const array = [];
  1209. for (let i = 0; i < this._value.length; i++) {
  1210. array[i] = this._value[i].evaluate(feature);
  1211. }
  1212. return array;
  1213. };
  1214. // PERFORMANCE_IDEA: Have "fast path" functions that deal only with specific types
  1215. // that we can assign if we know the types before runtime
  1216. Node.prototype._evaluateNot = function (feature) {
  1217. const left = this._left.evaluate(feature);
  1218. if (typeof left !== "boolean") {
  1219. throw new RuntimeError(
  1220. `Operator "!" requires a boolean argument. Argument is ${left}.`
  1221. );
  1222. }
  1223. return !left;
  1224. };
  1225. Node.prototype._evaluateNegative = function (feature) {
  1226. const left = this._left.evaluate(feature);
  1227. if (left instanceof Cartesian2) {
  1228. return Cartesian2.negate(left, scratchStorage.getCartesian2());
  1229. } else if (left instanceof Cartesian3) {
  1230. return Cartesian3.negate(left, scratchStorage.getCartesian3());
  1231. } else if (left instanceof Cartesian4) {
  1232. return Cartesian4.negate(left, scratchStorage.getCartesian4());
  1233. } else if (typeof left === "number") {
  1234. return -left;
  1235. }
  1236. throw new RuntimeError(
  1237. `Operator "-" requires a vector or number argument. Argument is ${left}.`
  1238. );
  1239. };
  1240. Node.prototype._evaluatePositive = function (feature) {
  1241. const left = this._left.evaluate(feature);
  1242. if (
  1243. !(
  1244. left instanceof Cartesian2 ||
  1245. left instanceof Cartesian3 ||
  1246. left instanceof Cartesian4 ||
  1247. typeof left === "number"
  1248. )
  1249. ) {
  1250. throw new RuntimeError(
  1251. `Operator "+" requires a vector or number argument. Argument is ${left}.`
  1252. );
  1253. }
  1254. return left;
  1255. };
  1256. Node.prototype._evaluateLessThan = function (feature) {
  1257. const left = this._left.evaluate(feature);
  1258. const right = this._right.evaluate(feature);
  1259. if (typeof left !== "number" || typeof right !== "number") {
  1260. throw new RuntimeError(
  1261. `Operator "<" requires number arguments. Arguments are ${left} and ${right}.`
  1262. );
  1263. }
  1264. return left < right;
  1265. };
  1266. Node.prototype._evaluateLessThanOrEquals = function (feature) {
  1267. const left = this._left.evaluate(feature);
  1268. const right = this._right.evaluate(feature);
  1269. if (typeof left !== "number" || typeof right !== "number") {
  1270. throw new RuntimeError(
  1271. `Operator "<=" requires number arguments. Arguments are ${left} and ${right}.`
  1272. );
  1273. }
  1274. return left <= right;
  1275. };
  1276. Node.prototype._evaluateGreaterThan = function (feature) {
  1277. const left = this._left.evaluate(feature);
  1278. const right = this._right.evaluate(feature);
  1279. if (typeof left !== "number" || typeof right !== "number") {
  1280. throw new RuntimeError(
  1281. `Operator ">" requires number arguments. Arguments are ${left} and ${right}.`
  1282. );
  1283. }
  1284. return left > right;
  1285. };
  1286. Node.prototype._evaluateGreaterThanOrEquals = function (feature) {
  1287. const left = this._left.evaluate(feature);
  1288. const right = this._right.evaluate(feature);
  1289. if (typeof left !== "number" || typeof right !== "number") {
  1290. throw new RuntimeError(
  1291. `Operator ">=" requires number arguments. Arguments are ${left} and ${right}.`
  1292. );
  1293. }
  1294. return left >= right;
  1295. };
  1296. Node.prototype._evaluateOr = function (feature) {
  1297. const left = this._left.evaluate(feature);
  1298. if (typeof left !== "boolean") {
  1299. throw new RuntimeError(
  1300. `Operator "||" requires boolean arguments. First argument is ${left}.`
  1301. );
  1302. }
  1303. // short circuit the expression
  1304. if (left) {
  1305. return true;
  1306. }
  1307. const right = this._right.evaluate(feature);
  1308. if (typeof right !== "boolean") {
  1309. throw new RuntimeError(
  1310. `Operator "||" requires boolean arguments. Second argument is ${right}.`
  1311. );
  1312. }
  1313. return left || right;
  1314. };
  1315. Node.prototype._evaluateAnd = function (feature) {
  1316. const left = this._left.evaluate(feature);
  1317. if (typeof left !== "boolean") {
  1318. throw new RuntimeError(
  1319. `Operator "&&" requires boolean arguments. First argument is ${left}.`
  1320. );
  1321. }
  1322. // short circuit the expression
  1323. if (!left) {
  1324. return false;
  1325. }
  1326. const right = this._right.evaluate(feature);
  1327. if (typeof right !== "boolean") {
  1328. throw new RuntimeError(
  1329. `Operator "&&" requires boolean arguments. Second argument is ${right}.`
  1330. );
  1331. }
  1332. return left && right;
  1333. };
  1334. Node.prototype._evaluatePlus = function (feature) {
  1335. const left = this._left.evaluate(feature);
  1336. const right = this._right.evaluate(feature);
  1337. if (right instanceof Cartesian2 && left instanceof Cartesian2) {
  1338. return Cartesian2.add(left, right, scratchStorage.getCartesian2());
  1339. } else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
  1340. return Cartesian3.add(left, right, scratchStorage.getCartesian3());
  1341. } else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
  1342. return Cartesian4.add(left, right, scratchStorage.getCartesian4());
  1343. } else if (typeof left === "string" || typeof right === "string") {
  1344. // If only one argument is a string the other argument calls its toString function.
  1345. return left + right;
  1346. } else if (typeof left === "number" && typeof right === "number") {
  1347. return left + right;
  1348. }
  1349. throw new RuntimeError(
  1350. `Operator "+" requires vector or number arguments of matching types, or at least one string argument. Arguments are ${left} and ${right}.`
  1351. );
  1352. };
  1353. Node.prototype._evaluateMinus = function (feature) {
  1354. const left = this._left.evaluate(feature);
  1355. const right = this._right.evaluate(feature);
  1356. if (right instanceof Cartesian2 && left instanceof Cartesian2) {
  1357. return Cartesian2.subtract(left, right, scratchStorage.getCartesian2());
  1358. } else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
  1359. return Cartesian3.subtract(left, right, scratchStorage.getCartesian3());
  1360. } else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
  1361. return Cartesian4.subtract(left, right, scratchStorage.getCartesian4());
  1362. } else if (typeof left === "number" && typeof right === "number") {
  1363. return left - right;
  1364. }
  1365. throw new RuntimeError(
  1366. `Operator "-" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`
  1367. );
  1368. };
  1369. Node.prototype._evaluateTimes = function (feature) {
  1370. const left = this._left.evaluate(feature);
  1371. const right = this._right.evaluate(feature);
  1372. if (right instanceof Cartesian2 && left instanceof Cartesian2) {
  1373. return Cartesian2.multiplyComponents(
  1374. left,
  1375. right,
  1376. scratchStorage.getCartesian2()
  1377. );
  1378. } else if (right instanceof Cartesian2 && typeof left === "number") {
  1379. return Cartesian2.multiplyByScalar(
  1380. right,
  1381. left,
  1382. scratchStorage.getCartesian2()
  1383. );
  1384. } else if (left instanceof Cartesian2 && typeof right === "number") {
  1385. return Cartesian2.multiplyByScalar(
  1386. left,
  1387. right,
  1388. scratchStorage.getCartesian2()
  1389. );
  1390. } else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
  1391. return Cartesian3.multiplyComponents(
  1392. left,
  1393. right,
  1394. scratchStorage.getCartesian3()
  1395. );
  1396. } else if (right instanceof Cartesian3 && typeof left === "number") {
  1397. return Cartesian3.multiplyByScalar(
  1398. right,
  1399. left,
  1400. scratchStorage.getCartesian3()
  1401. );
  1402. } else if (left instanceof Cartesian3 && typeof right === "number") {
  1403. return Cartesian3.multiplyByScalar(
  1404. left,
  1405. right,
  1406. scratchStorage.getCartesian3()
  1407. );
  1408. } else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
  1409. return Cartesian4.multiplyComponents(
  1410. left,
  1411. right,
  1412. scratchStorage.getCartesian4()
  1413. );
  1414. } else if (right instanceof Cartesian4 && typeof left === "number") {
  1415. return Cartesian4.multiplyByScalar(
  1416. right,
  1417. left,
  1418. scratchStorage.getCartesian4()
  1419. );
  1420. } else if (left instanceof Cartesian4 && typeof right === "number") {
  1421. return Cartesian4.multiplyByScalar(
  1422. left,
  1423. right,
  1424. scratchStorage.getCartesian4()
  1425. );
  1426. } else if (typeof left === "number" && typeof right === "number") {
  1427. return left * right;
  1428. }
  1429. throw new RuntimeError(
  1430. `Operator "*" requires vector or number arguments. If both arguments are vectors they must be matching types. Arguments are ${left} and ${right}.`
  1431. );
  1432. };
  1433. Node.prototype._evaluateDivide = function (feature) {
  1434. const left = this._left.evaluate(feature);
  1435. const right = this._right.evaluate(feature);
  1436. if (right instanceof Cartesian2 && left instanceof Cartesian2) {
  1437. return Cartesian2.divideComponents(
  1438. left,
  1439. right,
  1440. scratchStorage.getCartesian2()
  1441. );
  1442. } else if (left instanceof Cartesian2 && typeof right === "number") {
  1443. return Cartesian2.divideByScalar(
  1444. left,
  1445. right,
  1446. scratchStorage.getCartesian2()
  1447. );
  1448. } else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
  1449. return Cartesian3.divideComponents(
  1450. left,
  1451. right,
  1452. scratchStorage.getCartesian3()
  1453. );
  1454. } else if (left instanceof Cartesian3 && typeof right === "number") {
  1455. return Cartesian3.divideByScalar(
  1456. left,
  1457. right,
  1458. scratchStorage.getCartesian3()
  1459. );
  1460. } else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
  1461. return Cartesian4.divideComponents(
  1462. left,
  1463. right,
  1464. scratchStorage.getCartesian4()
  1465. );
  1466. } else if (left instanceof Cartesian4 && typeof right === "number") {
  1467. return Cartesian4.divideByScalar(
  1468. left,
  1469. right,
  1470. scratchStorage.getCartesian4()
  1471. );
  1472. } else if (typeof left === "number" && typeof right === "number") {
  1473. return left / right;
  1474. }
  1475. throw new RuntimeError(
  1476. `Operator "/" requires vector or number arguments of matching types, or a number as the second argument. Arguments are ${left} and ${right}.`
  1477. );
  1478. };
  1479. Node.prototype._evaluateMod = function (feature) {
  1480. const left = this._left.evaluate(feature);
  1481. const right = this._right.evaluate(feature);
  1482. if (right instanceof Cartesian2 && left instanceof Cartesian2) {
  1483. return Cartesian2.fromElements(
  1484. left.x % right.x,
  1485. left.y % right.y,
  1486. scratchStorage.getCartesian2()
  1487. );
  1488. } else if (right instanceof Cartesian3 && left instanceof Cartesian3) {
  1489. return Cartesian3.fromElements(
  1490. left.x % right.x,
  1491. left.y % right.y,
  1492. left.z % right.z,
  1493. scratchStorage.getCartesian3()
  1494. );
  1495. } else if (right instanceof Cartesian4 && left instanceof Cartesian4) {
  1496. return Cartesian4.fromElements(
  1497. left.x % right.x,
  1498. left.y % right.y,
  1499. left.z % right.z,
  1500. left.w % right.w,
  1501. scratchStorage.getCartesian4()
  1502. );
  1503. } else if (typeof left === "number" && typeof right === "number") {
  1504. return left % right;
  1505. }
  1506. throw new RuntimeError(
  1507. `Operator "%" requires vector or number arguments of matching types. Arguments are ${left} and ${right}.`
  1508. );
  1509. };
  1510. Node.prototype._evaluateEqualsStrict = function (feature) {
  1511. const left = this._left.evaluate(feature);
  1512. const right = this._right.evaluate(feature);
  1513. if (
  1514. (right instanceof Cartesian2 && left instanceof Cartesian2) ||
  1515. (right instanceof Cartesian3 && left instanceof Cartesian3) ||
  1516. (right instanceof Cartesian4 && left instanceof Cartesian4)
  1517. ) {
  1518. return left.equals(right);
  1519. }
  1520. return left === right;
  1521. };
  1522. Node.prototype._evaluateNotEqualsStrict = function (feature) {
  1523. const left = this._left.evaluate(feature);
  1524. const right = this._right.evaluate(feature);
  1525. if (
  1526. (right instanceof Cartesian2 && left instanceof Cartesian2) ||
  1527. (right instanceof Cartesian3 && left instanceof Cartesian3) ||
  1528. (right instanceof Cartesian4 && left instanceof Cartesian4)
  1529. ) {
  1530. return !left.equals(right);
  1531. }
  1532. return left !== right;
  1533. };
  1534. Node.prototype._evaluateConditional = function (feature) {
  1535. const test = this._test.evaluate(feature);
  1536. if (typeof test !== "boolean") {
  1537. throw new RuntimeError(
  1538. `Conditional argument of conditional expression must be a boolean. Argument is ${test}.`
  1539. );
  1540. }
  1541. if (test) {
  1542. return this._left.evaluate(feature);
  1543. }
  1544. return this._right.evaluate(feature);
  1545. };
  1546. Node.prototype._evaluateNaN = function (feature) {
  1547. return isNaN(this._left.evaluate(feature));
  1548. };
  1549. Node.prototype._evaluateIsFinite = function (feature) {
  1550. return isFinite(this._left.evaluate(feature));
  1551. };
  1552. Node.prototype._evaluateIsExactClass = function (feature) {
  1553. if (defined(feature)) {
  1554. return feature.isExactClass(this._left.evaluate(feature));
  1555. }
  1556. return false;
  1557. };
  1558. Node.prototype._evaluateIsClass = function (feature) {
  1559. if (defined(feature)) {
  1560. return feature.isClass(this._left.evaluate(feature));
  1561. }
  1562. return false;
  1563. };
  1564. Node.prototype._evaluateGetExactClassName = function (feature) {
  1565. if (defined(feature)) {
  1566. return feature.getExactClassName();
  1567. }
  1568. };
  1569. Node.prototype._evaluateBooleanConversion = function (feature) {
  1570. return Boolean(this._left.evaluate(feature));
  1571. };
  1572. Node.prototype._evaluateNumberConversion = function (feature) {
  1573. return Number(this._left.evaluate(feature));
  1574. };
  1575. Node.prototype._evaluateStringConversion = function (feature) {
  1576. return String(this._left.evaluate(feature));
  1577. };
  1578. Node.prototype._evaluateRegExp = function (feature) {
  1579. const pattern = this._value.evaluate(feature);
  1580. let flags = "";
  1581. if (defined(this._left)) {
  1582. flags = this._left.evaluate(feature);
  1583. }
  1584. let exp;
  1585. try {
  1586. exp = new RegExp(pattern, flags);
  1587. } catch (e) {
  1588. throw new RuntimeError(e);
  1589. }
  1590. return exp;
  1591. };
  1592. Node.prototype._evaluateRegExpTest = function (feature) {
  1593. const left = this._left.evaluate(feature);
  1594. const right = this._right.evaluate(feature);
  1595. if (!(left instanceof RegExp && typeof right === "string")) {
  1596. throw new RuntimeError(
  1597. `RegExp.test requires the first argument to be a RegExp and the second argument to be a string. Arguments are ${left} and ${right}.`
  1598. );
  1599. }
  1600. return left.test(right);
  1601. };
  1602. Node.prototype._evaluateRegExpMatch = function (feature) {
  1603. const left = this._left.evaluate(feature);
  1604. const right = this._right.evaluate(feature);
  1605. if (left instanceof RegExp && typeof right === "string") {
  1606. return left.test(right);
  1607. } else if (right instanceof RegExp && typeof left === "string") {
  1608. return right.test(left);
  1609. }
  1610. throw new RuntimeError(
  1611. `Operator "=~" requires one RegExp argument and one string argument. Arguments are ${left} and ${right}.`
  1612. );
  1613. };
  1614. Node.prototype._evaluateRegExpNotMatch = function (feature) {
  1615. const left = this._left.evaluate(feature);
  1616. const right = this._right.evaluate(feature);
  1617. if (left instanceof RegExp && typeof right === "string") {
  1618. return !left.test(right);
  1619. } else if (right instanceof RegExp && typeof left === "string") {
  1620. return !right.test(left);
  1621. }
  1622. throw new RuntimeError(
  1623. `Operator "!~" requires one RegExp argument and one string argument. Arguments are ${left} and ${right}.`
  1624. );
  1625. };
  1626. Node.prototype._evaluateRegExpExec = function (feature) {
  1627. const left = this._left.evaluate(feature);
  1628. const right = this._right.evaluate(feature);
  1629. if (!(left instanceof RegExp && typeof right === "string")) {
  1630. throw new RuntimeError(
  1631. `RegExp.exec requires the first argument to be a RegExp and the second argument to be a string. Arguments are ${left} and ${right}.`
  1632. );
  1633. }
  1634. const exec = left.exec(right);
  1635. if (!defined(exec)) {
  1636. return null;
  1637. }
  1638. return exec[1];
  1639. };
  1640. Node.prototype._evaluateToString = function (feature) {
  1641. const left = this._left.evaluate(feature);
  1642. if (
  1643. left instanceof RegExp ||
  1644. left instanceof Cartesian2 ||
  1645. left instanceof Cartesian3 ||
  1646. left instanceof Cartesian4
  1647. ) {
  1648. return String(left);
  1649. }
  1650. throw new RuntimeError(`Unexpected function call "${this._value}".`);
  1651. };
  1652. function convertHSLToRGB(ast) {
  1653. // Check if the color contains any nested expressions to see if the color can be converted here.
  1654. // E.g. "hsl(0.9, 0.6, 0.7)" is able to convert directly to rgb, "hsl(0.9, 0.6, ${Height})" is not.
  1655. const channels = ast._left;
  1656. const length = channels.length;
  1657. for (let i = 0; i < length; ++i) {
  1658. if (channels[i]._type !== ExpressionNodeType.LITERAL_NUMBER) {
  1659. return undefined;
  1660. }
  1661. }
  1662. const h = channels[0]._value;
  1663. const s = channels[1]._value;
  1664. const l = channels[2]._value;
  1665. const a = length === 4 ? channels[3]._value : 1.0;
  1666. return Color.fromHsl(h, s, l, a, scratchColor);
  1667. }
  1668. function convertRGBToColor(ast) {
  1669. // Check if the color contains any nested expressions to see if the color can be converted here.
  1670. // E.g. "rgb(255, 255, 255)" is able to convert directly to Color, "rgb(255, 255, ${Height})" is not.
  1671. const channels = ast._left;
  1672. const length = channels.length;
  1673. for (let i = 0; i < length; ++i) {
  1674. if (channels[i]._type !== ExpressionNodeType.LITERAL_NUMBER) {
  1675. return undefined;
  1676. }
  1677. }
  1678. const color = scratchColor;
  1679. color.red = channels[0]._value / 255.0;
  1680. color.green = channels[1]._value / 255.0;
  1681. color.blue = channels[2]._value / 255.0;
  1682. color.alpha = length === 4 ? channels[3]._value : 1.0;
  1683. return color;
  1684. }
  1685. function numberToString(number) {
  1686. if (number % 1 === 0) {
  1687. // Add a .0 to whole numbers
  1688. return number.toFixed(1);
  1689. }
  1690. return number.toString();
  1691. }
  1692. function colorToVec3(color) {
  1693. const r = numberToString(color.red);
  1694. const g = numberToString(color.green);
  1695. const b = numberToString(color.blue);
  1696. return `vec3(${r}, ${g}, ${b})`;
  1697. }
  1698. function colorToVec4(color) {
  1699. const r = numberToString(color.red);
  1700. const g = numberToString(color.green);
  1701. const b = numberToString(color.blue);
  1702. const a = numberToString(color.alpha);
  1703. return `vec4(${r}, ${g}, ${b}, ${a})`;
  1704. }
  1705. function getExpressionArray(
  1706. array,
  1707. variableSubstitutionMap,
  1708. shaderState,
  1709. parent
  1710. ) {
  1711. const length = array.length;
  1712. const expressions = new Array(length);
  1713. for (let i = 0; i < length; ++i) {
  1714. expressions[i] = array[i].getShaderExpression(
  1715. variableSubstitutionMap,
  1716. shaderState,
  1717. parent
  1718. );
  1719. }
  1720. return expressions;
  1721. }
  1722. function getVariableName(variableName, variableSubstitutionMap) {
  1723. if (!defined(variableSubstitutionMap[variableName])) {
  1724. return Expression.NULL_SENTINEL;
  1725. }
  1726. return variableSubstitutionMap[variableName];
  1727. }
  1728. /**
  1729. * @private
  1730. */
  1731. 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.
  1732. Node.prototype.getShaderExpression = function (
  1733. variableSubstitutionMap,
  1734. shaderState,
  1735. parent
  1736. ) {
  1737. let color;
  1738. let left;
  1739. let right;
  1740. let test;
  1741. const type = this._type;
  1742. let value = this._value;
  1743. if (defined(this._left)) {
  1744. if (Array.isArray(this._left)) {
  1745. // Left can be an array if the type is LITERAL_COLOR or LITERAL_VECTOR
  1746. left = getExpressionArray(
  1747. this._left,
  1748. variableSubstitutionMap,
  1749. shaderState,
  1750. this
  1751. );
  1752. } else {
  1753. left = this._left.getShaderExpression(
  1754. variableSubstitutionMap,
  1755. shaderState,
  1756. this
  1757. );
  1758. }
  1759. }
  1760. if (defined(this._right)) {
  1761. right = this._right.getShaderExpression(
  1762. variableSubstitutionMap,
  1763. shaderState,
  1764. this
  1765. );
  1766. }
  1767. if (defined(this._test)) {
  1768. test = this._test.getShaderExpression(
  1769. variableSubstitutionMap,
  1770. shaderState,
  1771. this
  1772. );
  1773. }
  1774. if (Array.isArray(this._value)) {
  1775. // For ARRAY type
  1776. value = getExpressionArray(
  1777. this._value,
  1778. variableSubstitutionMap,
  1779. shaderState,
  1780. this
  1781. );
  1782. }
  1783. let args;
  1784. let length;
  1785. let vectorExpression;
  1786. switch (type) {
  1787. case ExpressionNodeType.VARIABLE:
  1788. if (checkFeature(this)) {
  1789. return undefined;
  1790. }
  1791. return getVariableName(value, variableSubstitutionMap);
  1792. case ExpressionNodeType.UNARY:
  1793. // Supported types: +, -, !, Boolean, Number
  1794. if (value === "Boolean") {
  1795. return `bool(${left})`;
  1796. } else if (value === "Number") {
  1797. return `float(${left})`;
  1798. } else if (value === "round") {
  1799. return `floor(${left} + 0.5)`;
  1800. } else if (defined(unaryFunctions[value])) {
  1801. return `${value}(${left})`;
  1802. } else if (value === "isNaN") {
  1803. // In GLSL 2.0 use isnan instead
  1804. return `(${left} != ${left})`;
  1805. } else if (value === "isFinite") {
  1806. // 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.
  1807. return `(abs(${left}) < czm_infinity)`;
  1808. } else if (
  1809. value === "String" ||
  1810. value === "isExactClass" ||
  1811. value === "isClass" ||
  1812. value === "getExactClassName"
  1813. ) {
  1814. throw new RuntimeError(
  1815. `Error generating style shader: "${value}" is not supported.`
  1816. );
  1817. }
  1818. return value + left;
  1819. case ExpressionNodeType.BINARY:
  1820. // Supported types: ||, &&, ===, !==, <, >, <=, >=, +, -, *, /, %
  1821. if (value === "%") {
  1822. return `mod(${left}, ${right})`;
  1823. } else if (value === "===") {
  1824. return `(${left} == ${right})`;
  1825. } else if (value === "!==") {
  1826. return `(${left} != ${right})`;
  1827. } else if (value === "atan2") {
  1828. return `atan(${left}, ${right})`;
  1829. } else if (defined(binaryFunctions[value])) {
  1830. return `${value}(${left}, ${right})`;
  1831. }
  1832. return `(${left} ${value} ${right})`;
  1833. case ExpressionNodeType.TERNARY:
  1834. if (defined(ternaryFunctions[value])) {
  1835. return `${value}(${left}, ${right}, ${test})`;
  1836. }
  1837. break;
  1838. case ExpressionNodeType.CONDITIONAL:
  1839. return `(${test} ? ${left} : ${right})`;
  1840. case ExpressionNodeType.MEMBER:
  1841. if (checkFeature(this._left)) {
  1842. return getVariableName(right, variableSubstitutionMap);
  1843. }
  1844. // This is intended for accessing the components of vector properties. String members aren't supported.
  1845. // Check for 0.0 rather than 0 because all numbers are previously converted to decimals.
  1846. if (right === "r" || right === "x" || right === "0.0") {
  1847. return `${left}[0]`;
  1848. } else if (right === "g" || right === "y" || right === "1.0") {
  1849. return `${left}[1]`;
  1850. } else if (right === "b" || right === "z" || right === "2.0") {
  1851. return `${left}[2]`;
  1852. } else if (right === "a" || right === "w" || right === "3.0") {
  1853. return `${left}[3]`;
  1854. }
  1855. return `${left}[int(${right})]`;
  1856. case ExpressionNodeType.FUNCTION_CALL:
  1857. throw new RuntimeError(
  1858. `Error generating style shader: "${value}" is not supported.`
  1859. );
  1860. case ExpressionNodeType.ARRAY:
  1861. if (value.length === 4) {
  1862. return `vec4(${value[0]}, ${value[1]}, ${value[2]}, ${value[3]})`;
  1863. } else if (value.length === 3) {
  1864. return `vec3(${value[0]}, ${value[1]}, ${value[2]})`;
  1865. } else if (value.length === 2) {
  1866. return `vec2(${value[0]}, ${value[1]})`;
  1867. }
  1868. throw new RuntimeError(
  1869. "Error generating style shader: Invalid array length. Array length should be 2, 3, or 4."
  1870. );
  1871. case ExpressionNodeType.REGEX:
  1872. throw new RuntimeError(
  1873. "Error generating style shader: Regular expressions are not supported."
  1874. );
  1875. case ExpressionNodeType.VARIABLE_IN_STRING:
  1876. throw new RuntimeError(
  1877. "Error generating style shader: Converting a variable to a string is not supported."
  1878. );
  1879. case ExpressionNodeType.LITERAL_NULL:
  1880. return Expression.NULL_SENTINEL;
  1881. case ExpressionNodeType.LITERAL_BOOLEAN:
  1882. return value ? "true" : "false";
  1883. case ExpressionNodeType.LITERAL_NUMBER:
  1884. return numberToString(value);
  1885. case ExpressionNodeType.LITERAL_STRING:
  1886. if (defined(parent) && parent._type === ExpressionNodeType.MEMBER) {
  1887. if (
  1888. value === "r" ||
  1889. value === "g" ||
  1890. value === "b" ||
  1891. value === "a" ||
  1892. value === "x" ||
  1893. value === "y" ||
  1894. value === "z" ||
  1895. value === "w" ||
  1896. checkFeature(parent._left)
  1897. ) {
  1898. return value;
  1899. }
  1900. }
  1901. // Check for css color strings
  1902. color = Color.fromCssColorString(value, scratchColor);
  1903. if (defined(color)) {
  1904. return colorToVec3(color);
  1905. }
  1906. throw new RuntimeError(
  1907. "Error generating style shader: String literals are not supported."
  1908. );
  1909. case ExpressionNodeType.LITERAL_COLOR:
  1910. args = left;
  1911. if (value === "color") {
  1912. if (!defined(args)) {
  1913. return "vec4(1.0)";
  1914. } else if (args.length > 1) {
  1915. const rgb = args[0];
  1916. const alpha = args[1];
  1917. if (alpha !== "1.0") {
  1918. shaderState.translucent = true;
  1919. }
  1920. return `vec4(${rgb}, ${alpha})`;
  1921. }
  1922. return `vec4(${args[0]}, 1.0)`;
  1923. } else if (value === "rgb") {
  1924. color = convertRGBToColor(this);
  1925. if (defined(color)) {
  1926. return colorToVec4(color);
  1927. }
  1928. return `vec4(${args[0]} / 255.0, ${args[1]} / 255.0, ${args[2]} / 255.0, 1.0)`;
  1929. } else if (value === "rgba") {
  1930. if (args[3] !== "1.0") {
  1931. shaderState.translucent = true;
  1932. }
  1933. color = convertRGBToColor(this);
  1934. if (defined(color)) {
  1935. return colorToVec4(color);
  1936. }
  1937. return `vec4(${args[0]} / 255.0, ${args[1]} / 255.0, ${args[2]} / 255.0, ${args[3]})`;
  1938. } else if (value === "hsl") {
  1939. color = convertHSLToRGB(this);
  1940. if (defined(color)) {
  1941. return colorToVec4(color);
  1942. }
  1943. return `vec4(czm_HSLToRGB(vec3(${args[0]}, ${args[1]}, ${args[2]})), 1.0)`;
  1944. } else if (value === "hsla") {
  1945. color = convertHSLToRGB(this);
  1946. if (defined(color)) {
  1947. if (color.alpha !== 1.0) {
  1948. shaderState.translucent = true;
  1949. }
  1950. return colorToVec4(color);
  1951. }
  1952. if (args[3] !== "1.0") {
  1953. shaderState.translucent = true;
  1954. }
  1955. return `vec4(czm_HSLToRGB(vec3(${args[0]}, ${args[1]}, ${args[2]})), ${args[3]})`;
  1956. }
  1957. break;
  1958. case ExpressionNodeType.LITERAL_VECTOR:
  1959. //>>includeStart('debug', pragmas.debug);
  1960. if (!defined(left)) {
  1961. throw new DeveloperError(
  1962. "left should always be defined for type ExpressionNodeType.LITERAL_VECTOR"
  1963. );
  1964. }
  1965. //>>includeEnd('debug');
  1966. length = left.length;
  1967. vectorExpression = `${value}(`;
  1968. for (let i = 0; i < length; ++i) {
  1969. vectorExpression += left[i];
  1970. if (i < length - 1) {
  1971. vectorExpression += ", ";
  1972. }
  1973. }
  1974. vectorExpression += ")";
  1975. return vectorExpression;
  1976. case ExpressionNodeType.LITERAL_REGEX:
  1977. throw new RuntimeError(
  1978. "Error generating style shader: Regular expressions are not supported."
  1979. );
  1980. case ExpressionNodeType.LITERAL_UNDEFINED:
  1981. return Expression.NULL_SENTINEL;
  1982. case ExpressionNodeType.BUILTIN_VARIABLE:
  1983. if (value === "tiles3d_tileset_time") {
  1984. return value;
  1985. }
  1986. }
  1987. };
  1988. Node.prototype.getVariables = function (variables, parent) {
  1989. let array;
  1990. let length;
  1991. let i;
  1992. const type = this._type;
  1993. const value = this._value;
  1994. if (defined(this._left)) {
  1995. if (Array.isArray(this._left)) {
  1996. // Left can be an array if the type is LITERAL_COLOR or LITERAL_VECTOR
  1997. array = this._left;
  1998. length = array.length;
  1999. for (i = 0; i < length; ++i) {
  2000. array[i].getVariables(variables, this);
  2001. }
  2002. } else {
  2003. this._left.getVariables(variables, this);
  2004. }
  2005. }
  2006. if (defined(this._right)) {
  2007. this._right.getVariables(variables, this);
  2008. }
  2009. if (defined(this._test)) {
  2010. this._test.getVariables(variables, this);
  2011. }
  2012. if (Array.isArray(this._value)) {
  2013. // For ARRAY type
  2014. array = this._value;
  2015. length = array.length;
  2016. for (i = 0; i < length; ++i) {
  2017. array[i].getVariables(variables, this);
  2018. }
  2019. }
  2020. let match;
  2021. switch (type) {
  2022. case ExpressionNodeType.VARIABLE:
  2023. if (!checkFeature(this)) {
  2024. variables.push(value);
  2025. }
  2026. break;
  2027. case ExpressionNodeType.VARIABLE_IN_STRING:
  2028. match = variableRegex.exec(value);
  2029. while (match !== null) {
  2030. variables.push(match[1]);
  2031. match = variableRegex.exec(value);
  2032. }
  2033. break;
  2034. case ExpressionNodeType.LITERAL_STRING:
  2035. if (
  2036. defined(parent) &&
  2037. parent._type === ExpressionNodeType.MEMBER &&
  2038. checkFeature(parent._left)
  2039. ) {
  2040. variables.push(value);
  2041. }
  2042. break;
  2043. }
  2044. };
  2045. export default Expression;