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

Revision 5673, 7.8 kB (checked in by robert, 8 years ago)

Added osgSim::LineOfSight? and osgSim::HeightAboveTerrain? classes

  • 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 <osgSim/LineOfSight>
12
13#include <iostream>
14
15struct MyReadCallback : public osgUtil::IntersectionVisitor::ReadCallback
16{
17    virtual osg::Node* readNodeFile(const std::string& filename)
18    {
19        return osgDB::readNodeFile(filename);
20    }
21};
22
23
24int main(int argc, char **argv)
25{
26    // use an ArgumentParser object to manage the program arguments.
27    osg::ArgumentParser arguments(&argc,argv);
28   
29    // set up the usage document, in case we need to print out how to use this program.
30    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of node tracker.");
31    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName());
32    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
33   
34    osg::ref_ptr<osg::Node> root = osgDB::readNodeFiles(arguments);
35   
36    if (!root)
37    {
38        std::cout<<"No model loaded, please specify a valid model on the command line."<<std::endl;
39        return 0;
40    }
41   
42    std::cout<<"Intersection "<<std::endl;
43
44   
45   
46    osg::BoundingSphere bs = root->getBound();
47
48
49    bool useIntersectorGroup = true;
50    bool useLineOfSight = true;
51   
52    if (useLineOfSight)
53    {
54        osg::Timer_t startTick = osg::Timer::instance()->tick();
55   
56        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
57        osg::Vec3d end = bs.center();// - osg::Vec3d(0.0, bs.radius(),0.0);
58        osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01);
59        osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0);
60        unsigned int numRows = 50;
61        unsigned int numColumns = 50;
62
63        osgSim::LineOfSight los;
64
65        for(unsigned int r=0; r<numRows; ++r)
66        {
67            for(unsigned int c=0; c<numColumns; ++c)
68            {
69                osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r);
70                osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r);
71                los.addLOS(s,e);
72            }
73        }
74
75
76        los.computeIntersections(root.get());
77       
78        osg::Timer_t endTick = osg::Timer::instance()->tick();
79
80        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
81
82#if 0
83        for(unsigned int i=0; i<los.getNumLOS(); i++)
84        {
85            const osgSim::LineOfSight::Intersections& intersections = los.getIntersections(i);
86            for(osgSim::LineOfSight::Intersections::const_iterator itr = intersections.begin();
87                itr != intersections.end();
88                ++itr)
89            {
90                 std::cout<<"  point "<<*itr<<std::endl;
91            }
92        }
93#endif
94
95        // now do a second traversal to test performance of cache.
96        startTick = osg::Timer::instance()->tick();
97
98        los.computeIntersections(root.get());
99       
100        endTick = osg::Timer::instance()->tick();
101
102        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
103
104    }
105    else if (useIntersectorGroup)
106    {
107        osg::Timer_t startTick = osg::Timer::instance()->tick();
108   
109        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
110        osg::Vec3d end = bs.center();// - osg::Vec3d(0.0, bs.radius(),0.0);
111        osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01);
112        osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0);
113        unsigned int numRows = 20;
114        unsigned int numColumns = 20;
115
116        osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
117
118        for(unsigned int r=0; r<numRows; ++r)
119        {
120            for(unsigned int c=0; c<numColumns; ++c)
121            {
122                osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r);
123                osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r);
124                osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(s, e);
125                intersectorGroup->addIntersector( intersector.get() );
126            }
127        }
128
129       
130        osgUtil::IntersectionVisitor intersectVisitor( intersectorGroup.get(), new MyReadCallback );
131        root->accept(intersectVisitor);
132
133        osg::Timer_t endTick = osg::Timer::instance()->tick();
134
135        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
136
137        if ( intersectorGroup->containsIntersections() )
138        {
139            std::cout<<"Found intersections "<<std::endl;
140
141            osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
142            for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
143                intersector_itr != intersectors.end();
144                ++intersector_itr)
145            {
146                osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get());
147                if (lsi)
148                {
149                    osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections();
150                    for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
151                        itr != intersections.end();
152                        ++itr)
153                    {
154                        const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
155                        std::cout<<"  ratio "<<intersection.ratio<<std::endl;
156                        std::cout<<"  point "<<intersection.localIntersectionPoint<<std::endl;
157                        std::cout<<"  normal "<<intersection.localIntersectionNormal<<std::endl;
158                        std::cout<<"  indices "<<intersection.indexList.size()<<std::endl;
159                        std::cout<<"  primitiveIndex "<<intersection.primitiveIndex<<std::endl;
160                        std::cout<<std::endl;
161                    }
162                }
163            }
164       
165        }
166
167    }
168    else
169    {
170        osg::Timer_t startTick = osg::Timer::instance()->tick();
171
172    #if 1
173        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
174        osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0);
175    #else
176        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,0.0, bs.radius());
177        osg::Vec3d end = bs.center() - osg::Vec3d(0.0, 0.0, bs.radius());
178    #endif
179
180        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end);
181
182        osgUtil::IntersectionVisitor intersectVisitor( intersector.get(), new MyReadCallback );
183
184        root->accept(intersectVisitor);
185
186        osg::Timer_t endTick = osg::Timer::instance()->tick();
187
188        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
189
190        if ( intersector->containsIntersections() )
191        {
192            osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections();
193            for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
194                itr != intersections.end();
195                ++itr)
196            {
197                const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
198                std::cout<<"  ratio "<<intersection.ratio<<std::endl;
199                std::cout<<"  point "<<intersection.localIntersectionPoint<<std::endl;
200                std::cout<<"  normal "<<intersection.localIntersectionNormal<<std::endl;
201                std::cout<<"  indices "<<intersection.indexList.size()<<std::endl;
202                std::cout<<"  primitiveIndex "<<intersection.primitiveIndex<<std::endl;
203                std::cout<<std::endl;
204            }
205        }
206    }
207   
208    return 0;
209}
Note: See TracBrowser for help on using the browser.