PolylineVolumeGeometry.js 15 KB

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