Improved handling of projected coords in new intersection classes

This commit is contained in:
Robert Osfield
2006-11-01 17:18:45 +00:00
parent 75169ad16f
commit 15f7abe100
3 changed files with 52 additions and 61 deletions

View File

@@ -186,13 +186,18 @@ public:
// remap the mouse x,y into viewport coordinates.
osg::notify(osg::NOTICE)<<std::endl;
#if 0
// use non dimension coordinates - in projection/clip space
osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),0.0), osg::Vec3d(ea.getXnormalized(),ea.getYnormalized(),1.0) );
#else
// use window coordinates
float mx = _sceneView->getViewport()->x() + (int)((float)_sceneView->getViewport()->width()*(ea.getXnormalized()*0.5f+0.5f));
float my = _sceneView->getViewport()->y() + (int)((float)_sceneView->getViewport()->height()*(ea.getYnormalized()*0.5f+0.5f));
#if 1
osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) );
picker->setCoordinateFrame(osgUtil::Intersector::WINDOW);
osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::WINDOW, osg::Vec3d(mx,my,0.0), osg::Vec3d(mx,my,1.0) );
#endif
osgUtil::IntersectionVisitor iv(picker);
@@ -205,7 +210,7 @@ public:
osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection();
osg::notify(osg::NOTICE)<<"Picking "<<intersection.localIntersectionPoint<<std::endl;
#if 0
osg::NodePath& nodePath = intersection.nodePath;
osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
@@ -238,64 +243,34 @@ public:
}
}
}
#endif
}
#else
// do the pick traversal.
osgUtil::PickVisitor pick(_sceneView->getViewport(),
_sceneView->getProjectionMatrix(),
_sceneView->getViewMatrix(), mx, my);
scene->accept(pick);
osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList();
if (!segHitList.empty() && !segHitList.begin()->second.empty())
{
std::cout<<"Got hits"<<std::endl;
float mx = _sceneView->getViewport()->x() + (int)((float)_sceneView->getViewport()->width()*(ea.getXnormalized()*0.5f+0.5f));
float my = _sceneView->getViewport()->y() + (int)((float)_sceneView->getViewport()->height()*(ea.getYnormalized()*0.5f+0.5f));
// get the hits for the first segment
osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second;
// do the pick traversal use the other PickVisitor to double check results.
osgUtil::PickVisitor pick(_sceneView->getViewport(),
_sceneView->getProjectionMatrix(),
_sceneView->getViewMatrix(), mx, my);
scene->accept(pick);
// just take the first hit - nearest the eye point.
osgUtil::Hit& hit = hits.front();
osg::NodePath& nodePath = hit._nodePath;
osg::Node* node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
osg::Group* parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
// now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
if (parent && node)
osgUtil::PickVisitor::LineSegmentHitListMap& segHitList = pick.getSegHitList();
if (!segHitList.empty() && !segHitList.begin()->second.empty())
{
std::cout<<" parent "<<parent->className()<<std::endl;
// get the hits for the first segment
osgUtil::PickVisitor::HitList& hits = segHitList.begin()->second;
osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
if (!parentAsScribe)
{
// node not already picked, so highlight it with an osgFX::Scribe
osgFX::Scribe* scribe = new osgFX::Scribe();
scribe->addChild(node);
parent->replaceChild(node,scribe);
}
else
{
// node already picked so we want to remove scribe to unpick it.
osg::Node::ParentList parentList = parentAsScribe->getParents();
for(osg::Node::ParentList::iterator itr=parentList.begin();
itr!=parentList.end();
++itr)
{
(*itr)->replaceChild(parentAsScribe,node);
}
}
// just take the first hit - nearest the eye point.
osgUtil::Hit& hit = hits.front();
std::cout<<"Got hits"<<hit.getLocalIntersectPoint()<<std::endl;
}
}
#endif
}

View File

@@ -42,8 +42,8 @@ class Intersector : public osg::Referenced
MODEL
};
Intersector():
_coordinateFrame(MODEL),
Intersector(CoordinateFrame cf=MODEL):
_coordinateFrame(cf),
_disabledCount(0) {}
void setCoordinateFrame(CoordinateFrame cf) { _coordinateFrame = cf; }
@@ -84,6 +84,8 @@ class OSGUTIL_EXPORT LineSegmentIntersector : public Intersector
LineSegmentIntersector(const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersector* parent=0);
LineSegmentIntersector(CoordinateFrame cf, const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersector* parent=0);
struct Intersection
{
Intersection():

View File

@@ -217,6 +217,14 @@ LineSegmentIntersector::LineSegmentIntersector(const osg::Vec3d& start, const os
{
}
LineSegmentIntersector::LineSegmentIntersector(CoordinateFrame cf, const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersector* parent):
Intersector(cf),
_parent(parent),
_start(start),
_end(end)
{
}
Intersector* LineSegmentIntersector::clone(osgUtil::IntersectionVisitor& iv)
{
if (_coordinateFrame==MODEL && iv.getModelMatrix()==0)
@@ -257,7 +265,7 @@ Intersector* LineSegmentIntersector::clone(osgUtil::IntersectionVisitor& iv)
bool LineSegmentIntersector::enter(const osg::Node& node)
{
return intersects( node.getBound() );
return !node.isCullingActive() || intersects( node.getBound() );
}
void LineSegmentIntersector::leave()
@@ -270,6 +278,10 @@ void LineSegmentIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Dr
osg::Vec3d s(_start), e(_end);
if ( !intersectAndClip( s, e, drawable->getBound() ) ) return;
// reset the clipped range as it can be too close in on the BB, and cause missing due precission issues.
s = _start;
e = _end;
osg::TriangleFunctor<TriangleIntersector> ti;
ti.set(s,e);
drawable->accept(ti);
@@ -473,7 +485,9 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const
}
}
if (s==e) return false;
// osg::notify(osg::NOTICE)<<"clampped segment "<<s<<" "<<e<<std::endl;
// if (s==e) return false;
return true;
}
@@ -659,11 +673,11 @@ void IntersectionVisitor::apply(osg::Group& group)
void IntersectionVisitor::apply(osg::Geode& geode)
{
osg::notify(osg::NOTICE)<<"apply(Geode&)"<<std::endl;
// osg::notify(osg::NOTICE)<<"apply(Geode&)"<<std::endl;
if (!enter(geode)) return;
osg::notify(osg::NOTICE)<<"inside apply(Geode&)"<<std::endl;
// osg::notify(osg::NOTICE)<<"inside apply(Geode&)"<<std::endl;
for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
{
@@ -768,11 +782,11 @@ void IntersectionVisitor::apply(osg::Projection& projection)
void IntersectionVisitor::apply(osg::CameraNode& camera)
{
osg::notify(osg::NOTICE)<<"apply(CameraNode&)"<<std::endl;
//osg::notify(osg::NOTICE)<<"apply(CameraNode&)"<<std::endl;
// if (!enter(camera)) return;
if (!enter(camera)) return;
osg::notify(osg::NOTICE)<<"inside apply(CameraNode&)"<<std::endl;
// osg::notify(osg::NOTICE)<<"inside apply(CameraNode&)"<<std::endl;
if (camera.getViewport()) pushWindowMatrix( camera.getViewport() );
pushProjectionMatrix( new osg::RefMatrix(camera.getProjectionMatrix()) );