osg点选 框选对象

//点选 event事件虚函数调用的点选

void PointSelect( osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea )
{
    osgGroupPtr root = dynamic_cast<osg::Group*>(viewer->getSceneData());
    if (root.get() == NULL)
        return;
    //选中的对象
    osgNodePtr node;
    osgGroupPtr parent;
    //创建一个线段交集检测函数
    osgUtil::LineSegmentIntersector::Intersections intersections;
    if (viewer->computeIntersections(ea,intersections))
    {
        const osgUtil::LineSegmentIntersector::Intersection& hit = 
            *intersections.begin();
        const osg::NodePath& nodePath = hit.nodePath;

        //得到选中对象
        node = (nodePath.size() >= 1) ? nodePath[nodePath.size()-1] : 0;
        parent = (nodePath.size() >= 2) ? dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]) : 0;

        if (picktype == PickGeometry)
        {
            //选择Drawable
            if (node.get() && hit.drawable.get())
            {
                parent = dynamic_cast<osg::Group*>(node.get());
                node = hit.drawable;
            }        
        }
    }

}

//测标点框选 ex,ey为屏幕坐标
bool RectSearch( float ex, float ey ,osgViewer::Viewer* pViewer)
{
    double mx = ex;
    double my = ey;
    double w = max(dExpendX,1.0f);
    
    double h = max(dExpendY,1.0f);
    
    osgUtil::PolytopeIntersector* picker =
    new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h );
    
    osgUtil::IntersectionVisitor iv(picker);

    
    pViewer->getCamera()->accept(iv);

    
    if (picker->containsIntersections())
    
    {
        
        osgUtil::PolytopeIntersector::Intersections sections = picker->getIntersections();
        std::set<osgUtil::PolytopeIntersector::Intersection>::iterator it = sections.begin();
        for (it; it != sections.end(); it++)
        {
            osgUtil::PolytopeIntersector::Intersection& hit = *it;
            //得到选中对象
            node = (nodePath.size() >= 1) ? nodePath[nodePath.size()-1] : 0;
            parent = (nodePath.size() >= 2) ? dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]) : 0;
            //如果需要选择Drawable
            {
                if (node.get() && hit.drawable.get())
                {
                    parent = dynamic_cast<osg::Group*>(node.get());
                    node = hit.drawable;
                }        
            }
        }
    }
    if (vFeature.empty())
        return false;
    return true;
}

发布了21 篇原创文章 · 获赞 0 · 访问量 1141

猜你喜欢

转载自blog.csdn.net/snowwind_51/article/details/104475380