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

#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生成一个细长的小圆柱体来判断是否与点线模型相交


  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值