]> git.mxchange.org Git - simgear.git/blob - simgear/math/sg_geodesy.hxx
Mathias Fröhlich:
[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 // Returns the insersection of the line joining the center of the
8 // earth and the specified cylindrical point with the surface of the
9 // WGS84 ellipsoid.  Works by finding a normalization constant (in
10 // squashed space) that places the squashed point on the surface of
11 // the sphere.
12 inline double seaLevelRadius(double r, double z)
13 {
14     double sr = r * SGGeodesy::SQUASH;
15     double zz = z*z;
16     return SGGeodesy::POLRAD*sqrt((r*r + zz)/(sr*sr + zz));
17 }
18
19 /** 
20  * Convert from geocentric coordinates to geodetic coordinates
21  * @param lat_geoc (in) Geocentric latitude, radians, + = North
22  * @param radius (in) C.G. radius to earth center (meters)
23  * @param lat_geod (out) Geodetic latitude, radians, + = North
24  * @param alt (out) C.G. altitude above mean sea level (meters)
25  * @param sea_level_r (out) radius from earth center to sea level at
26  *        local vertical (surface normal) of C.G. (meters)
27  */
28 inline void sgGeocToGeod(double lat_geoc, double radius,
29                          double *lat_geod, double *alt, double *sea_level_r)
30 {
31   SGVec3<double> cart;
32   SGGeodesy::SGGeocToCart(SGGeoc::fromRadM(0, lat_geoc, radius), cart);
33   SGGeod geod;
34   SGGeodesy::SGCartToGeod(cart, geod);
35   *lat_geod = geod.getLatitudeRad();
36   *alt = geod.getElevationM();
37   *sea_level_r = SGGeodesy::SGGeodToSeaLevelRadius(geod);
38 }
39
40 /**
41  * Convert from geodetic coordinates to geocentric coordinates.
42  * WARNING: this function is non-reversible.  Due to the fact that
43  * "up" is a different direction for geocentric and geodetic frames,
44  * you can not simply add your "alt" parameter to the "sl_radius"
45  * result and get back (via sgGeodToGeoc()) to the coordinates you
46  * started with.  The error under normal conditions will be of
47  * centimeter order; whether that is important or not is application
48  * dependent. Consider using sgGeodToCart() instead.
49  *
50  * @param lat_geod (in) Geodetic latitude, radians, + = North
51  * @param alt (in) C.G. altitude above mean sea level (meters)
52  * @param sl_radius (out) SEA LEVEL radius to earth center (meters)
53  * @param lat_geoc (out) Geocentric latitude, radians, + = North
54  */
55 inline void sgGeodToGeoc(double lat_geod, double alt,
56                          double *sl_radius, double *lat_geoc)
57 {
58   SGVec3<double> cart;
59   SGGeodesy::SGGeodToCart(SGGeod::fromRadM(0, lat_geod, alt), cart);
60   SGGeoc geoc;
61   SGGeodesy::SGCartToGeoc(cart, geoc);
62   *lat_geoc = geoc.getLatitudeRad();
63   *sl_radius = seaLevelRadius(cart(0), cart(2));
64 }
65
66
67 /**
68  * Convert a cartesian point to a geodetic lat/lon/altitude.
69  *
70  * @param xyz (in) Pointer to cartesian point.
71  * @param lat (out) Latitude, in radians
72  * @param lon (out) Longitude, in radians
73  * @param alt (out) Altitude, in meters above the WGS84 ellipsoid
74  */
75 inline void sgCartToGeod(const double* xyz, double* lat, double* lon, double* alt)
76 {
77   SGGeod geod;
78   SGGeodesy::SGCartToGeod(SGVec3<double>(xyz), geod);
79   *lat = geod.getLatitudeRad();
80   *lon = geod.getLongitudeRad();
81   *alt = geod.getElevationM();
82 }
83
84 /**
85  * Convert a cartesian point to a geodetic lat/lon/altitude.
86  * Alternate form using Point3D objects.
87  *
88  * @param cartesian point
89  * @return geodetic point
90  */
91 inline Point3D sgCartToGeod(const Point3D& p)
92 {
93   SGGeod geod;
94   SGGeodesy::SGCartToGeod(SGVec3<double>(p.x(), p.y(), p.z()), geod);
95   return Point3D::fromSGGeod(geod);
96 }
97
98
99 /**
100  * Convert a geodetic lat/lon/altitude to a cartesian point.
101  *
102  * @param lat (in) Latitude, in radians
103  * @param lon (in) Longitude, in radians
104  * @param alt (in) Altitude, in meters above the WGS84 ellipsoid
105  * @param xyz (out) Pointer to cartesian point.
106  */
107 inline void sgGeodToCart(double lat, double lon, double alt, double* xyz)
108 {
109   SGVec3<double> cart;
110   SGGeodesy::SGGeodToCart(SGGeod::fromRadM(lon, lat, alt), cart);
111   xyz[0] = cart(0);
112   xyz[1] = cart(1);
113   xyz[2] = cart(2);
114 }
115
116 /**
117  * Convert a geodetic lat/lon/altitude to a cartesian point.
118  * Alternate form using Point3D objects.
119  *
120  * @param geodetic point
121  * @return cartesian point
122  */
123 inline Point3D sgGeodToCart(const Point3D& geod)
124 {
125   SGVec3<double> cart;
126   SGGeodesy::SGGeodToCart(SGGeod::fromRadM(geod.lon(), geod.lat(), geod.elev()), cart);
127   return Point3D::fromSGVec3(cart);
128 }
129
130 /**
131  * Given a starting position and an offset radial and distance,
132  * calculate an ending positon on a wgs84 ellipsoid.
133  * @param alt (in) meters
134  * @param lat1 (in) degrees
135  * @param lon1 (in) degrees
136  * @param az1 (in) degrees
137  * @param s (in) distance in meters
138  * @param lat2 (out) degrees
139  * @param lon2 (out) degrees
140  * @param az2 (out) return course in degrees
141  */
142 int geo_direct_wgs_84 ( double alt, double lat1,
143                         double lon1, double az1, 
144                         double s, double *lat2, double *lon2,
145                         double *az2 );
146
147
148 /**
149  * Given an altitude and two sets of (lat, lon) calculate great circle
150  * distance between them as well as the starting and ending azimuths.
151  * @param alt (in) meters
152  * @param lat1 (in) degrees
153  * @param lon1 (in) degrees
154  * @param lat2 (in) degrees
155  * @param lon2 (in) degrees
156  * @param az1 (out) start heading degrees
157  * @param az2 (out) end heading degrees
158  * @param s (out) distance meters
159  */
160 int geo_inverse_wgs_84( double alt, double lat1,
161                         double lon1, double lat2,
162                         double lon2, double *az1, double *az2,
163                         double *s );
164
165 #endif // _SG_GEODESY_HXX