openscenegraph
LineOfSight
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSGSIM_LINEOFSIGHT
15#define OSGSIM_LINEOFSIGHT 1
16
17#include <osgUtil/IntersectionVisitor>
18
19#include <osgSim/Export>
20
21namespace osgSim {
22
23class OSGSIM_EXPORT DatabaseCacheReadCallback : public osgUtil::IntersectionVisitor::ReadCallback
24{
25 public:
26 DatabaseCacheReadCallback();
27
28 void setMaximumNumOfFilesToCache(unsigned int maxNumFilesToCache) { _maxNumFilesToCache = maxNumFilesToCache; }
29 unsigned int getMaximumNumOfFilesToCache() const { return _maxNumFilesToCache; }
30
31 void clearDatabaseCache();
32
33 void pruneUnusedDatabaseCache();
34
35 virtual osg::ref_ptr<osg::Node> readNodeFile(const std::string& filename);
36
37 protected:
38
39 typedef std::map<std::string, osg::ref_ptr<osg::Node> > FileNameSceneMap;
40
41 unsigned int _maxNumFilesToCache;
42 OpenThreads::Mutex _mutex;
43 FileNameSceneMap _filenameSceneMap;
44};
45
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
55{
56 public :
57
58 LineOfSight();
59
60 /** Clear the internal LOS List so it contains no line of sight tests.*/
61 void clear();
62
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);
65
66 /** Get the number of line of sight tests.*/
67 unsigned int getNumLOS() const { return _LOSList.size(); }
68
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; }
71
72 /** Get the start point of single line of sight test.*/
73 const osg::Vec3d& getStartPoint(unsigned int i) const { return _LOSList[i]._start; }
74
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; }
77
78 /** Get the end point of single line of sight test.*/
79 const osg::Vec3d& getEndPoint(unsigned int i) const { return _LOSList[i]._end; }
80
81 typedef std::vector<osg::Vec3d> Intersections;
82
83 /** Get the intersection points for a single line of sight test.*/
84 const Intersections& getIntersections(unsigned int i) const { return _LOSList[i]._intersections; }
85
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);
89
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);
92
93
94 /** Clear the database cache.*/
95 void clearDatabaseCache() { if (_dcrc.valid()) _dcrc->clearDatabaseCache(); }
96
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);
101
102 /** Get the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.*/
103 DatabaseCacheReadCallback* getDatabaseCacheReadCallback() { return _dcrc.get(); }
104
105 protected :
106
107 struct LOS
108 {
109 LOS(const osg::Vec3d& start, const osg::Vec3d& end):
110 _start(start),
111 _end(end) {}
112
113
114 osg::Vec3d _start;
115 osg::Vec3d _end;
116 Intersections _intersections;
117 };
118
119 typedef std::vector<LOS> LOSList;
120 LOSList _LOSList;
121
122 osg::ref_ptr<DatabaseCacheReadCallback> _dcrc;
123 osgUtil::IntersectionVisitor _intersectionVisitor;
124
125};
126
127}
128
129#endif