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

Revision 6177, 9.5 kB (checked in by robert, 8 years ago)

Fixed warnings

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