createPolygonOutlineGeometry.js 27 KB

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