createPolylineVolumeGeometry.js 16 KB

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