关闭

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

2433人阅读 评论(0) 收藏 举报
分类:

功能描述如下:
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;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:115591次
    • 积分:1514
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:0篇
    • 译文:0篇
    • 评论:101条
    文章分类
    最新评论