LineOfSight 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifndef OSGSIM_LINEOFSIGHT
  14. #define OSGSIM_LINEOFSIGHT 1
  15. #include <osgUtil/IntersectionVisitor>
  16. #include <osgSim/Export>
  17. namespace osgSim {
  18. class OSGSIM_EXPORT DatabaseCacheReadCallback : public osgUtil::IntersectionVisitor::ReadCallback
  19. {
  20. public:
  21. DatabaseCacheReadCallback();
  22. void setMaximumNumOfFilesToCache(unsigned int maxNumFilesToCache) { _maxNumFilesToCache = maxNumFilesToCache; }
  23. unsigned int getMaximumNumOfFilesToCache() const { return _maxNumFilesToCache; }
  24. void clearDatabaseCache();
  25. void pruneUnusedDatabaseCache();
  26. virtual osg::ref_ptr<osg::Node> readNodeFile(const std::string& filename);
  27. protected:
  28. typedef std::map<std::string, osg::ref_ptr<osg::Node> > FileNameSceneMap;
  29. unsigned int _maxNumFilesToCache;
  30. OpenThreads::Mutex _mutex;
  31. FileNameSceneMap _filenameSceneMap;
  32. };
  33. /** Helper class for setting up and acquiring line of sight intersections with terrain.
  34. * By default assigns a osgSim::DatabaseCacheReadCallback that enables automatic loading
  35. * of external PagedLOD tiles to ensure that the highest level of detail is used in intersections.
  36. * This automatic loading of tiles is done by the intersection traversal that is done within
  37. * the computeIntersections(..) method, so can result in long intersection times when external
  38. * tiles have to be loaded.
  39. * The external loading of tiles can be disabled by removing the read callback, this is done by
  40. * calling the setDatabaseCacheReadCallback(DatabaseCacheReadCallback*) method with a value of 0.*/
  41. class OSGSIM_EXPORT LineOfSight
  42. {
  43. public :
  44. LineOfSight();
  45. /** Clear the internal LOS List so it contains no line of sight tests.*/
  46. void clear();
  47. /** Add a line of sight test, consisting of start and end point. Returns the index number of the newly adding LOS test.*/
  48. unsigned int addLOS(const osg::Vec3d& start, const osg::Vec3d& end);
  49. /** Get the number of line of sight tests.*/
  50. unsigned int getNumLOS() const { return _LOSList.size(); }
  51. /** Set the start point of single line of sight test.*/
  52. void setStartPoint(unsigned int i, const osg::Vec3d& start) { _LOSList[i]._start = start; }
  53. /** Get the start point of single line of sight test.*/
  54. const osg::Vec3d& getStartPoint(unsigned int i) const { return _LOSList[i]._start; }
  55. /** Set the end point of single line of sight test.*/
  56. void setEndPoint(unsigned int i, const osg::Vec3d& end) { _LOSList[i]._end = end; }
  57. /** Get the end point of single line of sight test.*/
  58. const osg::Vec3d& getEndPoint(unsigned int i) const { return _LOSList[i]._end; }
  59. typedef std::vector<osg::Vec3d> Intersections;
  60. /** Get the intersection points for a single line of sight test.*/
  61. const Intersections& getIntersections(unsigned int i) const { return _LOSList[i]._intersections; }
  62. /** Compute the LOS intersections with the specified scene graph.
  63. * The results are all stored in the form of Intersections list, one per LOS test.*/
  64. void computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask=0xffffffff);
  65. /** Compute the intersection between the specified scene graph and a single LOS start,end pair. Returns an IntersectionList, of all the points intersected.*/
  66. static Intersections computeIntersections(osg::Node* scene, const osg::Vec3d& start, const osg::Vec3d& end, osg::Node::NodeMask traversalMask=0xffffffff);
  67. /** Clear the database cache.*/
  68. void clearDatabaseCache() { if (_dcrc.valid()) _dcrc->clearDatabaseCache(); }
  69. /** Set the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.
  70. * Note, if you have multiple LineOfSight or HeightAboveTerrain objects in use at one time then you should share a single
  71. * DatabaseCacheReadCallback between all of them. */
  72. void setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc);
  73. /** Get the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.*/
  74. DatabaseCacheReadCallback* getDatabaseCacheReadCallback() { return _dcrc.get(); }
  75. protected :
  76. struct LOS
  77. {
  78. LOS(const osg::Vec3d& start, const osg::Vec3d& end):
  79. _start(start),
  80. _end(end) {}
  81. osg::Vec3d _start;
  82. osg::Vec3d _end;
  83. Intersections _intersections;
  84. };
  85. typedef std::vector<LOS> LOSList;
  86. LOSList _LOSList;
  87. osg::ref_ptr<DatabaseCacheReadCallback> _dcrc;
  88. osgUtil::IntersectionVisitor _intersectionVisitor;
  89. };
  90. }
  91. #endif