]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_geodesy.hxx
Small cleanups to the SGGeo[dc] classes, provide more hooks to use them directly
[simgear.git] / simgear / math / sg_geodesy.hxx
1 #ifndef _SG_GEODESY_HXX
2 #define _SG_GEODESY_HXX
3
4 #include <simgear/math/point3d.hxx>
5 #include "SGMath.hxx"
6
7 /** 
8  * Convert from geocentric coordinates to geodetic coordinates
9  * @param lat_geoc (in) Geocentric latitude, radians, + = North
10  * @param radius (in) C.G. radius to earth center (meters)
11  * @param lat_geod (out) Geodetic latitude, radians, + = North
12  * @param alt (out) C.G. altitude above mean sea level (meters)
13  * @param sea_level_r (out) radius from earth center to sea level at
14  *        local vertical (surface normal) of C.G. (meters)
15  */
16 inline void sgGeocToGeod(double lat_geoc, double radius,
17                          double *lat_geod, double *alt, double *sea_level_r)
18 {
19   SGVec3<double> cart;
20   SGGeodesy::SGGeocToCart(SGGeoc::fromRadM(0, lat_geoc, radius), cart);
21   SGGeod geod;
22   SGGeodesy::SGCartToGeod(cart, geod);
23   *lat_geod = geod.getLatitudeRad();
24   *alt = geod.getElevationM();
25   *sea_level_r = SGGeodesy::SGGeodToSeaLevelRadius(geod);
26 }
27
28 /**
29  * Convert from geodetic coordinates to geocentric coordinates.
30  * WARNING: this function is non-reversible.  Due to the fact that
31  * "up" is a different direction for geocentric and geodetic frames,
32  * you can not simply add your "alt" parameter to the "sl_radius"
33  * result and get back (via sgGeodToGeoc()) to the coordinates you
34  * started with.  The error under normal conditions will be of
35  * centimeter order; whether that is important or not is application
36  * dependent. Consider using sgGeodToCart() instead.
37  *
38  * @param lat_geod (in) Geodetic latitude, radians, + = North
39  * @param alt (in) C.G. altitude above mean sea level (meters)
40  * @param sl_radius (out) SEA LEVEL radius to earth center (meters)
41  * @param lat_geoc (out) Geocentric latitude, radians, + = North
42  */
43 inline void sgGeodToGeoc(double lat_geod, double alt,
44                          double *sl_radius, double *lat_geoc)
45 {
46   SGVec3<double> cart;
47   SGGeod geod = SGGeod::fromRadM(0, lat_geod, alt);
48   SGGeodesy::SGGeodToCart(geod, cart);
49   SGGeoc geoc;
50   SGGeodesy::SGCartToGeoc(cart, geoc);
51   *lat_geoc = geoc.getLatitudeRad();
52   *sl_radius = SGGeodesy::SGGeodToSeaLevelRadius(geod);
53 }
54
55
56 /**
57  * Convert a cartesian point to a geodetic lat/lon/altitude.
58  *
59  * @param xyz (in) Pointer to cartesian point.
60  * @param lat (out) Latitude, in radians
61  * @param lon (out) Longitude, in radians
62  * @param alt (out) Altitude, in meters above the WGS84 ellipsoid
63  */
64 inline void sgCartToGeod(const double* xyz, double* lat, double* lon, double* alt)
65 {
66   SGGeod geod;
67   SGGeodesy::SGCartToGeod(SGVec3<double>(xyz), geod);
68   *lat = geod.getLatitudeRad();
69   *lon = geod.getLongitudeRad();
70   *alt = geod.getElevationM();
71 }
72
73 /**
74  * Convert a cartesian point to a geodetic lat/lon/altitude.
75  * Alternate form using Point3D objects.
76  *
77  * @param cartesian point
78  * @return geodetic point
79  */
80 inline Point3D sgCartToGeod(const Point3D& p)
81 {
82   SGGeod geod;
83   SGGeodesy::SGCartToGeod(SGVec3<double>(p.x(), p.y(), p.z()), geod);
84   return Point3D::fromSGGeod(geod);
85 }
86
87
88 /**
89  * Convert a geodetic lat/lon/altitude to a cartesian point.
90  *
91  * @param lat (in) Latitude, in radians
92  * @param lon (in) Longitude, in radians
93  * @param alt (in) Altitude, in meters above the WGS84 ellipsoid
94  * @param xyz (out) Pointer to cartesian point.
95  */
96 inline void sgGeodToCart(double lat, double lon, double alt, double* xyz)
97 {
98   SGVec3<double> cart;
99   SGGeodesy::SGGeodToCart(SGGeod::fromRadM(lon, lat, alt), cart);
100   xyz[0] = cart(0);
101   xyz[1] = cart(1);
102   xyz[2] = cart(2);
103 }
104
105 /**
106  * Convert a geodetic lat/lon/altitude to a cartesian point.
107  * Alternate form using Point3D objects.
108  *
109  * @param geodetic point
110  * @return cartesian point
111  */
112 inline Point3D sgGeodToCart(const Point3D& geod)
113 {
114   SGVec3<double> cart;
115   SGGeodesy::SGGeodToCart(SGGeod::fromRadM(geod.lon(), geod.lat(), geod.elev()), cart);
116   return Point3D::fromSGVec3(cart);
117 }
118
119 /**
120  * Given a starting position and an offset radial and distance,
121  * calculate an ending positon on a wgs84 ellipsoid.
122  * @param alt (in) meters (unused)
123  * @param lat1 (in) degrees
124  * @param lon1 (in) degrees
125  * @param az1 (in) degrees
126  * @param s (in) distance in meters
127  * @param lat2 (out) degrees
128  * @param lon2 (out) degrees
129  * @param az2 (out) return course in degrees
130  */
131 int geo_direct_wgs_84 ( double lat1, double lon1, double az1, 
132                         double s, double *lat2, double *lon2,
133                         double *az2 );
134 inline int geo_direct_wgs_84 ( double alt, double lat1,
135                         double lon1, double az1, 
136                         double s, double *lat2, double *lon2,
137                         double *az2 )
138 { return geo_direct_wgs_84(lat1, lon1, az1, s, lat2, lon2, az2); }
139
140 /**
141  * Given a starting position and an offset radial and distance,
142  * calculate an ending positon on a wgs84 ellipsoid.
143  * @param p1 (in) geodetic position
144  * @param az1 (in) degrees
145  * @param s (in) distance in meters
146  * @param p2 (out) geodetic position
147  * @param az2 (out) return course in degrees
148  */
149 inline int geo_direct_wgs_84(const SGGeod& p1, double az1,
150                              double s, SGGeod& p2, double *az2 )
151 {
152   double lat2, lon2;
153   int ret = geo_direct_wgs_84(p1.getLatitudeDeg(), p1.getLongitudeDeg(),
154                               az1, s, &lat2, &lon2, az2);
155   p2.setLatitudeDeg(lat2);
156   p2.setLongitudeDeg(lon2);
157   return ret;
158 }
159
160 /**
161  * Given an altitude and two sets of (lat, lon) calculate great circle
162  * distance between them as well as the starting and ending azimuths.
163  * @param alt (in) meters (unused)
164  * @param lat1 (in) degrees
165  * @param lon1 (in) degrees
166  * @param lat2 (in) degrees
167  * @param lon2 (in) degrees
168  * @param az1 (out) start heading degrees
169  * @param az2 (out) end heading degrees
170  * @param s (out) distance meters
171  */
172 int geo_inverse_wgs_84( double lat1, double lon1, double lat2,
173                         double lon2, double *az1, double *az2,
174                         double *s );
175 inline int geo_inverse_wgs_84( double alt, double lat1,
176                                double lon1, double lat2,
177                                double lon2, double *az1, double *az2,
178                                double *s )
179 { return geo_inverse_wgs_84(lat1, lon1, lat2, lon2, az1, az2, s); }
180
181
182 /**
183  * Given an altitude and two sets of (lat, lon) calculate great circle
184  * distance between them as well as the starting and ending azimuths.
185  * @param p1 (in) first position
186  * @param p2 (in) fsecond position
187  * @param az1 (out) start heading degrees
188  * @param az2 (out) end heading degrees
189  * @param s (out) distance meters
190  */
191 inline int geo_inverse_wgs_84(const SGGeod& p1, const SGGeod& p2,
192                               double *az1, double *az2, double *s )
193 {
194   return geo_inverse_wgs_84(p1.getLatitudeDeg(), p1.getLongitudeDeg(),
195                             p2.getLatitudeDeg(), p2.getLongitudeDeg(),
196                             az1, az2, s);
197 }
198
199 #endif // _SG_GEODESY_HXX