drawline.h
#ifndef DRAWLINE_H
#define DRAWLINE_H
#include "handleadapter.h"
class DrawLine : public HandleAdapter
{
public:
DrawLine(GraphicsView* view);
~DrawLine();
protected:
virtual void slotPicked(osg::Vec3d pos);
virtual void slotMoveing(osg::Vec3d pos);
virtual void slotRightHandle();
private:
osgEarth::Symbology::Style m_lineStyle;
osgEarth::Features::Feature* m_pFeature;
osgEarth::Annotation::FeatureNode* m_pFeatureNode;
osgEarth::Symbology::Style m_stippleLineStyle;
osgEarth::Features::Feature* m_pStippleFeature;
osgEarth::Annotation::FeatureNode* m_pStippleFeatureNode;
std::vector<osg::Vec3d> m_vecPoint;
};
#endif // DRAWLINE_H
drawline.cpp
#include "drawline.h"
DrawLine::DrawLine(GraphicsView* view)
: HandleAdapter(view)
{
m_pFeature = NULL;
m_pFeatureNode = NULL;
m_pStippleFeature = NULL;
m_pStippleFeatureNode = NULL;
m_vecPoint.clear();
// init style
m_lineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->color() = osgEarth::Symbology::Color::Yellow;
m_lineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->width() = 5.0f;
m_lineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->tessellation() = 20.0;
m_lineStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->clamping() = osgEarth::Symbology::AltitudeSymbol::CLAMP_TO_TERRAIN;
m_lineStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->technique() = osgEarth::Symbology::AltitudeSymbol::TECHNIQUE_DRAPE;
m_lineStyle.getOrCreate<osgEarth::Symbology::AltitudeSymbol>()->verticalOffset() = 0.1;
m_stippleLineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->color() = osgEarth::Symbology::Color::Red;
m_stippleLineStyle.getOrCreate<osgEarth::Symbology::LineSymbol>()->stroke()->width() = 5.0f;
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;//虚线
}
DrawLine::~DrawLine()
{
}
void DrawLine::slotPicked(osg::Vec3d pos)
{
m_vecPoint.push_back(pos);
if (m_vecPoint.size() <= 1)
{
return;
}
if (m_pFeatureNode == NULL)
{
m_pFeature = new osgEarth::Features::Feature(new osgEarth::Annotation::LineString,m_pMap3D->getMapNode()->getMapSRS());
m_pFeatureNode = new osgEarth::Annotation::FeatureNode(m_pMap3D->getMapNode(),m_pFeature, m_lineStyle);
m_pLayerGroup->addChild(m_pFeatureNode);
//m_pMap3D->getMapNode()->addChild(m_pFeatureNode);
}
m_pFeature->getGeometry()->clear();
m_pFeatureNode->setStyle(m_lineStyle);
for (auto& n : m_vecPoint)
{
n[2] = 1000;//预设高度1000
m_pFeature->getGeometry()->push_back(n);
}
m_pFeatureNode->init();
if (m_pStippleFeatureNode != NULL)
{
m_pStippleFeature->getGeometry()->clear();
m_pStippleFeatureNode->init();
}
}
void DrawLine::slotMoveing(osg::Vec3d pos)
{
if (m_vecPoint.size() <= 0)
{
return;
}
if (m_pStippleFeatureNode == NULL)
{
m_pStippleFeature = new osgEarth::Features::Feature(new osgEarth::Annotation::LineString,m_pMap3D->getMapNode()->getMapSRS());
m_pStippleFeatureNode = new osgEarth::Annotation::FeatureNode(m_pMap3D->getMapNode(),m_pStippleFeature, m_lineStyle);
m_pLayerGroup->addChild(m_pStippleFeatureNode);
//m_pMap3D->getMapNode()->addChild(m_pStippleFeatureNode);//只能用mapnode添加
}
m_pStippleFeature->getGeometry()->clear();
m_pStippleFeatureNode->setStyle(m_stippleLineStyle);
m_pStippleFeature->getGeometry()->push_back(m_vecPoint[m_vecPoint.size() - 1]);
m_pStippleFeature->getGeometry()->push_back(pos);
m_pStippleFeatureNode->init();
}
void DrawLine::slotRightHandle()
{
m_vecPoint.clear();
if (m_pStippleFeatureNode != NULL)
{
m_pStippleFeature->getGeometry()->clear();
m_pStippleFeatureNode->init();
}
m_pFeatureNode = NULL;
}