PolygonPipeline-e486c11c.js 40 KB

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