GeographicLib  1.38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Geocentric.hpp
Go to the documentation of this file.
1 /**
2  * \file Geocentric.hpp
3  * \brief Header for GeographicLib::Geocentric class
4  *
5  * Copyright (c) Charles Karney (2008-2014) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #if !defined(GEOGRAPHICLIB_GEOCENTRIC_HPP)
11 #define GEOGRAPHICLIB_GEOCENTRIC_HPP 1
12 
13 #include <vector>
15 
16 namespace GeographicLib {
17 
18  /**
19  * \brief %Geocentric coordinates
20  *
21  * Convert between geodetic coordinates latitude = \e lat, longitude = \e
22  * lon, height = \e h (measured vertically from the surface of the ellipsoid)
23  * to geocentric coordinates (\e X, \e Y, \e Z). The origin of geocentric
24  * coordinates is at the center of the earth. The \e Z axis goes thru the
25  * north pole, \e lat = 90&deg;. The \e X axis goes thru \e lat = 0,
26  * \e lon = 0. %Geocentric coordinates are also known as earth centered,
27  * earth fixed (ECEF) coordinates.
28  *
29  * The conversion from geographic to geocentric coordinates is
30  * straightforward. For the reverse transformation we use
31  * - H. Vermeille,
32  * <a href="http://dx.doi.org/10.1007/s00190-002-0273-6"> Direct
33  * transformation from geocentric coordinates to geodetic coordinates</a>,
34  * J. Geodesy 76, 451--454 (2002).
35  * .
36  * Several changes have been made to ensure that the method returns accurate
37  * results for all finite inputs (even if \e h is infinite). The changes are
38  * described in Appendix B of
39  * - C. F. F. Karney,
40  * <a href="http://arxiv.org/abs/1102.1215v1">Geodesics
41  * on an ellipsoid of revolution</a>,
42  * Feb. 2011;
43  * preprint
44  * <a href="http://arxiv.org/abs/1102.1215v1">arxiv:1102.1215v1</a>.
45  * .
46  * Vermeille similarly updated his method in
47  * - H. Vermeille,
48  * <a href="http://dx.doi.org/10.1007/s00190-010-0419-x">
49  * An analytical method to transform geocentric into
50  * geodetic coordinates</a>, J. Geodesy 85, 105--117 (2011).
51  * .
52  * See \ref geocentric for more information.
53  *
54  * The errors in these routines are close to round-off. Specifically, for
55  * points within 5000 km of the surface of the ellipsoid (either inside or
56  * outside the ellipsoid), the error is bounded by 7 nm (7 nanometers) for
57  * the WGS84 ellipsoid. See \ref geocentric for further information on the
58  * errors.
59  *
60  * Example of use:
61  * \include example-Geocentric.cpp
62  *
63  * <a href="CartConvert.1.html">CartConvert</a> is a command-line utility
64  * providing access to the functionality of Geocentric and LocalCartesian.
65  **********************************************************************/
66 
68  private:
69  typedef Math::real real;
70  friend class LocalCartesian;
71  friend class MagneticCircle; // MagneticCircle uses Rotation
72  friend class MagneticModel; // MagneticModel uses IntForward
73  friend class GravityCircle; // GravityCircle uses Rotation
74  friend class GravityModel; // GravityModel uses IntForward
75  friend class NormalGravity; // NormalGravity uses IntForward
76  friend class SphericalHarmonic;
77  friend class SphericalHarmonic1;
78  friend class SphericalHarmonic2;
79  static const size_t dim_ = 3;
80  static const size_t dim2_ = dim_ * dim_;
81  real _a, _f, _e2, _e2m, _e2a, _e4a, _maxrad;
82  static void Rotation(real sphi, real cphi, real slam, real clam,
83  real M[dim2_]);
84  static void Rotate(real M[dim2_], real x, real y, real z,
85  real& X, real& Y, real& Z) {
86  // Perform [X,Y,Z]^t = M.[x,y,z]^t
87  // (typically local cartesian to geocentric)
88  X = M[0] * x + M[1] * y + M[2] * z;
89  Y = M[3] * x + M[4] * y + M[5] * z;
90  Z = M[6] * x + M[7] * y + M[8] * z;
91  }
92  static void Unrotate(real M[dim2_], real X, real Y, real Z,
93  real& x, real& y, real& z) {
94  // Perform [x,y,z]^t = M^t.[X,Y,Z]^t
95  // (typically geocentric to local cartesian)
96  x = M[0] * X + M[3] * Y + M[6] * Z;
97  y = M[1] * X + M[4] * Y + M[7] * Z;
98  z = M[2] * X + M[5] * Y + M[8] * Z;
99  }
100  void IntForward(real lat, real lon, real h, real& X, real& Y, real& Z,
101  real M[dim2_]) const;
102  void IntReverse(real X, real Y, real Z, real& lat, real& lon, real& h,
103  real M[dim2_]) const;
104 
105  public:
106 
107  /**
108  * Constructor for a ellipsoid with
109  *
110  * @param[in] a equatorial radius (meters).
111  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
112  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
113  * flattening to 1/\e f.
114  * @exception GeographicErr if \e a or (1 &minus; \e f) \e a is not
115  * positive.
116  **********************************************************************/
117  Geocentric(real a, real f);
118 
119  /**
120  * A default constructor (for use by NormalGravity).
121  **********************************************************************/
122  Geocentric() : _a(-1) {}
123 
124  /**
125  * Convert from geodetic to geocentric coordinates.
126  *
127  * @param[in] lat latitude of point (degrees).
128  * @param[in] lon longitude of point (degrees).
129  * @param[in] h height of point above the ellipsoid (meters).
130  * @param[out] X geocentric coordinate (meters).
131  * @param[out] Y geocentric coordinate (meters).
132  * @param[out] Z geocentric coordinate (meters).
133  *
134  * \e lat should be in the range [&minus;90&deg;, 90&deg;]; \e lon
135  * should be in the range [&minus;540&deg;, 540&deg;).
136  **********************************************************************/
137  void Forward(real lat, real lon, real h, real& X, real& Y, real& Z)
138  const {
139  if (Init())
140  IntForward(lat, lon, h, X, Y, Z, NULL);
141  }
142 
143  /**
144  * Convert from geodetic to geocentric coordinates and return rotation
145  * matrix.
146  *
147  * @param[in] lat latitude of point (degrees).
148  * @param[in] lon longitude of point (degrees).
149  * @param[in] h height of point above the ellipsoid (meters).
150  * @param[out] X geocentric coordinate (meters).
151  * @param[out] Y geocentric coordinate (meters).
152  * @param[out] Z geocentric coordinate (meters).
153  * @param[out] M if the length of the vector is 9, fill with the rotation
154  * matrix in row-major order.
155  *
156  * Let \e v be a unit vector located at (\e lat, \e lon, \e h). We can
157  * express \e v as \e column vectors in one of two ways
158  * - in east, north, up coordinates (where the components are relative to a
159  * local coordinate system at (\e lat, \e lon, \e h)); call this
160  * representation \e v1.
161  * - in geocentric \e X, \e Y, \e Z coordinates; call this representation
162  * \e v0.
163  * .
164  * Then we have \e v0 = \e M &sdot; \e v1.
165  **********************************************************************/
166  void Forward(real lat, real lon, real h, real& X, real& Y, real& Z,
167  std::vector<real>& M)
168  const {
169  if (!Init())
170  return;
171  if (M.end() == M.begin() + dim2_) {
172  real t[dim2_];
173  IntForward(lat, lon, h, X, Y, Z, t);
174  std::copy(t, t + dim2_, M.begin());
175  } else
176  IntForward(lat, lon, h, X, Y, Z, NULL);
177  }
178 
179  /**
180  * Convert from geocentric to geodetic to coordinates.
181  *
182  * @param[in] X geocentric coordinate (meters).
183  * @param[in] Y geocentric coordinate (meters).
184  * @param[in] Z geocentric coordinate (meters).
185  * @param[out] lat latitude of point (degrees).
186  * @param[out] lon longitude of point (degrees).
187  * @param[out] h height of point above the ellipsoid (meters).
188  *
189  * In general there are multiple solutions and the result which maximizes
190  * \e h is returned. If there are still multiple solutions with different
191  * latitudes (applies only if \e Z = 0), then the solution with \e lat > 0
192  * is returned. If there are still multiple solutions with different
193  * longitudes (applies only if \e X = \e Y = 0) then \e lon = 0 is
194  * returned. The value of \e h returned satisfies \e h &ge; &minus; \e a
195  * (1 &minus; <i>e</i><sup>2</sup>) / sqrt(1 &minus; <i>e</i><sup>2</sup>
196  * sin<sup>2</sup>\e lat). The value of \e lon returned is in the range
197  * [&minus;180&deg;, 180&deg;).
198  **********************************************************************/
199  void Reverse(real X, real Y, real Z, real& lat, real& lon, real& h)
200  const {
201  if (Init())
202  IntReverse(X, Y, Z, lat, lon, h, NULL);
203  }
204 
205  /**
206  * Convert from geocentric to geodetic to coordinates.
207  *
208  * @param[in] X geocentric coordinate (meters).
209  * @param[in] Y geocentric coordinate (meters).
210  * @param[in] Z geocentric coordinate (meters).
211  * @param[out] lat latitude of point (degrees).
212  * @param[out] lon longitude of point (degrees).
213  * @param[out] h height of point above the ellipsoid (meters).
214  * @param[out] M if the length of the vector is 9, fill with the rotation
215  * matrix in row-major order.
216  *
217  * Let \e v be a unit vector located at (\e lat, \e lon, \e h). We can
218  * express \e v as \e column vectors in one of two ways
219  * - in east, north, up coordinates (where the components are relative to a
220  * local coordinate system at (\e lat, \e lon, \e h)); call this
221  * representation \e v1.
222  * - in geocentric \e X, \e Y, \e Z coordinates; call this representation
223  * \e v0.
224  * .
225  * Then we have \e v1 = <i>M</i><sup>T</sup> &sdot; \e v0, where
226  * <i>M</i><sup>T</sup> is the transpose of \e M.
227  **********************************************************************/
228  void Reverse(real X, real Y, real Z, real& lat, real& lon, real& h,
229  std::vector<real>& M)
230  const {
231  if (!Init())
232  return;
233  if (M.end() == M.begin() + dim2_) {
234  real t[dim2_];
235  IntReverse(X, Y, Z, lat, lon, h, t);
236  std::copy(t, t + dim2_, M.begin());
237  } else
238  IntReverse(X, Y, Z, lat, lon, h, NULL);
239  }
240 
241  /** \name Inspector functions
242  **********************************************************************/
243  ///@{
244  /**
245  * @return true if the object has been initialized.
246  **********************************************************************/
247  bool Init() const { return _a > 0; }
248  /**
249  * @return \e a the equatorial radius of the ellipsoid (meters). This is
250  * the value used in the constructor.
251  **********************************************************************/
253  { return Init() ? _a : Math::NaN(); }
254 
255  /**
256  * @return \e f the flattening of the ellipsoid. This is the
257  * value used in the constructor.
258  **********************************************************************/
260  { return Init() ? _f : Math::NaN(); }
261  ///@}
262 
263  /// \cond SKIP
264  /**
265  * <b>DEPRECATED</b>
266  * @return \e r the inverse flattening of the ellipsoid.
267  **********************************************************************/
268  Math::real InverseFlattening() const
269  { return Init() ? 1/_f : Math::NaN(); }
270  /// \endcond
271 
272  /**
273  * A global instantiation of Geocentric with the parameters for the WGS84
274  * ellipsoid.
275  **********************************************************************/
276  static const Geocentric& WGS84();
277  };
278 
279 } // namespace GeographicLib
280 
281 #endif // GEOGRAPHICLIB_GEOCENTRIC_HPP
static T NaN()
Definition: Math.hpp:460
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z, std::vector< real > &M) const
Definition: Geocentric.hpp:166
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:70
GeographicLib::Math::real real
Definition: GeodSolve.cpp:40
The normal gravity of the earth.
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z) const
Definition: Geocentric.hpp:137
Model of the earth's magnetic field.
Geomagnetic field on a circle of latitude.
Geocentric coordinates
Definition: Geocentric.hpp:67
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
Math::real Flattening() const
Definition: Geocentric.hpp:259
Spherical harmonic series with two corrections to the coefficients.
Math::real MajorRadius() const
Definition: Geocentric.hpp:252
Local cartesian coordinates.
Model of the earth's gravity field.
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h) const
Definition: Geocentric.hpp:199
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h, std::vector< real > &M) const
Definition: Geocentric.hpp:228
Header for GeographicLib::Constants class.
Spherical harmonic series with a correction to the coefficients.
Spherical harmonic series.
Gravity on a circle of latitude.