CoplanarPolygonOutlineGeometry.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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(
  80. polygonHierarchy,
  81. Cartesian3
  82. ) + 1;
  83. }
  84. /**
  85. * A description of a coplanar polygon outline from an array of positions.
  86. *
  87. * @param {object} options Object with the following properties:
  88. * @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
  89. * @returns {CoplanarPolygonOutlineGeometry}
  90. */
  91. CoplanarPolygonOutlineGeometry.fromPositions = function (options) {
  92. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  93. //>>includeStart('debug', pragmas.debug);
  94. Check.defined("options.positions", options.positions);
  95. //>>includeEnd('debug');
  96. const newOptions = {
  97. polygonHierarchy: {
  98. positions: options.positions,
  99. },
  100. };
  101. return new CoplanarPolygonOutlineGeometry(newOptions);
  102. };
  103. /**
  104. * Stores the provided instance into the provided array.
  105. *
  106. * @param {CoplanarPolygonOutlineGeometry} value The value to pack.
  107. * @param {number[]} array The array to pack into.
  108. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  109. *
  110. * @returns {number[]} The array that was packed into
  111. */
  112. CoplanarPolygonOutlineGeometry.pack = function (value, array, startingIndex) {
  113. //>>includeStart('debug', pragmas.debug);
  114. Check.typeOf.object("value", value);
  115. Check.defined("array", array);
  116. //>>includeEnd('debug');
  117. startingIndex = defaultValue(startingIndex, 0);
  118. startingIndex = PolygonGeometryLibrary.packPolygonHierarchy(
  119. value._polygonHierarchy,
  120. array,
  121. startingIndex,
  122. Cartesian3
  123. );
  124. array[startingIndex] = value.packedLength;
  125. return array;
  126. };
  127. const scratchOptions = {
  128. polygonHierarchy: {},
  129. };
  130. /**
  131. * Retrieves an instance from a packed array.
  132. *
  133. * @param {number[]} array The packed array.
  134. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  135. * @param {CoplanarPolygonOutlineGeometry} [result] The object into which to store the result.
  136. * @returns {CoplanarPolygonOutlineGeometry} The modified result parameter or a new CoplanarPolygonOutlineGeometry instance if one was not provided.
  137. */
  138. CoplanarPolygonOutlineGeometry.unpack = function (
  139. array,
  140. startingIndex,
  141. result
  142. ) {
  143. //>>includeStart('debug', pragmas.debug);
  144. Check.defined("array", array);
  145. //>>includeEnd('debug');
  146. startingIndex = defaultValue(startingIndex, 0);
  147. const polygonHierarchy = PolygonGeometryLibrary.unpackPolygonHierarchy(
  148. array,
  149. startingIndex,
  150. Cartesian3
  151. );
  152. startingIndex = polygonHierarchy.startingIndex;
  153. delete polygonHierarchy.startingIndex;
  154. const packedLength = array[startingIndex];
  155. if (!defined(result)) {
  156. result = new CoplanarPolygonOutlineGeometry(scratchOptions);
  157. }
  158. result._polygonHierarchy = polygonHierarchy;
  159. result.packedLength = packedLength;
  160. return result;
  161. };
  162. /**
  163. * Computes the geometric representation of an arbitrary coplanar polygon, including its vertices, indices, and a bounding sphere.
  164. *
  165. * @param {CoplanarPolygonOutlineGeometry} polygonGeometry A description of the polygon.
  166. * @returns {Geometry|undefined} The computed vertices and indices.
  167. */
  168. CoplanarPolygonOutlineGeometry.createGeometry = function (polygonGeometry) {
  169. const polygonHierarchy = polygonGeometry._polygonHierarchy;
  170. let outerPositions = polygonHierarchy.positions;
  171. outerPositions = arrayRemoveDuplicates(
  172. outerPositions,
  173. Cartesian3.equalsEpsilon,
  174. true
  175. );
  176. if (outerPositions.length < 3) {
  177. return;
  178. }
  179. const isValid = CoplanarPolygonGeometryLibrary.validOutline(outerPositions);
  180. if (!isValid) {
  181. return undefined;
  182. }
  183. const polygons = PolygonGeometryLibrary.polygonOutlinesFromHierarchy(
  184. polygonHierarchy,
  185. false
  186. );
  187. if (polygons.length === 0) {
  188. return undefined;
  189. }
  190. const geometries = [];
  191. for (let i = 0; i < polygons.length; i++) {
  192. const geometryInstance = new GeometryInstance({
  193. geometry: createGeometryFromPositions(polygons[i]),
  194. });
  195. geometries.push(geometryInstance);
  196. }
  197. const geometry = GeometryPipeline.combineInstances(geometries)[0];
  198. const boundingSphere = BoundingSphere.fromPoints(polygonHierarchy.positions);
  199. return new Geometry({
  200. attributes: geometry.attributes,
  201. indices: geometry.indices,
  202. primitiveType: geometry.primitiveType,
  203. boundingSphere: boundingSphere,
  204. });
  205. };
  206. export default CoplanarPolygonOutlineGeometry;