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

TileBoundingRegion.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import BoundingSphere from "../Core/BoundingSphere.js";
  2. import Cartesian3 from "../Core/Cartesian3.js";
  3. import Cartographic from "../Core/Cartographic.js";
  4. import Check from "../Core/Check.js";
  5. import ColorGeometryInstanceAttribute from "../Core/ColorGeometryInstanceAttribute.js";
  6. import defined from "../Core/defined.js";
  7. import Ellipsoid from "../Core/Ellipsoid.js";
  8. import GeometryInstance from "../Core/GeometryInstance.js";
  9. import IntersectionTests from "../Core/IntersectionTests.js";
  10. import Matrix4 from "../Core/Matrix4.js";
  11. import OrientedBoundingBox from "../Core/OrientedBoundingBox.js";
  12. import Plane from "../Core/Plane.js";
  13. import Ray from "../Core/Ray.js";
  14. import Rectangle from "../Core/Rectangle.js";
  15. import RectangleOutlineGeometry from "../Core/RectangleOutlineGeometry.js";
  16. import PerInstanceColorAppearance from "./PerInstanceColorAppearance.js";
  17. import Primitive from "./Primitive.js";
  18. import SceneMode from "./SceneMode.js";
  19. /**
  20. * A tile bounding volume specified as a longitude/latitude/height region.
  21. * @alias TileBoundingRegion
  22. * @constructor
  23. *
  24. * @param {object} options Object with the following properties:
  25. * @param {Rectangle} options.rectangle The rectangle specifying the longitude and latitude range of the region.
  26. * @param {number} [options.minimumHeight=0.0] The minimum height of the region.
  27. * @param {number} [options.maximumHeight=0.0] The maximum height of the region.
  28. * @param {Ellipsoid} [options.ellipsoid=Cesium.Ellipsoid.WGS84] The ellipsoid.
  29. * @param {boolean} [options.computeBoundingVolumes=true] True to compute the {@link TileBoundingRegion#boundingVolume} and
  30. * {@link TileBoundingVolume#boundingSphere}. If false, these properties will be undefined.
  31. *
  32. * @private
  33. */
  34. function TileBoundingRegion(options) {
  35. //>>includeStart('debug', pragmas.debug);
  36. Check.typeOf.object("options", options);
  37. Check.typeOf.object("options.rectangle", options.rectangle);
  38. //>>includeEnd('debug');
  39. this.rectangle = Rectangle.clone(options.rectangle);
  40. this.minimumHeight = options.minimumHeight ?? 0.0;
  41. this.maximumHeight = options.maximumHeight ?? 0.0;
  42. /**
  43. * The world coordinates of the southwest corner of the tile's rectangle.
  44. *
  45. * @type {Cartesian3}
  46. * @default Cartesian3()
  47. */
  48. this.southwestCornerCartesian = new Cartesian3();
  49. /**
  50. * The world coordinates of the northeast corner of the tile's rectangle.
  51. *
  52. * @type {Cartesian3}
  53. * @default Cartesian3()
  54. */
  55. this.northeastCornerCartesian = new Cartesian3();
  56. /**
  57. * A normal that, along with southwestCornerCartesian, defines a plane at the western edge of
  58. * the tile. Any position above (in the direction of the normal) this plane is outside the tile.
  59. *
  60. * @type {Cartesian3}
  61. * @default Cartesian3()
  62. */
  63. this.westNormal = new Cartesian3();
  64. /**
  65. * A normal that, along with southwestCornerCartesian, defines a plane at the southern edge of
  66. * the tile. Any position above (in the direction of the normal) this plane is outside the tile.
  67. * Because points of constant latitude do not necessary lie in a plane, positions below this
  68. * plane are not necessarily inside the tile, but they are close.
  69. *
  70. * @type {Cartesian3}
  71. * @default Cartesian3()
  72. */
  73. this.southNormal = new Cartesian3();
  74. /**
  75. * A normal that, along with northeastCornerCartesian, defines a plane at the eastern edge of
  76. * the tile. Any position above (in the direction of the normal) this plane is outside the tile.
  77. *
  78. * @type {Cartesian3}
  79. * @default Cartesian3()
  80. */
  81. this.eastNormal = new Cartesian3();
  82. /**
  83. * A normal that, along with northeastCornerCartesian, defines a plane at the eastern edge of
  84. * the tile. Any position above (in the direction of the normal) this plane is outside the tile.
  85. * Because points of constant latitude do not necessary lie in a plane, positions below this
  86. * plane are not necessarily inside the tile, but they are close.
  87. *
  88. * @type {Cartesian3}
  89. * @default Cartesian3()
  90. */
  91. this.northNormal = new Cartesian3();
  92. const ellipsoid = options.ellipsoid ?? Ellipsoid.WGS84;
  93. computeBox(this, options.rectangle, ellipsoid);
  94. this._orientedBoundingBox = undefined;
  95. this._boundingSphere = undefined;
  96. if (options.computeBoundingVolumes ?? true) {
  97. this.computeBoundingVolumes(ellipsoid);
  98. }
  99. }
  100. Object.defineProperties(TileBoundingRegion.prototype, {
  101. /**
  102. * The underlying bounding volume
  103. *
  104. * @memberof TileBoundingRegion.prototype
  105. *
  106. * @type {object}
  107. * @readonly
  108. */
  109. boundingVolume: {
  110. get: function () {
  111. return this._orientedBoundingBox;
  112. },
  113. },
  114. /**
  115. * The underlying bounding sphere
  116. *
  117. * @memberof TileBoundingRegion.prototype
  118. *
  119. * @type {BoundingSphere}
  120. * @readonly
  121. */
  122. boundingSphere: {
  123. get: function () {
  124. return this._boundingSphere;
  125. },
  126. },
  127. });
  128. TileBoundingRegion.prototype.computeBoundingVolumes = function (ellipsoid) {
  129. // An oriented bounding box that encloses this tile's region. This is used to calculate tile visibility.
  130. this._orientedBoundingBox = OrientedBoundingBox.fromRectangle(
  131. this.rectangle,
  132. this.minimumHeight,
  133. this.maximumHeight,
  134. ellipsoid,
  135. );
  136. this._boundingSphere = BoundingSphere.fromOrientedBoundingBox(
  137. this._orientedBoundingBox,
  138. );
  139. };
  140. const cartesian3Scratch = new Cartesian3();
  141. const cartesian3Scratch2 = new Cartesian3();
  142. const cartesian3Scratch3 = new Cartesian3();
  143. const westNormalScratch = new Cartesian3();
  144. const eastWestNormalScratch = new Cartesian3();
  145. const westernMidpointScratch = new Cartesian3();
  146. const easternMidpointScratch = new Cartesian3();
  147. const cartographicScratch = new Cartographic();
  148. const planeScratch = new Plane(Cartesian3.UNIT_X, 0.0);
  149. const rayScratch = new Ray();
  150. function computeBox(tileBB, rectangle, ellipsoid) {
  151. ellipsoid.cartographicToCartesian(
  152. Rectangle.southwest(rectangle),
  153. tileBB.southwestCornerCartesian,
  154. );
  155. ellipsoid.cartographicToCartesian(
  156. Rectangle.northeast(rectangle),
  157. tileBB.northeastCornerCartesian,
  158. );
  159. // The middle latitude on the western edge.
  160. cartographicScratch.longitude = rectangle.west;
  161. cartographicScratch.latitude = (rectangle.south + rectangle.north) * 0.5;
  162. cartographicScratch.height = 0.0;
  163. const westernMidpointCartesian = ellipsoid.cartographicToCartesian(
  164. cartographicScratch,
  165. westernMidpointScratch,
  166. );
  167. // Compute the normal of the plane on the western edge of the tile.
  168. const westNormal = Cartesian3.cross(
  169. westernMidpointCartesian,
  170. Cartesian3.UNIT_Z,
  171. westNormalScratch,
  172. );
  173. Cartesian3.normalize(westNormal, tileBB.westNormal);
  174. // The middle latitude on the eastern edge.
  175. cartographicScratch.longitude = rectangle.east;
  176. const easternMidpointCartesian = ellipsoid.cartographicToCartesian(
  177. cartographicScratch,
  178. easternMidpointScratch,
  179. );
  180. // Compute the normal of the plane on the eastern edge of the tile.
  181. const eastNormal = Cartesian3.cross(
  182. Cartesian3.UNIT_Z,
  183. easternMidpointCartesian,
  184. cartesian3Scratch,
  185. );
  186. Cartesian3.normalize(eastNormal, tileBB.eastNormal);
  187. let westVector = Cartesian3.subtract(
  188. westernMidpointCartesian,
  189. easternMidpointCartesian,
  190. cartesian3Scratch,
  191. );
  192. if (Cartesian3.magnitude(westVector) === 0.0) {
  193. westVector = Cartesian3.clone(westNormal, westVector);
  194. }
  195. const eastWestNormal = Cartesian3.normalize(
  196. westVector,
  197. eastWestNormalScratch,
  198. );
  199. // Compute the normal of the plane bounding the southern edge of the tile.
  200. const south = rectangle.south;
  201. let southSurfaceNormal;
  202. if (south > 0.0) {
  203. // Compute a plane that doesn't cut through the tile.
  204. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  205. cartographicScratch.latitude = south;
  206. const southCenterCartesian = ellipsoid.cartographicToCartesian(
  207. cartographicScratch,
  208. rayScratch.origin,
  209. );
  210. Cartesian3.clone(eastWestNormal, rayScratch.direction);
  211. const westPlane = Plane.fromPointNormal(
  212. tileBB.southwestCornerCartesian,
  213. tileBB.westNormal,
  214. planeScratch,
  215. );
  216. // Find a point that is on the west and the south planes
  217. IntersectionTests.rayPlane(
  218. rayScratch,
  219. westPlane,
  220. tileBB.southwestCornerCartesian,
  221. );
  222. southSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  223. southCenterCartesian,
  224. cartesian3Scratch2,
  225. );
  226. } else {
  227. southSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
  228. Rectangle.southeast(rectangle),
  229. cartesian3Scratch2,
  230. );
  231. }
  232. const southNormal = Cartesian3.cross(
  233. southSurfaceNormal,
  234. westVector,
  235. cartesian3Scratch3,
  236. );
  237. Cartesian3.normalize(southNormal, tileBB.southNormal);
  238. // Compute the normal of the plane bounding the northern edge of the tile.
  239. const north = rectangle.north;
  240. let northSurfaceNormal;
  241. if (north < 0.0) {
  242. // Compute a plane that doesn't cut through the tile.
  243. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  244. cartographicScratch.latitude = north;
  245. const northCenterCartesian = ellipsoid.cartographicToCartesian(
  246. cartographicScratch,
  247. rayScratch.origin,
  248. );
  249. Cartesian3.negate(eastWestNormal, rayScratch.direction);
  250. const eastPlane = Plane.fromPointNormal(
  251. tileBB.northeastCornerCartesian,
  252. tileBB.eastNormal,
  253. planeScratch,
  254. );
  255. // Find a point that is on the east and the north planes
  256. IntersectionTests.rayPlane(
  257. rayScratch,
  258. eastPlane,
  259. tileBB.northeastCornerCartesian,
  260. );
  261. northSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  262. northCenterCartesian,
  263. cartesian3Scratch2,
  264. );
  265. } else {
  266. northSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
  267. Rectangle.northwest(rectangle),
  268. cartesian3Scratch2,
  269. );
  270. }
  271. const northNormal = Cartesian3.cross(
  272. westVector,
  273. northSurfaceNormal,
  274. cartesian3Scratch3,
  275. );
  276. Cartesian3.normalize(northNormal, tileBB.northNormal);
  277. }
  278. const southwestCornerScratch = new Cartesian3();
  279. const northeastCornerScratch = new Cartesian3();
  280. const negativeUnitY = new Cartesian3(0.0, -1.0, 0.0);
  281. const negativeUnitZ = new Cartesian3(0.0, 0.0, -1.0);
  282. const vectorScratch = new Cartesian3();
  283. function distanceToCameraRegion(tileBB, frameState) {
  284. const camera = frameState.camera;
  285. const cameraCartesianPosition = camera.positionWC;
  286. const cameraCartographicPosition = camera.positionCartographic;
  287. let result = 0.0;
  288. if (!Rectangle.contains(tileBB.rectangle, cameraCartographicPosition)) {
  289. let southwestCornerCartesian = tileBB.southwestCornerCartesian;
  290. let northeastCornerCartesian = tileBB.northeastCornerCartesian;
  291. let westNormal = tileBB.westNormal;
  292. let southNormal = tileBB.southNormal;
  293. let eastNormal = tileBB.eastNormal;
  294. let northNormal = tileBB.northNormal;
  295. if (frameState.mode !== SceneMode.SCENE3D) {
  296. southwestCornerCartesian = frameState.mapProjection.project(
  297. Rectangle.southwest(tileBB.rectangle),
  298. southwestCornerScratch,
  299. );
  300. southwestCornerCartesian.z = southwestCornerCartesian.y;
  301. southwestCornerCartesian.y = southwestCornerCartesian.x;
  302. southwestCornerCartesian.x = 0.0;
  303. northeastCornerCartesian = frameState.mapProjection.project(
  304. Rectangle.northeast(tileBB.rectangle),
  305. northeastCornerScratch,
  306. );
  307. northeastCornerCartesian.z = northeastCornerCartesian.y;
  308. northeastCornerCartesian.y = northeastCornerCartesian.x;
  309. northeastCornerCartesian.x = 0.0;
  310. westNormal = negativeUnitY;
  311. eastNormal = Cartesian3.UNIT_Y;
  312. southNormal = negativeUnitZ;
  313. northNormal = Cartesian3.UNIT_Z;
  314. }
  315. const vectorFromSouthwestCorner = Cartesian3.subtract(
  316. cameraCartesianPosition,
  317. southwestCornerCartesian,
  318. vectorScratch,
  319. );
  320. const distanceToWestPlane = Cartesian3.dot(
  321. vectorFromSouthwestCorner,
  322. westNormal,
  323. );
  324. const distanceToSouthPlane = Cartesian3.dot(
  325. vectorFromSouthwestCorner,
  326. southNormal,
  327. );
  328. const vectorFromNortheastCorner = Cartesian3.subtract(
  329. cameraCartesianPosition,
  330. northeastCornerCartesian,
  331. vectorScratch,
  332. );
  333. const distanceToEastPlane = Cartesian3.dot(
  334. vectorFromNortheastCorner,
  335. eastNormal,
  336. );
  337. const distanceToNorthPlane = Cartesian3.dot(
  338. vectorFromNortheastCorner,
  339. northNormal,
  340. );
  341. if (distanceToWestPlane > 0.0) {
  342. result += distanceToWestPlane * distanceToWestPlane;
  343. } else if (distanceToEastPlane > 0.0) {
  344. result += distanceToEastPlane * distanceToEastPlane;
  345. }
  346. if (distanceToSouthPlane > 0.0) {
  347. result += distanceToSouthPlane * distanceToSouthPlane;
  348. } else if (distanceToNorthPlane > 0.0) {
  349. result += distanceToNorthPlane * distanceToNorthPlane;
  350. }
  351. }
  352. let cameraHeight;
  353. let minimumHeight;
  354. let maximumHeight;
  355. if (frameState.mode === SceneMode.SCENE3D) {
  356. cameraHeight = cameraCartographicPosition.height;
  357. minimumHeight = tileBB.minimumHeight;
  358. maximumHeight = tileBB.maximumHeight;
  359. } else {
  360. cameraHeight = cameraCartesianPosition.x;
  361. minimumHeight = 0.0;
  362. maximumHeight = 0.0;
  363. }
  364. if (cameraHeight > maximumHeight) {
  365. const distanceAboveTop = cameraHeight - maximumHeight;
  366. result += distanceAboveTop * distanceAboveTop;
  367. } else if (cameraHeight < minimumHeight) {
  368. const distanceBelowBottom = minimumHeight - cameraHeight;
  369. result += distanceBelowBottom * distanceBelowBottom;
  370. }
  371. return Math.sqrt(result);
  372. }
  373. /**
  374. * Gets the distance from the camera to the closest point on the tile. This is used for level of detail selection.
  375. *
  376. * @param {FrameState} frameState The state information of the current rendering frame.
  377. * @returns {number} The distance from the camera to the closest point on the tile, in meters.
  378. */
  379. TileBoundingRegion.prototype.distanceToCamera = function (frameState) {
  380. //>>includeStart('debug', pragmas.debug);
  381. Check.defined("frameState", frameState);
  382. //>>includeEnd('debug');
  383. const regionResult = distanceToCameraRegion(this, frameState);
  384. if (
  385. frameState.mode === SceneMode.SCENE3D &&
  386. defined(this._orientedBoundingBox)
  387. ) {
  388. const obbResult = Math.sqrt(
  389. this._orientedBoundingBox.distanceSquaredTo(frameState.camera.positionWC),
  390. );
  391. return Math.max(regionResult, obbResult);
  392. }
  393. return regionResult;
  394. };
  395. /**
  396. * Determines which side of a plane this box is located.
  397. *
  398. * @param {Plane} plane The plane to test against.
  399. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  400. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  401. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  402. * intersects the plane.
  403. */
  404. TileBoundingRegion.prototype.intersectPlane = function (plane) {
  405. //>>includeStart('debug', pragmas.debug);
  406. Check.defined("plane", plane);
  407. //>>includeEnd('debug');
  408. return this._orientedBoundingBox.intersectPlane(plane);
  409. };
  410. /**
  411. * Creates a debug primitive that shows the outline of the tile bounding region.
  412. *
  413. * @param {Color} color The desired color of the primitive's mesh
  414. * @return {Primitive}
  415. *
  416. * @private
  417. */
  418. TileBoundingRegion.prototype.createDebugVolume = function (color) {
  419. //>>includeStart('debug', pragmas.debug);
  420. Check.defined("color", color);
  421. //>>includeEnd('debug');
  422. const modelMatrix = Matrix4.clone(Matrix4.IDENTITY);
  423. const geometry = new RectangleOutlineGeometry({
  424. rectangle: this.rectangle,
  425. height: this.minimumHeight,
  426. extrudedHeight: this.maximumHeight,
  427. });
  428. const instance = new GeometryInstance({
  429. geometry: geometry,
  430. id: "outline",
  431. modelMatrix: modelMatrix,
  432. attributes: {
  433. color: ColorGeometryInstanceAttribute.fromColor(color),
  434. },
  435. });
  436. return new Primitive({
  437. geometryInstances: instance,
  438. appearance: new PerInstanceColorAppearance({
  439. translucent: false,
  440. flat: true,
  441. }),
  442. asynchronous: false,
  443. });
  444. };
  445. export default TileBoundingRegion;