GeographicLocation 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2004 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. #ifndef OSGSIM_GEOGRAPHICLOCATION
  14. #define OSGSIM_GEOGRAPHICLOCATION 1
  15. #include <osg/Math>
  16. #include <osg/Referenced>
  17. #include <ostream>
  18. namespace osgSim {
  19. /** Stores a double precision geographic location, latitude and longitude.
  20. Derived from Referenced so it can be used as an osg::Object userData.
  21. */
  22. class GeographicLocation : public osg::Referenced
  23. {
  24. public:
  25. GeographicLocation() { _v[0]=0.; _v[1]=0.; }
  26. GeographicLocation( double lat, double lon ) { _v[0]=lat; _v[1]=lon; }
  27. inline bool operator == ( const GeographicLocation& v) const { return _v[0]==v._v[0] && _v[1]==v._v[1]; }
  28. inline bool operator != ( const GeographicLocation& v) const { return _v[0]!=v._v[0] || _v[1]!=v._v[1]; }
  29. inline bool operator < ( const GeographicLocation& v) const
  30. {
  31. if (_v[0]<v._v[0]) return true;
  32. else if (_v[0]>v._v[0]) return false;
  33. else if (_v[1]<v._v[1]) return true;
  34. else return false;
  35. }
  36. inline double* ptr() { return _v; }
  37. inline const double* ptr() const { return _v; }
  38. inline void set( double lat, double lon ) { _v[0]=lat; _v[1]=lon; }
  39. inline double& latitude() { return _v[0]; }
  40. inline double& longitude() { return _v[1]; }
  41. inline double latitude() const { return _v[0]; }
  42. inline double longitude() const { return _v[1]; }
  43. inline bool valid() const { return !isNaN(); }
  44. inline bool isNaN() const { return osg::isNaN(_v[0]) || osg::isNaN(_v[1]); }
  45. /// binary vector add
  46. inline const GeographicLocation operator+( const GeographicLocation& rhs) const
  47. {
  48. return GeographicLocation(_v[0]+rhs._v[0], _v[1]+rhs._v[1]);
  49. }
  50. /// binary vector subtract
  51. inline const GeographicLocation operator-( const GeographicLocation& rhs) const
  52. {
  53. return GeographicLocation(_v[0]-rhs._v[0], _v[1]-rhs._v[1]);
  54. }
  55. friend inline std::ostream& operator << (std::ostream& output, const GeographicLocation& loc)
  56. {
  57. output << loc._v[0] << " " << loc._v[1];
  58. return output; // to enable cascading
  59. }
  60. private:
  61. // Note the convention is to store lat in _v[0] and lon in _v[1].
  62. // This is contrary to typical X-Y convention. Who decided lat should come
  63. // before lon anyway? I'd like a word with him...
  64. double _v[2];
  65. }; // end of class GeographicLocation
  66. } // end of namespace osgSim
  67. #endif