OsgEarth下线段绘制线段(橡皮筋)

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;
}

 

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海亲王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值