您的位置:首页 > 其它

OSG 对线或者点组成的模型选取

2017-08-31 17:48 197 查看
#include "PickHandler.h"
#include "ModelShape.h"
#include "DraggerNodeVisitor.h"
#include <osgViewer/view>
#include <osgUtil/PolytopeIntersector>
#include <osgFX/Scribe>
#include <QDebug>

PickHandler::PickHandler(osg::Node* ptrSceneNode)
: m_X(0.0f)
, m_Y(0.0f)
, m_enableDragger(true)
, m_ptrSceneNode(ptrSceneNode)
{
}

PickHandler::~PickHandler()
{
}

bool PickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
if (NULL == view)
{
return false;
}

switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::PUSH:
{
m_X = ea.getX();
m_Y = ea.getY();
}
break;

case osgGA::GUIEventAdapter::RELEASE:
{
if (ea.getX() == m_X && ea.getY() == m_Y)
{
pick(ea, aa);
}
}
break;

case osgGA::GUIEventAdapter::KEYDOWN:
{
if (ea.getKey() == 'd')
{
m_enableDragger = !m_enableDragger;
}
}
break;
default:
break;
}

return false;
}

void PickHandler::pick(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
osgViewer::View* view = dynamic_cast<osgViewer::View*> (&aa);

// 创建求交圆柱体与切片节点相交判断
double mx = ea.getXnormalized();
double my = ea.getYnormalized();
osg::ref_ptr<osgUtil::PolytopeIntersector> picker =
new osgUtil::PolytopeIntersector(osgUtil::Intersector::PROJECTION, mx - 0.01, my - 0.01, mx + 0.01, my + 0.01);
picker->setDimensionMask(osgUtil::PolytopeIntersector::DimZero | osgUtil::PolytopeIntersector::DimOne);
osgUtil::IntersectionVisitor iv(picker);
aa.asView()->getCamera()->accept(iv);

BoundingBoxDisableNodeVisitor boundingBoxDisableVisitor = BoundingBoxDisableNodeVisitor();
m_ptrSceneNode->accept(boundingBoxDisableVisitor);

if (picker->containsIntersections())
{
osgUtil::PolytopeIntersector::Intersections  setIntersections = picker->getIntersections();
osgUtil::PolytopeIntersector::Intersection intersection = *setIntersections.begin();

osg::NodePath& nodePath = intersection.nodePath;
int nNodeSize = static_cast<int> (nodePath.size());

if (nNodeSize > 0)
{
osg::Node* node = nodePath[nNodeSize - 1];

osg::Node* grandParent = node->getParent(0);
while (grandParent->getName() != "ModelShape")
{
grandParent = grandParent->getParent(0);
}

ModelShape* ptrShape = dynamic_cast<ModelShape*>(grandParent);
ptrShape->setBoundingBoxShow(true);

if (ptrShape != NULL)
{
m_enableDragger ? ptrShape->enableDragger() : ptrShape->disableDragger();
}
}
}
}
对于点或者线生成的模型,由于没有三角面的存在,所以无法使用LineSegmentIntersector来判断相交。所以使用PolytopeIntersector生成一个细长的小圆柱体来判断是否与点线模型相交
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  osg
相关文章推荐