createPolylineVolumeGeometry.js 18 KB

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