WallGeometry.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. import BoundingSphere from "./BoundingSphere.js";
  2. import Cartesian3 from "./Cartesian3.js";
  3. import ComponentDatatype from "./ComponentDatatype.js";
  4. import defaultValue from "./defaultValue.js";
  5. import defined from "./defined.js";
  6. import DeveloperError from "./DeveloperError.js";
  7. import Ellipsoid from "./Ellipsoid.js";
  8. import Geometry from "./Geometry.js";
  9. import GeometryAttribute from "./GeometryAttribute.js";
  10. import GeometryAttributes from "./GeometryAttributes.js";
  11. import IndexDatatype from "./IndexDatatype.js";
  12. import CesiumMath from "./Math.js";
  13. import PrimitiveType from "./PrimitiveType.js";
  14. import VertexFormat from "./VertexFormat.js";
  15. import WallGeometryLibrary from "./WallGeometryLibrary.js";
  16. var scratchCartesian3Position1 = new Cartesian3();
  17. var scratchCartesian3Position2 = new Cartesian3();
  18. var scratchCartesian3Position4 = new Cartesian3();
  19. var scratchCartesian3Position5 = new Cartesian3();
  20. var scratchBitangent = new Cartesian3();
  21. var scratchTangent = new Cartesian3();
  22. var scratchNormal = new Cartesian3();
  23. /**
  24. * A description of a wall, which is similar to a KML line string. A wall is defined by a series of points,
  25. * which extrude down to the ground. Optionally, they can extrude downwards to a specified height.
  26. *
  27. * @alias WallGeometry
  28. * @constructor
  29. *
  30. * @param {Object} options Object with the following properties:
  31. * @param {Cartesian3[]} options.positions An array of Cartesian objects, which are the points of the wall.
  32. * @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.
  33. * @param {Number[]} [options.maximumHeights] An array parallel to <code>positions</code> that give the maximum height of the
  34. * wall at <code>positions</code>. If undefined, the height of each position in used.
  35. * @param {Number[]} [options.minimumHeights] An array parallel to <code>positions</code> that give the minimum height of the
  36. * wall at <code>positions</code>. If undefined, the height at each position is 0.0.
  37. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid for coordinate manipulation
  38. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  39. *
  40. * @exception {DeveloperError} positions length must be greater than or equal to 2.
  41. * @exception {DeveloperError} positions and maximumHeights must have the same length.
  42. * @exception {DeveloperError} positions and minimumHeights must have the same length.
  43. *
  44. * @see WallGeometry#createGeometry
  45. * @see WallGeometry#fromConstantHeight
  46. *
  47. * @demo {@link https://sandcastle.cesium.com/index.html?src=Wall.html|Cesium Sandcastle Wall Demo}
  48. *
  49. * @example
  50. * // create a wall that spans from ground level to 10000 meters
  51. * var wall = new Cesium.WallGeometry({
  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.WallGeometry.createGeometry(wall);
  61. */
  62. function WallGeometry(options) {
  63. options = defaultValue(options, 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 (!defined(wallPositions)) {
  69. throw new DeveloperError("options.positions is required.");
  70. }
  71. if (
  72. defined(maximumHeights) &&
  73. maximumHeights.length !== wallPositions.length
  74. ) {
  75. throw new DeveloperError(
  76. "options.positions and options.maximumHeights must have the same length."
  77. );
  78. }
  79. if (
  80. defined(minimumHeights) &&
  81. minimumHeights.length !== wallPositions.length
  82. ) {
  83. throw new DeveloperError(
  84. "options.positions and options.minimumHeights must have the same length."
  85. );
  86. }
  87. //>>includeEnd('debug');
  88. var vertexFormat = defaultValue(options.vertexFormat, VertexFormat.DEFAULT);
  89. var granularity = defaultValue(
  90. options.granularity,
  91. CesiumMath.RADIANS_PER_DEGREE
  92. );
  93. var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  94. this._positions = wallPositions;
  95. this._minimumHeights = minimumHeights;
  96. this._maximumHeights = maximumHeights;
  97. this._vertexFormat = VertexFormat.clone(vertexFormat);
  98. this._granularity = granularity;
  99. this._ellipsoid = Ellipsoid.clone(ellipsoid);
  100. this._workerName = "createWallGeometry";
  101. var numComponents = 1 + wallPositions.length * Cartesian3.packedLength + 2;
  102. if (defined(minimumHeights)) {
  103. numComponents += minimumHeights.length;
  104. }
  105. if (defined(maximumHeights)) {
  106. numComponents += maximumHeights.length;
  107. }
  108. /**
  109. * The number of elements used to pack the object into an array.
  110. * @type {Number}
  111. */
  112. this.packedLength =
  113. numComponents + Ellipsoid.packedLength + VertexFormat.packedLength + 1;
  114. }
  115. /**
  116. * Stores the provided instance into the provided array.
  117. *
  118. * @param {WallGeometry} value The value to pack.
  119. * @param {Number[]} array The array to pack into.
  120. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  121. *
  122. * @returns {Number[]} The array that was packed into
  123. */
  124. WallGeometry.pack = function (value, array, startingIndex) {
  125. //>>includeStart('debug', pragmas.debug);
  126. if (!defined(value)) {
  127. throw new DeveloperError("value is required");
  128. }
  129. if (!defined(array)) {
  130. throw new DeveloperError("array is required");
  131. }
  132. //>>includeEnd('debug');
  133. startingIndex = defaultValue(startingIndex, 0);
  134. var i;
  135. var positions = value._positions;
  136. var length = positions.length;
  137. array[startingIndex++] = length;
  138. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  139. Cartesian3.pack(positions[i], array, startingIndex);
  140. }
  141. var minimumHeights = value._minimumHeights;
  142. length = defined(minimumHeights) ? minimumHeights.length : 0;
  143. array[startingIndex++] = length;
  144. if (defined(minimumHeights)) {
  145. for (i = 0; i < length; ++i) {
  146. array[startingIndex++] = minimumHeights[i];
  147. }
  148. }
  149. var maximumHeights = value._maximumHeights;
  150. length = defined(maximumHeights) ? maximumHeights.length : 0;
  151. array[startingIndex++] = length;
  152. if (defined(maximumHeights)) {
  153. for (i = 0; i < length; ++i) {
  154. array[startingIndex++] = maximumHeights[i];
  155. }
  156. }
  157. Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  158. startingIndex += Ellipsoid.packedLength;
  159. VertexFormat.pack(value._vertexFormat, array, startingIndex);
  160. startingIndex += VertexFormat.packedLength;
  161. array[startingIndex] = value._granularity;
  162. return array;
  163. };
  164. var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  165. var scratchVertexFormat = new VertexFormat();
  166. var scratchOptions = {
  167. positions: undefined,
  168. minimumHeights: undefined,
  169. maximumHeights: undefined,
  170. ellipsoid: scratchEllipsoid,
  171. vertexFormat: scratchVertexFormat,
  172. granularity: undefined,
  173. };
  174. /**
  175. * Retrieves an instance from a packed array.
  176. *
  177. * @param {Number[]} array The packed array.
  178. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  179. * @param {WallGeometry} [result] The object into which to store the result.
  180. * @returns {WallGeometry} The modified result parameter or a new WallGeometry instance if one was not provided.
  181. */
  182. WallGeometry.unpack = function (array, startingIndex, result) {
  183. //>>includeStart('debug', pragmas.debug);
  184. if (!defined(array)) {
  185. throw new DeveloperError("array is required");
  186. }
  187. //>>includeEnd('debug');
  188. startingIndex = defaultValue(startingIndex, 0);
  189. var i;
  190. var length = array[startingIndex++];
  191. var positions = new Array(length);
  192. for (i = 0; i < length; ++i, startingIndex += Cartesian3.packedLength) {
  193. positions[i] = Cartesian3.unpack(array, startingIndex);
  194. }
  195. length = array[startingIndex++];
  196. var minimumHeights;
  197. if (length > 0) {
  198. minimumHeights = new Array(length);
  199. for (i = 0; i < length; ++i) {
  200. minimumHeights[i] = array[startingIndex++];
  201. }
  202. }
  203. length = array[startingIndex++];
  204. var maximumHeights;
  205. if (length > 0) {
  206. maximumHeights = new Array(length);
  207. for (i = 0; i < length; ++i) {
  208. maximumHeights[i] = array[startingIndex++];
  209. }
  210. }
  211. var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  212. startingIndex += Ellipsoid.packedLength;
  213. var vertexFormat = VertexFormat.unpack(
  214. array,
  215. startingIndex,
  216. scratchVertexFormat
  217. );
  218. startingIndex += VertexFormat.packedLength;
  219. var granularity = array[startingIndex];
  220. if (!defined(result)) {
  221. scratchOptions.positions = positions;
  222. scratchOptions.minimumHeights = minimumHeights;
  223. scratchOptions.maximumHeights = maximumHeights;
  224. scratchOptions.granularity = granularity;
  225. return new WallGeometry(scratchOptions);
  226. }
  227. result._positions = positions;
  228. result._minimumHeights = minimumHeights;
  229. result._maximumHeights = maximumHeights;
  230. result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
  231. result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
  232. result._granularity = granularity;
  233. return result;
  234. };
  235. /**
  236. * A description of a wall, which is similar to a KML line string. A wall is defined by a series of points,
  237. * which extrude down to the ground. Optionally, they can extrude downwards to a specified height.
  238. *
  239. * @param {Object} options Object with the following properties:
  240. * @param {Cartesian3[]} options.positions An array of Cartesian objects, which are the points of the wall.
  241. * @param {Number} [options.maximumHeight] A constant that defines the maximum height of the
  242. * wall at <code>positions</code>. If undefined, the height of each position in used.
  243. * @param {Number} [options.minimumHeight] A constant that defines the minimum height of the
  244. * wall at <code>positions</code>. If undefined, the height at each position is 0.0.
  245. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid for coordinate manipulation
  246. * @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
  247. * @returns {WallGeometry}
  248. *
  249. *
  250. * @example
  251. * // create a wall that spans from 10000 meters to 20000 meters
  252. * var wall = Cesium.WallGeometry.fromConstantHeights({
  253. * positions : Cesium.Cartesian3.fromDegreesArray([
  254. * 19.0, 47.0,
  255. * 19.0, 48.0,
  256. * 20.0, 48.0,
  257. * 20.0, 47.0,
  258. * 19.0, 47.0,
  259. * ]),
  260. * minimumHeight : 20000.0,
  261. * maximumHeight : 10000.0
  262. * });
  263. * var geometry = Cesium.WallGeometry.createGeometry(wall);
  264. *
  265. * @see WallGeometry#createGeometry
  266. */
  267. WallGeometry.fromConstantHeights = function (options) {
  268. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  269. var positions = options.positions;
  270. //>>includeStart('debug', pragmas.debug);
  271. if (!defined(positions)) {
  272. throw new DeveloperError("options.positions is required.");
  273. }
  274. //>>includeEnd('debug');
  275. var minHeights;
  276. var maxHeights;
  277. var min = options.minimumHeight;
  278. var max = options.maximumHeight;
  279. var doMin = defined(min);
  280. var doMax = defined(max);
  281. if (doMin || doMax) {
  282. var length = positions.length;
  283. minHeights = doMin ? new Array(length) : undefined;
  284. maxHeights = doMax ? new Array(length) : undefined;
  285. for (var i = 0; i < length; ++i) {
  286. if (doMin) {
  287. minHeights[i] = min;
  288. }
  289. if (doMax) {
  290. maxHeights[i] = max;
  291. }
  292. }
  293. }
  294. var newOptions = {
  295. positions: positions,
  296. maximumHeights: maxHeights,
  297. minimumHeights: minHeights,
  298. ellipsoid: options.ellipsoid,
  299. vertexFormat: options.vertexFormat,
  300. };
  301. return new WallGeometry(newOptions);
  302. };
  303. /**
  304. * Computes the geometric representation of a wall, including its vertices, indices, and a bounding sphere.
  305. *
  306. * @param {WallGeometry} wallGeometry A description of the wall.
  307. * @returns {Geometry|undefined} The computed vertices and indices.
  308. */
  309. WallGeometry.createGeometry = function (wallGeometry) {
  310. var wallPositions = wallGeometry._positions;
  311. var minimumHeights = wallGeometry._minimumHeights;
  312. var maximumHeights = wallGeometry._maximumHeights;
  313. var vertexFormat = wallGeometry._vertexFormat;
  314. var granularity = wallGeometry._granularity;
  315. var ellipsoid = wallGeometry._ellipsoid;
  316. var pos = WallGeometryLibrary.computePositions(
  317. ellipsoid,
  318. wallPositions,
  319. maximumHeights,
  320. minimumHeights,
  321. granularity,
  322. true
  323. );
  324. if (!defined(pos)) {
  325. return;
  326. }
  327. var bottomPositions = pos.bottomPositions;
  328. var topPositions = pos.topPositions;
  329. var numCorners = pos.numCorners;
  330. var length = topPositions.length;
  331. var size = length * 2;
  332. var positions = vertexFormat.position ? new Float64Array(size) : undefined;
  333. var normals = vertexFormat.normal ? new Float32Array(size) : undefined;
  334. var tangents = vertexFormat.tangent ? new Float32Array(size) : undefined;
  335. var bitangents = vertexFormat.bitangent ? new Float32Array(size) : undefined;
  336. var textureCoordinates = vertexFormat.st
  337. ? new Float32Array((size / 3) * 2)
  338. : undefined;
  339. var positionIndex = 0;
  340. var normalIndex = 0;
  341. var bitangentIndex = 0;
  342. var tangentIndex = 0;
  343. var stIndex = 0;
  344. // add lower and upper points one after the other, lower
  345. // points being even and upper points being odd
  346. var normal = scratchNormal;
  347. var tangent = scratchTangent;
  348. var bitangent = scratchBitangent;
  349. var recomputeNormal = true;
  350. length /= 3;
  351. var i;
  352. var s = 0;
  353. var ds = 1 / (length - numCorners - 1);
  354. for (i = 0; i < length; ++i) {
  355. var i3 = i * 3;
  356. var topPosition = Cartesian3.fromArray(
  357. topPositions,
  358. i3,
  359. scratchCartesian3Position1
  360. );
  361. var bottomPosition = Cartesian3.fromArray(
  362. bottomPositions,
  363. i3,
  364. scratchCartesian3Position2
  365. );
  366. if (vertexFormat.position) {
  367. // insert the lower point
  368. positions[positionIndex++] = bottomPosition.x;
  369. positions[positionIndex++] = bottomPosition.y;
  370. positions[positionIndex++] = bottomPosition.z;
  371. // insert the upper point
  372. positions[positionIndex++] = topPosition.x;
  373. positions[positionIndex++] = topPosition.y;
  374. positions[positionIndex++] = topPosition.z;
  375. }
  376. if (vertexFormat.st) {
  377. textureCoordinates[stIndex++] = s;
  378. textureCoordinates[stIndex++] = 0.0;
  379. textureCoordinates[stIndex++] = s;
  380. textureCoordinates[stIndex++] = 1.0;
  381. }
  382. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
  383. var nextTop = Cartesian3.clone(
  384. Cartesian3.ZERO,
  385. scratchCartesian3Position5
  386. );
  387. var groundPosition = Cartesian3.subtract(
  388. topPosition,
  389. ellipsoid.geodeticSurfaceNormal(
  390. topPosition,
  391. scratchCartesian3Position2
  392. ),
  393. scratchCartesian3Position2
  394. );
  395. if (i + 1 < length) {
  396. nextTop = Cartesian3.fromArray(
  397. topPositions,
  398. i3 + 3,
  399. scratchCartesian3Position5
  400. );
  401. }
  402. if (recomputeNormal) {
  403. var scalednextPosition = Cartesian3.subtract(
  404. nextTop,
  405. topPosition,
  406. scratchCartesian3Position4
  407. );
  408. var scaledGroundPosition = Cartesian3.subtract(
  409. groundPosition,
  410. topPosition,
  411. scratchCartesian3Position1
  412. );
  413. normal = Cartesian3.normalize(
  414. Cartesian3.cross(scaledGroundPosition, scalednextPosition, normal),
  415. normal
  416. );
  417. recomputeNormal = false;
  418. }
  419. if (
  420. Cartesian3.equalsEpsilon(topPosition, nextTop, CesiumMath.EPSILON10)
  421. ) {
  422. recomputeNormal = true;
  423. } else {
  424. s += ds;
  425. if (vertexFormat.tangent) {
  426. tangent = Cartesian3.normalize(
  427. Cartesian3.subtract(nextTop, topPosition, tangent),
  428. tangent
  429. );
  430. }
  431. if (vertexFormat.bitangent) {
  432. bitangent = Cartesian3.normalize(
  433. Cartesian3.cross(normal, tangent, bitangent),
  434. bitangent
  435. );
  436. }
  437. }
  438. if (vertexFormat.normal) {
  439. normals[normalIndex++] = normal.x;
  440. normals[normalIndex++] = normal.y;
  441. normals[normalIndex++] = normal.z;
  442. normals[normalIndex++] = normal.x;
  443. normals[normalIndex++] = normal.y;
  444. normals[normalIndex++] = normal.z;
  445. }
  446. if (vertexFormat.tangent) {
  447. tangents[tangentIndex++] = tangent.x;
  448. tangents[tangentIndex++] = tangent.y;
  449. tangents[tangentIndex++] = tangent.z;
  450. tangents[tangentIndex++] = tangent.x;
  451. tangents[tangentIndex++] = tangent.y;
  452. tangents[tangentIndex++] = tangent.z;
  453. }
  454. if (vertexFormat.bitangent) {
  455. bitangents[bitangentIndex++] = bitangent.x;
  456. bitangents[bitangentIndex++] = bitangent.y;
  457. bitangents[bitangentIndex++] = bitangent.z;
  458. bitangents[bitangentIndex++] = bitangent.x;
  459. bitangents[bitangentIndex++] = bitangent.y;
  460. bitangents[bitangentIndex++] = bitangent.z;
  461. }
  462. }
  463. }
  464. var attributes = new GeometryAttributes();
  465. if (vertexFormat.position) {
  466. attributes.position = new GeometryAttribute({
  467. componentDatatype: ComponentDatatype.DOUBLE,
  468. componentsPerAttribute: 3,
  469. values: positions,
  470. });
  471. }
  472. if (vertexFormat.normal) {
  473. attributes.normal = new GeometryAttribute({
  474. componentDatatype: ComponentDatatype.FLOAT,
  475. componentsPerAttribute: 3,
  476. values: normals,
  477. });
  478. }
  479. if (vertexFormat.tangent) {
  480. attributes.tangent = new GeometryAttribute({
  481. componentDatatype: ComponentDatatype.FLOAT,
  482. componentsPerAttribute: 3,
  483. values: tangents,
  484. });
  485. }
  486. if (vertexFormat.bitangent) {
  487. attributes.bitangent = new GeometryAttribute({
  488. componentDatatype: ComponentDatatype.FLOAT,
  489. componentsPerAttribute: 3,
  490. values: bitangents,
  491. });
  492. }
  493. if (vertexFormat.st) {
  494. attributes.st = new GeometryAttribute({
  495. componentDatatype: ComponentDatatype.FLOAT,
  496. componentsPerAttribute: 2,
  497. values: textureCoordinates,
  498. });
  499. }
  500. // prepare the side walls, two triangles for each wall
  501. //
  502. // A (i+1) B (i+3) E
  503. // +--------+-------+
  504. // | / | /| triangles: A C B
  505. // | / | / | B C D
  506. // | / | / |
  507. // | / | / |
  508. // | / | / |
  509. // | / | / |
  510. // +--------+-------+
  511. // C (i) D (i+2) F
  512. //
  513. var numVertices = size / 3;
  514. size -= 6 * (numCorners + 1);
  515. var indices = IndexDatatype.createTypedArray(numVertices, size);
  516. var edgeIndex = 0;
  517. for (i = 0; i < numVertices - 2; i += 2) {
  518. var LL = i;
  519. var LR = i + 2;
  520. var pl = Cartesian3.fromArray(
  521. positions,
  522. LL * 3,
  523. scratchCartesian3Position1
  524. );
  525. var pr = Cartesian3.fromArray(
  526. positions,
  527. LR * 3,
  528. scratchCartesian3Position2
  529. );
  530. if (Cartesian3.equalsEpsilon(pl, pr, CesiumMath.EPSILON10)) {
  531. continue;
  532. }
  533. var UL = i + 1;
  534. var UR = i + 3;
  535. indices[edgeIndex++] = UL;
  536. indices[edgeIndex++] = LL;
  537. indices[edgeIndex++] = UR;
  538. indices[edgeIndex++] = UR;
  539. indices[edgeIndex++] = LL;
  540. indices[edgeIndex++] = LR;
  541. }
  542. return new Geometry({
  543. attributes: attributes,
  544. indices: indices,
  545. primitiveType: PrimitiveType.TRIANGLES,
  546. boundingSphere: new BoundingSphere.fromVertices(positions),
  547. });
  548. };
  549. export default WallGeometry;