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

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

你可能感兴趣的:(C++/Qt,OSG)