root/OpenSceneGraph/trunk/examples/osgintersection/osgintersection.cpp @ 5664

Revision 5664, 6.0 kB (checked in by robert, 7 years ago)

Added osgUtil::IntersectorGroup? to handle groups of osgUtil::Intersectors

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
13struct 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
22int main(int argc, char **argv)
23{
24    // use an ArgumentParser object to manage the program arguments.
25    osg::ArgumentParser arguments(&argc,argv);
26   
27    // set up the usage document, in case we need to print out how to use this program.
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   
43   
44    osg::BoundingSphere bs = root->getBound();
45
46
47    bool useIntersectorGroup = true;
48   
49    if (useIntersectorGroup)
50    {
51        osg::Timer_t startTick = osg::Timer::instance()->tick();
52   
53        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
54        osg::Vec3d end = bs.center();// - osg::Vec3d(0.0, bs.radius(),0.0);
55        osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01);
56        osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0);
57        unsigned int numRows = 20;
58        unsigned int numColumns = 20;
59
60        osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
61
62        for(unsigned int r=0; r<numRows; ++r)
63        {
64            for(unsigned int c=0; c<numColumns; ++c)
65            {
66                osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r);
67                osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r);
68                osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(s, e);
69                intersectorGroup->addIntersector( intersector.get() );
70            }
71        }
72
73       
74        osgUtil::IntersectionVisitor intersectVisitor( intersectorGroup.get(), new MyReadCallback );
75        root->accept(intersectVisitor);
76
77        osg::Timer_t endTick = osg::Timer::instance()->tick();
78
79        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
80
81        if ( intersectorGroup->containsIntersections() )
82        {
83            std::cout<<"Found intersections "<<std::endl;
84
85            osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
86            for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
87                intersector_itr != intersectors.end();
88                ++intersector_itr)
89            {
90                osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get());
91                if (lsi)
92                {
93                    osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections();
94                    for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
95                        itr != intersections.end();
96                        ++itr)
97                    {
98                        const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
99                        std::cout<<"  ratio "<<intersection.ratio<<std::endl;
100                        std::cout<<"  point "<<intersection.localIntersectionPoint<<std::endl;
101                        std::cout<<"  normal "<<intersection.localIntersectionNormal<<std::endl;
102                        std::cout<<"  indices "<<intersection.indexList.size()<<std::endl;
103                        std::cout<<"  primitiveIndex "<<intersection.primitiveIndex<<std::endl;
104                        std::cout<<std::endl;
105                    }
106                }
107            }
108       
109        }
110
111    }
112    else
113    {
114        osg::Timer_t startTick = osg::Timer::instance()->tick();
115
116    #if 1
117        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
118        osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0);
119    #else
120        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,0.0, bs.radius());
121        osg::Vec3d end = bs.center() - osg::Vec3d(0.0, 0.0, bs.radius());
122    #endif
123
124        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end);
125
126        osgUtil::IntersectionVisitor intersectVisitor( intersector.get(), new MyReadCallback );
127
128        root->accept(intersectVisitor);
129
130        osg::Timer_t endTick = osg::Timer::instance()->tick();
131
132        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
133
134        if ( intersector->containsIntersections() )
135        {
136            osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections();
137            for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
138                itr != intersections.end();
139                ++itr)
140            {
141                const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
142                std::cout<<"  ratio "<<intersection.ratio<<std::endl;
143                std::cout<<"  point "<<intersection.localIntersectionPoint<<std::endl;
144                std::cout<<"  normal "<<intersection.localIntersectionNormal<<std::endl;
145                std::cout<<"  indices "<<intersection.indexList.size()<<std::endl;
146                std::cout<<"  primitiveIndex "<<intersection.primitiveIndex<<std::endl;
147                std::cout<<std::endl;
148            }
149        }
150    }
151   
152    return 0;
153}
Note: See TracBrowser for help on using the browser.