PolygonGeometryUpdater.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. var heightAndPerPositionHeightWarning =
  31. "Entity polygons cannot have both height and perPositionHeight. height will be ignored";
  32. var heightReferenceAndPerPositionHeightWarning =
  33. "heightReference is not supported for entity polygons with perPositionHeight. heightReference will be ignored";
  34. var scratchColor = new Color();
  35. var defaultOffset = Cartesian3.ZERO;
  36. var offsetScratch = new Cartesian3();
  37. var scratchRectangle = new Rectangle();
  38. var scratch2DPositions = [];
  39. var 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. var entity = this._entity;
  97. var isAvailable = entity.isAvailable(time);
  98. var options = this._options;
  99. var 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. var 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. var 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. var entity = this._entity;
  167. var isAvailable = entity.isAvailable(time);
  168. var options = this._options;
  169. var outlineColor = Property.getValueOrDefault(
  170. this._outlineColorProperty,
  171. time,
  172. Color.BLACK,
  173. scratchColor
  174. );
  175. var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(
  176. time
  177. );
  178. var 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. var 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. var hierarchy = Property.getValueOrUndefined(
  215. this._entity.polygon.hierarchy,
  216. time
  217. );
  218. if (!defined(hierarchy)) {
  219. return;
  220. }
  221. var positions = hierarchy.positions;
  222. if (positions.length === 0) {
  223. return;
  224. }
  225. var ellipsoid = this._scene.mapProjection.ellipsoid;
  226. var tangentPlane = EllipsoidTangentPlane.fromPoints(positions, ellipsoid);
  227. var positions2D = tangentPlane.projectPointsOntoPlane(
  228. positions,
  229. scratch2DPositions
  230. );
  231. var length = positions2D.length;
  232. var area = 0;
  233. var j = length - 1;
  234. var centroid2D = new Cartesian2();
  235. for (var i = 0; i < length; j = i++) {
  236. var p1 = positions2D[i];
  237. var p2 = positions2D[j];
  238. var f = p1.x * p2.y - p2.x * p1.y;
  239. var 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. var 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. var onTerrain = GroundGeometryUpdater.prototype._isOnTerrain.call(
  256. this,
  257. entity,
  258. polygon
  259. );
  260. var perPositionHeightProperty = polygon.perPositionHeight;
  261. var 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. var isColorMaterial = this._materialProperty instanceof ColorMaterialProperty;
  291. var options = this._options;
  292. options.vertexFormat = isColorMaterial
  293. ? PerInstanceColorAppearance.VERTEX_FORMAT
  294. : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  295. var hierarchyValue = polygon.hierarchy.getValue(Iso8601.MINIMUM_VALUE);
  296. var heightValue = Property.getValueOrUndefined(
  297. polygon.height,
  298. Iso8601.MINIMUM_VALUE
  299. );
  300. var heightReferenceValue = Property.getValueOrDefault(
  301. polygon.heightReference,
  302. Iso8601.MINIMUM_VALUE,
  303. HeightReference.NONE
  304. );
  305. var extrudedHeightValue = Property.getValueOrUndefined(
  306. polygon.extrudedHeight,
  307. Iso8601.MINIMUM_VALUE
  308. );
  309. var extrudedHeightReferenceValue = Property.getValueOrDefault(
  310. polygon.extrudedHeightReference,
  311. Iso8601.MINIMUM_VALUE,
  312. HeightReference.NONE
  313. );
  314. var perPositionHeightValue = Property.getValueOrDefault(
  315. polygon.perPositionHeight,
  316. Iso8601.MINIMUM_VALUE,
  317. false
  318. );
  319. heightValue = GroundGeometryUpdater.getGeometryHeight(
  320. heightValue,
  321. heightReferenceValue
  322. );
  323. var offsetAttribute;
  324. if (perPositionHeightValue) {
  325. if (defined(heightValue)) {
  326. heightValue = undefined;
  327. oneTimeWarning(heightAndPerPositionHeightWarning);
  328. }
  329. if (
  330. heightReferenceValue !== HeightReference.NONE &&
  331. perPositionHeightValue
  332. ) {
  333. heightValue = undefined;
  334. oneTimeWarning(heightReferenceAndPerPositionHeightWarning);
  335. }
  336. } else {
  337. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  338. heightValue = 0;
  339. }
  340. offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  341. heightValue,
  342. heightReferenceValue,
  343. extrudedHeightValue,
  344. extrudedHeightReferenceValue
  345. );
  346. }
  347. options.polygonHierarchy = hierarchyValue;
  348. options.granularity = Property.getValueOrUndefined(
  349. polygon.granularity,
  350. Iso8601.MINIMUM_VALUE
  351. );
  352. options.stRotation = Property.getValueOrUndefined(
  353. polygon.stRotation,
  354. Iso8601.MINIMUM_VALUE
  355. );
  356. options.perPositionHeight = perPositionHeightValue;
  357. options.closeTop = Property.getValueOrDefault(
  358. polygon.closeTop,
  359. Iso8601.MINIMUM_VALUE,
  360. true
  361. );
  362. options.closeBottom = Property.getValueOrDefault(
  363. polygon.closeBottom,
  364. Iso8601.MINIMUM_VALUE,
  365. true
  366. );
  367. options.offsetAttribute = offsetAttribute;
  368. options.height = heightValue;
  369. options.arcType = Property.getValueOrDefault(
  370. polygon.arcType,
  371. Iso8601.MINIMUM_VALUE,
  372. ArcType.GEODESIC
  373. );
  374. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  375. extrudedHeightValue,
  376. extrudedHeightReferenceValue
  377. );
  378. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  379. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  380. PolygonGeometry.computeRectangle(options, scratchRectangle)
  381. ).minimumTerrainHeight;
  382. }
  383. options.extrudedHeight = extrudedHeightValue;
  384. };
  385. PolygonGeometryUpdater.prototype._getIsClosed = function (options) {
  386. var height = options.height;
  387. var extrudedHeight = options.extrudedHeight;
  388. var isExtruded = defined(extrudedHeight) && extrudedHeight !== height;
  389. return (
  390. !options.perPositionHeight &&
  391. ((!isExtruded && height === 0) ||
  392. (isExtruded && options.closeTop && options.closeBottom))
  393. );
  394. };
  395. PolygonGeometryUpdater.DynamicGeometryUpdater = DyanmicPolygonGeometryUpdater;
  396. /**
  397. * @private
  398. */
  399. function DyanmicPolygonGeometryUpdater(
  400. geometryUpdater,
  401. primitives,
  402. groundPrimitives
  403. ) {
  404. DynamicGeometryUpdater.call(
  405. this,
  406. geometryUpdater,
  407. primitives,
  408. groundPrimitives
  409. );
  410. }
  411. if (defined(Object.create)) {
  412. DyanmicPolygonGeometryUpdater.prototype = Object.create(
  413. DynamicGeometryUpdater.prototype
  414. );
  415. DyanmicPolygonGeometryUpdater.prototype.constructor = DyanmicPolygonGeometryUpdater;
  416. }
  417. DyanmicPolygonGeometryUpdater.prototype._isHidden = function (
  418. entity,
  419. polygon,
  420. time
  421. ) {
  422. return (
  423. !defined(this._options.polygonHierarchy) ||
  424. DynamicGeometryUpdater.prototype._isHidden.call(this, entity, polygon, time)
  425. );
  426. };
  427. DyanmicPolygonGeometryUpdater.prototype._setOptions = function (
  428. entity,
  429. polygon,
  430. time
  431. ) {
  432. var options = this._options;
  433. options.polygonHierarchy = Property.getValueOrUndefined(
  434. polygon.hierarchy,
  435. time
  436. );
  437. var heightValue = Property.getValueOrUndefined(polygon.height, time);
  438. var heightReferenceValue = Property.getValueOrDefault(
  439. polygon.heightReference,
  440. time,
  441. HeightReference.NONE
  442. );
  443. var extrudedHeightReferenceValue = Property.getValueOrDefault(
  444. polygon.extrudedHeightReference,
  445. time,
  446. HeightReference.NONE
  447. );
  448. var extrudedHeightValue = Property.getValueOrUndefined(
  449. polygon.extrudedHeight,
  450. time
  451. );
  452. var perPositionHeightValue = Property.getValueOrUndefined(
  453. polygon.perPositionHeight,
  454. time
  455. );
  456. heightValue = GroundGeometryUpdater.getGeometryHeight(
  457. heightValue,
  458. extrudedHeightReferenceValue
  459. );
  460. var offsetAttribute;
  461. if (perPositionHeightValue) {
  462. if (defined(heightValue)) {
  463. heightValue = undefined;
  464. oneTimeWarning(heightAndPerPositionHeightWarning);
  465. }
  466. if (
  467. heightReferenceValue !== HeightReference.NONE &&
  468. perPositionHeightValue
  469. ) {
  470. heightValue = undefined;
  471. oneTimeWarning(heightReferenceAndPerPositionHeightWarning);
  472. }
  473. } else {
  474. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  475. heightValue = 0;
  476. }
  477. offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(
  478. heightValue,
  479. heightReferenceValue,
  480. extrudedHeightValue,
  481. extrudedHeightReferenceValue
  482. );
  483. }
  484. options.granularity = Property.getValueOrUndefined(polygon.granularity, time);
  485. options.stRotation = Property.getValueOrUndefined(polygon.stRotation, time);
  486. options.perPositionHeight = Property.getValueOrUndefined(
  487. polygon.perPositionHeight,
  488. time
  489. );
  490. options.closeTop = Property.getValueOrDefault(polygon.closeTop, time, true);
  491. options.closeBottom = Property.getValueOrDefault(
  492. polygon.closeBottom,
  493. time,
  494. true
  495. );
  496. options.offsetAttribute = offsetAttribute;
  497. options.height = heightValue;
  498. options.arcType = Property.getValueOrDefault(
  499. polygon.arcType,
  500. time,
  501. ArcType.GEODESIC
  502. );
  503. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(
  504. extrudedHeightValue,
  505. extrudedHeightReferenceValue
  506. );
  507. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  508. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(
  509. PolygonGeometry.computeRectangle(options, scratchRectangle)
  510. ).minimumTerrainHeight;
  511. }
  512. options.extrudedHeight = extrudedHeightValue;
  513. };
  514. export default PolygonGeometryUpdater;