createCoplanarPolygonOutlineGeometry.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['./arrayRemoveDuplicates-63722a6f', './Transforms-c78c4637', './Matrix2-ab676047', './RuntimeError-1088cc64', './ComponentDatatype-e06f4e16', './CoplanarPolygonGeometryLibrary-a1408391', './defaultValue-a6eb9f34', './GeometryAttribute-4f02e2ad', './GeometryAttributes-aff51037', './GeometryInstance-6f541616', './GeometryPipeline-5a61c463', './IndexDatatype-c2232ebd', './PolygonGeometryLibrary-5dd88675', './_commonjsHelpers-89c9b271', './combine-7cf28d88', './WebGLConstants-d81b330d', './OrientedBoundingBox-28f77441', './EllipsoidTangentPlane-6691e012', './AxisAlignedBoundingBox-51d5a498', './IntersectionTests-f96cd46d', './Plane-c985a1d2', './AttributeCompression-8033f934', './EncodedCartesian3-7959a913', './ArcType-b714639b', './EllipsoidRhumbLine-34574f75', './PolygonPipeline-dd4a5392'], (function (arrayRemoveDuplicates, Transforms, Matrix2, RuntimeError, ComponentDatatype, CoplanarPolygonGeometryLibrary, defaultValue, GeometryAttribute, GeometryAttributes, GeometryInstance, GeometryPipeline, IndexDatatype, PolygonGeometryLibrary, _commonjsHelpers, combine, WebGLConstants, OrientedBoundingBox, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, AttributeCompression, EncodedCartesian3, ArcType, EllipsoidRhumbLine, PolygonPipeline) { 'use strict';
  26. function createGeometryFromPositions(positions) {
  27. const length = positions.length;
  28. const flatPositions = new Float64Array(length * 3);
  29. const indices = IndexDatatype.IndexDatatype.createTypedArray(length, length * 2);
  30. let positionIndex = 0;
  31. let index = 0;
  32. for (let i = 0; i < length; i++) {
  33. const position = positions[i];
  34. flatPositions[positionIndex++] = position.x;
  35. flatPositions[positionIndex++] = position.y;
  36. flatPositions[positionIndex++] = position.z;
  37. indices[index++] = i;
  38. indices[index++] = (i + 1) % length;
  39. }
  40. const attributes = new GeometryAttributes.GeometryAttributes({
  41. position: new GeometryAttribute.GeometryAttribute({
  42. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  43. componentsPerAttribute: 3,
  44. values: flatPositions,
  45. }),
  46. });
  47. return new GeometryAttribute.Geometry({
  48. attributes: attributes,
  49. indices: indices,
  50. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  51. });
  52. }
  53. /**
  54. * A description of the outline of a polygon composed of arbitrary coplanar positions.
  55. *
  56. * @alias CoplanarPolygonOutlineGeometry
  57. * @constructor
  58. *
  59. * @param {Object} options Object with the following properties:
  60. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  61. *
  62. * @see CoplanarPolygonOutlineGeometry.createGeometry
  63. *
  64. * @example
  65. * const polygonOutline = new Cesium.CoplanarPolygonOutlineGeometry({
  66. * positions : Cesium.Cartesian3.fromDegreesArrayHeights([
  67. * -90.0, 30.0, 0.0,
  68. * -90.0, 30.0, 1000.0,
  69. * -80.0, 30.0, 1000.0,
  70. * -80.0, 30.0, 0.0
  71. * ])
  72. * });
  73. * const geometry = Cesium.CoplanarPolygonOutlineGeometry.createGeometry(polygonOutline);
  74. */
  75. function CoplanarPolygonOutlineGeometry(options) {
  76. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  77. const polygonHierarchy = options.polygonHierarchy;
  78. //>>includeStart('debug', pragmas.debug);
  79. RuntimeError.Check.defined("options.polygonHierarchy", polygonHierarchy);
  80. //>>includeEnd('debug');
  81. this._polygonHierarchy = polygonHierarchy;
  82. this._workerName = "createCoplanarPolygonOutlineGeometry";
  83. /**
  84. * The number of elements used to pack the object into an array.
  85. * @type {Number}
  86. */
  87. this.packedLength =
  88. PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(
  89. polygonHierarchy,
  90. Matrix2.Cartesian3
  91. ) + 1;
  92. }
  93. /**
  94. * A description of a coplanar polygon outline from an array of positions.
  95. *
  96. * @param {Object} options Object with the following properties:
  97. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  98. * @returns {CoplanarPolygonOutlineGeometry}
  99. */
  100. CoplanarPolygonOutlineGeometry.fromPositions = function (options) {
  101. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  102. //>>includeStart('debug', pragmas.debug);
  103. RuntimeError.Check.defined("options.positions", options.positions);
  104. //>>includeEnd('debug');
  105. const newOptions = {
  106. polygonHierarchy: {
  107. positions: options.positions,
  108. },
  109. };
  110. return new CoplanarPolygonOutlineGeometry(newOptions);
  111. };
  112. /**
  113. * Stores the provided instance into the provided array.
  114. *
  115. * @param {CoplanarPolygonOutlineGeometry} value The value to pack.
  116. * @param {Number[]} array The array to pack into.
  117. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  118. *
  119. * @returns {Number[]} The array that was packed into
  120. */
  121. CoplanarPolygonOutlineGeometry.pack = function (value, array, startingIndex) {
  122. //>>includeStart('debug', pragmas.debug);
  123. RuntimeError.Check.typeOf.object("value", value);
  124. RuntimeError.Check.defined("array", array);
  125. //>>includeEnd('debug');
  126. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  127. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  128. value._polygonHierarchy,
  129. array,
  130. startingIndex,
  131. Matrix2.Cartesian3
  132. );
  133. array[startingIndex] = value.packedLength;
  134. return array;
  135. };
  136. const scratchOptions = {
  137. polygonHierarchy: {},
  138. };
  139. /**
  140. * Retrieves an instance from a packed array.
  141. *
  142. * @param {Number[]} array The packed array.
  143. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  144. * @param {CoplanarPolygonOutlineGeometry} [result] The object into which to store the result.
  145. * @returns {CoplanarPolygonOutlineGeometry} The modified result parameter or a new CoplanarPolygonOutlineGeometry instance if one was not provided.
  146. */
  147. CoplanarPolygonOutlineGeometry.unpack = function (
  148. array,
  149. startingIndex,
  150. result
  151. ) {
  152. //>>includeStart('debug', pragmas.debug);
  153. RuntimeError.Check.defined("array", array);
  154. //>>includeEnd('debug');
  155. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  156. const polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  157. array,
  158. startingIndex,
  159. Matrix2.Cartesian3
  160. );
  161. startingIndex = polygonHierarchy.startingIndex;
  162. delete polygonHierarchy.startingIndex;
  163. const packedLength = array[startingIndex];
  164. if (!defaultValue.defined(result)) {
  165. result = new CoplanarPolygonOutlineGeometry(scratchOptions);
  166. }
  167. result._polygonHierarchy = polygonHierarchy;
  168. result.packedLength = packedLength;
  169. return result;
  170. };
  171. /**
  172. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  173. *
  174. * @param {CoplanarPolygonOutlineGeometry} polygonGeometry A description of the polygon.
  175. * @returns {Geometry|undefined} The computed vertices and indices.
  176. */
  177. CoplanarPolygonOutlineGeometry.createGeometry = function (polygonGeometry) {
  178. const polygonHierarchy = polygonGeometry._polygonHierarchy;
  179. let outerPositions = polygonHierarchy.positions;
  180. outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  181. outerPositions,
  182. Matrix2.Cartesian3.equalsEpsilon,
  183. true
  184. );
  185. if (outerPositions.length < 3) {
  186. return;
  187. }
  188. const isValid = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.validOutline(outerPositions);
  189. if (!isValid) {
  190. return undefined;
  191. }
  192. const polygons = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonOutlinesFromHierarchy(
  193. polygonHierarchy,
  194. false
  195. );
  196. if (polygons.length === 0) {
  197. return undefined;
  198. }
  199. const geometries = [];
  200. for (let i = 0; i < polygons.length; i++) {
  201. const geometryInstance = new GeometryInstance.GeometryInstance({
  202. geometry: createGeometryFromPositions(polygons[i]),
  203. });
  204. geometries.push(geometryInstance);
  205. }
  206. const geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  207. const boundingSphere = Transforms.BoundingSphere.fromPoints(polygonHierarchy.positions);
  208. return new GeometryAttribute.Geometry({
  209. attributes: geometry.attributes,
  210. indices: geometry.indices,
  211. primitiveType: geometry.primitiveType,
  212. boundingSphere: boundingSphere,
  213. });
  214. };
  215. function createCoplanarPolygonOutlineGeometry(polygonGeometry, offset) {
  216. if (defaultValue.defined(offset)) {
  217. polygonGeometry = CoplanarPolygonOutlineGeometry.unpack(
  218. polygonGeometry,
  219. offset
  220. );
  221. }
  222. polygonGeometry._ellipsoid = Matrix2.Ellipsoid.clone(polygonGeometry._ellipsoid);
  223. return CoplanarPolygonOutlineGeometry.createGeometry(polygonGeometry);
  224. }
  225. return createCoplanarPolygonOutlineGeometry;
  226. }));