createPolygonOutlineGeometry.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defaultValue-94c3e563', './Matrix2-69c32d33', './ArcType-0cf52f8c', './GeometryOffsetAttribute-3e8c299c', './Transforms-323408fe', './RuntimeError-c581ca93', './ComponentDatatype-b1ea011a', './EllipsoidTangentPlane-1e8d1fc2', './GeometryAttribute-cb73bb3f', './GeometryAttributes-7df9bef6', './GeometryInstance-f69fd420', './GeometryPipeline-e27e35f8', './IndexDatatype-c4099fe9', './PolygonGeometryLibrary-515b324f', './PolygonPipeline-d1884135', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96', './AxisAlignedBoundingBox-df2331b2', './IntersectionTests-d5d945ac', './Plane-069b6800', './AttributeCompression-3cfab808', './EncodedCartesian3-b1f97f8a', './arrayRemoveDuplicates-87160c89', './EllipsoidRhumbLine-5cb6da82'], (function (defaultValue, Matrix2, ArcType, GeometryOffsetAttribute, Transforms, RuntimeError, ComponentDatatype, EllipsoidTangentPlane, GeometryAttribute, GeometryAttributes, GeometryInstance, GeometryPipeline, IndexDatatype, PolygonGeometryLibrary, PolygonPipeline, _commonjsHelpers3aae1032, combine, WebGLConstants, AxisAlignedBoundingBox, IntersectionTests, Plane, AttributeCompression, EncodedCartesian3, arrayRemoveDuplicates, EllipsoidRhumbLine) { 'use strict';
  3. const createGeometryFromPositionsPositions = [];
  4. const createGeometryFromPositionsSubdivided = [];
  5. function createGeometryFromPositions(
  6. ellipsoid,
  7. positions,
  8. minDistance,
  9. perPositionHeight,
  10. arcType
  11. ) {
  12. const tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(positions, ellipsoid);
  13. const positions2D = tangentPlane.projectPointsOntoPlane(
  14. positions,
  15. createGeometryFromPositionsPositions
  16. );
  17. const originalWindingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(
  18. positions2D
  19. );
  20. if (originalWindingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  21. positions2D.reverse();
  22. positions = positions.slice().reverse();
  23. }
  24. let subdividedPositions;
  25. let i;
  26. let length = positions.length;
  27. let index = 0;
  28. if (!perPositionHeight) {
  29. let numVertices = 0;
  30. if (arcType === ArcType.ArcType.GEODESIC) {
  31. for (i = 0; i < length; i++) {
  32. numVertices += PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideLineCount(
  33. positions[i],
  34. positions[(i + 1) % length],
  35. minDistance
  36. );
  37. }
  38. } else if (arcType === ArcType.ArcType.RHUMB) {
  39. for (i = 0; i < length; i++) {
  40. numVertices += PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideRhumbLineCount(
  41. ellipsoid,
  42. positions[i],
  43. positions[(i + 1) % length],
  44. minDistance
  45. );
  46. }
  47. }
  48. subdividedPositions = new Float64Array(numVertices * 3);
  49. for (i = 0; i < length; i++) {
  50. let tempPositions;
  51. if (arcType === ArcType.ArcType.GEODESIC) {
  52. tempPositions = PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideLine(
  53. positions[i],
  54. positions[(i + 1) % length],
  55. minDistance,
  56. createGeometryFromPositionsSubdivided
  57. );
  58. } else if (arcType === ArcType.ArcType.RHUMB) {
  59. tempPositions = PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideRhumbLine(
  60. ellipsoid,
  61. positions[i],
  62. positions[(i + 1) % length],
  63. minDistance,
  64. createGeometryFromPositionsSubdivided
  65. );
  66. }
  67. const tempPositionsLength = tempPositions.length;
  68. for (let j = 0; j < tempPositionsLength; ++j) {
  69. subdividedPositions[index++] = tempPositions[j];
  70. }
  71. }
  72. } else {
  73. subdividedPositions = new Float64Array(length * 2 * 3);
  74. for (i = 0; i < length; i++) {
  75. const p0 = positions[i];
  76. const p1 = positions[(i + 1) % length];
  77. subdividedPositions[index++] = p0.x;
  78. subdividedPositions[index++] = p0.y;
  79. subdividedPositions[index++] = p0.z;
  80. subdividedPositions[index++] = p1.x;
  81. subdividedPositions[index++] = p1.y;
  82. subdividedPositions[index++] = p1.z;
  83. }
  84. }
  85. length = subdividedPositions.length / 3;
  86. const indicesSize = length * 2;
  87. const indices = IndexDatatype.IndexDatatype.createTypedArray(length, indicesSize);
  88. index = 0;
  89. for (i = 0; i < length - 1; i++) {
  90. indices[index++] = i;
  91. indices[index++] = i + 1;
  92. }
  93. indices[index++] = length - 1;
  94. indices[index++] = 0;
  95. return new GeometryInstance.GeometryInstance({
  96. geometry: new GeometryAttribute.Geometry({
  97. attributes: new GeometryAttributes.GeometryAttributes({
  98. position: new GeometryAttribute.GeometryAttribute({
  99. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  100. componentsPerAttribute: 3,
  101. values: subdividedPositions,
  102. }),
  103. }),
  104. indices: indices,
  105. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  106. }),
  107. });
  108. }
  109. function createGeometryFromPositionsExtruded(
  110. ellipsoid,
  111. positions,
  112. minDistance,
  113. perPositionHeight,
  114. arcType
  115. ) {
  116. const tangentPlane = EllipsoidTangentPlane.EllipsoidTangentPlane.fromPoints(positions, ellipsoid);
  117. const positions2D = tangentPlane.projectPointsOntoPlane(
  118. positions,
  119. createGeometryFromPositionsPositions
  120. );
  121. const originalWindingOrder = PolygonPipeline.PolygonPipeline.computeWindingOrder2D(
  122. positions2D
  123. );
  124. if (originalWindingOrder === PolygonPipeline.WindingOrder.CLOCKWISE) {
  125. positions2D.reverse();
  126. positions = positions.slice().reverse();
  127. }
  128. let subdividedPositions;
  129. let i;
  130. let length = positions.length;
  131. const corners = new Array(length);
  132. let index = 0;
  133. if (!perPositionHeight) {
  134. let numVertices = 0;
  135. if (arcType === ArcType.ArcType.GEODESIC) {
  136. for (i = 0; i < length; i++) {
  137. numVertices += PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideLineCount(
  138. positions[i],
  139. positions[(i + 1) % length],
  140. minDistance
  141. );
  142. }
  143. } else if (arcType === ArcType.ArcType.RHUMB) {
  144. for (i = 0; i < length; i++) {
  145. numVertices += PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideRhumbLineCount(
  146. ellipsoid,
  147. positions[i],
  148. positions[(i + 1) % length],
  149. minDistance
  150. );
  151. }
  152. }
  153. subdividedPositions = new Float64Array(numVertices * 3 * 2);
  154. for (i = 0; i < length; ++i) {
  155. corners[i] = index / 3;
  156. let tempPositions;
  157. if (arcType === ArcType.ArcType.GEODESIC) {
  158. tempPositions = PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideLine(
  159. positions[i],
  160. positions[(i + 1) % length],
  161. minDistance,
  162. createGeometryFromPositionsSubdivided
  163. );
  164. } else if (arcType === ArcType.ArcType.RHUMB) {
  165. tempPositions = PolygonGeometryLibrary.PolygonGeometryLibrary.subdivideRhumbLine(
  166. ellipsoid,
  167. positions[i],
  168. positions[(i + 1) % length],
  169. minDistance,
  170. createGeometryFromPositionsSubdivided
  171. );
  172. }
  173. const tempPositionsLength = tempPositions.length;
  174. for (let j = 0; j < tempPositionsLength; ++j) {
  175. subdividedPositions[index++] = tempPositions[j];
  176. }
  177. }
  178. } else {
  179. subdividedPositions = new Float64Array(length * 2 * 3 * 2);
  180. for (i = 0; i < length; ++i) {
  181. corners[i] = index / 3;
  182. const p0 = positions[i];
  183. const p1 = positions[(i + 1) % length];
  184. subdividedPositions[index++] = p0.x;
  185. subdividedPositions[index++] = p0.y;
  186. subdividedPositions[index++] = p0.z;
  187. subdividedPositions[index++] = p1.x;
  188. subdividedPositions[index++] = p1.y;
  189. subdividedPositions[index++] = p1.z;
  190. }
  191. }
  192. length = subdividedPositions.length / (3 * 2);
  193. const cornersLength = corners.length;
  194. const indicesSize = (length * 2 + cornersLength) * 2;
  195. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  196. length + cornersLength,
  197. indicesSize
  198. );
  199. index = 0;
  200. for (i = 0; i < length; ++i) {
  201. indices[index++] = i;
  202. indices[index++] = (i + 1) % length;
  203. indices[index++] = i + length;
  204. indices[index++] = ((i + 1) % length) + length;
  205. }
  206. for (i = 0; i < cornersLength; i++) {
  207. const corner = corners[i];
  208. indices[index++] = corner;
  209. indices[index++] = corner + length;
  210. }
  211. return new GeometryInstance.GeometryInstance({
  212. geometry: new GeometryAttribute.Geometry({
  213. attributes: new GeometryAttributes.GeometryAttributes({
  214. position: new GeometryAttribute.GeometryAttribute({
  215. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  216. componentsPerAttribute: 3,
  217. values: subdividedPositions,
  218. }),
  219. }),
  220. indices: indices,
  221. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  222. }),
  223. });
  224. }
  225. /**
  226. * A description of the outline of a polygon on the ellipsoid. The polygon is defined by a polygon hierarchy.
  227. *
  228. * @alias PolygonOutlineGeometry
  229. * @constructor
  230. *
  231. * @param {Object} options Object with the following properties:
  232. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  233. * @param {Number} [options.height=0.0] The distance in meters between the polygon and the ellipsoid surface.
  234. * @param {Number} [options.extrudedHeight] The distance in meters between the polygon's extruded face and the ellipsoid surface.
  235. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  236. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  237. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  238. * @param {Boolean} [options.perPositionHeight=false] Use the height of options.positions for each position instead of using options.height to determine the height.
  239. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of path the outline must follow. Valid options are {@link ArcType.GEODESIC} and {@link ArcType.RHUMB}.
  240. *
  241. * @see PolygonOutlineGeometry#createGeometry
  242. * @see PolygonOutlineGeometry#fromPositions
  243. *
  244. * @example
  245. * // 1. create a polygon outline from points
  246. * const polygon = new Cesium.PolygonOutlineGeometry({
  247. * polygonHierarchy : new Cesium.PolygonHierarchy(
  248. * Cesium.Cartesian3.fromDegreesArray([
  249. * -72.0, 40.0,
  250. * -70.0, 35.0,
  251. * -75.0, 30.0,
  252. * -70.0, 30.0,
  253. * -68.0, 40.0
  254. * ])
  255. * )
  256. * });
  257. * const geometry = Cesium.PolygonOutlineGeometry.createGeometry(polygon);
  258. *
  259. * // 2. create a nested polygon with holes outline
  260. * const polygonWithHole = new Cesium.PolygonOutlineGeometry({
  261. * polygonHierarchy : new Cesium.PolygonHierarchy(
  262. * Cesium.Cartesian3.fromDegreesArray([
  263. * -109.0, 30.0,
  264. * -95.0, 30.0,
  265. * -95.0, 40.0,
  266. * -109.0, 40.0
  267. * ]),
  268. * [new Cesium.PolygonHierarchy(
  269. * Cesium.Cartesian3.fromDegreesArray([
  270. * -107.0, 31.0,
  271. * -107.0, 39.0,
  272. * -97.0, 39.0,
  273. * -97.0, 31.0
  274. * ]),
  275. * [new Cesium.PolygonHierarchy(
  276. * Cesium.Cartesian3.fromDegreesArray([
  277. * -105.0, 33.0,
  278. * -99.0, 33.0,
  279. * -99.0, 37.0,
  280. * -105.0, 37.0
  281. * ]),
  282. * [new Cesium.PolygonHierarchy(
  283. * Cesium.Cartesian3.fromDegreesArray([
  284. * -103.0, 34.0,
  285. * -101.0, 34.0,
  286. * -101.0, 36.0,
  287. * -103.0, 36.0
  288. * ])
  289. * )]
  290. * )]
  291. * )]
  292. * )
  293. * });
  294. * const geometry = Cesium.PolygonOutlineGeometry.createGeometry(polygonWithHole);
  295. *
  296. * // 3. create extruded polygon outline
  297. * const extrudedPolygon = new Cesium.PolygonOutlineGeometry({
  298. * polygonHierarchy : new Cesium.PolygonHierarchy(
  299. * Cesium.Cartesian3.fromDegreesArray([
  300. * -72.0, 40.0,
  301. * -70.0, 35.0,
  302. * -75.0, 30.0,
  303. * -70.0, 30.0,
  304. * -68.0, 40.0
  305. * ])
  306. * ),
  307. * extrudedHeight: 300000
  308. * });
  309. * const geometry = Cesium.PolygonOutlineGeometry.createGeometry(extrudedPolygon);
  310. */
  311. function PolygonOutlineGeometry(options) {
  312. //>>includeStart('debug', pragmas.debug);
  313. RuntimeError.Check.typeOf.object("options", options);
  314. RuntimeError.Check.typeOf.object("options.polygonHierarchy", options.polygonHierarchy);
  315. if (options.perPositionHeight && defaultValue.defined(options.height)) {
  316. throw new RuntimeError.DeveloperError(
  317. "Cannot use both options.perPositionHeight and options.height"
  318. );
  319. }
  320. if (
  321. defaultValue.defined(options.arcType) &&
  322. options.arcType !== ArcType.ArcType.GEODESIC &&
  323. options.arcType !== ArcType.ArcType.RHUMB
  324. ) {
  325. throw new RuntimeError.DeveloperError(
  326. "Invalid arcType. Valid options are ArcType.GEODESIC and ArcType.RHUMB."
  327. );
  328. }
  329. //>>includeEnd('debug');
  330. const polygonHierarchy = options.polygonHierarchy;
  331. const ellipsoid = defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84);
  332. const granularity = defaultValue.defaultValue(
  333. options.granularity,
  334. ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE
  335. );
  336. const perPositionHeight = defaultValue.defaultValue(options.perPositionHeight, false);
  337. const perPositionHeightExtrude =
  338. perPositionHeight && defaultValue.defined(options.extrudedHeight);
  339. const arcType = defaultValue.defaultValue(options.arcType, ArcType.ArcType.GEODESIC);
  340. let height = defaultValue.defaultValue(options.height, 0.0);
  341. let extrudedHeight = defaultValue.defaultValue(options.extrudedHeight, height);
  342. if (!perPositionHeightExtrude) {
  343. const h = Math.max(height, extrudedHeight);
  344. extrudedHeight = Math.min(height, extrudedHeight);
  345. height = h;
  346. }
  347. this._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid);
  348. this._granularity = granularity;
  349. this._height = height;
  350. this._extrudedHeight = extrudedHeight;
  351. this._arcType = arcType;
  352. this._polygonHierarchy = polygonHierarchy;
  353. this._perPositionHeight = perPositionHeight;
  354. this._perPositionHeightExtrude = perPositionHeightExtrude;
  355. this._offsetAttribute = options.offsetAttribute;
  356. this._workerName = "createPolygonOutlineGeometry";
  357. /**
  358. * The number of elements used to pack the object into an array.
  359. * @type {Number}
  360. */
  361. this.packedLength =
  362. PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) +
  363. Matrix2.Ellipsoid.packedLength +
  364. 8;
  365. }
  366. /**
  367. * Stores the provided instance into the provided array.
  368. *
  369. * @param {PolygonOutlineGeometry} value The value to pack.
  370. * @param {Number[]} array The array to pack into.
  371. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  372. *
  373. * @returns {Number[]} The array that was packed into
  374. */
  375. PolygonOutlineGeometry.pack = function (value, array, startingIndex) {
  376. //>>includeStart('debug', pragmas.debug);
  377. RuntimeError.Check.typeOf.object("value", value);
  378. RuntimeError.Check.defined("array", array);
  379. //>>includeEnd('debug');
  380. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  381. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  382. value._polygonHierarchy,
  383. array,
  384. startingIndex
  385. );
  386. Matrix2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  387. startingIndex += Matrix2.Ellipsoid.packedLength;
  388. array[startingIndex++] = value._height;
  389. array[startingIndex++] = value._extrudedHeight;
  390. array[startingIndex++] = value._granularity;
  391. array[startingIndex++] = value._perPositionHeightExtrude ? 1.0 : 0.0;
  392. array[startingIndex++] = value._perPositionHeight ? 1.0 : 0.0;
  393. array[startingIndex++] = value._arcType;
  394. array[startingIndex++] = defaultValue.defaultValue(value._offsetAttribute, -1);
  395. array[startingIndex] = value.packedLength;
  396. return array;
  397. };
  398. const scratchEllipsoid = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  399. const dummyOptions = {
  400. polygonHierarchy: {},
  401. };
  402. /**
  403. * Retrieves an instance from a packed array.
  404. *
  405. * @param {Number[]} array The packed array.
  406. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  407. * @param {PolygonOutlineGeometry} [result] The object into which to store the result.
  408. * @returns {PolygonOutlineGeometry} The modified result parameter or a new PolygonOutlineGeometry instance if one was not provided.
  409. */
  410. PolygonOutlineGeometry.unpack = function (array, startingIndex, result) {
  411. //>>includeStart('debug', pragmas.debug);
  412. RuntimeError.Check.defined("array", array);
  413. //>>includeEnd('debug');
  414. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  415. const polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  416. array,
  417. startingIndex
  418. );
  419. startingIndex = polygonHierarchy.startingIndex;
  420. delete polygonHierarchy.startingIndex;
  421. const ellipsoid = Matrix2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  422. startingIndex += Matrix2.Ellipsoid.packedLength;
  423. const height = array[startingIndex++];
  424. const extrudedHeight = array[startingIndex++];
  425. const granularity = array[startingIndex++];
  426. const perPositionHeightExtrude = array[startingIndex++] === 1.0;
  427. const perPositionHeight = array[startingIndex++] === 1.0;
  428. const arcType = array[startingIndex++];
  429. const offsetAttribute = array[startingIndex++];
  430. const packedLength = array[startingIndex];
  431. if (!defaultValue.defined(result)) {
  432. result = new PolygonOutlineGeometry(dummyOptions);
  433. }
  434. result._polygonHierarchy = polygonHierarchy;
  435. result._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  436. result._height = height;
  437. result._extrudedHeight = extrudedHeight;
  438. result._granularity = granularity;
  439. result._perPositionHeight = perPositionHeight;
  440. result._perPositionHeightExtrude = perPositionHeightExtrude;
  441. result._arcType = arcType;
  442. result._offsetAttribute =
  443. offsetAttribute === -1 ? undefined : offsetAttribute;
  444. result.packedLength = packedLength;
  445. return result;
  446. };
  447. /**
  448. * A description of a polygon outline from an array of positions.
  449. *
  450. * @param {Object} options Object with the following properties:
  451. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  452. * @param {Number} [options.height=0.0] The height of the polygon.
  453. * @param {Number} [options.extrudedHeight] The height of the polygon extrusion.
  454. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  455. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  456. * @param {Boolean} [options.perPositionHeight=false] Use the height of options.positions for each position instead of using options.height to determine the height.
  457. * @param {ArcType} [options.arcType=ArcType.GEODESIC] The type of path the outline must follow. Valid options are {@link LinkType.GEODESIC} and {@link ArcType.RHUMB}.
  458. * @returns {PolygonOutlineGeometry}
  459. *
  460. *
  461. * @example
  462. * // create a polygon from points
  463. * const polygon = Cesium.PolygonOutlineGeometry.fromPositions({
  464. * positions : Cesium.Cartesian3.fromDegreesArray([
  465. * -72.0, 40.0,
  466. * -70.0, 35.0,
  467. * -75.0, 30.0,
  468. * -70.0, 30.0,
  469. * -68.0, 40.0
  470. * ])
  471. * });
  472. * const geometry = Cesium.PolygonOutlineGeometry.createGeometry(polygon);
  473. *
  474. * @see PolygonOutlineGeometry#createGeometry
  475. */
  476. PolygonOutlineGeometry.fromPositions = function (options) {
  477. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  478. //>>includeStart('debug', pragmas.debug);
  479. RuntimeError.Check.defined("options.positions", options.positions);
  480. //>>includeEnd('debug');
  481. const newOptions = {
  482. polygonHierarchy: {
  483. positions: options.positions,
  484. },
  485. height: options.height,
  486. extrudedHeight: options.extrudedHeight,
  487. ellipsoid: options.ellipsoid,
  488. granularity: options.granularity,
  489. perPositionHeight: options.perPositionHeight,
  490. arcType: options.arcType,
  491. offsetAttribute: options.offsetAttribute,
  492. };
  493. return new PolygonOutlineGeometry(newOptions);
  494. };
  495. /**
  496. * Computes the geometric representation of a polygon outline, including its vertices, indices, and a bounding sphere.
  497. *
  498. * @param {PolygonOutlineGeometry} polygonGeometry A description of the polygon outline.
  499. * @returns {Geometry|undefined} The computed vertices and indices.
  500. */
  501. PolygonOutlineGeometry.createGeometry = function (polygonGeometry) {
  502. const ellipsoid = polygonGeometry._ellipsoid;
  503. const granularity = polygonGeometry._granularity;
  504. const polygonHierarchy = polygonGeometry._polygonHierarchy;
  505. const perPositionHeight = polygonGeometry._perPositionHeight;
  506. const arcType = polygonGeometry._arcType;
  507. const polygons = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonOutlinesFromHierarchy(
  508. polygonHierarchy,
  509. !perPositionHeight,
  510. ellipsoid
  511. );
  512. if (polygons.length === 0) {
  513. return undefined;
  514. }
  515. let geometryInstance;
  516. const geometries = [];
  517. const minDistance = ComponentDatatype.CesiumMath.chordLength(
  518. granularity,
  519. ellipsoid.maximumRadius
  520. );
  521. const height = polygonGeometry._height;
  522. const extrudedHeight = polygonGeometry._extrudedHeight;
  523. const extrude =
  524. polygonGeometry._perPositionHeightExtrude ||
  525. !ComponentDatatype.CesiumMath.equalsEpsilon(height, extrudedHeight, 0, ComponentDatatype.CesiumMath.EPSILON2);
  526. let offsetValue;
  527. let i;
  528. if (extrude) {
  529. for (i = 0; i < polygons.length; i++) {
  530. geometryInstance = createGeometryFromPositionsExtruded(
  531. ellipsoid,
  532. polygons[i],
  533. minDistance,
  534. perPositionHeight,
  535. arcType
  536. );
  537. geometryInstance.geometry = PolygonGeometryLibrary.PolygonGeometryLibrary.scaleToGeodeticHeightExtruded(
  538. geometryInstance.geometry,
  539. height,
  540. extrudedHeight,
  541. ellipsoid,
  542. perPositionHeight
  543. );
  544. if (defaultValue.defined(polygonGeometry._offsetAttribute)) {
  545. const size =
  546. geometryInstance.geometry.attributes.position.values.length / 3;
  547. let offsetAttribute = new Uint8Array(size);
  548. if (polygonGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  549. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, 1, 0, size / 2);
  550. } else {
  551. offsetValue =
  552. polygonGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  553. ? 0
  554. : 1;
  555. offsetAttribute = GeometryOffsetAttribute.arrayFill(offsetAttribute, offsetValue);
  556. }
  557. geometryInstance.geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute(
  558. {
  559. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  560. componentsPerAttribute: 1,
  561. values: offsetAttribute,
  562. }
  563. );
  564. }
  565. geometries.push(geometryInstance);
  566. }
  567. } else {
  568. for (i = 0; i < polygons.length; i++) {
  569. geometryInstance = createGeometryFromPositions(
  570. ellipsoid,
  571. polygons[i],
  572. minDistance,
  573. perPositionHeight,
  574. arcType
  575. );
  576. geometryInstance.geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  577. geometryInstance.geometry.attributes.position.values,
  578. height,
  579. ellipsoid,
  580. !perPositionHeight
  581. );
  582. if (defaultValue.defined(polygonGeometry._offsetAttribute)) {
  583. const length =
  584. geometryInstance.geometry.attributes.position.values.length;
  585. const applyOffset = new Uint8Array(length / 3);
  586. offsetValue =
  587. polygonGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  588. ? 0
  589. : 1;
  590. GeometryOffsetAttribute.arrayFill(applyOffset, offsetValue);
  591. geometryInstance.geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute(
  592. {
  593. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  594. componentsPerAttribute: 1,
  595. values: applyOffset,
  596. }
  597. );
  598. }
  599. geometries.push(geometryInstance);
  600. }
  601. }
  602. const geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  603. const boundingSphere = Transforms.BoundingSphere.fromVertices(
  604. geometry.attributes.position.values
  605. );
  606. return new GeometryAttribute.Geometry({
  607. attributes: geometry.attributes,
  608. indices: geometry.indices,
  609. primitiveType: geometry.primitiveType,
  610. boundingSphere: boundingSphere,
  611. offsetAttribute: polygonGeometry._offsetAttribute,
  612. });
  613. };
  614. function createPolygonOutlineGeometry(polygonGeometry, offset) {
  615. if (defaultValue.defined(offset)) {
  616. polygonGeometry = PolygonOutlineGeometry.unpack(polygonGeometry, offset);
  617. }
  618. polygonGeometry._ellipsoid = Matrix2.Ellipsoid.clone(polygonGeometry._ellipsoid);
  619. return PolygonOutlineGeometry.createGeometry(polygonGeometry);
  620. }
  621. return createPolygonOutlineGeometry;
  622. }));