"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Measure = void 0;
var _cesium = require("cesium");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * 测量
 */
var Measure = exports.Measure = /*#__PURE__*/function () {
  function Measure(viewer) {
    _classCallCheck(this, Measure);
    this.viewer = viewer;
    this.entityCollection = [];
    this.viewer.scene.globe.depthTestAgainstTerrain = true;
  }

  // var this.entityCollection = [];
  return _createClass(Measure, [{
    key: "getCollection",
    value: function getCollection() {
      return this.entityCollection;
    }

    /**
     * 清除
     */
  }, {
    key: "destroy",
    value: function destroy(callback) {
      for (var i = 0; i < this.entityCollection.length; i++) {
        this.viewer.entities.remove(this.entityCollection[i]);
      }
      this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.LEFT_CLICK);
      this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.MOUSE_MOVE);
      this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.RIGHT_CLICK);
      this.entityCollection = [];
      this.viewer.cancelBubble = false;
      if (callback) {
        callback();
      }
    }

    /**
     * 测距
     */
  }, {
    key: "measurePolyLine",
    value: function measurePolyLine(callback) {
      var _this = this;
      var positions = [];
      var labelEntity = null; // 标签实体
      this.viewer.cancelBubble = true;
      // 注册鼠标左击事件
      this.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
        var cartesian = _this.viewer.scene.pickPosition(clickEvent.position); // 坐标
        // var cartesian = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(clickEvent.position), this.viewer.scene);
        // 存储第一个点
        if (!cartesian) {
          return false;
        }
        if (positions.length == 0) {
          positions.push(cartesian.clone());
          _this.addPoint(cartesian);

          // 注册鼠标移动事件
          _this.viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) {
            var movePosition = _this.viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点
            // var movePosition = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(moveEvent.endPosition), this.viewer.scene);

            if (!movePosition) {
              return false;
            }
            if (positions.length == 2) {
              positions.pop();
              positions.push(movePosition);

              // 绘制label
              if (labelEntity) {
                _this.viewer.entities.remove(labelEntity);
                _this.entityCollection.splice(_this.entityCollection.indexOf(labelEntity), 1);
              }

              // 计算中点
              var centerPoint = _cesium.Cartesian3.midpoint(positions[0], positions[1], new _cesium.Cartesian3());
              // 计算距离
              var lengthText = '距离：' + _this.getLengthText(positions[0], positions[1]);
              labelEntity = _this.addLabel(centerPoint, lengthText);
              _this.entityCollection.push(labelEntity);
            } else {
              positions.push(movePosition);

              // 绘制线
              _this.addLine(positions);
            }
          }, _cesium.ScreenSpaceEventType.MOUSE_MOVE);
        } else {
          // 存储第二个点
          positions.pop();
          positions.push(cartesian);
          _this.addPoint(cartesian);
          _this.addLine(positions);
          _this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.LEFT_CLICK);
          _this.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.MOUSE_MOVE);
          _this.viewer.cancelBubble = false;
          if (callback) {
            callback();
          }
        }
      }, _cesium.ScreenSpaceEventType.LEFT_CLICK);
      // this.viewer.screenSpaceEventHandler.setInputAction((clickEvent) => {

      //     this.viewer.screenSpaceEventHandler.removeInputAction(ScreenSpaceEventType.RIGHT_CLICK);
      // }, ScreenSpaceEventType.RIGHT_CLICK);
    }

    /**
     * 测面积
     */
  }, {
    key: "measurePolygon",
    value: function measurePolygon(callback) {
      var _this2 = this;
      var positions = [];
      var clickStatus = false;
      var labelEntity = null;
      this.viewer.scene.globe.depthTestAgainstTerrain = false;
      this.viewer.cancelBubble = true;
      this.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
        clickStatus = true;
        // var cartesian = this.viewer.scene.pickPosition(clickEvent.position);
        var cartesian = _this2.viewer.scene.globe.pick(_this2.viewer.camera.getPickRay(clickEvent.position), _this2.viewer.scene);
        // console.log(cartesian);

        if (!cartesian) {
          return false;
        }
        if (positions.length == 0) {
          positions.push(cartesian.clone()); //鼠标左击 添加第1个点
          _this2.addPoint(cartesian);
          _this2.viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) {
            // var movePosition = this.viewer.scene.pickPosition(moveEvent.endPosition);
            var movePosition = _this2.viewer.scene.globe.pick(_this2.viewer.camera.getPickRay(moveEvent.endPosition), _this2.viewer.scene);
            // console.log(movePosition);
            if (!movePosition) {
              return false;
            }
            if (positions.length == 1) {
              positions.push(movePosition);
              _this2.addLine(positions);
            } else {
              if (clickStatus) {
                positions.push(movePosition);
              } else {
                positions.pop();
                positions.push(movePosition);
              }
            }
            if (positions.length >= 3) {
              // 绘制label
              if (labelEntity) {
                _this2.viewer.entities.remove(labelEntity);
                _this2.entityCollection.splice(_this2.entityCollection.indexOf(labelEntity), 1);
              }
              var text = '面积：' + _this2.getArea(positions);
              var centerPoint = _this2.getCenterOfGravityPoint(positions);
              labelEntity = _this2.addLabel(centerPoint, text);
              _this2.entityCollection.push(labelEntity);
            }
            clickStatus = false;
          }, _cesium.ScreenSpaceEventType.MOUSE_MOVE);
        } else if (positions.length == 2) {
          if (!cartesian) {
            return false;
          }
          positions.pop();
          positions.push(cartesian.clone()); // 鼠标左击 添加第2个点

          _this2.addPoint(cartesian);
          _this2.addPolyGon(positions);

          // 右击结束
          _this2.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
            // var clickPosition = this.viewer.scene.pickPosition(clickEvent.position);
            var clickPosition = _this2.viewer.scene.globe.pick(_this2.viewer.camera.getPickRay(clickEvent.position), _this2.viewer.scene);
            // console.log(clickPosition);
            if (!clickPosition) {
              return false;
            }
            positions.pop();
            positions.push(clickPosition);
            positions.push(positions[0]); // 闭合
            _this2.addPoint(clickPosition);
            _this2.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.LEFT_CLICK);
            _this2.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.MOUSE_MOVE);
            _this2.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.RIGHT_CLICK);
            _this2.viewer.cancelBubble = false;
            if (callback) {
              callback();
            }
          }, _cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
        } else if (positions.length >= 3) {
          if (!cartesian) {
            return false;
          }
          positions.pop();
          positions.push(cartesian.clone()); // 鼠标左击 添加第3个点
          _this2.addPoint(cartesian);
        }
      }, _cesium.ScreenSpaceEventType.LEFT_CLICK);
    }

    /**
     * 测高
     */
  }, {
    key: "measureHeight",
    value: function measureHeight(callback) {
      var _this3 = this;
      var positions = [];
      var labelEntity_1 = null; // 标签实体
      var labelEntity_2 = null; // 标签实体
      var labelEntity_3 = null; // 标签实体

      // 注册鼠标左击事件
      this.viewer.screenSpaceEventHandler.setInputAction(function (clickEvent) {
        var cartesian = _this3.viewer.scene.pickPosition(clickEvent.position); // 坐标
        //var cartesian = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(clickEvent.position), this.viewer.scene);

        // 存储第一个点

        if (positions.length == 0) {
          if (!cartesian) {
            return false;
          }
          positions.push(cartesian.clone());
          _this3.addPoint(cartesian);

          // 注册鼠标移动事件
          _this3.viewer.screenSpaceEventHandler.setInputAction(function (moveEvent) {
            var movePosition = _this3.viewer.scene.pickPosition(moveEvent.endPosition); // 鼠标移动的点
            //var movePosition = this.viewer.scene.globe.pick(this.viewer.camera.getPickRay(moveEvent.endPosition), this.viewer.scene);
            if (!movePosition) {
              return false;
            }
            if (positions.length >= 2) {
              positions.pop();
              positions.pop();
              positions.pop();
              var cartographic = _cesium.Cartographic.fromCartesian(movePosition);
              var height = _cesium.Cartographic.fromCartesian(positions[0]).height;
              var verticalPoint = _cesium.Cartesian3.fromDegrees(Math.toDegrees(cartographic.longitude), Math.toDegrees(cartographic.latitude), height);
              positions.push(verticalPoint);
              positions.push(movePosition);
              positions.push(positions[0]);

              // 绘制label
              if (labelEntity_1) {
                _this3.viewer.entities.remove(labelEntity_1);
                _this3.entityCollection.splice(_this3.entityCollection.indexOf(labelEntity_1), 1);
                _this3.viewer.entities.remove(labelEntity_2);
                _this3.entityCollection.splice(_this3.entityCollection.indexOf(labelEntity_2), 1);
                _this3.viewer.entities.remove(labelEntity_3);
                _this3.entityCollection.splice(_this3.entityCollection.indexOf(labelEntity_3), 1);
              }

              // 计算中点
              var centerPoint_1 = _cesium.Cartesian3.midpoint(positions[0], positions[1], new _cesium.Cartesian3());
              // 计算距离
              var lengthText_1 = '水平距离：' + _this3.getLengthText(positions[0], positions[1]);
              labelEntity_1 = _this3.addLabel(centerPoint_1, lengthText_1);
              _this3.entityCollection.push(labelEntity_1);

              // 计算中点
              var centerPoint_2 = _cesium.Cartesian3.midpoint(positions[1], positions[2], new _cesium.Cartesian3());
              // 计算距离
              var lengthText_2 = '垂直距离：' + _this3.getLengthText(positions[1], positions[2]);
              labelEntity_2 = _this3.addLabel(centerPoint_2, lengthText_2);
              _this3.entityCollection.push(labelEntity_2);

              // 计算中点
              var centerPoint_3 = _cesium.Cartesian3.midpoint(positions[2], positions[3], new _cesium.Cartesian3());
              // 计算距离
              var lengthText_3 = '直线距离：' + _this3.getLengthText(positions[2], positions[3]);
              labelEntity_3 = _this3.addLabel(centerPoint_3, lengthText_3);
              _this3.entityCollection.push(labelEntity_3);
            } else {
              var _verticalPoint = new _cesium.Cartesian3(movePosition.x, movePosition.y, positions[0].z);
              positions.push(_verticalPoint);
              positions.push(movePosition);
              positions.push(positions[0]);
              // 绘制线
              _this3.addLine(positions);
            }
          }, _cesium.ScreenSpaceEventType.MOUSE_MOVE);
        } else {
          // 存储第二个点
          positions.pop();
          positions.pop();
          positions.pop();
          var cartographic = _cesium.Cartographic.fromCartesian(cartesian);
          var height = _cesium.Cartographic.fromCartesian(positions[0]).height;
          var verticalPoint = _cesium.Cartesian3.fromDegrees(Math.toDegrees(cartographic.longitude), Math.toDegrees(cartographic.latitude), height);
          positions.push(verticalPoint);
          positions.push(cartesian);
          positions.push(positions[0]);
          _this3.addPoint(cartesian);
          _this3.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.LEFT_CLICK);
          _this3.viewer.screenSpaceEventHandler.removeInputAction(_cesium.ScreenSpaceEventType.MOUSE_MOVE);
          if (callback) {
            callback();
          }
        }
      }, _cesium.ScreenSpaceEventType.LEFT_CLICK);
    }

    /**
     * 添加点
     * @param position
     */
  }, {
    key: "addPoint",
    value: function addPoint(position) {
      this.entityCollection.push(this.viewer.entities.add(new _cesium.Entity({
        cancelPopup: true,
        position: position,
        point: {
          color: _cesium.Color.BLUE,
          pixelSize: 5,
          heightReference: _cesium.HeightReference.NONE
        }
      })));
    }

    /**
     * 添加线
     * @param positions
     */
  }, {
    key: "addLine",
    value: function addLine(positions) {
      var dynamicPositions = new _cesium.CallbackProperty(function () {
        return positions;
      }, false);
      this.entityCollection.push(this.viewer.entities.add(new _cesium.Entity({
        cancelPopup: true,
        polyline: {
          positions: dynamicPositions,
          width: 2,
          arcType: _cesium.ArcType.RHUMB,
          clampToGround: true,
          material: _cesium.Color.RED,
          //获取或设置折线的表面外观
          heightReference: _cesium.HeightReference.RELATIVE_TO_GROUND
        }
      })));
    }

    /**
     * 添加面
     * @param positions
     */
  }, {
    key: "addPolyGon",
    value: function addPolyGon(positions) {
      var dynamicPositions = new _cesium.CallbackProperty(function () {
        return new _cesium.PolygonHierarchy(positions);
      }, false);
      this.entityCollection.push(this.viewer.entities.add(new _cesium.Entity({
        cancelPopup: true,
        polygon: {
          hierarchy: dynamicPositions,
          material: _cesium.Color.RED.withAlpha(0.6),
          classificationType: _cesium.ClassificationType.BOTH // 贴地表和贴模型,如果设置了，这不能使用挤出高度
        }
      })));
    }

    /**
     * 添加标签
     * @param position
     * @param text
     */
  }, {
    key: "addLabel",
    value: function addLabel(centerPoint, text) {
      return this.viewer.entities.add(new _cesium.Entity({
        cancelPopup: true,
        position: centerPoint,
        label: {
          text: text,
          font: '14px sans-serif',
          style: _cesium.LabelStyle.FILL_AND_OUTLINE,
          //FILL  FILL_AND_OUTLINE OUTLINE
          fillColor: _cesium.Color.YELLOW,
          showBackground: true,
          //指定标签后面背景的可见性
          backgroundColor: new _cesium.Color(0.165, 0.165, 0.165, 0.8),
          // 背景颜色
          backgroundPadding: new _cesium.Cartesian2(6, 6),
          //指定以像素为单位的水平和垂直背景填充padding
          pixelOffset: new _cesium.Cartesian2(0, -25),
          disableDepthTestDistance: Number.POSITIVE_INFINITY
        }
      }));
    }

    /**
     * 计算两点距离
     * @param firstPoint
     * @param secondPoint
     */
  }, {
    key: "getLengthText",
    value: function getLengthText(firstPoint, secondPoint) {
      // 计算距离
      var length = _cesium.Cartesian3.distance(firstPoint, secondPoint);
      if (length > 1000) {
        length = (length / 1000).toFixed(2) + ' 公里';
      } else {
        length = length.toFixed(2) + ' 米';
      }
      return length;
    }
  }, {
    key: "getArea",
    value: function getArea(points) {
      var _this4 = this;
      var ps = points.map(function (point) {
        var cartographic = _cesium.Ellipsoid.WGS84.cartesianToCartographic(point);
        var height = _this4.viewer.scene.globe.getHeight(cartographic);
        return _cesium.Cartesian3.fromDegrees(cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180, height);
      });
      var s = 0;
      for (var i = 0; i < ps.length; i++) {
        var p1 = ps[i];
        var p2 = void 0;
        if (i < ps.length - 1) p2 = ps[i + 1];else p2 = ps[0];
        s += p1.x * p2.y - p2.x * p1.y;
      }
      var res;
      if (s < 1000000) {
        res = Math.abs(s).toFixed(4) + ' 平方米';
      } else {
        res = Math.abs((s / 1000000.0).toFixed(4)) + ' 平方公里';
      }
      return res;
    }

    /**
     * 计算多边形的中心（简单的处理）
     * @param mPoints
     * @returns {*[]}
     */
  }, {
    key: "getCenterOfGravityPoint",
    value: function getCenterOfGravityPoint(mPoints) {
      var centerPoint = mPoints[0];
      for (var i = 1; i < mPoints.length; i++) {
        centerPoint = _cesium.Cartesian3.midpoint(centerPoint, mPoints[i], new _cesium.Cartesian3());
      }
      return centerPoint;
    }
  }]);
}();
//# sourceMappingURL=Measure.js.map
