root/OpenSceneGraph/trunk/src/osgTerrain/Locator.cpp @ 13041

Revision 13041, 4.5 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
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 <osgTerrain/Locator>
15#include <osg/Notify>
16
17#include <list>
18
19using namespace osgTerrain;
20
21//////////////////////////////////////////////////////////////////////////////
22//
23// Locator
24//
25Locator::Locator():
26    _coordinateSystemType(PROJECTED),
27    _ellipsoidModel(new osg::EllipsoidModel()),
28    _definedInFile(false),
29    _transformScaledByResolution(false)
30{
31}
32
33Locator::Locator(const Locator& locator,const osg::CopyOp& copyop):
34    osg::Object(locator,copyop),
35    _coordinateSystemType(locator._coordinateSystemType),
36    _format(locator._format),
37    _cs(locator._cs),
38    _ellipsoidModel(locator._ellipsoidModel),
39    _transform(locator._transform),
40    _definedInFile(locator._definedInFile),
41    _transformScaledByResolution(locator._transformScaledByResolution)
42{
43}
44
45Locator::~Locator()
46{
47}
48
49void Locator::setTransformAsExtents(double minX, double minY, double maxX, double maxY)
50{
51    _transform.set(maxX-minX, 0.0,       0.0, 0.0,
52                   0.0,       maxY-minY, 0.0, 0.0,
53                   0.0,       0.0,       1.0, 0.0,
54                   minX,      minY,      0.0, 1.0);
55
56    _inverse.invert(_transform);
57}
58
59bool Locator::computeLocalBounds(Locator& source, osg::Vec3d& bottomLeft, osg::Vec3d& topRight) const
60{
61    typedef std::list<osg::Vec3d> Corners;
62    Corners corners;
63
64    osg::Vec3d cornerNDC;
65    if (Locator::convertLocalCoordBetween(source, osg::Vec3d(0.0,0.0,0.0), *this, cornerNDC))
66    {
67        corners.push_back(cornerNDC);
68    }
69
70    if (Locator::convertLocalCoordBetween(source, osg::Vec3d(1.0,0.0,0.0), *this, cornerNDC))
71    {
72        corners.push_back(cornerNDC);
73    }
74
75    if (Locator::convertLocalCoordBetween(source, osg::Vec3d(0.0,1.0,0.0), *this, cornerNDC))
76    {
77        corners.push_back(cornerNDC);
78    }
79
80    if (Locator::convertLocalCoordBetween(source, osg::Vec3d(1.0,1.0,0.0), *this, cornerNDC))
81    {
82        corners.push_back(cornerNDC);
83    }
84
85    if (corners.empty()) return false;
86
87
88    Corners::iterator itr = corners.begin();
89
90    bottomLeft.x() = topRight.x() = itr->x();
91    bottomLeft.y() = topRight.y() = itr->y();
92
93    ++itr;
94
95    for(;
96        itr != corners.end();
97        ++itr)
98    {
99        bottomLeft.x() = osg::minimum( bottomLeft.x(), itr->x());
100        bottomLeft.y() = osg::minimum( bottomLeft.y(), itr->y());
101        topRight.x() = osg::maximum( topRight.x(), itr->x());
102        topRight.y() = osg::maximum( topRight.y(), itr->y());
103    }
104
105    return true;
106}
107
108bool Locator::orientationOpenGL() const
109{
110    return _transform(0,0) * _transform(1,1) >= 0.0;
111}
112
113bool Locator::convertLocalToModel(const osg::Vec3d& local, osg::Vec3d& world) const
114{
115    switch(_coordinateSystemType)
116    {
117        case(GEOCENTRIC):
118        {
119            osg::Vec3d geographic = local * _transform;
120
121            _ellipsoidModel->convertLatLongHeightToXYZ(geographic.y(), geographic.x(), geographic.z(),
122                                                       world.x(), world.y(), world.z());
123            return true;
124        }
125        case(GEOGRAPHIC):
126        {
127            world = local * _transform;
128            return true;
129        }
130        case(PROJECTED):
131        {
132            world = local * _transform;
133            return true;
134        }
135    }
136
137    return false;
138}
139
140bool Locator::convertModelToLocal(const osg::Vec3d& world, osg::Vec3d& local) const
141{
142    switch(_coordinateSystemType)
143    {
144        case(GEOCENTRIC):
145        {
146            double longitude, latitude, height;
147
148            _ellipsoidModel->convertXYZToLatLongHeight(world.x(), world.y(), world.z(),
149                                                       latitude, longitude, height );
150
151            local = osg::Vec3d(longitude, latitude, height) * _inverse;
152
153            return true;
154        }
155        case(GEOGRAPHIC):
156        {
157            local = world * _inverse;
158
159            return true;
160        }
161        case(PROJECTED):
162        {
163            local = world * _inverse;
164            return true;
165        }
166    }
167
168    return false;
169}
Note: See TracBrowser for help on using the browser.