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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. import Cartesian2 from "../Core/Cartesian2.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Cartesian4 from "../Core/Cartesian4.js";
  4. import Check from "../Core/Check.js";
  5. import ClippingPlane from "./ClippingPlane.js";
  6. import ContextLimits from "../Renderer/ContextLimits.js";
  7. import defined from "../Core/defined.js";
  8. import destroyObject from "../Core/destroyObject.js";
  9. import Event from "../Core/Event.js";
  10. import Frozen from "../Core/Frozen.js";
  11. import Intersect from "../Core/Intersect.js";
  12. import Matrix4 from "../Core/Matrix4.js";
  13. import PixelFormat from "../Core/PixelFormat.js";
  14. import PixelDatatype from "../Renderer/PixelDatatype.js";
  15. import Plane from "../Core/Plane.js";
  16. import Sampler from "../Renderer/Sampler.js";
  17. import Texture from "../Renderer/Texture.js";
  18. /**
  19. * Specifies a set of clipping planes defining rendering bounds for a {@link VoxelPrimitive}.
  20. *
  21. * @alias VoxelBoundsCollection
  22. * @constructor
  23. *
  24. * @param {object} [options] Object with the following properties:
  25. * @param {ClippingPlane[]} [options.planes=[]] An array of {@link ClippingPlane} objects used to selectively disable rendering on the outside of each plane.
  26. * @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] The 4x4 transformation matrix specifying an additional transform relative to the clipping planes original coordinate system.
  27. * @param {boolean} [options.unionClippingRegions=false] If true, a region will be clipped if it is on the outside of any plane in the collection. Otherwise, a region will only be clipped if it is on the outside of every plane.
  28. *
  29. * @private
  30. */
  31. function VoxelBoundsCollection(options) {
  32. const {
  33. planes,
  34. modelMatrix = Matrix4.IDENTITY,
  35. unionClippingRegions = false,
  36. } = options ?? Frozen.EMPTY_OBJECT;
  37. this._planes = [];
  38. /**
  39. * The 4x4 transformation matrix specifying an additional transform relative to the clipping planes
  40. * original coordinate system.
  41. *
  42. * @type {Matrix4}
  43. * @default Matrix4.IDENTITY
  44. */
  45. this.modelMatrix = Matrix4.clone(modelMatrix);
  46. /**
  47. * An event triggered when a new clipping plane is added to the collection. Event handlers
  48. * are passed the new plane and the index at which it was added.
  49. * @type {Event}
  50. * @readonly
  51. */
  52. this.planeAdded = new Event();
  53. /**
  54. * An event triggered when a new clipping plane is removed from the collection. Event handlers
  55. * are passed the new plane and the index from which it was removed.
  56. * @type {Event}
  57. * @readonly
  58. */
  59. this.planeRemoved = new Event();
  60. this._unionClippingRegions = unionClippingRegions;
  61. this._testIntersection = unionClippingRegions
  62. ? unionIntersectFunction
  63. : defaultIntersectFunction;
  64. this._float32View = undefined;
  65. this._clippingPlanesTexture = undefined;
  66. // Add each ClippingPlane object.
  67. if (defined(planes)) {
  68. for (let i = 0; i < planes.length; ++i) {
  69. this.add(planes[i]);
  70. }
  71. }
  72. }
  73. function unionIntersectFunction(value) {
  74. return value === Intersect.OUTSIDE;
  75. }
  76. function defaultIntersectFunction(value) {
  77. return value === Intersect.INSIDE;
  78. }
  79. Object.defineProperties(VoxelBoundsCollection.prototype, {
  80. /**
  81. * Returns the number of planes in this collection. This is commonly used with
  82. * {@link VoxelBoundsCollection#get} to iterate over all the planes
  83. * in the collection.
  84. *
  85. * @memberof VoxelBoundsCollection.prototype
  86. * @type {number}
  87. * @readonly
  88. */
  89. length: {
  90. get: function () {
  91. return this._planes.length;
  92. },
  93. },
  94. /**
  95. * If true, a region will be clipped if it is on the outside of any plane in the
  96. * collection. Otherwise, a region will only be clipped if it is on the
  97. * outside of every plane.
  98. *
  99. * @memberof VoxelBoundsCollection.prototype
  100. * @type {boolean}
  101. * @default false
  102. */
  103. unionClippingRegions: {
  104. get: function () {
  105. return this._unionClippingRegions;
  106. },
  107. set: function (value) {
  108. //>>includeStart('debug', pragmas.debug);
  109. Check.typeOf.bool("value", value);
  110. //>>includeEnd('debug');
  111. if (this._unionClippingRegions === value) {
  112. return;
  113. }
  114. this._unionClippingRegions = value;
  115. this._testIntersection = value
  116. ? unionIntersectFunction
  117. : defaultIntersectFunction;
  118. },
  119. },
  120. /**
  121. * Returns a texture containing packed, untransformed clipping planes.
  122. *
  123. * @memberof VoxelBoundsCollection.prototype
  124. * @type {Texture}
  125. * @readonly
  126. * @private
  127. */
  128. texture: {
  129. get: function () {
  130. return this._clippingPlanesTexture;
  131. },
  132. },
  133. /**
  134. * Returns a Number encapsulating the state for this VoxelBoundsCollection.
  135. *
  136. * Clipping mode is encoded in the sign of the number, which is just the plane count.
  137. * If this value changes, then shader regeneration is necessary.
  138. *
  139. * @memberof VoxelBoundsCollection.prototype
  140. * @returns {number} A Number that describes the VoxelBoundsCollection's state.
  141. * @readonly
  142. * @private
  143. */
  144. clippingPlanesState: {
  145. get: function () {
  146. return this._unionClippingRegions
  147. ? this._planes.length
  148. : -this._planes.length;
  149. },
  150. },
  151. });
  152. /**
  153. * Adds the specified {@link ClippingPlane} to the collection to be used to selectively disable rendering
  154. * on the outside of each plane. Use {@link VoxelBoundsCollection#unionClippingRegions} to modify
  155. * how modify the clipping behavior of multiple planes.
  156. *
  157. * @param {ClippingPlane} plane The ClippingPlane to add to the collection.
  158. *
  159. * @see VoxelBoundsCollection#unionClippingRegions
  160. * @see VoxelBoundsCollection#remove
  161. * @see VoxelBoundsCollection#removeAll
  162. */
  163. VoxelBoundsCollection.prototype.add = function (plane) {
  164. const newPlaneIndex = this._planes.length;
  165. plane.index = newPlaneIndex;
  166. this._planes.push(plane);
  167. this.planeAdded.raiseEvent(plane, newPlaneIndex);
  168. };
  169. /**
  170. * Returns the plane in the collection at the specified index. Indices are zero-based
  171. * and increase as planes are added. Removing a plane shifts all planes after
  172. * it to the left, changing their indices. This function is commonly used with
  173. * {@link VoxelBoundsCollection#length} to iterate over all the planes
  174. * in the collection.
  175. *
  176. * @param {number} index The zero-based index of the plane.
  177. * @returns {ClippingPlane} The ClippingPlane at the specified index.
  178. *
  179. * @see VoxelBoundsCollection#length
  180. */
  181. VoxelBoundsCollection.prototype.get = function (index) {
  182. //>>includeStart('debug', pragmas.debug);
  183. Check.typeOf.number("index", index);
  184. //>>includeEnd('debug');
  185. return this._planes[index];
  186. };
  187. function indexOf(planes, plane) {
  188. for (let i = 0; i < planes.length; ++i) {
  189. if (Plane.equals(planes[i], plane)) {
  190. return i;
  191. }
  192. }
  193. return -1;
  194. }
  195. /**
  196. * Checks whether this collection contains a ClippingPlane equal to the given ClippingPlane.
  197. *
  198. * @param {ClippingPlane} [clippingPlane] The ClippingPlane to check for.
  199. * @returns {boolean} true if this collection contains the ClippingPlane, false otherwise.
  200. *
  201. * @see VoxelBoundsCollection#get
  202. */
  203. VoxelBoundsCollection.prototype.contains = function (clippingPlane) {
  204. return indexOf(this._planes, clippingPlane) !== -1;
  205. };
  206. /**
  207. * Removes the first occurrence of the given ClippingPlane from the collection.
  208. *
  209. * @param {ClippingPlane} clippingPlane
  210. * @returns {boolean} <code>true</code> if the plane was removed; <code>false</code> if the plane was not found in the collection.
  211. *
  212. * @see VoxelBoundsCollection#add
  213. * @see VoxelBoundsCollection#contains
  214. * @see VoxelBoundsCollection#removeAll
  215. */
  216. VoxelBoundsCollection.prototype.remove = function (clippingPlane) {
  217. const planes = this._planes;
  218. const index = indexOf(planes, clippingPlane);
  219. if (index === -1) {
  220. return false;
  221. }
  222. // Unlink this VoxelBoundsCollection from the ClippingPlane
  223. if (clippingPlane instanceof ClippingPlane) {
  224. clippingPlane.onChangeCallback = undefined;
  225. clippingPlane.index = -1;
  226. }
  227. // Shift and update indices
  228. const length = planes.length - 1;
  229. for (let i = index; i < length; ++i) {
  230. const planeToKeep = planes[i + 1];
  231. planes[i] = planeToKeep;
  232. if (planeToKeep instanceof ClippingPlane) {
  233. planeToKeep.index = i;
  234. }
  235. }
  236. planes.length = length;
  237. this.planeRemoved.raiseEvent(clippingPlane, index);
  238. return true;
  239. };
  240. /**
  241. * Removes all planes from the collection.
  242. *
  243. * @see VoxelBoundsCollection#add
  244. * @see VoxelBoundsCollection#remove
  245. */
  246. VoxelBoundsCollection.prototype.removeAll = function () {
  247. // Dereference this VoxelBoundsCollection from all ClippingPlanes
  248. const planes = this._planes;
  249. for (let i = 0; i < planes.length; ++i) {
  250. const plane = planes[i];
  251. if (plane instanceof ClippingPlane) {
  252. plane.onChangeCallback = undefined;
  253. plane.index = -1;
  254. }
  255. this.planeRemoved.raiseEvent(plane, i);
  256. }
  257. this._planes = [];
  258. };
  259. const scratchPlane = new Plane(Cartesian3.fromElements(1.0, 0.0, 0.0), 0.0);
  260. // Pack starting at the beginning of the buffer to allow partial update
  261. function transformAndPackPlanes(clippingPlaneCollection, transform) {
  262. const float32View = clippingPlaneCollection._float32View;
  263. const planes = clippingPlaneCollection._planes;
  264. let floatIndex = 0;
  265. for (let i = 0; i < planes.length; ++i) {
  266. const { normal, distance } = transformPlane(
  267. planes[i],
  268. transform,
  269. scratchPlane,
  270. );
  271. float32View[floatIndex] = normal.x;
  272. float32View[floatIndex + 1] = normal.y;
  273. float32View[floatIndex + 2] = normal.z;
  274. float32View[floatIndex + 3] = distance;
  275. floatIndex += 4; // each plane is 4 floats
  276. }
  277. }
  278. const scratchPlaneCartesian4 = new Cartesian4();
  279. const scratchTransformedNormal = new Cartesian3();
  280. function transformPlane(plane, transform, result) {
  281. //>>includeStart('debug', pragmas.debug);
  282. Check.typeOf.object("plane", plane);
  283. Check.typeOf.object("transform", transform);
  284. //>>includeEnd('debug');
  285. const { normal, distance } = plane;
  286. const planeAsCartesian4 = Cartesian4.fromElements(
  287. normal.x,
  288. normal.y,
  289. normal.z,
  290. distance,
  291. scratchPlaneCartesian4,
  292. );
  293. let transformedPlane = Matrix4.multiplyByVector(
  294. transform,
  295. planeAsCartesian4,
  296. scratchPlaneCartesian4,
  297. );
  298. // Convert the transformed plane to Hessian Normal Form
  299. const transformedNormal = Cartesian3.fromCartesian4(
  300. transformedPlane,
  301. scratchTransformedNormal,
  302. );
  303. transformedPlane = Cartesian4.divideByScalar(
  304. transformedPlane,
  305. Cartesian3.magnitude(transformedNormal),
  306. scratchPlaneCartesian4,
  307. );
  308. return Plane.fromCartesian4(transformedPlane, result);
  309. }
  310. function computeTextureResolution(pixelsNeeded, result) {
  311. result.x = Math.min(pixelsNeeded, ContextLimits.maximumTextureSize);
  312. result.y = Math.ceil(pixelsNeeded / result.x);
  313. return result;
  314. }
  315. const textureResolutionScratch = new Cartesian2();
  316. /**
  317. * Called when {@link Viewer} or {@link CesiumWidget} render the scene to
  318. * build the resources for clipping planes.
  319. * <p>
  320. * Do not call this function directly.
  321. * </p>
  322. */
  323. VoxelBoundsCollection.prototype.update = function (frameState, transform) {
  324. let clippingPlanesTexture = this._clippingPlanesTexture;
  325. // Compute texture requirements for current planes
  326. // In RGBA FLOAT, a plane is 4 floats packed to a single RGBA pixel.
  327. const pixelsNeeded = this.length;
  328. if (defined(clippingPlanesTexture)) {
  329. const currentPixelCount =
  330. clippingPlanesTexture.width * clippingPlanesTexture.height;
  331. // Recreate the texture to double current requirement if it isn't big enough or is 4 times larger than it needs to be.
  332. // Optimization note: this isn't exactly the classic resizeable array algorithm
  333. // * not necessarily checking for resize after each add/remove operation
  334. // * random-access deletes instead of just pops
  335. // * alloc ops likely more expensive than demonstrable via big-O analysis
  336. if (
  337. currentPixelCount < pixelsNeeded ||
  338. pixelsNeeded < 0.25 * currentPixelCount
  339. ) {
  340. clippingPlanesTexture.destroy();
  341. clippingPlanesTexture = undefined;
  342. this._clippingPlanesTexture = undefined;
  343. }
  344. }
  345. // If there are no bound planes, there's nothing to update.
  346. if (this.length === 0) {
  347. return;
  348. }
  349. if (!defined(clippingPlanesTexture)) {
  350. const requiredResolution = computeTextureResolution(
  351. pixelsNeeded,
  352. textureResolutionScratch,
  353. );
  354. // Allocate twice as much space as needed to avoid frequent texture reallocation.
  355. // Allocate in the Y direction, since texture may be as wide as context texture support.
  356. requiredResolution.y *= 2;
  357. clippingPlanesTexture = new Texture({
  358. context: frameState.context,
  359. width: requiredResolution.x,
  360. height: requiredResolution.y,
  361. pixelFormat: PixelFormat.RGBA,
  362. pixelDatatype: PixelDatatype.FLOAT,
  363. sampler: Sampler.NEAREST,
  364. flipY: false,
  365. });
  366. this._float32View = new Float32Array(
  367. requiredResolution.x * requiredResolution.y * 4,
  368. );
  369. this._clippingPlanesTexture = clippingPlanesTexture;
  370. }
  371. const { width, height } = clippingPlanesTexture;
  372. transformAndPackPlanes(this, transform);
  373. clippingPlanesTexture.copyFrom({
  374. source: {
  375. width: width,
  376. height: height,
  377. arrayBufferView: this._float32View,
  378. },
  379. });
  380. };
  381. /**
  382. * Function for getting the clipping plane collection's texture resolution.
  383. * If the VoxelBoundsCollection hasn't been updated, returns the resolution that will be
  384. * allocated based on the current plane count.
  385. *
  386. * @param {VoxelBoundsCollection} clippingPlaneCollection The clipping plane collection
  387. * @param {Context} context The rendering context
  388. * @param {Cartesian2} result A Cartesian2 for the result.
  389. * @returns {Cartesian2} The required resolution.
  390. * @private
  391. */
  392. VoxelBoundsCollection.getTextureResolution = function (
  393. clippingPlaneCollection,
  394. context,
  395. result,
  396. ) {
  397. const texture = clippingPlaneCollection.texture;
  398. if (defined(texture)) {
  399. result.x = texture.width;
  400. result.y = texture.height;
  401. return result;
  402. }
  403. const pixelsNeeded = clippingPlaneCollection.length;
  404. const requiredResolution = computeTextureResolution(pixelsNeeded, result);
  405. // Allocate twice as much space as needed to avoid frequent texture reallocation.
  406. requiredResolution.y *= 2;
  407. return requiredResolution;
  408. };
  409. /**
  410. * Returns true if this object was destroyed; otherwise, false.
  411. * <br /><br />
  412. * If this object was destroyed, it should not be used; calling any function other than
  413. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
  414. *
  415. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  416. *
  417. * @see VoxelBoundsCollection#destroy
  418. */
  419. VoxelBoundsCollection.prototype.isDestroyed = function () {
  420. return false;
  421. };
  422. /**
  423. * Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
  424. * release of WebGL resources, instead of relying on the garbage collector to destroy this object.
  425. * <br />
  426. * Once an object is destroyed, it should not be used; calling any function other than
  427. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  428. * assign the return value (<code>undefined</code>) to the object as done in the example.
  429. *
  430. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  431. *
  432. * @example
  433. * voxelBounds = voxelBounds && voxelBounds.destroy();
  434. *
  435. * @see VoxelBoundsCollection#isDestroyed
  436. */
  437. VoxelBoundsCollection.prototype.destroy = function () {
  438. this._clippingPlanesTexture =
  439. this._clippingPlanesTexture && this._clippingPlanesTexture.destroy();
  440. return destroyObject(this);
  441. };
  442. export default VoxelBoundsCollection;