PolygonGraphics.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import defaultValue from "../Core/defaultValue.js";
  2. import defined from "../Core/defined.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import Event from "../Core/Event.js";
  5. import PolygonHierarchy from "../Core/PolygonHierarchy.js";
  6. import ConstantProperty from "./ConstantProperty.js";
  7. import createMaterialPropertyDescriptor from "./createMaterialPropertyDescriptor.js";
  8. import createPropertyDescriptor from "./createPropertyDescriptor.js";
  9. function createPolygonHierarchyProperty(value) {
  10. if (Array.isArray(value)) {
  11. // convert array of positions to PolygonHierarchy object
  12. value = new PolygonHierarchy(value);
  13. }
  14. return new ConstantProperty(value);
  15. }
  16. /**
  17. * @typedef {Object} PolygonGraphics.ConstructorOptions
  18. *
  19. * Initialization options for the PolygonGraphics constructor
  20. *
  21. * @property {Property | boolean} [show=true] A boolean Property specifying the visibility of the polygon.
  22. * @property {Property | PolygonHierarchy} [hierarchy] A Property specifying the {@link PolygonHierarchy}.
  23. * @property {Property | number} [height=0] A numeric Property specifying the altitude of the polygon relative to the ellipsoid surface.
  24. * @property {Property | HeightReference} [heightReference=HeightReference.NONE] A Property specifying what the height is relative to.
  25. * @property {Property | number} [extrudedHeight] A numeric Property specifying the altitude of the polygon's extruded face relative to the ellipsoid surface.
  26. * @property {Property | HeightReference} [extrudedHeightReference=HeightReference.NONE] A Property specifying what the extrudedHeight is relative to.
  27. * @property {Property | number} [stRotation=0.0] A numeric property specifying the rotation of the polygon texture counter-clockwise from north.
  28. * @property {Property | number} [granularity=Cesium.Math.RADIANS_PER_DEGREE] A numeric Property specifying the angular distance between each latitude and longitude point.
  29. * @property {Property | boolean} [fill=true] A boolean Property specifying whether the polygon is filled with the provided material.
  30. * @property {MaterialProperty | Color} [material=Color.WHITE] A Property specifying the material used to fill the polygon.
  31. * @property {Property | boolean} [outline=false] A boolean Property specifying whether the polygon is outlined.
  32. * @property {Property | Color} [outlineColor=Color.BLACK] A Property specifying the {@link Color} of the outline.
  33. * @property {Property | number} [outlineWidth=1.0] A numeric Property specifying the width of the outline.
  34. * @property {Property | boolean} [perPositionHeight=false] A boolean specifying whether or not the height of each position is used.
  35. * @property {Boolean | boolean} [closeTop=true] When false, leaves off the top of an extruded polygon open.
  36. * @property {Boolean | boolean} [closeBottom=true] When false, leaves off the bottom of an extruded polygon open.
  37. * @property {Property | ArcType} [arcType=ArcType.GEODESIC] The type of line the polygon edges must follow.
  38. * @property {Property | ShadowMode} [shadows=ShadowMode.DISABLED] An enum Property specifying whether the polygon casts or receives shadows from light sources.
  39. * @property {Property | DistanceDisplayCondition} [distanceDisplayCondition] A Property specifying at what distance from the camera that this polygon will be displayed.
  40. * @property {Property | ClassificationType} [classificationType=ClassificationType.BOTH] An enum Property specifying whether this polygon will classify terrain, 3D Tiles, or both when on the ground.
  41. * @property {ConstantProperty | number} [zIndex=0] A property specifying the zIndex used for ordering ground geometry. Only has an effect if the polygon is constant and neither height or extrudedHeight are specified.
  42. */
  43. /**
  44. * Describes a polygon defined by an hierarchy of linear rings which make up the outer shape and any nested holes.
  45. * The polygon conforms to the curvature of the globe and can be placed on the surface or
  46. * at altitude and can optionally be extruded into a volume.
  47. *
  48. * @alias PolygonGraphics
  49. * @constructor
  50. *
  51. * @param {PolygonGraphics.ConstructorOptions} [options] Object describing initialization options
  52. *
  53. * @see Entity
  54. * @demo {@link https://sandcastle.cesium.com/index.html?src=Polygon.html|Cesium Sandcastle Polygon Demo}
  55. */
  56. function PolygonGraphics(options) {
  57. this._definitionChanged = new Event();
  58. this._show = undefined;
  59. this._showSubscription = undefined;
  60. this._hierarchy = undefined;
  61. this._hierarchySubscription = undefined;
  62. this._height = undefined;
  63. this._heightSubscription = undefined;
  64. this._heightReference = undefined;
  65. this._heightReferenceSubscription = undefined;
  66. this._extrudedHeight = undefined;
  67. this._extrudedHeightSubscription = undefined;
  68. this._extrudedHeightReference = undefined;
  69. this._extrudedHeightReferenceSubscription = undefined;
  70. this._stRotation = undefined;
  71. this._stRotationSubscription = undefined;
  72. this._granularity = undefined;
  73. this._granularitySubscription = undefined;
  74. this._fill = undefined;
  75. this._fillSubscription = undefined;
  76. this._material = undefined;
  77. this._materialSubscription = undefined;
  78. this._outline = undefined;
  79. this._outlineSubscription = undefined;
  80. this._outlineColor = undefined;
  81. this._outlineColorSubscription = undefined;
  82. this._outlineWidth = undefined;
  83. this._outlineWidthSubscription = undefined;
  84. this._perPositionHeight = undefined;
  85. this._perPositionHeightSubscription = undefined;
  86. this._closeTop = undefined;
  87. this._closeTopSubscription = undefined;
  88. this._closeBottom = undefined;
  89. this._closeBottomSubscription = undefined;
  90. this._arcType = undefined;
  91. this._arcTypeSubscription = undefined;
  92. this._shadows = undefined;
  93. this._shadowsSubscription = undefined;
  94. this._distanceDisplayCondition = undefined;
  95. this._distanceDisplayConditionSubscription = undefined;
  96. this._classificationType = undefined;
  97. this._classificationTypeSubscription = undefined;
  98. this._zIndex = undefined;
  99. this._zIndexSubscription = undefined;
  100. this.merge(defaultValue(options, defaultValue.EMPTY_OBJECT));
  101. }
  102. Object.defineProperties(PolygonGraphics.prototype, {
  103. /**
  104. * Gets the event that is raised whenever a property or sub-property is changed or modified.
  105. * @memberof PolygonGraphics.prototype
  106. *
  107. * @type {Event}
  108. * @readonly
  109. */
  110. definitionChanged: {
  111. get: function () {
  112. return this._definitionChanged;
  113. },
  114. },
  115. /**
  116. * Gets or sets the boolean Property specifying the visibility of the polygon.
  117. * @memberof PolygonGraphics.prototype
  118. * @type {Property|undefined}
  119. * @default true
  120. */
  121. show: createPropertyDescriptor("show"),
  122. /**
  123. * Gets or sets the Property specifying the {@link PolygonHierarchy}.
  124. * @memberof PolygonGraphics.prototype
  125. * @type {Property|undefined}
  126. */
  127. hierarchy: createPropertyDescriptor(
  128. "hierarchy",
  129. undefined,
  130. createPolygonHierarchyProperty
  131. ),
  132. /**
  133. * Gets or sets the numeric Property specifying the constant altitude of the polygon.
  134. * @memberof PolygonGraphics.prototype
  135. * @type {Property|undefined}
  136. * @default 0.0
  137. */
  138. height: createPropertyDescriptor("height"),
  139. /**
  140. * Gets or sets the Property specifying the {@link HeightReference}.
  141. * @memberof PolygonGraphics.prototype
  142. * @type {Property|undefined}
  143. * @default HeightReference.NONE
  144. */
  145. heightReference: createPropertyDescriptor("heightReference"),
  146. /**
  147. * Gets or sets the numeric Property specifying the altitude of the polygon extrusion.
  148. * If {@link PolygonGraphics#perPositionHeight} is false, the volume starts at {@link PolygonGraphics#height} and ends at this altitude.
  149. * If {@link PolygonGraphics#perPositionHeight} is true, the volume starts at the height of each {@link PolygonGraphics#hierarchy} position and ends at this altitude.
  150. * @memberof PolygonGraphics.prototype
  151. * @type {Property|undefined}
  152. */
  153. extrudedHeight: createPropertyDescriptor("extrudedHeight"),
  154. /**
  155. * Gets or sets the Property specifying the extruded {@link HeightReference}.
  156. * @memberof PolygonGraphics.prototype
  157. * @type {Property|undefined}
  158. * @default HeightReference.NONE
  159. */
  160. extrudedHeightReference: createPropertyDescriptor("extrudedHeightReference"),
  161. /**
  162. * Gets or sets the numeric property specifying the rotation of the polygon texture counter-clockwise from north.
  163. * @memberof PolygonGraphics.prototype
  164. * @type {Property|undefined}
  165. * @default 0
  166. */
  167. stRotation: createPropertyDescriptor("stRotation"),
  168. /**
  169. * Gets or sets the numeric Property specifying the angular distance between points on the polygon.
  170. * @memberof PolygonGraphics.prototype
  171. * @type {Property|undefined}
  172. * @default {CesiumMath.RADIANS_PER_DEGREE}
  173. */
  174. granularity: createPropertyDescriptor("granularity"),
  175. /**
  176. * Gets or sets the boolean Property specifying whether the polygon is filled with the provided material.
  177. * @memberof PolygonGraphics.prototype
  178. * @type {Property|undefined}
  179. * @default true
  180. */
  181. fill: createPropertyDescriptor("fill"),
  182. /**
  183. * Gets or sets the Property specifying the material used to fill the polygon.
  184. * @memberof PolygonGraphics.prototype
  185. * @type {MaterialProperty}
  186. * @default Color.WHITE
  187. */
  188. material: createMaterialPropertyDescriptor("material"),
  189. /**
  190. * Gets or sets the Property specifying whether the polygon is outlined.
  191. * @memberof PolygonGraphics.prototype
  192. * @type {Property|undefined}
  193. * @default false
  194. */
  195. outline: createPropertyDescriptor("outline"),
  196. /**
  197. * Gets or sets the Property specifying the {@link Color} of the outline.
  198. * @memberof PolygonGraphics.prototype
  199. * @type {Property|undefined}
  200. * @default Color.BLACK
  201. */
  202. outlineColor: createPropertyDescriptor("outlineColor"),
  203. /**
  204. * Gets or sets the numeric Property specifying the width of the outline.
  205. * @memberof PolygonGraphics.prototype
  206. * @type {Property|undefined}
  207. * @default 1.0
  208. */
  209. outlineWidth: createPropertyDescriptor("outlineWidth"),
  210. /**
  211. * Gets or sets the boolean specifying whether or not the the height of each position is used.
  212. * If true, the shape will have non-uniform altitude defined by the height of each {@link PolygonGraphics#hierarchy} position.
  213. * If false, the shape will have a constant altitude as specified by {@link PolygonGraphics#height}.
  214. * @memberof PolygonGraphics.prototype
  215. * @type {Property|undefined}
  216. */
  217. perPositionHeight: createPropertyDescriptor("perPositionHeight"),
  218. /**
  219. * Gets or sets a boolean specifying whether or not the top of an extruded polygon is included.
  220. * @memberof PolygonGraphics.prototype
  221. * @type {Property|undefined}
  222. */
  223. closeTop: createPropertyDescriptor("closeTop"),
  224. /**
  225. * Gets or sets a boolean specifying whether or not the bottom of an extruded polygon is included.
  226. * @memberof PolygonGraphics.prototype
  227. * @type {Property|undefined}
  228. */
  229. closeBottom: createPropertyDescriptor("closeBottom"),
  230. /**
  231. * Gets or sets the {@link ArcType} Property specifying the type of lines the polygon edges use.
  232. * @memberof PolygonGraphics.prototype
  233. * @type {Property|undefined}
  234. * @default ArcType.GEODESIC
  235. */
  236. arcType: createPropertyDescriptor("arcType"),
  237. /**
  238. * Get or sets the enum Property specifying whether the polygon
  239. * casts or receives shadows from light sources.
  240. * @memberof PolygonGraphics.prototype
  241. * @type {Property|undefined}
  242. * @default ShadowMode.DISABLED
  243. */
  244. shadows: createPropertyDescriptor("shadows"),
  245. /**
  246. * Gets or sets the {@link DistanceDisplayCondition} Property specifying at what distance from the camera that this polygon will be displayed.
  247. * @memberof PolygonGraphics.prototype
  248. * @type {Property|undefined}
  249. */
  250. distanceDisplayCondition: createPropertyDescriptor(
  251. "distanceDisplayCondition"
  252. ),
  253. /**
  254. * Gets or sets the {@link ClassificationType} Property specifying whether this polygon will classify terrain, 3D Tiles, or both when on the ground.
  255. * @memberof PolygonGraphics.prototype
  256. * @type {Property|undefined}
  257. * @default ClassificationType.BOTH
  258. */
  259. classificationType: createPropertyDescriptor("classificationType"),
  260. /**
  261. * Gets or sets the zIndex Prperty specifying the ordering of ground geometry. Only has an effect if the polygon is constant and neither height or extrudedHeight are specified.
  262. * @memberof PolygonGraphics.prototype
  263. * @type {ConstantProperty|undefined}
  264. * @default 0
  265. */
  266. zIndex: createPropertyDescriptor("zIndex"),
  267. });
  268. /**
  269. * Duplicates this instance.
  270. *
  271. * @param {PolygonGraphics} [result] The object onto which to store the result.
  272. * @returns {PolygonGraphics} The modified result parameter or a new instance if one was not provided.
  273. */
  274. PolygonGraphics.prototype.clone = function (result) {
  275. if (!defined(result)) {
  276. return new PolygonGraphics(this);
  277. }
  278. result.show = this.show;
  279. result.hierarchy = this.hierarchy;
  280. result.height = this.height;
  281. result.heightReference = this.heightReference;
  282. result.extrudedHeight = this.extrudedHeight;
  283. result.extrudedHeightReference = this.extrudedHeightReference;
  284. result.stRotation = this.stRotation;
  285. result.granularity = this.granularity;
  286. result.fill = this.fill;
  287. result.material = this.material;
  288. result.outline = this.outline;
  289. result.outlineColor = this.outlineColor;
  290. result.outlineWidth = this.outlineWidth;
  291. result.perPositionHeight = this.perPositionHeight;
  292. result.closeTop = this.closeTop;
  293. result.closeBottom = this.closeBottom;
  294. result.arcType = this.arcType;
  295. result.shadows = this.shadows;
  296. result.distanceDisplayCondition = this.distanceDisplayCondition;
  297. result.classificationType = this.classificationType;
  298. result.zIndex = this.zIndex;
  299. return result;
  300. };
  301. /**
  302. * Assigns each unassigned property on this object to the value
  303. * of the same property on the provided source object.
  304. *
  305. * @param {PolygonGraphics} source The object to be merged into this object.
  306. */
  307. PolygonGraphics.prototype.merge = function (source) {
  308. //>>includeStart('debug', pragmas.debug);
  309. if (!defined(source)) {
  310. throw new DeveloperError("source is required.");
  311. }
  312. //>>includeEnd('debug');
  313. this.show = defaultValue(this.show, source.show);
  314. this.hierarchy = defaultValue(this.hierarchy, source.hierarchy);
  315. this.height = defaultValue(this.height, source.height);
  316. this.heightReference = defaultValue(
  317. this.heightReference,
  318. source.heightReference
  319. );
  320. this.extrudedHeight = defaultValue(
  321. this.extrudedHeight,
  322. source.extrudedHeight
  323. );
  324. this.extrudedHeightReference = defaultValue(
  325. this.extrudedHeightReference,
  326. source.extrudedHeightReference
  327. );
  328. this.stRotation = defaultValue(this.stRotation, source.stRotation);
  329. this.granularity = defaultValue(this.granularity, source.granularity);
  330. this.fill = defaultValue(this.fill, source.fill);
  331. this.material = defaultValue(this.material, source.material);
  332. this.outline = defaultValue(this.outline, source.outline);
  333. this.outlineColor = defaultValue(this.outlineColor, source.outlineColor);
  334. this.outlineWidth = defaultValue(this.outlineWidth, source.outlineWidth);
  335. this.perPositionHeight = defaultValue(
  336. this.perPositionHeight,
  337. source.perPositionHeight
  338. );
  339. this.closeTop = defaultValue(this.closeTop, source.closeTop);
  340. this.closeBottom = defaultValue(this.closeBottom, source.closeBottom);
  341. this.arcType = defaultValue(this.arcType, source.arcType);
  342. this.shadows = defaultValue(this.shadows, source.shadows);
  343. this.distanceDisplayCondition = defaultValue(
  344. this.distanceDisplayCondition,
  345. source.distanceDisplayCondition
  346. );
  347. this.classificationType = defaultValue(
  348. this.classificationType,
  349. source.classificationType
  350. );
  351. this.zIndex = defaultValue(this.zIndex, source.zIndex);
  352. };
  353. export default PolygonGraphics;