createCoplanarPolygonOutlineGeometry.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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(['./arrayRemoveDuplicates-1a15bd09', './Transforms-f0a54c7b', './Matrix2-d35cf4b5', './RuntimeError-8952249c', './ComponentDatatype-9e86ac8f', './CoplanarPolygonGeometryLibrary-73c52789', './defaultValue-81eec7ed', './GeometryAttribute-eeb38987', './GeometryAttributes-32b29525', './GeometryInstance-d60d0ef4', './GeometryPipeline-55e02a41', './IndexDatatype-bed3935d', './PolygonGeometryLibrary-6e68e6b6', './_commonjsHelpers-3aae1032-26891ab7', './combine-3c023bda', './WebGLConstants-508b9636', './OrientedBoundingBox-0b41570b', './EllipsoidTangentPlane-2abe082d', './AxisAlignedBoundingBox-7b93960a', './IntersectionTests-a25e058d', './Plane-24f22488', './AttributeCompression-d0b97a83', './EncodedCartesian3-530d5328', './ArcType-fc72c06c', './EllipsoidRhumbLine-d049f903', './PolygonPipeline-a3c0d57c'], (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';
  24. function createGeometryFromPositions(positions) {
  25. const length = positions.length;
  26. const flatPositions = new Float64Array(length * 3);
  27. const indices = IndexDatatype.IndexDatatype.createTypedArray(length, length * 2);
  28. let positionIndex = 0;
  29. let index = 0;
  30. for (let i = 0; i < length; i++) {
  31. const position = positions[i];
  32. flatPositions[positionIndex++] = position.x;
  33. flatPositions[positionIndex++] = position.y;
  34. flatPositions[positionIndex++] = position.z;
  35. indices[index++] = i;
  36. indices[index++] = (i + 1) % length;
  37. }
  38. const attributes = new GeometryAttributes.GeometryAttributes({
  39. position: new GeometryAttribute.GeometryAttribute({
  40. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  41. componentsPerAttribute: 3,
  42. values: flatPositions,
  43. }),
  44. });
  45. return new GeometryAttribute.Geometry({
  46. attributes: attributes,
  47. indices: indices,
  48. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  49. });
  50. }
  51. /**
  52. * A description of the outline of a polygon composed of arbitrary coplanar positions.
  53. *
  54. * @alias CoplanarPolygonOutlineGeometry
  55. * @constructor
  56. *
  57. * @param {Object} options Object with the following properties:
  58. * @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
  59. *
  60. * @see CoplanarPolygonOutlineGeometry.createGeometry
  61. *
  62. * @example
  63. * const polygonOutline = new Cesium.CoplanarPolygonOutlineGeometry({
  64. * positions : Cesium.Cartesian3.fromDegreesArrayHeights([
  65. * -90.0, 30.0, 0.0,
  66. * -90.0, 30.0, 1000.0,
  67. * -80.0, 30.0, 1000.0,
  68. * -80.0, 30.0, 0.0
  69. * ])
  70. * });
  71. * const geometry = Cesium.CoplanarPolygonOutlineGeometry.createGeometry(polygonOutline);
  72. */
  73. function CoplanarPolygonOutlineGeometry(options) {
  74. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  75. const polygonHierarchy = options.polygonHierarchy;
  76. //>>includeStart('debug', pragmas.debug);
  77. RuntimeError.Check.defined("options.polygonHierarchy", polygonHierarchy);
  78. //>>includeEnd('debug');
  79. this._polygonHierarchy = polygonHierarchy;
  80. this._workerName = "createCoplanarPolygonOutlineGeometry";
  81. /**
  82. * The number of elements used to pack the object into an array.
  83. * @type {Number}
  84. */
  85. this.packedLength =
  86. PolygonGeometryLibrary.PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + 1;
  87. }
  88. /**
  89. * A description of a coplanar polygon outline from an array of positions.
  90. *
  91. * @param {Object} options Object with the following properties:
  92. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  93. * @returns {CoplanarPolygonOutlineGeometry}
  94. */
  95. CoplanarPolygonOutlineGeometry.fromPositions = function (options) {
  96. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  97. //>>includeStart('debug', pragmas.debug);
  98. RuntimeError.Check.defined("options.positions", options.positions);
  99. //>>includeEnd('debug');
  100. const newOptions = {
  101. polygonHierarchy: {
  102. positions: options.positions,
  103. },
  104. };
  105. return new CoplanarPolygonOutlineGeometry(newOptions);
  106. };
  107. /**
  108. * Stores the provided instance into the provided array.
  109. *
  110. * @param {CoplanarPolygonOutlineGeometry} value The value to pack.
  111. * @param {Number[]} array The array to pack into.
  112. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  113. *
  114. * @returns {Number[]} The array that was packed into
  115. */
  116. CoplanarPolygonOutlineGeometry.pack = function (value, array, startingIndex) {
  117. //>>includeStart('debug', pragmas.debug);
  118. RuntimeError.Check.typeOf.object("value", value);
  119. RuntimeError.Check.defined("array", array);
  120. //>>includeEnd('debug');
  121. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  122. startingIndex = PolygonGeometryLibrary.PolygonGeometryLibrary.packPolygonHierarchy(
  123. value._polygonHierarchy,
  124. array,
  125. startingIndex
  126. );
  127. array[startingIndex] = value.packedLength;
  128. return array;
  129. };
  130. const scratchOptions = {
  131. polygonHierarchy: {},
  132. };
  133. /**
  134. * Retrieves an instance from a packed array.
  135. *
  136. * @param {Number[]} array The packed array.
  137. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  138. * @param {CoplanarPolygonOutlineGeometry} [result] The object into which to store the result.
  139. * @returns {CoplanarPolygonOutlineGeometry} The modified result parameter or a new CoplanarPolygonOutlineGeometry instance if one was not provided.
  140. */
  141. CoplanarPolygonOutlineGeometry.unpack = function (
  142. array,
  143. startingIndex,
  144. result
  145. ) {
  146. //>>includeStart('debug', pragmas.debug);
  147. RuntimeError.Check.defined("array", array);
  148. //>>includeEnd('debug');
  149. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  150. const polygonHierarchy = PolygonGeometryLibrary.PolygonGeometryLibrary.unpackPolygonHierarchy(
  151. array,
  152. startingIndex
  153. );
  154. startingIndex = polygonHierarchy.startingIndex;
  155. delete polygonHierarchy.startingIndex;
  156. const packedLength = array[startingIndex];
  157. if (!defaultValue.defined(result)) {
  158. result = new CoplanarPolygonOutlineGeometry(scratchOptions);
  159. }
  160. result._polygonHierarchy = polygonHierarchy;
  161. result.packedLength = packedLength;
  162. return result;
  163. };
  164. /**
  165. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  166. *
  167. * @param {CoplanarPolygonOutlineGeometry} polygonGeometry A description of the polygon.
  168. * @returns {Geometry|undefined} The computed vertices and indices.
  169. */
  170. CoplanarPolygonOutlineGeometry.createGeometry = function (polygonGeometry) {
  171. const polygonHierarchy = polygonGeometry._polygonHierarchy;
  172. let outerPositions = polygonHierarchy.positions;
  173. outerPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  174. outerPositions,
  175. Matrix2.Cartesian3.equalsEpsilon,
  176. true
  177. );
  178. if (outerPositions.length < 3) {
  179. return;
  180. }
  181. const isValid = CoplanarPolygonGeometryLibrary.CoplanarPolygonGeometryLibrary.validOutline(outerPositions);
  182. if (!isValid) {
  183. return undefined;
  184. }
  185. const polygons = PolygonGeometryLibrary.PolygonGeometryLibrary.polygonOutlinesFromHierarchy(
  186. polygonHierarchy,
  187. false
  188. );
  189. if (polygons.length === 0) {
  190. return undefined;
  191. }
  192. const geometries = [];
  193. for (let i = 0; i < polygons.length; i++) {
  194. const geometryInstance = new GeometryInstance.GeometryInstance({
  195. geometry: createGeometryFromPositions(polygons[i]),
  196. });
  197. geometries.push(geometryInstance);
  198. }
  199. const geometry = GeometryPipeline.GeometryPipeline.combineInstances(geometries)[0];
  200. const boundingSphere = Transforms.BoundingSphere.fromPoints(polygonHierarchy.positions);
  201. return new GeometryAttribute.Geometry({
  202. attributes: geometry.attributes,
  203. indices: geometry.indices,
  204. primitiveType: geometry.primitiveType,
  205. boundingSphere: boundingSphere,
  206. });
  207. };
  208. function createCoplanarPolygonOutlineGeometry(polygonGeometry, offset) {
  209. if (defaultValue.defined(offset)) {
  210. polygonGeometry = CoplanarPolygonOutlineGeometry.unpack(
  211. polygonGeometry,
  212. offset
  213. );
  214. }
  215. polygonGeometry._ellipsoid = Matrix2.Ellipsoid.clone(polygonGeometry._ellipsoid);
  216. return CoplanarPolygonOutlineGeometry.createGeometry(polygonGeometry);
  217. }
  218. return createCoplanarPolygonOutlineGeometry;
  219. }));
  220. //# sourceMappingURL=createCoplanarPolygonOutlineGeometry.js.map