Changeset 353 for experimental/distortionNG/DistortionManipulator.cpp
- Timestamp:
- Apr 21, 2012, 9:49:26 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
experimental/distortionNG/DistortionManipulator.cpp
r350 r353 20 20 21 21 #include<osgViewer/Viewer> 22 #include<osg/Point> 22 23 23 24 using namespace osg; … … 25 26 26 27 27 DistortionManipulator::DistortionManipulator() 28 DistortionManipulator::DistortionManipulator(DistortionSet* ds) 29 : _highlightColor( osg::Vec4(1.0f, 1.0f, 0.0f, 1.0f) ), _distortionSet( ds ) 28 30 { 29 31 activeSetupMode = DISABLED; … … 32 34 activeVisualizationMode = NONE; 33 35 34 _distortionSet = NULL; 36 _highlighter = NULL; 37 38 createVertexHighlighter(); 39 40 _camera = 0; 41 //_distortionMesh(distortionMesh) 35 42 } 36 43 … … 52 59 bool DistortionManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa, osg::Object* obj, osg::NodeVisitor* nv) 53 60 { 54 //osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa);55 //if ( viewer && !_distortionSet.isValid())56 //{57 // OSG_ALWAYS<<"Fetching valid DistortionSet!"<<std::endl;58 // _distortionSet = viewer->getDistortionSet();59 //}60 61 61 switch(ea.getEventType()) 62 62 { … … 64 64 case(osgGA::GUIEventAdapter::DRAG): 65 65 { 66 OSG_ALWAYS<<"DRAG!"<<std::endl; 67 break; 66 osg::notify(osg::ALWAYS)<<std::endl<<"Drag:"<<std::endl<<"ea.getGraphicsContext()="<<ea.getGraphicsContext()<<std::endl; 67 osg::notify(osg::ALWAYS)<<"ea.getXnormalized()="<<ea.getXnormalized()<<std::endl; 68 osg::notify(osg::ALWAYS)<<"ea.getYnormalized()="<<ea.getYnormalized()<<std::endl; 69 osg::notify(osg::ALWAYS)<<"ea.getX()="<<ea.getX()<<std::endl; 70 osg::notify(osg::ALWAYS)<<"ea.getXin()="<<ea.getXmin()<<std::endl; 71 osg::notify(osg::ALWAYS)<<"ea.getXmax()="<<ea.getXmax()<<std::endl; 72 osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; 73 osg::notify(osg::ALWAYS)<<"ea.getYin()="<<ea.getYmin()<<std::endl; 74 osg::notify(osg::ALWAYS)<<"ea.getYmax()="<<ea.getYmax()<<std::endl; 75 76 return true; // true means event handled: not forwarded to the camera manipulator; 77 68 78 } 69 79 case(osgGA::GUIEventAdapter::PUSH): 70 80 { 71 if(ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) 72 { 73 OSG_ALWAYS<<"mouse click left!"<<std::endl; 81 OSG_ALWAYS<<"mouse click!"<<std::endl; 82 if ( activeSetupMode == MANUAL && ea.getModKeyMask()&osgGA::GUIEventAdapter::MODKEY_CTRL && ea.getButton() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) 83 { 84 osgViewer::View* viewer = dynamic_cast<osgViewer::View*>(&aa); 85 if ( viewer ) 86 { 87 osg::notify(osg::ALWAYS)<<std::endl<<"Intersection:"<<std::endl<<"ea.getX()="<<ea.getX()<<std::endl; 88 osg::notify(osg::ALWAYS)<<"ea.getY()="<<ea.getY()<<std::endl; 89 osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(osgUtil::Intersector::PROJECTION, ea.getX(), ea.getY()); 90 osgUtil::IntersectionVisitor iv( intersector.get() ); 91 _distortionSet->getDistortionCamera()->accept( iv ); 92 93 if ( intersector->containsIntersections() ) 94 { 95 osgUtil::LineSegmentIntersector::Intersection& result = *(intersector->getIntersections().begin()); 96 computeSelectedVertex( result ); 97 } 98 } 74 99 } 75 100 break; … … 92 117 switch(activeSetupMode) 93 118 { 94 case DISABLED : activeSetupMode = MANUAL; break; 95 case MANUAL : activeSetupMode = DELEGATED; break; 96 case DELEGATED : activeSetupMode = DISABLED; break; 119 case DISABLED : 120 { 121 activeSetupMode = MANUAL; 122 OSG_ALWAYS<<"SetupMode MANUAL activated"<<std::endl; 123 _distortionSet->getDistortionInternals()->setValue(1, true); // per definition #0 = mesh, #1 = highlighter 124 break; 125 } 126 case MANUAL : 127 { 128 activeSetupMode = DELEGATED; 129 _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter 130 OSG_ALWAYS<<"SetupMode DELEGATED activated"<<std::endl; 131 break; 132 } 133 case DELEGATED : 134 { 135 activeSetupMode = DISABLED; 136 _distortionSet->getDistortionInternals()->setValue(1, false); // per definition #0 = mesh, #1 = highlighter 137 OSG_ALWAYS<<"SetupMode DISABLED activated"<<std::endl; 138 break; 139 } 97 140 } 98 141 99 OSG_ALWAYS<<"KEY_KP_4 : Setup Mode now "<<activeSetupMode<<std::endl;100 142 return(true); 101 143 } … … 229 271 _distortionSet->setShowIntensityMapOnly(show); 230 272 } 273 274 void DistortionManipulator::createVertexHighlighter() 275 { 276 osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array(1); 277 (*colors)[0] = _highlightColor; 278 279 _highlighter = new osg::Geometry; 280 _highlighter->setDataVariance( osg::Object::DYNAMIC ); 281 _highlighter->setUseDisplayList( false ); 282 _highlighter->setUseVertexBufferObjects( true ); 283 _highlighter->setVertexArray( new osg::Vec3Array(1) ); // The highlighter vertex is updated by computeSelectedVertex(..) 284 _highlighter->setColorArray( colors.get() ); 285 _highlighter->setColorBinding( osg::Geometry::BIND_OVERALL ); 286 _highlighter->addPrimitiveSet( new osg::DrawArrays(GL_POINTS, 0, 1) ); 287 288 289 osg::ref_ptr<osg::Geode> geode = new osg::Geode; 290 geode->addDrawable( _highlighter.get() ); 291 geode->getOrCreateStateSet()->setAttributeAndModes( new osg::Point(8.0f) ); 292 geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); 293 geode->setCullingActive(false); // disable the culling for the selector, otherwise the selector is culled away on the edge. 294 295 _distortionSet->getDistortionInternals()->addChild(geode, false); 296 } 297 298 void DistortionManipulator::computeSelectedVertex( osgUtil::LineSegmentIntersector::Intersection& result ) 299 { 300 osg::Geometry* geom = dynamic_cast<osg::Geometry*>( result.drawable.get() ); 301 if ( !geom || geom==_highlighter ) 302 return; 303 304 osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>( geom->getVertexArray() ); 305 osg::Vec3Array* selVertices = dynamic_cast<osg::Vec3Array*>( _highlighter->getVertexArray() ); 306 if ( !vertices || !selVertices ) 307 return; 308 309 OSG_NOTIFY(osg::ALWAYS)<<"size of vertices="<<vertices->size()<<std::endl; 310 OSG_NOTIFY(osg::ALWAYS)<<"size of selVertices="<<selVertices->size()<<std::endl; 311 312 osg::Vec3 point = result.getWorldIntersectPoint(); 313 osg::Matrix matrix = osg::computeLocalToWorld( result.nodePath ); // To compute the intersection vertices in world coordinates not in model coordinates 314 OSG_NOTIFY(osg::ALWAYS) << "Intersection-indices: Size=" << result.indexList.size() << std::endl; 315 const std::vector<unsigned int>& selIndices = result.indexList; 316 { 317 double maxRatio = 0.0; 318 int closestVertexIndex = 0; 319 for ( unsigned int i=0; i<3 && i<result.ratioList.size(); i++ ) //iterate through rations and search for maxRation=nearestVertex 320 { 321 if(result.ratioList[i] > maxRatio) 322 { 323 maxRatio = result.ratioList[i]; 324 closestVertexIndex = result.indexList[i]; 325 } 326 OSG_NOTIFY(osg::ALWAYS)<<"maxRatio="<<maxRatio<<std::endl; 327 OSG_NOTIFY(osg::ALWAYS)<<"closestVertexIndex="<<closestVertexIndex<<std::endl; 328 } 329 OSG_NOTIFY(osg::ALWAYS)<<"nearest vertex: X="<<(*vertices)[closestVertexIndex].x()<<" Y="<<(*vertices)[closestVertexIndex].y()<<" Z="<<(*vertices)[closestVertexIndex].z()<<std::endl; 330 osg::Vec3 vertex = (*vertices)[closestVertexIndex] * matrix; 331 332 selVertices->front() = vertex; 333 OSG_NOTIFY(osg::ALWAYS)<<"selected vertice: X="<<vertex.x()<<" Y="<<vertex.y()<<" Z="<<vertex.z()<<std::endl; 334 } 335 selVertices->dirty(); 336 _highlighter->dirtyBound(); 337 }
Note: See TracChangeset
for help on using the changeset viewer.