智慧水务管理系统 - 精河县供水工程综合管理平台

PolygonGeometryUpdater.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. import ApproximateTerrainHeights from "../Core/ApproximateTerrainHeights.js";
  2. import ArcType from "../Core/ArcType.js";
  3. import Cartesian2 from "../Core/Cartesian2.js";
  4. import Cartesian3 from "../Core/Cartesian3.js";
  5. import Check from "../Core/Check.js";
  6. import Color from "../Core/Color.js";
  7. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  8. import CoplanarPolygonGeometry from "../Core/CoplanarPolygonGeometry.js";
  9. import CoplanarPolygonOutlineGeometry from "../Core/CoplanarPolygonOutlineGeometry.js";
  10. import defined from "../Core/defined.js";
  11. import DeveloperError from "../Core/DeveloperError.js";
  12. import DistanceDisplayConditionGeometryInstanceAttribute from "../Core/DistanceDisplayConditionGeometryInstanceAttribute.js";
  13. import EllipsoidTangentPlane from "../Core/EllipsoidTangentPlane.js";
  14. import GeometryInstance from "../Core/GeometryInstance.js";
  15. import Iso8601 from "../Core/Iso8601.js";
  16. import OffsetGeometryInstanceAttribute from "../Core/OffsetGeometryInstanceAttribute.js";
  17. import oneTimeWarning from "../Core/oneTimeWarning.js";
  18. import PolygonGeometry from "../Core/PolygonGeometry.js";
  19. import PolygonOutlineGeometry from "../Core/PolygonOutlineGeometry.js";
  20. import Rectangle from "../Core/Rectangle.js";
  21. import ShowGeometryInstanceAttribute from "../Core/ShowGeometryInstanceAttribute.js";
  22. import HeightReference from "../Scene/HeightReference.js";
  23. import MaterialAppearance from "../Scene/MaterialAppearance.js";
  24. import PerInstanceColorAppearance from "../Scene/PerInstanceColorAppearance.js";
  25. import ColorMaterialProperty from "./ColorMaterialProperty.js";
  26. import DynamicGeometryUpdater from "./DynamicGeometryUpdater.js";
  27. import GeometryUpdater from "./GeometryUpdater.js";
  28. import GroundGeometryUpdater from "./GroundGeometryUpdater.js";
  29. import Property from "./Property.js";
  30. const heightAndPerPositionHeightWarning =
  31. "Entity polygons cannot have both height and perPositionHeight. height will be ignored";
  32. const heightReferenceAndPerPositionHeightWarning =
  33. "heightReference is not supported for entity polygons with perPositionHeight. heightReference will be ignored";
  34. const scratchColor = new Color();
  35. const defaultOffset = Cartesian3.ZERO;
  36. const offsetScratch = new Cartesian3();
  37. const scratchRectangle = new Rectangle();
  38. const scratch2DPositions = [];
  39. const cart2Scratch = new Cartesian2();
  40. function PolygonGeometryOptions(entity) {
  41. this.id = entity;
  42. this.vertexFormat = undefined;
  43. this.polygonHierarchy = undefined;
  44. this.perPositionHeight = undefined;
  45. this.closeTop = undefined;
  46. this.closeBottom = undefined;
  47. this.height = undefined;
  48. this.extrudedHeight = undefined;
  49. this.granularity = undefined;
  50. this.stRotation = undefined;
  51. this.offsetAttribute = undefined;
  52. this.arcType = undefined;
  53. this.textureCoordinates = undefined;
  54. }
  55. /**
  56. * A {@link GeometryUpdater} for polygons.
  57. * Clients do not normally create this class directly, but instead rely on {@link DataSourceDisplay}.
  58. * @alias PolygonGeometryUpdater
  59. * @constructor
  60. *
  61. * @param {Entity} entity The entity containing the geometry to be visualized.
  62. * @param {Scene} scene The scene where visualization is taking place.
  63. */
  64. function PolygonGeometryUpdater(entity, scene) {
  65. GroundGeometryUpdater.call(this, {
  66. entity: entity,
  67. scene: scene,
  68. geometryOptions: new PolygonGeometryOptions(entity),
  69. geometryPropertyName: "polygon",
  70. observedPropertyNames: ["availability", "polygon"],
  71. });
  72. this._onEntityPropertyChanged(entity, "polygon", entity.polygon, undefined);
  73. }
  74. if (defined(Object.create)) {
  75. PolygonGeometryUpdater.prototype = Object.create(
  76. GroundGeometryUpdater.prototype,
  77. );
  78. PolygonGeometryUpdater.prototype.constructor = PolygonGeometryUpdater;
  79. }
  80. /**
  81. * Creates the geometry instance which represents the fill of the geometry.
  82. *
  83. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  84. * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
  85. *
  86. * @exception {DeveloperError} This instance does not represent a filled geometry.
  87. */
  88. PolygonGeometryUpdater.prototype.createFillGeometryInstance = function (time) {
  89. //>>includeStart('debug', pragmas.debug);
  90. Check.defined("time", time);
  91. if (!this._fillEnabled) {
  92. throw new DeveloperError(
  93. "This instance does not represent a filled geometry.",
  94. );
  95. }
  96. //>>includeEnd('debug');
  97. const entity = this._entity;
  98. const isAvailable = entity.isAvailable(time);
  99. const options = this._options;
  100. const attributes = {
  101. show: new ShowGeometryInstanceAttribute(
  102. isAvailable &&
  103. entity.isShowing &&
  104. this._showProperty.getValue(time) &&
  105. this._fillProperty.getValue(time),
  106. ),
  107. distanceDisplayCondition:
  108. DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
  109. this._distanceDisplayConditionProperty.getValue(time),
  110. ),
  111. offset: undefined,
  112. color: undefined,
  113. };
  114. if (this._materialProperty instanceof ColorMaterialProperty) {
  115. let currentColor;
  116. if (
  117. defined(this._materialProperty.color) &&
  118. (this._materialProperty.color.isConstant || isAvailable)
  119. ) {
  120. currentColor = this._materialProperty.color.getValue(time, scratchColor);
  121. }
  122. if (!defined(currentColor)) {
  123. currentColor = Color.WHITE;
  124. }
  125. attributes.color = ColorGeometryInstanceAttribute.fromColor(currentColor);
  126. }
  127. if (defined(options.offsetAttribute)) {
  128. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(
  129. Property.getValueOrDefault(
  130. this._terrainOffsetProperty,
  131. time,
  132. defaultOffset,
  133. offsetScratch,
  134. ),
  135. );
  136. }
  137. let geometry;
  138. if (options.perPositionHeight && !defined(options.extrudedHeight)) {
  139. geometry = new CoplanarPolygonGeometry(options);
  140. } else {
  141. geometry = new PolygonGeometry(options);
  142. }
  143. return new GeometryInstance({
  144. id: entity,
  145. geometry: geometry,
  146. attributes: attributes,
  147. });
  148. };
  149. /**
  150. * Creates the geometry instance which represents the outline of the geometry.
  151. *
  152. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  153. * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
  154. *
  155. * @exception {DeveloperError} This instance does not represent an outlined geometry.
  156. */
  157. PolygonGeometryUpdater.prototype.createOutlineGeometryInstance = function (
  158. time,
  159. ) {
  160. //>>includeStart('debug', pragmas.debug);
  161. Check.defined("time", time);
  162. if (!this._outlineEnabled) {
  163. throw new DeveloperError(
  164. "This instance does not represent an outlined geometry.",
  165. );
  166. }
  167. //>>includeEnd('debug');
  168. const entity = this._entity;
  169. const isAvailable = entity.isAvailable(time);
  170. const options = this._options;
  171. const outlineColor = Property.getValueOrDefault(
  172. this._outlineColorProperty,
  173. time,
  174. Color.BLACK,
  175. scratchColor,
  176. );
  177. const distanceDisplayCondition =
  178. this._distanceDisplayConditionProperty.getValue(time);
  179. const attributes = {
  180. show: new ShowGeometryInstanceAttribute(
  181. isAvailable &&
  182. entity.isShowing &&
  183. this._showProperty.getValue(time) &&
  184. this._showOutlineProperty.getValue(time),
  185. ),
  186. color: ColorGeometryInstanceAttribute.fromColor(outlineColor),
  187. distanceDisplayCondition:
  188. DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(
  189. distanceDisplayCondition,
  190. ),
  191. offset: undefined,
  192. };
  193. if (defined(options.offsetAttribute)) {
  194. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(
  195. Property.getValueOrDefault(
  196. this._terrainOffsetProperty,
  197. time,
  198. defaultOffset,
  199. offsetScratch,
  200. ),
  201. );
  202. }
  203. let geometry;
  204. if (options.perPositionHeight && !defined(options.extrudedHeight)) {
  205. geometry = new CoplanarPolygonOutlineGeometry(options);
  206. } else {
  207. geometry = new PolygonOutlineGeometry(options);
  208. }
  209. return new GeometryInstance({
  210. id: entity,
  211. geometry: geometry,
  212. attributes: attributes,
  213. });
  214. };
  215. PolygonGeometryUpdater.prototype._computeCenter = function (time, result) {
  216. const hierarchy = Property.getValueOrUndefined(
  217. this._entity.polygon.hierarchy,
  218. time,
  219. );
  220. if (!defined(hierarchy)) {
  221. return;
  222. }
  223. const positions = hierarchy.positions;
  224. if (positions.length === 0) {
  225. return;
  226. }
  227. const ellipsoid = this._scene.ellipsoid;
  228. const tangentPlane = EllipsoidTangentPlane.fromPoints(positions, ellipsoid);
  229. const positions2D = tangentPlane.projectPointsOntoPlane(
  230. positions,
  231. scratch2DPositions,
  232. );
  233. const length = positions2D.length;
  234. let area = 0;
  235. let j = length - 1;
  236. let centroid2D = new Cartesian2();
  237. for (let i = 0; i < length; j = i++) {
  238. const p1 = positions2D[i];
  239. const p2 = positions2D[j];
  240. const f = p1.x * p2.y - p2.x * p1.y;
  241. let sum = Cartesian2.add(p1, p2, cart2Scratch);
  242. sum = Cartesian2.multiplyByScalar(sum, f, sum);
  243. centroid2D = Cartesian2.add(centroid2D, sum, centroid2D);
  244. area += f;
  245. }
  246. const a = 1.0 / (area * 3.0);
  247. centroid2D = Cartesian2.multiplyByScalar(centroid2D, a, centroid2D);
  248. return tangentPlane.projectPointOntoEllipsoid(centroid2D, result);
  249. };
  250. PolygonGeometryUpdater.prototype._isHidden = function (entity, polygon) {
  251. return (
  252. !defined(polygon.hierarchy) ||
  253. GeometryUpdater.prototype._isHidden.call(this, entity, polygon)
  254. );
  255. };
  256. PolygonGeometryUpdater.prototype._isOnTerrain = function (entity, polygon) {
  257. const onTerrain = GroundGeometryUpdater.prototype._isOnTerrain.call(
  258. this,
  259. entity,
  260. polygon,
  261. );
  262. const perPositionHeightProperty = polygon.perPositionHeight;
  263. const perPositionHeightEnabled =
  264. defined(perPositionHeightProperty) &&
  265. (perPositionHeightProperty.isConstant
  266. ? perPositionHeightProperty.getValue(Iso8601.MINIMUM_VALUE)
  267. : true);
  268. return onTerrain && !perPositionHeightEnabled;
  269. };
  270. PolygonGeometryUpdater.prototype._isDynamic = function (entity, polygon) {
  271. return (
  272. !polygon.hierarchy.isConstant || //
  273. !Property.isConstant(polygon.height) || //
  274. !Property.isConstant(polygon.extrudedHeight) || //
  275. !Property.isConstant(polygon.granularity) || //
  276. !Property.isConstant(polygon.stRotation) || //
  277. !Property.isConstant(polygon.textureCoordinates) || //
  278. !Property.isConstant(polygon.outlineWidth) || //
  279. !Property.isConstant(polygon.perPositionHeight) || //
  280. !Property.isConstant(polygon.closeTop) || //
  281. !Property.isConstant(polygon.closeBottom) || //
  282. !Property.isConstant(polygon.zIndex) || //
  283. !Property.isConstant(polygon.arcType) || //
  284. (this._onTerrain &&
  285. !Property.isConstant(this._materialProperty) &&
  286. !(this._materialProperty instanceof ColorMaterialProperty))
  287. );
  288. };
  289. PolygonGeometryUpdater.prototype._setStaticOptions = function (
  290. entity,
  291. polygon,
  292. ) {
  293. const isColorMaterial =
  294. this._materialProperty instanceof ColorMaterialProperty;
  295. const options = this._options;
  296. options.vertexFormat = isColorMaterial
  297. ? PerInstanceColorAppearance.VERTEX_FORMAT
  298. : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  299. const hierarchyValue = polygon.hierarchy.getValue(Iso8601.MINIMUM_VALUE);
  300. let heightValue = Property.getValueOrUndefined(
  301. polygon.height,
  302. Iso8601.MINIMUM_VALUE,
  303. );
  304. const heightReferenceValue = Property.getValueOrDefault(
  305. polygon.heightReference,
  306. Iso8601.MINIMUM_VALUE,
  307. HeightReference.NONE,
  308. );
  309. let extrudedHeightValue = Property.getValueOrUndefined(
  310. polygon.extrudedHeight,
  311. Iso8601.MINIMUM_VALUE,
  312. );
  313. const extrudedHeightReferenceValue = Property.getValueOrDefault(
  314. polygon.extrudedHeightReference,
  315. Iso8601.MINIMUM_VALUE,
  316. HeightReference.NONE,
  317. );
  318. const perPositionHeightValue = Property.getValueOrDefault(
  319. polygon.perPositionHeight,
  320. Iso8601.MINIMUM_VALUE,
  321. false,
  322. );
  323. heightValue = GroundGeometryUpdater.getGeometryHeight(
  324. heightValue,
  325. heightReferenceValue,
  326. );
  327. let offsetAttribute;
  328. if (perPositionHeightValue) {
  329. if (defined(heightValue)) {
  330. heightValue = undefined;
  331. oneTimeWarning(heightAndPerPositionHeightWarning);
  332. }
  333. if (
  334. heightReferenceValue !== HeightReference.NONE &&
  335. perPositionHeightValue
  336. ) {
  337. heightValue = undefined;
  338. oneTimeWarning(heightReferenceAndPerPositionHeightWarning);
  339. }
  340. } else {
  341. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  342. heightValue = 0;
  343. }
  344. offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  345. heightValue,
  346. heightReferenceValue,
  347. extrudedHeightValue,
  348. extrudedHeightReferenceValue,
  349. );
  350. }
  351. options.polygonHierarchy = hierarchyValue;
  352. options.granularity = Property.getValueOrUndefined(
  353. polygon.granularity,
  354. Iso8601.MINIMUM_VALUE,
  355. );
  356. options.stRotation = Property.getValueOrUndefined(
  357. polygon.stRotation,
  358. Iso8601.MINIMUM_VALUE,
  359. );
  360. options.perPositionHeight = perPositionHeightValue;
  361. options.closeTop = Property.getValueOrDefault(
  362. polygon.closeTop,
  363. Iso8601.MINIMUM_VALUE,
  364. true,
  365. );
  366. options.closeBottom = Property.getValueOrDefault(
  367. polygon.closeBottom,
  368. Iso8601.MINIMUM_VALUE,
  369. true,
  370. );
  371. options.offsetAttribute = offsetAttribute;
  372. options.height = heightValue;
  373. options.arcType = Property.getValueOrDefault(
  374. polygon.arcType,
  375. Iso8601.MINIMUM_VALUE,
  376. ArcType.GEODESIC,
  377. );
  378. options.textureCoordinates = Property.getValueOrUndefined(
  379. polygon.textureCoordinates,
  380. Iso8601.MINIMUM_VALUE,
  381. );
  382. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  383. extrudedHeightValue,
  384. extrudedHeightReferenceValue,
  385. );
  386. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  387. const rectangle = PolygonGeometry.computeRectangleFromPositions(
  388. options.polygonHierarchy.positions,
  389. options.ellipsoid,
  390. options.arcType,
  391. scratchRectangle,
  392. );
  393. extrudedHeightValue =
  394. ApproximateTerrainHeights.getMinimumMaximumHeights(
  395. rectangle,
  396. ).minimumTerrainHeight;
  397. }
  398. options.extrudedHeight = extrudedHeightValue;
  399. };
  400. PolygonGeometryUpdater.prototype._getIsClosed = function (options) {
  401. const height = options.height;
  402. const extrudedHeight = options.extrudedHeight;
  403. const isExtruded = defined(extrudedHeight) && extrudedHeight !== height;
  404. return (
  405. !options.perPositionHeight &&
  406. ((!isExtruded && height === 0) ||
  407. (isExtruded && options.closeTop && options.closeBottom))
  408. );
  409. };
  410. PolygonGeometryUpdater.DynamicGeometryUpdater = DyanmicPolygonGeometryUpdater;
  411. /**
  412. * @private
  413. */
  414. function DyanmicPolygonGeometryUpdater(
  415. geometryUpdater,
  416. primitives,
  417. groundPrimitives,
  418. ) {
  419. DynamicGeometryUpdater.call(
  420. this,
  421. geometryUpdater,
  422. primitives,
  423. groundPrimitives,
  424. );
  425. }
  426. if (defined(Object.create)) {
  427. DyanmicPolygonGeometryUpdater.prototype = Object.create(
  428. DynamicGeometryUpdater.prototype,
  429. );
  430. DyanmicPolygonGeometryUpdater.prototype.constructor =
  431. DyanmicPolygonGeometryUpdater;
  432. }
  433. DyanmicPolygonGeometryUpdater.prototype._isHidden = function (
  434. entity,
  435. polygon,
  436. time,
  437. ) {
  438. return (
  439. !defined(this._options.polygonHierarchy) ||
  440. DynamicGeometryUpdater.prototype._isHidden.call(this, entity, polygon, time)
  441. );
  442. };
  443. DyanmicPolygonGeometryUpdater.prototype._setOptions = function (
  444. entity,
  445. polygon,
  446. time,
  447. ) {
  448. const options = this._options;
  449. options.polygonHierarchy = Property.getValueOrUndefined(
  450. polygon.hierarchy,
  451. time,
  452. );
  453. let heightValue = Property.getValueOrUndefined(polygon.height, time);
  454. const heightReferenceValue = Property.getValueOrDefault(
  455. polygon.heightReference,
  456. time,
  457. HeightReference.NONE,
  458. );
  459. const extrudedHeightReferenceValue = Property.getValueOrDefault(
  460. polygon.extrudedHeightReference,
  461. time,
  462. HeightReference.NONE,
  463. );
  464. let extrudedHeightValue = Property.getValueOrUndefined(
  465. polygon.extrudedHeight,
  466. time,
  467. );
  468. const perPositionHeightValue = Property.getValueOrUndefined(
  469. polygon.perPositionHeight,
  470. time,
  471. );
  472. heightValue = GroundGeometryUpdater.getGeometryHeight(
  473. heightValue,
  474. extrudedHeightReferenceValue,
  475. );
  476. let offsetAttribute;
  477. if (perPositionHeightValue) {
  478. if (defined(heightValue)) {
  479. heightValue = undefined;
  480. oneTimeWarning(heightAndPerPositionHeightWarning);
  481. }
  482. if (
  483. heightReferenceValue !== HeightReference.NONE &&
  484. perPositionHeightValue
  485. ) {
  486. heightValue = undefined;
  487. oneTimeWarning(heightReferenceAndPerPositionHeightWarning);
  488. }
  489. } else {
  490. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  491. heightValue = 0;
  492. }
  493. offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  494. heightValue,
  495. heightReferenceValue,
  496. extrudedHeightValue,
  497. extrudedHeightReferenceValue,
  498. );
  499. }
  500. options.granularity = Property.getValueOrUndefined(polygon.granularity, time);
  501. options.stRotation = Property.getValueOrUndefined(polygon.stRotation, time);
  502. options.textureCoordinates = Property.getValueOrUndefined(
  503. polygon.textureCoordinates,
  504. time,
  505. );
  506. options.perPositionHeight = Property.getValueOrUndefined(
  507. polygon.perPositionHeight,
  508. time,
  509. );
  510. options.closeTop = Property.getValueOrDefault(polygon.closeTop, time, true);
  511. options.closeBottom = Property.getValueOrDefault(
  512. polygon.closeBottom,
  513. time,
  514. true,
  515. );
  516. options.offsetAttribute = offsetAttribute;
  517. options.height = heightValue;
  518. options.arcType = Property.getValueOrDefault(
  519. polygon.arcType,
  520. time,
  521. ArcType.GEODESIC,
  522. );
  523. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  524. extrudedHeightValue,
  525. extrudedHeightReferenceValue,
  526. );
  527. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  528. const rectangle = PolygonGeometry.computeRectangleFromPositions(
  529. options.polygonHierarchy.positions,
  530. options.ellipsoid,
  531. options.arcType,
  532. scratchRectangle,
  533. );
  534. extrudedHeightValue =
  535. ApproximateTerrainHeights.getMinimumMaximumHeights(
  536. rectangle,
  537. ).minimumTerrainHeight;
  538. }
  539. options.extrudedHeight = extrudedHeightValue;
  540. };
  541. export default PolygonGeometryUpdater;