GeographicLib  1.43
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="https://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="https://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  static const size_t dim_ = 3;
77  static const size_t dim2_ = dim_ * dim_;
78  real _a, _f, _e2, _e2m, _e2a, _e4a, _maxrad;
79  static void Rotation(real sphi, real cphi, real slam, real clam,
80  real M[dim2_]);
81  static void Rotate(real M[dim2_], real x, real y, real z,
82  real& X, real& Y, real& Z) {
83  // Perform [X,Y,Z]^t = M.[x,y,z]^t
84  // (typically local cartesian to geocentric)
85  X = M[0] * x + M[1] * y + M[2] * z;
86  Y = M[3] * x + M[4] * y + M[5] * z;
87  Z = M[6] * x + M[7] * y + M[8] * z;
88  }
89  static void Unrotate(real M[dim2_], real X, real Y, real Z,
90  real& x, real& y, real& z) {
91  // Perform [x,y,z]^t = M^t.[X,Y,Z]^t
92  // (typically geocentric to local cartesian)
93  x = M[0] * X + M[3] * Y + M[6] * Z;
94  y = M[1] * X + M[4] * Y + M[7] * Z;
95  z = M[2] * X + M[5] * Y + M[8] * Z;
96  }
97  void IntForward(real lat, real lon, real h, real& X, real& Y, real& Z,
98  real M[dim2_]) const;
99  void IntReverse(real X, real Y, real Z, real& lat, real& lon, real& h,
100  real M[dim2_]) const;
101 
102  public:
103 
104  /**
105  * Constructor for a ellipsoid with
106  *
107  * @param[in] a equatorial radius (meters).
108  * @param[in] f flattening of ellipsoid. Setting \e f = 0 gives a sphere.
109  * Negative \e f gives a prolate ellipsoid. If \e f &gt; 1, set
110  * flattening to 1/\e f.
111  * @exception GeographicErr if \e a or (1 &minus; \e f) \e a is not
112  * positive.
113  **********************************************************************/
114  Geocentric(real a, real f);
115 
116  /**
117  * A default constructor (for use by NormalGravity).
118  **********************************************************************/
119  Geocentric() : _a(-1) {}
120 
121  /**
122  * Convert from geodetic to geocentric coordinates.
123  *
124  * @param[in] lat latitude of point (degrees).
125  * @param[in] lon longitude of point (degrees).
126  * @param[in] h height of point above the ellipsoid (meters).
127  * @param[out] X geocentric coordinate (meters).
128  * @param[out] Y geocentric coordinate (meters).
129  * @param[out] Z geocentric coordinate (meters).
130  *
131  * \e lat should be in the range [&minus;90&deg;, 90&deg;]; \e lon
132  * should be in the range [&minus;540&deg;, 540&deg;).
133  **********************************************************************/
134  void Forward(real lat, real lon, real h, real& X, real& Y, real& Z)
135  const {
136  if (Init())
137  IntForward(lat, lon, h, X, Y, Z, NULL);
138  }
139 
140  /**
141  * Convert from geodetic to geocentric coordinates and return rotation
142  * matrix.
143  *
144  * @param[in] lat latitude of point (degrees).
145  * @param[in] lon longitude of point (degrees).
146  * @param[in] h height of point above the ellipsoid (meters).
147  * @param[out] X geocentric coordinate (meters).
148  * @param[out] Y geocentric coordinate (meters).
149  * @param[out] Z geocentric coordinate (meters).
150  * @param[out] M if the length of the vector is 9, fill with the rotation
151  * matrix in row-major order.
152  *
153  * Let \e v be a unit vector located at (\e lat, \e lon, \e h). We can
154  * express \e v as \e column vectors in one of two ways
155  * - in east, north, up coordinates (where the components are relative to a
156  * local coordinate system at (\e lat, \e lon, \e h)); call this
157  * representation \e v1.
158  * - in geocentric \e X, \e Y, \e Z coordinates; call this representation
159  * \e v0.
160  * .
161  * Then we have \e v0 = \e M &sdot; \e v1.
162  **********************************************************************/
163  void Forward(real lat, real lon, real h, real& X, real& Y, real& Z,
164  std::vector<real>& M)
165  const {
166  if (!Init())
167  return;
168  if (M.end() == M.begin() + dim2_) {
169  real t[dim2_];
170  IntForward(lat, lon, h, X, Y, Z, t);
171  std::copy(t, t + dim2_, M.begin());
172  } else
173  IntForward(lat, lon, h, X, Y, Z, NULL);
174  }
175 
176  /**
177  * Convert from geocentric to geodetic to coordinates.
178  *
179  * @param[in] X geocentric coordinate (meters).
180  * @param[in] Y geocentric coordinate (meters).
181  * @param[in] Z geocentric coordinate (meters).
182  * @param[out] lat latitude of point (degrees).
183  * @param[out] lon longitude of point (degrees).
184  * @param[out] h height of point above the ellipsoid (meters).
185  *
186  * In general there are multiple solutions and the result which maximizes
187  * \e h is returned. If there are still multiple solutions with different
188  * latitudes (applies only if \e Z = 0), then the solution with \e lat > 0
189  * is returned. If there are still multiple solutions with different
190  * longitudes (applies only if \e X = \e Y = 0) then \e lon = 0 is
191  * returned. The value of \e h returned satisfies \e h &ge; &minus; \e a
192  * (1 &minus; <i>e</i><sup>2</sup>) / sqrt(1 &minus; <i>e</i><sup>2</sup>
193  * sin<sup>2</sup>\e lat). The value of \e lon returned is in the range
194  * [&minus;180&deg;, 180&deg;).
195  **********************************************************************/
196  void Reverse(real X, real Y, real Z, real& lat, real& lon, real& h)
197  const {
198  if (Init())
199  IntReverse(X, Y, Z, lat, lon, h, NULL);
200  }
201 
202  /**
203  * Convert from geocentric to geodetic to coordinates.
204  *
205  * @param[in] X geocentric coordinate (meters).
206  * @param[in] Y geocentric coordinate (meters).
207  * @param[in] Z geocentric coordinate (meters).
208  * @param[out] lat latitude of point (degrees).
209  * @param[out] lon longitude of point (degrees).
210  * @param[out] h height of point above the ellipsoid (meters).
211  * @param[out] M if the length of the vector is 9, fill with the rotation
212  * matrix in row-major order.
213  *
214  * Let \e v be a unit vector located at (\e lat, \e lon, \e h). We can
215  * express \e v as \e column vectors in one of two ways
216  * - in east, north, up coordinates (where the components are relative to a
217  * local coordinate system at (\e lat, \e lon, \e h)); call this
218  * representation \e v1.
219  * - in geocentric \e X, \e Y, \e Z coordinates; call this representation
220  * \e v0.
221  * .
222  * Then we have \e v1 = <i>M</i><sup>T</sup> &sdot; \e v0, where
223  * <i>M</i><sup>T</sup> is the transpose of \e M.
224  **********************************************************************/
225  void Reverse(real X, real Y, real Z, real& lat, real& lon, real& h,
226  std::vector<real>& M)
227  const {
228  if (!Init())
229  return;
230  if (M.end() == M.begin() + dim2_) {
231  real t[dim2_];
232  IntReverse(X, Y, Z, lat, lon, h, t);
233  std::copy(t, t + dim2_, M.begin());
234  } else
235  IntReverse(X, Y, Z, lat, lon, h, NULL);
236  }
237 
238  /** \name Inspector functions
239  **********************************************************************/
240  ///@{
241  /**
242  * @return true if the object has been initialized.
243  **********************************************************************/
244  bool Init() const { return _a > 0; }
245  /**
246  * @return \e a the equatorial radius of the ellipsoid (meters). This is
247  * the value used in the constructor.
248  **********************************************************************/
250  { return Init() ? _a : Math::NaN(); }
251 
252  /**
253  * @return \e f the flattening of the ellipsoid. This is the
254  * value used in the constructor.
255  **********************************************************************/
257  { return Init() ? _f : Math::NaN(); }
258  ///@}
259 
260  /// \cond SKIP
261  /**
262  * <b>DEPRECATED</b>
263  * @return \e r the inverse flattening of the ellipsoid.
264  **********************************************************************/
265  Math::real InverseFlattening() const
266  { return Init() ? 1/_f : Math::NaN(); }
267  /// \endcond
268 
269  /**
270  * A global instantiation of Geocentric with the parameters for the WGS84
271  * ellipsoid.
272  **********************************************************************/
273  static const Geocentric& WGS84();
274  };
275 
276 } // namespace GeographicLib
277 
278 #endif // GEOGRAPHICLIB_GEOCENTRIC_HPP
static T NaN()
Definition: Math.hpp:629
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z, std::vector< real > &M) const
Definition: Geocentric.hpp:163
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:90
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
The normal gravity of the earth.
void Forward(real lat, real lon, real h, real &X, real &Y, real &Z) const
Definition: Geocentric.hpp:134
Model of the earth&#39;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:256
Math::real MajorRadius() const
Definition: Geocentric.hpp:249
Local cartesian coordinates.
Model of the earth&#39;s gravity field.
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h) const
Definition: Geocentric.hpp:196
void Reverse(real X, real Y, real Z, real &lat, real &lon, real &h, std::vector< real > &M) const
Definition: Geocentric.hpp:225
Header for GeographicLib::Constants class.
Gravity on a circle of latitude.