PolygonPipeline-cc031b9f.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './Matrix2-fc7e9822', './RuntimeError-c581ca93', './ComponentDatatype-4a60b8d6', './defaultValue-94c3e563', './EllipsoidRhumbLine-daebc75b', './GeometryAttribute-2ecf73f6', './WebGLConstants-7dccdc96'], (function (exports, Matrix2, RuntimeError, ComponentDatatype, defaultValue, EllipsoidRhumbLine, GeometryAttribute, WebGLConstants) { 'use strict';
  3. /* This file is automatically rebuilt by the Cesium build process. */
  4. var earcut_1 = earcut;
  5. var _default = earcut;
  6. function earcut(data, holeIndices, dim) {
  7. dim = dim || 2;
  8. var hasHoles = holeIndices && holeIndices.length,
  9. outerLen = hasHoles ? holeIndices[0] * dim : data.length,
  10. outerNode = linkedList(data, 0, outerLen, dim, true),
  11. triangles = [];
  12. if (!outerNode || outerNode.next === outerNode.prev) return triangles;
  13. var minX, minY, maxX, maxY, x, y, invSize;
  14. if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
  15. // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
  16. if (data.length > 80 * dim) {
  17. minX = maxX = data[0];
  18. minY = maxY = data[1];
  19. for (var i = dim; i < outerLen; i += dim) {
  20. x = data[i];
  21. y = data[i + 1];
  22. if (x < minX) minX = x;
  23. if (y < minY) minY = y;
  24. if (x > maxX) maxX = x;
  25. if (y > maxY) maxY = y;
  26. }
  27. // minX, minY and invSize are later used to transform coords into integers for z-order calculation
  28. invSize = Math.max(maxX - minX, maxY - minY);
  29. invSize = invSize !== 0 ? 1 / invSize : 0;
  30. }
  31. earcutLinked(outerNode, triangles, dim, minX, minY, invSize);
  32. return triangles;
  33. }
  34. // create a circular doubly linked list from polygon points in the specified winding order
  35. function linkedList(data, start, end, dim, clockwise) {
  36. var i, last;
  37. if (clockwise === (signedArea(data, start, end, dim) > 0)) {
  38. for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
  39. } else {
  40. for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
  41. }
  42. if (last && equals(last, last.next)) {
  43. removeNode(last);
  44. last = last.next;
  45. }
  46. return last;
  47. }
  48. // eliminate colinear or duplicate points
  49. function filterPoints(start, end) {
  50. if (!start) return start;
  51. if (!end) end = start;
  52. var p = start,
  53. again;
  54. do {
  55. again = false;
  56. if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
  57. removeNode(p);
  58. p = end = p.prev;
  59. if (p === p.next) break;
  60. again = true;
  61. } else {
  62. p = p.next;
  63. }
  64. } while (again || p !== end);
  65. return end;
  66. }
  67. // main ear slicing loop which triangulates a polygon (given as a linked list)
  68. function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
  69. if (!ear) return;
  70. // interlink polygon nodes in z-order
  71. if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
  72. var stop = ear,
  73. prev, next;
  74. // iterate through ears, slicing them one by one
  75. while (ear.prev !== ear.next) {
  76. prev = ear.prev;
  77. next = ear.next;
  78. if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
  79. // cut off the triangle
  80. triangles.push(prev.i / dim);
  81. triangles.push(ear.i / dim);
  82. triangles.push(next.i / dim);
  83. removeNode(ear);
  84. // skipping the next vertex leads to less sliver triangles
  85. ear = next.next;
  86. stop = next.next;
  87. continue;
  88. }
  89. ear = next;
  90. // if we looped through the whole remaining polygon and can't find any more ears
  91. if (ear === stop) {
  92. // try filtering points and slicing again
  93. if (!pass) {
  94. earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
  95. // if this didn't work, try curing all small self-intersections locally
  96. } else if (pass === 1) {
  97. ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
  98. earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
  99. // as a last resort, try splitting the remaining polygon into two
  100. } else if (pass === 2) {
  101. splitEarcut(ear, triangles, dim, minX, minY, invSize);
  102. }
  103. break;
  104. }
  105. }
  106. }
  107. // check whether a polygon node forms a valid ear with adjacent nodes
  108. function isEar(ear) {
  109. var a = ear.prev,
  110. b = ear,
  111. c = ear.next;
  112. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  113. // now make sure we don't have other points inside the potential ear
  114. var p = ear.next.next;
  115. while (p !== ear.prev) {
  116. if (pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  117. area(p.prev, p, p.next) >= 0) return false;
  118. p = p.next;
  119. }
  120. return true;
  121. }
  122. function isEarHashed(ear, minX, minY, invSize) {
  123. var a = ear.prev,
  124. b = ear,
  125. c = ear.next;
  126. if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
  127. // triangle bbox; min & max are calculated like this for speed
  128. var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x),
  129. minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y),
  130. maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x),
  131. maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y);
  132. // z-order range for the current triangle bbox;
  133. var minZ = zOrder(minTX, minTY, minX, minY, invSize),
  134. maxZ = zOrder(maxTX, maxTY, minX, minY, invSize);
  135. var p = ear.prevZ,
  136. n = ear.nextZ;
  137. // look for points inside the triangle in both directions
  138. while (p && p.z >= minZ && n && n.z <= maxZ) {
  139. if (p !== ear.prev && p !== ear.next &&
  140. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  141. area(p.prev, p, p.next) >= 0) return false;
  142. p = p.prevZ;
  143. if (n !== ear.prev && n !== ear.next &&
  144. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y) &&
  145. area(n.prev, n, n.next) >= 0) return false;
  146. n = n.nextZ;
  147. }
  148. // look for remaining points in decreasing z-order
  149. while (p && p.z >= minZ) {
  150. if (p !== ear.prev && p !== ear.next &&
  151. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  152. area(p.prev, p, p.next) >= 0) return false;
  153. p = p.prevZ;
  154. }
  155. // look for remaining points in increasing z-order
  156. while (n && n.z <= maxZ) {
  157. if (n !== ear.prev && n !== ear.next &&
  158. pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y) &&
  159. area(n.prev, n, n.next) >= 0) return false;
  160. n = n.nextZ;
  161. }
  162. return true;
  163. }
  164. // go through all polygon nodes and cure small local self-intersections
  165. function cureLocalIntersections(start, triangles, dim) {
  166. var p = start;
  167. do {
  168. var a = p.prev,
  169. b = p.next.next;
  170. if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
  171. triangles.push(a.i / dim);
  172. triangles.push(p.i / dim);
  173. triangles.push(b.i / dim);
  174. // remove two nodes involved
  175. removeNode(p);
  176. removeNode(p.next);
  177. p = start = b;
  178. }
  179. p = p.next;
  180. } while (p !== start);
  181. return filterPoints(p);
  182. }
  183. // try splitting polygon into two and triangulate them independently
  184. function splitEarcut(start, triangles, dim, minX, minY, invSize) {
  185. // look for a valid diagonal that divides the polygon into two
  186. var a = start;
  187. do {
  188. var b = a.next.next;
  189. while (b !== a.prev) {
  190. if (a.i !== b.i && isValidDiagonal(a, b)) {
  191. // split the polygon in two by the diagonal
  192. var c = splitPolygon(a, b);
  193. // filter colinear points around the cuts
  194. a = filterPoints(a, a.next);
  195. c = filterPoints(c, c.next);
  196. // run earcut on each half
  197. earcutLinked(a, triangles, dim, minX, minY, invSize);
  198. earcutLinked(c, triangles, dim, minX, minY, invSize);
  199. return;
  200. }
  201. b = b.next;
  202. }
  203. a = a.next;
  204. } while (a !== start);
  205. }
  206. // link every hole into the outer loop, producing a single-ring polygon without holes
  207. function eliminateHoles(data, holeIndices, outerNode, dim) {
  208. var queue = [],
  209. i, len, start, end, list;
  210. for (i = 0, len = holeIndices.length; i < len; i++) {
  211. start = holeIndices[i] * dim;
  212. end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  213. list = linkedList(data, start, end, dim, false);
  214. if (list === list.next) list.steiner = true;
  215. queue.push(getLeftmost(list));
  216. }
  217. queue.sort(compareX);
  218. // process holes from left to right
  219. for (i = 0; i < queue.length; i++) {
  220. outerNode = eliminateHole(queue[i], outerNode);
  221. outerNode = filterPoints(outerNode, outerNode.next);
  222. }
  223. return outerNode;
  224. }
  225. function compareX(a, b) {
  226. return a.x - b.x;
  227. }
  228. // find a bridge between vertices that connects hole with an outer ring and and link it
  229. function eliminateHole(hole, outerNode) {
  230. var bridge = findHoleBridge(hole, outerNode);
  231. if (!bridge) {
  232. return outerNode;
  233. }
  234. var bridgeReverse = splitPolygon(bridge, hole);
  235. // filter collinear points around the cuts
  236. var filteredBridge = filterPoints(bridge, bridge.next);
  237. filterPoints(bridgeReverse, bridgeReverse.next);
  238. // Check if input node was removed by the filtering
  239. return outerNode === bridge ? filteredBridge : outerNode;
  240. }
  241. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  242. function findHoleBridge(hole, outerNode) {
  243. var p = outerNode,
  244. hx = hole.x,
  245. hy = hole.y,
  246. qx = -Infinity,
  247. m;
  248. // find a segment intersected by a ray from the hole's leftmost point to the left;
  249. // segment's endpoint with lesser x will be potential connection point
  250. do {
  251. if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
  252. var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  253. if (x <= hx && x > qx) {
  254. qx = x;
  255. if (x === hx) {
  256. if (hy === p.y) return p;
  257. if (hy === p.next.y) return p.next;
  258. }
  259. m = p.x < p.next.x ? p : p.next;
  260. }
  261. }
  262. p = p.next;
  263. } while (p !== outerNode);
  264. if (!m) return null;
  265. if (hx === qx) return m; // hole touches outer segment; pick leftmost endpoint
  266. // look for points inside the triangle of hole point, segment intersection and endpoint;
  267. // if there are no points found, we have a valid connection;
  268. // otherwise choose the point of the minimum angle with the ray as connection point
  269. var stop = m,
  270. mx = m.x,
  271. my = m.y,
  272. tanMin = Infinity,
  273. tan;
  274. p = m;
  275. do {
  276. if (hx >= p.x && p.x >= mx && hx !== p.x &&
  277. pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  278. tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
  279. if (locallyInside(p, hole) &&
  280. (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
  281. m = p;
  282. tanMin = tan;
  283. }
  284. }
  285. p = p.next;
  286. } while (p !== stop);
  287. return m;
  288. }
  289. // whether sector in vertex m contains sector in vertex p in the same coordinates
  290. function sectorContainsSector(m, p) {
  291. return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
  292. }
  293. // interlink polygon nodes in z-order
  294. function indexCurve(start, minX, minY, invSize) {
  295. var p = start;
  296. do {
  297. if (p.z === null) p.z = zOrder(p.x, p.y, minX, minY, invSize);
  298. p.prevZ = p.prev;
  299. p.nextZ = p.next;
  300. p = p.next;
  301. } while (p !== start);
  302. p.prevZ.nextZ = null;
  303. p.prevZ = null;
  304. sortLinked(p);
  305. }
  306. // Simon Tatham's linked list merge sort algorithm
  307. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  308. function sortLinked(list) {
  309. var i, p, q, e, tail, numMerges, pSize, qSize,
  310. inSize = 1;
  311. do {
  312. p = list;
  313. list = null;
  314. tail = null;
  315. numMerges = 0;
  316. while (p) {
  317. numMerges++;
  318. q = p;
  319. pSize = 0;
  320. for (i = 0; i < inSize; i++) {
  321. pSize++;
  322. q = q.nextZ;
  323. if (!q) break;
  324. }
  325. qSize = inSize;
  326. while (pSize > 0 || (qSize > 0 && q)) {
  327. if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
  328. e = p;
  329. p = p.nextZ;
  330. pSize--;
  331. } else {
  332. e = q;
  333. q = q.nextZ;
  334. qSize--;
  335. }
  336. if (tail) tail.nextZ = e;
  337. else list = e;
  338. e.prevZ = tail;
  339. tail = e;
  340. }
  341. p = q;
  342. }
  343. tail.nextZ = null;
  344. inSize *= 2;
  345. } while (numMerges > 1);
  346. return list;
  347. }
  348. // z-order of a point given coords and inverse of the longer side of data bbox
  349. function zOrder(x, y, minX, minY, invSize) {
  350. // coords are transformed into non-negative 15-bit integer range
  351. x = 32767 * (x - minX) * invSize;
  352. y = 32767 * (y - minY) * invSize;
  353. x = (x | (x << 8)) & 0x00FF00FF;
  354. x = (x | (x << 4)) & 0x0F0F0F0F;
  355. x = (x | (x << 2)) & 0x33333333;
  356. x = (x | (x << 1)) & 0x55555555;
  357. y = (y | (y << 8)) & 0x00FF00FF;
  358. y = (y | (y << 4)) & 0x0F0F0F0F;
  359. y = (y | (y << 2)) & 0x33333333;
  360. y = (y | (y << 1)) & 0x55555555;
  361. return x | (y << 1);
  362. }
  363. // find the leftmost node of a polygon ring
  364. function getLeftmost(start) {
  365. var p = start,
  366. leftmost = start;
  367. do {
  368. if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
  369. p = p.next;
  370. } while (p !== start);
  371. return leftmost;
  372. }
  373. // check if a point lies within a convex triangle
  374. function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
  375. return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
  376. (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
  377. (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
  378. }
  379. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  380. function isValidDiagonal(a, b) {
  381. return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
  382. (locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
  383. (area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
  384. equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
  385. }
  386. // signed area of a triangle
  387. function area(p, q, r) {
  388. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  389. }
  390. // check if two points are equal
  391. function equals(p1, p2) {
  392. return p1.x === p2.x && p1.y === p2.y;
  393. }
  394. // check if two segments intersect
  395. function intersects(p1, q1, p2, q2) {
  396. var o1 = sign(area(p1, q1, p2));
  397. var o2 = sign(area(p1, q1, q2));
  398. var o3 = sign(area(p2, q2, p1));
  399. var o4 = sign(area(p2, q2, q1));
  400. if (o1 !== o2 && o3 !== o4) return true; // general case
  401. if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
  402. if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
  403. if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
  404. if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
  405. return false;
  406. }
  407. // for collinear points p, q, r, check if point q lies on segment pr
  408. function onSegment(p, q, r) {
  409. return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
  410. }
  411. function sign(num) {
  412. return num > 0 ? 1 : num < 0 ? -1 : 0;
  413. }
  414. // check if a polygon diagonal intersects any polygon segments
  415. function intersectsPolygon(a, b) {
  416. var p = a;
  417. do {
  418. if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  419. intersects(p, p.next, a, b)) return true;
  420. p = p.next;
  421. } while (p !== a);
  422. return false;
  423. }
  424. // check if a polygon diagonal is locally inside the polygon
  425. function locallyInside(a, b) {
  426. return area(a.prev, a, a.next) < 0 ?
  427. area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
  428. area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
  429. }
  430. // check if the middle point of a polygon diagonal is inside the polygon
  431. function middleInside(a, b) {
  432. var p = a,
  433. inside = false,
  434. px = (a.x + b.x) / 2,
  435. py = (a.y + b.y) / 2;
  436. do {
  437. if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
  438. (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  439. inside = !inside;
  440. p = p.next;
  441. } while (p !== a);
  442. return inside;
  443. }
  444. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  445. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  446. function splitPolygon(a, b) {
  447. var a2 = new Node(a.i, a.x, a.y),
  448. b2 = new Node(b.i, b.x, b.y),
  449. an = a.next,
  450. bp = b.prev;
  451. a.next = b;
  452. b.prev = a;
  453. a2.next = an;
  454. an.prev = a2;
  455. b2.next = a2;
  456. a2.prev = b2;
  457. bp.next = b2;
  458. b2.prev = bp;
  459. return b2;
  460. }
  461. // create a node and optionally link it with previous one (in a circular doubly linked list)
  462. function insertNode(i, x, y, last) {
  463. var p = new Node(i, x, y);
  464. if (!last) {
  465. p.prev = p;
  466. p.next = p;
  467. } else {
  468. p.next = last.next;
  469. p.prev = last;
  470. last.next.prev = p;
  471. last.next = p;
  472. }
  473. return p;
  474. }
  475. function removeNode(p) {
  476. p.next.prev = p.prev;
  477. p.prev.next = p.next;
  478. if (p.prevZ) p.prevZ.nextZ = p.nextZ;
  479. if (p.nextZ) p.nextZ.prevZ = p.prevZ;
  480. }
  481. function Node(i, x, y) {
  482. // vertex index in coordinates array
  483. this.i = i;
  484. // vertex coordinates
  485. this.x = x;
  486. this.y = y;
  487. // previous and next vertex nodes in a polygon ring
  488. this.prev = null;
  489. this.next = null;
  490. // z-order curve value
  491. this.z = null;
  492. // previous and next nodes in z-order
  493. this.prevZ = null;
  494. this.nextZ = null;
  495. // indicates whether this is a steiner point
  496. this.steiner = false;
  497. }
  498. // return a percentage difference between the polygon area and its triangulation area;
  499. // used to verify correctness of triangulation
  500. earcut.deviation = function (data, holeIndices, dim, triangles) {
  501. var hasHoles = holeIndices && holeIndices.length;
  502. var outerLen = hasHoles ? holeIndices[0] * dim : data.length;
  503. var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
  504. if (hasHoles) {
  505. for (var i = 0, len = holeIndices.length; i < len; i++) {
  506. var start = holeIndices[i] * dim;
  507. var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  508. polygonArea -= Math.abs(signedArea(data, start, end, dim));
  509. }
  510. }
  511. var trianglesArea = 0;
  512. for (i = 0; i < triangles.length; i += 3) {
  513. var a = triangles[i] * dim;
  514. var b = triangles[i + 1] * dim;
  515. var c = triangles[i + 2] * dim;
  516. trianglesArea += Math.abs(
  517. (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
  518. (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
  519. }
  520. return polygonArea === 0 && trianglesArea === 0 ? 0 :
  521. Math.abs((trianglesArea - polygonArea) / polygonArea);
  522. };
  523. function signedArea(data, start, end, dim) {
  524. var sum = 0;
  525. for (var i = start, j = end - dim; i < end; i += dim) {
  526. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  527. j = i;
  528. }
  529. return sum;
  530. }
  531. // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
  532. earcut.flatten = function (data) {
  533. var dim = data[0][0].length,
  534. result = {vertices: [], holes: [], dimensions: dim},
  535. holeIndex = 0;
  536. for (var i = 0; i < data.length; i++) {
  537. for (var j = 0; j < data[i].length; j++) {
  538. for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
  539. }
  540. if (i > 0) {
  541. holeIndex += data[i - 1].length;
  542. result.holes.push(holeIndex);
  543. }
  544. }
  545. return result;
  546. };
  547. earcut_1.default = _default;
  548. /**
  549. * Winding order defines the order of vertices for a triangle to be considered front-facing.
  550. *
  551. * @enum {Number}
  552. */
  553. const WindingOrder = {
  554. /**
  555. * Vertices are in clockwise order.
  556. *
  557. * @type {Number}
  558. * @constant
  559. */
  560. CLOCKWISE: WebGLConstants.WebGLConstants.CW,
  561. /**
  562. * Vertices are in counter-clockwise order.
  563. *
  564. * @type {Number}
  565. * @constant
  566. */
  567. COUNTER_CLOCKWISE: WebGLConstants.WebGLConstants.CCW,
  568. };
  569. /**
  570. * @private
  571. */
  572. WindingOrder.validate = function (windingOrder) {
  573. return (
  574. windingOrder === WindingOrder.CLOCKWISE ||
  575. windingOrder === WindingOrder.COUNTER_CLOCKWISE
  576. );
  577. };
  578. var WindingOrder$1 = Object.freeze(WindingOrder);
  579. const scaleToGeodeticHeightN = new Matrix2.Cartesian3();
  580. const scaleToGeodeticHeightP = new Matrix2.Cartesian3();
  581. /**
  582. * @private
  583. */
  584. const PolygonPipeline = {};
  585. /**
  586. * @exception {DeveloperError} At least three positions are required.
  587. */
  588. PolygonPipeline.computeArea2D = function (positions) {
  589. //>>includeStart('debug', pragmas.debug);
  590. RuntimeError.Check.defined("positions", positions);
  591. RuntimeError.Check.typeOf.number.greaterThanOrEquals(
  592. "positions.length",
  593. positions.length,
  594. 3
  595. );
  596. //>>includeEnd('debug');
  597. const length = positions.length;
  598. let area = 0.0;
  599. for (let i0 = length - 1, i1 = 0; i1 < length; i0 = i1++) {
  600. const v0 = positions[i0];
  601. const v1 = positions[i1];
  602. area += v0.x * v1.y - v1.x * v0.y;
  603. }
  604. return area * 0.5;
  605. };
  606. /**
  607. * @returns {WindingOrder} The winding order.
  608. *
  609. * @exception {DeveloperError} At least three positions are required.
  610. */
  611. PolygonPipeline.computeWindingOrder2D = function (positions) {
  612. const area = PolygonPipeline.computeArea2D(positions);
  613. return area > 0.0 ? WindingOrder$1.COUNTER_CLOCKWISE : WindingOrder$1.CLOCKWISE;
  614. };
  615. /**
  616. * Triangulate a polygon.
  617. *
  618. * @param {Cartesian2[]} positions Cartesian2 array containing the vertices of the polygon
  619. * @param {Number[]} [holes] An array of the staring indices of the holes.
  620. * @returns {Number[]} Index array representing triangles that fill the polygon
  621. */
  622. PolygonPipeline.triangulate = function (positions, holes) {
  623. //>>includeStart('debug', pragmas.debug);
  624. RuntimeError.Check.defined("positions", positions);
  625. //>>includeEnd('debug');
  626. const flattenedPositions = Matrix2.Cartesian2.packArray(positions);
  627. return earcut_1(flattenedPositions, holes, 2);
  628. };
  629. const subdivisionV0Scratch = new Matrix2.Cartesian3();
  630. const subdivisionV1Scratch = new Matrix2.Cartesian3();
  631. const subdivisionV2Scratch = new Matrix2.Cartesian3();
  632. const subdivisionS0Scratch = new Matrix2.Cartesian3();
  633. const subdivisionS1Scratch = new Matrix2.Cartesian3();
  634. const subdivisionS2Scratch = new Matrix2.Cartesian3();
  635. const subdivisionMidScratch = new Matrix2.Cartesian3();
  636. const subdivisionT0Scratch = new Matrix2.Cartesian2();
  637. const subdivisionT1Scratch = new Matrix2.Cartesian2();
  638. const subdivisionT2Scratch = new Matrix2.Cartesian2();
  639. const subdivisionTexcoordMidScratch = new Matrix2.Cartesian2();
  640. /**
  641. * Subdivides positions and raises points to the surface of the ellipsoid.
  642. *
  643. * @param {Ellipsoid} ellipsoid The ellipsoid the polygon in on.
  644. * @param {Cartesian3[]} positions An array of {@link Cartesian3} positions of the polygon.
  645. * @param {Number[]} indices An array of indices that determines the triangles in the polygon.
  646. * @param {Cartesian2[]} texcoords An optional array of {@link Cartesian2} texture coordinates of the polygon.
  647. * @param {Number} [granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  648. *
  649. * @exception {DeveloperError} At least three indices are required.
  650. * @exception {DeveloperError} The number of indices must be divisable by three.
  651. * @exception {DeveloperError} Granularity must be greater than zero.
  652. */
  653. PolygonPipeline.computeSubdivision = function (
  654. ellipsoid,
  655. positions,
  656. indices,
  657. texcoords,
  658. granularity
  659. ) {
  660. granularity = defaultValue.defaultValue(granularity, ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE);
  661. const hasTexcoords = defaultValue.defined(texcoords);
  662. //>>includeStart('debug', pragmas.debug);
  663. RuntimeError.Check.typeOf.object("ellipsoid", ellipsoid);
  664. RuntimeError.Check.defined("positions", positions);
  665. RuntimeError.Check.defined("indices", indices);
  666. RuntimeError.Check.typeOf.number.greaterThanOrEquals("indices.length", indices.length, 3);
  667. RuntimeError.Check.typeOf.number.equals("indices.length % 3", "0", indices.length % 3, 0);
  668. RuntimeError.Check.typeOf.number.greaterThan("granularity", granularity, 0.0);
  669. //>>includeEnd('debug');
  670. // triangles that need (or might need) to be subdivided.
  671. const triangles = indices.slice(0);
  672. // New positions due to edge splits are appended to the positions list.
  673. let i;
  674. const length = positions.length;
  675. const subdividedPositions = new Array(length * 3);
  676. const subdividedTexcoords = new Array(length * 2);
  677. let q = 0;
  678. let p = 0;
  679. for (i = 0; i < length; i++) {
  680. const item = positions[i];
  681. subdividedPositions[q++] = item.x;
  682. subdividedPositions[q++] = item.y;
  683. subdividedPositions[q++] = item.z;
  684. if (hasTexcoords) {
  685. const texcoordItem = texcoords[i];
  686. subdividedTexcoords[p++] = texcoordItem.x;
  687. subdividedTexcoords[p++] = texcoordItem.y;
  688. }
  689. }
  690. const subdividedIndices = [];
  691. // Used to make sure shared edges are not split more than once.
  692. const edges = {};
  693. const radius = ellipsoid.maximumRadius;
  694. const minDistance = ComponentDatatype.CesiumMath.chordLength(granularity, radius);
  695. const minDistanceSqrd = minDistance * minDistance;
  696. while (triangles.length > 0) {
  697. const i2 = triangles.pop();
  698. const i1 = triangles.pop();
  699. const i0 = triangles.pop();
  700. const v0 = Matrix2.Cartesian3.fromArray(
  701. subdividedPositions,
  702. i0 * 3,
  703. subdivisionV0Scratch
  704. );
  705. const v1 = Matrix2.Cartesian3.fromArray(
  706. subdividedPositions,
  707. i1 * 3,
  708. subdivisionV1Scratch
  709. );
  710. const v2 = Matrix2.Cartesian3.fromArray(
  711. subdividedPositions,
  712. i2 * 3,
  713. subdivisionV2Scratch
  714. );
  715. let t0, t1, t2;
  716. if (hasTexcoords) {
  717. t0 = Matrix2.Cartesian2.fromArray(
  718. subdividedTexcoords,
  719. i0 * 2,
  720. subdivisionT0Scratch
  721. );
  722. t1 = Matrix2.Cartesian2.fromArray(
  723. subdividedTexcoords,
  724. i1 * 2,
  725. subdivisionT1Scratch
  726. );
  727. t2 = Matrix2.Cartesian2.fromArray(
  728. subdividedTexcoords,
  729. i2 * 2,
  730. subdivisionT2Scratch
  731. );
  732. }
  733. const s0 = Matrix2.Cartesian3.multiplyByScalar(
  734. Matrix2.Cartesian3.normalize(v0, subdivisionS0Scratch),
  735. radius,
  736. subdivisionS0Scratch
  737. );
  738. const s1 = Matrix2.Cartesian3.multiplyByScalar(
  739. Matrix2.Cartesian3.normalize(v1, subdivisionS1Scratch),
  740. radius,
  741. subdivisionS1Scratch
  742. );
  743. const s2 = Matrix2.Cartesian3.multiplyByScalar(
  744. Matrix2.Cartesian3.normalize(v2, subdivisionS2Scratch),
  745. radius,
  746. subdivisionS2Scratch
  747. );
  748. const g0 = Matrix2.Cartesian3.magnitudeSquared(
  749. Matrix2.Cartesian3.subtract(s0, s1, subdivisionMidScratch)
  750. );
  751. const g1 = Matrix2.Cartesian3.magnitudeSquared(
  752. Matrix2.Cartesian3.subtract(s1, s2, subdivisionMidScratch)
  753. );
  754. const g2 = Matrix2.Cartesian3.magnitudeSquared(
  755. Matrix2.Cartesian3.subtract(s2, s0, subdivisionMidScratch)
  756. );
  757. const max = Math.max(g0, g1, g2);
  758. let edge;
  759. let mid;
  760. let midTexcoord;
  761. // if the max length squared of a triangle edge is greater than the chord length of squared
  762. // of the granularity, subdivide the triangle
  763. if (max > minDistanceSqrd) {
  764. if (g0 === max) {
  765. edge = `${Math.min(i0, i1)} ${Math.max(i0, i1)}`;
  766. i = edges[edge];
  767. if (!defaultValue.defined(i)) {
  768. mid = Matrix2.Cartesian3.add(v0, v1, subdivisionMidScratch);
  769. Matrix2.Cartesian3.multiplyByScalar(mid, 0.5, mid);
  770. subdividedPositions.push(mid.x, mid.y, mid.z);
  771. i = subdividedPositions.length / 3 - 1;
  772. edges[edge] = i;
  773. if (hasTexcoords) {
  774. midTexcoord = Matrix2.Cartesian2.add(t0, t1, subdivisionTexcoordMidScratch);
  775. Matrix2.Cartesian2.multiplyByScalar(midTexcoord, 0.5, midTexcoord);
  776. subdividedTexcoords.push(midTexcoord.x, midTexcoord.y);
  777. }
  778. }
  779. triangles.push(i0, i, i2);
  780. triangles.push(i, i1, i2);
  781. } else if (g1 === max) {
  782. edge = `${Math.min(i1, i2)} ${Math.max(i1, i2)}`;
  783. i = edges[edge];
  784. if (!defaultValue.defined(i)) {
  785. mid = Matrix2.Cartesian3.add(v1, v2, subdivisionMidScratch);
  786. Matrix2.Cartesian3.multiplyByScalar(mid, 0.5, mid);
  787. subdividedPositions.push(mid.x, mid.y, mid.z);
  788. i = subdividedPositions.length / 3 - 1;
  789. edges[edge] = i;
  790. if (hasTexcoords) {
  791. midTexcoord = Matrix2.Cartesian2.add(t1, t2, subdivisionTexcoordMidScratch);
  792. Matrix2.Cartesian2.multiplyByScalar(midTexcoord, 0.5, midTexcoord);
  793. subdividedTexcoords.push(midTexcoord.x, midTexcoord.y);
  794. }
  795. }
  796. triangles.push(i1, i, i0);
  797. triangles.push(i, i2, i0);
  798. } else if (g2 === max) {
  799. edge = `${Math.min(i2, i0)} ${Math.max(i2, i0)}`;
  800. i = edges[edge];
  801. if (!defaultValue.defined(i)) {
  802. mid = Matrix2.Cartesian3.add(v2, v0, subdivisionMidScratch);
  803. Matrix2.Cartesian3.multiplyByScalar(mid, 0.5, mid);
  804. subdividedPositions.push(mid.x, mid.y, mid.z);
  805. i = subdividedPositions.length / 3 - 1;
  806. edges[edge] = i;
  807. if (hasTexcoords) {
  808. midTexcoord = Matrix2.Cartesian2.add(t2, t0, subdivisionTexcoordMidScratch);
  809. Matrix2.Cartesian2.multiplyByScalar(midTexcoord, 0.5, midTexcoord);
  810. subdividedTexcoords.push(midTexcoord.x, midTexcoord.y);
  811. }
  812. }
  813. triangles.push(i2, i, i1);
  814. triangles.push(i, i0, i1);
  815. }
  816. } else {
  817. subdividedIndices.push(i0);
  818. subdividedIndices.push(i1);
  819. subdividedIndices.push(i2);
  820. }
  821. }
  822. const geometryOptions = {
  823. attributes: {
  824. position: new GeometryAttribute.GeometryAttribute({
  825. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  826. componentsPerAttribute: 3,
  827. values: subdividedPositions,
  828. }),
  829. },
  830. indices: subdividedIndices,
  831. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  832. };
  833. if (hasTexcoords) {
  834. geometryOptions.attributes.st = new GeometryAttribute.GeometryAttribute({
  835. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  836. componentsPerAttribute: 2,
  837. values: subdividedTexcoords,
  838. });
  839. }
  840. return new GeometryAttribute.Geometry(geometryOptions);
  841. };
  842. const subdivisionC0Scratch = new Matrix2.Cartographic();
  843. const subdivisionC1Scratch = new Matrix2.Cartographic();
  844. const subdivisionC2Scratch = new Matrix2.Cartographic();
  845. const subdivisionCartographicScratch = new Matrix2.Cartographic();
  846. /**
  847. * Subdivides positions on rhumb lines and raises points to the surface of the ellipsoid.
  848. *
  849. * @param {Ellipsoid} ellipsoid The ellipsoid the polygon in on.
  850. * @param {Cartesian3[]} positions An array of {@link Cartesian3} positions of the polygon.
  851. * @param {Number[]} indices An array of indices that determines the triangles in the polygon.
  852. * @param {Cartesian2[]} texcoords An optional array of {@link Cartesian2} texture coordinates of the polygon.
  853. * @param {Number} [granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  854. *
  855. * @exception {DeveloperError} At least three indices are required.
  856. * @exception {DeveloperError} The number of indices must be divisable by three.
  857. * @exception {DeveloperError} Granularity must be greater than zero.
  858. */
  859. PolygonPipeline.computeRhumbLineSubdivision = function (
  860. ellipsoid,
  861. positions,
  862. indices,
  863. texcoords,
  864. granularity
  865. ) {
  866. granularity = defaultValue.defaultValue(granularity, ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE);
  867. const hasTexcoords = defaultValue.defined(texcoords);
  868. //>>includeStart('debug', pragmas.debug);
  869. RuntimeError.Check.typeOf.object("ellipsoid", ellipsoid);
  870. RuntimeError.Check.defined("positions", positions);
  871. RuntimeError.Check.defined("indices", indices);
  872. RuntimeError.Check.typeOf.number.greaterThanOrEquals("indices.length", indices.length, 3);
  873. RuntimeError.Check.typeOf.number.equals("indices.length % 3", "0", indices.length % 3, 0);
  874. RuntimeError.Check.typeOf.number.greaterThan("granularity", granularity, 0.0);
  875. //>>includeEnd('debug');
  876. // triangles that need (or might need) to be subdivided.
  877. const triangles = indices.slice(0);
  878. // New positions due to edge splits are appended to the positions list.
  879. let i;
  880. const length = positions.length;
  881. const subdividedPositions = new Array(length * 3);
  882. const subdividedTexcoords = new Array(length * 2);
  883. let q = 0;
  884. let p = 0;
  885. for (i = 0; i < length; i++) {
  886. const item = positions[i];
  887. subdividedPositions[q++] = item.x;
  888. subdividedPositions[q++] = item.y;
  889. subdividedPositions[q++] = item.z;
  890. if (hasTexcoords) {
  891. const texcoordItem = texcoords[i];
  892. subdividedTexcoords[p++] = texcoordItem.x;
  893. subdividedTexcoords[p++] = texcoordItem.y;
  894. }
  895. }
  896. const subdividedIndices = [];
  897. // Used to make sure shared edges are not split more than once.
  898. const edges = {};
  899. const radius = ellipsoid.maximumRadius;
  900. const minDistance = ComponentDatatype.CesiumMath.chordLength(granularity, radius);
  901. const rhumb0 = new EllipsoidRhumbLine.EllipsoidRhumbLine(undefined, undefined, ellipsoid);
  902. const rhumb1 = new EllipsoidRhumbLine.EllipsoidRhumbLine(undefined, undefined, ellipsoid);
  903. const rhumb2 = new EllipsoidRhumbLine.EllipsoidRhumbLine(undefined, undefined, ellipsoid);
  904. while (triangles.length > 0) {
  905. const i2 = triangles.pop();
  906. const i1 = triangles.pop();
  907. const i0 = triangles.pop();
  908. const v0 = Matrix2.Cartesian3.fromArray(
  909. subdividedPositions,
  910. i0 * 3,
  911. subdivisionV0Scratch
  912. );
  913. const v1 = Matrix2.Cartesian3.fromArray(
  914. subdividedPositions,
  915. i1 * 3,
  916. subdivisionV1Scratch
  917. );
  918. const v2 = Matrix2.Cartesian3.fromArray(
  919. subdividedPositions,
  920. i2 * 3,
  921. subdivisionV2Scratch
  922. );
  923. let t0, t1, t2;
  924. if (hasTexcoords) {
  925. t0 = Matrix2.Cartesian2.fromArray(
  926. subdividedTexcoords,
  927. i0 * 2,
  928. subdivisionT0Scratch
  929. );
  930. t1 = Matrix2.Cartesian2.fromArray(
  931. subdividedTexcoords,
  932. i1 * 2,
  933. subdivisionT1Scratch
  934. );
  935. t2 = Matrix2.Cartesian2.fromArray(
  936. subdividedTexcoords,
  937. i2 * 2,
  938. subdivisionT2Scratch
  939. );
  940. }
  941. const c0 = ellipsoid.cartesianToCartographic(v0, subdivisionC0Scratch);
  942. const c1 = ellipsoid.cartesianToCartographic(v1, subdivisionC1Scratch);
  943. const c2 = ellipsoid.cartesianToCartographic(v2, subdivisionC2Scratch);
  944. rhumb0.setEndPoints(c0, c1);
  945. const g0 = rhumb0.surfaceDistance;
  946. rhumb1.setEndPoints(c1, c2);
  947. const g1 = rhumb1.surfaceDistance;
  948. rhumb2.setEndPoints(c2, c0);
  949. const g2 = rhumb2.surfaceDistance;
  950. const max = Math.max(g0, g1, g2);
  951. let edge;
  952. let mid;
  953. let midHeight;
  954. let midCartesian3;
  955. let midTexcoord;
  956. // if the max length squared of a triangle edge is greater than granularity, subdivide the triangle
  957. if (max > minDistance) {
  958. if (g0 === max) {
  959. edge = `${Math.min(i0, i1)} ${Math.max(i0, i1)}`;
  960. i = edges[edge];
  961. if (!defaultValue.defined(i)) {
  962. mid = rhumb0.interpolateUsingFraction(
  963. 0.5,
  964. subdivisionCartographicScratch
  965. );
  966. midHeight = (c0.height + c1.height) * 0.5;
  967. midCartesian3 = Matrix2.Cartesian3.fromRadians(
  968. mid.longitude,
  969. mid.latitude,
  970. midHeight,
  971. ellipsoid,
  972. subdivisionMidScratch
  973. );
  974. subdividedPositions.push(
  975. midCartesian3.x,
  976. midCartesian3.y,
  977. midCartesian3.z
  978. );
  979. i = subdividedPositions.length / 3 - 1;
  980. edges[edge] = i;
  981. if (hasTexcoords) {
  982. midTexcoord = Matrix2.Cartesian2.add(t0, t1, subdivisionTexcoordMidScratch);
  983. Matrix2.Cartesian2.multiplyByScalar(midTexcoord, 0.5, midTexcoord);
  984. subdividedTexcoords.push(midTexcoord.x, midTexcoord.y);
  985. }
  986. }
  987. triangles.push(i0, i, i2);
  988. triangles.push(i, i1, i2);
  989. } else if (g1 === max) {
  990. edge = `${Math.min(i1, i2)} ${Math.max(i1, i2)}`;
  991. i = edges[edge];
  992. if (!defaultValue.defined(i)) {
  993. mid = rhumb1.interpolateUsingFraction(
  994. 0.5,
  995. subdivisionCartographicScratch
  996. );
  997. midHeight = (c1.height + c2.height) * 0.5;
  998. midCartesian3 = Matrix2.Cartesian3.fromRadians(
  999. mid.longitude,
  1000. mid.latitude,
  1001. midHeight,
  1002. ellipsoid,
  1003. subdivisionMidScratch
  1004. );
  1005. subdividedPositions.push(
  1006. midCartesian3.x,
  1007. midCartesian3.y,
  1008. midCartesian3.z
  1009. );
  1010. i = subdividedPositions.length / 3 - 1;
  1011. edges[edge] = i;
  1012. if (hasTexcoords) {
  1013. midTexcoord = Matrix2.Cartesian2.add(t1, t2, subdivisionTexcoordMidScratch);
  1014. Matrix2.Cartesian2.multiplyByScalar(midTexcoord, 0.5, midTexcoord);
  1015. subdividedTexcoords.push(midTexcoord.x, midTexcoord.y);
  1016. }
  1017. }
  1018. triangles.push(i1, i, i0);
  1019. triangles.push(i, i2, i0);
  1020. } else if (g2 === max) {
  1021. edge = `${Math.min(i2, i0)} ${Math.max(i2, i0)}`;
  1022. i = edges[edge];
  1023. if (!defaultValue.defined(i)) {
  1024. mid = rhumb2.interpolateUsingFraction(
  1025. 0.5,
  1026. subdivisionCartographicScratch
  1027. );
  1028. midHeight = (c2.height + c0.height) * 0.5;
  1029. midCartesian3 = Matrix2.Cartesian3.fromRadians(
  1030. mid.longitude,
  1031. mid.latitude,
  1032. midHeight,
  1033. ellipsoid,
  1034. subdivisionMidScratch
  1035. );
  1036. subdividedPositions.push(
  1037. midCartesian3.x,
  1038. midCartesian3.y,
  1039. midCartesian3.z
  1040. );
  1041. i = subdividedPositions.length / 3 - 1;
  1042. edges[edge] = i;
  1043. if (hasTexcoords) {
  1044. midTexcoord = Matrix2.Cartesian2.add(t2, t0, subdivisionTexcoordMidScratch);
  1045. Matrix2.Cartesian2.multiplyByScalar(midTexcoord, 0.5, midTexcoord);
  1046. subdividedTexcoords.push(midTexcoord.x, midTexcoord.y);
  1047. }
  1048. }
  1049. triangles.push(i2, i, i1);
  1050. triangles.push(i, i0, i1);
  1051. }
  1052. } else {
  1053. subdividedIndices.push(i0);
  1054. subdividedIndices.push(i1);
  1055. subdividedIndices.push(i2);
  1056. }
  1057. }
  1058. const geometryOptions = {
  1059. attributes: {
  1060. position: new GeometryAttribute.GeometryAttribute({
  1061. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  1062. componentsPerAttribute: 3,
  1063. values: subdividedPositions,
  1064. }),
  1065. },
  1066. indices: subdividedIndices,
  1067. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  1068. };
  1069. if (hasTexcoords) {
  1070. geometryOptions.attributes.st = new GeometryAttribute.GeometryAttribute({
  1071. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  1072. componentsPerAttribute: 2,
  1073. values: subdividedTexcoords,
  1074. });
  1075. }
  1076. return new GeometryAttribute.Geometry(geometryOptions);
  1077. };
  1078. /**
  1079. * Scales each position of a geometry's position attribute to a height, in place.
  1080. *
  1081. * @param {Number[]} positions The array of numbers representing the positions to be scaled
  1082. * @param {Number} [height=0.0] The desired height to add to the positions
  1083. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the positions lie.
  1084. * @param {Boolean} [scaleToSurface=true] <code>true</code> if the positions need to be scaled to the surface before the height is added.
  1085. * @returns {Number[]} The input array of positions, scaled to height
  1086. */
  1087. PolygonPipeline.scaleToGeodeticHeight = function (
  1088. positions,
  1089. height,
  1090. ellipsoid,
  1091. scaleToSurface
  1092. ) {
  1093. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix2.Ellipsoid.WGS84);
  1094. let n = scaleToGeodeticHeightN;
  1095. let p = scaleToGeodeticHeightP;
  1096. height = defaultValue.defaultValue(height, 0.0);
  1097. scaleToSurface = defaultValue.defaultValue(scaleToSurface, true);
  1098. if (defaultValue.defined(positions)) {
  1099. const length = positions.length;
  1100. for (let i = 0; i < length; i += 3) {
  1101. Matrix2.Cartesian3.fromArray(positions, i, p);
  1102. if (scaleToSurface) {
  1103. p = ellipsoid.scaleToGeodeticSurface(p, p);
  1104. }
  1105. if (height !== 0) {
  1106. n = ellipsoid.geodeticSurfaceNormal(p, n);
  1107. Matrix2.Cartesian3.multiplyByScalar(n, height, n);
  1108. Matrix2.Cartesian3.add(p, n, p);
  1109. }
  1110. positions[i] = p.x;
  1111. positions[i + 1] = p.y;
  1112. positions[i + 2] = p.z;
  1113. }
  1114. }
  1115. return positions;
  1116. };
  1117. exports.PolygonPipeline = PolygonPipeline;
  1118. exports.WindingOrder = WindingOrder$1;
  1119. }));