createPolylineVolumeOutlineGeometry.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  5. *
  6. * Copyright 2011-2022 Cesium Contributors
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * Columbus View (Pat. Pend.)
  21. *
  22. * Portions licensed separately.
  23. * See https://github.com/CesiumGS/cesium/blob/main/LICENSE.md for full licensing details.
  24. */
  25. define(['./defaultValue-a6eb9f34', './Matrix2-ab676047', './arrayRemoveDuplicates-63722a6f', './BoundingRectangle-66558952', './Transforms-c78c4637', './ComponentDatatype-e06f4e16', './PolylineVolumeGeometryLibrary-f654b9e2', './RuntimeError-1088cc64', './GeometryAttribute-4f02e2ad', './GeometryAttributes-aff51037', './IndexDatatype-c2232ebd', './PolygonPipeline-dd4a5392', './_commonjsHelpers-89c9b271', './combine-7cf28d88', './WebGLConstants-d81b330d', './EllipsoidTangentPlane-6691e012', './AxisAlignedBoundingBox-51d5a498', './IntersectionTests-f96cd46d', './Plane-c985a1d2', './PolylinePipeline-3b0ed402', './EllipsoidGeodesic-f7721517', './EllipsoidRhumbLine-34574f75'], (function (defaultValue, Matrix2, arrayRemoveDuplicates, BoundingRectangle, Transforms, ComponentDatatype, PolylineVolumeGeometryLibrary, RuntimeError, GeometryAttribute, GeometryAttributes, IndexDatatype, PolygonPipeline, _commonjsHelpers, combine, WebGLConstants, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, PolylinePipeline, EllipsoidGeodesic, EllipsoidRhumbLine) { 'use strict';
  26. function computeAttributes(positions, shape) {
  27. const attributes = new GeometryAttributes.GeometryAttributes();
  28. attributes.position = new GeometryAttribute.GeometryAttribute({
  29. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  30. componentsPerAttribute: 3,
  31. values: positions,
  32. });
  33. const shapeLength = shape.length;
  34. const vertexCount = attributes.position.values.length / 3;
  35. const positionLength = positions.length / 3;
  36. const shapeCount = positionLength / shapeLength;
  37. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  38. vertexCount,
  39. 2 * shapeLength * (shapeCount + 1)
  40. );
  41. let i, j;
  42. let index = 0;
  43. i = 0;
  44. let 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. i = shapeCount - 1;
  52. offset = i * shapeLength;
  53. for (j = 0; j < shapeLength - 1; j++) {
  54. indices[index++] = j + offset;
  55. indices[index++] = j + offset + 1;
  56. }
  57. indices[index++] = shapeLength - 1 + offset;
  58. indices[index++] = offset;
  59. for (i = 0; i < shapeCount - 1; i++) {
  60. const firstOffset = shapeLength * i;
  61. const secondOffset = firstOffset + shapeLength;
  62. for (j = 0; j < shapeLength; j++) {
  63. indices[index++] = j + firstOffset;
  64. indices[index++] = j + secondOffset;
  65. }
  66. }
  67. const geometry = new GeometryAttribute.Geometry({
  68. attributes: attributes,
  69. indices: IndexDatatype.IndexDatatype.createTypedArray(vertexCount, indices),
  70. boundingSphere: Transforms.BoundingSphere.fromVertices(positions),
  71. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  72. });
  73. return geometry;
  74. }
  75. /**
  76. * A description of a polyline with a volume (a 2D shape extruded along a polyline).
  77. *
  78. * @alias PolylineVolumeOutlineGeometry
  79. * @constructor
  80. *
  81. * @param {Object} options Object with the following properties:
  82. * @param {Cartesian3[]} options.polylinePositions An array of positions that define the center of the polyline volume.
  83. * @param {Cartesian2[]} options.shapePositions An array of positions that define the shape to be extruded along the polyline
  84. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  85. * @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.
  86. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  87. *
  88. * @see PolylineVolumeOutlineGeometry#createGeometry
  89. *
  90. * @example
  91. * function computeCircle(radius) {
  92. * const positions = [];
  93. * for (let i = 0; i < 360; i++) {
  94. * const radians = Cesium.Math.toRadians(i);
  95. * positions.push(new Cesium.Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
  96. * }
  97. * return positions;
  98. * }
  99. *
  100. * const volumeOutline = new Cesium.PolylineVolumeOutlineGeometry({
  101. * polylinePositions : Cesium.Cartesian3.fromDegreesArray([
  102. * -72.0, 40.0,
  103. * -70.0, 35.0
  104. * ]),
  105. * shapePositions : computeCircle(100000.0)
  106. * });
  107. */
  108. function PolylineVolumeOutlineGeometry(options) {
  109. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  110. const positions = options.polylinePositions;
  111. const shape = options.shapePositions;
  112. //>>includeStart('debug', pragmas.debug);
  113. if (!defaultValue.defined(positions)) {
  114. throw new RuntimeError.DeveloperError("options.polylinePositions is required.");
  115. }
  116. if (!defaultValue.defined(shape)) {
  117. throw new RuntimeError.DeveloperError("options.shapePositions is required.");
  118. }
  119. //>>includeEnd('debug');
  120. this._positions = positions;
  121. this._shape = shape;
  122. this._ellipsoid = Matrix2.Ellipsoid.clone(
  123. defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84)
  124. );
  125. this._cornerType = defaultValue.defaultValue(options.cornerType, PolylineVolumeGeometryLibrary.CornerType.ROUNDED);
  126. this._granularity = defaultValue.defaultValue(
  127. options.granularity,
  128. ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE
  129. );
  130. this._workerName = "createPolylineVolumeOutlineGeometry";
  131. let numComponents = 1 + positions.length * Matrix2.Cartesian3.packedLength;
  132. numComponents += 1 + shape.length * Matrix2.Cartesian2.packedLength;
  133. /**
  134. * The number of elements used to pack the object into an array.
  135. * @type {Number}
  136. */
  137. this.packedLength = numComponents + Matrix2.Ellipsoid.packedLength + 2;
  138. }
  139. /**
  140. * Stores the provided instance into the provided array.
  141. *
  142. * @param {PolylineVolumeOutlineGeometry} value The value to pack.
  143. * @param {Number[]} array The array to pack into.
  144. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  145. *
  146. * @returns {Number[]} The array that was packed into
  147. */
  148. PolylineVolumeOutlineGeometry.pack = function (value, array, startingIndex) {
  149. //>>includeStart('debug', pragmas.debug);
  150. if (!defaultValue.defined(value)) {
  151. throw new RuntimeError.DeveloperError("value is required");
  152. }
  153. if (!defaultValue.defined(array)) {
  154. throw new RuntimeError.DeveloperError("array is required");
  155. }
  156. //>>includeEnd('debug');
  157. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  158. let i;
  159. const positions = value._positions;
  160. let length = positions.length;
  161. array[startingIndex++] = length;
  162. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  163. Matrix2.Cartesian3.pack(positions[i], array, startingIndex);
  164. }
  165. const shape = value._shape;
  166. length = shape.length;
  167. array[startingIndex++] = length;
  168. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian2.packedLength) {
  169. Matrix2.Cartesian2.pack(shape[i], array, startingIndex);
  170. }
  171. Matrix2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  172. startingIndex += Matrix2.Ellipsoid.packedLength;
  173. array[startingIndex++] = value._cornerType;
  174. array[startingIndex] = value._granularity;
  175. return array;
  176. };
  177. const scratchEllipsoid = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  178. const scratchOptions = {
  179. polylinePositions: undefined,
  180. shapePositions: undefined,
  181. ellipsoid: scratchEllipsoid,
  182. height: undefined,
  183. cornerType: undefined,
  184. granularity: undefined,
  185. };
  186. /**
  187. * Retrieves an instance from a packed array.
  188. *
  189. * @param {Number[]} array The packed array.
  190. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  191. * @param {PolylineVolumeOutlineGeometry} [result] The object into which to store the result.
  192. * @returns {PolylineVolumeOutlineGeometry} The modified result parameter or a new PolylineVolumeOutlineGeometry instance if one was not provided.
  193. */
  194. PolylineVolumeOutlineGeometry.unpack = function (array, startingIndex, result) {
  195. //>>includeStart('debug', pragmas.debug);
  196. if (!defaultValue.defined(array)) {
  197. throw new RuntimeError.DeveloperError("array is required");
  198. }
  199. //>>includeEnd('debug');
  200. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  201. let i;
  202. let length = array[startingIndex++];
  203. const positions = new Array(length);
  204. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  205. positions[i] = Matrix2.Cartesian3.unpack(array, startingIndex);
  206. }
  207. length = array[startingIndex++];
  208. const shape = new Array(length);
  209. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian2.packedLength) {
  210. shape[i] = Matrix2.Cartesian2.unpack(array, startingIndex);
  211. }
  212. const ellipsoid = Matrix2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  213. startingIndex += Matrix2.Ellipsoid.packedLength;
  214. const cornerType = array[startingIndex++];
  215. const granularity = array[startingIndex];
  216. if (!defaultValue.defined(result)) {
  217. scratchOptions.polylinePositions = positions;
  218. scratchOptions.shapePositions = shape;
  219. scratchOptions.cornerType = cornerType;
  220. scratchOptions.granularity = granularity;
  221. return new PolylineVolumeOutlineGeometry(scratchOptions);
  222. }
  223. result._positions = positions;
  224. result._shape = shape;
  225. result._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  226. result._cornerType = cornerType;
  227. result._granularity = granularity;
  228. return result;
  229. };
  230. const brScratch = new BoundingRectangle.BoundingRectangle();
  231. /**
  232. * Computes the geometric representation of the outline of a polyline with a volume, including its vertices, indices, and a bounding sphere.
  233. *
  234. * @param {PolylineVolumeOutlineGeometry} polylineVolumeOutlineGeometry A description of the polyline volume outline.
  235. * @returns {Geometry|undefined} The computed vertices and indices.
  236. */
  237. PolylineVolumeOutlineGeometry.createGeometry = function (
  238. polylineVolumeOutlineGeometry
  239. ) {
  240. const positions = polylineVolumeOutlineGeometry._positions;
  241. const cleanPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  242. positions,
  243. Matrix2.Cartesian3.equalsEpsilon
  244. );
  245. let shape2D = polylineVolumeOutlineGeometry._shape;
  246. shape2D = PolylineVolumeGeometryLibrary.PolylineVolumeGeometryLibrary.removeDuplicatesFromShape(shape2D);
  247. if (cleanPositions.length < 2 || shape2D.length < 3) {
  248. return undefined;
  249. }
  250. if (
  251. PolygonPipeline.PolygonPipeline.computeWindingOrder2D(shape2D) === PolygonPipeline.WindingOrder.CLOCKWISE
  252. ) {
  253. shape2D.reverse();
  254. }
  255. const boundingRectangle = BoundingRectangle.BoundingRectangle.fromPoints(shape2D, brScratch);
  256. const computedPositions = PolylineVolumeGeometryLibrary.PolylineVolumeGeometryLibrary.computePositions(
  257. cleanPositions,
  258. shape2D,
  259. boundingRectangle,
  260. polylineVolumeOutlineGeometry,
  261. false
  262. );
  263. return computeAttributes(computedPositions, shape2D);
  264. };
  265. function createPolylineVolumeOutlineGeometry(
  266. polylineVolumeOutlineGeometry,
  267. offset
  268. ) {
  269. if (defaultValue.defined(offset)) {
  270. polylineVolumeOutlineGeometry = PolylineVolumeOutlineGeometry.unpack(
  271. polylineVolumeOutlineGeometry,
  272. offset
  273. );
  274. }
  275. polylineVolumeOutlineGeometry._ellipsoid = Matrix2.Ellipsoid.clone(
  276. polylineVolumeOutlineGeometry._ellipsoid
  277. );
  278. return PolylineVolumeOutlineGeometry.createGeometry(
  279. polylineVolumeOutlineGeometry
  280. );
  281. }
  282. return createPolylineVolumeOutlineGeometry;
  283. }));