PolygonGeometryUpdater.js 18 KB

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