PolygonGeometryUpdater.js 17 KB

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