| 1 | #include <osg/ArgumentParser> |
|---|
| 2 | #include <osg/ApplicationUsage> |
|---|
| 3 | #include <osg/Timer> |
|---|
| 4 | #include <osg/Notify> |
|---|
| 5 | #include <osg/io_utils> |
|---|
| 6 | |
|---|
| 7 | #include <osgDB/ReadFile> |
|---|
| 8 | |
|---|
| 9 | #include <osgUtil/IntersectionVisitor> |
|---|
| 10 | |
|---|
| 11 | #include <iostream> |
|---|
| 12 | |
|---|
| 13 | struct MyReadCallback : public osgUtil::IntersectionVisitor::ReadCallback |
|---|
| 14 | { |
|---|
| 15 | virtual osg::Node* readNodeFile(const std::string& filename) |
|---|
| 16 | { |
|---|
| 17 | return osgDB::readNodeFile(filename); |
|---|
| 18 | } |
|---|
| 19 | }; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | int main(int argc, char **argv) |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker."); |
|---|
| 29 | arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()); |
|---|
| 30 | arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information"); |
|---|
| 31 | |
|---|
| 32 | osg::ref_ptr<osg::Node> root = osgDB::readNodeFiles(arguments); |
|---|
| 33 | |
|---|
| 34 | if (!root) |
|---|
| 35 | { |
|---|
| 36 | std::cout<<"No model loaded, please specify a valid model on the command line."<<std::endl; |
|---|
| 37 | return 0; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | std::cout<<"Intersection "<<std::endl; |
|---|
| 41 | |
|---|
| 42 | osg::Timer_t startTick = osg::Timer::instance()->tick(); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | osg::BoundingSphere bs = root->getBound(); |
|---|
| 46 | #if 1 |
|---|
| 47 | osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0); |
|---|
| 48 | osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0); |
|---|
| 49 | #else |
|---|
| 50 | osg::Vec3d start = bs.center() + osg::Vec3d(0.0,0.0, bs.radius()); |
|---|
| 51 | osg::Vec3d end = bs.center() - osg::Vec3d(0.0, 0.0, bs.radius()); |
|---|
| 52 | #endif |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end); |
|---|
| 56 | |
|---|
| 57 | osgUtil::IntersectionVisitor intersectVisitor( intersector.get(), new MyReadCallback ); |
|---|
| 58 | |
|---|
| 59 | root->accept(intersectVisitor); |
|---|
| 60 | |
|---|
| 61 | osg::Timer_t endTick = osg::Timer::instance()->tick(); |
|---|
| 62 | |
|---|
| 63 | std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl; |
|---|
| 64 | |
|---|
| 65 | if ( intersector->containsIntersections() ) |
|---|
| 66 | { |
|---|
| 67 | osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections(); |
|---|
| 68 | for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin(); |
|---|
| 69 | itr != intersections.end(); |
|---|
| 70 | ++itr) |
|---|
| 71 | { |
|---|
| 72 | const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr; |
|---|
| 73 | std::cout<<" ratio "<<intersection.ratio<<std::endl; |
|---|
| 74 | std::cout<<" point "<<intersection.localIntersectionPoint<<std::endl; |
|---|
| 75 | std::cout<<" normal "<<intersection.localIntersectionNormal<<std::endl; |
|---|
| 76 | std::cout<<" indices "<<intersection.indexList.size()<<std::endl; |
|---|
| 77 | std::cout<<" primitiveIndex "<<intersection.primitiveIndex<<std::endl; |
|---|
| 78 | std::cout<<std::endl; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | return 0; |
|---|
| 83 | } |
|---|