root/OpenSceneGraph/trunk/src/osgSim/HeightAboveTerrain.cpp @ 13041

Revision 13041, 4.4 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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#include <osg/CoordinateSystemNode>
15#include <osgSim/HeightAboveTerrain>
16
17#include <osg/Notify>
18#include <osgUtil/LineSegmentIntersector>
19
20using namespace osgSim;
21
22HeightAboveTerrain::HeightAboveTerrain()
23{
24    _lowestHeight = -1000.0;
25
26    setDatabaseCacheReadCallback(new DatabaseCacheReadCallback);
27}
28
29void HeightAboveTerrain::clear()
30{
31    _HATList.clear();
32}
33
34unsigned 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
41void 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
112double 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
120void HeightAboveTerrain::setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc)
121{
122    _dcrc = dcrc;
123    _intersectionVisitor.setReadCallback(dcrc);
124}
Note: See TracBrowser for help on using the browser.