PolylineVolumeGeometry.js 13 KB

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