From Peter Hrenka, (note from Robert Osfield, renamed GenericPrimitiveFunctor mention below to TemplatePrimitiveFunctor).
"Since we desperately needed a means for picking Lines
and Points I implemented (hopefully!) proper geometrical tests
for the PolytopeIntersector.
First of all I implemented a new "GenericPrimiteFunctor"
which is basically an extended copy TriangleFunctor which also
handles Points, Lines and Quads through suitable overloads of
operator(). I would have liked to call it "PrimitiveFunctor"
but that name was already used...
I used a template method to remove redundancy in the
drawElements method overloads. If you know of platforms where
this will not work I can change it to the style used
in TriangleFunctor.
In PolytopeIntersector.cpp I implemented a
"PolytopePrimitiveIntersector" which provides the needed
overloads for Points, Lines, Triangles and Quads to
the GenericPrimitiveFunctor. This is then used in the
intersect method of PolytopeIntersector.
Implementation summary:
- Points: Check distance to all planes
- Lines: Check distance of both ends against each plane.
If both are outside -> line is out
If both are in -> continue checking
One is in, one is out -> compute intersection point (candidate)
Then check all candidates against all other polytope
planes. The remaining candidates are the proper
intersection points of the line with the polytope.
- Triangles: Perform Line-Checks for all edges of the
triangle as above. If there is an proper intersection
-> done.
In the case where there are more than 2 polytope
plane to check against we have to check for the case
where the triangle encloses the polytope.
In that case the intersection lines of the polytope
planes are computed and checked against the triangle.
- Quads: handled as two triangles.
This is implementation is certainly not the fastest.
There are certainly ways and strategies to improve it.
I also enabled the code for PolytopeIntersector
in osgkeyboardmouse and added keybindings to
switch the type of intersector ('p') and the picking
coordinate system ('c') on the fly. Since the
PolytopeIntersector does not have a canonical
ordering for its intersections (as opposed to
the LineSegementIntersector) I chaged the
implementation to toggle all hit geometries.
I tested the functionality with osgkeyboardmouse
and several models and it seems to work for
polygonal models. Special nodes such as billboards
do not work.
The next thing on my todo-list is to implement
a an improved Intersection-Structure for the
PolytopeIntersector. We need to know
which primitives where hit (and where).
"
This commit is contained in:
@@ -143,7 +143,9 @@ class PickHandler : public osgGA::GUIEventHandler
|
||||
public:
|
||||
|
||||
PickHandler():
|
||||
_mx(0.0),_my(0.0) {}
|
||||
_mx(0.0),_my(0.0),
|
||||
_usePolytopeIntersector(false),
|
||||
_useWindowCoordinates(false) {}
|
||||
|
||||
~PickHandler() {}
|
||||
|
||||
@@ -165,6 +167,26 @@ public:
|
||||
osg::notify(osg::NOTICE)<<"Saved model to file 'saved_model.osg'"<<std::endl;
|
||||
osgDB::writeNodeFile(*(viewer->getSceneData()), "saved_model.osg");
|
||||
}
|
||||
else if (ea.getKey()=='p')
|
||||
{
|
||||
_usePolytopeIntersector = !_usePolytopeIntersector;
|
||||
if (_usePolytopeIntersector)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"Using PolytopeIntersector"<<std::endl;
|
||||
} else {
|
||||
osg::notify(osg::NOTICE)<<"Using LineSegmentIntersector"<<std::endl;
|
||||
}
|
||||
}
|
||||
else if (ea.getKey()=='c')
|
||||
{
|
||||
_useWindowCoordinates = !_useWindowCoordinates;
|
||||
if (_useWindowCoordinates)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"Using window coordinates for picking"<<std::endl;
|
||||
} else {
|
||||
osg::notify(osg::NOTICE)<<"Using projection coordiates for picking"<<std::endl;
|
||||
}
|
||||
}
|
||||
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Delete || ea.getKey()==osgGA::GUIEventAdapter::KEY_BackSpace)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"Delete"<<std::endl;
|
||||
@@ -206,60 +228,66 @@ public:
|
||||
osg::Node* node = 0;
|
||||
osg::Group* parent = 0;
|
||||
|
||||
bool usePolytopePicking = false;
|
||||
if (usePolytopePicking)
|
||||
if (_usePolytopeIntersector)
|
||||
{
|
||||
osgUtil::PolytopeIntersector* picker;
|
||||
if (_useWindowCoordinates)
|
||||
{
|
||||
// use window coordinates
|
||||
// remap the mouse x,y into viewport coordinates.
|
||||
osg::Viewport* viewport = viewer->getCamera()->getViewport();
|
||||
double mx = viewport->x() + (int)((double )viewport->width()*(ea.getXnormalized()*0.5+0.5));
|
||||
double my = viewport->y() + (int)((double )viewport->height()*(ea.getYnormalized()*0.5+0.5));
|
||||
|
||||
#if 0
|
||||
// use window coordinates
|
||||
// remap the mouse x,y into viewport coordinates.
|
||||
osg::Viewport* viewport = viewer->getCamera()->getViewport();
|
||||
double mx = viewport->x() + (int)((double )viewport->width()*(ea.getXnormalized()*0.5+0.5));
|
||||
double my = viewport->y() + (int)((double )viewport->height()*(ea.getYnormalized()*0.5+0.5));
|
||||
|
||||
// half width, height.
|
||||
double w = 5.0f;
|
||||
double h = 5.0f;
|
||||
osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h );
|
||||
#else
|
||||
double mx = ea.getXnormalized();
|
||||
double my = ea.getYnormalized();
|
||||
double w = 0.05;
|
||||
double h = 0.05;
|
||||
osgUtil::PolytopeIntersector* picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::PROJECTION, mx-w, my-h, mx+w, my+h );
|
||||
#endif
|
||||
// half width, height.
|
||||
double w = 5.0f;
|
||||
double h = 5.0f;
|
||||
picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h );
|
||||
} else {
|
||||
double mx = ea.getXnormalized();
|
||||
double my = ea.getYnormalized();
|
||||
double w = 0.05;
|
||||
double h = 0.05;
|
||||
picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::PROJECTION, mx-w, my-h, mx+w, my+h );
|
||||
}
|
||||
osgUtil::IntersectionVisitor iv(picker);
|
||||
|
||||
viewer->getCamera()->accept(iv);
|
||||
|
||||
if (picker->containsIntersections())
|
||||
{
|
||||
osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection();
|
||||
osgUtil::PolytopeIntersector::Intersections& intersections = picker->getIntersections();
|
||||
|
||||
osg::NodePath& nodePath = intersection.nodePath;
|
||||
node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
|
||||
parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;
|
||||
for (osgUtil::PolytopeIntersector::Intersections::iterator it=intersections.begin();
|
||||
it!=intersections.end(); ++it) {
|
||||
osgUtil::PolytopeIntersector::Intersection intersection=*it;
|
||||
|
||||
if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl;
|
||||
osg::NodePath& nodePath = intersection.nodePath;
|
||||
node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
|
||||
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;
|
||||
toggleScribe(parent, node);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
#if 0
|
||||
// use non dimensional coordinates - in projection/clip space
|
||||
osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::PROJECTION, ea.getXnormalized(),ea.getYnormalized() );
|
||||
#else
|
||||
// use window coordinates
|
||||
// remap the mouse x,y into viewport coordinates.
|
||||
osg::Viewport* viewport = viewer->getCamera()->getViewport();
|
||||
float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f));
|
||||
float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f));
|
||||
osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, mx, my );
|
||||
#endif
|
||||
|
||||
osgUtil::LineSegmentIntersector* picker;
|
||||
if (!_useWindowCoordinates)
|
||||
{
|
||||
// use non dimensional coordinates - in projection/clip space
|
||||
picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::PROJECTION, ea.getXnormalized(),ea.getYnormalized() );
|
||||
} else {
|
||||
// use window coordinates
|
||||
// remap the mouse x,y into viewport coordinates.
|
||||
osg::Viewport* viewport = viewer->getCamera()->getViewport();
|
||||
float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f));
|
||||
float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f));
|
||||
picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, mx, my );
|
||||
}
|
||||
osgUtil::IntersectionVisitor iv(picker);
|
||||
|
||||
viewer->getCamera()->accept(iv);
|
||||
@@ -274,36 +302,38 @@ public:
|
||||
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;
|
||||
|
||||
toggleScribe(parent, node);
|
||||
}
|
||||
}
|
||||
|
||||
// now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked"
|
||||
if (parent && node)
|
||||
}
|
||||
|
||||
void toggleScribe(osg::Group* parent, osg::Node* node) {
|
||||
if (!parent || !node) return;
|
||||
|
||||
std::cout<<" parent "<<parent->className()<<std::endl;
|
||||
|
||||
osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent);
|
||||
if (!parentAsScribe)
|
||||
{
|
||||
|
||||
std::cout<<" parent "<<parent->className()<<std::endl;
|
||||
|
||||
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)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
(*itr)->replaceChild(parentAsScribe,node);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void saveSelectedModel(osg::Node* scene)
|
||||
@@ -323,6 +353,8 @@ public:
|
||||
protected:
|
||||
|
||||
float _mx,_my;
|
||||
bool _usePolytopeIntersector;
|
||||
bool _useWindowCoordinates;
|
||||
};
|
||||
|
||||
int main( int argc, char **argv )
|
||||
|
||||
Reference in New Issue
Block a user