PolygonGeometryUpdater.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 = 'Entity polygons cannot have both height and perPositionHeight. height will be ignored';
  31. var heightReferenceAndPerPositionHeightWarning = 'heightReference is not supported for entity polygons with perPositionHeight. heightReference will be ignored';
  32. var scratchColor = new Color();
  33. var defaultOffset = Cartesian3.ZERO;
  34. var offsetScratch = new Cartesian3();
  35. var scratchRectangle = new Rectangle();
  36. var scratch2DPositions = [];
  37. var cart2Scratch = new Cartesian2();
  38. function PolygonGeometryOptions(entity) {
  39. this.id = entity;
  40. this.vertexFormat = undefined;
  41. this.polygonHierarchy = undefined;
  42. this.perPositionHeight = undefined;
  43. this.closeTop = undefined;
  44. this.closeBottom = undefined;
  45. this.height = undefined;
  46. this.extrudedHeight = undefined;
  47. this.granularity = undefined;
  48. this.stRotation = undefined;
  49. this.offsetAttribute = undefined;
  50. this.arcType = undefined;
  51. }
  52. /**
  53. * A {@link GeometryUpdater} for polygons.
  54. * Clients do not normally create this class directly, but instead rely on {@link DataSourceDisplay}.
  55. * @alias PolygonGeometryUpdater
  56. * @constructor
  57. *
  58. * @param {Entity} entity The entity containing the geometry to be visualized.
  59. * @param {Scene} scene The scene where visualization is taking place.
  60. */
  61. function PolygonGeometryUpdater(entity, scene) {
  62. GroundGeometryUpdater.call(this, {
  63. entity : entity,
  64. scene : scene,
  65. geometryOptions : new PolygonGeometryOptions(entity),
  66. geometryPropertyName : 'polygon',
  67. observedPropertyNames : ['availability', 'polygon']
  68. });
  69. this._onEntityPropertyChanged(entity, 'polygon', entity.polygon, undefined);
  70. }
  71. if (defined(Object.create)) {
  72. PolygonGeometryUpdater.prototype = Object.create(GroundGeometryUpdater.prototype);
  73. PolygonGeometryUpdater.prototype.constructor = PolygonGeometryUpdater;
  74. }
  75. /**
  76. * Creates the geometry instance which represents the fill of the geometry.
  77. *
  78. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  79. * @returns {GeometryInstance} The geometry instance representing the filled portion of the geometry.
  80. *
  81. * @exception {DeveloperError} This instance does not represent a filled geometry.
  82. */
  83. PolygonGeometryUpdater.prototype.createFillGeometryInstance = function(time) {
  84. //>>includeStart('debug', pragmas.debug);
  85. Check.defined('time', time);
  86. if (!this._fillEnabled) {
  87. throw new DeveloperError('This instance does not represent a filled geometry.');
  88. }
  89. //>>includeEnd('debug');
  90. var entity = this._entity;
  91. var isAvailable = entity.isAvailable(time);
  92. var options = this._options;
  93. var attributes = {
  94. show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._fillProperty.getValue(time)),
  95. distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(this._distanceDisplayConditionProperty.getValue(time)),
  96. offset : undefined,
  97. color : undefined
  98. };
  99. if (this._materialProperty instanceof ColorMaterialProperty) {
  100. var currentColor;
  101. if (defined(this._materialProperty.color) && (this._materialProperty.color.isConstant || isAvailable)) {
  102. currentColor = this._materialProperty.color.getValue(time, scratchColor);
  103. }
  104. if (!defined(currentColor)) {
  105. currentColor = Color.WHITE;
  106. }
  107. attributes.color = ColorGeometryInstanceAttribute.fromColor(currentColor);
  108. }
  109. if (defined(options.offsetAttribute)) {
  110. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
  111. }
  112. var geometry;
  113. if (options.perPositionHeight && !defined(options.extrudedHeight)) {
  114. geometry = new CoplanarPolygonGeometry(options);
  115. } else {
  116. geometry = new PolygonGeometry(options);
  117. }
  118. return new GeometryInstance({
  119. id : entity,
  120. geometry : geometry,
  121. attributes : attributes
  122. });
  123. };
  124. /**
  125. * Creates the geometry instance which represents the outline of the geometry.
  126. *
  127. * @param {JulianDate} time The time to use when retrieving initial attribute values.
  128. * @returns {GeometryInstance} The geometry instance representing the outline portion of the geometry.
  129. *
  130. * @exception {DeveloperError} This instance does not represent an outlined geometry.
  131. */
  132. PolygonGeometryUpdater.prototype.createOutlineGeometryInstance = function(time) {
  133. //>>includeStart('debug', pragmas.debug);
  134. Check.defined('time', time);
  135. if (!this._outlineEnabled) {
  136. throw new DeveloperError('This instance does not represent an outlined geometry.');
  137. }
  138. //>>includeEnd('debug');
  139. var entity = this._entity;
  140. var isAvailable = entity.isAvailable(time);
  141. var options = this._options;
  142. var outlineColor = Property.getValueOrDefault(this._outlineColorProperty, time, Color.BLACK, scratchColor);
  143. var distanceDisplayCondition = this._distanceDisplayConditionProperty.getValue(time);
  144. var attributes = {
  145. show : new ShowGeometryInstanceAttribute(isAvailable && entity.isShowing && this._showProperty.getValue(time) && this._showOutlineProperty.getValue(time)),
  146. color : ColorGeometryInstanceAttribute.fromColor(outlineColor),
  147. distanceDisplayCondition : DistanceDisplayConditionGeometryInstanceAttribute.fromDistanceDisplayCondition(distanceDisplayCondition),
  148. offset : undefined
  149. };
  150. if (defined(options.offsetAttribute)) {
  151. attributes.offset = OffsetGeometryInstanceAttribute.fromCartesian3(Property.getValueOrDefault(this._terrainOffsetProperty, time, defaultOffset, offsetScratch));
  152. }
  153. var geometry;
  154. if (options.perPositionHeight && !defined(options.extrudedHeight)) {
  155. geometry = new CoplanarPolygonOutlineGeometry(options);
  156. } else {
  157. geometry = new PolygonOutlineGeometry(options);
  158. }
  159. return new GeometryInstance({
  160. id : entity,
  161. geometry : geometry,
  162. attributes : attributes
  163. });
  164. };
  165. PolygonGeometryUpdater.prototype._computeCenter = function(time, result) {
  166. var hierarchy = Property.getValueOrUndefined(this._entity.polygon.hierarchy, time);
  167. if (!defined(hierarchy)) {
  168. return;
  169. }
  170. var positions = hierarchy.positions;
  171. if (positions.length === 0) {
  172. return;
  173. }
  174. var ellipsoid = this._scene.mapProjection.ellipsoid;
  175. var tangentPlane = EllipsoidTangentPlane.fromPoints(positions, ellipsoid);
  176. var positions2D = tangentPlane.projectPointsOntoPlane(positions, scratch2DPositions);
  177. var length = positions2D.length;
  178. var area = 0;
  179. var j = length - 1;
  180. var centroid2D = new Cartesian2();
  181. for (var i = 0; i < length; j = i++) {
  182. var p1 = positions2D[i];
  183. var p2 = positions2D[j];
  184. var f = p1.x * p2.y - p2.x * p1.y;
  185. var sum = Cartesian2.add(p1, p2, cart2Scratch);
  186. sum = Cartesian2.multiplyByScalar(sum, f, sum);
  187. centroid2D = Cartesian2.add(centroid2D, sum, centroid2D);
  188. area += f;
  189. }
  190. var a = 1.0 / (area * 3.0);
  191. centroid2D = Cartesian2.multiplyByScalar(centroid2D, a, centroid2D);
  192. return tangentPlane.projectPointOntoEllipsoid(centroid2D, result);
  193. };
  194. PolygonGeometryUpdater.prototype._isHidden = function(entity, polygon) {
  195. return !defined(polygon.hierarchy) || GeometryUpdater.prototype._isHidden.call(this, entity, polygon);
  196. };
  197. PolygonGeometryUpdater.prototype._isOnTerrain = function(entity, polygon) {
  198. var onTerrain = GroundGeometryUpdater.prototype._isOnTerrain.call(this, entity, polygon);
  199. var perPositionHeightProperty = polygon.perPositionHeight;
  200. var perPositionHeightEnabled = defined(perPositionHeightProperty) && (perPositionHeightProperty.isConstant ? perPositionHeightProperty.getValue(Iso8601.MINIMUM_VALUE) : true);
  201. return onTerrain && !perPositionHeightEnabled;
  202. };
  203. PolygonGeometryUpdater.prototype._isDynamic = function(entity, polygon) {
  204. return !polygon.hierarchy.isConstant || //
  205. !Property.isConstant(polygon.height) || //
  206. !Property.isConstant(polygon.extrudedHeight) || //
  207. !Property.isConstant(polygon.granularity) || //
  208. !Property.isConstant(polygon.stRotation) || //
  209. !Property.isConstant(polygon.outlineWidth) || //
  210. !Property.isConstant(polygon.perPositionHeight) || //
  211. !Property.isConstant(polygon.closeTop) || //
  212. !Property.isConstant(polygon.closeBottom) || //
  213. !Property.isConstant(polygon.zIndex) || //
  214. !Property.isConstant(polygon.arcType) || //
  215. (this._onTerrain && !Property.isConstant(this._materialProperty));
  216. };
  217. PolygonGeometryUpdater.prototype._setStaticOptions = function(entity, polygon) {
  218. var isColorMaterial = this._materialProperty instanceof ColorMaterialProperty;
  219. var options = this._options;
  220. options.vertexFormat = isColorMaterial ? PerInstanceColorAppearance.VERTEX_FORMAT : MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat;
  221. var hierarchyValue = polygon.hierarchy.getValue(Iso8601.MINIMUM_VALUE);
  222. var heightValue = Property.getValueOrUndefined(polygon.height, Iso8601.MINIMUM_VALUE);
  223. var heightReferenceValue = Property.getValueOrDefault(polygon.heightReference, Iso8601.MINIMUM_VALUE, HeightReference.NONE);
  224. var extrudedHeightValue = Property.getValueOrUndefined(polygon.extrudedHeight, Iso8601.MINIMUM_VALUE);
  225. var extrudedHeightReferenceValue = Property.getValueOrDefault(polygon.extrudedHeightReference, Iso8601.MINIMUM_VALUE, HeightReference.NONE);
  226. var perPositionHeightValue = Property.getValueOrDefault(polygon.perPositionHeight, Iso8601.MINIMUM_VALUE, false);
  227. heightValue = GroundGeometryUpdater.getGeometryHeight(heightValue, heightReferenceValue);
  228. var offsetAttribute;
  229. if (perPositionHeightValue) {
  230. if (defined(heightValue)) {
  231. heightValue = undefined;
  232. oneTimeWarning(heightAndPerPositionHeightWarning);
  233. }
  234. if (heightReferenceValue !== HeightReference.NONE && perPositionHeightValue) {
  235. heightValue = undefined;
  236. oneTimeWarning(heightReferenceAndPerPositionHeightWarning);
  237. }
  238. } else {
  239. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  240. heightValue = 0;
  241. }
  242. offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(heightValue, heightReferenceValue, extrudedHeightValue, extrudedHeightReferenceValue);
  243. }
  244. options.polygonHierarchy = hierarchyValue;
  245. options.granularity = Property.getValueOrUndefined(polygon.granularity, Iso8601.MINIMUM_VALUE);
  246. options.stRotation = Property.getValueOrUndefined(polygon.stRotation, Iso8601.MINIMUM_VALUE);
  247. options.perPositionHeight = perPositionHeightValue;
  248. options.closeTop = Property.getValueOrDefault(polygon.closeTop, Iso8601.MINIMUM_VALUE, true);
  249. options.closeBottom = Property.getValueOrDefault(polygon.closeBottom, Iso8601.MINIMUM_VALUE, true);
  250. options.offsetAttribute = offsetAttribute;
  251. options.height = heightValue;
  252. options.arcType = Property.getValueOrDefault(polygon.arcType, Iso8601.MINIMUM_VALUE, ArcType.GEODESIC);
  253. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(extrudedHeightValue, extrudedHeightReferenceValue);
  254. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  255. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(PolygonGeometry.computeRectangle(options, scratchRectangle)).minimumTerrainHeight;
  256. }
  257. options.extrudedHeight = extrudedHeightValue;
  258. };
  259. PolygonGeometryUpdater.prototype._getIsClosed = function(options) {
  260. var height = options.height;
  261. var extrudedHeight = options.extrudedHeight;
  262. var isExtruded = defined(extrudedHeight) && extrudedHeight !== height;
  263. return !options.perPositionHeight && (!isExtruded && height === 0 || (isExtruded && options.closeTop && options.closeBottom));
  264. };
  265. PolygonGeometryUpdater.DynamicGeometryUpdater = DyanmicPolygonGeometryUpdater;
  266. /**
  267. * @private
  268. */
  269. function DyanmicPolygonGeometryUpdater(geometryUpdater, primitives, groundPrimitives) {
  270. DynamicGeometryUpdater.call(this, geometryUpdater, primitives, groundPrimitives);
  271. }
  272. if (defined(Object.create)) {
  273. DyanmicPolygonGeometryUpdater.prototype = Object.create(DynamicGeometryUpdater.prototype);
  274. DyanmicPolygonGeometryUpdater.prototype.constructor = DyanmicPolygonGeometryUpdater;
  275. }
  276. DyanmicPolygonGeometryUpdater.prototype._isHidden = function(entity, polygon, time) {
  277. return !defined(this._options.polygonHierarchy) || DynamicGeometryUpdater.prototype._isHidden.call(this, entity, polygon, time);
  278. };
  279. DyanmicPolygonGeometryUpdater.prototype._setOptions = function(entity, polygon, time) {
  280. var options = this._options;
  281. options.polygonHierarchy = Property.getValueOrUndefined(polygon.hierarchy, time);
  282. var heightValue = Property.getValueOrUndefined(polygon.height, time);
  283. var heightReferenceValue = Property.getValueOrDefault(polygon.heightReference, time, HeightReference.NONE);
  284. var extrudedHeightReferenceValue = Property.getValueOrDefault(polygon.extrudedHeightReference, time, HeightReference.NONE);
  285. var extrudedHeightValue = Property.getValueOrUndefined(polygon.extrudedHeight, time);
  286. var perPositionHeightValue = Property.getValueOrUndefined(polygon.perPositionHeight, time);
  287. heightValue = GroundGeometryUpdater.getGeometryHeight(heightValue, extrudedHeightReferenceValue);
  288. var offsetAttribute;
  289. if (perPositionHeightValue) {
  290. if (defined(heightValue)) {
  291. heightValue = undefined;
  292. oneTimeWarning(heightAndPerPositionHeightWarning);
  293. }
  294. if (heightReferenceValue !== HeightReference.NONE && perPositionHeightValue) {
  295. heightValue = undefined;
  296. oneTimeWarning(heightReferenceAndPerPositionHeightWarning);
  297. }
  298. } else {
  299. if (defined(extrudedHeightValue) && !defined(heightValue)) {
  300. heightValue = 0;
  301. }
  302. offsetAttribute = GroundGeometryUpdater.computeGeometryOffsetAttribute(heightValue, heightReferenceValue, extrudedHeightValue, extrudedHeightReferenceValue);
  303. }
  304. options.granularity = Property.getValueOrUndefined(polygon.granularity, time);
  305. options.stRotation = Property.getValueOrUndefined(polygon.stRotation, time);
  306. options.perPositionHeight = Property.getValueOrUndefined(polygon.perPositionHeight, time);
  307. options.closeTop = Property.getValueOrDefault(polygon.closeTop, time, true);
  308. options.closeBottom = Property.getValueOrDefault(polygon.closeBottom, time, true);
  309. options.offsetAttribute = offsetAttribute;
  310. options.height = heightValue;
  311. options.arcType = Property.getValueOrDefault(polygon.arcType, time, ArcType.GEODESIC);
  312. extrudedHeightValue = GroundGeometryUpdater.getGeometryExtrudedHeight(extrudedHeightValue, extrudedHeightReferenceValue);
  313. if (extrudedHeightValue === GroundGeometryUpdater.CLAMP_TO_GROUND) {
  314. extrudedHeightValue = ApproximateTerrainHeights.getMinimumMaximumHeights(PolygonGeometry.computeRectangle(options, scratchRectangle)).minimumTerrainHeight;
  315. }
  316. options.extrudedHeight = extrudedHeightValue;
  317. };
  318. export default PolygonGeometryUpdater;