PolygonGeometryLibrary-073be5ba.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /**
  2. * Cesium - https://github.com/AnalyticalGraphicsInc/cesium
  3. *
  4. * Copyright 2011-2017 Cesium Contributors
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * Columbus View (Pat. Pend.)
  19. *
  20. * Portions licensed separately.
  21. * See https://github.com/AnalyticalGraphicsInc/cesium/blob/master/LICENSE.md for full licensing details.
  22. */
  23. define(['exports', './when-8d13db60', './Math-61ede240', './Cartographic-fe4be337', './Cartesian2-85064f09', './BoundingSphere-775c5788', './ComponentDatatype-5862616f', './GeometryAttribute-91704ebb', './PrimitiveType-97893bc7', './Transforms-b2e71640', './GeometryAttributes-aacecde6', './GeometryPipeline-f95a0a6f', './IndexDatatype-9435b55f', './arrayRemoveDuplicates-f0b089b1', './ArcType-66bc286a', './EllipsoidRhumbLine-f161e674', './PolygonPipeline-6a35d737'], function (exports, when, _Math, Cartographic, Cartesian2, BoundingSphere, ComponentDatatype, GeometryAttribute, PrimitiveType, Transforms, GeometryAttributes, GeometryPipeline, IndexDatatype, arrayRemoveDuplicates, ArcType, EllipsoidRhumbLine, PolygonPipeline) { 'use strict';
  24. /**
  25. * A queue that can enqueue items at the end, and dequeue items from the front.
  26. *
  27. * @alias Queue
  28. * @constructor
  29. */
  30. function Queue() {
  31. this._array = [];
  32. this._offset = 0;
  33. this._length = 0;
  34. }
  35. Object.defineProperties(Queue.prototype, {
  36. /**
  37. * The length of the queue.
  38. *
  39. * @memberof Queue.prototype
  40. *
  41. * @type {Number}
  42. * @readonly
  43. */
  44. length : {
  45. get : function() {
  46. return this._length;
  47. }
  48. }
  49. });
  50. /**
  51. * Enqueues the specified item.
  52. *
  53. * @param {*} item The item to enqueue.
  54. */
  55. Queue.prototype.enqueue = function(item) {
  56. this._array.push(item);
  57. this._length++;
  58. };
  59. /**
  60. * Dequeues an item. Returns undefined if the queue is empty.
  61. *
  62. * @returns {*} The the dequeued item.
  63. */
  64. Queue.prototype.dequeue = function() {
  65. if (this._length === 0) {
  66. return undefined;
  67. }
  68. var array = this._array;
  69. var offset = this._offset;
  70. var item = array[offset];
  71. array[offset] = undefined;
  72. offset++;
  73. if ((offset > 10) && (offset * 2 > array.length)) {
  74. //compact array
  75. this._array = array.slice(offset);
  76. offset = 0;
  77. }
  78. this._offset = offset;
  79. this._length--;
  80. return item;
  81. };
  82. /**
  83. * Returns the item at the front of the queue. Returns undefined if the queue is empty.
  84. *
  85. * @returns {*} The item at the front of the queue.
  86. */
  87. Queue.prototype.peek = function() {
  88. if (this._length === 0) {
  89. return undefined;
  90. }
  91. return this._array[this._offset];
  92. };
  93. /**
  94. * Check whether this queue contains the specified item.
  95. *
  96. * @param {*} item The item to search for.
  97. */
  98. Queue.prototype.contains = function(item) {
  99. return this._array.indexOf(item) !== -1;
  100. };
  101. /**
  102. * Remove all items from the queue.
  103. */
  104. Queue.prototype.clear = function() {
  105. this._array.length = this._offset = this._length = 0;
  106. };
  107. /**
  108. * Sort the items in the queue in-place.
  109. *
  110. * @param {Queue~Comparator} compareFunction A function that defines the sort order.
  111. */
  112. Queue.prototype.sort = function(compareFunction) {
  113. if (this._offset > 0) {
  114. //compact array
  115. this._array = this._array.slice(this._offset);
  116. this._offset = 0;
  117. }
  118. this._array.sort(compareFunction);
  119. };
  120. /**
  121. * @private
  122. */
  123. var PolygonGeometryLibrary = {};
  124. PolygonGeometryLibrary.computeHierarchyPackedLength = function(polygonHierarchy) {
  125. var numComponents = 0;
  126. var stack = [polygonHierarchy];
  127. while (stack.length > 0) {
  128. var hierarchy = stack.pop();
  129. if (!when.defined(hierarchy)) {
  130. continue;
  131. }
  132. numComponents += 2;
  133. var positions = hierarchy.positions;
  134. var holes = hierarchy.holes;
  135. if (when.defined(positions)) {
  136. numComponents += positions.length * Cartographic.Cartesian3.packedLength;
  137. }
  138. if (when.defined(holes)) {
  139. var length = holes.length;
  140. for (var i = 0; i < length; ++i) {
  141. stack.push(holes[i]);
  142. }
  143. }
  144. }
  145. return numComponents;
  146. };
  147. PolygonGeometryLibrary.packPolygonHierarchy = function(polygonHierarchy, array, startingIndex) {
  148. var stack = [polygonHierarchy];
  149. while (stack.length > 0) {
  150. var hierarchy = stack.pop();
  151. if (!when.defined(hierarchy)) {
  152. continue;
  153. }
  154. var positions = hierarchy.positions;
  155. var holes = hierarchy.holes;
  156. array[startingIndex++] = when.defined(positions) ? positions.length : 0;
  157. array[startingIndex++] = when.defined(holes) ? holes.length : 0;
  158. if (when.defined(positions)) {
  159. var positionsLength = positions.length;
  160. for (var i = 0; i < positionsLength; ++i, startingIndex += 3) {
  161. Cartographic.Cartesian3.pack(positions[i], array, startingIndex);
  162. }
  163. }
  164. if (when.defined(holes)) {
  165. var holesLength = holes.length;
  166. for (var j = 0; j < holesLength; ++j) {
  167. stack.push(holes[j]);
  168. }
  169. }
  170. }
  171. return startingIndex;
  172. };
  173. PolygonGeometryLibrary.unpackPolygonHierarchy = function(array, startingIndex) {
  174. var positionsLength = array[startingIndex++];
  175. var holesLength = array[startingIndex++];
  176. var positions = new Array(positionsLength);
  177. var holes = holesLength > 0 ? new Array(holesLength) : undefined;
  178. for (var i = 0; i < positionsLength; ++i, startingIndex += Cartographic.Cartesian3.packedLength) {
  179. positions[i] = Cartographic.Cartesian3.unpack(array, startingIndex);
  180. }
  181. for (var j = 0; j < holesLength; ++j) {
  182. holes[j] = PolygonGeometryLibrary.unpackPolygonHierarchy(array, startingIndex);
  183. startingIndex = holes[j].startingIndex;
  184. delete holes[j].startingIndex;
  185. }
  186. return {
  187. positions : positions,
  188. holes : holes,
  189. startingIndex : startingIndex
  190. };
  191. };
  192. var distanceScratch = new Cartographic.Cartesian3();
  193. function getPointAtDistance(p0, p1, distance, length) {
  194. Cartographic.Cartesian3.subtract(p1, p0, distanceScratch);
  195. Cartographic.Cartesian3.multiplyByScalar(distanceScratch, distance / length, distanceScratch);
  196. Cartographic.Cartesian3.add(p0, distanceScratch, distanceScratch);
  197. return [distanceScratch.x, distanceScratch.y, distanceScratch.z];
  198. }
  199. PolygonGeometryLibrary.subdivideLineCount = function(p0, p1, minDistance) {
  200. var distance = Cartographic.Cartesian3.distance(p0, p1);
  201. var n = distance / minDistance;
  202. var countDivide = Math.max(0, Math.ceil(_Math.CesiumMath.log2(n)));
  203. return Math.pow(2, countDivide);
  204. };
  205. var scratchCartographic0 = new Cartographic.Cartographic();
  206. var scratchCartographic1 = new Cartographic.Cartographic();
  207. var scratchCartographic2 = new Cartographic.Cartographic();
  208. var scratchCartesian0 = new Cartographic.Cartesian3();
  209. PolygonGeometryLibrary.subdivideRhumbLineCount = function(ellipsoid, p0, p1, minDistance) {
  210. var c0 = ellipsoid.cartesianToCartographic(p0, scratchCartographic0);
  211. var c1 = ellipsoid.cartesianToCartographic(p1, scratchCartographic1);
  212. var rhumb = new EllipsoidRhumbLine.EllipsoidRhumbLine(c0, c1, ellipsoid);
  213. var n = rhumb.surfaceDistance / minDistance;
  214. var countDivide = Math.max(0, Math.ceil(_Math.CesiumMath.log2(n)));
  215. return Math.pow(2, countDivide);
  216. };
  217. PolygonGeometryLibrary.subdivideLine = function(p0, p1, minDistance, result) {
  218. var numVertices = PolygonGeometryLibrary.subdivideLineCount(p0, p1, minDistance);
  219. var length = Cartographic.Cartesian3.distance(p0, p1);
  220. var distanceBetweenVertices = length / numVertices;
  221. if (!when.defined(result)) {
  222. result = [];
  223. }
  224. var positions = result;
  225. positions.length = numVertices * 3;
  226. var index = 0;
  227. for ( var i = 0; i < numVertices; i++) {
  228. var p = getPointAtDistance(p0, p1, i * distanceBetweenVertices, length);
  229. positions[index++] = p[0];
  230. positions[index++] = p[1];
  231. positions[index++] = p[2];
  232. }
  233. return positions;
  234. };
  235. PolygonGeometryLibrary.subdivideRhumbLine = function(ellipsoid, p0, p1, minDistance, result) {
  236. var c0 = ellipsoid.cartesianToCartographic(p0, scratchCartographic0);
  237. var c1 = ellipsoid.cartesianToCartographic(p1, scratchCartographic1);
  238. var rhumb = new EllipsoidRhumbLine.EllipsoidRhumbLine(c0, c1, ellipsoid);
  239. var n = rhumb.surfaceDistance / minDistance;
  240. var countDivide = Math.max(0, Math.ceil(_Math.CesiumMath.log2(n)));
  241. var numVertices = Math.pow(2, countDivide);
  242. var distanceBetweenVertices = rhumb.surfaceDistance / numVertices;
  243. if (!when.defined(result)) {
  244. result = [];
  245. }
  246. var positions = result;
  247. positions.length = numVertices * 3;
  248. var index = 0;
  249. for ( var i = 0; i < numVertices; i++) {
  250. var c = rhumb.interpolateUsingSurfaceDistance(i * distanceBetweenVertices, scratchCartographic2);
  251. var p = ellipsoid.cartographicToCartesian(c, scratchCartesian0);
  252. positions[index++] = p.x;
  253. positions[index++] = p.y;
  254. positions[index++] = p.z;
  255. }
  256. return positions;
  257. };
  258. var scaleToGeodeticHeightN1 = new Cartographic.Cartesian3();
  259. var scaleToGeodeticHeightN2 = new Cartographic.Cartesian3();
  260. var scaleToGeodeticHeightP1 = new Cartographic.Cartesian3();
  261. var scaleToGeodeticHeightP2 = new Cartographic.Cartesian3();
  262. PolygonGeometryLibrary.scaleToGeodeticHeightExtruded = function(geometry, maxHeight, minHeight, ellipsoid, perPositionHeight) {
  263. ellipsoid = when.defaultValue(ellipsoid, Cartesian2.Ellipsoid.WGS84);
  264. var n1 = scaleToGeodeticHeightN1;
  265. var n2 = scaleToGeodeticHeightN2;
  266. var p = scaleToGeodeticHeightP1;
  267. var p2 = scaleToGeodeticHeightP2;
  268. if (when.defined(geometry) && when.defined(geometry.attributes) && when.defined(geometry.attributes.position)) {
  269. var positions = geometry.attributes.position.values;
  270. var length = positions.length / 2;
  271. for ( var i = 0; i < length; i += 3) {
  272. Cartographic.Cartesian3.fromArray(positions, i, p);
  273. ellipsoid.geodeticSurfaceNormal(p, n1);
  274. p2 = ellipsoid.scaleToGeodeticSurface(p, p2);
  275. n2 = Cartographic.Cartesian3.multiplyByScalar(n1, minHeight, n2);
  276. n2 = Cartographic.Cartesian3.add(p2, n2, n2);
  277. positions[i + length] = n2.x;
  278. positions[i + 1 + length] = n2.y;
  279. positions[i + 2 + length] = n2.z;
  280. if (perPositionHeight) {
  281. p2 = Cartographic.Cartesian3.clone(p, p2);
  282. }
  283. n2 = Cartographic.Cartesian3.multiplyByScalar(n1, maxHeight, n2);
  284. n2 = Cartographic.Cartesian3.add(p2, n2, n2);
  285. positions[i] = n2.x;
  286. positions[i + 1] = n2.y;
  287. positions[i + 2] = n2.z;
  288. }
  289. }
  290. return geometry;
  291. };
  292. PolygonGeometryLibrary.polygonOutlinesFromHierarchy = function(polygonHierarchy, scaleToEllipsoidSurface, ellipsoid) {
  293. // create from a polygon hierarchy
  294. // Algorithm adapted from http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
  295. var polygons = [];
  296. var queue = new Queue();
  297. queue.enqueue(polygonHierarchy);
  298. var i;
  299. var j;
  300. var length;
  301. while (queue.length !== 0) {
  302. var outerNode = queue.dequeue();
  303. var outerRing = outerNode.positions;
  304. if (scaleToEllipsoidSurface) {
  305. length = outerRing.length;
  306. for (i = 0; i < length; i++) {
  307. ellipsoid.scaleToGeodeticSurface(outerRing[i], outerRing[i]);
  308. }
  309. }
  310. outerRing = arrayRemoveDuplicates.arrayRemoveDuplicates(outerRing, Cartographic.Cartesian3.equalsEpsilon, true);
  311. if (outerRing.length < 3) {
  312. continue;
  313. }
  314. var numChildren = outerNode.holes ? outerNode.holes.length : 0;
  315. // The outer polygon contains inner polygons
  316. for (i = 0; i < numChildren; i++) {
  317. var hole = outerNode.holes[i];
  318. var holePositions = hole.positions;
  319. if (scaleToEllipsoidSurface) {
  320. length = holePositions.length;
  321. for (j = 0; j < length; ++j) {
  322. ellipsoid.scaleToGeodeticSurface(holePositions[j], holePositions[j]);
  323. }
  324. }
  325. holePositions = arrayRemoveDuplicates.arrayRemoveDuplicates(holePositions, Cartographic.Cartesian3.equalsEpsilon, true);
  326. if (holePositions.length < 3) {
  327. continue;
  328. }
  329. polygons.push(holePositions);
  330. var numGrandchildren = 0;
  331. if (when.defined(hole.holes)) {
  332. numGrandchildren = hole.holes.length;
  333. }
  334. for (j = 0; j < numGrandchildren; j++) {
  335. queue.enqueue(hole.holes[j]);
  336. }
  337. }
  338. polygons.push(outerRing);
  339. }
  340. return polygons;
  341. };
  342. PolygonGeometryLibrary.polygonsFromHierarchy = function(polygonHierarchy, projectPointsTo2D, scaleToEllipsoidSurface, ellipsoid) {
  343. // create from a polygon hierarchy
  344. // Algorithm adapted from http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
  345. var hierarchy = [];
  346. var polygons = [];
  347. var queue = new Queue();
  348. queue.enqueue(polygonHierarchy);
  349. while (queue.length !== 0) {
  350. var outerNode = queue.dequeue();
  351. var outerRing = outerNode.positions;
  352. var holes = outerNode.holes;
  353. var i;
  354. var length;
  355. if (scaleToEllipsoidSurface) {
  356. length = outerRing.length;
  357. for (i = 0; i < length; i++) {
  358. ellipsoid.scaleToGeodeticSurface(outerRing[i], outerRing[i]);
  359. }
  360. }
  361. outerRing = arrayRemoveDuplicates.arrayRemoveDuplicates(outerRing, Cartographic.Cartesian3.equalsEpsilon, true);
  362. if (outerRing.length < 3) {
  363. continue;
  364. }
  365. var positions2D = projectPointsTo2D(outerRing);
  366. if (!when.defined(positions2D)) {
  367. continue;
  368. }
  369. var holeIndices = [];
  370. var originalWindingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(positions2D);
  371. if (originalWindingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  372. positions2D.reverse();
  373. outerRing = outerRing.slice().reverse();
  374. }
  375. var positions = outerRing.slice();
  376. var numChildren = when.defined(holes) ? holes.length : 0;
  377. var polygonHoles = [];
  378. var j;
  379. for (i = 0; i < numChildren; i++) {
  380. var hole = holes[i];
  381. var holePositions = hole.positions;
  382. if (scaleToEllipsoidSurface) {
  383. length = holePositions.length;
  384. for (j = 0; j < length; ++j) {
  385. ellipsoid.scaleToGeodeticSurface(holePositions[j], holePositions[j]);
  386. }
  387. }
  388. holePositions = arrayRemoveDuplicates.arrayRemoveDuplicates(holePositions, Cartographic.Cartesian3.equalsEpsilon, true);
  389. if (holePositions.length < 3) {
  390. continue;
  391. }
  392. var holePositions2D = projectPointsTo2D(holePositions);
  393. if (!when.defined(holePositions2D)) {
  394. continue;
  395. }
  396. originalWindingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(holePositions2D);
  397. if (originalWindingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  398. holePositions2D.reverse();
  399. holePositions = holePositions.slice().reverse();
  400. }
  401. polygonHoles.push(holePositions);
  402. holeIndices.push(positions.length);
  403. positions = positions.concat(holePositions);
  404. positions2D = positions2D.concat(holePositions2D);
  405. var numGrandchildren = 0;
  406. if (when.defined(hole.holes)) {
  407. numGrandchildren = hole.holes.length;
  408. }
  409. for (j = 0; j < numGrandchildren; j++) {
  410. queue.enqueue(hole.holes[j]);
  411. }
  412. }
  413. hierarchy.push({
  414. outerRing : outerRing,
  415. holes : polygonHoles
  416. });
  417. polygons.push({
  418. positions : positions,
  419. positions2D : positions2D,
  420. holes : holeIndices
  421. });
  422. }
  423. return {
  424. hierarchy : hierarchy,
  425. polygons : polygons
  426. };
  427. };
  428. var computeBoundingRectangleCartesian2 = new Cartesian2.Cartesian2();
  429. var computeBoundingRectangleCartesian3 = new Cartographic.Cartesian3();
  430. var computeBoundingRectangleQuaternion = new Transforms.Quaternion();
  431. var computeBoundingRectangleMatrix3 = new BoundingSphere.Matrix3();
  432. PolygonGeometryLibrary.computeBoundingRectangle = function (planeNormal, projectPointTo2D, positions, angle, result) {
  433. var rotation = Transforms.Quaternion.fromAxisAngle(planeNormal, angle, computeBoundingRectangleQuaternion);
  434. var textureMatrix = BoundingSphere.Matrix3.fromQuaternion(rotation, computeBoundingRectangleMatrix3);
  435. var minX = Number.POSITIVE_INFINITY;
  436. var maxX = Number.NEGATIVE_INFINITY;
  437. var minY = Number.POSITIVE_INFINITY;
  438. var maxY = Number.NEGATIVE_INFINITY;
  439. var length = positions.length;
  440. for ( var i = 0; i < length; ++i) {
  441. var p = Cartographic.Cartesian3.clone(positions[i], computeBoundingRectangleCartesian3);
  442. BoundingSphere.Matrix3.multiplyByVector(textureMatrix, p, p);
  443. var st = projectPointTo2D(p, computeBoundingRectangleCartesian2);
  444. if (when.defined(st)) {
  445. minX = Math.min(minX, st.x);
  446. maxX = Math.max(maxX, st.x);
  447. minY = Math.min(minY, st.y);
  448. maxY = Math.max(maxY, st.y);
  449. }
  450. }
  451. result.x = minX;
  452. result.y = minY;
  453. result.width = maxX - minX;
  454. result.height = maxY - minY;
  455. return result;
  456. };
  457. PolygonGeometryLibrary.createGeometryFromPositions = function(ellipsoid, polygon, granularity, perPositionHeight, vertexFormat, arcType) {
  458. var indices = PolygonPipeline.PolygonPipeline.triangulate(polygon.positions2D, polygon.holes);
  459. /* If polygon is completely unrenderable, just use the first three vertices */
  460. if (indices.length < 3) {
  461. indices = [0, 1, 2];
  462. }
  463. var positions = polygon.positions;
  464. if (perPositionHeight) {
  465. var length = positions.length;
  466. var flattenedPositions = new Array(length * 3);
  467. var index = 0;
  468. for ( var i = 0; i < length; i++) {
  469. var p = positions[i];
  470. flattenedPositions[index++] = p.x;
  471. flattenedPositions[index++] = p.y;
  472. flattenedPositions[index++] = p.z;
  473. }
  474. var geometry = new GeometryAttribute.Geometry({
  475. attributes : {
  476. position : new GeometryAttribute.GeometryAttribute({
  477. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  478. componentsPerAttribute : 3,
  479. values : flattenedPositions
  480. })
  481. },
  482. indices : indices,
  483. primitiveType : PrimitiveType.PrimitiveType.TRIANGLES
  484. });
  485. if (vertexFormat.normal) {
  486. return GeometryPipeline.GeometryPipeline.computeNormal(geometry);
  487. }
  488. return geometry;
  489. }
  490. if (arcType === ArcType.ArcType.GEODESIC) {
  491. return PolygonPipeline.PolygonPipeline.computeSubdivision(ellipsoid, positions, indices, granularity);
  492. } else if (arcType === ArcType.ArcType.RHUMB) {
  493. return PolygonPipeline.PolygonPipeline.computeRhumbLineSubdivision(ellipsoid, positions, indices, granularity);
  494. }
  495. };
  496. var computeWallIndicesSubdivided = [];
  497. var p1Scratch = new Cartographic.Cartesian3();
  498. var p2Scratch = new Cartographic.Cartesian3();
  499. PolygonGeometryLibrary.computeWallGeometry = function(positions, ellipsoid, granularity, perPositionHeight, arcType) {
  500. var edgePositions;
  501. var topEdgeLength;
  502. var i;
  503. var p1;
  504. var p2;
  505. var length = positions.length;
  506. var index = 0;
  507. if (!perPositionHeight) {
  508. var minDistance = _Math.CesiumMath.chordLength(granularity, ellipsoid.maximumRadius);
  509. var numVertices = 0;
  510. if (arcType === ArcType.ArcType.GEODESIC) {
  511. for (i = 0; i < length; i++) {
  512. numVertices += PolygonGeometryLibrary.subdivideLineCount(positions[i], positions[(i + 1) % length], minDistance);
  513. }
  514. } else if (arcType === ArcType.ArcType.RHUMB) {
  515. for (i = 0; i < length; i++) {
  516. numVertices += PolygonGeometryLibrary.subdivideRhumbLineCount(ellipsoid, positions[i], positions[(i + 1) % length], minDistance);
  517. }
  518. }
  519. topEdgeLength = (numVertices + length) * 3;
  520. edgePositions = new Array(topEdgeLength * 2);
  521. for (i = 0; i < length; i++) {
  522. p1 = positions[i];
  523. p2 = positions[(i + 1) % length];
  524. var tempPositions;
  525. if (arcType === ArcType.ArcType.GEODESIC) {
  526. tempPositions = PolygonGeometryLibrary.subdivideLine(p1, p2, minDistance, computeWallIndicesSubdivided);
  527. } else if (arcType === ArcType.ArcType.RHUMB) {
  528. tempPositions = PolygonGeometryLibrary.subdivideRhumbLine(ellipsoid, p1, p2, minDistance, computeWallIndicesSubdivided);
  529. }
  530. var tempPositionsLength = tempPositions.length;
  531. for (var j = 0; j < tempPositionsLength; ++j, ++index) {
  532. edgePositions[index] = tempPositions[j];
  533. edgePositions[index + topEdgeLength] = tempPositions[j];
  534. }
  535. edgePositions[index] = p2.x;
  536. edgePositions[index + topEdgeLength] = p2.x;
  537. ++index;
  538. edgePositions[index] = p2.y;
  539. edgePositions[index + topEdgeLength] = p2.y;
  540. ++index;
  541. edgePositions[index] = p2.z;
  542. edgePositions[index + topEdgeLength] = p2.z;
  543. ++index;
  544. }
  545. } else {
  546. topEdgeLength = length * 3 * 2;
  547. edgePositions = new Array(topEdgeLength * 2);
  548. for (i = 0; i < length; i++) {
  549. p1 = positions[i];
  550. p2 = positions[(i + 1) % length];
  551. edgePositions[index] = edgePositions[index + topEdgeLength] = p1.x;
  552. ++index;
  553. edgePositions[index] = edgePositions[index + topEdgeLength] = p1.y;
  554. ++index;
  555. edgePositions[index] = edgePositions[index + topEdgeLength] = p1.z;
  556. ++index;
  557. edgePositions[index] = edgePositions[index + topEdgeLength] = p2.x;
  558. ++index;
  559. edgePositions[index] = edgePositions[index + topEdgeLength] = p2.y;
  560. ++index;
  561. edgePositions[index] = edgePositions[index + topEdgeLength] = p2.z;
  562. ++index;
  563. }
  564. }
  565. length = edgePositions.length;
  566. var indices = IndexDatatype.IndexDatatype.createTypedArray(length / 3, length - positions.length * 6);
  567. var edgeIndex = 0;
  568. length /= 6;
  569. for (i = 0; i < length; i++) {
  570. var UL = i;
  571. var UR = UL + 1;
  572. var LL = UL + length;
  573. var LR = LL + 1;
  574. p1 = Cartographic.Cartesian3.fromArray(edgePositions, UL * 3, p1Scratch);
  575. p2 = Cartographic.Cartesian3.fromArray(edgePositions, UR * 3, p2Scratch);
  576. if (Cartographic.Cartesian3.equalsEpsilon(p1, p2, _Math.CesiumMath.EPSILON14)) {
  577. continue;
  578. }
  579. indices[edgeIndex++] = UL;
  580. indices[edgeIndex++] = LL;
  581. indices[edgeIndex++] = UR;
  582. indices[edgeIndex++] = UR;
  583. indices[edgeIndex++] = LL;
  584. indices[edgeIndex++] = LR;
  585. }
  586. return new GeometryAttribute.Geometry({
  587. attributes : new GeometryAttributes.GeometryAttributes({
  588. position : new GeometryAttribute.GeometryAttribute({
  589. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  590. componentsPerAttribute : 3,
  591. values : edgePositions
  592. })
  593. }),
  594. indices : indices,
  595. primitiveType : PrimitiveType.PrimitiveType.TRIANGLES
  596. });
  597. };
  598. exports.PolygonGeometryLibrary = PolygonGeometryLibrary;
  599. });