createPolylineVolumeGeometry.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./defaultValue-94c3e563', './Matrix2-69c32d33', './arrayRemoveDuplicates-87160c89', './BoundingRectangle-41078ce4', './Transforms-323408fe', './ComponentDatatype-b1ea011a', './PolylineVolumeGeometryLibrary-8388aac3', './RuntimeError-c581ca93', './GeometryAttribute-cb73bb3f', './GeometryAttributes-7df9bef6', './GeometryPipeline-e27e35f8', './IndexDatatype-c4099fe9', './PolygonPipeline-d1884135', './VertexFormat-e46f29d6', './_commonjsHelpers-3aae1032-f55dc0c4', './combine-761d9c3f', './WebGLConstants-7dccdc96', './EllipsoidTangentPlane-1e8d1fc2', './AxisAlignedBoundingBox-df2331b2', './IntersectionTests-d5d945ac', './Plane-069b6800', './PolylinePipeline-aa50e501', './EllipsoidGeodesic-98096082', './EllipsoidRhumbLine-5cb6da82', './AttributeCompression-3cfab808', './EncodedCartesian3-b1f97f8a'], (function (defaultValue, Matrix2, arrayRemoveDuplicates, BoundingRectangle, Transforms, ComponentDatatype, PolylineVolumeGeometryLibrary, RuntimeError, GeometryAttribute, GeometryAttributes, GeometryPipeline, IndexDatatype, PolygonPipeline, VertexFormat, _commonjsHelpers3aae1032, combine, WebGLConstants, EllipsoidTangentPlane, AxisAlignedBoundingBox, IntersectionTests, Plane, PolylinePipeline, EllipsoidGeodesic, EllipsoidRhumbLine, AttributeCompression, EncodedCartesian3) { 'use strict';
  3. function computeAttributes(
  4. combinedPositions,
  5. shape,
  6. boundingRectangle,
  7. vertexFormat
  8. ) {
  9. const attributes = new GeometryAttributes.GeometryAttributes();
  10. if (vertexFormat.position) {
  11. attributes.position = new GeometryAttribute.GeometryAttribute({
  12. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  13. componentsPerAttribute: 3,
  14. values: combinedPositions,
  15. });
  16. }
  17. const shapeLength = shape.length;
  18. const vertexCount = combinedPositions.length / 3;
  19. const length = (vertexCount - shapeLength * 2) / (shapeLength * 2);
  20. const firstEndIndices = PolygonPipeline.PolygonPipeline.triangulate(shape);
  21. const indicesCount =
  22. (length - 1) * shapeLength * 6 + firstEndIndices.length * 2;
  23. const indices = IndexDatatype.IndexDatatype.createTypedArray(vertexCount, indicesCount);
  24. let i, j;
  25. let ll, ul, ur, lr;
  26. const offset = shapeLength * 2;
  27. let index = 0;
  28. for (i = 0; i < length - 1; i++) {
  29. for (j = 0; j < shapeLength - 1; j++) {
  30. ll = j * 2 + i * shapeLength * 2;
  31. lr = ll + offset;
  32. ul = ll + 1;
  33. ur = ul + offset;
  34. indices[index++] = ul;
  35. indices[index++] = ll;
  36. indices[index++] = ur;
  37. indices[index++] = ur;
  38. indices[index++] = ll;
  39. indices[index++] = lr;
  40. }
  41. ll = shapeLength * 2 - 2 + i * shapeLength * 2;
  42. ul = ll + 1;
  43. ur = ul + offset;
  44. lr = ll + offset;
  45. indices[index++] = ul;
  46. indices[index++] = ll;
  47. indices[index++] = ur;
  48. indices[index++] = ur;
  49. indices[index++] = ll;
  50. indices[index++] = lr;
  51. }
  52. if (vertexFormat.st || vertexFormat.tangent || vertexFormat.bitangent) {
  53. // st required for tangent/bitangent calculation
  54. const st = new Float32Array(vertexCount * 2);
  55. const lengthSt = 1 / (length - 1);
  56. const heightSt = 1 / boundingRectangle.height;
  57. const heightOffset = boundingRectangle.height / 2;
  58. let s, t;
  59. let stindex = 0;
  60. for (i = 0; i < length; i++) {
  61. s = i * lengthSt;
  62. t = heightSt * (shape[0].y + heightOffset);
  63. st[stindex++] = s;
  64. st[stindex++] = t;
  65. for (j = 1; j < shapeLength; j++) {
  66. t = heightSt * (shape[j].y + heightOffset);
  67. st[stindex++] = s;
  68. st[stindex++] = t;
  69. st[stindex++] = s;
  70. st[stindex++] = t;
  71. }
  72. t = heightSt * (shape[0].y + heightOffset);
  73. st[stindex++] = s;
  74. st[stindex++] = t;
  75. }
  76. for (j = 0; j < shapeLength; j++) {
  77. s = 0;
  78. t = heightSt * (shape[j].y + heightOffset);
  79. st[stindex++] = s;
  80. st[stindex++] = t;
  81. }
  82. for (j = 0; j < shapeLength; j++) {
  83. s = (length - 1) * lengthSt;
  84. t = heightSt * (shape[j].y + heightOffset);
  85. st[stindex++] = s;
  86. st[stindex++] = t;
  87. }
  88. attributes.st = new GeometryAttribute.GeometryAttribute({
  89. componentDatatype: ComponentDatatype.ComponentDatatype.FLOAT,
  90. componentsPerAttribute: 2,
  91. values: new Float32Array(st),
  92. });
  93. }
  94. const endOffset = vertexCount - shapeLength * 2;
  95. for (i = 0; i < firstEndIndices.length; i += 3) {
  96. const v0 = firstEndIndices[i] + endOffset;
  97. const v1 = firstEndIndices[i + 1] + endOffset;
  98. const v2 = firstEndIndices[i + 2] + endOffset;
  99. indices[index++] = v0;
  100. indices[index++] = v1;
  101. indices[index++] = v2;
  102. indices[index++] = v2 + shapeLength;
  103. indices[index++] = v1 + shapeLength;
  104. indices[index++] = v0 + shapeLength;
  105. }
  106. let geometry = new GeometryAttribute.Geometry({
  107. attributes: attributes,
  108. indices: indices,
  109. boundingSphere: Transforms.BoundingSphere.fromVertices(combinedPositions),
  110. primitiveType: GeometryAttribute.PrimitiveType.TRIANGLES,
  111. });
  112. if (vertexFormat.normal) {
  113. geometry = GeometryPipeline.GeometryPipeline.computeNormal(geometry);
  114. }
  115. if (vertexFormat.tangent || vertexFormat.bitangent) {
  116. try {
  117. geometry = GeometryPipeline.GeometryPipeline.computeTangentAndBitangent(geometry);
  118. } catch (e) {
  119. PolylineVolumeGeometryLibrary.oneTimeWarning(
  120. "polyline-volume-tangent-bitangent",
  121. "Unable to compute tangents and bitangents for polyline volume geometry"
  122. );
  123. //TODO https://github.com/CesiumGS/cesium/issues/3609
  124. }
  125. if (!vertexFormat.tangent) {
  126. geometry.attributes.tangent = undefined;
  127. }
  128. if (!vertexFormat.bitangent) {
  129. geometry.attributes.bitangent = undefined;
  130. }
  131. if (!vertexFormat.st) {
  132. geometry.attributes.st = undefined;
  133. }
  134. }
  135. return geometry;
  136. }
  137. /**
  138. * A description of a polyline with a volume (a 2D shape extruded along a polyline).
  139. *
  140. * @alias PolylineVolumeGeometry
  141. * @constructor
  142. *
  143. * @param {Object} options Object with the following properties:
  144. * @param {Cartesian3[]} options.polylinePositions An array of {@link Cartesian3} positions that define the center of the polyline volume.
  145. * @param {Cartesian2[]} options.shapePositions An array of {@link Cartesian2} positions that define the shape to be extruded along the polyline
  146. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
  147. * @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.
  148. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  149. * @param {CornerType} [options.cornerType=CornerType.ROUNDED] Determines the style of the corners.
  150. *
  151. * @see PolylineVolumeGeometry#createGeometry
  152. *
  153. * @demo {@link https://sandcastle.cesium.com/index.html?src=Polyline%20Volume.html|Cesium Sandcastle Polyline Volume Demo}
  154. *
  155. * @example
  156. * function computeCircle(radius) {
  157. * const positions = [];
  158. * for (let i = 0; i < 360; i++) {
  159. * const radians = Cesium.Math.toRadians(i);
  160. * positions.push(new Cesium.Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
  161. * }
  162. * return positions;
  163. * }
  164. *
  165. * const volume = new Cesium.PolylineVolumeGeometry({
  166. * vertexFormat : Cesium.VertexFormat.POSITION_ONLY,
  167. * polylinePositions : Cesium.Cartesian3.fromDegreesArray([
  168. * -72.0, 40.0,
  169. * -70.0, 35.0
  170. * ]),
  171. * shapePositions : computeCircle(100000.0)
  172. * });
  173. */
  174. function PolylineVolumeGeometry(options) {
  175. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  176. const positions = options.polylinePositions;
  177. const shape = options.shapePositions;
  178. //>>includeStart('debug', pragmas.debug);
  179. if (!defaultValue.defined(positions)) {
  180. throw new RuntimeError.DeveloperError("options.polylinePositions is required.");
  181. }
  182. if (!defaultValue.defined(shape)) {
  183. throw new RuntimeError.DeveloperError("options.shapePositions is required.");
  184. }
  185. //>>includeEnd('debug');
  186. this._positions = positions;
  187. this._shape = shape;
  188. this._ellipsoid = Matrix2.Ellipsoid.clone(
  189. defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84)
  190. );
  191. this._cornerType = defaultValue.defaultValue(options.cornerType, PolylineVolumeGeometryLibrary.CornerType.ROUNDED);
  192. this._vertexFormat = VertexFormat.VertexFormat.clone(
  193. defaultValue.defaultValue(options.vertexFormat, VertexFormat.VertexFormat.DEFAULT)
  194. );
  195. this._granularity = defaultValue.defaultValue(
  196. options.granularity,
  197. ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE
  198. );
  199. this._workerName = "createPolylineVolumeGeometry";
  200. let numComponents = 1 + positions.length * Matrix2.Cartesian3.packedLength;
  201. numComponents += 1 + shape.length * Matrix2.Cartesian2.packedLength;
  202. /**
  203. * The number of elements used to pack the object into an array.
  204. * @type {Number}
  205. */
  206. this.packedLength =
  207. numComponents + Matrix2.Ellipsoid.packedLength + VertexFormat.VertexFormat.packedLength + 2;
  208. }
  209. /**
  210. * Stores the provided instance into the provided array.
  211. *
  212. * @param {PolylineVolumeGeometry} value The value to pack.
  213. * @param {Number[]} array The array to pack into.
  214. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  215. *
  216. * @returns {Number[]} The array that was packed into
  217. */
  218. PolylineVolumeGeometry.pack = function (value, array, startingIndex) {
  219. //>>includeStart('debug', pragmas.debug);
  220. if (!defaultValue.defined(value)) {
  221. throw new RuntimeError.DeveloperError("value is required");
  222. }
  223. if (!defaultValue.defined(array)) {
  224. throw new RuntimeError.DeveloperError("array is required");
  225. }
  226. //>>includeEnd('debug');
  227. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  228. let i;
  229. const positions = value._positions;
  230. let length = positions.length;
  231. array[startingIndex++] = length;
  232. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  233. Matrix2.Cartesian3.pack(positions[i], array, startingIndex);
  234. }
  235. const shape = value._shape;
  236. length = shape.length;
  237. array[startingIndex++] = length;
  238. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian2.packedLength) {
  239. Matrix2.Cartesian2.pack(shape[i], array, startingIndex);
  240. }
  241. Matrix2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  242. startingIndex += Matrix2.Ellipsoid.packedLength;
  243. VertexFormat.VertexFormat.pack(value._vertexFormat, array, startingIndex);
  244. startingIndex += VertexFormat.VertexFormat.packedLength;
  245. array[startingIndex++] = value._cornerType;
  246. array[startingIndex] = value._granularity;
  247. return array;
  248. };
  249. const scratchEllipsoid = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  250. const scratchVertexFormat = new VertexFormat.VertexFormat();
  251. const scratchOptions = {
  252. polylinePositions: undefined,
  253. shapePositions: undefined,
  254. ellipsoid: scratchEllipsoid,
  255. vertexFormat: scratchVertexFormat,
  256. cornerType: undefined,
  257. granularity: undefined,
  258. };
  259. /**
  260. * Retrieves an instance from a packed array.
  261. *
  262. * @param {Number[]} array The packed array.
  263. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  264. * @param {PolylineVolumeGeometry} [result] The object into which to store the result.
  265. * @returns {PolylineVolumeGeometry} The modified result parameter or a new PolylineVolumeGeometry instance if one was not provided.
  266. */
  267. PolylineVolumeGeometry.unpack = function (array, startingIndex, result) {
  268. //>>includeStart('debug', pragmas.debug);
  269. if (!defaultValue.defined(array)) {
  270. throw new RuntimeError.DeveloperError("array is required");
  271. }
  272. //>>includeEnd('debug');
  273. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  274. let i;
  275. let length = array[startingIndex++];
  276. const positions = new Array(length);
  277. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  278. positions[i] = Matrix2.Cartesian3.unpack(array, startingIndex);
  279. }
  280. length = array[startingIndex++];
  281. const shape = new Array(length);
  282. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian2.packedLength) {
  283. shape[i] = Matrix2.Cartesian2.unpack(array, startingIndex);
  284. }
  285. const ellipsoid = Matrix2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  286. startingIndex += Matrix2.Ellipsoid.packedLength;
  287. const vertexFormat = VertexFormat.VertexFormat.unpack(
  288. array,
  289. startingIndex,
  290. scratchVertexFormat
  291. );
  292. startingIndex += VertexFormat.VertexFormat.packedLength;
  293. const cornerType = array[startingIndex++];
  294. const granularity = array[startingIndex];
  295. if (!defaultValue.defined(result)) {
  296. scratchOptions.polylinePositions = positions;
  297. scratchOptions.shapePositions = shape;
  298. scratchOptions.cornerType = cornerType;
  299. scratchOptions.granularity = granularity;
  300. return new PolylineVolumeGeometry(scratchOptions);
  301. }
  302. result._positions = positions;
  303. result._shape = shape;
  304. result._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  305. result._vertexFormat = VertexFormat.VertexFormat.clone(vertexFormat, result._vertexFormat);
  306. result._cornerType = cornerType;
  307. result._granularity = granularity;
  308. return result;
  309. };
  310. const brScratch = new BoundingRectangle.BoundingRectangle();
  311. /**
  312. * Computes the geometric representation of a polyline with a volume, including its vertices, indices, and a bounding sphere.
  313. *
  314. * @param {PolylineVolumeGeometry} polylineVolumeGeometry A description of the polyline volume.
  315. * @returns {Geometry|undefined} The computed vertices and indices.
  316. */
  317. PolylineVolumeGeometry.createGeometry = function (polylineVolumeGeometry) {
  318. const positions = polylineVolumeGeometry._positions;
  319. const cleanPositions = arrayRemoveDuplicates.arrayRemoveDuplicates(
  320. positions,
  321. Matrix2.Cartesian3.equalsEpsilon
  322. );
  323. let shape2D = polylineVolumeGeometry._shape;
  324. shape2D = PolylineVolumeGeometryLibrary.PolylineVolumeGeometryLibrary.removeDuplicatesFromShape(shape2D);
  325. if (cleanPositions.length < 2 || shape2D.length < 3) {
  326. return undefined;
  327. }
  328. if (
  329. PolygonPipeline.PolygonPipeline.computeWindingOrder2D(shape2D) === PolygonPipeline.WindingOrder.CLOCKWISE
  330. ) {
  331. shape2D.reverse();
  332. }
  333. const boundingRectangle = BoundingRectangle.BoundingRectangle.fromPoints(shape2D, brScratch);
  334. const computedPositions = PolylineVolumeGeometryLibrary.PolylineVolumeGeometryLibrary.computePositions(
  335. cleanPositions,
  336. shape2D,
  337. boundingRectangle,
  338. polylineVolumeGeometry,
  339. true
  340. );
  341. return computeAttributes(
  342. computedPositions,
  343. shape2D,
  344. boundingRectangle,
  345. polylineVolumeGeometry._vertexFormat
  346. );
  347. };
  348. function createPolylineVolumeGeometry(polylineVolumeGeometry, offset) {
  349. if (defaultValue.defined(offset)) {
  350. polylineVolumeGeometry = PolylineVolumeGeometry.unpack(
  351. polylineVolumeGeometry,
  352. offset
  353. );
  354. }
  355. polylineVolumeGeometry._ellipsoid = Matrix2.Ellipsoid.clone(
  356. polylineVolumeGeometry._ellipsoid
  357. );
  358. return PolylineVolumeGeometry.createGeometry(polylineVolumeGeometry);
  359. }
  360. return createPolylineVolumeGeometry;
  361. }));