Skip to content

Commit

Permalink
增加回到home视点的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
ytyisme committed Apr 7, 2021
1 parent cb7fffd commit 368fe01
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 81 deletions.
5 changes: 3 additions & 2 deletions Inc/Inner/IOsgViewPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class IOsgViewPoint
/// 设置视点状态
enum ViewPointType
{
View_2D, /// 二维查看
View_3D, /// 三维查看
View_2DMap, /// 二维查看
View_3DMap, /// 三维查看
View_Osg, /// 不使用地图的时候的操作器
View_User,/// 用户查看
View_Node /// 跟踪节点
};
Expand Down
2 changes: 2 additions & 0 deletions LoadMap/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ void MainWindow::on_action_triggered()

void MainWindow::on_action_2_triggered()
{
m_pSceneGraph->GetMainWindow()->GetMainViewPoint()->HomeViewPoint();
return;
// SceneColor color;
// color.fG=color.fB=0.0f;
// pSatellite->SetOribitColor(color);
Expand Down
33 changes: 33 additions & 0 deletions SceneCore/SceneGraph/DealViewPortChange.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,37 @@ class ChangeManipulator:public osg::Operation
osg::ref_ptr<osgGA::CameraManipulator> m_pManipulator;
};

/// 更改视点
class ChangeViewPoint:public osg::Operation
{
public:
ChangeViewPoint(osgGA::CameraManipulator* pManipulator,SceneViewPoint viewPoint,IOsgViewPoint::ViewPointType type):
m_pManipulator(pManipulator),
m_stViewPoint(viewPoint),
m_emPointType(type){}

void operator()(osg::Object*)
{
m_pManipulator;
}
private:
osg::ref_ptr<osgGA::CameraManipulator> m_pManipulator;
SceneViewPoint m_stViewPoint;
IOsgViewPoint::ViewPointType m_emPointType;
};

/// 更改视点
class HomeViewPoint:public osg::Operation
{
public:
HomeViewPoint(osgGA::CameraManipulator* pManipulator):m_pManipulator(pManipulator){}

void operator()(osg::Object*)
{
m_pManipulator->home(0);
}
private:
osg::ref_ptr<osgGA::CameraManipulator> m_pManipulator;
};

#endif // DEALVIEWPORTCHANGE_H
63 changes: 46 additions & 17 deletions SceneCore/SceneGraph/MyEarthManipulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ void CMyEarthManipulator::ChangeMap(MapType emType)
{
case MAP_3D:
getSettings()->setThrowingEnabled(true);
getSettings()->setThrowDecayRate(0.1);
getSettings()->setThrowDecayRate(0.009);
getSettings()->setMinMaxDistance(1,DBL_MAX);
getSettings()->setLockAzimuthWhilePanning(false);
getSettings()->setTerrainAvoidanceEnabled(true);
break;
case MAP_2D:
getSettings()->setMinMaxDistance(10,m_dMaxDistance);
AdjustViewPoint();
auto vp = getViewpoint();
if(AdjustViewPoint(vp))
{
setViewpoint(vp);
}
break;
}
}
Expand Down Expand Up @@ -91,10 +95,9 @@ void CMyEarthManipulator::pan(double dx, double dy)
vp.focalPoint()->y() = m_extent.yMin();
}

AdjustViewPoint(vp);
/// 设置视点位置
setViewpoint(vp);

AdjustViewPoint();
}
}

Expand All @@ -115,18 +118,21 @@ void CMyEarthManipulator::zoom(double dx, double dy, osg::View *view)
osgEarth::Util::EarthManipulator::zoom(-dx,-dy,view);
if(MAP_2D == m_emType)
{
AdjustViewPoint();
auto vp = getViewpoint();
if(AdjustViewPoint(vp))
{
setViewpoint(vp);
}
}
}

/// 调整视点
void CMyEarthManipulator::AdjustViewPoint()
bool CMyEarthManipulator::AdjustViewPoint(osgEarth::Viewpoint& vp)
{
if(!m_bInit)
{
return;
return(false);
}
osgEarth::Viewpoint vp = getViewpoint();

const double YOffet = vp.focalPoint()->y();
const double EyeHeight = vp.range()->getValue();
Expand All @@ -135,31 +141,54 @@ void CMyEarthManipulator::AdjustViewPoint()
if(YOffet > 0 && (YOffet + EyeHeight*m_dFactor) > m_extent.yMax())
{
vp.focalPoint()->y() = DISTANCE;
setViewpoint(vp);
return(true);
}
else if(YOffet < 0 && (-YOffet + EyeHeight*m_dFactor) > m_extent.yMax())
{
vp.focalPoint()->y() = -DISTANCE;
setViewpoint(vp);
return(true);
}

return(false);
}

/// 初始化视点
void CMyEarthManipulator::InitViewPoint()
void CMyEarthManipulator::InitHomePoint(const SceneViewPoint &homePoint)
{
osgEarth::Viewpoint vp;
auto srs = osgEarth::SpatialReference::get("wgs84");
vp.focalPoint()= osgEarth::GeoPoint(osgEarth::SpatialReference::get("wgs84"),
homePoint.stPos.fX,homePoint.stPos.fY,homePoint.stPos.fZ);
vp.heading()->set(homePoint.fAzimuth,osgEarth::Units::DEGREES);
vp.range()->set(homePoint.fDistance,osgEarth::Units::METERS);

if(MAP_2D == m_emType)
{
AdjustViewPoint(vp);
}
else
{
vp.pitch()->set(homePoint.fElev-90.,osgEarth::Units::DEGREES);
}

vp.focalPoint()= osgEarth::GeoPoint(srs,118.8,32.1,0);
vp.heading()->set(0,osgEarth::Units::DEGREES);
vp.pitch()->set(-90,osgEarth::Units::DEGREES);
vp.range()->set(12000,osgEarth::Units::KILOMETERS);
setHomeViewpoint(vp);
}

/// 设置视点
void CMyEarthManipulator::SetViewPoint(const SceneViewPoint &viewPoint,double dTimes)
{
osgEarth::Viewpoint vp;
vp.focalPoint()= osgEarth::GeoPoint(osgEarth::SpatialReference::get("wgs84"),
viewPoint.stPos.fX,viewPoint.stPos.fY,viewPoint.stPos.fZ);
vp.heading()->set(viewPoint.fAzimuth,osgEarth::Units::DEGREES);
vp.range()->set(viewPoint.fDistance,osgEarth::Units::METERS);

if(MAP_2D == m_emType)
{
AdjustViewPoint();
AdjustViewPoint(vp);
}
else
{
vp.pitch()->set(0-viewPoint.fElev,osgEarth::Units::DEGREES);
}
setViewpoint(vp,dTimes);
}
11 changes: 9 additions & 2 deletions SceneCore/SceneGraph/MyEarthManipulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#endif

#include <Plot/Map/IMap.h>
#include <SceneGraph/ViewType.h>

class CMyEarthManipulator : public osgEarth::Util::EarthManipulator
{
Expand All @@ -30,14 +31,20 @@ class CMyEarthManipulator : public osgEarth::Util::EarthManipulator
/**
* @brief 初始化视点
*/
void InitViewPoint();
void InitHomePoint(const SceneViewPoint& homePoint);

/**
* @brief 设置视点
* @param viewPoint
*/
void SetViewPoint(const SceneViewPoint& viewPoint, double dTimes);

protected:

/**
* @brief 调整视点
*/
void AdjustViewPoint();
bool AdjustViewPoint(osgEarth::Viewpoint &vp);

private:
MapType m_emType;
Expand Down
4 changes: 2 additions & 2 deletions SceneCore/SceneGraph/QtSceneGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ void QtSceneGraph::InitSceneGraph()
{
if(SCENEGRAPH_2D == m_emType)
{
pViewPoint->ViewPointTypeChanged(IOsgViewPoint::View_2D);
pViewPoint->ViewPointTypeChanged(IOsgViewPoint::View_2DMap);
}
else
{
pViewPoint->ViewPointTypeChanged(IOsgViewPoint::View_3D);
pViewPoint->ViewPointTypeChanged(IOsgViewPoint::View_3DMap);
}

m_pMap->AsOsgSceneNode()->Init();
Expand Down
Loading

0 comments on commit 368fe01

Please sign in to comment.