CoplanarPolygonOutlineGeometry.js 7.2 KB

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