智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. import createGuid from "../Core/createGuid.js";
  2. import Frozen from "../Core/Frozen.js";
  3. import defined from "../Core/defined.js";
  4. import destroyObject from "../Core/destroyObject.js";
  5. import DeveloperError from "../Core/DeveloperError.js";
  6. import Event from "../Core/Event.js";
  7. /**
  8. * A collection of primitives. This is most often used with {@link Scene#primitives},
  9. * but <code>PrimitiveCollection</code> is also a primitive itself so collections can
  10. * be added to collections forming a hierarchy.
  11. *
  12. * @alias PrimitiveCollection
  13. * @constructor
  14. *
  15. * @param {object} [options] Object with the following properties:
  16. * @param {boolean} [options.show=true] Determines if the primitives in the collection will be shown.
  17. * @param {boolean} [options.destroyPrimitives=true] Determines if primitives in the collection are destroyed when they are removed.
  18. * @privateParam {boolean} [options.countReferences=false] Specifies whether adding and removing primitives from this collection alters their reference counts. If so, adding a
  19. * primitive to this collection increments its reference count. Removing the primitive decrements its reference count and - if the count reaches zero **and** destroyPrimitives is true - destroys the primitive.
  20. * This permits primitives to be shared between multiple collections.
  21. *
  22. * @example
  23. * const billboards = new Cesium.BillboardCollection();
  24. * const labels = new Cesium.LabelCollection();
  25. *
  26. * const collection = new Cesium.PrimitiveCollection();
  27. * collection.add(billboards);
  28. *
  29. * scene.primitives.add(collection); // Add collection
  30. * scene.primitives.add(labels); // Add regular primitive
  31. */
  32. function PrimitiveCollection(options) {
  33. options = options ?? Frozen.EMPTY_OBJECT;
  34. this._primitives = [];
  35. this._guid = createGuid();
  36. this._primitiveAdded = new Event();
  37. this._primitiveRemoved = new Event();
  38. // Used by the OrderedGroundPrimitiveCollection
  39. this._zIndex = undefined;
  40. /**
  41. * Determines if primitives in this collection will be shown.
  42. *
  43. * @type {boolean}
  44. * @default true
  45. */
  46. this.show = options.show ?? true;
  47. /**
  48. * Determines if primitives in the collection are destroyed when they are removed by
  49. * {@link PrimitiveCollection#destroy} or {@link PrimitiveCollection#remove} or implicitly
  50. * by {@link PrimitiveCollection#removeAll}.
  51. *
  52. * @type {boolean}
  53. * @default true
  54. *
  55. * @example
  56. * // Example 1. Primitives are destroyed by default.
  57. * const primitives = new Cesium.PrimitiveCollection();
  58. * const labels = primitives.add(new Cesium.LabelCollection());
  59. * primitives = primitives.destroy();
  60. * const b = labels.isDestroyed(); // true
  61. *
  62. * @example
  63. * // Example 2. Do not destroy primitives in a collection.
  64. * const primitives = new Cesium.PrimitiveCollection();
  65. * primitives.destroyPrimitives = false;
  66. * const labels = primitives.add(new Cesium.LabelCollection());
  67. * primitives = primitives.destroy();
  68. * const b = labels.isDestroyed(); // false
  69. * labels = labels.destroy(); // explicitly destroy
  70. */
  71. this.destroyPrimitives = options.destroyPrimitives ?? true;
  72. this._countReferences = options.countReferences ?? false;
  73. }
  74. Object.defineProperties(PrimitiveCollection.prototype, {
  75. /**
  76. * Gets the number of primitives in the collection.
  77. *
  78. * @memberof PrimitiveCollection.prototype
  79. *
  80. * @type {number}
  81. * @readonly
  82. */
  83. length: {
  84. get: function () {
  85. return this._primitives.length;
  86. },
  87. },
  88. /**
  89. * An event that is raised when a primitive is added to the collection.
  90. * Event handlers are passed the primitive that was added.
  91. * @memberof PrimitiveCollection.prototype
  92. * @type {Event}
  93. * @readonly
  94. */
  95. primitiveAdded: {
  96. get: function () {
  97. return this._primitiveAdded;
  98. },
  99. },
  100. /**
  101. * An event that is raised when a primitive is removed from the collection.
  102. * Event handlers are passed the primitive that was removed.
  103. * <p>
  104. * Note: Depending on the destroyPrimitives constructor option, the primitive may already be destroyed.
  105. * </p>
  106. * @memberof PrimitiveCollection.prototype
  107. * @type {Event}
  108. * @readonly
  109. */
  110. primitiveRemoved: {
  111. get: function () {
  112. return this._primitiveRemoved;
  113. },
  114. },
  115. });
  116. /**
  117. * Adds a primitive to the collection.
  118. *
  119. * @param {object} primitive The primitive to add.
  120. * @param {number} [index] The index to add the layer at. If omitted, the primitive will be added at the bottom of all existing primitives.
  121. * @returns {object} The primitive added to the collection.
  122. *
  123. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  124. *
  125. * @example
  126. * const billboards = scene.primitives.add(new Cesium.BillboardCollection());
  127. */
  128. PrimitiveCollection.prototype.add = function (primitive, index) {
  129. const hasIndex = defined(index);
  130. //>>includeStart('debug', pragmas.debug);
  131. if (!defined(primitive)) {
  132. throw new DeveloperError("primitive is required.");
  133. }
  134. if (hasIndex) {
  135. if (index < 0) {
  136. throw new DeveloperError("index must be greater than or equal to zero.");
  137. } else if (index > this._primitives.length) {
  138. throw new DeveloperError(
  139. "index must be less than or equal to the number of primitives.",
  140. );
  141. }
  142. }
  143. //>>includeEnd('debug');
  144. const external = (primitive._external = primitive._external || {});
  145. const composites = (external._composites = external._composites || {});
  146. composites[this._guid] = {
  147. collection: this,
  148. };
  149. if (!hasIndex) {
  150. this._primitives.push(primitive);
  151. } else {
  152. this._primitives.splice(index, 0, primitive);
  153. }
  154. if (this._countReferences) {
  155. if (!defined(external._referenceCount)) {
  156. external._referenceCount = 1;
  157. } else {
  158. ++external._referenceCount;
  159. }
  160. }
  161. this._primitiveAdded.raiseEvent(primitive);
  162. return primitive;
  163. };
  164. /**
  165. * Removes a primitive from the collection.
  166. *
  167. * @param {object} [primitive] The primitive to remove.
  168. * @returns {boolean} <code>true</code> if the primitive was removed; <code>false</code> if the primitive is <code>undefined</code> or was not found in the collection.
  169. *
  170. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  171. *
  172. *
  173. * @example
  174. * const billboards = scene.primitives.add(new Cesium.BillboardCollection());
  175. * scene.primitives.remove(billboards); // Returns true
  176. *
  177. * @see PrimitiveCollection#destroyPrimitives
  178. */
  179. PrimitiveCollection.prototype.remove = function (primitive) {
  180. // PERFORMANCE_IDEA: We can obviously make this a lot faster.
  181. if (this.contains(primitive)) {
  182. const index = this._primitives.indexOf(primitive);
  183. if (index !== -1) {
  184. this._primitives.splice(index, 1);
  185. delete primitive._external._composites[this._guid];
  186. if (this._countReferences) {
  187. primitive._external._referenceCount--;
  188. }
  189. if (
  190. this.destroyPrimitives &&
  191. (!this._countReferences || primitive._external._referenceCount <= 0)
  192. ) {
  193. primitive.destroy();
  194. }
  195. this._primitiveRemoved.raiseEvent(primitive);
  196. return true;
  197. }
  198. // else ... this is not possible, I swear.
  199. }
  200. return false;
  201. };
  202. /**
  203. * Removes and destroys a primitive, regardless of destroyPrimitives or countReferences setting.
  204. * @private
  205. */
  206. PrimitiveCollection.prototype.removeAndDestroy = function (primitive) {
  207. const removed = this.remove(primitive);
  208. if (removed && !this.destroyPrimitives) {
  209. primitive.destroy();
  210. }
  211. return removed;
  212. };
  213. /**
  214. * Removes all primitives in the collection.
  215. *
  216. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  217. *
  218. * @see PrimitiveCollection#destroyPrimitives
  219. */
  220. PrimitiveCollection.prototype.removeAll = function () {
  221. const primitives = this._primitives;
  222. const length = primitives.length;
  223. for (let i = 0; i < length; ++i) {
  224. const primitive = primitives[i];
  225. delete primitive._external._composites[this._guid];
  226. if (this._countReferences) {
  227. primitive._external._referenceCount--;
  228. }
  229. if (
  230. this.destroyPrimitives &&
  231. (!this._countReferences || primitive._external._referenceCount <= 0)
  232. ) {
  233. primitive.destroy();
  234. }
  235. this._primitiveRemoved.raiseEvent(primitive);
  236. }
  237. this._primitives = [];
  238. };
  239. /**
  240. * Determines if this collection contains a primitive.
  241. *
  242. * @param {object} [primitive] The primitive to check for.
  243. * @returns {boolean} <code>true</code> if the primitive is in the collection; <code>false</code> if the primitive is <code>undefined</code> or was not found in the collection.
  244. *
  245. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  246. *
  247. * @see PrimitiveCollection#get
  248. */
  249. PrimitiveCollection.prototype.contains = function (primitive) {
  250. return !!(
  251. defined(primitive) &&
  252. primitive._external &&
  253. primitive._external._composites &&
  254. primitive._external._composites[this._guid]
  255. );
  256. };
  257. function getPrimitiveIndex(compositePrimitive, primitive) {
  258. //>>includeStart('debug', pragmas.debug);
  259. if (!compositePrimitive.contains(primitive)) {
  260. throw new DeveloperError("primitive is not in this collection.");
  261. }
  262. //>>includeEnd('debug');
  263. return compositePrimitive._primitives.indexOf(primitive);
  264. }
  265. /**
  266. * Raises a primitive "up one" in the collection. If all primitives in the collection are drawn
  267. * on the globe surface, this visually moves the primitive up one.
  268. *
  269. * @param {object} [primitive] The primitive to raise.
  270. *
  271. * @exception {DeveloperError} primitive is not in this collection.
  272. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  273. *
  274. * @see PrimitiveCollection#raiseToTop
  275. * @see PrimitiveCollection#lower
  276. * @see PrimitiveCollection#lowerToBottom
  277. */
  278. PrimitiveCollection.prototype.raise = function (primitive) {
  279. if (defined(primitive)) {
  280. const index = getPrimitiveIndex(this, primitive);
  281. const primitives = this._primitives;
  282. if (index !== primitives.length - 1) {
  283. const p = primitives[index];
  284. primitives[index] = primitives[index + 1];
  285. primitives[index + 1] = p;
  286. }
  287. }
  288. };
  289. /**
  290. * Raises a primitive to the "top" of the collection. If all primitives in the collection are drawn
  291. * on the globe surface, this visually moves the primitive to the top.
  292. *
  293. * @param {object} [primitive] The primitive to raise the top.
  294. *
  295. * @exception {DeveloperError} primitive is not in this collection.
  296. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  297. *
  298. * @see PrimitiveCollection#raise
  299. * @see PrimitiveCollection#lower
  300. * @see PrimitiveCollection#lowerToBottom
  301. */
  302. PrimitiveCollection.prototype.raiseToTop = function (primitive) {
  303. if (defined(primitive)) {
  304. const index = getPrimitiveIndex(this, primitive);
  305. const primitives = this._primitives;
  306. if (index !== primitives.length - 1) {
  307. // PERFORMANCE_IDEA: Could be faster
  308. primitives.splice(index, 1);
  309. primitives.push(primitive);
  310. }
  311. }
  312. };
  313. /**
  314. * Lowers a primitive "down one" in the collection. If all primitives in the collection are drawn
  315. * on the globe surface, this visually moves the primitive down one.
  316. *
  317. * @param {object} [primitive] The primitive to lower.
  318. *
  319. * @exception {DeveloperError} primitive is not in this collection.
  320. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  321. *
  322. * @see PrimitiveCollection#lowerToBottom
  323. * @see PrimitiveCollection#raise
  324. * @see PrimitiveCollection#raiseToTop
  325. */
  326. PrimitiveCollection.prototype.lower = function (primitive) {
  327. if (defined(primitive)) {
  328. const index = getPrimitiveIndex(this, primitive);
  329. const primitives = this._primitives;
  330. if (index !== 0) {
  331. const p = primitives[index];
  332. primitives[index] = primitives[index - 1];
  333. primitives[index - 1] = p;
  334. }
  335. }
  336. };
  337. /**
  338. * Lowers a primitive to the "bottom" of the collection. If all primitives in the collection are drawn
  339. * on the globe surface, this visually moves the primitive to the bottom.
  340. *
  341. * @param {object} [primitive] The primitive to lower to the bottom.
  342. *
  343. * @exception {DeveloperError} primitive is not in this collection.
  344. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  345. *
  346. * @see PrimitiveCollection#lower
  347. * @see PrimitiveCollection#raise
  348. * @see PrimitiveCollection#raiseToTop
  349. */
  350. PrimitiveCollection.prototype.lowerToBottom = function (primitive) {
  351. if (defined(primitive)) {
  352. const index = getPrimitiveIndex(this, primitive);
  353. const primitives = this._primitives;
  354. if (index !== 0) {
  355. // PERFORMANCE_IDEA: Could be faster
  356. primitives.splice(index, 1);
  357. primitives.unshift(primitive);
  358. }
  359. }
  360. };
  361. /**
  362. * Returns the primitive in the collection at the specified index.
  363. *
  364. * @param {number} index The zero-based index of the primitive to return.
  365. * @returns {object} The primitive at the <code>index</code>.
  366. *
  367. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  368. *
  369. *
  370. * @example
  371. * // Toggle the show property of every primitive in the collection.
  372. * const primitives = scene.primitives;
  373. * const length = primitives.length;
  374. * for (let i = 0; i < length; ++i) {
  375. * const p = primitives.get(i);
  376. * p.show = !p.show;
  377. * }
  378. *
  379. * @see PrimitiveCollection#length
  380. */
  381. PrimitiveCollection.prototype.get = function (index) {
  382. //>>includeStart('debug', pragmas.debug);
  383. if (!defined(index)) {
  384. throw new DeveloperError("index is required.");
  385. }
  386. //>>includeEnd('debug');
  387. return this._primitives[index];
  388. };
  389. /**
  390. * @private
  391. */
  392. PrimitiveCollection.prototype.update = function (frameState) {
  393. if (!this.show) {
  394. return;
  395. }
  396. const primitives = this._primitives;
  397. // Using primitives.length in the loop is a temporary workaround
  398. // to allow quadtree updates to add and remove primitives in
  399. // update(). This will be changed to manage added and removed lists.
  400. for (let i = 0; i < primitives.length; ++i) {
  401. primitives[i].update(frameState);
  402. }
  403. };
  404. /**
  405. * @private
  406. */
  407. PrimitiveCollection.prototype.prePassesUpdate = function (frameState) {
  408. const primitives = this._primitives;
  409. // Using primitives.length in the loop is a temporary workaround
  410. // to allow quadtree updates to add and remove primitives in
  411. // update(). This will be changed to manage added and removed lists.
  412. for (let i = 0; i < primitives.length; ++i) {
  413. const primitive = primitives[i];
  414. if (defined(primitive.prePassesUpdate)) {
  415. primitive.prePassesUpdate(frameState);
  416. }
  417. }
  418. };
  419. /**
  420. * @private
  421. */
  422. PrimitiveCollection.prototype.updateForPass = function (frameState, passState) {
  423. const primitives = this._primitives;
  424. // Using primitives.length in the loop is a temporary workaround
  425. // to allow quadtree updates to add and remove primitives in
  426. // update(). This will be changed to manage added and removed lists.
  427. for (let i = 0; i < primitives.length; ++i) {
  428. const primitive = primitives[i];
  429. if (defined(primitive.updateForPass)) {
  430. primitive.updateForPass(frameState, passState);
  431. }
  432. }
  433. };
  434. /**
  435. * @private
  436. */
  437. PrimitiveCollection.prototype.postPassesUpdate = function (frameState) {
  438. const primitives = this._primitives;
  439. // Using primitives.length in the loop is a temporary workaround
  440. // to allow quadtree updates to add and remove primitives in
  441. // update(). This will be changed to manage added and removed lists.
  442. for (let i = 0; i < primitives.length; ++i) {
  443. const primitive = primitives[i];
  444. if (defined(primitive.postPassesUpdate)) {
  445. primitive.postPassesUpdate(frameState);
  446. }
  447. }
  448. };
  449. /**
  450. * Returns true if this object was destroyed; otherwise, false.
  451. * <br /><br />
  452. * If this object was destroyed, it should not be used; calling any function other than
  453. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  454. *
  455. * @returns {boolean} True if this object was destroyed; otherwise, false.
  456. *
  457. * @see PrimitiveCollection#destroy
  458. */
  459. PrimitiveCollection.prototype.isDestroyed = function () {
  460. return false;
  461. };
  462. /**
  463. * Destroys the WebGL resources held by each primitive in this collection. Explicitly destroying this
  464. * collection allows for deterministic release of WebGL resources, instead of relying on the garbage
  465. * collector to destroy this collection.
  466. * <br /><br />
  467. * Since destroying a collection destroys all the contained primitives, only destroy a collection
  468. * when you are sure no other code is still using any of the contained primitives.
  469. * <br /><br />
  470. * Once this collection is destroyed, it should not be used; calling any function other than
  471. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  472. * assign the return value (<code>undefined</code>) to the object as done in the example.
  473. *
  474. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  475. *
  476. *
  477. * @example
  478. * primitives = primitives && primitives.destroy();
  479. *
  480. * @see PrimitiveCollection#isDestroyed
  481. */
  482. PrimitiveCollection.prototype.destroy = function () {
  483. this.removeAll();
  484. return destroyObject(this);
  485. };
  486. export default PrimitiveCollection;