createWallOutlineGeometry.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/main/LICENSE.md for full licensing details.
  22. */
  23. define(['./defaultValue-81eec7ed', './Matrix2-d35cf4b5', './Transforms-f0a54c7b', './ComponentDatatype-9e86ac8f', './RuntimeError-8952249c', './GeometryAttribute-eeb38987', './GeometryAttributes-32b29525', './IndexDatatype-bed3935d', './WallGeometryLibrary-cb884ccb', './_commonjsHelpers-3aae1032-26891ab7', './combine-3c023bda', './WebGLConstants-508b9636', './arrayRemoveDuplicates-1a15bd09', './PolylinePipeline-3b5d6486', './EllipsoidGeodesic-924f7301', './EllipsoidRhumbLine-d049f903', './IntersectionTests-a25e058d', './Plane-24f22488'], (function (defaultValue, Matrix2, Transforms, ComponentDatatype, RuntimeError, GeometryAttribute, GeometryAttributes, IndexDatatype, WallGeometryLibrary, _commonjsHelpers3aae1032, combine, WebGLConstants, arrayRemoveDuplicates, PolylinePipeline, EllipsoidGeodesic, EllipsoidRhumbLine, IntersectionTests, Plane) { 'use strict';
  24. const scratchCartesian3Position1 = new Matrix2.Cartesian3();
  25. const scratchCartesian3Position2 = new Matrix2.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. * const 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. * const geometry = Cesium.WallOutlineGeometry.createGeometry(wall);
  61. */
  62. function WallOutlineGeometry(options) {
  63. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  64. const wallPositions = options.positions;
  65. const maximumHeights = options.maximumHeights;
  66. const minimumHeights = options.minimumHeights;
  67. //>>includeStart('debug', pragmas.debug);
  68. if (!defaultValue.defined(wallPositions)) {
  69. throw new RuntimeError.DeveloperError("options.positions is required.");
  70. }
  71. if (
  72. defaultValue.defined(maximumHeights) &&
  73. maximumHeights.length !== wallPositions.length
  74. ) {
  75. throw new RuntimeError.DeveloperError(
  76. "options.positions and options.maximumHeights must have the same length."
  77. );
  78. }
  79. if (
  80. defaultValue.defined(minimumHeights) &&
  81. minimumHeights.length !== wallPositions.length
  82. ) {
  83. throw new RuntimeError.DeveloperError(
  84. "options.positions and options.minimumHeights must have the same length."
  85. );
  86. }
  87. //>>includeEnd('debug');
  88. const granularity = defaultValue.defaultValue(
  89. options.granularity,
  90. ComponentDatatype.CesiumMath.RADIANS_PER_DEGREE
  91. );
  92. const ellipsoid = defaultValue.defaultValue(options.ellipsoid, Matrix2.Ellipsoid.WGS84);
  93. this._positions = wallPositions;
  94. this._minimumHeights = minimumHeights;
  95. this._maximumHeights = maximumHeights;
  96. this._granularity = granularity;
  97. this._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid);
  98. this._workerName = "createWallOutlineGeometry";
  99. let numComponents = 1 + wallPositions.length * Matrix2.Cartesian3.packedLength + 2;
  100. if (defaultValue.defined(minimumHeights)) {
  101. numComponents += minimumHeights.length;
  102. }
  103. if (defaultValue.defined(maximumHeights)) {
  104. numComponents += maximumHeights.length;
  105. }
  106. /**
  107. * The number of elements used to pack the object into an array.
  108. * @type {Number}
  109. */
  110. this.packedLength = numComponents + Matrix2.Ellipsoid.packedLength + 1;
  111. }
  112. /**
  113. * Stores the provided instance into the provided array.
  114. *
  115. * @param {WallOutlineGeometry} value The value to pack.
  116. * @param {Number[]} array The array to pack into.
  117. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  118. *
  119. * @returns {Number[]} The array that was packed into
  120. */
  121. WallOutlineGeometry.pack = function (value, array, startingIndex) {
  122. //>>includeStart('debug', pragmas.debug);
  123. if (!defaultValue.defined(value)) {
  124. throw new RuntimeError.DeveloperError("value is required");
  125. }
  126. if (!defaultValue.defined(array)) {
  127. throw new RuntimeError.DeveloperError("array is required");
  128. }
  129. //>>includeEnd('debug');
  130. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  131. let i;
  132. const positions = value._positions;
  133. let length = positions.length;
  134. array[startingIndex++] = length;
  135. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  136. Matrix2.Cartesian3.pack(positions[i], array, startingIndex);
  137. }
  138. const minimumHeights = value._minimumHeights;
  139. length = defaultValue.defined(minimumHeights) ? minimumHeights.length : 0;
  140. array[startingIndex++] = length;
  141. if (defaultValue.defined(minimumHeights)) {
  142. for (i = 0; i < length; ++i) {
  143. array[startingIndex++] = minimumHeights[i];
  144. }
  145. }
  146. const maximumHeights = value._maximumHeights;
  147. length = defaultValue.defined(maximumHeights) ? maximumHeights.length : 0;
  148. array[startingIndex++] = length;
  149. if (defaultValue.defined(maximumHeights)) {
  150. for (i = 0; i < length; ++i) {
  151. array[startingIndex++] = maximumHeights[i];
  152. }
  153. }
  154. Matrix2.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  155. startingIndex += Matrix2.Ellipsoid.packedLength;
  156. array[startingIndex] = value._granularity;
  157. return array;
  158. };
  159. const scratchEllipsoid = Matrix2.Ellipsoid.clone(Matrix2.Ellipsoid.UNIT_SPHERE);
  160. const scratchOptions = {
  161. positions: undefined,
  162. minimumHeights: undefined,
  163. maximumHeights: undefined,
  164. ellipsoid: scratchEllipsoid,
  165. granularity: undefined,
  166. };
  167. /**
  168. * Retrieves an instance from a packed array.
  169. *
  170. * @param {Number[]} array The packed array.
  171. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  172. * @param {WallOutlineGeometry} [result] The object into which to store the result.
  173. * @returns {WallOutlineGeometry} The modified result parameter or a new WallOutlineGeometry instance if one was not provided.
  174. */
  175. WallOutlineGeometry.unpack = function (array, startingIndex, result) {
  176. //>>includeStart('debug', pragmas.debug);
  177. if (!defaultValue.defined(array)) {
  178. throw new RuntimeError.DeveloperError("array is required");
  179. }
  180. //>>includeEnd('debug');
  181. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  182. let i;
  183. let length = array[startingIndex++];
  184. const positions = new Array(length);
  185. for (i = 0; i < length; ++i, startingIndex += Matrix2.Cartesian3.packedLength) {
  186. positions[i] = Matrix2.Cartesian3.unpack(array, startingIndex);
  187. }
  188. length = array[startingIndex++];
  189. let minimumHeights;
  190. if (length > 0) {
  191. minimumHeights = new Array(length);
  192. for (i = 0; i < length; ++i) {
  193. minimumHeights[i] = array[startingIndex++];
  194. }
  195. }
  196. length = array[startingIndex++];
  197. let maximumHeights;
  198. if (length > 0) {
  199. maximumHeights = new Array(length);
  200. for (i = 0; i < length; ++i) {
  201. maximumHeights[i] = array[startingIndex++];
  202. }
  203. }
  204. const ellipsoid = Matrix2.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  205. startingIndex += Matrix2.Ellipsoid.packedLength;
  206. const granularity = array[startingIndex];
  207. if (!defaultValue.defined(result)) {
  208. scratchOptions.positions = positions;
  209. scratchOptions.minimumHeights = minimumHeights;
  210. scratchOptions.maximumHeights = maximumHeights;
  211. scratchOptions.granularity = granularity;
  212. return new WallOutlineGeometry(scratchOptions);
  213. }
  214. result._positions = positions;
  215. result._minimumHeights = minimumHeights;
  216. result._maximumHeights = maximumHeights;
  217. result._ellipsoid = Matrix2.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  218. result._granularity = granularity;
  219. return result;
  220. };
  221. /**
  222. * A description of a walloutline. A wall is defined by a series of points,
  223. * which extrude down to the ground. Optionally, they can extrude downwards to a specified height.
  224. *
  225. * @param {Object} options Object with the following properties:
  226. * @param {Cartesian3[]} options.positions An array of Cartesian objects, which are the points of the wall.
  227. * @param {Number} [options.maximumHeight] A constant that defines the maximum height of the
  228. * wall at <code>positions</code>. If undefined, the height of each position in used.
  229. * @param {Number} [options.minimumHeight] A constant that defines the minimum height of the
  230. * wall at <code>positions</code>. If undefined, the height at each position is 0.0.
  231. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid for coordinate manipulation
  232. * @returns {WallOutlineGeometry}
  233. *
  234. *
  235. * @example
  236. * // create a wall that spans from 10000 meters to 20000 meters
  237. * const wall = Cesium.WallOutlineGeometry.fromConstantHeights({
  238. * positions : Cesium.Cartesian3.fromDegreesArray([
  239. * 19.0, 47.0,
  240. * 19.0, 48.0,
  241. * 20.0, 48.0,
  242. * 20.0, 47.0,
  243. * 19.0, 47.0,
  244. * ]),
  245. * minimumHeight : 20000.0,
  246. * maximumHeight : 10000.0
  247. * });
  248. * const geometry = Cesium.WallOutlineGeometry.createGeometry(wall);
  249. *
  250. * @see WallOutlineGeometry#createGeometry
  251. */
  252. WallOutlineGeometry.fromConstantHeights = function (options) {
  253. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  254. const positions = options.positions;
  255. //>>includeStart('debug', pragmas.debug);
  256. if (!defaultValue.defined(positions)) {
  257. throw new RuntimeError.DeveloperError("options.positions is required.");
  258. }
  259. //>>includeEnd('debug');
  260. let minHeights;
  261. let maxHeights;
  262. const min = options.minimumHeight;
  263. const max = options.maximumHeight;
  264. const doMin = defaultValue.defined(min);
  265. const doMax = defaultValue.defined(max);
  266. if (doMin || doMax) {
  267. const length = positions.length;
  268. minHeights = doMin ? new Array(length) : undefined;
  269. maxHeights = doMax ? new Array(length) : undefined;
  270. for (let i = 0; i < length; ++i) {
  271. if (doMin) {
  272. minHeights[i] = min;
  273. }
  274. if (doMax) {
  275. maxHeights[i] = max;
  276. }
  277. }
  278. }
  279. const newOptions = {
  280. positions: positions,
  281. maximumHeights: maxHeights,
  282. minimumHeights: minHeights,
  283. ellipsoid: options.ellipsoid,
  284. };
  285. return new WallOutlineGeometry(newOptions);
  286. };
  287. /**
  288. * Computes the geometric representation of a wall outline, including its vertices, indices, and a bounding sphere.
  289. *
  290. * @param {WallOutlineGeometry} wallGeometry A description of the wall outline.
  291. * @returns {Geometry|undefined} The computed vertices and indices.
  292. */
  293. WallOutlineGeometry.createGeometry = function (wallGeometry) {
  294. const wallPositions = wallGeometry._positions;
  295. const minimumHeights = wallGeometry._minimumHeights;
  296. const maximumHeights = wallGeometry._maximumHeights;
  297. const granularity = wallGeometry._granularity;
  298. const ellipsoid = wallGeometry._ellipsoid;
  299. const pos = WallGeometryLibrary.WallGeometryLibrary.computePositions(
  300. ellipsoid,
  301. wallPositions,
  302. maximumHeights,
  303. minimumHeights,
  304. granularity,
  305. false
  306. );
  307. if (!defaultValue.defined(pos)) {
  308. return;
  309. }
  310. const bottomPositions = pos.bottomPositions;
  311. const topPositions = pos.topPositions;
  312. let length = topPositions.length;
  313. let size = length * 2;
  314. const positions = new Float64Array(size);
  315. let positionIndex = 0;
  316. // add lower and upper points one after the other, lower
  317. // points being even and upper points being odd
  318. length /= 3;
  319. let i;
  320. for (i = 0; i < length; ++i) {
  321. const i3 = i * 3;
  322. const topPosition = Matrix2.Cartesian3.fromArray(
  323. topPositions,
  324. i3,
  325. scratchCartesian3Position1
  326. );
  327. const bottomPosition = Matrix2.Cartesian3.fromArray(
  328. bottomPositions,
  329. i3,
  330. scratchCartesian3Position2
  331. );
  332. // insert the lower point
  333. positions[positionIndex++] = bottomPosition.x;
  334. positions[positionIndex++] = bottomPosition.y;
  335. positions[positionIndex++] = bottomPosition.z;
  336. // insert the upper point
  337. positions[positionIndex++] = topPosition.x;
  338. positions[positionIndex++] = topPosition.y;
  339. positions[positionIndex++] = topPosition.z;
  340. }
  341. const attributes = new GeometryAttributes.GeometryAttributes({
  342. position: new GeometryAttribute.GeometryAttribute({
  343. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  344. componentsPerAttribute: 3,
  345. values: positions,
  346. }),
  347. });
  348. const numVertices = size / 3;
  349. size = 2 * numVertices - 4 + numVertices;
  350. const indices = IndexDatatype.IndexDatatype.createTypedArray(numVertices, size);
  351. let edgeIndex = 0;
  352. for (i = 0; i < numVertices - 2; i += 2) {
  353. const LL = i;
  354. const LR = i + 2;
  355. const pl = Matrix2.Cartesian3.fromArray(
  356. positions,
  357. LL * 3,
  358. scratchCartesian3Position1
  359. );
  360. const pr = Matrix2.Cartesian3.fromArray(
  361. positions,
  362. LR * 3,
  363. scratchCartesian3Position2
  364. );
  365. if (Matrix2.Cartesian3.equalsEpsilon(pl, pr, ComponentDatatype.CesiumMath.EPSILON10)) {
  366. continue;
  367. }
  368. const UL = i + 1;
  369. const UR = i + 3;
  370. indices[edgeIndex++] = UL;
  371. indices[edgeIndex++] = LL;
  372. indices[edgeIndex++] = UL;
  373. indices[edgeIndex++] = UR;
  374. indices[edgeIndex++] = LL;
  375. indices[edgeIndex++] = LR;
  376. }
  377. indices[edgeIndex++] = numVertices - 2;
  378. indices[edgeIndex++] = numVertices - 1;
  379. return new GeometryAttribute.Geometry({
  380. attributes: attributes,
  381. indices: indices,
  382. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  383. boundingSphere: new Transforms.BoundingSphere.fromVertices(positions),
  384. });
  385. };
  386. function createWallOutlineGeometry(wallGeometry, offset) {
  387. if (defaultValue.defined(offset)) {
  388. wallGeometry = WallOutlineGeometry.unpack(wallGeometry, offset);
  389. }
  390. wallGeometry._ellipsoid = Matrix2.Ellipsoid.clone(wallGeometry._ellipsoid);
  391. return WallOutlineGeometry.createGeometry(wallGeometry);
  392. }
  393. return createWallOutlineGeometry;
  394. }));
  395. //# sourceMappingURL=createWallOutlineGeometry.js.map