TileBoundingRegion.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 westNormalScratch = new Cartesian3();
  145. const eastWestNormalScratch = new Cartesian3();
  146. const westernMidpointScratch = new Cartesian3();
  147. const easternMidpointScratch = new Cartesian3();
  148. const cartographicScratch = new Cartographic();
  149. const planeScratch = new Plane(Cartesian3.UNIT_X, 0.0);
  150. const rayScratch = new Ray();
  151. function computeBox(tileBB, rectangle, ellipsoid) {
  152. ellipsoid.cartographicToCartesian(
  153. Rectangle.southwest(rectangle),
  154. tileBB.southwestCornerCartesian
  155. );
  156. ellipsoid.cartographicToCartesian(
  157. Rectangle.northeast(rectangle),
  158. tileBB.northeastCornerCartesian
  159. );
  160. // The middle latitude on the western edge.
  161. cartographicScratch.longitude = rectangle.west;
  162. cartographicScratch.latitude = (rectangle.south + rectangle.north) * 0.5;
  163. cartographicScratch.height = 0.0;
  164. const westernMidpointCartesian = ellipsoid.cartographicToCartesian(
  165. cartographicScratch,
  166. westernMidpointScratch
  167. );
  168. // Compute the normal of the plane on the western edge of the tile.
  169. const westNormal = Cartesian3.cross(
  170. westernMidpointCartesian,
  171. Cartesian3.UNIT_Z,
  172. westNormalScratch
  173. );
  174. Cartesian3.normalize(westNormal, tileBB.westNormal);
  175. // The middle latitude on the eastern edge.
  176. cartographicScratch.longitude = rectangle.east;
  177. const easternMidpointCartesian = ellipsoid.cartographicToCartesian(
  178. cartographicScratch,
  179. easternMidpointScratch
  180. );
  181. // Compute the normal of the plane on the eastern edge of the tile.
  182. const eastNormal = Cartesian3.cross(
  183. Cartesian3.UNIT_Z,
  184. easternMidpointCartesian,
  185. cartesian3Scratch
  186. );
  187. Cartesian3.normalize(eastNormal, tileBB.eastNormal);
  188. let westVector = Cartesian3.subtract(
  189. westernMidpointCartesian,
  190. easternMidpointCartesian,
  191. cartesian3Scratch
  192. );
  193. if (Cartesian3.magnitude(westVector) === 0.0) {
  194. westVector = Cartesian3.clone(westNormal, westVector);
  195. }
  196. const eastWestNormal = Cartesian3.normalize(
  197. westVector,
  198. eastWestNormalScratch
  199. );
  200. // Compute the normal of the plane bounding the southern edge of the tile.
  201. const south = rectangle.south;
  202. let southSurfaceNormal;
  203. if (south > 0.0) {
  204. // Compute a plane that doesn't cut through the tile.
  205. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  206. cartographicScratch.latitude = south;
  207. const southCenterCartesian = ellipsoid.cartographicToCartesian(
  208. cartographicScratch,
  209. rayScratch.origin
  210. );
  211. Cartesian3.clone(eastWestNormal, rayScratch.direction);
  212. const westPlane = Plane.fromPointNormal(
  213. tileBB.southwestCornerCartesian,
  214. tileBB.westNormal,
  215. planeScratch
  216. );
  217. // Find a point that is on the west and the south planes
  218. IntersectionTests.rayPlane(
  219. rayScratch,
  220. westPlane,
  221. tileBB.southwestCornerCartesian
  222. );
  223. southSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  224. southCenterCartesian,
  225. cartesian3Scratch2
  226. );
  227. } else {
  228. southSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
  229. Rectangle.southeast(rectangle),
  230. cartesian3Scratch2
  231. );
  232. }
  233. const southNormal = Cartesian3.cross(
  234. southSurfaceNormal,
  235. westVector,
  236. cartesian3Scratch3
  237. );
  238. Cartesian3.normalize(southNormal, tileBB.southNormal);
  239. // Compute the normal of the plane bounding the northern edge of the tile.
  240. const north = rectangle.north;
  241. let northSurfaceNormal;
  242. if (north < 0.0) {
  243. // Compute a plane that doesn't cut through the tile.
  244. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  245. cartographicScratch.latitude = north;
  246. const northCenterCartesian = ellipsoid.cartographicToCartesian(
  247. cartographicScratch,
  248. rayScratch.origin
  249. );
  250. Cartesian3.negate(eastWestNormal, rayScratch.direction);
  251. const eastPlane = Plane.fromPointNormal(
  252. tileBB.northeastCornerCartesian,
  253. tileBB.eastNormal,
  254. planeScratch
  255. );
  256. // Find a point that is on the east and the north planes
  257. IntersectionTests.rayPlane(
  258. rayScratch,
  259. eastPlane,
  260. tileBB.northeastCornerCartesian
  261. );
  262. northSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  263. northCenterCartesian,
  264. cartesian3Scratch2
  265. );
  266. } else {
  267. northSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
  268. Rectangle.northwest(rectangle),
  269. cartesian3Scratch2
  270. );
  271. }
  272. const northNormal = Cartesian3.cross(
  273. westVector,
  274. northSurfaceNormal,
  275. cartesian3Scratch3
  276. );
  277. Cartesian3.normalize(northNormal, tileBB.northNormal);
  278. }
  279. const southwestCornerScratch = new Cartesian3();
  280. const northeastCornerScratch = new Cartesian3();
  281. const negativeUnitY = new Cartesian3(0.0, -1.0, 0.0);
  282. const negativeUnitZ = new Cartesian3(0.0, 0.0, -1.0);
  283. const vectorScratch = new Cartesian3();
  284. function distanceToCameraRegion(tileBB, frameState) {
  285. const camera = frameState.camera;
  286. const cameraCartesianPosition = camera.positionWC;
  287. const cameraCartographicPosition = camera.positionCartographic;
  288. let result = 0.0;
  289. if (!Rectangle.contains(tileBB.rectangle, cameraCartographicPosition)) {
  290. let southwestCornerCartesian = tileBB.southwestCornerCartesian;
  291. let northeastCornerCartesian = tileBB.northeastCornerCartesian;
  292. let westNormal = tileBB.westNormal;
  293. let southNormal = tileBB.southNormal;
  294. let eastNormal = tileBB.eastNormal;
  295. let northNormal = tileBB.northNormal;
  296. if (frameState.mode !== SceneMode.SCENE3D) {
  297. southwestCornerCartesian = frameState.mapProjection.project(
  298. Rectangle.southwest(tileBB.rectangle),
  299. southwestCornerScratch
  300. );
  301. southwestCornerCartesian.z = southwestCornerCartesian.y;
  302. southwestCornerCartesian.y = southwestCornerCartesian.x;
  303. southwestCornerCartesian.x = 0.0;
  304. northeastCornerCartesian = frameState.mapProjection.project(
  305. Rectangle.northeast(tileBB.rectangle),
  306. northeastCornerScratch
  307. );
  308. northeastCornerCartesian.z = northeastCornerCartesian.y;
  309. northeastCornerCartesian.y = northeastCornerCartesian.x;
  310. northeastCornerCartesian.x = 0.0;
  311. westNormal = negativeUnitY;
  312. eastNormal = Cartesian3.UNIT_Y;
  313. southNormal = negativeUnitZ;
  314. northNormal = Cartesian3.UNIT_Z;
  315. }
  316. const vectorFromSouthwestCorner = Cartesian3.subtract(
  317. cameraCartesianPosition,
  318. southwestCornerCartesian,
  319. vectorScratch
  320. );
  321. const distanceToWestPlane = Cartesian3.dot(
  322. vectorFromSouthwestCorner,
  323. westNormal
  324. );
  325. const distanceToSouthPlane = Cartesian3.dot(
  326. vectorFromSouthwestCorner,
  327. southNormal
  328. );
  329. const vectorFromNortheastCorner = Cartesian3.subtract(
  330. cameraCartesianPosition,
  331. northeastCornerCartesian,
  332. vectorScratch
  333. );
  334. const distanceToEastPlane = Cartesian3.dot(
  335. vectorFromNortheastCorner,
  336. eastNormal
  337. );
  338. const distanceToNorthPlane = Cartesian3.dot(
  339. vectorFromNortheastCorner,
  340. northNormal
  341. );
  342. if (distanceToWestPlane > 0.0) {
  343. result += distanceToWestPlane * distanceToWestPlane;
  344. } else if (distanceToEastPlane > 0.0) {
  345. result += distanceToEastPlane * distanceToEastPlane;
  346. }
  347. if (distanceToSouthPlane > 0.0) {
  348. result += distanceToSouthPlane * distanceToSouthPlane;
  349. } else if (distanceToNorthPlane > 0.0) {
  350. result += distanceToNorthPlane * distanceToNorthPlane;
  351. }
  352. }
  353. let cameraHeight;
  354. let minimumHeight;
  355. let maximumHeight;
  356. if (frameState.mode === SceneMode.SCENE3D) {
  357. cameraHeight = cameraCartographicPosition.height;
  358. minimumHeight = tileBB.minimumHeight;
  359. maximumHeight = tileBB.maximumHeight;
  360. } else {
  361. cameraHeight = cameraCartesianPosition.x;
  362. minimumHeight = 0.0;
  363. maximumHeight = 0.0;
  364. }
  365. if (cameraHeight > maximumHeight) {
  366. const distanceAboveTop = cameraHeight - maximumHeight;
  367. result += distanceAboveTop * distanceAboveTop;
  368. } else if (cameraHeight < minimumHeight) {
  369. const distanceBelowBottom = minimumHeight - cameraHeight;
  370. result += distanceBelowBottom * distanceBelowBottom;
  371. }
  372. return Math.sqrt(result);
  373. }
  374. /**
  375. * Gets the distance from the camera to the closest point on the tile. This is used for level of detail selection.
  376. *
  377. * @param {FrameState} frameState The state information of the current rendering frame.
  378. * @returns {number} The distance from the camera to the closest point on the tile, in meters.
  379. */
  380. TileBoundingRegion.prototype.distanceToCamera = function (frameState) {
  381. //>>includeStart('debug', pragmas.debug);
  382. Check.defined("frameState", frameState);
  383. //>>includeEnd('debug');
  384. const regionResult = distanceToCameraRegion(this, frameState);
  385. if (
  386. frameState.mode === SceneMode.SCENE3D &&
  387. defined(this._orientedBoundingBox)
  388. ) {
  389. const obbResult = Math.sqrt(
  390. this._orientedBoundingBox.distanceSquaredTo(frameState.camera.positionWC)
  391. );
  392. return Math.max(regionResult, obbResult);
  393. }
  394. return regionResult;
  395. };
  396. /**
  397. * Determines which side of a plane this box is located.
  398. *
  399. * @param {Plane} plane The plane to test against.
  400. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  401. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  402. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  403. * intersects the plane.
  404. */
  405. TileBoundingRegion.prototype.intersectPlane = function (plane) {
  406. //>>includeStart('debug', pragmas.debug);
  407. Check.defined("plane", plane);
  408. //>>includeEnd('debug');
  409. return this._orientedBoundingBox.intersectPlane(plane);
  410. };
  411. /**
  412. * Creates a debug primitive that shows the outline of the tile bounding region.
  413. *
  414. * @param {Color} color The desired color of the primitive's mesh
  415. * @return {Primitive}
  416. *
  417. * @private
  418. */
  419. TileBoundingRegion.prototype.createDebugVolume = function (color) {
  420. //>>includeStart('debug', pragmas.debug);
  421. Check.defined("color", color);
  422. //>>includeEnd('debug');
  423. const modelMatrix = new Matrix4.clone(Matrix4.IDENTITY);
  424. const geometry = new RectangleOutlineGeometry({
  425. rectangle: this.rectangle,
  426. height: this.minimumHeight,
  427. extrudedHeight: this.maximumHeight,
  428. });
  429. const instance = new GeometryInstance({
  430. geometry: geometry,
  431. id: "outline",
  432. modelMatrix: modelMatrix,
  433. attributes: {
  434. color: ColorGeometryInstanceAttribute.fromColor(color),
  435. },
  436. });
  437. return new Primitive({
  438. geometryInstances: instance,
  439. appearance: new PerInstanceColorAppearance({
  440. translucent: false,
  441. flat: true,
  442. }),
  443. asynchronous: false,
  444. });
  445. };
  446. export default TileBoundingRegion;