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