可视化场景内任意绘制多边形并测量面积

一般测量功能主要表现在两方面,一是测量距离,二是测量面积。面积的测量是根据鼠标绘制的范围,通过地理坐标系的转换而计算出实际面积大小,距离的测量是根据鼠标在地图上绘制的点,实时计算出两点之间的实际距离。如何在3D场景中测量面积?下面我就在ThingJS平台实现鼠标任意点绘制多边形面积,计算绘制总长度和占地面积,支持在数字孪生可视化场景内任意绘制多边形并测量面积。

先来看一下实现效果:

实现思路

1、首先是添加注册事件,单击鼠标左键添加点位,鼠标移动持续绘制测量线段,双击或单击鼠标右键结束。


  appClick() {
        let _this = this;
        // 点击左键添加节点右键结束绘制
        _this.opts.app.on('SingleClick', function(e) {
            if (e.button == 0) {
                if (!e.picked) return;
                _this.numIndex++;
                _this.ePosition = e.pickedPosition;
                _this.createPoint(_this.ePosition);
                _this.coordinatesArr.push(_this.ePosition);
                _this.lineCoor.push(_this.ePosition);
                _this.createLine(_this.coordinatesArr);
                _this.getDistance();
                _this.template =
                    `
`; if (_this.lineDistanceAll != null) { _this.template += _this.lineDistanceAll + `米`; } else { _this.template += `` + _this.planeId + ` 起点` } _this.template += `
`; _this.boardId = 'div' + _this.opts.modelNum + _this.numIndex; _this.createCard(_this.regionPoint); _this.pointsObj = { id: 'points' + _this.opts.modelNum + _this.numIndex, // 起点顶牌span标签id parent: 'div' + _this.opts.modelNum + _this.numIndex, // 起点顶牌div标签id coor: _this.opts.currPosition, // 起点坐标 distance: 0 }; _this.pointsObjArr.push(_this.pointsObj); _this.cardClick(); } else { if (_this.coordinatesArr.length < 2) { _this.destroyAll(); _this.rianleyDom.css('display', 'none'); return; }; _this.end(); } _this.rianleyDom.css('display', 'none'); }, "点击"); // 鼠标移动持续绘制测量线段 _this.opts.app.on('MouseMove', function(e) { if (e.picked) { _this.ePosition = e.pickedPosition; _this.pointsArr = [..._this.coordinatesArr, _this.ePosition]; _this.createLine(_this.pointsArr); _this.line.style.color = '#f88020'; if (_this.pointsArr.length >= 2) { _this.moveDistance = THING.Math.getDistance(_this.pointsArr[_this.pointsArr.length - 1], _this.pointsArr[_this.pointsArr.length - 2]); let countNum = 0; _this.disArr.forEach(v => { countNum += parseFloat(v); }); countNum = 1 * parseFloat(countNum).toFixed(2) + 1 * parseFloat(_this.moveDistance).toFixed(2); _this.rianleyDom.css('display', 'block'); _this.rianleyDom.find('span.value').text(countNum.toFixed(2)); _this.rianleyDom.css('left', e.clientX + 10 + 'px'); _this.rianleyDom.css('top', e.clientY + 'px'); } } }, '移动'); // 结束绘制当前测量线段 _this.opts.app.on('DBLClick', function(ev) { if (_this.coordinatesArr.length < 2) { _this.destroyAll(); _this.rianleyDom.css('display', 'none'); return; }; if (_this.coordinatesArr.length >= 3) { _this.createPolygon(_this.coordinatesArr); } _this.end(); }, '双击'); }

可视化场景内任意绘制多边形并测量面积_第1张图片

2、创建节点、线段和生成测量吗这些基本元素,确定起点、各个节点的坐标。通过节点和线段来创建参数组,统一所有鼠标点击后的坐标点集合,生成不规则图形的测量面积。


createPoint(ePosition) {
var _this = this;
_this.regionPoint = app.create({
type: 'Sphere',
id: 'points' + _this.opts.modelNum + _this.numIndex,
name: 'points' + _this.opts.modelNum,
radius: 0.2, // 半径
widthSegments: 16,
heightSegments: 16,
position: ePosition, // 球体坐标
style: {
color: '#c10000',
                roughness: 50,
                opacity: 0.8
            }
        });
    }
createLine(coordinates) {
        if (this.line) {
            this.line.destroy();
        }
        let id = this.opts.modelNum > 10 ? this.opts.modelNum : '0' + this.opts.modelNum;
        this.line = app.create({
            type: 'PolygonLine',
            name: 'line',
            id: 'line' + id,
            width: 0.03,
            points: coordinates,
            style: {
                image: '/guide/examples/images/measure/redLine.png',
                opacity: 0.9
            }
        });
    }

3、面积测量的对象是带有地理位置(coordinates)的多边形要素,需要创建一个Constructor ()构造器,设置构造参数。

constructor(option) {
        this.opts = option;
        this.pointsArr = [this.opts.currPosition]; // 鼠标移动中坐标点的集合
        this.coordinatesArr = [this.opts.currPosition]; // 存储鼠标点击后坐标点的集合
        this.ePosition = null; // 存储触发事件后鼠标的位置
        this.lineCoor = [this.opts.currPosition]; // 存储当前两个坐标点
        this.disArr = []; // 存储所有坐标点与坐标点间的距离
        this.numIndex = 0; // 自增变量
        this.reSetDistance = 0; // 两点间的距离
        this.lastStatus = false; // 判断是否绘制结束值为false为未结束true为结束
        this.pointsObjArr = []; // 存储所有节点的id与顶牌id
        this.rianleyDom = $('#marker'); // 跟随鼠标的提示
        this.pointCardDom = $('#pointMarker'); // 鼠标移动至节点的提示
        this.init();
        this.appClick();
    }

4、创建完一个测量多边形面积的方法,停止画图的时候就会触发选定部分的面积的大小了,然后我们就需要在测量结束的时候弹出一个顶牌来显示测量的面积。

createTopCard(position) {
        $('#div3d').append(this.template);
        this.polygonCard = document.getElementById(this.boardId);
        this.uiTop = app.create({
            type: 'UIAnchor',
            element: this.polygonCard,
            position: [position[0], position[1], position[2]]
        });
    }

可视化场景内任意绘制多边形并测量面积_第2张图片

是不是非常简单就可以实现在数字孪生可视化场景中测量多边形面积?完整代码戳官网—https://www.thingjs.com/

你可能感兴趣的:(python,java,c语言,javascript,c++)