osgearth-osgEarthUtil-MeasureTool handle代码解析
2016-05-03 12:05
441 查看
bool MeasureToolHandler::handle( const osgGA::GUIEventAdapter&
ea, osgGA::GUIActionAdapter& aa )
{
if (
ea.getHandled() )
{
returnfalse;
}
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
//左键一直按下,_mouseDown区分开鼠标左键是否长按
if (ea.getEventType()
== osgGA::GUIEventAdapter::PUSH && ea.getButton() == _mouseButton)
{
_mouseDown = true;
_mouseDownX = ea.getX();
_mouseDownY = ea.getY();
}
//左键释放
elseif (ea.getEventType()
== osgGA::GUIEventAdapter::RELEASE && ea.getButton() == _mouseButton)
{
float eps
= 1.0f;
_mouseDown = false;
//判定鼠标左键按下并释放时,屏幕位置是否相同
if (osg::equivalent(ea.getX(),
_mouseDownX) && osg::equivalent(ea.getY(), _mouseDownY))
{
//相同——单击情况如下
//获取鼠标点击位置的经纬度
double lon,
lat;
if (getLocationAt(view,
ea.getX(), ea.getY(), lon, lat))
{
if (!_gotFirstLocation)//获取第一个点击点
{
_finished = false;//取点结束状态为假
clear(); //清除所画路径
_gotFirstLocation = true;//表示已经获得第一个点
_feature->getGeometry()->push_back( osg::Vec3d(
lon, lat, 0 ) );//压入矢量点
}
else//第一个点已经获取完毕
{
if (_lastPointTemporary)//如果是暂存的最后一个点
{
//将此点压入矢量点
_feature->getGeometry()->back() =
osg::Vec3d( lon, lat, 0 );
//暂存的最后一个点状态为false
_lastPointTemporary = false;
}
else//如果是不是暂存的最后一个点
{
_feature->getGeometry()->push_back(
osg::Vec3d( lon, lat, 0 ) );
}
//初始化featureNode
_featureNode->init();
//_gotFirstLocation
= false;
//_finished
= true;
//如果绘制结束或者画的是不是多条线段
if (_finished
|| !_isPath) {
_gotFirstLocation = false;//获取第一点为假
}
#ifdef SHOW_EXTENT
const GeoExtent
ex( _feature->getSRS(), _feature->getGeometry()->getBounds() );
//OE_INFO
<< "extent = " << ex.toString() << std::endl;
Geometry* eg = _extentFeature->getGeometry();
osg::Vec3d fc = ex.getCentroid();
eg->clear();
eg->push_back( ex.west(), ex.south()
);
if (
ex.width() >= 180.0 )
eg->push_back( fc.x(), ex.south()
);
eg->push_back( ex.east(), ex.south()
);
eg->push_back( ex.east(), ex.north()
);
if (
ex.width() >= 180.0 )
eg->push_back( fc.x(), ex.north()
);
eg->push_back( ex.west(), ex.north()
);
_extentFeatureNode->init();
#endif
fireDistanceChanged();
//触发重绘事件
aa.requestRedraw();
}
}
}
}
elseif (ea.getEventType()
== osgGA::GUIEventAdapter::DOUBLECLICK) {
if (_gotFirstLocation)//如果得到第一个点为真
{
//_gotFirstLocation
= false;
_finished = true; //结束绘制
aa.requestRedraw(); //重绘
returntrue;
}
}
elseif (ea.getEventType()
== osgGA::GUIEventAdapter::MOVE)
{
if (_gotFirstLocation)//如果得到第一个点为真
{
double lon,
lat;
if (getLocationAt(view,
ea.getX(), ea.getY(), lon, lat))
{
//如果最后一点暂存点为假
if (!_lastPointTemporary)
{
_feature->getGeometry()->push_back( osg::Vec3d(
lon, lat, 0 ) );
_lastPointTemporary = true;
}
else
{
_feature->getGeometry()->back() = osg::Vec3d(
lon, lat, 0 );
}
_featureNode->init();
fireDistanceChanged();
aa.requestRedraw();
}
}
}
returnfalse;
}
//获取屏幕所在点的世界坐标系坐标
bool MeasureToolHandler::getLocationAt(osgViewer::View*
view, double x, double y, double &lon, double &lat)
{
osgUtil::LineSegmentIntersector::Intersections results;
if (
getMapNode() && view->computeIntersections( x, y, results, _intersectionMask ) )
{
//
find the first hit under the mouse:
osgUtil::LineSegmentIntersector::Intersection first
= *(results.begin());
osg::Vec3d point = first.getWorldIntersectPoint();
double lat_rad,
lon_rad, height;
getMapNode()->getMap()->getProfile()->getSRS()->getEllipsoid()->convertXYZToLatLongHeight(
point.x(), point.y(), point.z(), lat_rad, lon_rad,
height );
lat = osg::RadiansToDegrees( lat_rad );
lon = osg::RadiansToDegrees( lon_rad );
returntrue;
}
returnfalse;
}
ea, osgGA::GUIActionAdapter& aa )
{
if (
ea.getHandled() )
{
returnfalse;
}
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
//左键一直按下,_mouseDown区分开鼠标左键是否长按
if (ea.getEventType()
== osgGA::GUIEventAdapter::PUSH && ea.getButton() == _mouseButton)
{
_mouseDown = true;
_mouseDownX = ea.getX();
_mouseDownY = ea.getY();
}
//左键释放
elseif (ea.getEventType()
== osgGA::GUIEventAdapter::RELEASE && ea.getButton() == _mouseButton)
{
float eps
= 1.0f;
_mouseDown = false;
//判定鼠标左键按下并释放时,屏幕位置是否相同
if (osg::equivalent(ea.getX(),
_mouseDownX) && osg::equivalent(ea.getY(), _mouseDownY))
{
//相同——单击情况如下
//获取鼠标点击位置的经纬度
double lon,
lat;
if (getLocationAt(view,
ea.getX(), ea.getY(), lon, lat))
{
if (!_gotFirstLocation)//获取第一个点击点
{
_finished = false;//取点结束状态为假
clear(); //清除所画路径
_gotFirstLocation = true;//表示已经获得第一个点
_feature->getGeometry()->push_back( osg::Vec3d(
lon, lat, 0 ) );//压入矢量点
}
else//第一个点已经获取完毕
{
if (_lastPointTemporary)//如果是暂存的最后一个点
{
//将此点压入矢量点
_feature->getGeometry()->back() =
osg::Vec3d( lon, lat, 0 );
//暂存的最后一个点状态为false
_lastPointTemporary = false;
}
else//如果是不是暂存的最后一个点
{
_feature->getGeometry()->push_back(
osg::Vec3d( lon, lat, 0 ) );
}
//初始化featureNode
_featureNode->init();
//_gotFirstLocation
= false;
//_finished
= true;
//如果绘制结束或者画的是不是多条线段
if (_finished
|| !_isPath) {
_gotFirstLocation = false;//获取第一点为假
}
#ifdef SHOW_EXTENT
const GeoExtent
ex( _feature->getSRS(), _feature->getGeometry()->getBounds() );
//OE_INFO
<< "extent = " << ex.toString() << std::endl;
Geometry* eg = _extentFeature->getGeometry();
osg::Vec3d fc = ex.getCentroid();
eg->clear();
eg->push_back( ex.west(), ex.south()
);
if (
ex.width() >= 180.0 )
eg->push_back( fc.x(), ex.south()
);
eg->push_back( ex.east(), ex.south()
);
eg->push_back( ex.east(), ex.north()
);
if (
ex.width() >= 180.0 )
eg->push_back( fc.x(), ex.north()
);
eg->push_back( ex.west(), ex.north()
);
_extentFeatureNode->init();
#endif
fireDistanceChanged();
//触发重绘事件
aa.requestRedraw();
}
}
}
}
elseif (ea.getEventType()
== osgGA::GUIEventAdapter::DOUBLECLICK) {
if (_gotFirstLocation)//如果得到第一个点为真
{
//_gotFirstLocation
= false;
_finished = true; //结束绘制
aa.requestRedraw(); //重绘
returntrue;
}
}
elseif (ea.getEventType()
== osgGA::GUIEventAdapter::MOVE)
{
if (_gotFirstLocation)//如果得到第一个点为真
{
double lon,
lat;
if (getLocationAt(view,
ea.getX(), ea.getY(), lon, lat))
{
//如果最后一点暂存点为假
if (!_lastPointTemporary)
{
_feature->getGeometry()->push_back( osg::Vec3d(
lon, lat, 0 ) );
_lastPointTemporary = true;
}
else
{
_feature->getGeometry()->back() = osg::Vec3d(
lon, lat, 0 );
}
_featureNode->init();
fireDistanceChanged();
aa.requestRedraw();
}
}
}
returnfalse;
}
//获取屏幕所在点的世界坐标系坐标
bool MeasureToolHandler::getLocationAt(osgViewer::View*
view, double x, double y, double &lon, double &lat)
{
osgUtil::LineSegmentIntersector::Intersections results;
if (
getMapNode() && view->computeIntersections( x, y, results, _intersectionMask ) )
{
//
find the first hit under the mouse:
osgUtil::LineSegmentIntersector::Intersection first
= *(results.begin());
osg::Vec3d point = first.getWorldIntersectPoint();
double lat_rad,
lon_rad, height;
getMapNode()->getMap()->getProfile()->getSRS()->getEllipsoid()->convertXYZToLatLongHeight(
point.x(), point.y(), point.z(), lat_rad, lon_rad,
height );
lat = osg::RadiansToDegrees( lat_rad );
lon = osg::RadiansToDegrees( lon_rad );
returntrue;
}
returnfalse;
}
相关文章推荐
- 关于Spring加载classpath与classpath*的过程剖析
- 上线代码关键部分
- lua匹配UTF-8中文汉字
- python udp简单示例
- Java WEB安全问题及解决方案
- java:提示Could not initialize class sun.awt.X11GraphicsEnvironment
- SpringMVC配合EL表达式以及AJAX实现局部刷新
- Java自动重载Spring配置文件
- java生成指定范围的随机数
- EXCEL2010打开phpexcel生成的excel时,显示文件已损坏,无法打开
- 关于C++中的虚拟继承的一些总结
- c# 把时间写入文件
- java内存模型
- 当我学完Python我学了些什么
- 公钥,私钥和数字签名 图文结合
- Java框架介绍
- C++ 值传递 指针传递 引用传递
- JAVA命令制作API文档
- spring Batch实现数据库大数据量读写
- 防御性编程的重要性