TileBoundingRegion.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 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 = defaultValue(options.minimumHeight, 0.0);
  41. this.maximumHeight = defaultValue(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. var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  93. computeBox(this, options.rectangle, ellipsoid);
  94. if (defaultValue(options.computeBoundingVolumes, true)) {
  95. // An oriented bounding box that encloses this tile's region. This is used to calculate tile visibility.
  96. this._orientedBoundingBox = OrientedBoundingBox.fromRectangle(
  97. this.rectangle,
  98. this.minimumHeight,
  99. this.maximumHeight,
  100. ellipsoid
  101. );
  102. this._boundingSphere = BoundingSphere.fromOrientedBoundingBox(
  103. this._orientedBoundingBox
  104. );
  105. }
  106. }
  107. Object.defineProperties(TileBoundingRegion.prototype, {
  108. /**
  109. * The underlying bounding volume
  110. *
  111. * @memberof TileBoundingRegion.prototype
  112. *
  113. * @type {Object}
  114. * @readonly
  115. */
  116. boundingVolume: {
  117. get: function () {
  118. return this._orientedBoundingBox;
  119. },
  120. },
  121. /**
  122. * The underlying bounding sphere
  123. *
  124. * @memberof TileBoundingRegion.prototype
  125. *
  126. * @type {BoundingSphere}
  127. * @readonly
  128. */
  129. boundingSphere: {
  130. get: function () {
  131. return this._boundingSphere;
  132. },
  133. },
  134. });
  135. var cartesian3Scratch = new Cartesian3();
  136. var cartesian3Scratch2 = new Cartesian3();
  137. var cartesian3Scratch3 = new Cartesian3();
  138. var eastWestNormalScratch = new Cartesian3();
  139. var westernMidpointScratch = new Cartesian3();
  140. var easternMidpointScratch = new Cartesian3();
  141. var cartographicScratch = new Cartographic();
  142. var planeScratch = new Plane(Cartesian3.UNIT_X, 0.0);
  143. var rayScratch = new Ray();
  144. function computeBox(tileBB, rectangle, ellipsoid) {
  145. ellipsoid.cartographicToCartesian(
  146. Rectangle.southwest(rectangle),
  147. tileBB.southwestCornerCartesian
  148. );
  149. ellipsoid.cartographicToCartesian(
  150. Rectangle.northeast(rectangle),
  151. tileBB.northeastCornerCartesian
  152. );
  153. // The middle latitude on the western edge.
  154. cartographicScratch.longitude = rectangle.west;
  155. cartographicScratch.latitude = (rectangle.south + rectangle.north) * 0.5;
  156. cartographicScratch.height = 0.0;
  157. var westernMidpointCartesian = ellipsoid.cartographicToCartesian(
  158. cartographicScratch,
  159. westernMidpointScratch
  160. );
  161. // Compute the normal of the plane on the western edge of the tile.
  162. var westNormal = Cartesian3.cross(
  163. westernMidpointCartesian,
  164. Cartesian3.UNIT_Z,
  165. cartesian3Scratch
  166. );
  167. Cartesian3.normalize(westNormal, tileBB.westNormal);
  168. // The middle latitude on the eastern edge.
  169. cartographicScratch.longitude = rectangle.east;
  170. var easternMidpointCartesian = ellipsoid.cartographicToCartesian(
  171. cartographicScratch,
  172. easternMidpointScratch
  173. );
  174. // Compute the normal of the plane on the eastern edge of the tile.
  175. var eastNormal = Cartesian3.cross(
  176. Cartesian3.UNIT_Z,
  177. easternMidpointCartesian,
  178. cartesian3Scratch
  179. );
  180. Cartesian3.normalize(eastNormal, tileBB.eastNormal);
  181. // Compute the normal of the plane bounding the southern edge of the tile.
  182. var westVector = Cartesian3.subtract(
  183. westernMidpointCartesian,
  184. easternMidpointCartesian,
  185. cartesian3Scratch
  186. );
  187. var eastWestNormal = Cartesian3.normalize(westVector, eastWestNormalScratch);
  188. var south = rectangle.south;
  189. var southSurfaceNormal;
  190. if (south > 0.0) {
  191. // Compute a plane that doesn't cut through the tile.
  192. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  193. cartographicScratch.latitude = south;
  194. var southCenterCartesian = ellipsoid.cartographicToCartesian(
  195. cartographicScratch,
  196. rayScratch.origin
  197. );
  198. Cartesian3.clone(eastWestNormal, rayScratch.direction);
  199. var westPlane = Plane.fromPointNormal(
  200. tileBB.southwestCornerCartesian,
  201. tileBB.westNormal,
  202. planeScratch
  203. );
  204. // Find a point that is on the west and the south planes
  205. IntersectionTests.rayPlane(
  206. rayScratch,
  207. westPlane,
  208. tileBB.southwestCornerCartesian
  209. );
  210. southSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  211. southCenterCartesian,
  212. cartesian3Scratch2
  213. );
  214. } else {
  215. southSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
  216. Rectangle.southeast(rectangle),
  217. cartesian3Scratch2
  218. );
  219. }
  220. var southNormal = Cartesian3.cross(
  221. southSurfaceNormal,
  222. westVector,
  223. cartesian3Scratch3
  224. );
  225. Cartesian3.normalize(southNormal, tileBB.southNormal);
  226. // Compute the normal of the plane bounding the northern edge of the tile.
  227. var north = rectangle.north;
  228. var northSurfaceNormal;
  229. if (north < 0.0) {
  230. // Compute a plane that doesn't cut through the tile.
  231. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  232. cartographicScratch.latitude = north;
  233. var northCenterCartesian = ellipsoid.cartographicToCartesian(
  234. cartographicScratch,
  235. rayScratch.origin
  236. );
  237. Cartesian3.negate(eastWestNormal, rayScratch.direction);
  238. var eastPlane = Plane.fromPointNormal(
  239. tileBB.northeastCornerCartesian,
  240. tileBB.eastNormal,
  241. planeScratch
  242. );
  243. // Find a point that is on the east and the north planes
  244. IntersectionTests.rayPlane(
  245. rayScratch,
  246. eastPlane,
  247. tileBB.northeastCornerCartesian
  248. );
  249. northSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
  250. northCenterCartesian,
  251. cartesian3Scratch2
  252. );
  253. } else {
  254. northSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(
  255. Rectangle.northwest(rectangle),
  256. cartesian3Scratch2
  257. );
  258. }
  259. var northNormal = Cartesian3.cross(
  260. westVector,
  261. northSurfaceNormal,
  262. cartesian3Scratch3
  263. );
  264. Cartesian3.normalize(northNormal, tileBB.northNormal);
  265. }
  266. var southwestCornerScratch = new Cartesian3();
  267. var northeastCornerScratch = new Cartesian3();
  268. var negativeUnitY = new Cartesian3(0.0, -1.0, 0.0);
  269. var negativeUnitZ = new Cartesian3(0.0, 0.0, -1.0);
  270. var vectorScratch = new Cartesian3();
  271. /**
  272. * Gets the distance from the camera to the closest point on the tile. This is used for level of detail selection.
  273. *
  274. * @param {FrameState} frameState The state information of the current rendering frame.
  275. * @returns {Number} The distance from the camera to the closest point on the tile, in meters.
  276. */
  277. TileBoundingRegion.prototype.distanceToCamera = function (frameState) {
  278. //>>includeStart('debug', pragmas.debug);
  279. Check.defined("frameState", frameState);
  280. //>>includeEnd('debug');
  281. var camera = frameState.camera;
  282. var cameraCartesianPosition = camera.positionWC;
  283. var cameraCartographicPosition = camera.positionCartographic;
  284. var result = 0.0;
  285. if (!Rectangle.contains(this.rectangle, cameraCartographicPosition)) {
  286. var southwestCornerCartesian = this.southwestCornerCartesian;
  287. var northeastCornerCartesian = this.northeastCornerCartesian;
  288. var westNormal = this.westNormal;
  289. var southNormal = this.southNormal;
  290. var eastNormal = this.eastNormal;
  291. var northNormal = this.northNormal;
  292. if (frameState.mode !== SceneMode.SCENE3D) {
  293. southwestCornerCartesian = frameState.mapProjection.project(
  294. Rectangle.southwest(this.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(this.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. var vectorFromSouthwestCorner = Cartesian3.subtract(
  313. cameraCartesianPosition,
  314. southwestCornerCartesian,
  315. vectorScratch
  316. );
  317. var distanceToWestPlane = Cartesian3.dot(
  318. vectorFromSouthwestCorner,
  319. westNormal
  320. );
  321. var distanceToSouthPlane = Cartesian3.dot(
  322. vectorFromSouthwestCorner,
  323. southNormal
  324. );
  325. var vectorFromNortheastCorner = Cartesian3.subtract(
  326. cameraCartesianPosition,
  327. northeastCornerCartesian,
  328. vectorScratch
  329. );
  330. var distanceToEastPlane = Cartesian3.dot(
  331. vectorFromNortheastCorner,
  332. eastNormal
  333. );
  334. var 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. var cameraHeight;
  350. var minimumHeight;
  351. var maximumHeight;
  352. if (frameState.mode === SceneMode.SCENE3D) {
  353. cameraHeight = cameraCartographicPosition.height;
  354. minimumHeight = this.minimumHeight;
  355. maximumHeight = this.maximumHeight;
  356. } else {
  357. cameraHeight = cameraCartesianPosition.x;
  358. minimumHeight = 0.0;
  359. maximumHeight = 0.0;
  360. }
  361. if (cameraHeight > maximumHeight) {
  362. var distanceAboveTop = cameraHeight - maximumHeight;
  363. result += distanceAboveTop * distanceAboveTop;
  364. } else if (cameraHeight < minimumHeight) {
  365. var distanceBelowBottom = minimumHeight - cameraHeight;
  366. result += distanceBelowBottom * distanceBelowBottom;
  367. }
  368. return Math.sqrt(result);
  369. };
  370. /**
  371. * Determines which side of a plane this box is located.
  372. *
  373. * @param {Plane} plane The plane to test against.
  374. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  375. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  376. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  377. * intersects the plane.
  378. */
  379. TileBoundingRegion.prototype.intersectPlane = function (plane) {
  380. //>>includeStart('debug', pragmas.debug);
  381. Check.defined("plane", plane);
  382. //>>includeEnd('debug');
  383. return this._orientedBoundingBox.intersectPlane(plane);
  384. };
  385. /**
  386. * Creates a debug primitive that shows the outline of the tile bounding region.
  387. *
  388. * @param {Color} color The desired color of the primitive's mesh
  389. * @return {Primitive}
  390. *
  391. * @private
  392. */
  393. TileBoundingRegion.prototype.createDebugVolume = function (color) {
  394. //>>includeStart('debug', pragmas.debug);
  395. Check.defined("color", color);
  396. //>>includeEnd('debug');
  397. var modelMatrix = new Matrix4.clone(Matrix4.IDENTITY);
  398. var geometry = new RectangleOutlineGeometry({
  399. rectangle: this.rectangle,
  400. height: this.minimumHeight,
  401. extrudedHeight: this.maximumHeight,
  402. });
  403. var instance = new GeometryInstance({
  404. geometry: geometry,
  405. id: "outline",
  406. modelMatrix: modelMatrix,
  407. attributes: {
  408. color: ColorGeometryInstanceAttribute.fromColor(color),
  409. },
  410. });
  411. return new Primitive({
  412. geometryInstances: instance,
  413. appearance: new PerInstanceColorAppearance({
  414. translucent: false,
  415. flat: true,
  416. }),
  417. asynchronous: false,
  418. });
  419. };
  420. export default TileBoundingRegion;