TileBoundingRegion.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 defineProperties from '../Core/defineProperties.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. var ellipsoid = defaultValue(options.ellipsoid, Ellipsoid.WGS84);
  94. computeBox(this, options.rectangle, ellipsoid);
  95. if (defaultValue(options.computeBoundingVolumes, true)) {
  96. // An oriented bounding box that encloses this tile's region. This is used to calculate tile visibility.
  97. this._orientedBoundingBox = OrientedBoundingBox.fromRectangle(this.rectangle, this.minimumHeight, this.maximumHeight, ellipsoid);
  98. this._boundingSphere = BoundingSphere.fromOrientedBoundingBox(this._orientedBoundingBox);
  99. }
  100. }
  101. 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. var cartesian3Scratch = new Cartesian3();
  130. var cartesian3Scratch2 = new Cartesian3();
  131. var cartesian3Scratch3 = new Cartesian3();
  132. var eastWestNormalScratch = new Cartesian3();
  133. var westernMidpointScratch = new Cartesian3();
  134. var easternMidpointScratch = new Cartesian3();
  135. var cartographicScratch = new Cartographic();
  136. var planeScratch = new Plane(Cartesian3.UNIT_X, 0.0);
  137. var rayScratch = new Ray();
  138. function computeBox(tileBB, rectangle, ellipsoid) {
  139. ellipsoid.cartographicToCartesian(Rectangle.southwest(rectangle), tileBB.southwestCornerCartesian);
  140. ellipsoid.cartographicToCartesian(Rectangle.northeast(rectangle), tileBB.northeastCornerCartesian);
  141. // The middle latitude on the western edge.
  142. cartographicScratch.longitude = rectangle.west;
  143. cartographicScratch.latitude = (rectangle.south + rectangle.north) * 0.5;
  144. cartographicScratch.height = 0.0;
  145. var westernMidpointCartesian = ellipsoid.cartographicToCartesian(cartographicScratch, westernMidpointScratch);
  146. // Compute the normal of the plane on the western edge of the tile.
  147. var westNormal = Cartesian3.cross(westernMidpointCartesian, Cartesian3.UNIT_Z, cartesian3Scratch);
  148. Cartesian3.normalize(westNormal, tileBB.westNormal);
  149. // The middle latitude on the eastern edge.
  150. cartographicScratch.longitude = rectangle.east;
  151. var easternMidpointCartesian = ellipsoid.cartographicToCartesian(cartographicScratch, easternMidpointScratch);
  152. // Compute the normal of the plane on the eastern edge of the tile.
  153. var eastNormal = Cartesian3.cross(Cartesian3.UNIT_Z, easternMidpointCartesian, cartesian3Scratch);
  154. Cartesian3.normalize(eastNormal, tileBB.eastNormal);
  155. // Compute the normal of the plane bounding the southern edge of the tile.
  156. var westVector = Cartesian3.subtract(westernMidpointCartesian, easternMidpointCartesian, cartesian3Scratch);
  157. var eastWestNormal = Cartesian3.normalize(westVector, eastWestNormalScratch);
  158. var south = rectangle.south;
  159. var southSurfaceNormal;
  160. if (south > 0.0) {
  161. // Compute a plane that doesn't cut through the tile.
  162. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  163. cartographicScratch.latitude = south;
  164. var southCenterCartesian = ellipsoid.cartographicToCartesian(cartographicScratch, rayScratch.origin);
  165. Cartesian3.clone(eastWestNormal, rayScratch.direction);
  166. var westPlane = Plane.fromPointNormal(tileBB.southwestCornerCartesian, tileBB.westNormal, planeScratch);
  167. // Find a point that is on the west and the south planes
  168. IntersectionTests.rayPlane(rayScratch, westPlane, tileBB.southwestCornerCartesian);
  169. southSurfaceNormal = ellipsoid.geodeticSurfaceNormal(southCenterCartesian, cartesian3Scratch2);
  170. } else {
  171. southSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(Rectangle.southeast(rectangle), cartesian3Scratch2);
  172. }
  173. var southNormal = Cartesian3.cross(southSurfaceNormal, westVector, cartesian3Scratch3);
  174. Cartesian3.normalize(southNormal, tileBB.southNormal);
  175. // Compute the normal of the plane bounding the northern edge of the tile.
  176. var north = rectangle.north;
  177. var northSurfaceNormal;
  178. if (north < 0.0) {
  179. // Compute a plane that doesn't cut through the tile.
  180. cartographicScratch.longitude = (rectangle.west + rectangle.east) * 0.5;
  181. cartographicScratch.latitude = north;
  182. var northCenterCartesian = ellipsoid.cartographicToCartesian(cartographicScratch, rayScratch.origin);
  183. Cartesian3.negate(eastWestNormal, rayScratch.direction);
  184. var eastPlane = Plane.fromPointNormal(tileBB.northeastCornerCartesian, tileBB.eastNormal, planeScratch);
  185. // Find a point that is on the east and the north planes
  186. IntersectionTests.rayPlane(rayScratch, eastPlane, tileBB.northeastCornerCartesian);
  187. northSurfaceNormal = ellipsoid.geodeticSurfaceNormal(northCenterCartesian, cartesian3Scratch2);
  188. } else {
  189. northSurfaceNormal = ellipsoid.geodeticSurfaceNormalCartographic(Rectangle.northwest(rectangle), cartesian3Scratch2);
  190. }
  191. var northNormal = Cartesian3.cross(westVector, northSurfaceNormal, cartesian3Scratch3);
  192. Cartesian3.normalize(northNormal, tileBB.northNormal);
  193. }
  194. var southwestCornerScratch = new Cartesian3();
  195. var northeastCornerScratch = new Cartesian3();
  196. var negativeUnitY = new Cartesian3(0.0, -1.0, 0.0);
  197. var negativeUnitZ = new Cartesian3(0.0, 0.0, -1.0);
  198. var vectorScratch = new Cartesian3();
  199. /**
  200. * Gets the distance from the camera to the closest point on the tile. This is used for level of detail selection.
  201. *
  202. * @param {FrameState} frameState The state information of the current rendering frame.
  203. * @returns {Number} The distance from the camera to the closest point on the tile, in meters.
  204. */
  205. TileBoundingRegion.prototype.distanceToCamera = function(frameState) {
  206. //>>includeStart('debug', pragmas.debug);
  207. Check.defined('frameState', frameState);
  208. //>>includeEnd('debug');
  209. var camera = frameState.camera;
  210. var cameraCartesianPosition = camera.positionWC;
  211. var cameraCartographicPosition = camera.positionCartographic;
  212. var result = 0.0;
  213. if (!Rectangle.contains(this.rectangle, cameraCartographicPosition)) {
  214. var southwestCornerCartesian = this.southwestCornerCartesian;
  215. var northeastCornerCartesian = this.northeastCornerCartesian;
  216. var westNormal = this.westNormal;
  217. var southNormal = this.southNormal;
  218. var eastNormal = this.eastNormal;
  219. var northNormal = this.northNormal;
  220. if (frameState.mode !== SceneMode.SCENE3D) {
  221. southwestCornerCartesian = frameState.mapProjection.project(Rectangle.southwest(this.rectangle), southwestCornerScratch);
  222. southwestCornerCartesian.z = southwestCornerCartesian.y;
  223. southwestCornerCartesian.y = southwestCornerCartesian.x;
  224. southwestCornerCartesian.x = 0.0;
  225. northeastCornerCartesian = frameState.mapProjection.project(Rectangle.northeast(this.rectangle), northeastCornerScratch);
  226. northeastCornerCartesian.z = northeastCornerCartesian.y;
  227. northeastCornerCartesian.y = northeastCornerCartesian.x;
  228. northeastCornerCartesian.x = 0.0;
  229. westNormal = negativeUnitY;
  230. eastNormal = Cartesian3.UNIT_Y;
  231. southNormal = negativeUnitZ;
  232. northNormal = Cartesian3.UNIT_Z;
  233. }
  234. var vectorFromSouthwestCorner = Cartesian3.subtract(cameraCartesianPosition, southwestCornerCartesian, vectorScratch);
  235. var distanceToWestPlane = Cartesian3.dot(vectorFromSouthwestCorner, westNormal);
  236. var distanceToSouthPlane = Cartesian3.dot(vectorFromSouthwestCorner, southNormal);
  237. var vectorFromNortheastCorner = Cartesian3.subtract(cameraCartesianPosition, northeastCornerCartesian, vectorScratch);
  238. var distanceToEastPlane = Cartesian3.dot(vectorFromNortheastCorner, eastNormal);
  239. var distanceToNorthPlane = Cartesian3.dot(vectorFromNortheastCorner, northNormal);
  240. if (distanceToWestPlane > 0.0) {
  241. result += distanceToWestPlane * distanceToWestPlane;
  242. } else if (distanceToEastPlane > 0.0) {
  243. result += distanceToEastPlane * distanceToEastPlane;
  244. }
  245. if (distanceToSouthPlane > 0.0) {
  246. result += distanceToSouthPlane * distanceToSouthPlane;
  247. } else if (distanceToNorthPlane > 0.0) {
  248. result += distanceToNorthPlane * distanceToNorthPlane;
  249. }
  250. }
  251. var cameraHeight;
  252. var minimumHeight;
  253. var maximumHeight;
  254. if (frameState.mode === SceneMode.SCENE3D) {
  255. cameraHeight = cameraCartographicPosition.height;
  256. minimumHeight = this.minimumHeight;
  257. maximumHeight = this.maximumHeight;
  258. } else {
  259. cameraHeight = cameraCartesianPosition.x;
  260. minimumHeight = 0.0;
  261. maximumHeight = 0.0;
  262. }
  263. if (cameraHeight > maximumHeight) {
  264. var distanceAboveTop = cameraHeight - maximumHeight;
  265. result += distanceAboveTop * distanceAboveTop;
  266. } else if (cameraHeight < minimumHeight) {
  267. var distanceBelowBottom = minimumHeight - cameraHeight;
  268. result += distanceBelowBottom * distanceBelowBottom;
  269. }
  270. return Math.sqrt(result);
  271. };
  272. /**
  273. * Determines which side of a plane this box is located.
  274. *
  275. * @param {Plane} plane The plane to test against.
  276. * @returns {Intersect} {@link Intersect.INSIDE} if the entire box is on the side of the plane
  277. * the normal is pointing, {@link Intersect.OUTSIDE} if the entire box is
  278. * on the opposite side, and {@link Intersect.INTERSECTING} if the box
  279. * intersects the plane.
  280. */
  281. TileBoundingRegion.prototype.intersectPlane = function(plane) {
  282. //>>includeStart('debug', pragmas.debug);
  283. Check.defined('plane', plane);
  284. //>>includeEnd('debug');
  285. return this._orientedBoundingBox.intersectPlane(plane);
  286. };
  287. /**
  288. * Creates a debug primitive that shows the outline of the tile bounding region.
  289. *
  290. * @param {Color} color The desired color of the primitive's mesh
  291. * @return {Primitive}
  292. *
  293. * @private
  294. */
  295. TileBoundingRegion.prototype.createDebugVolume = function(color) {
  296. //>>includeStart('debug', pragmas.debug);
  297. Check.defined('color', color);
  298. //>>includeEnd('debug');
  299. var modelMatrix = new Matrix4.clone(Matrix4.IDENTITY);
  300. var geometry = new RectangleOutlineGeometry({
  301. rectangle : this.rectangle,
  302. height : this.minimumHeight,
  303. extrudedHeight: this.maximumHeight
  304. });
  305. var instance = new GeometryInstance({
  306. geometry : geometry,
  307. id : 'outline',
  308. modelMatrix : modelMatrix,
  309. attributes : {
  310. color : ColorGeometryInstanceAttribute.fromColor(color)
  311. }
  312. });
  313. return new Primitive({
  314. geometryInstances : instance,
  315. appearance : new PerInstanceColorAppearance({
  316. translucent : false,
  317. flat : true
  318. }),
  319. asynchronous : false
  320. });
  321. };
  322. export default TileBoundingRegion;