CoplanarPolygonOutlineGeometry.js 8.0 KB

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