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

Revision 5763, 9.4 kB (checked in by robert, 7 years ago)

Added support for float or double osg::Plane, and the default osg::Plane to double.
Performance tests on big models did not indicate any performance penalty in using doubles over floats,
so the move to doubles should mainly impact precision improvements for whole earth databases.

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