1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGSIM_LINEOFSIGHT
15#define OSGSIM_LINEOFSIGHT 1
17#include <osgUtil/IntersectionVisitor>
19#include <osgSim/Export>
23class OSGSIM_EXPORT DatabaseCacheReadCallback : public osgUtil::IntersectionVisitor::ReadCallback
26 DatabaseCacheReadCallback();
28 void setMaximumNumOfFilesToCache(unsigned int maxNumFilesToCache) { _maxNumFilesToCache = maxNumFilesToCache; }
29 unsigned int getMaximumNumOfFilesToCache() const { return _maxNumFilesToCache; }
31 void clearDatabaseCache();
33 void pruneUnusedDatabaseCache();
35 virtual osg::ref_ptr<osg::Node> readNodeFile(const std::string& filename);
39 typedef std::map<std::string, osg::ref_ptr<osg::Node> > FileNameSceneMap;
41 unsigned int _maxNumFilesToCache;
42 OpenThreads::Mutex _mutex;
43 FileNameSceneMap _filenameSceneMap;
46/** Helper class for setting up and acquiring line of sight intersections with terrain.
47 * By default assigns a osgSim::DatabaseCacheReadCallback that enables automatic loading
48 * of external PagedLOD tiles to ensure that the highest level of detail is used in intersections.
49 * This automatic loading of tiles is done by the intersection traversal that is done within
50 * the computeIntersections(..) method, so can result in long intersection times when external
51 * tiles have to be loaded.
52 * The external loading of tiles can be disabled by removing the read callback, this is done by
53 * calling the setDatabaseCacheReadCallback(DatabaseCacheReadCallback*) method with a value of 0.*/
54class OSGSIM_EXPORT LineOfSight
60 /** Clear the internal LOS List so it contains no line of sight tests.*/
63 /** Add a line of sight test, consisting of start and end point. Returns the index number of the newly adding LOS test.*/
64 unsigned int addLOS(const osg::Vec3d& start, const osg::Vec3d& end);
66 /** Get the number of line of sight tests.*/
67 unsigned int getNumLOS() const { return _LOSList.size(); }
69 /** Set the start point of single line of sight test.*/
70 void setStartPoint(unsigned int i, const osg::Vec3d& start) { _LOSList[i]._start = start; }
72 /** Get the start point of single line of sight test.*/
73 const osg::Vec3d& getStartPoint(unsigned int i) const { return _LOSList[i]._start; }
75 /** Set the end point of single line of sight test.*/
76 void setEndPoint(unsigned int i, const osg::Vec3d& end) { _LOSList[i]._end = end; }
78 /** Get the end point of single line of sight test.*/
79 const osg::Vec3d& getEndPoint(unsigned int i) const { return _LOSList[i]._end; }
81 typedef std::vector<osg::Vec3d> Intersections;
83 /** Get the intersection points for a single line of sight test.*/
84 const Intersections& getIntersections(unsigned int i) const { return _LOSList[i]._intersections; }
86 /** Compute the LOS intersections with the specified scene graph.
87 * The results are all stored in the form of Intersections list, one per LOS test.*/
88 void computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask=0xffffffff);
90 /** Compute the intersection between the specified scene graph and a single LOS start,end pair. Returns an IntersectionList, of all the points intersected.*/
91 static Intersections computeIntersections(osg::Node* scene, const osg::Vec3d& start, const osg::Vec3d& end, osg::Node::NodeMask traversalMask=0xffffffff);
94 /** Clear the database cache.*/
95 void clearDatabaseCache() { if (_dcrc.valid()) _dcrc->clearDatabaseCache(); }
97 /** Set the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.
98 * Note, if you have multiple LineOfSight or HeightAboveTerrain objects in use at one time then you should share a single
99 * DatabaseCacheReadCallback between all of them. */
100 void setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc);
102 /** Get the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.*/
103 DatabaseCacheReadCallback* getDatabaseCacheReadCallback() { return _dcrc.get(); }
109 LOS(const osg::Vec3d& start, const osg::Vec3d& end):
116 Intersections _intersections;
119 typedef std::vector<LOS> LOSList;
122 osg::ref_ptr<DatabaseCacheReadCallback> _dcrc;
123 osgUtil::IntersectionVisitor _intersectionVisitor;