| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #include <osg/Timer> |
|---|
| 23 | #include <osg/io_utils> |
|---|
| 24 | #include <osg/observer_ptr> |
|---|
| 25 | |
|---|
| 26 | #include <osgUtil/IntersectionVisitor> |
|---|
| 27 | #include <osgUtil/PolytopeIntersector> |
|---|
| 28 | #include <osgUtil/LineSegmentIntersector> |
|---|
| 29 | |
|---|
| 30 | #include <osgDB/ReadFile> |
|---|
| 31 | #include <osgDB/WriteFile> |
|---|
| 32 | |
|---|
| 33 | #include <osgGA/TrackballManipulator> |
|---|
| 34 | #include <osgGA/StateSetManipulator> |
|---|
| 35 | |
|---|
| 36 | #include <osgViewer/Viewer> |
|---|
| 37 | |
|---|
| 38 | #include <osgFX/Scribe> |
|---|
| 39 | |
|---|
| 40 | #include <iostream> |
|---|
| 41 | |
|---|
| 42 | class CreateModelToSaveVisitor : public osg::NodeVisitor |
|---|
| 43 | { |
|---|
| 44 | public: |
|---|
| 45 | |
|---|
| 46 | CreateModelToSaveVisitor(): |
|---|
| 47 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 48 | { |
|---|
| 49 | _group = new osg::Group; |
|---|
| 50 | _addToModel = false; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | virtual void apply(osg::Node& node) |
|---|
| 54 | { |
|---|
| 55 | osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node); |
|---|
| 56 | if (scribe) |
|---|
| 57 | { |
|---|
| 58 | for(unsigned int i=0; i<scribe->getNumChildren(); ++i) |
|---|
| 59 | { |
|---|
| 60 | _group->addChild(scribe->getChild(i)); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | else |
|---|
| 64 | { |
|---|
| 65 | traverse(node); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | osg::ref_ptr<osg::Group> _group; |
|---|
| 70 | bool _addToModel; |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | class DeleteSelectedNodesVisitor : public osg::NodeVisitor |
|---|
| 74 | { |
|---|
| 75 | public: |
|---|
| 76 | |
|---|
| 77 | DeleteSelectedNodesVisitor(): |
|---|
| 78 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) |
|---|
| 79 | { |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | virtual void apply(osg::Node& node) |
|---|
| 83 | { |
|---|
| 84 | osgFX::Scribe* scribe = dynamic_cast<osgFX::Scribe*>(&node); |
|---|
| 85 | if (scribe) |
|---|
| 86 | { |
|---|
| 87 | _selectedNodes.push_back(scribe); |
|---|
| 88 | } |
|---|
| 89 | else |
|---|
| 90 | { |
|---|
| 91 | traverse(node); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void pruneSelectedNodes() |
|---|
| 96 | { |
|---|
| 97 | for(SelectedNodes::iterator itr = _selectedNodes.begin(); |
|---|
| 98 | itr != _selectedNodes.end(); |
|---|
| 99 | ++itr) |
|---|
| 100 | { |
|---|
| 101 | osg::Node* node = itr->get(); |
|---|
| 102 | osg::Node::ParentList parents = node->getParents(); |
|---|
| 103 | for(osg::Node::ParentList::iterator pitr = parents.begin(); |
|---|
| 104 | pitr != parents.end(); |
|---|
| 105 | ++pitr) |
|---|
| 106 | { |
|---|
| 107 | osg::Group* parent = *pitr; |
|---|
| 108 | parent->removeChild(node); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | typedef std::vector< osg::ref_ptr<osgFX::Scribe> > SelectedNodes; |
|---|
| 114 | SelectedNodes _selectedNodes; |
|---|
| 115 | |
|---|
| 116 | }; |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | class PickHandler : public osgGA::GUIEventHandler |
|---|
| 120 | { |
|---|
| 121 | public: |
|---|
| 122 | |
|---|
| 123 | PickHandler(): |
|---|
| 124 | _mx(0.0),_my(0.0), |
|---|
| 125 | _usePolytopeIntersector(false), |
|---|
| 126 | _useWindowCoordinates(false) {} |
|---|
| 127 | |
|---|
| 128 | ~PickHandler() {} |
|---|
| 129 | |
|---|
| 130 | bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) |
|---|
| 131 | { |
|---|
| 132 | osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa); |
|---|
| 133 | if (!viewer) return false; |
|---|
| 134 | |
|---|
| 135 | switch(ea.getEventType()) |
|---|
| 136 | { |
|---|
| 137 | case(osgGA::GUIEventAdapter::KEYUP): |
|---|
| 138 | { |
|---|
| 139 | if (ea.getKey()=='s') |
|---|
| 140 | { |
|---|
| 141 | saveSelectedModel(viewer->getSceneData()); |
|---|
| 142 | } |
|---|
| 143 | else if (ea.getKey()=='o') |
|---|
| 144 | { |
|---|
| 145 | osg::notify(osg::NOTICE)<<"Saved model to file 'saved_model.osg'"<<std::endl; |
|---|
| 146 | osgDB::writeNodeFile(*(viewer->getSceneData()), "saved_model.osg"); |
|---|
| 147 | } |
|---|
| 148 | else if (ea.getKey()=='p') |
|---|
| 149 | { |
|---|
| 150 | _usePolytopeIntersector = !_usePolytopeIntersector; |
|---|
| 151 | if (_usePolytopeIntersector) |
|---|
| 152 | { |
|---|
| 153 | osg::notify(osg::NOTICE)<<"Using PolytopeIntersector"<<std::endl; |
|---|
| 154 | } else { |
|---|
| 155 | osg::notify(osg::NOTICE)<<"Using LineSegmentIntersector"<<std::endl; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | else if (ea.getKey()=='c') |
|---|
| 159 | { |
|---|
| 160 | _useWindowCoordinates = !_useWindowCoordinates; |
|---|
| 161 | if (_useWindowCoordinates) |
|---|
| 162 | { |
|---|
| 163 | osg::notify(osg::NOTICE)<<"Using window coordinates for picking"<<std::endl; |
|---|
| 164 | } else { |
|---|
| 165 | osg::notify(osg::NOTICE)<<"Using projection coordiates for picking"<<std::endl; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Delete || ea.getKey()==osgGA::GUIEventAdapter::KEY_BackSpace) |
|---|
| 169 | { |
|---|
| 170 | osg::notify(osg::NOTICE)<<"Delete"<<std::endl; |
|---|
| 171 | DeleteSelectedNodesVisitor dsnv; |
|---|
| 172 | viewer->getSceneData()->accept(dsnv); |
|---|
| 173 | dsnv.pruneSelectedNodes(); |
|---|
| 174 | } |
|---|
| 175 | return false; |
|---|
| 176 | } |
|---|
| 177 | case(osgGA::GUIEventAdapter::PUSH): |
|---|
| 178 | case(osgGA::GUIEventAdapter::MOVE): |
|---|
| 179 | { |
|---|
| 180 | _mx = ea.getX(); |
|---|
| 181 | _my = ea.getY(); |
|---|
| 182 | return false; |
|---|
| 183 | } |
|---|
| 184 | case(osgGA::GUIEventAdapter::RELEASE): |
|---|
| 185 | { |
|---|
| 186 | if (_mx == ea.getX() && _my == ea.getY()) |
|---|
| 187 | { |
|---|
| 188 | |
|---|
| 189 | pick(ea,viewer); |
|---|
| 190 | } |
|---|
| 191 | return true; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | default: |
|---|
| 195 | return false; |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | void pick(const osgGA::GUIEventAdapter& ea, osgViewer::Viewer* viewer) |
|---|
| 200 | { |
|---|
| 201 | osg::Node* scene = viewer->getSceneData(); |
|---|
| 202 | if (!scene) return; |
|---|
| 203 | |
|---|
| 204 | osg::notify(osg::NOTICE)<<std::endl; |
|---|
| 205 | |
|---|
| 206 | osg::Node* node = 0; |
|---|
| 207 | osg::Group* parent = 0; |
|---|
| 208 | |
|---|
| 209 | if (_usePolytopeIntersector) |
|---|
| 210 | { |
|---|
| 211 | osgUtil::PolytopeIntersector* picker; |
|---|
| 212 | if (_useWindowCoordinates) |
|---|
| 213 | { |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | osg::Viewport* viewport = viewer->getCamera()->getViewport(); |
|---|
| 217 | double mx = viewport->x() + (int)((double )viewport->width()*(ea.getXnormalized()*0.5+0.5)); |
|---|
| 218 | double my = viewport->y() + (int)((double )viewport->height()*(ea.getYnormalized()*0.5+0.5)); |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | double w = 5.0f; |
|---|
| 222 | double h = 5.0f; |
|---|
| 223 | picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::WINDOW, mx-w, my-h, mx+w, my+h ); |
|---|
| 224 | } else { |
|---|
| 225 | double mx = ea.getXnormalized(); |
|---|
| 226 | double my = ea.getYnormalized(); |
|---|
| 227 | double w = 0.05; |
|---|
| 228 | double h = 0.05; |
|---|
| 229 | picker = new osgUtil::PolytopeIntersector( osgUtil::Intersector::PROJECTION, mx-w, my-h, mx+w, my+h ); |
|---|
| 230 | } |
|---|
| 231 | osgUtil::IntersectionVisitor iv(picker); |
|---|
| 232 | |
|---|
| 233 | viewer->getCamera()->accept(iv); |
|---|
| 234 | |
|---|
| 235 | if (picker->containsIntersections()) |
|---|
| 236 | { |
|---|
| 237 | osgUtil::PolytopeIntersector::Intersection intersection = picker->getFirstIntersection(); |
|---|
| 238 | |
|---|
| 239 | osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl |
|---|
| 240 | <<" Distance to ref. plane "<<intersection.distance |
|---|
| 241 | <<", max. dist "<<intersection.maxDistance |
|---|
| 242 | <<", primitive index "<<intersection.primitiveIndex |
|---|
| 243 | <<", numIntersectionPoints " |
|---|
| 244 | <<intersection.numIntersectionPoints |
|---|
| 245 | <<std::endl; |
|---|
| 246 | |
|---|
| 247 | osg::NodePath& nodePath = intersection.nodePath; |
|---|
| 248 | node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
|---|
| 249 | parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
|---|
| 250 | |
|---|
| 251 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size "<<nodePath.size()<<std::endl; |
|---|
| 252 | toggleScribe(parent, node); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | } |
|---|
| 256 | else |
|---|
| 257 | { |
|---|
| 258 | osgUtil::LineSegmentIntersector* picker; |
|---|
| 259 | if (!_useWindowCoordinates) |
|---|
| 260 | { |
|---|
| 261 | |
|---|
| 262 | picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::PROJECTION, ea.getXnormalized(),ea.getYnormalized() ); |
|---|
| 263 | } else { |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | osg::Viewport* viewport = viewer->getCamera()->getViewport(); |
|---|
| 267 | float mx = viewport->x() + (int)((float)viewport->width()*(ea.getXnormalized()*0.5f+0.5f)); |
|---|
| 268 | float my = viewport->y() + (int)((float)viewport->height()*(ea.getYnormalized()*0.5f+0.5f)); |
|---|
| 269 | picker = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, mx, my ); |
|---|
| 270 | } |
|---|
| 271 | osgUtil::IntersectionVisitor iv(picker); |
|---|
| 272 | |
|---|
| 273 | viewer->getCamera()->accept(iv); |
|---|
| 274 | |
|---|
| 275 | if (picker->containsIntersections()) |
|---|
| 276 | { |
|---|
| 277 | osgUtil::LineSegmentIntersector::Intersection intersection = picker->getFirstIntersection(); |
|---|
| 278 | osg::notify(osg::NOTICE)<<"Picked "<<intersection.localIntersectionPoint<<std::endl; |
|---|
| 279 | |
|---|
| 280 | osg::NodePath& nodePath = intersection.nodePath; |
|---|
| 281 | node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; |
|---|
| 282 | parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0; |
|---|
| 283 | |
|---|
| 284 | if (node) std::cout<<" Hits "<<node->className()<<" nodePath size"<<nodePath.size()<<std::endl; |
|---|
| 285 | toggleScribe(parent, node); |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | void toggleScribe(osg::Group* parent, osg::Node* node) { |
|---|
| 293 | if (!parent || !node) return; |
|---|
| 294 | |
|---|
| 295 | std::cout<<" parent "<<parent->className()<<std::endl; |
|---|
| 296 | |
|---|
| 297 | osgFX::Scribe* parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent); |
|---|
| 298 | if (!parentAsScribe) |
|---|
| 299 | { |
|---|
| 300 | |
|---|
| 301 | osgFX::Scribe* scribe = new osgFX::Scribe(); |
|---|
| 302 | scribe->addChild(node); |
|---|
| 303 | parent->replaceChild(node,scribe); |
|---|
| 304 | } |
|---|
| 305 | else |
|---|
| 306 | { |
|---|
| 307 | |
|---|
| 308 | osg::Node::ParentList parentList = parentAsScribe->getParents(); |
|---|
| 309 | for(osg::Node::ParentList::iterator itr=parentList.begin(); |
|---|
| 310 | itr!=parentList.end(); |
|---|
| 311 | ++itr) |
|---|
| 312 | { |
|---|
| 313 | (*itr)->replaceChild(parentAsScribe,node); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | void saveSelectedModel(osg::Node* scene) |
|---|
| 320 | { |
|---|
| 321 | if (!scene) return; |
|---|
| 322 | |
|---|
| 323 | CreateModelToSaveVisitor cmtsv; |
|---|
| 324 | scene->accept(cmtsv); |
|---|
| 325 | |
|---|
| 326 | if (cmtsv._group->getNumChildren()>0) |
|---|
| 327 | { |
|---|
| 328 | std::cout<<"Writing selected compoents to 'selected_model.osg'"<<std::endl; |
|---|
| 329 | osgDB::writeNodeFile(*cmtsv._group, "selected_model.osg"); |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | protected: |
|---|
| 334 | |
|---|
| 335 | float _mx,_my; |
|---|
| 336 | bool _usePolytopeIntersector; |
|---|
| 337 | bool _useWindowCoordinates; |
|---|
| 338 | }; |
|---|
| 339 | |
|---|
| 340 | int main( int argc, char **argv ) |
|---|
| 341 | { |
|---|
| 342 | osg::ref_ptr<osg::Node> loadedModel; |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | if (argc>1) loadedModel = osgDB::readNodeFile(argv[1]); |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | if (!loadedModel) loadedModel = osgDB::readNodeFile("dumptruck.osg"); |
|---|
| 349 | |
|---|
| 350 | if (!loadedModel) |
|---|
| 351 | { |
|---|
| 352 | std::cout << argv[0] <<": No data loaded." << std::endl; |
|---|
| 353 | return 1; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 358 | traits->x = 200; |
|---|
| 359 | traits->y = 200; |
|---|
| 360 | traits->width = 800; |
|---|
| 361 | traits->height = 600; |
|---|
| 362 | traits->windowDecoration = true; |
|---|
| 363 | traits->doubleBuffer = true; |
|---|
| 364 | traits->sharedContext = 0; |
|---|
| 365 | |
|---|
| 366 | osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 367 | osgViewer::GraphicsWindow* gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get()); |
|---|
| 368 | if (!gw) |
|---|
| 369 | { |
|---|
| 370 | osg::notify(osg::NOTICE)<<"Error: unable to create graphics window."<<std::endl; |
|---|
| 371 | return 1; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | osgViewer::Viewer viewer; |
|---|
| 376 | viewer.getCamera()->setGraphicsContext(gc.get()); |
|---|
| 377 | viewer.getCamera()->setViewport(0,0,800,600); |
|---|
| 378 | viewer.setSceneData(loadedModel.get()); |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | viewer.setCameraManipulator( new osgGA::TrackballManipulator ); |
|---|
| 382 | |
|---|
| 383 | osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(viewer.getCamera()->getStateSet()); |
|---|
| 384 | viewer.addEventHandler(statesetManipulator.get()); |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | viewer.addEventHandler(new PickHandler()); |
|---|
| 388 | |
|---|
| 389 | viewer.realize(); |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | while(!viewer.done()) |
|---|
| 393 | { |
|---|
| 394 | viewer.frame(); |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | return 0; |
|---|
| 398 | } |
|---|