OSGEarth绘制Geometry:绘多边形(三)

原创 2015年07月09日 22:38:15

功能描述如下:
1. 鼠标多次点击的区域绘制多边形,为黄色;
2. 最近一次点击的点以及第一次点击的点和鼠标move到的点之间连线,为两条红色的虚线,表示下次即将绘制的区域块。
代码如下:drawpolygon.h

class DrawPolygon : public HandleAdapter
{
public:
    DrawPolygon(GraphicsView* view);
    ~DrawPolygon();

protected:
    virtual void slotPicked(osg::Vec3d pos);
    virtual void slotMoveing(osg::Vec3d pos);
    virtual void slotRightHandle();

private:
    std::vector<osg::Vec3d> m_vecPoints;

    // 多边形绘制
    osgEarth::Symbology::Style m_polygonStyle;
    osgEarth::Annotation::FeatureNode* m_pFeatureNode;
    osgEarth::Annotation::FeatureEditor* m_pPolygonEdit;

    // 虚线
    osgEarth::Symbology::Style m_stippleLineStyle;
    osgEarth::Annotation::FeatureNode* m_pStippleFeatureNode;
};

功能实现如下:drawpolygon.cpp

DrawPolygon::DrawPolygon(GraphicsView* view)
    : HandleAdapter(view)
{
    m_vecPoints.clear();
    m_pFeatureNode = NULL;
    m_pStippleFeatureNode = NULL;
    m_pPolygonEdit = NULL;

    // 多边形的style
    m_polygonStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>()
        ->fill()->color() = osgEarth::Symbology::Color::Yellow;
    m_polygonStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
        ->clamping() = osgEarth::Symbology::AltitudeSymbol::CLAMP_TO_TERRAIN;
    m_polygonStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
        ->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_DRAPE;
    m_polygonStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
        ->verticalOffset() = 0.1;

    // 线的style
    m_stippleLineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
        ->stroke()->color() = osgEarth::Symbology::Color::Red;
    m_stippleLineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
        ->stroke()->width() = 2.0;
    m_stippleLineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
        ->tessellation() = 20.0;
    m_stippleLineStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
        ->clamping() = osgEarth::Symbology::AltitudeSymbol::CLAMP_TO_TERRAIN;
    m_stippleLineStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
        ->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_DRAPE;
    m_stippleLineStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()
        ->verticalOffset() = 0.1;
    m_stippleLineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()
        ->stroke()->stipple() = 255;
}

DrawPolygon::~DrawPolygon()
{

}

void DrawPolygon::slotPicked(osg::Vec3d pos)
{
    m_vecPoints.push_back(pos);
    if (m_vecPoints.size() <= 2)
    {
        return;
    }

    if (m_pPolygonEdit != NULL)
    {
        m_pPolygonEdit->removeChildren(0, m_pPolygonEdit->getNumChildren());
        m_pPolygonEdit = NULL;
    }
    if (m_pFeatureNode == NULL)
    {
        osgEarth::Features::Feature* pFeature = new osgEarth::Features::Feature(
            new osgEarth::Symbology::Polygon,
            m_pMap3D->getSRS(), m_polygonStyle);

        m_pFeatureNode = new osgEarth::Annotation::FeatureNode(
            m_pMap3D->getMapNode(), pFeature);

        m_pLayerGroup->addChild(m_pFeatureNode);
    }

    osgEarth::Symbology::Geometry* pGeometry = m_pFeatureNode->getFeature()->getGeometry();
    pGeometry->clear();
    m_pFeatureNode->setStyle(m_polygonStyle);
    for (int i = 0; i < m_vecPoints.size(); ++i)
    {
        pGeometry->push_back(m_vecPoints[i]);
    }

    m_pFeatureNode->init();
    if (m_pStippleFeatureNode != NULL)
    {
        m_pStippleFeatureNode->getFeature()->getGeometry()->clear();
    }
    if (m_pPolygonEdit == NULL)
    {
        m_pPolygonEdit = new osgEarth::Annotation::FeatureEditor(m_pFeatureNode);
        m_pLayerGroup->addChild(m_pPolygonEdit);
    }
}

void DrawPolygon::slotMoveing(osg::Vec3d pos)
{
    if (m_vecPoints.size() < 2)
    {
        return;
    }
    if (m_pStippleFeatureNode == NULL)
    {
        osgEarth::Features::Feature* pFeature = new osgEarth::Features::Feature(
            new osgEarth::Annotation::LineString,
            m_pMap3D->getSRS(), m_stippleLineStyle);
        m_pStippleFeatureNode = new osgEarth::Annotation::FeatureNode(
            m_pMap3D->getMapNode(), pFeature);

        m_pLayerGroup->addChild(m_pStippleFeatureNode);
    }

    osgEarth::Symbology::Geometry* pGeometry = m_pStippleFeatureNode->getFeature()->getGeometry();
    pGeometry->clear();
    m_pStippleFeatureNode->setStyle(m_stippleLineStyle);
    pGeometry->push_back(m_vecPoints[0]);
    pGeometry->push_back(pos);
    pGeometry->push_back(m_vecPoints[m_vecPoints.size() - 1]);

    m_pStippleFeatureNode->init();
}

void DrawPolygon::slotRightHandle()
{
    m_vecPoints.clear();
    if (m_pStippleFeatureNode != NULL)
    {
        m_pStippleFeatureNode->getFeature()->getGeometry()->clear();
    }
    // 确保下次绘制的多边形和这个没任何关系
    m_pFeatureNode = nullptr;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

osgearth各个例子功能概述

最近在学习osgearth,对其还不是很理解,有些例子不会运行,有些可以,把可以的做个统计,以备后面查用。 1、osgearth_graticule:生成经纬线。 2、osgearth_annotat...

OSG在MFC多窗口的使用

OpenSceneGraph的例子中有如何在MFC中使用OSG的,也有如何在控制台中使用多视图多窗口渲染的例子,但是如果只是将这两个例子简单的结合在一起的话程序是会出问题的。还有一些细节上面的变化需要...

OSGEarth绘制Geometry:绘圆(二)

功能描述如下: 1. 鼠标点击之处为圆心位置; 2. 以圆心到鼠标move的距离为半径绘圆; 3. 鼠标有点点击结束绘制; 4. 鼠标点击为新的圆心,继续下次绘制。 代码如下:draw...

osgearth API编程动态建立一个地球!

在研究了怎么样用earth文件来建立地球之后,我开始着手实现很多osgearth学习笔记中的API编程来建立地球。可是我照着其中的例子(http://blog.csdn.net/whucv/artic...

osgearth加载本地离线影像瓦片地图

osgearth加载本地离线影像瓦片地图案例 说明 本实例演示重新编译bing驱动直接加载本地离线影像瓦片地图。 本实例使用软件版本:osg3.3.1和osgEarth2.5 VC10编译环境(参考o...

OSG入门即osgEarth建立一个地球的详细步骤

最近在学习有关osg的知识,刚开始一头雾水,幸好在老师的指导下才茅塞顿开,同时又结合网上的多个资料总结 一下作为一个初学者入门的经验。希望广大初学者少走弯路!   开始osg编程之前要做osg的编...

OSGearth学习(一)

今天开始仔细研究osgearth,osgearth是一个开源的地理信息库,里面有很方便的

osgearth_manip示例解析

/* -*-c++-*- */ /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph * Copyright 2008-2...

osgEarthSamples1.0运行事例

osgEarthSamples是一个osgEarth测试事例运行程序,用户可在系统中设置测试事例目录加载.earth后缀xml文档,对文档进行格式化显示,可编辑,保存。 1、文件-->打开驱动器设定“...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)