Added LineOfSight and HeightAboveTerrain classes

This commit is contained in:
Robert Osfield 2006-10-31 08:41:24 +00:00
parent 16c238fc60
commit e9b501002c
4 changed files with 339 additions and 0 deletions

View File

@ -0,0 +1,34 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGSIM_HEIGHTABOVETERRAIN
#define OSGSIM_HEIGHTABOVETERRAIN 1
#include <osgUtil/IntersectionVisitor>
#include <osgSim/Export>
namespace osgSim {
/** Helper class for setting up and aquiring height above terrain intersections with terrain.*/
class OSGSIM_EXPORT HeightAboveTerrain
{
public :
protected :
};
}
#endif

127
include/osgSim/LineOfSight Normal file
View File

@ -0,0 +1,127 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGSIM_LINEOFSIGHT
#define OSGSIM_LINEOFSIGHT 1
#include <osgUtil/IntersectionVisitor>
#include <osgSim/Export>
namespace osgSim {
class OSGSIM_EXPORT DatabaseCacheReadCallback : public osgUtil::IntersectionVisitor::ReadCallback
{
public:
DatabaseCacheReadCallback();
void setMaximumNumOfFilesToCache(unsigned int maxNumFilesToCache) { _maxNumFilesToCache = maxNumFilesToCache; }
unsigned int getMaximumNumOfFilesToCache() const { return _maxNumFilesToCache; }
void clearDatabaseCache();
void pruneUnusedDatabaseCache();
virtual osg::Node* readNodeFile(const std::string& filename);
protected:
typedef std::map<std::string, osg::ref_ptr<osg::Node> > FileNameSceneMap;
unsigned int _maxNumFilesToCache;
OpenThreads::Mutex _mutex;
FileNameSceneMap _filenameSceneMap;
};
/** Helper class for setting up and aquiring line of sight intersections with terrain.
* Supports automatic paging in of PagedLOD tiles. */
class OSGSIM_EXPORT LineOfSight
{
public :
LineOfSight();
/** Clear the internal LOS List so it contains no line of sight tests.*/
void clearLOS();
/** Add a line of sight test, consisting of start and end point. Returns the index number of the newly adding LOS test.*/
unsigned int addLOS(const osg::Vec3d& start, const osg::Vec3d& end);
/** Get the number of line of sight tests.*/
unsigned int getNumLOS() const { return _LOSList.size(); }
/** Set the start point of signel line of sight test.*/
void setStartPoint(unsigned int i, const osg::Vec3d& start) { _LOSList[i]._start = start; }
/** Get the start point of single line of sight test.*/
const osg::Vec3d& getStartPoint(unsigned int i) const { return _LOSList[i]._start; }
/** Set the end point of single line of sight test.*/
void setEndPoint(unsigned int i, const osg::Vec3d& end) { _LOSList[i]._end = end; }
/** Get the end point of single line of sight test.*/
const osg::Vec3d& getEndPoint(unsigned int i) const { return _LOSList[i]._end; }
typedef std::vector<osg::Vec3d> Intersections;
/** Get the intersection points for a single line of sight test.*/
const Intersections& getIntersections(unsigned int i) const { return _LOSList[i]._intersections; }
/** Compute the LOS intersections with the specified scene graph.
* The results are all stored in the form of IntersectionList, one per LOS test.*/
void computeIntersections(osg::Node* scene);
/** Compute the intersection between the specified scene graph and a single LOS start,end pair. Returns an IntersectionList, of all the points intersected.*/
static Intersections computeIntersections(osg::Node* scene, const osg::Vec3d& start, const osg::Vec3d& end);
/** Clear the database cache.*/
void clearDatabaseCache() { if (_dcrc.valid()) _dcrc->clearDatabaseCache(); }
/** Set the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.
* Note, if you have mulitple LineOfSight or HeightAboveTerrain objects in use at one time then you should share a single
* DatabaseCacheReadCallback between all of them. */
void setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc);
/** Get the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.*/
DatabaseCacheReadCallback* getDatabaseCacheReadCallback() { return _dcrc.get(); }
/** Get the IntersectionVistor that does the intersection traversal over the scene.
* Note, if you want to customized the traversal then you can use the IntersectionVisitor's method to alter its behavior. */
osgUtil::IntersectionVisitor& getIntersectionVisitor() { return _intersectionVisitor; }
protected :
struct LOS
{
LOS(const osg::Vec3d& start, const osg::Vec3d& end):
_start(start),
_end(end) {}
osg::Vec3d _start;
osg::Vec3d _end;
Intersections _intersections;
};
typedef std::vector<LOS> LOSList;
LOSList _LOSList;
osg::ref_ptr<DatabaseCacheReadCallback> _dcrc;
osgUtil::IntersectionVisitor _intersectionVisitor;
};
}
#endif

View File

@ -0,0 +1,16 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgSim/HeightAboveTerrain>
using namespace osgSim;

162
src/osgSim/LineOfSight.cpp Normal file
View File

@ -0,0 +1,162 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osgSim/LineOfSight>
#include <osg/Notify>
#include <osgDB/ReadFile>
using namespace osgSim;
DatabaseCacheReadCallback::DatabaseCacheReadCallback()
{
_maxNumFilesToCache = 2000;
}
void DatabaseCacheReadCallback::clearDatabaseCache()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_filenameSceneMap.clear();
}
void DatabaseCacheReadCallback::pruneUnusedDatabaseCache()
{
}
osg::Node* DatabaseCacheReadCallback::readNodeFile(const std::string& filename)
{
// first check to see if file is already loaded.
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
FileNameSceneMap::iterator itr = _filenameSceneMap.find(filename);
if (itr != _filenameSceneMap.end())
{
osg::notify(osg::NOTICE)<<"Getting from cache "<<filename<<std::endl;
return itr->second.get();
}
}
// now load the file.
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(filename);
// insert into the cache.
if (node.valid())
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (_filenameSceneMap.size() < _maxNumFilesToCache)
{
osg::notify(osg::NOTICE)<<"Inserting into cache "<<filename<<std::endl;
_filenameSceneMap[filename] = node;
}
else
{
// for time being implement a crude search for a candidate to chuck out from the cache.
for(FileNameSceneMap::iterator itr = _filenameSceneMap.begin();
itr != _filenameSceneMap.end();
++itr)
{
if (itr->second->referenceCount()==1)
{
osg::notify(osg::NOTICE)<<"Erasing "<<itr->first<<std::endl;
// found a node which is only referenced in the cache so we can disgard it
// and know that the actual memory will be released.
_filenameSceneMap.erase(itr);
break;
}
}
osg::notify(osg::NOTICE)<<"And the replacing with "<<filename<<std::endl;
_filenameSceneMap[filename] = node;
}
}
return node.release();
}
LineOfSight::LineOfSight()
{
setDatabaseCacheReadCallback(new DatabaseCacheReadCallback);
}
void LineOfSight::clearLOS()
{
_LOSList.clear();
}
unsigned int LineOfSight::addLOS(const osg::Vec3d& start, const osg::Vec3d& end)
{
unsigned int index = _LOSList.size();
_LOSList.push_back(LOS(start,end));
return index;
}
void LineOfSight::computeIntersections(osg::Node* scene)
{
osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup();
for(LOSList::iterator itr = _LOSList.begin();
itr != _LOSList.end();
++itr)
{
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(itr->_start, itr->_end);
intersectorGroup->addIntersector( intersector.get() );
}
_intersectionVisitor.reset();
_intersectionVisitor.setIntersector( intersectorGroup.get() );
scene->accept(_intersectionVisitor);
unsigned int index = 0;
osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors();
for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin();
intersector_itr != intersectors.end();
++intersector_itr, ++index)
{
osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get());
if (lsi)
{
Intersections& intersectionsLOS = _LOSList[index]._intersections;
_LOSList[index]._intersections.clear();
osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections();
for(osgUtil::LineSegmentIntersector::Intersections::iterator itr = intersections.begin();
itr != intersections.end();
++itr)
{
const osgUtil::LineSegmentIntersector::Intersection& intersection = *itr;
intersectionsLOS.push_back( intersection.localIntersectionPoint * (*intersection.matrix) );
}
}
}
}
LineOfSight::Intersections LineOfSight::computeIntersections(osg::Node* scene, const osg::Vec3d& start, const osg::Vec3d& end)
{
LineOfSight los;
unsigned int index = los.addLOS(start,end);
los.computeIntersections(scene);
return los.getIntersections(index);
}
void LineOfSight::setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc)
{
_dcrc = dcrc;
_intersectionVisitor.setReadCallback(dcrc);
}