TileBoundingRegion.js 16 KB

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