PolygonPipeline-d1884135.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './Matrix2-69c32d33', './RuntimeError-c581ca93', './ComponentDatatype-b1ea011a', './defaultValue-94c3e563', './EllipsoidRhumbLine-5cb6da82', './GeometryAttribute-cb73bb3f', './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. /**
  637. * Subdivides positions and raises points to the surface of the ellipsoid.
  638. *
  639. * @param {Ellipsoid} ellipsoid The ellipsoid the polygon in on.
  640. * @param {Cartesian3[]} positions An array of {@link Cartesian3} positions of the polygon.
  641. * @param {Number[]} indices An array of indices that determines the triangles in the polygon.
  642. * @param {Number} [granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  643. *
  644. * @exception {DeveloperError} At least three indices are required.
  645. * @exception {DeveloperError} The number of indices must be divisable by three.
  646. * @exception {DeveloperError} Granularity must be greater than zero.
  647. */
  648. PolygonPipeline.computeSubdivision = function (
  649. ellipsoid,
  650. positions,
  651. indices,
  652. granularity
  653. ) {
  654. granularity = defaultValue.defaultValue(granularity, ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE);
  655. //>>includeStart('debug', pragmas.debug);
  656. RuntimeError.Check.typeOf.object("ellipsoid", ellipsoid);
  657. RuntimeError.Check.defined("positions", positions);
  658. RuntimeError.Check.defined("indices", indices);
  659. RuntimeError.Check.typeOf.number.greaterThanOrEquals("indices.length", indices.length, 3);
  660. RuntimeError.Check.typeOf.number.equals("indices.length % 3", "0", indices.length % 3, 0);
  661. RuntimeError.Check.typeOf.number.greaterThan("granularity", granularity, 0.0);
  662. //>>includeEnd('debug');
  663. // triangles that need (or might need) to be subdivided.
  664. const triangles = indices.slice(0);
  665. // New positions due to edge splits are appended to the positions list.
  666. let i;
  667. const length = positions.length;
  668. const subdividedPositions = new Array(length * 3);
  669. let q = 0;
  670. for (i = 0; i < length; i++) {
  671. const item = positions[i];
  672. subdividedPositions[q++] = item.x;
  673. subdividedPositions[q++] = item.y;
  674. subdividedPositions[q++] = item.z;
  675. }
  676. const subdividedIndices = [];
  677. // Used to make sure shared edges are not split more than once.
  678. const edges = {};
  679. const radius = ellipsoid.maximumRadius;
  680. const minDistance = ComponentDatatype.CesiumMath.chordLength(granularity, radius);
  681. const minDistanceSqrd = minDistance * minDistance;
  682. while (triangles.length > 0) {
  683. const i2 = triangles.pop();
  684. const i1 = triangles.pop();
  685. const i0 = triangles.pop();
  686. const v0 = Matrix2.Cartesian3.fromArray(
  687. subdividedPositions,
  688. i0 * 3,
  689. subdivisionV0Scratch
  690. );
  691. const v1 = Matrix2.Cartesian3.fromArray(
  692. subdividedPositions,
  693. i1 * 3,
  694. subdivisionV1Scratch
  695. );
  696. const v2 = Matrix2.Cartesian3.fromArray(
  697. subdividedPositions,
  698. i2 * 3,
  699. subdivisionV2Scratch
  700. );
  701. const s0 = Matrix2.Cartesian3.multiplyByScalar(
  702. Matrix2.Cartesian3.normalize(v0, subdivisionS0Scratch),
  703. radius,
  704. subdivisionS0Scratch
  705. );
  706. const s1 = Matrix2.Cartesian3.multiplyByScalar(
  707. Matrix2.Cartesian3.normalize(v1, subdivisionS1Scratch),
  708. radius,
  709. subdivisionS1Scratch
  710. );
  711. const s2 = Matrix2.Cartesian3.multiplyByScalar(
  712. Matrix2.Cartesian3.normalize(v2, subdivisionS2Scratch),
  713. radius,
  714. subdivisionS2Scratch
  715. );
  716. const g0 = Matrix2.Cartesian3.magnitudeSquared(
  717. Matrix2.Cartesian3.subtract(s0, s1, subdivisionMidScratch)
  718. );
  719. const g1 = Matrix2.Cartesian3.magnitudeSquared(
  720. Matrix2.Cartesian3.subtract(s1, s2, subdivisionMidScratch)
  721. );
  722. const g2 = Matrix2.Cartesian3.magnitudeSquared(
  723. Matrix2.Cartesian3.subtract(s2, s0, subdivisionMidScratch)
  724. );
  725. const max = Math.max(g0, g1, g2);
  726. let edge;
  727. let mid;
  728. // if the max length squared of a triangle edge is greater than the chord length of squared
  729. // of the granularity, subdivide the triangle
  730. if (max > minDistanceSqrd) {
  731. if (g0 === max) {
  732. edge = `${Math.min(i0, i1)} ${Math.max(i0, i1)}`;
  733. i = edges[edge];
  734. if (!defaultValue.defined(i)) {
  735. mid = Matrix2.Cartesian3.add(v0, v1, subdivisionMidScratch);
  736. Matrix2.Cartesian3.multiplyByScalar(mid, 0.5, mid);
  737. subdividedPositions.push(mid.x, mid.y, mid.z);
  738. i = subdividedPositions.length / 3 - 1;
  739. edges[edge] = i;
  740. }
  741. triangles.push(i0, i, i2);
  742. triangles.push(i, i1, i2);
  743. } else if (g1 === max) {
  744. edge = `${Math.min(i1, i2)} ${Math.max(i1, i2)}`;
  745. i = edges[edge];
  746. if (!defaultValue.defined(i)) {
  747. mid = Matrix2.Cartesian3.add(v1, v2, subdivisionMidScratch);
  748. Matrix2.Cartesian3.multiplyByScalar(mid, 0.5, mid);
  749. subdividedPositions.push(mid.x, mid.y, mid.z);
  750. i = subdividedPositions.length / 3 - 1;
  751. edges[edge] = i;
  752. }
  753. triangles.push(i1, i, i0);
  754. triangles.push(i, i2, i0);
  755. } else if (g2 === max) {
  756. edge = `${Math.min(i2, i0)} ${Math.max(i2, i0)}`;
  757. i = edges[edge];
  758. if (!defaultValue.defined(i)) {
  759. mid = Matrix2.Cartesian3.add(v2, v0, subdivisionMidScratch);
  760. Matrix2.Cartesian3.multiplyByScalar(mid, 0.5, mid);
  761. subdividedPositions.push(mid.x, mid.y, mid.z);
  762. i = subdividedPositions.length / 3 - 1;
  763. edges[edge] = i;
  764. }
  765. triangles.push(i2, i, i1);
  766. triangles.push(i, i0, i1);
  767. }
  768. } else {
  769. subdividedIndices.push(i0);
  770. subdividedIndices.push(i1);
  771. subdividedIndices.push(i2);
  772. }
  773. }
  774. return new GeometryAttribute.Geometry({
  775. attributes: {
  776. position: new GeometryAttribute.GeometryAttribute({
  777. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  778. componentsPerAttribute: 3,
  779. values: subdividedPositions,
  780. }),
  781. },
  782. indices: subdividedIndices,
  783. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  784. });
  785. };
  786. const subdivisionC0Scratch = new Matrix2.Cartographic();
  787. const subdivisionC1Scratch = new Matrix2.Cartographic();
  788. const subdivisionC2Scratch = new Matrix2.Cartographic();
  789. const subdivisionCartographicScratch = new Matrix2.Cartographic();
  790. /**
  791. * Subdivides positions on rhumb lines and raises points to the surface of the ellipsoid.
  792. *
  793. * @param {Ellipsoid} ellipsoid The ellipsoid the polygon in on.
  794. * @param {Cartesian3[]} positions An array of {@link Cartesian3} positions of the polygon.
  795. * @param {Number[]} indices An array of indices that determines the triangles in the polygon.
  796. * @param {Number} [granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  797. *
  798. * @exception {DeveloperError} At least three indices are required.
  799. * @exception {DeveloperError} The number of indices must be divisable by three.
  800. * @exception {DeveloperError} Granularity must be greater than zero.
  801. */
  802. PolygonPipeline.computeRhumbLineSubdivision = function (
  803. ellipsoid,
  804. positions,
  805. indices,
  806. granularity
  807. ) {
  808. granularity = defaultValue.defaultValue(granularity, ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE);
  809. //>>includeStart('debug', pragmas.debug);
  810. RuntimeError.Check.typeOf.object("ellipsoid", ellipsoid);
  811. RuntimeError.Check.defined("positions", positions);
  812. RuntimeError.Check.defined("indices", indices);
  813. RuntimeError.Check.typeOf.number.greaterThanOrEquals("indices.length", indices.length, 3);
  814. RuntimeError.Check.typeOf.number.equals("indices.length % 3", "0", indices.length % 3, 0);
  815. RuntimeError.Check.typeOf.number.greaterThan("granularity", granularity, 0.0);
  816. //>>includeEnd('debug');
  817. // triangles that need (or might need) to be subdivided.
  818. const triangles = indices.slice(0);
  819. // New positions due to edge splits are appended to the positions list.
  820. let i;
  821. const length = positions.length;
  822. const subdividedPositions = new Array(length * 3);
  823. let q = 0;
  824. for (i = 0; i < length; i++) {
  825. const item = positions[i];
  826. subdividedPositions[q++] = item.x;
  827. subdividedPositions[q++] = item.y;
  828. subdividedPositions[q++] = item.z;
  829. }
  830. const subdividedIndices = [];
  831. // Used to make sure shared edges are not split more than once.
  832. const edges = {};
  833. const radius = ellipsoid.maximumRadius;
  834. const minDistance = ComponentDatatype.CesiumMath.chordLength(granularity, radius);
  835. const rhumb0 = new EllipsoidRhumbLine.EllipsoidRhumbLine(undefined, undefined, ellipsoid);
  836. const rhumb1 = new EllipsoidRhumbLine.EllipsoidRhumbLine(undefined, undefined, ellipsoid);
  837. const rhumb2 = new EllipsoidRhumbLine.EllipsoidRhumbLine(undefined, undefined, ellipsoid);
  838. while (triangles.length > 0) {
  839. const i2 = triangles.pop();
  840. const i1 = triangles.pop();
  841. const i0 = triangles.pop();
  842. const v0 = Matrix2.Cartesian3.fromArray(
  843. subdividedPositions,
  844. i0 * 3,
  845. subdivisionV0Scratch
  846. );
  847. const v1 = Matrix2.Cartesian3.fromArray(
  848. subdividedPositions,
  849. i1 * 3,
  850. subdivisionV1Scratch
  851. );
  852. const v2 = Matrix2.Cartesian3.fromArray(
  853. subdividedPositions,
  854. i2 * 3,
  855. subdivisionV2Scratch
  856. );
  857. const c0 = ellipsoid.cartesianToCartographic(v0, subdivisionC0Scratch);
  858. const c1 = ellipsoid.cartesianToCartographic(v1, subdivisionC1Scratch);
  859. const c2 = ellipsoid.cartesianToCartographic(v2, subdivisionC2Scratch);
  860. rhumb0.setEndPoints(c0, c1);
  861. const g0 = rhumb0.surfaceDistance;
  862. rhumb1.setEndPoints(c1, c2);
  863. const g1 = rhumb1.surfaceDistance;
  864. rhumb2.setEndPoints(c2, c0);
  865. const g2 = rhumb2.surfaceDistance;
  866. const max = Math.max(g0, g1, g2);
  867. let edge;
  868. let mid;
  869. let midHeight;
  870. let midCartesian3;
  871. // if the max length squared of a triangle edge is greater than granularity, subdivide the triangle
  872. if (max > minDistance) {
  873. if (g0 === max) {
  874. edge = `${Math.min(i0, i1)} ${Math.max(i0, i1)}`;
  875. i = edges[edge];
  876. if (!defaultValue.defined(i)) {
  877. mid = rhumb0.interpolateUsingFraction(
  878. 0.5,
  879. subdivisionCartographicScratch
  880. );
  881. midHeight = (c0.height + c1.height) * 0.5;
  882. midCartesian3 = Matrix2.Cartesian3.fromRadians(
  883. mid.longitude,
  884. mid.latitude,
  885. midHeight,
  886. ellipsoid,
  887. subdivisionMidScratch
  888. );
  889. subdividedPositions.push(
  890. midCartesian3.x,
  891. midCartesian3.y,
  892. midCartesian3.z
  893. );
  894. i = subdividedPositions.length / 3 - 1;
  895. edges[edge] = i;
  896. }
  897. triangles.push(i0, i, i2);
  898. triangles.push(i, i1, i2);
  899. } else if (g1 === max) {
  900. edge = `${Math.min(i1, i2)} ${Math.max(i1, i2)}`;
  901. i = edges[edge];
  902. if (!defaultValue.defined(i)) {
  903. mid = rhumb1.interpolateUsingFraction(
  904. 0.5,
  905. subdivisionCartographicScratch
  906. );
  907. midHeight = (c1.height + c2.height) * 0.5;
  908. midCartesian3 = Matrix2.Cartesian3.fromRadians(
  909. mid.longitude,
  910. mid.latitude,
  911. midHeight,
  912. ellipsoid,
  913. subdivisionMidScratch
  914. );
  915. subdividedPositions.push(
  916. midCartesian3.x,
  917. midCartesian3.y,
  918. midCartesian3.z
  919. );
  920. i = subdividedPositions.length / 3 - 1;
  921. edges[edge] = i;
  922. }
  923. triangles.push(i1, i, i0);
  924. triangles.push(i, i2, i0);
  925. } else if (g2 === max) {
  926. edge = `${Math.min(i2, i0)} ${Math.max(i2, i0)}`;
  927. i = edges[edge];
  928. if (!defaultValue.defined(i)) {
  929. mid = rhumb2.interpolateUsingFraction(
  930. 0.5,
  931. subdivisionCartographicScratch
  932. );
  933. midHeight = (c2.height + c0.height) * 0.5;
  934. midCartesian3 = Matrix2.Cartesian3.fromRadians(
  935. mid.longitude,
  936. mid.latitude,
  937. midHeight,
  938. ellipsoid,
  939. subdivisionMidScratch
  940. );
  941. subdividedPositions.push(
  942. midCartesian3.x,
  943. midCartesian3.y,
  944. midCartesian3.z
  945. );
  946. i = subdividedPositions.length / 3 - 1;
  947. edges[edge] = i;
  948. }
  949. triangles.push(i2, i, i1);
  950. triangles.push(i, i0, i1);
  951. }
  952. } else {
  953. subdividedIndices.push(i0);
  954. subdividedIndices.push(i1);
  955. subdividedIndices.push(i2);
  956. }
  957. }
  958. return new GeometryAttribute.Geometry({
  959. attributes: {
  960. position: new GeometryAttribute.GeometryAttribute({
  961. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  962. componentsPerAttribute: 3,
  963. values: subdividedPositions,
  964. }),
  965. },
  966. indices: subdividedIndices,
  967. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  968. });
  969. };
  970. /**
  971. * Scales each position of a geometry's position attribute to a height, in place.
  972. *
  973. * @param {Number[]} positions The array of numbers representing the positions to be scaled
  974. * @param {Number} [height=0.0] The desired height to add to the positions
  975. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the positions lie.
  976. * @param {Boolean} [scaleToSurface=true] <code>true</code> if the positions need to be scaled to the surface before the height is added.
  977. * @returns {Number[]} The input array of positions, scaled to height
  978. */
  979. PolygonPipeline.scaleToGeodeticHeight = function (
  980. positions,
  981. height,
  982. ellipsoid,
  983. scaleToSurface
  984. ) {
  985. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix2.Ellipsoid.WGS84);
  986. let n = scaleToGeodeticHeightN;
  987. let p = scaleToGeodeticHeightP;
  988. height = defaultValue.defaultValue(height, 0.0);
  989. scaleToSurface = defaultValue.defaultValue(scaleToSurface, true);
  990. if (defaultValue.defined(positions)) {
  991. const length = positions.length;
  992. for (let i = 0; i < length; i += 3) {
  993. Matrix2.Cartesian3.fromArray(positions, i, p);
  994. if (scaleToSurface) {
  995. p = ellipsoid.scaleToGeodeticSurface(p, p);
  996. }
  997. if (height !== 0) {
  998. n = ellipsoid.geodeticSurfaceNormal(p, n);
  999. Matrix2.Cartesian3.multiplyByScalar(n, height, n);
  1000. Matrix2.Cartesian3.add(p, n, p);
  1001. }
  1002. positions[i] = p.x;
  1003. positions[i + 1] = p.y;
  1004. positions[i + 2] = p.z;
  1005. }
  1006. }
  1007. return positions;
  1008. };
  1009. exports.PolygonPipeline = PolygonPipeline;
  1010. exports.WindingOrder = WindingOrder$1;
  1011. }));