createCoplanarPolygonOutlineGeometry.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./arrayRemoveDuplicates-87160c89', './Transforms-323408fe', './Matrix2-69c32d33', './RuntimeError-c581ca93', './ComponentDatatype-b1ea011a', './CoplanarPolygonGeometryLibrary-757a06dc', './defaultValue-94c3e563', './GeometryAttribute-cb73bb3f', './GeometryAttributes-7df9bef6', './GeometryInstance-f69fd420', './GeometryPipeline-e27e35f8', './IndexDatatype-c4099fe9', './PolygonGeometryLibrary-515b324f', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96', './OrientedBoundingBox-a5e39a7d', './EllipsoidTangentPlane-1e8d1fc2', './AxisAlignedBoundingBox-df2331b2', './IntersectionTests-d5d945ac', './Plane-069b6800', './AttributeCompression-3cfab808', './EncodedCartesian3-b1f97f8a', './ArcType-0cf52f8c', './EllipsoidRhumbLine-5cb6da82', './PolygonPipeline-d1884135'], (function (arrayRemoveDuplicates, Transforms, Matrix2, RuntimeError, ComponentDatatype, CoplanarPolygonGeometryLibrary, defaultValue, GeometryAttribute, GeometryAttributes, GeometryInstance, GeometryPipeline, IndexDatatype, PolygonGeometryLibrary, _commonjsHelpers3aae1032, combine, WebGLConstants, OrientedBoundingBox, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, AttributeCompression, EncodedCartesian3, ArcType, EllipsoidRhumbLine, PolygonPipeline) { 'use strict';
  3. function createGeometryFromPositions(positions) {
  4. const length = positions.length;
  5. const flatPositions = new Float64Array(length * 3);
  6. const indices = IndexDatatype.IndexDatatype.createTypedArray(length, length * 2);
  7. let positionIndex = 0;
  8. let index = 0;
  9. for (let i = 0; i < length; i++) {
  10. const position = positions[i];
  11. flatPositions[positionIndex++] = position.x;
  12. flatPositions[positionIndex++] = position.y;
  13. flatPositions[positionIndex++] = position.z;
  14. indices[index++] = i;
  15. indices[index++] = (i + 1) % length;
  16. }
  17. const attributes = new GeometryAttributes.GeometryAttributes({
  18. position: new GeometryAttribute.GeometryAttribute({
  19. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  20. componentsPerAttribute: 3,
  21. values: flatPositions,
  22. }),
  23. });
  24. return new GeometryAttribute.Geometry({
  25. attributes: attributes,
  26. indices: indices,
  27. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  28. });
  29. }
  30. /**
  31. * A description of the outline of a polygon composed of arbitrary coplanar positions.
  32. *
  33. * @alias CoplanarPolygonOutlineGeometry
  34. * @constructor
  35. *
  36. * @param {Object} options Object with the following properties:
  37. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  38. *
  39. * @see CoplanarPolygonOutlineGeometry.createGeometry
  40. *
  41. * @example
  42. * const polygonOutline = new Cesium.CoplanarPolygonOutlineGeometry({
  43. * positions : Cesium.Cartesian3.fromDegreesArrayHeights([
  44. * -90.0, 30.0, 0.0,
  45. * -90.0, 30.0, 1000.0,
  46. * -80.0, 30.0, 1000.0,
  47. * -80.0, 30.0, 0.0
  48. * ])
  49. * });
  50. * const geometry = Cesium.CoplanarPolygonOutlineGeometry.createGeometry(polygonOutline);
  51. */
  52. function CoplanarPolygonOutlineGeometry(options) {
  53. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  54. const polygonHierarchy = options.polygonHierarchy;
  55. //>>includeStart('debug', pragmas.debug);
  56. RuntimeError.Check.defined("options.polygonHierarchy", polygonHierarchy);
  57. //>>includeEnd('debug');
  58. this._polygonHierarchy = polygonHierarchy;
  59. this._workerName = "createCoplanarPolygonOutlineGeometry";
  60. /**
  61. * The number of elements used to pack the object into an array.
  62. * @type {Number}
  63. */
  64. this.packedLength =
  65. PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + 1;
  66. }
  67. /**
  68. * A description of a coplanar polygon outline from an array of positions.
  69. *
  70. * @param {Object} options Object with the following properties:
  71. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  72. * @returns {CoplanarPolygonOutlineGeometry}
  73. */
  74. CoplanarPolygonOutlineGeometry.fromPositions = function (options) {
  75. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  76. //>>includeStart('debug', pragmas.debug);
  77. RuntimeError.Check.defined("options.positions", options.positions);
  78. //>>includeEnd('debug');
  79. const newOptions = {
  80. polygonHierarchy: {
  81. positions: options.positions,
  82. },
  83. };
  84. return new CoplanarPolygonOutlineGeometry(newOptions);
  85. };
  86. /**
  87. * Stores the provided instance into the provided array.
  88. *
  89. * @param {CoplanarPolygonOutlineGeometry} value The value to pack.
  90. * @param {Number[]} array The array to pack into.
  91. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  92. *
  93. * @returns {Number[]} The array that was packed into
  94. */
  95. CoplanarPolygonOutlineGeometry.pack = function (value, array, startingIndex) {
  96. //>>includeStart('debug', pragmas.debug);
  97. RuntimeError.Check.typeOf.object("value", value);
  98. RuntimeError.Check.defined("array", array);
  99. //>>includeEnd('debug');
  100. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  101. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  102. value._polygonHierarchy,
  103. array,
  104. startingIndex
  105. );
  106. array[startingIndex] = value.packedLength;
  107. return array;
  108. };
  109. const scratchOptions = {
  110. polygonHierarchy: {},
  111. };
  112. /**
  113. * Retrieves an instance from a packed array.
  114. *
  115. * @param {Number[]} array The packed array.
  116. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  117. * @param {CoplanarPolygonOutlineGeometry} [result] The object into which to store the result.
  118. * @returns {CoplanarPolygonOutlineGeometry} The modified result parameter or a new CoplanarPolygonOutlineGeometry instance if one was not provided.
  119. */
  120. CoplanarPolygonOutlineGeometry.unpack = function (
  121. array,
  122. startingIndex,
  123. result
  124. ) {
  125. //>>includeStart('debug', pragmas.debug);
  126. RuntimeError.Check.defined("array", array);
  127. //>>includeEnd('debug');
  128. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  129. const polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  130. array,
  131. startingIndex
  132. );
  133. startingIndex = polygonHierarchy.startingIndex;
  134. delete polygonHierarchy.startingIndex;
  135. const packedLength = array[startingIndex];
  136. if (!defaultValue.defined(result)) {
  137. result = new CoplanarPolygonOutlineGeometry(scratchOptions);
  138. }
  139. result._polygonHierarchy = polygonHierarchy;
  140. result.packedLength = packedLength;
  141. return result;
  142. };
  143. /**
  144. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  145. *
  146. * @param {CoplanarPolygonOutlineGeometry} polygonGeometry A description of the polygon.
  147. * @returns {Geometry|undefined} The computed vertices and indices.
  148. */
  149. CoplanarPolygonOutlineGeometry.createGeometry = function (polygonGeometry) {
  150. const polygonHierarchy = polygonGeometry._polygonHierarchy;
  151. let outerPositions = polygonHierarchy.positions;
  152. outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  153. outerPositions,
  154. Matrix2.Cartesian3.equalsEpsilon,
  155. true
  156. );
  157. if (outerPositions.length < 3) {
  158. return;
  159. }
  160. const isValid = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.validOutline(outerPositions);
  161. if (!isValid) {
  162. return undefined;
  163. }
  164. const polygons = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonOutlinesFromHierarchy(
  165. polygonHierarchy,
  166. false
  167. );
  168. if (polygons.length === 0) {
  169. return undefined;
  170. }
  171. const geometries = [];
  172. for (let i = 0; i < polygons.length; i++) {
  173. const geometryInstance = new GeometryInstance.GeometryInstance({
  174. geometry: createGeometryFromPositions(polygons[i]),
  175. });
  176. geometries.push(geometryInstance);
  177. }
  178. const geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  179. const boundingSphere = Transforms.BoundingSphere.fromPoints(polygonHierarchy.positions);
  180. return new GeometryAttribute.Geometry({
  181. attributes: geometry.attributes,
  182. indices: geometry.indices,
  183. primitiveType: geometry.primitiveType,
  184. boundingSphere: boundingSphere,
  185. });
  186. };
  187. function createCoplanarPolygonOutlineGeometry(polygonGeometry, offset) {
  188. if (defaultValue.defined(offset)) {
  189. polygonGeometry = CoplanarPolygonOutlineGeometry.unpack(
  190. polygonGeometry,
  191. offset
  192. );
  193. }
  194. polygonGeometry._ellipsoid = Matrix2.Ellipsoid.clone(polygonGeometry._ellipsoid);
  195. return CoplanarPolygonOutlineGeometry.createGeometry(polygonGeometry);
  196. }
  197. return createCoplanarPolygonOutlineGeometry;
  198. }));