createPolylineVolumeGeometry.js 17 KB

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