createPolylineVolumeGeometry.js 15 KB

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