2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgintersection.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2006-10-27 00:03:17 +08:00
|
|
|
#include <osg/ArgumentParser>
|
|
|
|
#include <osg/ApplicationUsage>
|
|
|
|
#include <osg/Timer>
|
2006-10-31 20:59:51 +08:00
|
|
|
#include <osg/CoordinateSystemNode>
|
2006-10-27 00:03:17 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/io_utils>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <osgUtil/IntersectionVisitor>
|
2006-11-29 00:30:38 +08:00
|
|
|
#include <osgUtil/LineSegmentIntersector>
|
2006-10-27 00:03:17 +08:00
|
|
|
|
2006-10-31 04:29:06 +08:00
|
|
|
#include <osgSim/LineOfSight>
|
2006-10-31 20:59:51 +08:00
|
|
|
#include <osgSim/HeightAboveTerrain>
|
2006-11-29 00:00:52 +08:00
|
|
|
#include <osgSim/ElevationSlice>
|
2006-10-31 04:29:06 +08:00
|
|
|
|
2006-10-27 00:03:17 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
struct MyReadCallback : public osgUtil::IntersectionVisitor::ReadCallback
|
|
|
|
{
|
|
|
|
virtual osg::Node* readNodeFile(const std::string& filename)
|
|
|
|
{
|
2015-10-22 22:14:53 +08:00
|
|
|
return osgDB::readRefNodeFile(filename).release();
|
2006-10-27 00:03:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
2015-10-22 22:14:53 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::Node> scene = osgDB::readRefNodeFiles(arguments);
|
|
|
|
|
|
|
|
if (!scene)
|
2006-10-27 00:03:17 +08:00
|
|
|
{
|
|
|
|
std::cout<<"No model loaded, please specify a valid model on the command line."<<std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-27 00:03:17 +08:00
|
|
|
std::cout<<"Intersection "<<std::endl;
|
2015-10-22 22:14:53 +08:00
|
|
|
|
|
|
|
|
2006-10-31 20:59:51 +08:00
|
|
|
osg::BoundingSphere bs = scene->getBound();
|
2006-10-27 00:03:17 +08:00
|
|
|
|
2006-10-27 23:11:17 +08:00
|
|
|
|
|
|
|
bool useIntersectorGroup = true;
|
2006-10-31 04:29:06 +08:00
|
|
|
bool useLineOfSight = true;
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2007-02-14 06:24:51 +08:00
|
|
|
//osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(scene.get());
|
|
|
|
//osg::EllipsoidModel* em = csn ? csn->getEllipsoidModel() : 0;
|
|
|
|
|
2006-10-31 04:29:06 +08:00
|
|
|
if (useLineOfSight)
|
|
|
|
{
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-31 04:29:06 +08:00
|
|
|
osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
|
2006-11-29 00:00:52 +08:00
|
|
|
osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0);
|
2006-10-31 04:29:06 +08:00
|
|
|
osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01);
|
|
|
|
osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0);
|
|
|
|
|
|
|
|
osgSim::LineOfSight los;
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2007-02-14 06:24:51 +08:00
|
|
|
#if 1
|
|
|
|
unsigned int numRows = 20;
|
|
|
|
unsigned int numColumns = 20;
|
2006-10-31 20:59:51 +08:00
|
|
|
osgSim::HeightAboveTerrain hat;
|
|
|
|
hat.setDatabaseCacheReadCallback(los.getDatabaseCacheReadCallback());
|
2006-10-31 04:29:06 +08:00
|
|
|
|
|
|
|
for(unsigned int r=0; r<numRows; ++r)
|
|
|
|
{
|
|
|
|
for(unsigned int c=0; c<numColumns; ++c)
|
|
|
|
{
|
|
|
|
osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r);
|
|
|
|
osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r);
|
|
|
|
los.addLOS(s,e);
|
2006-10-31 20:59:51 +08:00
|
|
|
hat.addPoint(s);
|
2006-10-31 04:29:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
{
|
|
|
|
std::cout<<"Computing LineOfSight"<<std::endl;
|
2006-10-31 20:59:51 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
osg::Timer_t startTick = osg::Timer::instance()->tick();
|
|
|
|
|
|
|
|
los.computeIntersections(scene.get());
|
2006-10-31 04:29:06 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
osg::Timer_t endTick = osg::Timer::instance()->tick();
|
2006-10-31 04:29:06 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
|
|
|
|
|
|
|
|
for(unsigned int i=0; i<los.getNumLOS(); i++)
|
|
|
|
{
|
|
|
|
const osgSim::LineOfSight::Intersections& intersections = los.getIntersections(i);
|
|
|
|
for(osgSim::LineOfSight::Intersections::const_iterator itr = intersections.begin();
|
|
|
|
itr != intersections.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
std::cout<<" point "<<*itr<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-31 04:29:06 +08:00
|
|
|
{
|
2006-11-29 00:00:52 +08:00
|
|
|
// now do a second traversal to test performance of cache.
|
|
|
|
osg::Timer_t startTick = osg::Timer::instance()->tick();
|
|
|
|
|
|
|
|
std::cout<<"Computing HeightAboveTerrain"<<std::endl;
|
|
|
|
|
|
|
|
hat.computeIntersections(scene.get());
|
|
|
|
|
|
|
|
osg::Timer_t endTick = osg::Timer::instance()->tick();
|
|
|
|
|
|
|
|
for(unsigned int i=0; i<hat.getNumPoints(); i++)
|
2006-10-31 04:29:06 +08:00
|
|
|
{
|
2006-11-29 00:00:52 +08:00
|
|
|
std::cout<<" point = "<<hat.getPoint(i)<<" hat = "<<hat.getHeightAboveTerrain(i)<<std::endl;
|
2006-10-31 04:29:06 +08:00
|
|
|
}
|
2006-11-29 00:00:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
|
2006-10-31 04:29:06 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
{
|
|
|
|
// now do a second traversal to test performance of cache.
|
|
|
|
osg::Timer_t startTick = osg::Timer::instance()->tick();
|
2006-10-31 04:29:06 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
std::cout<<"Computing ElevationSlice"<<std::endl;
|
|
|
|
osgSim::ElevationSlice es;
|
|
|
|
es.setDatabaseCacheReadCallback(los.getDatabaseCacheReadCallback());
|
2006-10-31 20:59:51 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
es.setStartPoint(bs.center()+osg::Vec3d(bs.radius(),0.0,0.0) );
|
2006-12-13 05:48:59 +08:00
|
|
|
es.setEndPoint(bs.center()+osg::Vec3d(0.0,0.0, bs.radius()) );
|
2006-10-31 20:59:51 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
es.computeIntersections(scene.get());
|
2006-10-31 04:29:06 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
osg::Timer_t endTick = osg::Timer::instance()->tick();
|
2006-10-31 04:29:06 +08:00
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
|
2006-12-04 20:36:13 +08:00
|
|
|
|
|
|
|
typedef osgSim::ElevationSlice::DistanceHeightList DistanceHeightList;
|
|
|
|
const DistanceHeightList& dhl = es.getDistanceHeightIntersections();
|
|
|
|
std::cout<<"Number of intersections ="<<dhl.size()<<std::endl;
|
|
|
|
for(DistanceHeightList::const_iterator dhitr = dhl.begin();
|
|
|
|
dhitr != dhl.end();
|
|
|
|
++dhitr)
|
|
|
|
{
|
2006-12-05 01:31:20 +08:00
|
|
|
std::cout.precision(10);
|
2006-12-04 20:36:13 +08:00
|
|
|
std::cout<<" "<<dhitr->first<<" "<<dhitr->second<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-29 00:00:52 +08:00
|
|
|
}
|
2006-10-31 04:29:06 +08:00
|
|
|
}
|
|
|
|
else if (useIntersectorGroup)
|
2006-10-27 23:11:17 +08:00
|
|
|
{
|
|
|
|
osg::Timer_t startTick = osg::Timer::instance()->tick();
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-27 23:11:17 +08:00
|
|
|
osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
|
|
|
|
osg::Vec3d end = bs.center();// - osg::Vec3d(0.0, bs.radius(),0.0);
|
|
|
|
osg::Vec3d deltaRow( 0.0, 0.0, bs.radius()*0.01);
|
|
|
|
osg::Vec3d deltaColumn( bs.radius()*0.01, 0.0, 0.0);
|
|
|
|
unsigned int numRows = 20;
|
|
|
|
unsigned int numColumns = 20;
|
2006-10-27 00:03:17 +08:00
|
|
|
|
2006-10-27 23:11:17 +08:00
|
|
|
osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
|
2006-10-27 00:03:17 +08:00
|
|
|
|
2006-10-27 23:11:17 +08:00
|
|
|
for(unsigned int r=0; r<numRows; ++r)
|
|
|
|
{
|
|
|
|
for(unsigned int c=0; c<numColumns; ++c)
|
|
|
|
{
|
|
|
|
osg::Vec3d s = start + deltaColumn * double(c) + deltaRow * double(r);
|
|
|
|
osg::Vec3d e = end + deltaColumn * double(c) + deltaRow * double(r);
|
|
|
|
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(s, e);
|
|
|
|
intersectorGroup->addIntersector( intersector.get() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-27 23:11:17 +08:00
|
|
|
osgUtil::IntersectionVisitor intersectVisitor( intersectorGroup.get(), new MyReadCallback );
|
2006-10-31 20:59:51 +08:00
|
|
|
scene->accept(intersectVisitor);
|
2006-10-27 23:11:17 +08:00
|
|
|
|
|
|
|
osg::Timer_t endTick = osg::Timer::instance()->tick();
|
|
|
|
|
|
|
|
std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
|
|
|
|
|
|
|
|
if ( intersectorGroup->containsIntersections() )
|
2006-10-27 00:03:17 +08:00
|
|
|
{
|
2006-10-27 23:11:17 +08:00
|
|
|
std::cout<<"Found intersections "<<std::endl;
|
|
|
|
|
|
|
|
osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
|
|
|
|
for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
|
|
|
|
intersector_itr != intersectors.end();
|
|
|
|
++intersector_itr)
|
|
|
|
{
|
|
|
|
osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get());
|
|
|
|
if (lsi)
|
|
|
|
{
|
|
|
|
osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections();
|
|
|
|
for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
|
|
|
|
itr != intersections.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
|
|
|
|
std::cout<<" ratio "<<intersection.ratio<<std::endl;
|
|
|
|
std::cout<<" point "<<intersection.localIntersectionPoint<<std::endl;
|
|
|
|
std::cout<<" normal "<<intersection.localIntersectionNormal<<std::endl;
|
|
|
|
std::cout<<" indices "<<intersection.indexList.size()<<std::endl;
|
|
|
|
std::cout<<" primitiveIndex "<<intersection.primitiveIndex<<std::endl;
|
|
|
|
std::cout<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-27 00:03:17 +08:00
|
|
|
}
|
2006-10-27 23:11:17 +08:00
|
|
|
|
2006-10-27 00:03:17 +08:00
|
|
|
}
|
2006-10-27 23:11:17 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::Timer_t startTick = osg::Timer::instance()->tick();
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
osg::Vec3d start = bs.center() + osg::Vec3d(0.0,bs.radius(),0.0);
|
|
|
|
osg::Vec3d end = bs.center() - osg::Vec3d(0.0, bs.radius(),0.0);
|
|
|
|
#else
|
|
|
|
osg::Vec3d start = bs.center() + osg::Vec3d(0.0,0.0, bs.radius());
|
|
|
|
osg::Vec3d end = bs.center() - osg::Vec3d(0.0, 0.0, bs.radius());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end);
|
|
|
|
|
|
|
|
osgUtil::IntersectionVisitor intersectVisitor( intersector.get(), new MyReadCallback );
|
|
|
|
|
2006-10-31 20:59:51 +08:00
|
|
|
scene->accept(intersectVisitor);
|
2006-10-27 00:03:17 +08:00
|
|
|
|
2006-10-27 23:11:17 +08:00
|
|
|
osg::Timer_t endTick = osg::Timer::instance()->tick();
|
|
|
|
|
|
|
|
std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
|
|
|
|
|
|
|
|
if ( intersector->containsIntersections() )
|
|
|
|
{
|
|
|
|
osgUtil::LineSegmentIntersector::Intersections& intersections = intersector->getIntersections();
|
|
|
|
for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
|
|
|
|
itr != intersections.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
|
|
|
|
std::cout<<" ratio "<<intersection.ratio<<std::endl;
|
|
|
|
std::cout<<" point "<<intersection.localIntersectionPoint<<std::endl;
|
|
|
|
std::cout<<" normal "<<intersection.localIntersectionNormal<<std::endl;
|
|
|
|
std::cout<<" indices "<<intersection.indexList.size()<<std::endl;
|
|
|
|
std::cout<<" primitiveIndex "<<intersection.primitiveIndex<<std::endl;
|
|
|
|
std::cout<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-22 22:14:53 +08:00
|
|
|
|
2006-10-27 00:03:17 +08:00
|
|
|
return 0;
|
|
|
|
}
|