[307] | 1 | #include "distortionNG.h" |
---|
| 2 | |
---|
[309] | 3 | #include<osg/Point> |
---|
| 4 | |
---|
[313] | 5 | |
---|
[309] | 6 | #include<osgViewer/Viewer> |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | osg::Geode* distortionHandler::createVertexHighlighter() |
---|
| 11 | { |
---|
| 12 | osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1); |
---|
| 13 | (*colors)[0] = _highlightColor; |
---|
| 14 | |
---|
| 15 | _highlighter = new osg::Geometry; |
---|
| 16 | _highlighter->setDataVariance( osg::Object::DYNAMIC ); |
---|
| 17 | _highlighter->setUseDisplayList( false ); |
---|
| 18 | _highlighter->setUseVertexBufferObjects( true ); |
---|
[329] | 19 | _highlighter->setVertexArray( new osg::Vec3Array(1) ); // The highlighter vertex is updated by computeSelectedVertex(..) |
---|
[309] | 20 | _highlighter->setColorArray( colors.get() ); |
---|
| 21 | _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); |
---|
| 22 | _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); |
---|
| 23 | |
---|
| 24 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
---|
| 25 | geode->addDrawable( _highlighter.get() ); |
---|
[310] | 26 | geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) ); |
---|
[309] | 27 | geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); |
---|
| 28 | |
---|
| 29 | return geode.release(); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | bool distortionHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ) |
---|
| 33 | { |
---|
[310] | 34 | // Only pressing CTRL activates distortion editing |
---|
| 35 | if ( !(ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL) ) |
---|
| 36 | return false; // False means not handled. |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) |
---|
| 40 | { |
---|
| 41 | osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa); |
---|
| 42 | if ( viewer ) |
---|
| 43 | { |
---|
[340] | 44 | osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
| 45 | osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
| 46 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY()); |
---|
[310] | 47 | osgUtil::IntersectionVisitor iv( intersector.get() ); |
---|
[329] | 48 | //viewer->getCamera()->accept( iv ); |
---|
| 49 | _camera->accept( iv ); |
---|
[310] | 50 | |
---|
| 51 | if ( intersector->containsIntersections() ) |
---|
| 52 | { |
---|
| 53 | osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); |
---|
| 54 | computeSelectedVertex( result ); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | if ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) |
---|
| 60 | { |
---|
[340] | 61 | osg::notify(osg::ALWAYS)<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl; |
---|
[310] | 62 | osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl; |
---|
| 63 | osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl; |
---|
| 64 | osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl; |
---|
| 65 | osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl; |
---|
| 66 | osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl; |
---|
| 67 | osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; |
---|
| 68 | osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl; |
---|
| 69 | osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl; |
---|
| 70 | |
---|
[353] | 71 | return true; // true means event handled: not forwarded to the camera manipulator; |
---|
[310] | 72 | } |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | return false; // False means not handled. |
---|
[309] | 76 | } |
---|
| 77 | |
---|
| 78 | void distortionHandler::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) |
---|
| 79 | { |
---|
| 80 | osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); |
---|
| 81 | if ( !geom || !_highlighter || geom==_highlighter ) |
---|
| 82 | return; |
---|
| 83 | |
---|
| 84 | osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); |
---|
| 85 | osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); |
---|
| 86 | if ( !vertices || !selVertices ) |
---|
| 87 | return; |
---|
[329] | 88 | |
---|
| 89 | OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl; |
---|
| 90 | OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl; |
---|
[309] | 91 | |
---|
| 92 | osg::Vec3 point = result.getWorldIntersectPoint(); |
---|
| 93 | osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates |
---|
[329] | 94 | OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl; |
---|
[309] | 95 | const std::vector<unsigned int>& selIndices = result.indexList; |
---|
| 96 | { |
---|
| 97 | double maxRatio = 0.0; |
---|
| 98 | int closestVertexIndex = 0; |
---|
[329] | 99 | for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex |
---|
[309] | 100 | { |
---|
| 101 | if(result.ratioList[i] > maxRatio) |
---|
| 102 | { |
---|
| 103 | maxRatio = result.ratioList[i]; |
---|
| 104 | closestVertexIndex = result.indexList[i]; |
---|
| 105 | } |
---|
[340] | 106 | OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl; |
---|
| 107 | OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl; |
---|
[309] | 108 | } |
---|
[329] | 109 | OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl; |
---|
[309] | 110 | osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; |
---|
[329] | 111 | |
---|
| 112 | selVertices->front() = vertex; |
---|
| 113 | OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl; |
---|
[309] | 114 | } |
---|
| 115 | selVertices->dirty(); |
---|
| 116 | _highlighter->dirtyBound(); |
---|
[331] | 117 | } |
---|