| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/CoordinateSystemNode> |
|---|
| 15 | #include <osgSim/HeightAboveTerrain> |
|---|
| 16 | |
|---|
| 17 | #include <osg/Notify> |
|---|
| 18 | #include <osgUtil/LineSegmentIntersector> |
|---|
| 19 | |
|---|
| 20 | using namespace osgSim; |
|---|
| 21 | |
|---|
| 22 | HeightAboveTerrain::HeightAboveTerrain() |
|---|
| 23 | { |
|---|
| 24 | _lowestHeight = -1000.0; |
|---|
| 25 | |
|---|
| 26 | setDatabaseCacheReadCallback(new DatabaseCacheReadCallback); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | void HeightAboveTerrain::clear() |
|---|
| 30 | { |
|---|
| 31 | _HATList.clear(); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | unsigned int HeightAboveTerrain::addPoint(const osg::Vec3d& point) |
|---|
| 35 | { |
|---|
| 36 | unsigned int index = _HATList.size(); |
|---|
| 37 | _HATList.push_back(HAT(point)); |
|---|
| 38 | return index; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | void HeightAboveTerrain::computeIntersections(osg::Node* scene, osg::Node::NodeMask traversalMask) |
|---|
| 42 | { |
|---|
| 43 | osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(scene); |
|---|
| 44 | osg::EllipsoidModel* em = csn ? csn->getEllipsoidModel() : 0; |
|---|
| 45 | |
|---|
| 46 | osg::ref_ptr<osgUtil::IntersectorGroup> intersectorGroup = new osgUtil::IntersectorGroup(); |
|---|
| 47 | |
|---|
| 48 | for(HATList::iterator itr = _HATList.begin(); |
|---|
| 49 | itr != _HATList.end(); |
|---|
| 50 | ++itr) |
|---|
| 51 | { |
|---|
| 52 | if (em) |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | osg::Vec3d start = itr->_point; |
|---|
| 56 | osg::Vec3d upVector = em->computeLocalUpVector(start.x(), start.y(), start.z()); |
|---|
| 57 | |
|---|
| 58 | double latitude, longitude, height; |
|---|
| 59 | em->convertXYZToLatLongHeight(start.x(), start.y(), start.z(), latitude, longitude, height); |
|---|
| 60 | osg::Vec3d end = start - upVector * (height - _lowestHeight); |
|---|
| 61 | |
|---|
| 62 | itr->_hat = height; |
|---|
| 63 | |
|---|
| 64 | OSG_NOTICE<<"lat = "<<latitude<<" longitude = "<<longitude<<" height = "<<height<<std::endl; |
|---|
| 65 | |
|---|
| 66 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector(start, end); |
|---|
| 67 | intersectorGroup->addIntersector( intersector.get() ); |
|---|
| 68 | } |
|---|
| 69 | else |
|---|
| 70 | { |
|---|
| 71 | osg::Vec3d start = itr->_point; |
|---|
| 72 | osg::Vec3d upVector (0.0, 0.0, 1.0); |
|---|
| 73 | |
|---|
| 74 | double height = start.z(); |
|---|
| 75 | osg::Vec3d end = start - upVector * (height - _lowestHeight); |
|---|
| 76 | |
|---|
| 77 | itr->_hat = height; |
|---|
| 78 | |
|---|
| 79 | osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector = new osgUtil::LineSegmentIntersector( start, end); |
|---|
| 80 | intersectorGroup->addIntersector( intersector.get() ); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | _intersectionVisitor.reset(); |
|---|
| 85 | _intersectionVisitor.setTraversalMask(traversalMask); |
|---|
| 86 | _intersectionVisitor.setIntersector( intersectorGroup.get() ); |
|---|
| 87 | |
|---|
| 88 | scene->accept(_intersectionVisitor); |
|---|
| 89 | |
|---|
| 90 | unsigned int index = 0; |
|---|
| 91 | osgUtil::IntersectorGroup::Intersectors& intersectors = intersectorGroup->getIntersectors(); |
|---|
| 92 | for(osgUtil::IntersectorGroup::Intersectors::iterator intersector_itr = intersectors.begin(); |
|---|
| 93 | intersector_itr != intersectors.end(); |
|---|
| 94 | ++intersector_itr, ++index) |
|---|
| 95 | { |
|---|
| 96 | osgUtil::LineSegmentIntersector* lsi = dynamic_cast<osgUtil::LineSegmentIntersector*>(intersector_itr->get()); |
|---|
| 97 | if (lsi) |
|---|
| 98 | { |
|---|
| 99 | osgUtil::LineSegmentIntersector::Intersections& intersections = lsi->getIntersections(); |
|---|
| 100 | if (!intersections.empty()) |
|---|
| 101 | { |
|---|
| 102 | const osgUtil::LineSegmentIntersector::Intersection& intersection = *intersections.begin(); |
|---|
| 103 | osg::Vec3d intersectionPoint = intersection.matrix.valid() ? intersection.localIntersectionPoint * (*intersection.matrix) : |
|---|
| 104 | intersection.localIntersectionPoint; |
|---|
| 105 | _HATList[index]._hat = (_HATList[index]._point - intersectionPoint).length(); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | double HeightAboveTerrain::computeHeightAboveTerrain(osg::Node* scene, const osg::Vec3d& point, osg::Node::NodeMask traversalMask) |
|---|
| 113 | { |
|---|
| 114 | HeightAboveTerrain hat; |
|---|
| 115 | unsigned int index = hat.addPoint(point); |
|---|
| 116 | hat.computeIntersections(scene, traversalMask); |
|---|
| 117 | return hat.getHeightAboveTerrain(index); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void HeightAboveTerrain::setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc) |
|---|
| 121 | { |
|---|
| 122 | _dcrc = dcrc; |
|---|
| 123 | _intersectionVisitor.setReadCallback(dcrc); |
|---|
| 124 | } |
|---|