"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addLayer = addLayer;
exports.computeCircle = computeCircle;
exports.transformWGS84ToCartesian = exports.transformCartesianToWGS84 = exports.getPosition = exports.getCartesian3FromXY = exports["default"] = void 0;
var _cesium = require("cesium");
var getPosition = exports.getPosition = function getPosition(viewer, cartesian3) {
  var ellipsoid = viewer.scene.globe.ellipsoid;
  var cartographic = ellipsoid.cartesianToCartographic(cartesian3);
  var latitude = _cesium.Math.toDegrees(cartographic.latitude);
  var longitude = _cesium.Math.toDegrees(cartographic.longitude);
  return {
    longitude: longitude,
    latitude: latitude
  };
};
var transformWGS84ToCartesian = exports.transformWGS84ToCartesian = function transformWGS84ToCartesian(viewer, lng, lat, alt) {
  var ellipsoid = viewer.scene.globe.ellipsoid;
  var cartographic = _cesium.Cartographic.fromDegrees(lng, lat, alt);
  var cartesian3 = ellipsoid.cartographicToCartesian(cartographic);
  return cartesian3;
};
var transformCartesianToWGS84 = exports.transformCartesianToWGS84 = function transformCartesianToWGS84(viewer, cartesian3) {
  var ellipsoid = viewer.scene.globe.ellipsoid;
  var cartographic = ellipsoid.cartesianToCartographic(cartesian3);
  var lat = _cesium.Math.toDegrees(cartographic.latitude);
  var lng = _cesium.Math.toDegrees(cartographic.longitude);
  var alt = cartographic.height;
  return {
    lng: lng,
    lat: lat,
    height: alt
  };
};

/**
 * 屏幕坐标转世界坐标
 * @param position 屏幕坐标
 * @param viewer 视图对象
 *
 * @return 世界坐标
 */
var getCartesian3FromXY = exports.getCartesian3FromXY = function getCartesian3FromXY(viewer, position) {
  if (viewer && position) {
    // 返回位于特定窗口坐标位置的所有基本体的对象列表
    var picks = viewer.scene.drillPick(position);
    var cartesian = null;
    var isOn3dtiles = false;
    var isOnTerrain = false;
    for (var i in picks) {
      var pick = picks[i];
      if (pick && pick.primitive instanceof _cesium.Cesium3DTileFeature || pick && pick.primitive instanceof _cesium.Cesium3DTileset || pick && pick.primitive instanceof _cesium.Model) {
        // 存在模型数据，从模型上拾取坐标
        isOn3dtiles = true;
      }

      // 3dtilset
      if (isOn3dtiles) {
        viewer.scene.pick(position);
        cartesian = viewer.scene.pickPosition(position);
        // 计算高度值
        if (cartesian) {
          // 笛卡尔坐标系转化为 wgs 坐标（弧度）
          var cartographic = _cesium.Cartographic.fromCartesian(cartesian);
          if (cartographic.height < 0) cartographic.height = 0;
          // 将弧度转为度
          var lon = _cesium.Math.toDegrees(cartographic.longitude);
          var lat = _cesium.Math.toDegrees(cartographic.latitude);
          var height = cartographic.height;
          var res = transformWGS84ToCartesian(viewer, lon, lat, height);
          if (res) {
            cartesian = res;
          }
        }
      }
    }
    // 存在地形数据
    var boolTerrain = viewer.terrainProvider instanceof _cesium.EllipsoidTerrainProvider;
    if (!isOn3dtiles && !boolTerrain) {
      var ray = viewer.scene.camera.getPickRay(position);
      if (!ray) return null;
      var _res = viewer.scene.globe.pick(ray, viewer.scene);
      if (_res) {
        cartesian = _res;
      }
      isOnTerrain = true;
    }
    // 地球
    if (!isOn3dtiles && !isOnTerrain && boolTerrain) {
      var _res2 = viewer.scene.camera.pickEllipsoid(position, viewer.scene.globe.ellipsoid);
      if (_res2) {
        cartesian = _res2;
      }
    }
    if (cartesian) {
      var px = transformCartesianToWGS84(viewer, cartesian);
      if (px && px.height < 0) {
        var _res3 = transformWGS84ToCartesian(viewer, px.lng, px.lat, px.height);
        if (_res3) {
          cartesian = _res3;
        }
        return cartesian;
      }
      return cartesian;
    }
    return false;
  }
};
function computeCircle(radius) {
  var positions = [];
  for (var i = 0; i < 360; i++) {
    var radians = _cesium.Math.toRadians(i);
    positions.push(new _cesium.Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
  }
  return positions;
}

/**
 * 添加图层
 * @param {*} viewer
 * @param {*} layConfig 图层相关配置
 */
function addLayer(viewer, layConfig) {
  return new Promise(function (resolve) {
    // 模型图层对象
    if (layConfig.type === '3dtiles' && layConfig.url) {
      // 加载自定义数据
      var tilest = new _cesium.Cesium3DTileset({
        url: layConfig.url
      });
      tilest.readyPromise.then(function (tilests) {
        if (tilests) {
          var surfaceNormal = _cesium.Ellipsoid.WGS84.geodeticSurfaceNormal(tilests.boundingSphere.center);
          var translationb = _cesium.Cartesian3.multiplyByScalar(surfaceNormal, 0, new _cesium.Cartesian3());
          tilests.modelMatrix = _cesium.Matrix4.fromTranslation(translationb);
          if (viewer) {
            viewer.scene.primitives.add(tilest);
            if (layConfig.show) {
              viewer.zoomTo(tilest, new _cesium.HeadingPitchRange(0.0, -0.5, tilest.boundingSphere.radius * 2.0));
            }
            resolve({
              type: layConfig.type ? layConfig.type : '3dtiles',
              layer: tilest,
              key: layConfig.id
            });
          }
        }
      });
    } else if (layConfig.type === 'geogson' && layConfig.url) {
      var dataSource = _cesium.GeoJsonDataSource.load(layConfig.url, {
        clampToGround: true // 贴地加载
      });
      dataSource.then(function (dataSource) {
        if (viewer) {
          var areaEntities = dataSource.entities.values;
          areaEntities.forEach(function (entity) {
            if ((0, _cesium.defined)(entity.polygon)) {
              entity.name = entity.properties ? entity.properties.name : '';
            }
          });
          viewer.dataSources.add(dataSource);
          if (layConfig.show) {
            viewer.zoomTo(dataSource);
          }
          resolve({
            type: layConfig.type ? layConfig.type : 'geogson',
            layer: dataSource,
            key: layConfig.id
          });
        }
      });
    }
  });
}
var _default = exports["default"] = {};
//# sourceMappingURL=cesium.js.map
