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

Revision 6941, 10.5 kB (checked in by robert, 8 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgintersection.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/ArgumentParser>
20#include <osg/ApplicationUsage>
21#include <osg/Timer>
22#include <osg/CoordinateSystemNode>
23#include <osg/Notify>
24#include <osg/io_utils>
25
26#include <osgDB/ReadFile>
27
28#include <osgUtil/IntersectionVisitor>
29#include <osgUtil/LineSegmentIntersector>
30
31#include <osgSim/LineOfSight>
32#include <osgSim/HeightAboveTerrain>
33#include <osgSim/ElevationSlice>
34
35#include <iostream>
36
37struct MyReadCallback : public osgUtil::IntersectionVisitor::ReadCallback
38{
39    virtual osg::Node* readNodeFile(const std::string& filename)
40    {
41        return osgDB::readNodeFile(filename);
42    }
43};
44
45
46int main(int argc, char **argv)
47{
48    // use an ArgumentParser object to manage the program arguments.
49    osg::ArgumentParser arguments(&argc,argv);
50   
51    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
52   
53    if (!scene)
54    {
55        std::cout<<"No model loaded, please specify a valid model on the command line."<<std::endl;
56        return 0;
57    }
58   
59    std::cout<<"Intersection "<<std::endl;
60   
61   
62    osg::BoundingSphere bs = scene->getBound();
63
64
65    bool useIntersectorGroup = true;
66    bool useLineOfSight = true;
67   
68    //osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(scene.get());
69    //osg::EllipsoidModel* em = csn ? csn->getEllipsoidModel() : 0;
70
71    if (useLineOfSight)
72    {
73   
74        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
75        osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0);
76        osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01);
77        osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0);
78
79        osgSim::LineOfSight los;
80       
81#if 1
82        unsigned int numRows = 20;
83        unsigned int numColumns = 20;
84        osgSim::HeightAboveTerrain hat;
85        hat.setDatabaseCacheReadCallback(los.getDatabaseCacheReadCallback());
86
87        for(unsigned int r=0; r<numRows; ++r)
88        {
89            for(unsigned int c=0; c<numColumns; ++c)
90            {
91                osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r);
92                osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r);
93                los.addLOS(s,e);
94                hat.addPoint(s);
95            }
96        }
97
98
99        {
100            std::cout<<"Computing LineOfSight"<<std::endl;
101
102            osg::Timer_t startTick = osg::Timer::instance()->tick();
103
104            los.computeIntersections(scene.get());
105
106            osg::Timer_t endTick = osg::Timer::instance()->tick();
107
108            std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
109
110            for(unsigned int i=0; i<los.getNumLOS(); i++)
111            {
112                const osgSim::LineOfSight::Intersections& intersections = los.getIntersections(i);
113                for(osgSim::LineOfSight::Intersections::const_iterator itr = intersections.begin();
114                    itr != intersections.end();
115                    ++itr)
116                {
117                     std::cout<<"  point "<<*itr<<std::endl;
118                }
119            }
120        }
121       
122        {
123            // now do a second traversal to test performance of cache.
124            osg::Timer_t startTick = osg::Timer::instance()->tick();
125
126            std::cout<<"Computing HeightAboveTerrain"<<std::endl;
127
128            hat.computeIntersections(scene.get());
129
130            osg::Timer_t endTick = osg::Timer::instance()->tick();
131
132            for(unsigned int i=0; i<hat.getNumPoints(); i++)
133            {
134                 std::cout<<"  point = "<<hat.getPoint(i)<<" hat = "<<hat.getHeightAboveTerrain(i)<<std::endl;
135            }
136
137
138            std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
139        }
140#endif
141
142        {
143            // now do a second traversal to test performance of cache.
144            osg::Timer_t startTick = osg::Timer::instance()->tick();
145
146            std::cout<<"Computing ElevationSlice"<<std::endl;
147            osgSim::ElevationSlice es;
148            es.setDatabaseCacheReadCallback(los.getDatabaseCacheReadCallback());
149
150            es.setStartPoint(bs.center()+osg::Vec3d(bs.radius(),0.0,0.0) );
151            es.setEndPoint(bs.center()+osg::Vec3d(0.0,0.0, bs.radius()) );
152
153            es.computeIntersections(scene.get());
154
155            osg::Timer_t endTick = osg::Timer::instance()->tick();
156
157            std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
158
159            typedef osgSim::ElevationSlice::DistanceHeightList DistanceHeightList;
160            const DistanceHeightList& dhl = es.getDistanceHeightIntersections();
161            std::cout<<"Number of intersections ="<<dhl.size()<<std::endl;
162            for(DistanceHeightList::const_iterator dhitr = dhl.begin();
163                dhitr != dhl.end();
164                ++dhitr)
165            {
166                 std::cout.precision(10);
167                 std::cout<<"  "<<dhitr->first<<" "<<dhitr->second<<std::endl;
168            }
169
170
171        }
172    }
173    else if (useIntersectorGroup)
174    {
175        osg::Timer_t startTick = osg::Timer::instance()->tick();
176   
177        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
178        osg::Vec3d end = bs.center();// - osg::Vec3d(0.0, bs.radius(),0.0);
179        osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01);
180        osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0);
181        unsigned int numRows = 20;
182        unsigned int numColumns = 20;
183
184        osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
185
186        for(unsigned int r=0; r<numRows; ++r)
187        {
188            for(unsigned int c=0; c<numColumns; ++c)
189            {
190                osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r);
191                osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r);
192                osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(s, e);
193                intersectorGroup->addIntersector( intersector.get() );
194            }
195        }
196
197       
198        osgUtil::IntersectionVisitor intersectVisitor( intersectorGroup.get(), new MyReadCallback );
199        scene->accept(intersectVisitor);
200
201        osg::Timer_t endTick = osg::Timer::instance()->tick();
202
203        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
204
205        if ( intersectorGroup->containsIntersections() )
206        {
207            std::cout<<"Found intersections "<<std::endl;
208
209            osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
210            for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
211                intersector_itr != intersectors.end();
212                ++intersector_itr)
213            {
214                osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get());
215                if (lsi)
216                {
217                    osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections();
218                    for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
219                        itr != intersections.end();
220                        ++itr)
221                    {
222                        const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
223                        std::cout<<"  ratio "<<intersection.ratio<<std::endl;
224                        std::cout<<"  point "<<intersection.localIntersectionPoint<<std::endl;
225                        std::cout<<"  normal "<<intersection.localIntersectionNormal<<std::endl;
226                        std::cout<<"  indices "<<intersection.indexList.size()<<std::endl;
227                        std::cout<<"  primitiveIndex "<<intersection.primitiveIndex<<std::endl;
228                        std::cout<<std::endl;
229                    }
230                }
231            }
232       
233        }
234
235    }
236    else
237    {
238        osg::Timer_t startTick = osg::Timer::instance()->tick();
239
240    #if 1
241        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
242        osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0);
243    #else
244        osg::Vec3d start = bs.center() + osg::Vec3d(0.0,0.0, bs.radius());
245        osg::Vec3d end = bs.center() - osg::Vec3d(0.0, 0.0, bs.radius());
246    #endif
247
248        osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end);
249
250        osgUtil::IntersectionVisitor intersectVisitor( intersector.get(), new MyReadCallback );
251
252        scene->accept(intersectVisitor);
253
254        osg::Timer_t endTick = osg::Timer::instance()->tick();
255
256        std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
257
258        if ( intersector->containsIntersections() )
259        {
260            osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections();
261            for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
262                itr != intersections.end();
263                ++itr)
264            {
265                const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
266                std::cout<<"  ratio "<<intersection.ratio<<std::endl;
267                std::cout<<"  point "<<intersection.localIntersectionPoint<<std::endl;
268                std::cout<<"  normal "<<intersection.localIntersectionNormal<<std::endl;
269                std::cout<<"  indices "<<intersection.indexList.size()<<std::endl;
270                std::cout<<"  primitiveIndex "<<intersection.primitiveIndex<<std::endl;
271                std::cout<<std::endl;
272            }
273        }
274    }
275   
276    return 0;
277}
Note: See TracBrowser for help on using the browser.