/* *通视分析 * @Author: Wang jianLei * @Date: 2022-04-17 22:04:52 * @Last Modified by: Wang JianLei * @Last Modified time: 2022-04-17 22:05:13 */ import Tools from "../../../Tools"; import Event from "../../../Event"; import MouseTip from "../../../MouseTip"; import EventBinding from '../../Element/Dialog/eventBinding'; import Dialog from '../../../BaseDialog'; import { html } from "./_element"; class VisibilityAnalysis extends Tools { constructor(sdk, options = {}, _Dialog = {}) { super(sdk, options) this.viewer = sdk.viewer; this.resultObject = { viewPoint: undefined, //通视分析起点 targetPoints: [], //通视分析目标点集合 targetPoint: undefined, //当前目标点 objectExclude: [], //射线排除集合 entities: [], //创建的Entity对象 }; this.options = {} this._elms = {}; this.viewPointHeight = options.viewPointHeight this.Dialog = _Dialog this._EventBinding = new EventBinding() this.tools = new Tools(this.sdk) YJ.Analysis.AnalysesResults.push(this) // VisibilityAnalysis.edit(this) VisibilityAnalysis.create(this) } get viewPointHeight() { return this.options.viewPointHeight } set viewPointHeight(v) { let viewPointHeight = Math.floor(Number(v) * 10) / 10 if (isNaN(viewPointHeight)) { viewPointHeight = 1.8 } if (viewPointHeight < 0) { viewPointHeight = 0 } this.options.viewPointHeight = viewPointHeight this._elms.viewPointHeight && this._elms.viewPointHeight.forEach((item) => { item.value = viewPointHeight }) } static create(that) { if (!YJ.Measure.GetMeasureStatus()) { if (that._DialogObject && that._DialogObject.close) { that._DialogObject.close() that._DialogObject = null } that.event = new Event(that.sdk) that.tip = new MouseTip('左键点击创建视角起点', that.sdk) YJ.Measure.SetMeasureStatus(true) let count = 0; that.event.mouse_left(async (movement, cartesian) => { that.tip.set_text("左键创建视角终点,右键结束通视分析") if (!that.resultObject.viewPoint) { let pos84 = that.cartesian3Towgs84(cartesian, that.viewer) let positions if (that.sdk.viewer.terrainProvider.availability) { positions = await Cesium.sampleTerrainMostDetailed( that.sdk.viewer.terrainProvider, [Cesium.Cartographic.fromDegrees(pos84.lng, pos84.lat)] ); } if (positions && positions[0].height > pos84.alt) { pos84.alt = positions[0].height } pos84.alt = pos84.alt + that.viewPointHeight let pos = Cesium.Cartesian3.fromDegrees(pos84.lng, pos84.lat, pos84.alt) that.resultObject.viewPoint = pos; let pointEntity = that.viewer.entities.add({ position: pos, point: { color: Cesium.Color.YELLOW, pixelSize: 5, }, }); that.resultObject.objectExclude.push(pointEntity); that.resultObject.entities.push(pointEntity); } else { that.resultObject.targetPoint = cartesian; let pointEntity = that.viewer.entities.add({ position: cartesian, point: { color: Cesium.Color.YELLOW, pixelSize: 5, }, }); that.resultObject.objectExclude.push(pointEntity); that.resultObject.entities.push(pointEntity); let direction = Cesium.Cartesian3.normalize( Cesium.Cartesian3.subtract( that.resultObject.targetPoint, that.resultObject.viewPoint, new Cesium.Cartesian3() ), new Cesium.Cartesian3() ); let ray = new Cesium.Ray(that.resultObject.viewPoint, direction); let pickedObjects = that.viewer.scene.drillPickFromRay(ray); let result = {} for (let i = 0; i < pickedObjects.length; i++) { if (pickedObjects[i].position) { result = pickedObjects[i] break } } // let result = that.viewer.scene.pickFromRay( // ray, // that.resultObject.objectExclude // ); // 计算交互点,返回第一个 if (result) { let dis0 = VisibilityAnalysis.distance( that.resultObject.viewPoint, that.resultObject.targetPoint ); let dis1 = VisibilityAnalysis.distance( that.resultObject.viewPoint, result.position || cartesian ); let dis2 = VisibilityAnalysis.distance( result.position || cartesian, that.resultObject.targetPoint ); if (dis0 > dis1) { let _poly0 = that.viewer.entities.add({ polyline: { positions: [that.resultObject.viewPoint, result.position], material: Cesium.Color.GREEN, width: 3, zIndex: 99999999 }, }); that.resultObject.entities.push(_poly0); let _poly1 = that.viewer.entities.add({ polyline: { positions: [result.position, that.resultObject.targetPoint], material: Cesium.Color.RED, width: 3, zIndex: 99999999 }, }); that.resultObject.entities.push(_poly1); that.resultObject.targetPoints.push({ targetPoint: cartesian, visual: false, //如果dis2足够小,其实他是可视的 distance: [dis0, dis1, dis2], //[初始点和终点,初始点和交点,交点和终点] }); } else { let _poly2 = that.viewer.entities.add({ polyline: { positions: [ that.resultObject.viewPoint, that.resultObject.targetPoint, ], material: Cesium.Color.GREEN, width: 3, zIndex: 99999999 }, }); that.resultObject.entities.push(_poly2); that.resultObject.targetPoints.push({ targetPoint: cartesian, visual: true, //如果dis2足够小,其实他是可视的 distance: [dis0, dis1, dis2], //[初始点和终点,初始点和交点,交点和终点] }); } } } }) that.event.mouse_move((movement, cartesian) => { that.tip.setPosition(cartesian, movement.endPosition.x, movement.endPosition.y) }) that.event.mouse_right((movement, cartesian) => { that.end() }) that.event.gesture_pinck_start((movement, cartesian) => { let startTime = new Date() that.event.gesture_pinck_end(() => { let endTime = new Date() if (endTime - startTime >= 500) { that.end() } }) }) } else { that.tools.message({ text: '上一次测量未结束', type: 'warning', }); } } end() { YJ.Measure.SetMeasureStatus(false) this.tip && this.tip.destroy() this.event && this.event.destroy() this.tip = null this.event = null } // static update(that) { // if (!that.resultObject.viewPoint) { // return // } // for (let i = that.resultObject.entities.length - 1; i >= 0; i--) { // if (that.resultObject.entities[i].point) { // that.viewer.entities.remove(that.resultObject.entities[i]); // that.resultObject.entities.splice(i, 1) // } // } // setTimeout(() => { // for (let i = 0; i < that.resultObject.targetPoints.length; i++) { // that.resultObject.targetPoint = that.resultObject.targetPoints[i].targetPoint; // let direction = Cesium.Cartesian3.normalize( // Cesium.Cartesian3.subtract( // that.resultObject.targetPoint, // that.resultObject.viewPoint, // new Cesium.Cartesian3() // ), // new Cesium.Cartesian3() // ); // let ray = new Cesium.Ray(that.resultObject.viewPoint, direction); // let pickedObjects = that.viewer.scene.drillPickFromRay(ray); // let result = {} // for (let i = 0; i < pickedObjects.length; i++) { // if (pickedObjects[i].position) { // result = pickedObjects[i] // break // } // } // // let result = that.viewer.scene.pickFromRay( // // ray, // // that.resultObject.objectExclude // // ); // 计算交互点,返回第一个 // if (result) { // let dis0 = VisibilityAnalysis.distance( // that.resultObject.viewPoint, // that.resultObject.targetPoint // ); // let dis1 = VisibilityAnalysis.distance( // that.resultObject.viewPoint, // result.position || cartesian // ); // let dis2 = VisibilityAnalysis.distance( // result.position || cartesian, // that.resultObject.targetPoint // ); // if (dis0 > dis1) { // let _poly0 = that.viewer.entities.add({ // polyline: { // positions: [that.resultObject.viewPoint, result.position], // material: Cesium.Color.GREEN, // width: 3, // zIndex: 99999999 // }, // }); // that.resultObject.entities.push(_poly0); // let _poly1 = that.viewer.entities.add({ // polyline: { // positions: [result.position, that.resultObject.targetPoint], // material: Cesium.Color.RED, // width: 3, // zIndex: 99999999 // }, // }); // that.resultObject.entities.push(_poly1); // } else { // let _poly2 = that.viewer.entities.add({ // polyline: { // positions: [ // that.resultObject.viewPoint, // that.resultObject.targetPoint, // ], // material: Cesium.Color.GREEN, // width: 3, // zIndex: 99999999 // }, // }); // that.resultObject.entities.push(_poly2); // } // } // } // }, 1000); // } static async edit(that) { if (that._DialogObject && that._DialogObject.close) { that._DialogObject.close() that._DialogObject = null } that._DialogObject = await new Dialog(that.sdk.viewer._container, { title: '多点视线分析', left: '180px', top: '100px', closeCallBack: () => { that.Dialog.closeCallBack && that.Dialog.closeCallBack() YJ.Measure.SetMeasureStatus(false) }, }) await that._DialogObject.init() that._DialogObject._element.body.className = that._DialogObject._element.body.className + ' visibility' let contentElm = document.createElement('div'); contentElm.innerHTML = html() that._DialogObject.contentAppChild(contentElm) let drawElm = document.createElement('button') drawElm.innerHTML = '绘制' drawElm.addEventListener('click', () => { VisibilityAnalysis.create(that) }) that._DialogObject.footAppChild(drawElm) let all_elm = contentElm.getElementsByTagName("*") that._EventBinding.on(that, all_elm) that._elms = that._EventBinding.element } //空间两点间距离 static distance(point1, point2) { let point1cartographic = Cesium.Cartographic.fromCartesian(point1); let point2cartographic = Cesium.Cartographic.fromCartesian(point2); /**根据经纬度计算出距离**/ let geodesic = new Cesium.EllipsoidGeodesic(); geodesic.setEndPoints(point1cartographic, point2cartographic); let s = geodesic.surfaceDistance; //返回两点之间的距离 s = Math.sqrt( Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2) ); return s; } destroy() { this.resultObject.entities.forEach((element) => { this.viewer.entities.remove(element); }); this.resultObject = { viewPoint: undefined, //通视分析起点 targetPoints: [], //通视分析目标点集合 targetPoint: undefined, //当前目标点 objectExclude: [], //射线排除集合 entities: [], //创建的Entity对象 }; this.tip && this.tip.destroy() this.event && this.event.destroy() this.tip = null this.event = null YJ.Measure.SetMeasureStatus(false) } } export default VisibilityAnalysis;