createPolylineVolumeGeometry.js 20 KB

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