createPolylineVolumeOutlineGeometry.js 12 KB

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