All files / engine/Source/Scene computeFlyToLocationForRectangle.js

96% Statements 24/25
83.33% Branches 10/12
100% Functions 2/2
96% Lines 24/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                  6x 6x 6x     6x 6x 4x   2x     6x 1x     5x   5x 3x     2x                 2x         2x 2x 10x     10x 10x     2x 2x 2x     2x       1x      
import defined from "../Core/defined.js";
import Rectangle from "../Core/Rectangle.js";
import sampleTerrainMostDetailed from "../Core/sampleTerrainMostDetailed.js";
import SceneMode from "./SceneMode.js";
 
/**
 * Computes the final camera location to view a rectangle adjusted for the current terrain.
 * If the terrain does not support availability, the height above the ellipsoid is used.
 *
 * @param {Rectangle} rectangle The rectangle being zoomed to.
 * @param {Scene} scene The scene being used.
 *
 * @returns {Promise<Cartographic>} The optimal location to place the camera so that the entire rectangle is in view.
 *
 * @private
 */
async function computeFlyToLocationForRectangle(rectangle, scene) {
  const terrainProvider = scene.terrainProvider;
  const mapProjection = scene.mapProjection;
  const ellipsoid = mapProjection.ellipsoid;
 
  let positionWithoutTerrain;
  const tmp = scene.camera.getRectangleCameraCoordinates(rectangle);
  if (scene.mode === SceneMode.SCENE3D) {
    positionWithoutTerrain = ellipsoid.cartesianToCartographic(tmp);
  } else {
    positionWithoutTerrain = mapProjection.unproject(tmp);
  }
 
  if (!defined(terrainProvider)) {
    return positionWithoutTerrain;
  }
 
  const availability = terrainProvider.availability;
 
  if (!defined(availability) || scene.mode === SceneMode.SCENE2D) {
    return positionWithoutTerrain;
  }
 
  const cartographics = [
    Rectangle.center(rectangle),
    Rectangle.southeast(rectangle),
    Rectangle.southwest(rectangle),
    Rectangle.northeast(rectangle),
    Rectangle.northwest(rectangle),
  ];
 
  const positionsOnTerrain =
    await computeFlyToLocationForRectangle._sampleTerrainMostDetailed(
      terrainProvider,
      cartographics,
    );
 
  let heightFound = false;
  const maxHeight = positionsOnTerrain.reduce(function (currentMax, item) {
    Iif (!defined(item.height)) {
      return currentMax;
    }
    heightFound = true;
    return Math.max(item.height, currentMax);
  }, -Number.MAX_VALUE);
 
  const finalPosition = positionWithoutTerrain;
  Eif (heightFound) {
    finalPosition.height += maxHeight;
  }
 
  return finalPosition;
}
 
//Exposed for testing.
computeFlyToLocationForRectangle._sampleTerrainMostDetailed =
  sampleTerrainMostDetailed;
export default computeFlyToLocationForRectangle;