ApproximateTerrainHeights.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import BoundingSphere from "./BoundingSphere.js";
  2. import buildModuleUrl from "./buildModuleUrl.js";
  3. import Cartesian2 from "./Cartesian2.js";
  4. import Cartesian3 from "./Cartesian3.js";
  5. import Cartographic from "./Cartographic.js";
  6. import Check from "./Check.js";
  7. import defaultValue from "./defaultValue.js";
  8. import defined from "./defined.js";
  9. import DeveloperError from "./DeveloperError.js";
  10. import Ellipsoid from "./Ellipsoid.js";
  11. import GeographicTilingScheme from "./GeographicTilingScheme.js";
  12. import Rectangle from "./Rectangle.js";
  13. import Resource from "./Resource.js";
  14. const scratchDiagonalCartesianNE = new Cartesian3();
  15. const scratchDiagonalCartesianSW = new Cartesian3();
  16. const scratchDiagonalCartographic = new Cartographic();
  17. const scratchCenterCartesian = new Cartesian3();
  18. const scratchSurfaceCartesian = new Cartesian3();
  19. const scratchBoundingSphere = new BoundingSphere();
  20. const tilingScheme = new GeographicTilingScheme();
  21. const scratchCorners = [
  22. new Cartographic(),
  23. new Cartographic(),
  24. new Cartographic(),
  25. new Cartographic(),
  26. ];
  27. const scratchTileXY = new Cartesian2();
  28. /**
  29. * A collection of functions for approximating terrain height
  30. * @private
  31. */
  32. const ApproximateTerrainHeights = {};
  33. /**
  34. * Initializes the minimum and maximum terrain heights
  35. * @return {Promise<void>}
  36. */
  37. ApproximateTerrainHeights.initialize = function () {
  38. let initPromise = ApproximateTerrainHeights._initPromise;
  39. if (defined(initPromise)) {
  40. return initPromise;
  41. }
  42. initPromise = Resource.fetchJson(
  43. buildModuleUrl("Assets/approximateTerrainHeights.json")
  44. ).then(function (json) {
  45. ApproximateTerrainHeights._terrainHeights = json;
  46. });
  47. ApproximateTerrainHeights._initPromise = initPromise;
  48. return initPromise;
  49. };
  50. /**
  51. * Computes the minimum and maximum terrain heights for a given rectangle
  52. * @param {Rectangle} rectangle The bounding rectangle
  53. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid
  54. * @return {{minimumTerrainHeight: number, maximumTerrainHeight: number}}
  55. */
  56. ApproximateTerrainHeights.getMinimumMaximumHeights = function (
  57. rectangle,
  58. ellipsoid
  59. ) {
  60. //>>includeStart('debug', pragmas.debug);
  61. Check.defined("rectangle", rectangle);
  62. if (!defined(ApproximateTerrainHeights._terrainHeights)) {
  63. throw new DeveloperError(
  64. "You must call ApproximateTerrainHeights.initialize and wait for the promise to resolve before using this function"
  65. );
  66. }
  67. //>>includeEnd('debug');
  68. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  69. const xyLevel = getTileXYLevel(rectangle);
  70. // Get the terrain min/max for that tile
  71. let minTerrainHeight = ApproximateTerrainHeights._defaultMinTerrainHeight;
  72. let maxTerrainHeight = ApproximateTerrainHeights._defaultMaxTerrainHeight;
  73. if (defined(xyLevel)) {
  74. const key = `${xyLevel.level}-${xyLevel.x}-${xyLevel.y}`;
  75. const heights = ApproximateTerrainHeights._terrainHeights[key];
  76. if (defined(heights)) {
  77. minTerrainHeight = heights[0];
  78. maxTerrainHeight = heights[1];
  79. }
  80. // Compute min by taking the center of the NE->SW diagonal and finding distance to the surface
  81. ellipsoid.cartographicToCartesian(
  82. Rectangle.northeast(rectangle, scratchDiagonalCartographic),
  83. scratchDiagonalCartesianNE
  84. );
  85. ellipsoid.cartographicToCartesian(
  86. Rectangle.southwest(rectangle, scratchDiagonalCartographic),
  87. scratchDiagonalCartesianSW
  88. );
  89. Cartesian3.midpoint(
  90. scratchDiagonalCartesianSW,
  91. scratchDiagonalCartesianNE,
  92. scratchCenterCartesian
  93. );
  94. const surfacePosition = ellipsoid.scaleToGeodeticSurface(
  95. scratchCenterCartesian,
  96. scratchSurfaceCartesian
  97. );
  98. if (defined(surfacePosition)) {
  99. const distance = Cartesian3.distance(
  100. scratchCenterCartesian,
  101. surfacePosition
  102. );
  103. minTerrainHeight = Math.min(minTerrainHeight, -distance);
  104. } else {
  105. minTerrainHeight = ApproximateTerrainHeights._defaultMinTerrainHeight;
  106. }
  107. }
  108. minTerrainHeight = Math.max(
  109. ApproximateTerrainHeights._defaultMinTerrainHeight,
  110. minTerrainHeight
  111. );
  112. return {
  113. minimumTerrainHeight: minTerrainHeight,
  114. maximumTerrainHeight: maxTerrainHeight,
  115. };
  116. };
  117. /**
  118. * Computes the bounding sphere based on the tile heights in the rectangle
  119. * @param {Rectangle} rectangle The bounding rectangle
  120. * @param {Ellipsoid} [ellipsoid=Ellipsoid.WGS84] The ellipsoid
  121. * @return {BoundingSphere} The result bounding sphere
  122. */
  123. ApproximateTerrainHeights.getBoundingSphere = function (rectangle, ellipsoid) {
  124. //>>includeStart('debug', pragmas.debug);
  125. Check.defined("rectangle", rectangle);
  126. if (!defined(ApproximateTerrainHeights._terrainHeights)) {
  127. throw new DeveloperError(
  128. "You must call ApproximateTerrainHeights.initialize and wait for the promise to resolve before using this function"
  129. );
  130. }
  131. //>>includeEnd('debug');
  132. ellipsoid = defaultValue(ellipsoid, Ellipsoid.WGS84);
  133. const xyLevel = getTileXYLevel(rectangle);
  134. // Get the terrain max for that tile
  135. let maxTerrainHeight = ApproximateTerrainHeights._defaultMaxTerrainHeight;
  136. if (defined(xyLevel)) {
  137. const key = `${xyLevel.level}-${xyLevel.x}-${xyLevel.y}`;
  138. const heights = ApproximateTerrainHeights._terrainHeights[key];
  139. if (defined(heights)) {
  140. maxTerrainHeight = heights[1];
  141. }
  142. }
  143. const result = BoundingSphere.fromRectangle3D(rectangle, ellipsoid, 0.0);
  144. BoundingSphere.fromRectangle3D(
  145. rectangle,
  146. ellipsoid,
  147. maxTerrainHeight,
  148. scratchBoundingSphere
  149. );
  150. return BoundingSphere.union(result, scratchBoundingSphere, result);
  151. };
  152. function getTileXYLevel(rectangle) {
  153. Cartographic.fromRadians(
  154. rectangle.east,
  155. rectangle.north,
  156. 0.0,
  157. scratchCorners[0]
  158. );
  159. Cartographic.fromRadians(
  160. rectangle.west,
  161. rectangle.north,
  162. 0.0,
  163. scratchCorners[1]
  164. );
  165. Cartographic.fromRadians(
  166. rectangle.east,
  167. rectangle.south,
  168. 0.0,
  169. scratchCorners[2]
  170. );
  171. Cartographic.fromRadians(
  172. rectangle.west,
  173. rectangle.south,
  174. 0.0,
  175. scratchCorners[3]
  176. );
  177. // Determine which tile the bounding rectangle is in
  178. let lastLevelX = 0,
  179. lastLevelY = 0;
  180. let currentX = 0,
  181. currentY = 0;
  182. const maxLevel = ApproximateTerrainHeights._terrainHeightsMaxLevel;
  183. let i;
  184. for (i = 0; i <= maxLevel; ++i) {
  185. let failed = false;
  186. for (let j = 0; j < 4; ++j) {
  187. const corner = scratchCorners[j];
  188. tilingScheme.positionToTileXY(corner, i, scratchTileXY);
  189. if (j === 0) {
  190. currentX = scratchTileXY.x;
  191. currentY = scratchTileXY.y;
  192. } else if (currentX !== scratchTileXY.x || currentY !== scratchTileXY.y) {
  193. failed = true;
  194. break;
  195. }
  196. }
  197. if (failed) {
  198. break;
  199. }
  200. lastLevelX = currentX;
  201. lastLevelY = currentY;
  202. }
  203. if (i === 0) {
  204. return undefined;
  205. }
  206. return {
  207. x: lastLevelX,
  208. y: lastLevelY,
  209. level: i > maxLevel ? maxLevel : i - 1,
  210. };
  211. }
  212. ApproximateTerrainHeights._terrainHeightsMaxLevel = 6;
  213. ApproximateTerrainHeights._defaultMaxTerrainHeight = 9000.0;
  214. ApproximateTerrainHeights._defaultMinTerrainHeight = -100000.0;
  215. ApproximateTerrainHeights._terrainHeights = undefined;
  216. ApproximateTerrainHeights._initPromise = undefined;
  217. Object.defineProperties(ApproximateTerrainHeights, {
  218. /**
  219. * Determines if the terrain heights are initialized and ready to use. To initialize the terrain heights,
  220. * call {@link ApproximateTerrainHeights#initialize} and wait for the returned promise to resolve.
  221. * @type {boolean}
  222. * @readonly
  223. * @memberof ApproximateTerrainHeights
  224. */
  225. initialized: {
  226. get: function () {
  227. return defined(ApproximateTerrainHeights._terrainHeights);
  228. },
  229. },
  230. });
  231. export default ApproximateTerrainHeights;