| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgTerrain/Locator> |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | |
|---|
| 17 | #include <list> |
|---|
| 18 | |
|---|
| 19 | using namespace osgTerrain; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | Locator::Locator(): |
|---|
| 26 | _coordinateSystemType(PROJECTED), |
|---|
| 27 | _ellipsoidModel(new osg::EllipsoidModel()), |
|---|
| 28 | _definedInFile(false), |
|---|
| 29 | _transformScaledByResolution(false) |
|---|
| 30 | { |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | Locator::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 | |
|---|
| 45 | Locator::~Locator() |
|---|
| 46 | { |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | void 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 | |
|---|
| 59 | bool 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 | |
|---|
| 108 | bool Locator::orientationOpenGL() const |
|---|
| 109 | { |
|---|
| 110 | return _transform(0,0) * _transform(1,1) >= 0.0; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | bool 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 | |
|---|
| 140 | bool 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 | } |
|---|