WallGeometry.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. const scratchCartesian3Position1 = new Cartesian3();
  17. const scratchCartesian3Position2 = new Cartesian3();
  18. const scratchCartesian3Position4 = new Cartesian3();
  19. const scratchCartesian3Position5 = new Cartesian3();
  20. const scratchBitangent = new Cartesian3();
  21. const scratchTangent = new Cartesian3();
  22. const 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. * const 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. * const geometry = Cesium.WallGeometry.createGeometry(wall);
  61. */
  62. function WallGeometry(options) {
  63. options = defaultValue(options, 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 (!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. const vertexFormat = defaultValue(options.vertexFormat, VertexFormat.DEFAULT);
  89. const granularity = defaultValue(
  90. options.granularity,
  91. CesiumMath.RADIANS_PER_DEGREE
  92. );
  93. const 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. let 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. let i;
  135. const positions = value._positions;
  136. let 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. const 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. const 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. const scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
  165. const scratchVertexFormat = new VertexFormat();
  166. const 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. let i;
  190. let length = array[startingIndex++];
  191. const 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. let 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. let 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. const ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  212. startingIndex += Ellipsoid.packedLength;
  213. const vertexFormat = VertexFormat.unpack(
  214. array,
  215. startingIndex,
  216. scratchVertexFormat
  217. );
  218. startingIndex += VertexFormat.packedLength;
  219. const 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. * const 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. * const geometry = Cesium.WallGeometry.createGeometry(wall);
  264. *
  265. * @see WallGeometry#createGeometry
  266. */
  267. WallGeometry.fromConstantHeights = function (options) {
  268. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  269. const 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. let minHeights;
  276. let maxHeights;
  277. const min = options.minimumHeight;
  278. const max = options.maximumHeight;
  279. const doMin = defined(min);
  280. const doMax = defined(max);
  281. if (doMin || doMax) {
  282. const length = positions.length;
  283. minHeights = doMin ? new Array(length) : undefined;
  284. maxHeights = doMax ? new Array(length) : undefined;
  285. for (let i = 0; i < length; ++i) {
  286. if (doMin) {
  287. minHeights[i] = min;
  288. }
  289. if (doMax) {
  290. maxHeights[i] = max;
  291. }
  292. }
  293. }
  294. const 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. const wallPositions = wallGeometry._positions;
  311. const minimumHeights = wallGeometry._minimumHeights;
  312. const maximumHeights = wallGeometry._maximumHeights;
  313. const vertexFormat = wallGeometry._vertexFormat;
  314. const granularity = wallGeometry._granularity;
  315. const ellipsoid = wallGeometry._ellipsoid;
  316. const pos = WallGeometryLibrary.computePositions(
  317. ellipsoid,
  318. wallPositions,
  319. maximumHeights,
  320. minimumHeights,
  321. granularity,
  322. true
  323. );
  324. if (!defined(pos)) {
  325. return;
  326. }
  327. const bottomPositions = pos.bottomPositions;
  328. const topPositions = pos.topPositions;
  329. const numCorners = pos.numCorners;
  330. let length = topPositions.length;
  331. let size = length * 2;
  332. const positions = vertexFormat.position ? new Float64Array(size) : undefined;
  333. const normals = vertexFormat.normal ? new Float32Array(size) : undefined;
  334. const tangents = vertexFormat.tangent ? new Float32Array(size) : undefined;
  335. const bitangents = vertexFormat.bitangent
  336. ? new Float32Array(size)
  337. : undefined;
  338. const textureCoordinates = vertexFormat.st
  339. ? new Float32Array((size / 3) * 2)
  340. : undefined;
  341. let positionIndex = 0;
  342. let normalIndex = 0;
  343. let bitangentIndex = 0;
  344. let tangentIndex = 0;
  345. let stIndex = 0;
  346. // add lower and upper points one after the other, lower
  347. // points being even and upper points being odd
  348. let normal = scratchNormal;
  349. let tangent = scratchTangent;
  350. let bitangent = scratchBitangent;
  351. let recomputeNormal = true;
  352. length /= 3;
  353. let i;
  354. let s = 0;
  355. const ds = 1 / (length - numCorners - 1);
  356. for (i = 0; i < length; ++i) {
  357. const i3 = i * 3;
  358. const topPosition = Cartesian3.fromArray(
  359. topPositions,
  360. i3,
  361. scratchCartesian3Position1
  362. );
  363. const bottomPosition = Cartesian3.fromArray(
  364. bottomPositions,
  365. i3,
  366. scratchCartesian3Position2
  367. );
  368. if (vertexFormat.position) {
  369. // insert the lower point
  370. positions[positionIndex++] = bottomPosition.x;
  371. positions[positionIndex++] = bottomPosition.y;
  372. positions[positionIndex++] = bottomPosition.z;
  373. // insert the upper point
  374. positions[positionIndex++] = topPosition.x;
  375. positions[positionIndex++] = topPosition.y;
  376. positions[positionIndex++] = topPosition.z;
  377. }
  378. if (vertexFormat.st) {
  379. textureCoordinates[stIndex++] = s;
  380. textureCoordinates[stIndex++] = 0.0;
  381. textureCoordinates[stIndex++] = s;
  382. textureCoordinates[stIndex++] = 1.0;
  383. }
  384. if (vertexFormat.normal || vertexFormat.tangent || vertexFormat.bitangent) {
  385. let nextTop = Cartesian3.clone(
  386. Cartesian3.ZERO,
  387. scratchCartesian3Position5
  388. );
  389. const groundPosition = Cartesian3.subtract(
  390. topPosition,
  391. ellipsoid.geodeticSurfaceNormal(
  392. topPosition,
  393. scratchCartesian3Position2
  394. ),
  395. scratchCartesian3Position2
  396. );
  397. if (i + 1 < length) {
  398. nextTop = Cartesian3.fromArray(
  399. topPositions,
  400. i3 + 3,
  401. scratchCartesian3Position5
  402. );
  403. }
  404. if (recomputeNormal) {
  405. const scalednextPosition = Cartesian3.subtract(
  406. nextTop,
  407. topPosition,
  408. scratchCartesian3Position4
  409. );
  410. const scaledGroundPosition = Cartesian3.subtract(
  411. groundPosition,
  412. topPosition,
  413. scratchCartesian3Position1
  414. );
  415. normal = Cartesian3.normalize(
  416. Cartesian3.cross(scaledGroundPosition, scalednextPosition, normal),
  417. normal
  418. );
  419. recomputeNormal = false;
  420. }
  421. if (
  422. Cartesian3.equalsEpsilon(topPosition, nextTop, CesiumMath.EPSILON10)
  423. ) {
  424. recomputeNormal = true;
  425. } else {
  426. s += ds;
  427. if (vertexFormat.tangent) {
  428. tangent = Cartesian3.normalize(
  429. Cartesian3.subtract(nextTop, topPosition, tangent),
  430. tangent
  431. );
  432. }
  433. if (vertexFormat.bitangent) {
  434. bitangent = Cartesian3.normalize(
  435. Cartesian3.cross(normal, tangent, bitangent),
  436. bitangent
  437. );
  438. }
  439. }
  440. if (vertexFormat.normal) {
  441. normals[normalIndex++] = normal.x;
  442. normals[normalIndex++] = normal.y;
  443. normals[normalIndex++] = normal.z;
  444. normals[normalIndex++] = normal.x;
  445. normals[normalIndex++] = normal.y;
  446. normals[normalIndex++] = normal.z;
  447. }
  448. if (vertexFormat.tangent) {
  449. tangents[tangentIndex++] = tangent.x;
  450. tangents[tangentIndex++] = tangent.y;
  451. tangents[tangentIndex++] = tangent.z;
  452. tangents[tangentIndex++] = tangent.x;
  453. tangents[tangentIndex++] = tangent.y;
  454. tangents[tangentIndex++] = tangent.z;
  455. }
  456. if (vertexFormat.bitangent) {
  457. bitangents[bitangentIndex++] = bitangent.x;
  458. bitangents[bitangentIndex++] = bitangent.y;
  459. bitangents[bitangentIndex++] = bitangent.z;
  460. bitangents[bitangentIndex++] = bitangent.x;
  461. bitangents[bitangentIndex++] = bitangent.y;
  462. bitangents[bitangentIndex++] = bitangent.z;
  463. }
  464. }
  465. }
  466. const attributes = new GeometryAttributes();
  467. if (vertexFormat.position) {
  468. attributes.position = new GeometryAttribute({
  469. componentDatatype: ComponentDatatype.DOUBLE,
  470. componentsPerAttribute: 3,
  471. values: positions,
  472. });
  473. }
  474. if (vertexFormat.normal) {
  475. attributes.normal = new GeometryAttribute({
  476. componentDatatype: ComponentDatatype.FLOAT,
  477. componentsPerAttribute: 3,
  478. values: normals,
  479. });
  480. }
  481. if (vertexFormat.tangent) {
  482. attributes.tangent = new GeometryAttribute({
  483. componentDatatype: ComponentDatatype.FLOAT,
  484. componentsPerAttribute: 3,
  485. values: tangents,
  486. });
  487. }
  488. if (vertexFormat.bitangent) {
  489. attributes.bitangent = new GeometryAttribute({
  490. componentDatatype: ComponentDatatype.FLOAT,
  491. componentsPerAttribute: 3,
  492. values: bitangents,
  493. });
  494. }
  495. if (vertexFormat.st) {
  496. attributes.st = new GeometryAttribute({
  497. componentDatatype: ComponentDatatype.FLOAT,
  498. componentsPerAttribute: 2,
  499. values: textureCoordinates,
  500. });
  501. }
  502. // prepare the side walls, two triangles for each wall
  503. //
  504. // A (i+1) B (i+3) E
  505. // +--------+-------+
  506. // | / | /| triangles: A C B
  507. // | / | / | B C D
  508. // | / | / |
  509. // | / | / |
  510. // | / | / |
  511. // | / | / |
  512. // +--------+-------+
  513. // C (i) D (i+2) F
  514. //
  515. const numVertices = size / 3;
  516. size -= 6 * (numCorners + 1);
  517. const indices = IndexDatatype.createTypedArray(numVertices, size);
  518. let edgeIndex = 0;
  519. for (i = 0; i < numVertices - 2; i += 2) {
  520. const LL = i;
  521. const LR = i + 2;
  522. const pl = Cartesian3.fromArray(
  523. positions,
  524. LL * 3,
  525. scratchCartesian3Position1
  526. );
  527. const pr = Cartesian3.fromArray(
  528. positions,
  529. LR * 3,
  530. scratchCartesian3Position2
  531. );
  532. if (Cartesian3.equalsEpsilon(pl, pr, CesiumMath.EPSILON10)) {
  533. continue;
  534. }
  535. const UL = i + 1;
  536. const UR = i + 3;
  537. indices[edgeIndex++] = UL;
  538. indices[edgeIndex++] = LL;
  539. indices[edgeIndex++] = UR;
  540. indices[edgeIndex++] = UR;
  541. indices[edgeIndex++] = LL;
  542. indices[edgeIndex++] = LR;
  543. }
  544. return new Geometry({
  545. attributes: attributes,
  546. indices: indices,
  547. primitiveType: PrimitiveType.TRIANGLES,
  548. boundingSphere: new BoundingSphere.fromVertices(positions),
  549. });
  550. };
  551. export default WallGeometry;