HeightAboveTerrain 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_HEIGHTABOVETERRAIN
  14. #define OSGSIM_HEIGHTABOVETERRAIN 1
  15. #include <osgUtil/IntersectionVisitor>
  16. // include so we can get access to the DatabaseCacheReadCallback
  17. #include <osgSim/LineOfSight>
  18. namespace osgSim {
  19. /** Helper class for setting up and acquiring height above terrain intersections with terrain.
  20. * By default assigns a osgSim::DatabaseCacheReadCallback that enables automatic loading
  21. * of external PagedLOD tiles to ensure that the highest level of detail is used in intersections.
  22. * This automatic loading of tiles is done by the intersection traversal that is done within
  23. * the computeIntersections(..) method, so can result in long intersection times when external
  24. * tiles have to be loaded.
  25. * The external loading of tiles can be disabled by removing the read callback, this is done by
  26. * calling the setDatabaseCacheReadCallback(DatabaseCacheReadCallback*) method with a value of 0.*/
  27. class OSGSIM_EXPORT HeightAboveTerrain
  28. {
  29. public :
  30. HeightAboveTerrain();
  31. /** Clear the internal HAT List so it contains no height above terrain tests.*/
  32. void clear();
  33. /** Add a height above terrain test point in the CoordinateFrame.*/
  34. unsigned int addPoint(const osg::Vec3d& point);
  35. /** Get the number of height above terrain tests.*/
  36. unsigned int getNumPoints() const { return _HATList.size(); }
  37. /** Set the source point of single height above terrain test.*/
  38. void setPoint(unsigned int i, const osg::Vec3d& point) { _HATList[i]._point = point; }
  39. /** Get the source point of single height above terrain test.*/
  40. const osg::Vec3d& getPoint(unsigned int i) const { return _HATList[i]._point; }
  41. /** Get the intersection height for a single height above terrain test.
  42. * Note, you must call computeIntersections(..) before you can query the HeightAboveTerrain.
  43. * If no intersections are found then height returned will be the height above mean sea level. */
  44. double getHeightAboveTerrain(unsigned int i) const { return _HATList[i]._hat; }
  45. /** Set the lowest height that the should be tested for.
  46. * Defaults to -1000, i.e. 1000m below mean sea level. */
  47. void setLowestHeight(double lowestHeight) { _lowestHeight = lowestHeight; }
  48. /** Get the lowest height that the should be tested for.*/
  49. double getLowestHeight() const { return _lowestHeight; }
  50. /** Compute the HAT intersections with the specified scene graph.
  51. * The results are all stored in the form of a single height above terrain value per HAT test.
  52. * Note, if the topmost node is a CoordinateSystemNode then the input points are assumed to be geocentric,
  53. * with the up vector defined by the EllipsoidModel attached to the CoordinateSystemNode.
  54. * If the topmost node is not a CoordinateSystemNode then a local coordinates frame is assumed, with a local up vector. */
  55. void computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask=0xffffffff);
  56. /** Compute the vertical distance between the specified scene graph and a single HAT point. */
  57. static double computeHeightAboveTerrain(osg::Node* scene, const osg::Vec3d& point, osg::Node::NodeMask traversalMask=0xffffffff);
  58. /** Clear the database cache.*/
  59. void clearDatabaseCache() { if (_dcrc.valid()) _dcrc->clearDatabaseCache(); }
  60. /** Set the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.
  61. * Note, if you have multiple LineOfSight or HeightAboveTerrain objects in use at one time then you should share a single
  62. * DatabaseCacheReadCallback between all of them. */
  63. void setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc);
  64. /** Get the ReadCallback that does the reading of external PagedLOD models, and caching of loaded subgraphs.*/
  65. DatabaseCacheReadCallback* getDatabaseCacheReadCallback() { return _dcrc.get(); }
  66. protected :
  67. struct HAT
  68. {
  69. HAT(const osg::Vec3d& point):
  70. _point(point),
  71. _hat(0.0) {}
  72. osg::Vec3d _point;
  73. double _hat;
  74. };
  75. typedef std::vector<HAT> HATList;
  76. double _lowestHeight;
  77. HATList _HATList;
  78. osg::ref_ptr<DatabaseCacheReadCallback> _dcrc;
  79. osgUtil::IntersectionVisitor _intersectionVisitor;
  80. };
  81. }
  82. #endif