2012-03-22 01:36:20 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2006-10-31 16:41:24 +08:00
*
2012-03-22 01:36:20 +08:00
* 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
2006-10-31 16:41:24 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2012-03-22 01:36:20 +08:00
*
2006-10-31 16:41:24 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-03-22 01:36:20 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2006-10-31 16:41:24 +08:00
* 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();
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
void setMaximumNumOfFilesToCache(unsigned int maxNumFilesToCache) { _maxNumFilesToCache = maxNumFilesToCache; }
unsigned int getMaximumNumOfFilesToCache() const { return _maxNumFilesToCache; }
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
void clearDatabaseCache();
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
void pruneUnusedDatabaseCache();
2015-10-22 21:42:19 +08:00
virtual osg::ref_ptr<osg::Node> readNodeFile(const std::string& filename);
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
protected:
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
typedef std::map<std::string, osg::ref_ptr<osg::Node> > FileNameSceneMap;
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
unsigned int _maxNumFilesToCache;
OpenThreads::Mutex _mutex;
FileNameSceneMap _filenameSceneMap;
};
2007-12-11 01:30:18 +08:00
/** Helper class for setting up and acquiring line of sight intersections with terrain.
2012-03-22 01:36:20 +08:00
* By default assigns a osgSim::DatabaseCacheReadCallback that enables automatic loading
2011-07-18 20:41:22 +08:00
* of external PagedLOD tiles to ensure that the highest level of detail is used in intersections.
* This automatic loading of tiles is done by the intersection traversal that is done within
* the computeIntersections(..) method, so can result in long intersection times when external
* tiles have to be loaded.
* The external loading of tiles can be disabled by removing the read callback, this is done by
* calling the setDatabaseCacheReadCallback(DatabaseCacheReadCallback*) method with a value of 0.*/
2006-10-31 16:41:24 +08:00
class OSGSIM_EXPORT LineOfSight
{
public :
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
LineOfSight();
/** Clear the internal LOS List so it contains no line of sight tests.*/
2006-10-31 20:59:51 +08:00
void clear();
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
/** 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);
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
/** Get the number of line of sight tests.*/
unsigned int getNumLOS() const { return _LOSList.size(); }
2012-03-22 01:36:20 +08:00
2007-12-11 01:30:18 +08:00
/** Set the start point of single line of sight test.*/
2006-10-31 16:41:24 +08:00
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; }
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
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.
2006-10-31 20:59:51 +08:00
* The results are all stored in the form of Intersections list, one per LOS test.*/
2006-12-20 00:45:09 +08:00
void computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask=0xffffffff);
2006-10-31 16:41:24 +08:00
/** Compute the intersection between the specified scene graph and a single LOS start,end pair. Returns an IntersectionList, of all the points intersected.*/
2006-12-20 00:45:09 +08:00
static Intersections computeIntersections(osg::Node* scene, const osg::Vec3d& start, const osg::Vec3d& end, osg::Node::NodeMask traversalMask=0xffffffff);
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
/** 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.
2007-12-11 01:30:18 +08:00
* Note, if you have multiple LineOfSight or HeightAboveTerrain objects in use at one time then you should share a single
2006-10-31 16:41:24 +08:00
* 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(); }
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
protected :
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
struct LOS
{
LOS(const osg::Vec3d& start, const osg::Vec3d& end):
_start(start),
_end(end) {}
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
osg::Vec3d _start;
osg::Vec3d _end;
Intersections _intersections;
};
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
typedef std::vector<LOS> LOSList;
LOSList _LOSList;
2012-03-22 01:36:20 +08:00
2006-10-31 16:41:24 +08:00
osg::ref_ptr<DatabaseCacheReadCallback> _dcrc;
osgUtil::IntersectionVisitor _intersectionVisitor;
};
}
#endif