GeographicLib  1.44
Constants.hpp
Go to the documentation of this file.
1 /**
2  * \file Constants.hpp
3  * \brief Header for GeographicLib::Constants class
4  *
5  * Copyright (c) Charles Karney (2008-2015) <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_CONSTANTS_HPP)
11 #define GEOGRAPHICLIB_CONSTANTS_HPP 1
12 
13 #include <GeographicLib/Config.h>
14 
15 /**
16  * @relates GeographicLib::Constants
17  * Pack the version components into a single integer. Users should not rely on
18  * this particular packing of the components of the version number; see the
19  * documentation for GEOGRAPHICLIB_VERSION, below.
20  **********************************************************************/
21 #define GEOGRAPHICLIB_VERSION_NUM(a,b,c) ((((a) * 10000 + (b)) * 100) + (c))
22 
23 /**
24  * @relates GeographicLib::Constants
25  * The version of GeographicLib as a single integer, packed as MMmmmmpp where
26  * MM is the major version, mmmm is the minor version, and pp is the patch
27  * level. Users should not rely on this particular packing of the components
28  * of the version number. Instead they should use a test such as \code
29  #if GEOGRAPHICLIB_VERSION >= GEOGRAPHICLIB_VERSION_NUM(1,37,0)
30  ...
31  #endif
32  * \endcode
33  **********************************************************************/
34 #define GEOGRAPHICLIB_VERSION \
35  GEOGRAPHICLIB_VERSION_NUM(GEOGRAPHICLIB_VERSION_MAJOR, \
36  GEOGRAPHICLIB_VERSION_MINOR, \
37  GEOGRAPHICLIB_VERSION_PATCH)
38 
39 /**
40  * @relates GeographicLib::Constants
41  * Is the C++11 static_assert available?
42  **********************************************************************/
43 #if !defined(GEOGRAPHICLIB_HAS_STATIC_ASSERT)
44 # if __cplusplus >= 201103 || defined(__GXX_EXPERIMENTAL_CXX0X__)
45 # define GEOGRAPHICLIB_HAS_STATIC_ASSERT 1
46 # elif defined(_MSC_VER) && _MSC_VER >= 1600
47 // For reference, here is a table of Visual Studio and _MSC_VER
48 // correspondences:
49 //
50 // _MSC_VER Visual Studio
51 // 1100 vc5
52 // 1200 vc6
53 // 1300 vc7
54 // 1310 vc7.1 (2003)
55 // 1400 vc8 (2005)
56 // 1500 vc9 (2008)
57 // 1600 vc10 (2010)
58 // 1700 vc11 (2012)
59 // 1800 vc12 (2013)
60 // 1900 vc14 (2015)
61 # define GEOGRAPHICLIB_HAS_STATIC_ASSERT 1
62 # else
63 # define GEOGRAPHICLIB_HAS_STATIC_ASSERT 0
64 # endif
65 #endif
66 
67 /**
68  * @relates GeographicLib::Constants
69  * A compile-time assert. Use C++11 static_assert, if available.
70  **********************************************************************/
71 #if !defined(GEOGRAPHICLIB_STATIC_ASSERT)
72 # if GEOGRAPHICLIB_HAS_STATIC_ASSERT
73 # define GEOGRAPHICLIB_STATIC_ASSERT static_assert
74 # else
75 # define GEOGRAPHICLIB_STATIC_ASSERT(cond,reason) \
76  { enum{ GEOGRAPHICLIB_STATIC_ASSERT_ENUM = 1/int(cond) }; }
77 # endif
78 #endif
79 
80 #if defined(_MSC_VER) && defined(GEOGRAPHICLIB_SHARED_LIB) && \
81  GEOGRAPHICLIB_SHARED_LIB
82 # if GEOGRAPHICLIB_SHARED_LIB > 1
83 # error GEOGRAPHICLIB_SHARED_LIB must be 0 or 1
84 # elif defined(GeographicLib_EXPORTS)
85 # define GEOGRAPHICLIB_EXPORT __declspec(dllexport)
86 # else
87 # define GEOGRAPHICLIB_EXPORT __declspec(dllimport)
88 # endif
89 #else
90 # define GEOGRAPHICLIB_EXPORT
91 #endif
92 
93 #include <stdexcept>
94 #include <string>
95 #include <GeographicLib/Math.hpp>
96 
97 /**
98  * \brief Namespace for %GeographicLib
99  *
100  * All of %GeographicLib is defined within the GeographicLib namespace. In
101  * addition all the header files are included via %GeographicLib/Class.hpp.
102  * This minimizes the likelihood of conflicts with other packages.
103  **********************************************************************/
104 namespace GeographicLib {
105 
106  /**
107  * \brief %Constants needed by %GeographicLib
108  *
109  * Define constants specifying the WGS84 ellipsoid, the UTM and UPS
110  * projections, and various unit conversions.
111  *
112  * Example of use:
113  * \include example-Constants.cpp
114  **********************************************************************/
116  private:
117  typedef Math::real real;
118  Constants(); // Disable constructor
119 
120  public:
121  /**
122  * A synonym for Math::degree<real>().
123  **********************************************************************/
124  static inline Math::real degree() { return Math::degree(); }
125  /**
126  * @return the number of radians in an arcminute.
127  **********************************************************************/
128  static inline Math::real arcminute()
129  { return Math::degree() / 60; }
130  /**
131  * @return the number of radians in an arcsecond.
132  **********************************************************************/
133  static inline Math::real arcsecond()
134  { return Math::degree() / 3600; }
135 
136  /** \name Ellipsoid parameters
137  **********************************************************************/
138  ///@{
139  /**
140  * @tparam T the type of the returned value.
141  * @return the equatorial radius of WGS84 ellipsoid (6378137 m).
142  **********************************************************************/
143  template<typename T> static inline T WGS84_a()
144  { return 6378137 * meter<T>(); }
145  /**
146  * A synonym for WGS84_a<real>().
147  **********************************************************************/
148  static inline Math::real WGS84_a() { return WGS84_a<real>(); }
149  /**
150  * @tparam T the type of the returned value.
151  * @return the flattening of WGS84 ellipsoid (1/298.257223563).
152  **********************************************************************/
153  template<typename T> static inline T WGS84_f() {
154  // Evaluating this as 1000000000 / T(298257223563LL) reduces the
155  // round-off error by about 10%. However, expressing the flattening as
156  // 1/298.257223563 is well ingrained.
157  return 1 / ( T(298257223563LL) / 1000000000 );
158  }
159  /**
160  * A synonym for WGS84_f<real>().
161  **********************************************************************/
162  static inline Math::real WGS84_f() { return WGS84_f<real>(); }
163  /**
164  * @tparam T the type of the returned value.
165  * @return the gravitational constant of the WGS84 ellipsoid, \e GM, in
166  * m<sup>3</sup> s<sup>&minus;2</sup>.
167  **********************************************************************/
168  template<typename T> static inline T WGS84_GM()
169  { return T(3986004) * 100000000 + 41800000; }
170  /**
171  * A synonym for WGS84_GM<real>().
172  **********************************************************************/
173  static inline Math::real WGS84_GM() { return WGS84_GM<real>(); }
174  /**
175  * @tparam T the type of the returned value.
176  * @return the angular velocity of the WGS84 ellipsoid, &omega;, in rad
177  * s<sup>&minus;1</sup>.
178  **********************************************************************/
179  template<typename T> static inline T WGS84_omega()
180  { return 7292115 / (T(1000000) * 100000); }
181  /**
182  * A synonym for WGS84_omega<real>().
183  **********************************************************************/
184  static inline Math::real WGS84_omega() { return WGS84_omega<real>(); }
185  /// \cond SKIP
186  /**
187  * <b>DEPRECATED</b>
188  * @return the reciprocal flattening of WGS84 ellipsoid.
189  **********************************************************************/
190  template<typename T> static inline T WGS84_r()
191  { return 1/WGS84_f<T>(); }
192  /**
193  * <b>DEPRECATED</b>
194  * A synonym for WGS84_r<real>().
195  **********************************************************************/
196  static inline Math::real WGS84_r() { return WGS84_r<real>(); }
197  /// \endcond
198  /**
199  * @tparam T the type of the returned value.
200  * @return the equatorial radius of GRS80 ellipsoid, \e a, in m.
201  **********************************************************************/
202  template<typename T> static inline T GRS80_a()
203  { return 6378137 * meter<T>(); }
204  /**
205  * A synonym for GRS80_a<real>().
206  **********************************************************************/
207  static inline Math::real GRS80_a() { return GRS80_a<real>(); }
208  /**
209  * @tparam T the type of the returned value.
210  * @return the gravitational constant of the GRS80 ellipsoid, \e GM, in
211  * m<sup>3</sup> s<sup>&minus;2</sup>.
212  **********************************************************************/
213  template<typename T> static inline T GRS80_GM()
214  { return T(3986005) * 100000000; }
215  /**
216  * A synonym for GRS80_GM<real>().
217  **********************************************************************/
218  static inline Math::real GRS80_GM() { return GRS80_GM<real>(); }
219  /**
220  * @tparam T the type of the returned value.
221  * @return the angular velocity of the GRS80 ellipsoid, &omega;, in rad
222  * s<sup>&minus;1</sup>.
223  *
224  * This is about 2 &pi; 366.25 / (365.25 &times; 24 &times; 3600) rad
225  * s<sup>&minus;1</sup>. 365.25 is the number of days in a Julian year and
226  * 365.35/366.25 converts from solar days to sidereal days. Using the
227  * number of days in a Gregorian year (365.2425) results in a worse
228  * approximation (because the Gregorian year includes the precession of the
229  * earth's axis).
230  **********************************************************************/
231  template<typename T> static inline T GRS80_omega()
232  { return 7292115 / (T(1000000) * 100000); }
233  /**
234  * A synonym for GRS80_omega<real>().
235  **********************************************************************/
236  static inline Math::real GRS80_omega() { return GRS80_omega<real>(); }
237  /**
238  * @tparam T the type of the returned value.
239  * @return the dynamical form factor of the GRS80 ellipsoid,
240  * <i>J</i><sub>2</sub>.
241  **********************************************************************/
242  template<typename T> static inline T GRS80_J2()
243  { return T(108263) / 100000000; }
244  /**
245  * A synonym for GRS80_J2<real>().
246  **********************************************************************/
247  static inline Math::real GRS80_J2() { return GRS80_J2<real>(); }
248  /**
249  * @tparam T the type of the returned value.
250  * @return the central scale factor for UTM (0.9996).
251  **********************************************************************/
252  template<typename T> static inline T UTM_k0()
253  {return T(9996) / 10000; }
254  /**
255  * A synonym for UTM_k0<real>().
256  **********************************************************************/
257  static inline Math::real UTM_k0() { return UTM_k0<real>(); }
258  /**
259  * @tparam T the type of the returned value.
260  * @return the central scale factor for UPS (0.994).
261  **********************************************************************/
262  template<typename T> static inline T UPS_k0()
263  { return T(994) / 1000; }
264  /**
265  * A synonym for UPS_k0<real>().
266  **********************************************************************/
267  static inline Math::real UPS_k0() { return UPS_k0<real>(); }
268  ///@}
269 
270  /** \name SI units
271  **********************************************************************/
272  ///@{
273  /**
274  * @tparam T the type of the returned value.
275  * @return the number of meters in a meter.
276  *
277  * This is unity, but this lets the internal system of units be changed if
278  * necessary.
279  **********************************************************************/
280  template<typename T> static inline T meter() { return T(1); }
281  /**
282  * A synonym for meter<real>().
283  **********************************************************************/
284  static inline Math::real meter() { return meter<real>(); }
285  /**
286  * @return the number of meters in a kilometer.
287  **********************************************************************/
288  static inline Math::real kilometer()
289  { return 1000 * meter<real>(); }
290  /**
291  * @return the number of meters in a nautical mile (approximately 1 arc
292  * minute)
293  **********************************************************************/
294  static inline Math::real nauticalmile()
295  { return 1852 * meter<real>(); }
296 
297  /**
298  * @tparam T the type of the returned value.
299  * @return the number of square meters in a square meter.
300  *
301  * This is unity, but this lets the internal system of units be changed if
302  * necessary.
303  **********************************************************************/
304  template<typename T> static inline T square_meter()
305  { return meter<real>() * meter<real>(); }
306  /**
307  * A synonym for square_meter<real>().
308  **********************************************************************/
309  static inline Math::real square_meter()
310  { return square_meter<real>(); }
311  /**
312  * @return the number of square meters in a hectare.
313  **********************************************************************/
314  static inline Math::real hectare()
315  { return 10000 * square_meter<real>(); }
316  /**
317  * @return the number of square meters in a square kilometer.
318  **********************************************************************/
319  static inline Math::real square_kilometer()
320  { return kilometer() * kilometer(); }
321  /**
322  * @return the number of square meters in a square nautical mile.
323  **********************************************************************/
325  { return nauticalmile() * nauticalmile(); }
326  ///@}
327 
328  /** \name Anachronistic British units
329  **********************************************************************/
330  ///@{
331  /**
332  * @return the number of meters in an international foot.
333  **********************************************************************/
334  static inline Math::real foot()
335  { return real(254 * 12) / 10000 * meter<real>(); }
336  /**
337  * @return the number of meters in a yard.
338  **********************************************************************/
339  static inline Math::real yard() { return 3 * foot(); }
340  /**
341  * @return the number of meters in a fathom.
342  **********************************************************************/
343  static inline Math::real fathom() { return 2 * yard(); }
344  /**
345  * @return the number of meters in a chain.
346  **********************************************************************/
347  static inline Math::real chain() { return 22 * yard(); }
348  /**
349  * @return the number of meters in a furlong.
350  **********************************************************************/
351  static inline Math::real furlong() { return 10 * chain(); }
352  /**
353  * @return the number of meters in a statute mile.
354  **********************************************************************/
355  static inline Math::real mile() { return 8 * furlong(); }
356  /**
357  * @return the number of square meters in an acre.
358  **********************************************************************/
359  static inline Math::real acre() { return chain() * furlong(); }
360  /**
361  * @return the number of square meters in a square statute mile.
362  **********************************************************************/
363  static inline Math::real square_mile() { return mile() * mile(); }
364  ///@}
365 
366  /** \name Anachronistic US units
367  **********************************************************************/
368  ///@{
369  /**
370  * @return the number of meters in a US survey foot.
371  **********************************************************************/
372  static inline Math::real surveyfoot()
373  { return real(1200) / 3937 * meter<real>(); }
374  ///@}
375  };
376 
377  /**
378  * \brief Exception handling for %GeographicLib
379  *
380  * A class to handle exceptions. It's derived from std::runtime_error so it
381  * can be caught by the usual catch clauses.
382  *
383  * Example of use:
384  * \include example-GeographicErr.cpp
385  **********************************************************************/
386  class GeographicErr : public std::runtime_error {
387  public:
388 
389  /**
390  * Constructor
391  *
392  * @param[in] msg a string message, which is accessible in the catch
393  * clause via what().
394  **********************************************************************/
395  GeographicErr(const std::string& msg) : std::runtime_error(msg) {}
396  };
397 
398 } // namespace GeographicLib
399 
400 #endif // GEOGRAPHICLIB_CONSTANTS_HPP
static Math::real arcminute()
Definition: Constants.hpp:128
static Math::real mile()
Definition: Constants.hpp:355
static Math::real kilometer()
Definition: Constants.hpp:288
static Math::real yard()
Definition: Constants.hpp:339
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:90
static Math::real UPS_k0()
Definition: Constants.hpp:267
static Math::real square_nauticalmile()
Definition: Constants.hpp:324
static Math::real WGS84_omega()
Definition: Constants.hpp:184
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
static Math::real nauticalmile()
Definition: Constants.hpp:294
static Math::real arcsecond()
Definition: Constants.hpp:133
static Math::real foot()
Definition: Constants.hpp:334
static Math::real surveyfoot()
Definition: Constants.hpp:372
static Math::real furlong()
Definition: Constants.hpp:351
static Math::real hectare()
Definition: Constants.hpp:314
static Math::real GRS80_omega()
Definition: Constants.hpp:236
static Math::real meter()
Definition: Constants.hpp:284
static Math::real degree()
Definition: Constants.hpp:124
static Math::real fathom()
Definition: Constants.hpp:343
static Math::real UTM_k0()
Definition: Constants.hpp:257
static Math::real acre()
Definition: Constants.hpp:359
Header for GeographicLib::Math class.
static Math::real chain()
Definition: Constants.hpp:347
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:230
static Math::real WGS84_GM()
Definition: Constants.hpp:173
static Math::real square_meter()
Definition: Constants.hpp:309
Constants needed by GeographicLib
Definition: Constants.hpp:115
static Math::real WGS84_a()
Definition: Constants.hpp:148
Exception handling for GeographicLib.
Definition: Constants.hpp:386
static Math::real square_kilometer()
Definition: Constants.hpp:319
static Math::real GRS80_a()
Definition: Constants.hpp:207
static Math::real square_mile()
Definition: Constants.hpp:363
static Math::real GRS80_GM()
Definition: Constants.hpp:218
static Math::real GRS80_J2()
Definition: Constants.hpp:247
static Math::real WGS84_f()
Definition: Constants.hpp:162
GeographicErr(const std::string &msg)
Definition: Constants.hpp:395