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