createWallOutlineGeometry.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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', './IndexDatatype-9435b55f', './IntersectionTests-ca40c01c', './Plane-b1361c67', './EllipsoidTangentPlane-0b4ce564', './EllipsoidRhumbLine-f161e674', './earcut-2.2.1-b404d9e6', './PolygonPipeline-fd46002b', './EllipsoidGeodesic-84507801', './PolylinePipeline-a9f32196', './WallGeometryLibrary-a04c2a8b'], function (when, Check, _Math, Cartographic, Cartesian4, Cartesian2, BoundingSphere, RuntimeError, WebGLConstants, ComponentDatatype, GeometryAttribute, PrimitiveType, FeatureDetection, Transforms, buildModuleUrl, GeometryAttributes, IndexDatatype, IntersectionTests, Plane, EllipsoidTangentPlane, EllipsoidRhumbLine, earcut2_2_1, PolygonPipeline, EllipsoidGeodesic, PolylinePipeline, WallGeometryLibrary) { 'use strict';
  24. var scratchCartesian3Position1 = new Cartographic.Cartesian3();
  25. var scratchCartesian3Position2 = new Cartographic.Cartesian3();
  26. /**
  27. * A description of a wall outline. A wall is defined by a series of points,
  28. * which extrude down to the ground. Optionally, they can extrude downwards to a specified height.
  29. *
  30. * @alias WallOutlineGeometry
  31. * @constructor
  32. *
  33. * @param {Object} options Object with the following properties:
  34. * @param {Cartesian3[]} options.positions An array of Cartesian objects, which are the points of the wall.
  35. * @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.
  36. * @param {Number[]} [options.maximumHeights] An array parallel to <code>positions</code> that give the maximum height of the
  37. * wall at <code>positions</code>. If undefined, the height of each position in used.
  38. * @param {Number[]} [options.minimumHeights] An array parallel to <code>positions</code> that give the minimum height of the
  39. * wall at <code>positions</code>. If undefined, the height at each position is 0.0.
  40. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid for coordinate manipulation
  41. *
  42. * @exception {DeveloperError} positions length must be greater than or equal to 2.
  43. * @exception {DeveloperError} positions and maximumHeights must have the same length.
  44. * @exception {DeveloperError} positions and minimumHeights must have the same length.
  45. *
  46. * @see WallGeometry#createGeometry
  47. * @see WallGeometry#fromConstantHeight
  48. *
  49. * @example
  50. * // create a wall outline that spans from ground level to 10000 meters
  51. * var wall = new Cesium.WallOutlineGeometry({
  52. * positions : Cesium.Cartesian3.fromDegreesArrayHeights([
  53. * 19.0, 47.0, 10000.0,
  54. * 19.0, 48.0, 10000.0,
  55. * 20.0, 48.0, 10000.0,
  56. * 20.0, 47.0, 10000.0,
  57. * 19.0, 47.0, 10000.0
  58. * ])
  59. * });
  60. * var geometry = Cesium.WallOutlineGeometry.createGeometry(wall);
  61. */
  62. function WallOutlineGeometry(options) {
  63. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  64. var wallPositions = options.positions;
  65. var maximumHeights = options.maximumHeights;
  66. var minimumHeights = options.minimumHeights;
  67. //>>includeStart('debug', pragmas.debug);
  68. if (!when.defined(wallPositions)) {
  69. throw new Check.DeveloperError('options.positions is required.');
  70. }
  71. if (when.defined(maximumHeights) && maximumHeights.length !== wallPositions.length) {
  72. throw new Check.DeveloperError('options.positions and options.maximumHeights must have the same length.');
  73. }
  74. if (when.defined(minimumHeights) && minimumHeights.length !== wallPositions.length) {
  75. throw new Check.DeveloperError('options.positions and options.minimumHeights must have the same length.');
  76. }
  77. //>>includeEnd('debug');
  78. var granularity = when.defaultValue(options.granularity, _Math.CesiumMath.RADIANS_PER_DEGREE);
  79. var ellipsoid = when.defaultValue(options.ellipsoid, Cartesian2.Ellipsoid.WGS84);
  80. this._positions = wallPositions;
  81. this._minimumHeights = minimumHeights;
  82. this._maximumHeights = maximumHeights;
  83. this._granularity = granularity;
  84. this._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid);
  85. this._workerName = 'createWallOutlineGeometry';
  86. var numComponents = 1 + wallPositions.length * Cartographic.Cartesian3.packedLength + 2;
  87. if (when.defined(minimumHeights)) {
  88. numComponents += minimumHeights.length;
  89. }
  90. if (when.defined(maximumHeights)) {
  91. numComponents += maximumHeights.length;
  92. }
  93. /**
  94. * The number of elements used to pack the object into an array.
  95. * @type {Number}
  96. */
  97. this.packedLength = numComponents + Cartesian2.Ellipsoid.packedLength + 1;
  98. }
  99. /**
  100. * Stores the provided instance into the provided array.
  101. *
  102. * @param {WallOutlineGeometry} value The value to pack.
  103. * @param {Number[]} array The array to pack into.
  104. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  105. *
  106. * @returns {Number[]} The array that was packed into
  107. */
  108. WallOutlineGeometry.pack = function(value, array, startingIndex) {
  109. //>>includeStart('debug', pragmas.debug);
  110. if (!when.defined(value)) {
  111. throw new Check.DeveloperError('value is required');
  112. }
  113. if (!when.defined(array)) {
  114. throw new Check.DeveloperError('array is required');
  115. }
  116. //>>includeEnd('debug');
  117. startingIndex = when.defaultValue(startingIndex, 0);
  118. var i;
  119. var positions = value._positions;
  120. var length = positions.length;
  121. array[startingIndex++] = length;
  122. for (i = 0; i < length; ++i, startingIndex += Cartographic.Cartesian3.packedLength) {
  123. Cartographic.Cartesian3.pack(positions[i], array, startingIndex);
  124. }
  125. var minimumHeights = value._minimumHeights;
  126. length = when.defined(minimumHeights) ? minimumHeights.length : 0;
  127. array[startingIndex++] = length;
  128. if (when.defined(minimumHeights)) {
  129. for (i = 0; i < length; ++i) {
  130. array[startingIndex++] = minimumHeights[i];
  131. }
  132. }
  133. var maximumHeights = value._maximumHeights;
  134. length = when.defined(maximumHeights) ? maximumHeights.length : 0;
  135. array[startingIndex++] = length;
  136. if (when.defined(maximumHeights)) {
  137. for (i = 0; i < length; ++i) {
  138. array[startingIndex++] = maximumHeights[i];
  139. }
  140. }
  141. Cartesian2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  142. startingIndex += Cartesian2.Ellipsoid.packedLength;
  143. array[startingIndex] = value._granularity;
  144. return array;
  145. };
  146. var scratchEllipsoid = Cartesian2.Ellipsoid.clone(Cartesian2.Ellipsoid.UNIT_SPHERE);
  147. var scratchOptions = {
  148. positions : undefined,
  149. minimumHeights : undefined,
  150. maximumHeights : undefined,
  151. ellipsoid : scratchEllipsoid,
  152. granularity : undefined
  153. };
  154. /**
  155. * Retrieves an instance from a packed array.
  156. *
  157. * @param {Number[]} array The packed array.
  158. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  159. * @param {WallOutlineGeometry} [result] The object into which to store the result.
  160. * @returns {WallOutlineGeometry} The modified result parameter or a new WallOutlineGeometry instance if one was not provided.
  161. */
  162. WallOutlineGeometry.unpack = function(array, startingIndex, result) {
  163. //>>includeStart('debug', pragmas.debug);
  164. if (!when.defined(array)) {
  165. throw new Check.DeveloperError('array is required');
  166. }
  167. //>>includeEnd('debug');
  168. startingIndex = when.defaultValue(startingIndex, 0);
  169. var i;
  170. var length = array[startingIndex++];
  171. var positions = new Array(length);
  172. for (i = 0; i < length; ++i, startingIndex += Cartographic.Cartesian3.packedLength) {
  173. positions[i] = Cartographic.Cartesian3.unpack(array, startingIndex);
  174. }
  175. length = array[startingIndex++];
  176. var minimumHeights;
  177. if (length > 0) {
  178. minimumHeights = new Array(length);
  179. for (i = 0; i < length; ++i) {
  180. minimumHeights[i] = array[startingIndex++];
  181. }
  182. }
  183. length = array[startingIndex++];
  184. var maximumHeights;
  185. if (length > 0) {
  186. maximumHeights = new Array(length);
  187. for (i = 0; i < length; ++i) {
  188. maximumHeights[i] = array[startingIndex++];
  189. }
  190. }
  191. var ellipsoid = Cartesian2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  192. startingIndex += Cartesian2.Ellipsoid.packedLength;
  193. var granularity = array[startingIndex];
  194. if (!when.defined(result)) {
  195. scratchOptions.positions = positions;
  196. scratchOptions.minimumHeights = minimumHeights;
  197. scratchOptions.maximumHeights = maximumHeights;
  198. scratchOptions.granularity = granularity;
  199. return new WallOutlineGeometry(scratchOptions);
  200. }
  201. result._positions = positions;
  202. result._minimumHeights = minimumHeights;
  203. result._maximumHeights = maximumHeights;
  204. result._ellipsoid = Cartesian2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  205. result._granularity = granularity;
  206. return result;
  207. };
  208. /**
  209. * A description of a walloutline. A wall is defined by a series of points,
  210. * which extrude down to the ground. Optionally, they can extrude downwards to a specified height.
  211. *
  212. * @param {Object} options Object with the following properties:
  213. * @param {Cartesian3[]} options.positions An array of Cartesian objects, which are the points of the wall.
  214. * @param {Number} [options.maximumHeight] A constant that defines the maximum height of the
  215. * wall at <code>positions</code>. If undefined, the height of each position in used.
  216. * @param {Number} [options.minimumHeight] A constant that defines the minimum height of the
  217. * wall at <code>positions</code>. If undefined, the height at each position is 0.0.
  218. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid for coordinate manipulation
  219. * @returns {WallOutlineGeometry}
  220. *
  221. *
  222. * @example
  223. * // create a wall that spans from 10000 meters to 20000 meters
  224. * var wall = Cesium.WallOutlineGeometry.fromConstantHeights({
  225. * positions : Cesium.Cartesian3.fromDegreesArray([
  226. * 19.0, 47.0,
  227. * 19.0, 48.0,
  228. * 20.0, 48.0,
  229. * 20.0, 47.0,
  230. * 19.0, 47.0,
  231. * ]),
  232. * minimumHeight : 20000.0,
  233. * maximumHeight : 10000.0
  234. * });
  235. * var geometry = Cesium.WallOutlineGeometry.createGeometry(wall);
  236. *
  237. * @see WallOutlineGeometry#createGeometry
  238. */
  239. WallOutlineGeometry.fromConstantHeights = function(options) {
  240. options = when.defaultValue(options, when.defaultValue.EMPTY_OBJECT);
  241. var positions = options.positions;
  242. //>>includeStart('debug', pragmas.debug);
  243. if (!when.defined(positions)) {
  244. throw new Check.DeveloperError('options.positions is required.');
  245. }
  246. //>>includeEnd('debug');
  247. var minHeights;
  248. var maxHeights;
  249. var min = options.minimumHeight;
  250. var max = options.maximumHeight;
  251. var doMin = when.defined(min);
  252. var doMax = when.defined(max);
  253. if (doMin || doMax) {
  254. var length = positions.length;
  255. minHeights = (doMin) ? new Array(length) : undefined;
  256. maxHeights = (doMax) ? new Array(length) : undefined;
  257. for (var i = 0; i < length; ++i) {
  258. if (doMin) {
  259. minHeights[i] = min;
  260. }
  261. if (doMax) {
  262. maxHeights[i] = max;
  263. }
  264. }
  265. }
  266. var newOptions = {
  267. positions : positions,
  268. maximumHeights : maxHeights,
  269. minimumHeights : minHeights,
  270. ellipsoid : options.ellipsoid
  271. };
  272. return new WallOutlineGeometry(newOptions);
  273. };
  274. /**
  275. * Computes the geometric representation of a wall outline, including its vertices, indices, and a bounding sphere.
  276. *
  277. * @param {WallOutlineGeometry} wallGeometry A description of the wall outline.
  278. * @returns {Geometry|undefined} The computed vertices and indices.
  279. */
  280. WallOutlineGeometry.createGeometry = function(wallGeometry) {
  281. var wallPositions = wallGeometry._positions;
  282. var minimumHeights = wallGeometry._minimumHeights;
  283. var maximumHeights = wallGeometry._maximumHeights;
  284. var granularity = wallGeometry._granularity;
  285. var ellipsoid = wallGeometry._ellipsoid;
  286. var pos = WallGeometryLibrary.WallGeometryLibrary.computePositions(ellipsoid, wallPositions, maximumHeights, minimumHeights, granularity, false);
  287. if (!when.defined(pos)) {
  288. return;
  289. }
  290. var bottomPositions = pos.pos.bottomPositions;
  291. var topPositions = pos.pos.topPositions;
  292. var length = topPositions.length;
  293. var size = length * 2;
  294. var positions = new Float64Array(size);
  295. var positionIndex = 0;
  296. // add lower and upper points one after the other, lower
  297. // points being even and upper points being odd
  298. length /= 3;
  299. var i;
  300. for (i = 0; i < length; ++i) {
  301. var i3 = i * 3;
  302. var topPosition = Cartographic.Cartesian3.fromArray(topPositions, i3, scratchCartesian3Position1);
  303. var bottomPosition = Cartographic.Cartesian3.fromArray(bottomPositions, i3, scratchCartesian3Position2);
  304. // insert the lower point
  305. positions[positionIndex++] = bottomPosition.x;
  306. positions[positionIndex++] = bottomPosition.y;
  307. positions[positionIndex++] = bottomPosition.z;
  308. // insert the upper point
  309. positions[positionIndex++] = topPosition.x;
  310. positions[positionIndex++] = topPosition.y;
  311. positions[positionIndex++] = topPosition.z;
  312. }
  313. var attributes = new GeometryAttributes.GeometryAttributes({
  314. position : new GeometryAttribute.GeometryAttribute({
  315. componentDatatype : ComponentDatatype.ComponentDatatype.DOUBLE,
  316. componentsPerAttribute : 3,
  317. values : positions
  318. })
  319. });
  320. var numVertices = size / 3;
  321. size = 2 * numVertices - 4 + numVertices;
  322. var indices = IndexDatatype.IndexDatatype.createTypedArray(numVertices, size);
  323. var edgeIndex = 0;
  324. for (i = 0; i < numVertices - 2; i += 2) {
  325. var LL = i;
  326. var LR = i + 2;
  327. var pl = Cartographic.Cartesian3.fromArray(positions, LL * 3, scratchCartesian3Position1);
  328. var pr = Cartographic.Cartesian3.fromArray(positions, LR * 3, scratchCartesian3Position2);
  329. if (Cartographic.Cartesian3.equalsEpsilon(pl, pr, _Math.CesiumMath.EPSILON10)) {
  330. continue;
  331. }
  332. var UL = i + 1;
  333. var UR = i + 3;
  334. indices[edgeIndex++] = UL;
  335. indices[edgeIndex++] = LL;
  336. indices[edgeIndex++] = UL;
  337. indices[edgeIndex++] = UR;
  338. indices[edgeIndex++] = LL;
  339. indices[edgeIndex++] = LR;
  340. }
  341. indices[edgeIndex++] = numVertices - 2;
  342. indices[edgeIndex++] = numVertices - 1;
  343. return new GeometryAttribute.Geometry({
  344. attributes : attributes,
  345. indices : indices,
  346. primitiveType : PrimitiveType.PrimitiveType.LINES,
  347. boundingSphere : new BoundingSphere.BoundingSphere.fromVertices(positions)
  348. });
  349. };
  350. function createWallOutlineGeometry(wallGeometry, offset) {
  351. if (when.defined(offset)) {
  352. wallGeometry = WallOutlineGeometry.unpack(wallGeometry, offset);
  353. }
  354. wallGeometry._ellipsoid = Cartesian2.Ellipsoid.clone(wallGeometry._ellipsoid);
  355. return WallOutlineGeometry.createGeometry(wallGeometry);
  356. }
  357. return createWallOutlineGeometry;
  358. });