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

Revision 5675, 8.5 kB (checked in by robert, 7 years ago)

Implemented HeightAboveTerrain? and added usage into osgintersection

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