PolygonGeometryLibrary-6d3a5ed4.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './ArcType-0cf52f8c', './arrayRemoveDuplicates-06991c15', './Matrix2-fc7e9822', './ComponentDatatype-4a60b8d6', './defaultValue-94c3e563', './EllipsoidRhumbLine-daebc75b', './GeometryAttribute-2ecf73f6', './GeometryAttributes-7df9bef6', './GeometryPipeline-b4816e69', './IndexDatatype-db156785', './PolygonPipeline-cc031b9f', './Transforms-a076dbe6'], (function (exports, ArcType, arrayRemoveDuplicates, Matrix2, ComponentDatatype, defaultValue, EllipsoidRhumbLine, GeometryAttribute, GeometryAttributes, GeometryPipeline, IndexDatatype, PolygonPipeline, Transforms) { 'use strict';
  3. /**
  4. * A queue that can enqueue items at the end, and dequeue items from the front.
  5. *
  6. * @alias Queue
  7. * @constructor
  8. */
  9. function Queue() {
  10. this._array = [];
  11. this._offset = 0;
  12. this._length = 0;
  13. }
  14. Object.defineProperties(Queue.prototype, {
  15. /**
  16. * The length of the queue.
  17. *
  18. * @memberof Queue.prototype
  19. *
  20. * @type {Number}
  21. * @readonly
  22. */
  23. length: {
  24. get: function () {
  25. return this._length;
  26. },
  27. },
  28. });
  29. /**
  30. * Enqueues the specified item.
  31. *
  32. * @param {*} item The item to enqueue.
  33. */
  34. Queue.prototype.enqueue = function (item) {
  35. this._array.push(item);
  36. this._length++;
  37. };
  38. /**
  39. * Dequeues an item. Returns undefined if the queue is empty.
  40. *
  41. * @returns {*} The the dequeued item.
  42. */
  43. Queue.prototype.dequeue = function () {
  44. if (this._length === 0) {
  45. return undefined;
  46. }
  47. const array = this._array;
  48. let offset = this._offset;
  49. const item = array[offset];
  50. array[offset] = undefined;
  51. offset++;
  52. if (offset > 10 && offset * 2 > array.length) {
  53. //compact array
  54. this._array = array.slice(offset);
  55. offset = 0;
  56. }
  57. this._offset = offset;
  58. this._length--;
  59. return item;
  60. };
  61. /**
  62. * Returns the item at the front of the queue. Returns undefined if the queue is empty.
  63. *
  64. * @returns {*} The item at the front of the queue.
  65. */
  66. Queue.prototype.peek = function () {
  67. if (this._length === 0) {
  68. return undefined;
  69. }
  70. return this._array[this._offset];
  71. };
  72. /**
  73. * Check whether this queue contains the specified item.
  74. *
  75. * @param {*} item The item to search for.
  76. */
  77. Queue.prototype.contains = function (item) {
  78. return this._array.indexOf(item) !== -1;
  79. };
  80. /**
  81. * Remove all items from the queue.
  82. */
  83. Queue.prototype.clear = function () {
  84. this._array.length = this._offset = this._length = 0;
  85. };
  86. /**
  87. * Sort the items in the queue in-place.
  88. *
  89. * @param {Queue.Comparator} compareFunction A function that defines the sort order.
  90. */
  91. Queue.prototype.sort = function (compareFunction) {
  92. if (this._offset > 0) {
  93. //compact array
  94. this._array = this._array.slice(this._offset);
  95. this._offset = 0;
  96. }
  97. this._array.sort(compareFunction);
  98. };
  99. /**
  100. * @private
  101. */
  102. const PolygonGeometryLibrary = {};
  103. PolygonGeometryLibrary.computeHierarchyPackedLength = function (
  104. polygonHierarchy,
  105. CartesianX
  106. ) {
  107. let numComponents = 0;
  108. const stack = [polygonHierarchy];
  109. while (stack.length > 0) {
  110. const hierarchy = stack.pop();
  111. if (!defaultValue.defined(hierarchy)) {
  112. continue;
  113. }
  114. numComponents += 2;
  115. const positions = hierarchy.positions;
  116. const holes = hierarchy.holes;
  117. if (defaultValue.defined(positions) && positions.length > 0) {
  118. numComponents += positions.length * CartesianX.packedLength;
  119. }
  120. if (defaultValue.defined(holes)) {
  121. const length = holes.length;
  122. for (let i = 0; i < length; ++i) {
  123. stack.push(holes[i]);
  124. }
  125. }
  126. }
  127. return numComponents;
  128. };
  129. PolygonGeometryLibrary.packPolygonHierarchy = function (
  130. polygonHierarchy,
  131. array,
  132. startingIndex,
  133. CartesianX
  134. ) {
  135. const stack = [polygonHierarchy];
  136. while (stack.length > 0) {
  137. const hierarchy = stack.pop();
  138. if (!defaultValue.defined(hierarchy)) {
  139. continue;
  140. }
  141. const positions = hierarchy.positions;
  142. const holes = hierarchy.holes;
  143. array[startingIndex++] = defaultValue.defined(positions) ? positions.length : 0;
  144. array[startingIndex++] = defaultValue.defined(holes) ? holes.length : 0;
  145. if (defaultValue.defined(positions)) {
  146. const positionsLength = positions.length;
  147. for (
  148. let i = 0;
  149. i < positionsLength;
  150. ++i, startingIndex += CartesianX.packedLength
  151. ) {
  152. CartesianX.pack(positions[i], array, startingIndex);
  153. }
  154. }
  155. if (defaultValue.defined(holes)) {
  156. const holesLength = holes.length;
  157. for (let j = 0; j < holesLength; ++j) {
  158. stack.push(holes[j]);
  159. }
  160. }
  161. }
  162. return startingIndex;
  163. };
  164. PolygonGeometryLibrary.unpackPolygonHierarchy = function (
  165. array,
  166. startingIndex,
  167. CartesianX
  168. ) {
  169. const positionsLength = array[startingIndex++];
  170. const holesLength = array[startingIndex++];
  171. const positions = new Array(positionsLength);
  172. const holes = holesLength > 0 ? new Array(holesLength) : undefined;
  173. for (
  174. let i = 0;
  175. i < positionsLength;
  176. ++i, startingIndex += CartesianX.packedLength
  177. ) {
  178. positions[i] = CartesianX.unpack(array, startingIndex);
  179. }
  180. for (let j = 0; j < holesLength; ++j) {
  181. holes[j] = PolygonGeometryLibrary.unpackPolygonHierarchy(
  182. array,
  183. startingIndex,
  184. CartesianX
  185. );
  186. startingIndex = holes[j].startingIndex;
  187. delete holes[j].startingIndex;
  188. }
  189. return {
  190. positions: positions,
  191. holes: holes,
  192. startingIndex: startingIndex,
  193. };
  194. };
  195. const distance2DScratch = new Matrix2.Cartesian2();
  196. function getPointAtDistance2D(p0, p1, distance, length) {
  197. Matrix2.Cartesian2.subtract(p1, p0, distance2DScratch);
  198. Matrix2.Cartesian2.multiplyByScalar(
  199. distance2DScratch,
  200. distance / length,
  201. distance2DScratch
  202. );
  203. Matrix2.Cartesian2.add(p0, distance2DScratch, distance2DScratch);
  204. return [distance2DScratch.x, distance2DScratch.y];
  205. }
  206. const distanceScratch = new Matrix2.Cartesian3();
  207. function getPointAtDistance(p0, p1, distance, length) {
  208. Matrix2.Cartesian3.subtract(p1, p0, distanceScratch);
  209. Matrix2.Cartesian3.multiplyByScalar(
  210. distanceScratch,
  211. distance / length,
  212. distanceScratch
  213. );
  214. Matrix2.Cartesian3.add(p0, distanceScratch, distanceScratch);
  215. return [distanceScratch.x, distanceScratch.y, distanceScratch.z];
  216. }
  217. PolygonGeometryLibrary.subdivideLineCount = function (p0, p1, minDistance) {
  218. const distance = Matrix2.Cartesian3.distance(p0, p1);
  219. const n = distance / minDistance;
  220. const countDivide = Math.max(0, Math.ceil(ComponentDatatype.CesiumMath.log2(n)));
  221. return Math.pow(2, countDivide);
  222. };
  223. const scratchCartographic0 = new Matrix2.Cartographic();
  224. const scratchCartographic1 = new Matrix2.Cartographic();
  225. const scratchCartographic2 = new Matrix2.Cartographic();
  226. const scratchCartesian0 = new Matrix2.Cartesian3();
  227. const scratchRhumbLine = new EllipsoidRhumbLine.EllipsoidRhumbLine();
  228. PolygonGeometryLibrary.subdivideRhumbLineCount = function (
  229. ellipsoid,
  230. p0,
  231. p1,
  232. minDistance
  233. ) {
  234. const c0 = ellipsoid.cartesianToCartographic(p0, scratchCartographic0);
  235. const c1 = ellipsoid.cartesianToCartographic(p1, scratchCartographic1);
  236. const rhumb = new EllipsoidRhumbLine.EllipsoidRhumbLine(c0, c1, ellipsoid);
  237. const n = rhumb.surfaceDistance / minDistance;
  238. const countDivide = Math.max(0, Math.ceil(ComponentDatatype.CesiumMath.log2(n)));
  239. return Math.pow(2, countDivide);
  240. };
  241. /**
  242. * Subdivides texture coordinates based on the subdivision of the associated world positions.
  243. *
  244. * @param {Cartesian2} t0 First texture coordinate.
  245. * @param {Cartesian2} t1 Second texture coordinate.
  246. * @param {Cartesian3} p0 First world position.
  247. * @param {Cartesian3} p1 Second world position.
  248. * @param {Number} minDistance Minimum distance for a segment.
  249. * @param {Array<Cartesian2>} result The subdivided texture coordinates.
  250. *
  251. * @private
  252. */
  253. PolygonGeometryLibrary.subdivideTexcoordLine = function (
  254. t0,
  255. t1,
  256. p0,
  257. p1,
  258. minDistance,
  259. result
  260. ) {
  261. // Compute the number of subdivisions.
  262. const subdivisions = PolygonGeometryLibrary.subdivideLineCount(
  263. p0,
  264. p1,
  265. minDistance
  266. );
  267. // Compute the distance between each subdivided point.
  268. const length2D = Matrix2.Cartesian2.distance(t0, t1);
  269. const distanceBetweenCoords = length2D / subdivisions;
  270. // Resize the result array.
  271. const texcoords = result;
  272. texcoords.length = subdivisions * 2;
  273. // Compute texture coordinates using linear interpolation.
  274. let index = 0;
  275. for (let i = 0; i < subdivisions; i++) {
  276. const t = getPointAtDistance2D(t0, t1, i * distanceBetweenCoords, length2D);
  277. texcoords[index++] = t[0];
  278. texcoords[index++] = t[1];
  279. }
  280. return texcoords;
  281. };
  282. PolygonGeometryLibrary.subdivideLine = function (p0, p1, minDistance, result) {
  283. const numVertices = PolygonGeometryLibrary.subdivideLineCount(
  284. p0,
  285. p1,
  286. minDistance
  287. );
  288. const length = Matrix2.Cartesian3.distance(p0, p1);
  289. const distanceBetweenVertices = length / numVertices;
  290. if (!defaultValue.defined(result)) {
  291. result = [];
  292. }
  293. const positions = result;
  294. positions.length = numVertices * 3;
  295. let index = 0;
  296. for (let i = 0; i < numVertices; i++) {
  297. const p = getPointAtDistance(p0, p1, i * distanceBetweenVertices, length);
  298. positions[index++] = p[0];
  299. positions[index++] = p[1];
  300. positions[index++] = p[2];
  301. }
  302. return positions;
  303. };
  304. /**
  305. * Subdivides texture coordinates based on the subdivision of the associated world positions using a rhumb line.
  306. *
  307. * @param {Cartesian2} t0 First texture coordinate.
  308. * @param {Cartesian2} t1 Second texture coordinate.
  309. * @param {Ellipsoid} ellipsoid The ellipsoid.
  310. * @param {Cartesian3} p0 First world position.
  311. * @param {Cartesian3} p1 Second world position.
  312. * @param {Number} minDistance Minimum distance for a segment.
  313. * @param {Array<Cartesian2>} result The subdivided texture coordinates.
  314. *
  315. * @private
  316. */
  317. PolygonGeometryLibrary.subdivideTexcoordRhumbLine = function (
  318. t0,
  319. t1,
  320. ellipsoid,
  321. p0,
  322. p1,
  323. minDistance,
  324. result
  325. ) {
  326. // Compute the surface distance.
  327. const c0 = ellipsoid.cartesianToCartographic(p0, scratchCartographic0);
  328. const c1 = ellipsoid.cartesianToCartographic(p1, scratchCartographic1);
  329. scratchRhumbLine.setEndPoints(c0, c1);
  330. const n = scratchRhumbLine.surfaceDistance / minDistance;
  331. // Compute the number of subdivisions.
  332. const countDivide = Math.max(0, Math.ceil(ComponentDatatype.CesiumMath.log2(n)));
  333. const subdivisions = Math.pow(2, countDivide);
  334. // Compute the distance between each subdivided point.
  335. const length2D = Matrix2.Cartesian2.distance(t0, t1);
  336. const distanceBetweenCoords = length2D / subdivisions;
  337. // Resize the result array.
  338. const texcoords = result;
  339. texcoords.length = subdivisions * 2;
  340. // Compute texture coordinates using linear interpolation.
  341. let index = 0;
  342. for (let i = 0; i < subdivisions; i++) {
  343. const t = getPointAtDistance2D(t0, t1, i * distanceBetweenCoords, length2D);
  344. texcoords[index++] = t[0];
  345. texcoords[index++] = t[1];
  346. }
  347. return texcoords;
  348. };
  349. PolygonGeometryLibrary.subdivideRhumbLine = function (
  350. ellipsoid,
  351. p0,
  352. p1,
  353. minDistance,
  354. result
  355. ) {
  356. const c0 = ellipsoid.cartesianToCartographic(p0, scratchCartographic0);
  357. const c1 = ellipsoid.cartesianToCartographic(p1, scratchCartographic1);
  358. const rhumb = new EllipsoidRhumbLine.EllipsoidRhumbLine(c0, c1, ellipsoid);
  359. const n = rhumb.surfaceDistance / minDistance;
  360. const countDivide = Math.max(0, Math.ceil(ComponentDatatype.CesiumMath.log2(n)));
  361. const numVertices = Math.pow(2, countDivide);
  362. const distanceBetweenVertices = rhumb.surfaceDistance / numVertices;
  363. if (!defaultValue.defined(result)) {
  364. result = [];
  365. }
  366. const positions = result;
  367. positions.length = numVertices * 3;
  368. let index = 0;
  369. for (let i = 0; i < numVertices; i++) {
  370. const c = rhumb.interpolateUsingSurfaceDistance(
  371. i * distanceBetweenVertices,
  372. scratchCartographic2
  373. );
  374. const p = ellipsoid.cartographicToCartesian(c, scratchCartesian0);
  375. positions[index++] = p.x;
  376. positions[index++] = p.y;
  377. positions[index++] = p.z;
  378. }
  379. return positions;
  380. };
  381. const scaleToGeodeticHeightN1 = new Matrix2.Cartesian3();
  382. const scaleToGeodeticHeightN2 = new Matrix2.Cartesian3();
  383. const scaleToGeodeticHeightP1 = new Matrix2.Cartesian3();
  384. const scaleToGeodeticHeightP2 = new Matrix2.Cartesian3();
  385. PolygonGeometryLibrary.scaleToGeodeticHeightExtruded = function (
  386. geometry,
  387. maxHeight,
  388. minHeight,
  389. ellipsoid,
  390. perPositionHeight
  391. ) {
  392. ellipsoid = defaultValue.defaultValue(ellipsoid, Matrix2.Ellipsoid.WGS84);
  393. const n1 = scaleToGeodeticHeightN1;
  394. let n2 = scaleToGeodeticHeightN2;
  395. const p = scaleToGeodeticHeightP1;
  396. let p2 = scaleToGeodeticHeightP2;
  397. if (
  398. defaultValue.defined(geometry) &&
  399. defaultValue.defined(geometry.attributes) &&
  400. defaultValue.defined(geometry.attributes.position)
  401. ) {
  402. const positions = geometry.attributes.position.values;
  403. const length = positions.length / 2;
  404. for (let i = 0; i < length; i += 3) {
  405. Matrix2.Cartesian3.fromArray(positions, i, p);
  406. ellipsoid.geodeticSurfaceNormal(p, n1);
  407. p2 = ellipsoid.scaleToGeodeticSurface(p, p2);
  408. n2 = Matrix2.Cartesian3.multiplyByScalar(n1, minHeight, n2);
  409. n2 = Matrix2.Cartesian3.add(p2, n2, n2);
  410. positions[i + length] = n2.x;
  411. positions[i + 1 + length] = n2.y;
  412. positions[i + 2 + length] = n2.z;
  413. if (perPositionHeight) {
  414. p2 = Matrix2.Cartesian3.clone(p, p2);
  415. }
  416. n2 = Matrix2.Cartesian3.multiplyByScalar(n1, maxHeight, n2);
  417. n2 = Matrix2.Cartesian3.add(p2, n2, n2);
  418. positions[i] = n2.x;
  419. positions[i + 1] = n2.y;
  420. positions[i + 2] = n2.z;
  421. }
  422. }
  423. return geometry;
  424. };
  425. PolygonGeometryLibrary.polygonOutlinesFromHierarchy = function (
  426. polygonHierarchy,
  427. scaleToEllipsoidSurface,
  428. ellipsoid
  429. ) {
  430. // create from a polygon hierarchy
  431. // Algorithm adapted from http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
  432. const polygons = [];
  433. const queue = new Queue();
  434. queue.enqueue(polygonHierarchy);
  435. let i;
  436. let j;
  437. let length;
  438. while (queue.length !== 0) {
  439. const outerNode = queue.dequeue();
  440. let outerRing = outerNode.positions;
  441. if (scaleToEllipsoidSurface) {
  442. length = outerRing.length;
  443. for (i = 0; i < length; i++) {
  444. ellipsoid.scaleToGeodeticSurface(outerRing[i], outerRing[i]);
  445. }
  446. }
  447. outerRing = arrayRemoveDuplicates.arrayRemoveDuplicates(
  448. outerRing,
  449. Matrix2.Cartesian3.equalsEpsilon,
  450. true
  451. );
  452. if (outerRing.length < 3) {
  453. continue;
  454. }
  455. const numChildren = outerNode.holes ? outerNode.holes.length : 0;
  456. // The outer polygon contains inner polygons
  457. for (i = 0; i < numChildren; i++) {
  458. const hole = outerNode.holes[i];
  459. let holePositions = hole.positions;
  460. if (scaleToEllipsoidSurface) {
  461. length = holePositions.length;
  462. for (j = 0; j < length; ++j) {
  463. ellipsoid.scaleToGeodeticSurface(holePositions[j], holePositions[j]);
  464. }
  465. }
  466. holePositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  467. holePositions,
  468. Matrix2.Cartesian3.equalsEpsilon,
  469. true
  470. );
  471. if (holePositions.length < 3) {
  472. continue;
  473. }
  474. polygons.push(holePositions);
  475. let numGrandchildren = 0;
  476. if (defaultValue.defined(hole.holes)) {
  477. numGrandchildren = hole.holes.length;
  478. }
  479. for (j = 0; j < numGrandchildren; j++) {
  480. queue.enqueue(hole.holes[j]);
  481. }
  482. }
  483. polygons.push(outerRing);
  484. }
  485. return polygons;
  486. };
  487. PolygonGeometryLibrary.polygonsFromHierarchy = function (
  488. polygonHierarchy,
  489. keepDuplicates,
  490. projectPointsTo2D,
  491. scaleToEllipsoidSurface,
  492. ellipsoid
  493. ) {
  494. // create from a polygon hierarchy
  495. // Algorithm adapted from http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
  496. const hierarchy = [];
  497. const polygons = [];
  498. const queue = new Queue();
  499. queue.enqueue(polygonHierarchy);
  500. while (queue.length !== 0) {
  501. const outerNode = queue.dequeue();
  502. let outerRing = outerNode.positions;
  503. const holes = outerNode.holes;
  504. let i;
  505. let length;
  506. if (scaleToEllipsoidSurface) {
  507. length = outerRing.length;
  508. for (i = 0; i < length; i++) {
  509. ellipsoid.scaleToGeodeticSurface(outerRing[i], outerRing[i]);
  510. }
  511. }
  512. if (!keepDuplicates) {
  513. outerRing = arrayRemoveDuplicates.arrayRemoveDuplicates(
  514. outerRing,
  515. Matrix2.Cartesian3.equalsEpsilon,
  516. true
  517. );
  518. }
  519. if (outerRing.length < 3) {
  520. continue;
  521. }
  522. let positions2D = projectPointsTo2D(outerRing);
  523. if (!defaultValue.defined(positions2D)) {
  524. continue;
  525. }
  526. const holeIndices = [];
  527. let originalWindingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(
  528. positions2D
  529. );
  530. if (originalWindingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  531. positions2D.reverse();
  532. outerRing = outerRing.slice().reverse();
  533. }
  534. let positions = outerRing.slice();
  535. const numChildren = defaultValue.defined(holes) ? holes.length : 0;
  536. const polygonHoles = [];
  537. let j;
  538. for (i = 0; i < numChildren; i++) {
  539. const hole = holes[i];
  540. let holePositions = hole.positions;
  541. if (scaleToEllipsoidSurface) {
  542. length = holePositions.length;
  543. for (j = 0; j < length; ++j) {
  544. ellipsoid.scaleToGeodeticSurface(holePositions[j], holePositions[j]);
  545. }
  546. }
  547. if (!keepDuplicates) {
  548. holePositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  549. holePositions,
  550. Matrix2.Cartesian3.equalsEpsilon,
  551. true
  552. );
  553. }
  554. if (holePositions.length < 3) {
  555. continue;
  556. }
  557. const holePositions2D = projectPointsTo2D(holePositions);
  558. if (!defaultValue.defined(holePositions2D)) {
  559. continue;
  560. }
  561. originalWindingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(
  562. holePositions2D
  563. );
  564. if (originalWindingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  565. holePositions2D.reverse();
  566. holePositions = holePositions.slice().reverse();
  567. }
  568. polygonHoles.push(holePositions);
  569. holeIndices.push(positions.length);
  570. positions = positions.concat(holePositions);
  571. positions2D = positions2D.concat(holePositions2D);
  572. let numGrandchildren = 0;
  573. if (defaultValue.defined(hole.holes)) {
  574. numGrandchildren = hole.holes.length;
  575. }
  576. for (j = 0; j < numGrandchildren; j++) {
  577. queue.enqueue(hole.holes[j]);
  578. }
  579. }
  580. hierarchy.push({
  581. outerRing: outerRing,
  582. holes: polygonHoles,
  583. });
  584. polygons.push({
  585. positions: positions,
  586. positions2D: positions2D,
  587. holes: holeIndices,
  588. });
  589. }
  590. return {
  591. hierarchy: hierarchy,
  592. polygons: polygons,
  593. };
  594. };
  595. const computeBoundingRectangleCartesian2 = new Matrix2.Cartesian2();
  596. const computeBoundingRectangleCartesian3 = new Matrix2.Cartesian3();
  597. const computeBoundingRectangleQuaternion = new Transforms.Quaternion();
  598. const computeBoundingRectangleMatrix3 = new Matrix2.Matrix3();
  599. PolygonGeometryLibrary.computeBoundingRectangle = function (
  600. planeNormal,
  601. projectPointTo2D,
  602. positions,
  603. angle,
  604. result
  605. ) {
  606. const rotation = Transforms.Quaternion.fromAxisAngle(
  607. planeNormal,
  608. angle,
  609. computeBoundingRectangleQuaternion
  610. );
  611. const textureMatrix = Matrix2.Matrix3.fromQuaternion(
  612. rotation,
  613. computeBoundingRectangleMatrix3
  614. );
  615. let minX = Number.POSITIVE_INFINITY;
  616. let maxX = Number.NEGATIVE_INFINITY;
  617. let minY = Number.POSITIVE_INFINITY;
  618. let maxY = Number.NEGATIVE_INFINITY;
  619. const length = positions.length;
  620. for (let i = 0; i < length; ++i) {
  621. const p = Matrix2.Cartesian3.clone(
  622. positions[i],
  623. computeBoundingRectangleCartesian3
  624. );
  625. Matrix2.Matrix3.multiplyByVector(textureMatrix, p, p);
  626. const st = projectPointTo2D(p, computeBoundingRectangleCartesian2);
  627. if (defaultValue.defined(st)) {
  628. minX = Math.min(minX, st.x);
  629. maxX = Math.max(maxX, st.x);
  630. minY = Math.min(minY, st.y);
  631. maxY = Math.max(maxY, st.y);
  632. }
  633. }
  634. result.x = minX;
  635. result.y = minY;
  636. result.width = maxX - minX;
  637. result.height = maxY - minY;
  638. return result;
  639. };
  640. PolygonGeometryLibrary.createGeometryFromPositions = function (
  641. ellipsoid,
  642. polygon,
  643. textureCoordinates,
  644. granularity,
  645. perPositionHeight,
  646. vertexFormat,
  647. arcType
  648. ) {
  649. let indices = PolygonPipeline.PolygonPipeline.triangulate(polygon.positions2D, polygon.holes);
  650. /* If polygon is completely unrenderable, just use the first three vertices */
  651. if (indices.length < 3) {
  652. indices = [0, 1, 2];
  653. }
  654. const positions = polygon.positions;
  655. const hasTexcoords = defaultValue.defined(textureCoordinates);
  656. const texcoords = hasTexcoords ? textureCoordinates.positions : undefined;
  657. if (perPositionHeight) {
  658. const length = positions.length;
  659. const flattenedPositions = new Array(length * 3);
  660. let index = 0;
  661. for (let i = 0; i < length; i++) {
  662. const p = positions[i];
  663. flattenedPositions[index++] = p.x;
  664. flattenedPositions[index++] = p.y;
  665. flattenedPositions[index++] = p.z;
  666. }
  667. const geometryOptions = {
  668. attributes: {
  669. position: new GeometryAttribute.GeometryAttribute({
  670. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  671. componentsPerAttribute: 3,
  672. values: flattenedPositions,
  673. }),
  674. },
  675. indices: indices,
  676. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  677. };
  678. if (hasTexcoords) {
  679. geometryOptions.attributes.st = new GeometryAttribute.GeometryAttribute({
  680. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  681. componentsPerAttribute: 2,
  682. values: Matrix2.Cartesian2.packArray(texcoords),
  683. });
  684. }
  685. const geometry = new GeometryAttribute.Geometry(geometryOptions);
  686. if (vertexFormat.normal) {
  687. return GeometryPipeline.GeometryPipeline.computeNormal(geometry);
  688. }
  689. return geometry;
  690. }
  691. if (arcType === ArcType.ArcType.GEODESIC) {
  692. return PolygonPipeline.PolygonPipeline.computeSubdivision(
  693. ellipsoid,
  694. positions,
  695. indices,
  696. texcoords,
  697. granularity
  698. );
  699. } else if (arcType === ArcType.ArcType.RHUMB) {
  700. return PolygonPipeline.PolygonPipeline.computeRhumbLineSubdivision(
  701. ellipsoid,
  702. positions,
  703. indices,
  704. texcoords,
  705. granularity
  706. );
  707. }
  708. };
  709. const computeWallTexcoordsSubdivided = [];
  710. const computeWallIndicesSubdivided = [];
  711. const p1Scratch = new Matrix2.Cartesian3();
  712. const p2Scratch = new Matrix2.Cartesian3();
  713. PolygonGeometryLibrary.computeWallGeometry = function (
  714. positions,
  715. textureCoordinates,
  716. ellipsoid,
  717. granularity,
  718. perPositionHeight,
  719. arcType
  720. ) {
  721. let edgePositions;
  722. let topEdgeLength;
  723. let i;
  724. let p1;
  725. let p2;
  726. let t1;
  727. let t2;
  728. let edgeTexcoords;
  729. let topEdgeTexcoordLength;
  730. let length = positions.length;
  731. let index = 0;
  732. let textureIndex = 0;
  733. const hasTexcoords = defaultValue.defined(textureCoordinates);
  734. const texcoords = hasTexcoords ? textureCoordinates.positions : undefined;
  735. if (!perPositionHeight) {
  736. const minDistance = ComponentDatatype.CesiumMath.chordLength(
  737. granularity,
  738. ellipsoid.maximumRadius
  739. );
  740. let numVertices = 0;
  741. if (arcType === ArcType.ArcType.GEODESIC) {
  742. for (i = 0; i < length; i++) {
  743. numVertices += PolygonGeometryLibrary.subdivideLineCount(
  744. positions[i],
  745. positions[(i + 1) % length],
  746. minDistance
  747. );
  748. }
  749. } else if (arcType === ArcType.ArcType.RHUMB) {
  750. for (i = 0; i < length; i++) {
  751. numVertices += PolygonGeometryLibrary.subdivideRhumbLineCount(
  752. ellipsoid,
  753. positions[i],
  754. positions[(i + 1) % length],
  755. minDistance
  756. );
  757. }
  758. }
  759. topEdgeLength = (numVertices + length) * 3;
  760. edgePositions = new Array(topEdgeLength * 2);
  761. if (hasTexcoords) {
  762. topEdgeTexcoordLength = (numVertices + length) * 2;
  763. edgeTexcoords = new Array(topEdgeTexcoordLength * 2);
  764. }
  765. for (i = 0; i < length; i++) {
  766. p1 = positions[i];
  767. p2 = positions[(i + 1) % length];
  768. let tempPositions;
  769. let tempTexcoords;
  770. if (hasTexcoords) {
  771. t1 = texcoords[i];
  772. t2 = texcoords[(i + 1) % length];
  773. }
  774. if (arcType === ArcType.ArcType.GEODESIC) {
  775. tempPositions = PolygonGeometryLibrary.subdivideLine(
  776. p1,
  777. p2,
  778. minDistance,
  779. computeWallIndicesSubdivided
  780. );
  781. if (hasTexcoords) {
  782. tempTexcoords = PolygonGeometryLibrary.subdivideTexcoordLine(
  783. t1,
  784. t2,
  785. p1,
  786. p2,
  787. minDistance,
  788. computeWallTexcoordsSubdivided
  789. );
  790. }
  791. } else if (arcType === ArcType.ArcType.RHUMB) {
  792. tempPositions = PolygonGeometryLibrary.subdivideRhumbLine(
  793. ellipsoid,
  794. p1,
  795. p2,
  796. minDistance,
  797. computeWallIndicesSubdivided
  798. );
  799. if (hasTexcoords) {
  800. tempTexcoords = PolygonGeometryLibrary.subdivideTexcoordRhumbLine(
  801. t1,
  802. t2,
  803. ellipsoid,
  804. p1,
  805. p2,
  806. minDistance,
  807. computeWallTexcoordsSubdivided
  808. );
  809. }
  810. }
  811. const tempPositionsLength = tempPositions.length;
  812. for (let j = 0; j < tempPositionsLength; ++j, ++index) {
  813. edgePositions[index] = tempPositions[j];
  814. edgePositions[index + topEdgeLength] = tempPositions[j];
  815. }
  816. edgePositions[index] = p2.x;
  817. edgePositions[index + topEdgeLength] = p2.x;
  818. ++index;
  819. edgePositions[index] = p2.y;
  820. edgePositions[index + topEdgeLength] = p2.y;
  821. ++index;
  822. edgePositions[index] = p2.z;
  823. edgePositions[index + topEdgeLength] = p2.z;
  824. ++index;
  825. if (hasTexcoords) {
  826. const tempTexcoordsLength = tempTexcoords.length;
  827. for (let k = 0; k < tempTexcoordsLength; ++k, ++textureIndex) {
  828. edgeTexcoords[textureIndex] = tempTexcoords[k];
  829. edgeTexcoords[textureIndex + topEdgeTexcoordLength] =
  830. tempTexcoords[k];
  831. }
  832. edgeTexcoords[textureIndex] = t2.x;
  833. edgeTexcoords[textureIndex + topEdgeTexcoordLength] = t2.x;
  834. ++textureIndex;
  835. edgeTexcoords[textureIndex] = t2.y;
  836. edgeTexcoords[textureIndex + topEdgeTexcoordLength] = t2.y;
  837. ++textureIndex;
  838. }
  839. }
  840. } else {
  841. topEdgeLength = length * 3 * 2;
  842. edgePositions = new Array(topEdgeLength * 2);
  843. if (hasTexcoords) {
  844. topEdgeTexcoordLength = length * 2 * 2;
  845. edgeTexcoords = new Array(topEdgeTexcoordLength * 2);
  846. }
  847. for (i = 0; i < length; i++) {
  848. p1 = positions[i];
  849. p2 = positions[(i + 1) % length];
  850. edgePositions[index] = edgePositions[index + topEdgeLength] = p1.x;
  851. ++index;
  852. edgePositions[index] = edgePositions[index + topEdgeLength] = p1.y;
  853. ++index;
  854. edgePositions[index] = edgePositions[index + topEdgeLength] = p1.z;
  855. ++index;
  856. edgePositions[index] = edgePositions[index + topEdgeLength] = p2.x;
  857. ++index;
  858. edgePositions[index] = edgePositions[index + topEdgeLength] = p2.y;
  859. ++index;
  860. edgePositions[index] = edgePositions[index + topEdgeLength] = p2.z;
  861. ++index;
  862. if (hasTexcoords) {
  863. t1 = texcoords[i];
  864. t2 = texcoords[(i + 1) % length];
  865. edgeTexcoords[textureIndex] = edgeTexcoords[
  866. textureIndex + topEdgeTexcoordLength
  867. ] = t1.x;
  868. ++textureIndex;
  869. edgeTexcoords[textureIndex] = edgeTexcoords[
  870. textureIndex + topEdgeTexcoordLength
  871. ] = t1.y;
  872. ++textureIndex;
  873. edgeTexcoords[textureIndex] = edgeTexcoords[
  874. textureIndex + topEdgeTexcoordLength
  875. ] = t2.x;
  876. ++textureIndex;
  877. edgeTexcoords[textureIndex] = edgeTexcoords[
  878. textureIndex + topEdgeTexcoordLength
  879. ] = t2.y;
  880. ++textureIndex;
  881. }
  882. }
  883. }
  884. length = edgePositions.length;
  885. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  886. length / 3,
  887. length - positions.length * 6
  888. );
  889. let edgeIndex = 0;
  890. length /= 6;
  891. for (i = 0; i < length; i++) {
  892. const UL = i;
  893. const UR = UL + 1;
  894. const LL = UL + length;
  895. const LR = LL + 1;
  896. p1 = Matrix2.Cartesian3.fromArray(edgePositions, UL * 3, p1Scratch);
  897. p2 = Matrix2.Cartesian3.fromArray(edgePositions, UR * 3, p2Scratch);
  898. if (
  899. Matrix2.Cartesian3.equalsEpsilon(
  900. p1,
  901. p2,
  902. ComponentDatatype.CesiumMath.EPSILON10,
  903. ComponentDatatype.CesiumMath.EPSILON10
  904. )
  905. ) {
  906. //skip corner
  907. continue;
  908. }
  909. indices[edgeIndex++] = UL;
  910. indices[edgeIndex++] = LL;
  911. indices[edgeIndex++] = UR;
  912. indices[edgeIndex++] = UR;
  913. indices[edgeIndex++] = LL;
  914. indices[edgeIndex++] = LR;
  915. }
  916. const geometryOptions = {
  917. attributes: new GeometryAttributes.GeometryAttributes({
  918. position: new GeometryAttribute.GeometryAttribute({
  919. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  920. componentsPerAttribute: 3,
  921. values: edgePositions,
  922. }),
  923. }),
  924. indices: indices,
  925. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  926. };
  927. if (hasTexcoords) {
  928. geometryOptions.attributes.st = new GeometryAttribute.GeometryAttribute({
  929. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  930. componentsPerAttribute: 2,
  931. values: edgeTexcoords,
  932. });
  933. }
  934. const geometry = new GeometryAttribute.Geometry(geometryOptions);
  935. return geometry;
  936. };
  937. exports.PolygonGeometryLibrary = PolygonGeometryLibrary;
  938. }));