PolylineVolumeOutlineGeometry.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import arrayRemoveDuplicates from './arrayRemoveDuplicates.js';
  2. import BoundingRectangle from './BoundingRectangle.js';
  3. import BoundingSphere from './BoundingSphere.js';
  4. import Cartesian2 from './Cartesian2.js';
  5. import Cartesian3 from './Cartesian3.js';
  6. import ComponentDatatype from './ComponentDatatype.js';
  7. import CornerType from './CornerType.js';
  8. import defaultValue from './defaultValue.js';
  9. import defined from './defined.js';
  10. import DeveloperError from './DeveloperError.js';
  11. import Ellipsoid from './Ellipsoid.js';
  12. import Geometry from './Geometry.js';
  13. import GeometryAttribute from './GeometryAttribute.js';
  14. import GeometryAttributes from './GeometryAttributes.js';
  15. import IndexDatatype from './IndexDatatype.js';
  16. import CesiumMath from './Math.js';
  17. import PolygonPipeline from './PolygonPipeline.js';
  18. import PolylineVolumeGeometryLibrary from './PolylineVolumeGeometryLibrary.js';
  19. import PrimitiveType from './PrimitiveType.js';
  20. import WindingOrder from './WindingOrder.js';
  21. function computeAttributes(positions, shape) {
  22. var attributes = new GeometryAttributes();
  23. attributes.position = new GeometryAttribute({
  24. componentDatatype : ComponentDatatype.DOUBLE,
  25. componentsPerAttribute : 3,
  26. values : positions
  27. });
  28. var shapeLength = shape.length;
  29. var vertexCount = attributes.position.values.length / 3;
  30. var positionLength = positions.length / 3;
  31. var shapeCount = positionLength / shapeLength;
  32. var indices = IndexDatatype.createTypedArray(vertexCount, 2 * shapeLength * (shapeCount + 1));
  33. var i, j;
  34. var index = 0;
  35. i = 0;
  36. var offset = i * shapeLength;
  37. for (j = 0; j < shapeLength - 1; j++) {
  38. indices[index++] = j + offset;
  39. indices[index++] = j + offset + 1;
  40. }
  41. indices[index++] = shapeLength - 1 + offset;
  42. indices[index++] = offset;
  43. i = shapeCount - 1;
  44. offset = i * shapeLength;
  45. for (j = 0; j < shapeLength - 1; j++) {
  46. indices[index++] = j + offset;
  47. indices[index++] = j + offset + 1;
  48. }
  49. indices[index++] = shapeLength - 1 + offset;
  50. indices[index++] = offset;
  51. for (i = 0; i < shapeCount - 1; i++) {
  52. var firstOffset = shapeLength * i;
  53. var secondOffset = firstOffset + shapeLength;
  54. for (j = 0; j < shapeLength; j++) {
  55. indices[index++] = j + firstOffset;
  56. indices[index++] = j + secondOffset;
  57. }
  58. }
  59. var geometry = new Geometry({
  60. attributes : attributes,
  61. indices : IndexDatatype.createTypedArray(vertexCount, indices),
  62. boundingSphere : BoundingSphere.fromVertices(positions),
  63. primitiveType : PrimitiveType.LINES
  64. });
  65. return geometry;
  66. }
  67. /**
  68. * A description of a polyline with a volume (a 2D shape extruded along a polyline).
  69. *
  70. * @alias PolylineVolumeOutlineGeometry
  71. * @constructor
  72. *
  73. * @param {Object} options Object with the following properties:
  74. * @param {Cartesian3[]} options.polylinePositions An array of positions that define the center of the polyline volume.
  75. * @param {Cartesian2[]} options.shapePositions An array of positions that define the shape to be extruded along the polyline
  76. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  77. * @param {Number} [options.granularity=CesiumMath.RADIANS_PER_DEGREE] The distance, in radians, between each latitude and longitude. Determines the number of positions in the buffer.
  78. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  79. *
  80. * @see PolylineVolumeOutlineGeometry#createGeometry
  81. *
  82. * @example
  83. * function computeCircle(radius) {
  84. * var positions = [];
  85. * for (var i = 0; i < 360; i++) {
  86. * var radians = Cesium.Math.toRadians(i);
  87. * positions.push(new Cesium.Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
  88. * }
  89. * return positions;
  90. * }
  91. *
  92. * var volumeOutline = new Cesium.PolylineVolumeOutlineGeometry({
  93. * polylinePositions : Cesium.Cartesian3.fromDegreesArray([
  94. * -72.0, 40.0,
  95. * -70.0, 35.0
  96. * ]),
  97. * shapePositions : computeCircle(100000.0)
  98. * });
  99. */
  100. function PolylineVolumeOutlineGeometry(options) {
  101. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  102. var positions = options.polylinePositions;
  103. var shape = options.shapePositions;
  104. //>>includeStart('debug', pragmas.debug);
  105. if (!defined(positions)) {
  106. throw new DeveloperError('options.polylinePositions is required.');
  107. }
  108. if (!defined(shape)) {
  109. throw new DeveloperError('options.shapePositions is required.');
  110. }
  111. //>>includeEnd('debug');
  112. this._positions = positions;
  113. this._shape = shape;
  114. this._ellipsoid = Ellipsoid.clone(defaultValue(options.ellipsoid, Ellipsoid.WGS84));
  115. this._cornerType = defaultValue(options.cornerType, CornerType.ROUNDED);
  116. this._granularity = defaultValue(options.granularity, CesiumMath.RADIANS_PER_DEGREE);
  117. this._workerName = 'createPolylineVolumeOutlineGeometry';
  118. var numComponents = 1 + positions.length * Cartesian3.packedLength;
  119. numComponents += 1 + shape.length * Cartesian2.packedLength;
  120. /**
  121. * The number of elements used to pack the object into an array.
  122. * @type {Number}
  123. */
  124. this.packedLength = numComponents + Ellipsoid.packedLength + 2;
  125. }
  126. /**
  127. * Stores the provided instance into the provided array.
  128. *
  129. * @param {PolylineVolumeOutlineGeometry} value The value to pack.
  130. * @param {Number[]} array The array to pack into.
  131. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  132. *
  133. * @returns {Number[]} The array that was packed into
  134. */
  135. PolylineVolumeOutlineGeometry.pack = function(value, array, startingIndex) {
  136. //>>includeStart('debug', pragmas.debug);
  137. if (!defined(value)) {
  138. throw new DeveloperError('value is required');
  139. }
  140. if (!defined(array)) {
  141. throw new DeveloperError('array is required');
  142. }
  143. //>>includeEnd('debug');
  144. startingIndex = defaultValue(startingIndex, 0);
  145. var i;
  146. var positions = value._positions;
  147. var length = positions.length;
  148. array[startingIndex++] = length;
  149. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  150. Cartesian3.pack(positions[i], array, startingIndex);
  151. }
  152. var shape = value._shape;
  153. length = shape.length;
  154. array[startingIndex++] = length;
  155. for (i = 0; i < length; ++i, startingIndex += Cartesian2.packedLength) {
  156. Cartesian2.pack(shape[i], array, startingIndex);
  157. }
  158. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  159. startingIndex += Ellipsoid.packedLength;
  160. array[startingIndex++] = value._cornerType;
  161. array[startingIndex] = value._granularity;
  162. return array;
  163. };
  164. var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  165. var scratchOptions = {
  166. polylinePositions : undefined,
  167. shapePositions : undefined,
  168. ellipsoid : scratchEllipsoid,
  169. height : undefined,
  170. cornerType : undefined,
  171. granularity : undefined
  172. };
  173. /**
  174. * Retrieves an instance from a packed array.
  175. *
  176. * @param {Number[]} array The packed array.
  177. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  178. * @param {PolylineVolumeOutlineGeometry} [result] The object into which to store the result.
  179. * @returns {PolylineVolumeOutlineGeometry} The modified result parameter or a new PolylineVolumeOutlineGeometry instance if one was not provided.
  180. */
  181. PolylineVolumeOutlineGeometry.unpack = function(array, startingIndex, result) {
  182. //>>includeStart('debug', pragmas.debug);
  183. if (!defined(array)) {
  184. throw new DeveloperError('array is required');
  185. }
  186. //>>includeEnd('debug');
  187. startingIndex = defaultValue(startingIndex, 0);
  188. var i;
  189. var length = array[startingIndex++];
  190. var positions = new Array(length);
  191. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  192. positions[i] = Cartesian3.unpack(array, startingIndex);
  193. }
  194. length = array[startingIndex++];
  195. var shape = new Array(length);
  196. for (i = 0; i < length; ++i, startingIndex += Cartesian2.packedLength) {
  197. shape[i] = Cartesian2.unpack(array, startingIndex);
  198. }
  199. var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  200. startingIndex += Ellipsoid.packedLength;
  201. var cornerType = array[startingIndex++];
  202. var granularity = array[startingIndex];
  203. if (!defined(result)) {
  204. scratchOptions.polylinePositions = positions;
  205. scratchOptions.shapePositions = shape;
  206. scratchOptions.cornerType = cornerType;
  207. scratchOptions.granularity = granularity;
  208. return new PolylineVolumeOutlineGeometry(scratchOptions);
  209. }
  210. result._positions = positions;
  211. result._shape = shape;
  212. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  213. result._cornerType = cornerType;
  214. result._granularity = granularity;
  215. return result;
  216. };
  217. var brScratch = new BoundingRectangle();
  218. /**
  219. * Computes the geometric representation of the outline of a polyline with a volume, including its vertices, indices, and a bounding sphere.
  220. *
  221. * @param {PolylineVolumeOutlineGeometry} polylineVolumeOutlineGeometry A description of the polyline volume outline.
  222. * @returns {Geometry|undefined} The computed vertices and indices.
  223. */
  224. PolylineVolumeOutlineGeometry.createGeometry = function(polylineVolumeOutlineGeometry) {
  225. var positions = polylineVolumeOutlineGeometry._positions;
  226. var cleanPositions = arrayRemoveDuplicates(positions, Cartesian3.equalsEpsilon);
  227. var shape2D = polylineVolumeOutlineGeometry._shape;
  228. shape2D = PolylineVolumeGeometryLibrary.removeDuplicatesFromShape(shape2D);
  229. if (cleanPositions.length < 2 || shape2D.length < 3) {
  230. return undefined;
  231. }
  232. if (PolygonPipeline.computeWindingOrder2D(shape2D) === WindingOrder.CLOCKWISE) {
  233. shape2D.reverse();
  234. }
  235. var boundingRectangle = BoundingRectangle.fromPoints(shape2D, brScratch);
  236. var computedPositions = PolylineVolumeGeometryLibrary.computePositions(cleanPositions, shape2D, boundingRectangle, polylineVolumeOutlineGeometry, false);
  237. return computeAttributes(computedPositions, shape2D);
  238. };
  239. export default PolylineVolumeOutlineGeometry;