createRectangleOutlineGeometry.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. define(['./defaultValue-fe22d8c0', './Matrix3-41c58dde', './Matrix2-e1298525', './Transforms-bc45e707', './ComponentDatatype-cf1fa08e', './Check-6ede7e26', './GeometryAttribute-a466e9c7', './GeometryAttributes-ad136444', './GeometryOffsetAttribute-9ad0019c', './IndexDatatype-2643aa47', './Math-0a2ac845', './PolygonPipeline-1fe328c0', './RectangleGeometryLibrary-a23c9ebe', './RuntimeError-ef395448', './combine-d9581036', './WebGLConstants-0b1ce7ba', './EllipsoidRhumbLine-ef872433'], (function (defaultValue, Matrix3, Matrix2, Transforms, ComponentDatatype, Check, GeometryAttribute, GeometryAttributes, GeometryOffsetAttribute, IndexDatatype, Math$1, PolygonPipeline, RectangleGeometryLibrary, RuntimeError, combine, WebGLConstants, EllipsoidRhumbLine) { 'use strict';
  2. const bottomBoundingSphere = new Transforms.BoundingSphere();
  3. const topBoundingSphere = new Transforms.BoundingSphere();
  4. const positionScratch = new Matrix3.Cartesian3();
  5. const rectangleScratch = new Matrix2.Rectangle();
  6. function constructRectangle(geometry, computedOptions) {
  7. const ellipsoid = geometry._ellipsoid;
  8. const height = computedOptions.height;
  9. const width = computedOptions.width;
  10. const northCap = computedOptions.northCap;
  11. const southCap = computedOptions.southCap;
  12. let rowHeight = height;
  13. let widthMultiplier = 2;
  14. let size = 0;
  15. let corners = 4;
  16. if (northCap) {
  17. widthMultiplier -= 1;
  18. rowHeight -= 1;
  19. size += 1;
  20. corners -= 2;
  21. }
  22. if (southCap) {
  23. widthMultiplier -= 1;
  24. rowHeight -= 1;
  25. size += 1;
  26. corners -= 2;
  27. }
  28. size += widthMultiplier * width + 2 * rowHeight - corners;
  29. const positions = new Float64Array(size * 3);
  30. let posIndex = 0;
  31. let row = 0;
  32. let col;
  33. const position = positionScratch;
  34. if (northCap) {
  35. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(
  36. computedOptions,
  37. ellipsoid,
  38. false,
  39. row,
  40. 0,
  41. position
  42. );
  43. positions[posIndex++] = position.x;
  44. positions[posIndex++] = position.y;
  45. positions[posIndex++] = position.z;
  46. } else {
  47. for (col = 0; col < width; col++) {
  48. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(
  49. computedOptions,
  50. ellipsoid,
  51. false,
  52. row,
  53. col,
  54. position
  55. );
  56. positions[posIndex++] = position.x;
  57. positions[posIndex++] = position.y;
  58. positions[posIndex++] = position.z;
  59. }
  60. }
  61. col = width - 1;
  62. for (row = 1; row < height; row++) {
  63. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(
  64. computedOptions,
  65. ellipsoid,
  66. false,
  67. row,
  68. col,
  69. position
  70. );
  71. positions[posIndex++] = position.x;
  72. positions[posIndex++] = position.y;
  73. positions[posIndex++] = position.z;
  74. }
  75. row = height - 1;
  76. if (!southCap) {
  77. // if southCap is true, we dont need to add any more points because the south pole point was added by the iteration above
  78. for (col = width - 2; col >= 0; col--) {
  79. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(
  80. computedOptions,
  81. ellipsoid,
  82. false,
  83. row,
  84. col,
  85. position
  86. );
  87. positions[posIndex++] = position.x;
  88. positions[posIndex++] = position.y;
  89. positions[posIndex++] = position.z;
  90. }
  91. }
  92. col = 0;
  93. for (row = height - 2; row > 0; row--) {
  94. RectangleGeometryLibrary.RectangleGeometryLibrary.computePosition(
  95. computedOptions,
  96. ellipsoid,
  97. false,
  98. row,
  99. col,
  100. position
  101. );
  102. positions[posIndex++] = position.x;
  103. positions[posIndex++] = position.y;
  104. positions[posIndex++] = position.z;
  105. }
  106. const indicesSize = (positions.length / 3) * 2;
  107. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  108. positions.length / 3,
  109. indicesSize
  110. );
  111. let index = 0;
  112. for (let i = 0; i < positions.length / 3 - 1; i++) {
  113. indices[index++] = i;
  114. indices[index++] = i + 1;
  115. }
  116. indices[index++] = positions.length / 3 - 1;
  117. indices[index++] = 0;
  118. const geo = new GeometryAttribute.Geometry({
  119. attributes: new GeometryAttributes.GeometryAttributes(),
  120. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  121. });
  122. geo.attributes.position = new GeometryAttribute.GeometryAttribute({
  123. componentDatatype: ComponentDatatype.ComponentDatatype.DOUBLE,
  124. componentsPerAttribute: 3,
  125. values: positions,
  126. });
  127. geo.indices = indices;
  128. return geo;
  129. }
  130. function constructExtrudedRectangle(rectangleGeometry, computedOptions) {
  131. const surfaceHeight = rectangleGeometry._surfaceHeight;
  132. const extrudedHeight = rectangleGeometry._extrudedHeight;
  133. const ellipsoid = rectangleGeometry._ellipsoid;
  134. const minHeight = extrudedHeight;
  135. const maxHeight = surfaceHeight;
  136. const geo = constructRectangle(rectangleGeometry, computedOptions);
  137. const height = computedOptions.height;
  138. const width = computedOptions.width;
  139. const topPositions = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  140. geo.attributes.position.values,
  141. maxHeight,
  142. ellipsoid,
  143. false
  144. );
  145. let length = topPositions.length;
  146. const positions = new Float64Array(length * 2);
  147. positions.set(topPositions);
  148. const bottomPositions = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  149. geo.attributes.position.values,
  150. minHeight,
  151. ellipsoid
  152. );
  153. positions.set(bottomPositions, length);
  154. geo.attributes.position.values = positions;
  155. const northCap = computedOptions.northCap;
  156. const southCap = computedOptions.southCap;
  157. let corners = 4;
  158. if (northCap) {
  159. corners -= 1;
  160. }
  161. if (southCap) {
  162. corners -= 1;
  163. }
  164. const indicesSize = (positions.length / 3 + corners) * 2;
  165. const indices = IndexDatatype.IndexDatatype.createTypedArray(
  166. positions.length / 3,
  167. indicesSize
  168. );
  169. length = positions.length / 6;
  170. let index = 0;
  171. for (let i = 0; i < length - 1; i++) {
  172. indices[index++] = i;
  173. indices[index++] = i + 1;
  174. indices[index++] = i + length;
  175. indices[index++] = i + length + 1;
  176. }
  177. indices[index++] = length - 1;
  178. indices[index++] = 0;
  179. indices[index++] = length + length - 1;
  180. indices[index++] = length;
  181. indices[index++] = 0;
  182. indices[index++] = length;
  183. let bottomCorner;
  184. if (northCap) {
  185. bottomCorner = height - 1;
  186. } else {
  187. const topRightCorner = width - 1;
  188. indices[index++] = topRightCorner;
  189. indices[index++] = topRightCorner + length;
  190. bottomCorner = width + height - 2;
  191. }
  192. indices[index++] = bottomCorner;
  193. indices[index++] = bottomCorner + length;
  194. if (!southCap) {
  195. const bottomLeftCorner = width + bottomCorner - 1;
  196. indices[index++] = bottomLeftCorner;
  197. indices[index] = bottomLeftCorner + length;
  198. }
  199. geo.indices = indices;
  200. return geo;
  201. }
  202. /**
  203. * A description of the outline of a a cartographic rectangle on an ellipsoid centered at the origin.
  204. *
  205. * @alias RectangleOutlineGeometry
  206. * @constructor
  207. *
  208. * @param {object} options Object with the following properties:
  209. * @param {Rectangle} options.rectangle A cartographic rectangle with north, south, east and west properties in radians.
  210. * @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid on which the rectangle lies.
  211. * @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.
  212. * @param {number} [options.height=0.0] The distance in meters between the rectangle and the ellipsoid surface.
  213. * @param {number} [options.rotation=0.0] The rotation of the rectangle, in radians. A positive rotation is counter-clockwise.
  214. * @param {number} [options.extrudedHeight] The distance in meters between the rectangle's extruded face and the ellipsoid surface.
  215. *
  216. * @exception {DeveloperError} <code>options.rectangle.north</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  217. * @exception {DeveloperError} <code>options.rectangle.south</code> must be in the interval [<code>-Pi/2</code>, <code>Pi/2</code>].
  218. * @exception {DeveloperError} <code>options.rectangle.east</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  219. * @exception {DeveloperError} <code>options.rectangle.west</code> must be in the interval [<code>-Pi</code>, <code>Pi</code>].
  220. * @exception {DeveloperError} <code>options.rectangle.north</code> must be greater than <code>rectangle.south</code>.
  221. *
  222. * @see RectangleOutlineGeometry#createGeometry
  223. *
  224. * @example
  225. * const rectangle = new Cesium.RectangleOutlineGeometry({
  226. * ellipsoid : Cesium.Ellipsoid.WGS84,
  227. * rectangle : Cesium.Rectangle.fromDegrees(-80.0, 39.0, -74.0, 42.0),
  228. * height : 10000.0
  229. * });
  230. * const geometry = Cesium.RectangleOutlineGeometry.createGeometry(rectangle);
  231. */
  232. function RectangleOutlineGeometry(options) {
  233. options = defaultValue.defaultValue(options, defaultValue.defaultValue.EMPTY_OBJECT);
  234. const rectangle = options.rectangle;
  235. const granularity = defaultValue.defaultValue(
  236. options.granularity,
  237. Math$1.CesiumMath.RADIANS_PER_DEGREE
  238. );
  239. const ellipsoid = defaultValue.defaultValue(options.ellipsoid, Matrix3.Ellipsoid.WGS84);
  240. const rotation = defaultValue.defaultValue(options.rotation, 0.0);
  241. //>>includeStart('debug', pragmas.debug);
  242. if (!defaultValue.defined(rectangle)) {
  243. throw new Check.DeveloperError("rectangle is required.");
  244. }
  245. Matrix2.Rectangle.validate(rectangle);
  246. if (rectangle.north < rectangle.south) {
  247. throw new Check.DeveloperError(
  248. "options.rectangle.north must be greater than options.rectangle.south"
  249. );
  250. }
  251. //>>includeEnd('debug');
  252. const height = defaultValue.defaultValue(options.height, 0.0);
  253. const extrudedHeight = defaultValue.defaultValue(options.extrudedHeight, height);
  254. this._rectangle = Matrix2.Rectangle.clone(rectangle);
  255. this._granularity = granularity;
  256. this._ellipsoid = ellipsoid;
  257. this._surfaceHeight = Math.max(height, extrudedHeight);
  258. this._rotation = rotation;
  259. this._extrudedHeight = Math.min(height, extrudedHeight);
  260. this._offsetAttribute = options.offsetAttribute;
  261. this._workerName = "createRectangleOutlineGeometry";
  262. }
  263. /**
  264. * The number of elements used to pack the object into an array.
  265. * @type {number}
  266. */
  267. RectangleOutlineGeometry.packedLength =
  268. Matrix2.Rectangle.packedLength + Matrix3.Ellipsoid.packedLength + 5;
  269. /**
  270. * Stores the provided instance into the provided array.
  271. *
  272. * @param {RectangleOutlineGeometry} value The value to pack.
  273. * @param {number[]} array The array to pack into.
  274. * @param {number} [startingIndex=0] The index into the array at which to start packing the elements.
  275. *
  276. * @returns {number[]} The array that was packed into
  277. */
  278. RectangleOutlineGeometry.pack = function (value, array, startingIndex) {
  279. //>>includeStart('debug', pragmas.debug);
  280. if (!defaultValue.defined(value)) {
  281. throw new Check.DeveloperError("value is required");
  282. }
  283. if (!defaultValue.defined(array)) {
  284. throw new Check.DeveloperError("array is required");
  285. }
  286. //>>includeEnd('debug');
  287. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  288. Matrix2.Rectangle.pack(value._rectangle, array, startingIndex);
  289. startingIndex += Matrix2.Rectangle.packedLength;
  290. Matrix3.Ellipsoid.pack(value._ellipsoid, array, startingIndex);
  291. startingIndex += Matrix3.Ellipsoid.packedLength;
  292. array[startingIndex++] = value._granularity;
  293. array[startingIndex++] = value._surfaceHeight;
  294. array[startingIndex++] = value._rotation;
  295. array[startingIndex++] = value._extrudedHeight;
  296. array[startingIndex] = defaultValue.defaultValue(value._offsetAttribute, -1);
  297. return array;
  298. };
  299. const scratchRectangle = new Matrix2.Rectangle();
  300. const scratchEllipsoid = Matrix3.Ellipsoid.clone(Matrix3.Ellipsoid.UNIT_SPHERE);
  301. const scratchOptions = {
  302. rectangle: scratchRectangle,
  303. ellipsoid: scratchEllipsoid,
  304. granularity: undefined,
  305. height: undefined,
  306. rotation: undefined,
  307. extrudedHeight: undefined,
  308. offsetAttribute: undefined,
  309. };
  310. /**
  311. * Retrieves an instance from a packed array.
  312. *
  313. * @param {number[]} array The packed array.
  314. * @param {number} [startingIndex=0] The starting index of the element to be unpacked.
  315. * @param {RectangleOutlineGeometry} [result] The object into which to store the result.
  316. * @returns {RectangleOutlineGeometry} The modified result parameter or a new Quaternion instance if one was not provided.
  317. */
  318. RectangleOutlineGeometry.unpack = function (array, startingIndex, result) {
  319. //>>includeStart('debug', pragmas.debug);
  320. if (!defaultValue.defined(array)) {
  321. throw new Check.DeveloperError("array is required");
  322. }
  323. //>>includeEnd('debug');
  324. startingIndex = defaultValue.defaultValue(startingIndex, 0);
  325. const rectangle = Matrix2.Rectangle.unpack(array, startingIndex, scratchRectangle);
  326. startingIndex += Matrix2.Rectangle.packedLength;
  327. const ellipsoid = Matrix3.Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
  328. startingIndex += Matrix3.Ellipsoid.packedLength;
  329. const granularity = array[startingIndex++];
  330. const height = array[startingIndex++];
  331. const rotation = array[startingIndex++];
  332. const extrudedHeight = array[startingIndex++];
  333. const offsetAttribute = array[startingIndex];
  334. if (!defaultValue.defined(result)) {
  335. scratchOptions.granularity = granularity;
  336. scratchOptions.height = height;
  337. scratchOptions.rotation = rotation;
  338. scratchOptions.extrudedHeight = extrudedHeight;
  339. scratchOptions.offsetAttribute =
  340. offsetAttribute === -1 ? undefined : offsetAttribute;
  341. return new RectangleOutlineGeometry(scratchOptions);
  342. }
  343. result._rectangle = Matrix2.Rectangle.clone(rectangle, result._rectangle);
  344. result._ellipsoid = Matrix3.Ellipsoid.clone(ellipsoid, result._ellipsoid);
  345. result._surfaceHeight = height;
  346. result._rotation = rotation;
  347. result._extrudedHeight = extrudedHeight;
  348. result._offsetAttribute =
  349. offsetAttribute === -1 ? undefined : offsetAttribute;
  350. return result;
  351. };
  352. const nwScratch = new Matrix3.Cartographic();
  353. /**
  354. * Computes the geometric representation of an outline of a rectangle, including its vertices, indices, and a bounding sphere.
  355. *
  356. * @param {RectangleOutlineGeometry} rectangleGeometry A description of the rectangle outline.
  357. * @returns {Geometry|undefined} The computed vertices and indices.
  358. *
  359. * @exception {DeveloperError} Rotated rectangle is invalid.
  360. */
  361. RectangleOutlineGeometry.createGeometry = function (rectangleGeometry) {
  362. const rectangle = rectangleGeometry._rectangle;
  363. const ellipsoid = rectangleGeometry._ellipsoid;
  364. const computedOptions = RectangleGeometryLibrary.RectangleGeometryLibrary.computeOptions(
  365. rectangle,
  366. rectangleGeometry._granularity,
  367. rectangleGeometry._rotation,
  368. 0,
  369. rectangleScratch,
  370. nwScratch
  371. );
  372. let geometry;
  373. let boundingSphere;
  374. if (
  375. Math$1.CesiumMath.equalsEpsilon(
  376. rectangle.north,
  377. rectangle.south,
  378. Math$1.CesiumMath.EPSILON10
  379. ) ||
  380. Math$1.CesiumMath.equalsEpsilon(
  381. rectangle.east,
  382. rectangle.west,
  383. Math$1.CesiumMath.EPSILON10
  384. )
  385. ) {
  386. return undefined;
  387. }
  388. const surfaceHeight = rectangleGeometry._surfaceHeight;
  389. const extrudedHeight = rectangleGeometry._extrudedHeight;
  390. const extrude = !Math$1.CesiumMath.equalsEpsilon(
  391. surfaceHeight,
  392. extrudedHeight,
  393. 0,
  394. Math$1.CesiumMath.EPSILON2
  395. );
  396. let offsetValue;
  397. if (extrude) {
  398. geometry = constructExtrudedRectangle(rectangleGeometry, computedOptions);
  399. if (defaultValue.defined(rectangleGeometry._offsetAttribute)) {
  400. const size = geometry.attributes.position.values.length / 3;
  401. let offsetAttribute = new Uint8Array(size);
  402. if (rectangleGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.TOP) {
  403. offsetAttribute = offsetAttribute.fill(1, 0, size / 2);
  404. } else {
  405. offsetValue =
  406. rectangleGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  407. ? 0
  408. : 1;
  409. offsetAttribute = offsetAttribute.fill(offsetValue);
  410. }
  411. geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  412. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  413. componentsPerAttribute: 1,
  414. values: offsetAttribute,
  415. });
  416. }
  417. const topBS = Transforms.BoundingSphere.fromRectangle3D(
  418. rectangle,
  419. ellipsoid,
  420. surfaceHeight,
  421. topBoundingSphere
  422. );
  423. const bottomBS = Transforms.BoundingSphere.fromRectangle3D(
  424. rectangle,
  425. ellipsoid,
  426. extrudedHeight,
  427. bottomBoundingSphere
  428. );
  429. boundingSphere = Transforms.BoundingSphere.union(topBS, bottomBS);
  430. } else {
  431. geometry = constructRectangle(rectangleGeometry, computedOptions);
  432. geometry.attributes.position.values = PolygonPipeline.PolygonPipeline.scaleToGeodeticHeight(
  433. geometry.attributes.position.values,
  434. surfaceHeight,
  435. ellipsoid,
  436. false
  437. );
  438. if (defaultValue.defined(rectangleGeometry._offsetAttribute)) {
  439. const length = geometry.attributes.position.values.length;
  440. offsetValue =
  441. rectangleGeometry._offsetAttribute === GeometryOffsetAttribute.GeometryOffsetAttribute.NONE
  442. ? 0
  443. : 1;
  444. const applyOffset = new Uint8Array(length / 3).fill(offsetValue);
  445. geometry.attributes.applyOffset = new GeometryAttribute.GeometryAttribute({
  446. componentDatatype: ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE,
  447. componentsPerAttribute: 1,
  448. values: applyOffset,
  449. });
  450. }
  451. boundingSphere = Transforms.BoundingSphere.fromRectangle3D(
  452. rectangle,
  453. ellipsoid,
  454. surfaceHeight
  455. );
  456. }
  457. return new GeometryAttribute.Geometry({
  458. attributes: geometry.attributes,
  459. indices: geometry.indices,
  460. primitiveType: GeometryAttribute.PrimitiveType.LINES,
  461. boundingSphere: boundingSphere,
  462. offsetAttribute: rectangleGeometry._offsetAttribute,
  463. });
  464. };
  465. function createRectangleOutlineGeometry(rectangleGeometry, offset) {
  466. if (defaultValue.defined(offset)) {
  467. rectangleGeometry = RectangleOutlineGeometry.unpack(
  468. rectangleGeometry,
  469. offset
  470. );
  471. }
  472. rectangleGeometry._ellipsoid = Matrix3.Ellipsoid.clone(rectangleGeometry._ellipsoid);
  473. rectangleGeometry._rectangle = Matrix2.Rectangle.clone(rectangleGeometry._rectangle);
  474. return RectangleOutlineGeometry.createGeometry(rectangleGeometry);
  475. }
  476. return createRectangleOutlineGeometry;
  477. }));