PolygonPipeline-2277311a.js 47 KB

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