PolygonPipeline-197b7d5c.js 41 KB

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