createCoplanarPolygonOutlineGeometry.js 8.9 KB

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