GeographicLib  1.42
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-2011) <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  { return 1 / ( T(298257223563LL) / 1000000000 ); }
155  /**
156  * A synonym for WGS84_f<real>().
157  **********************************************************************/
158  static inline Math::real WGS84_f() { return WGS84_f<real>(); }
159  /**
160  * @tparam T the type of the returned value.
161  * @return the gravitational constant of the WGS84 ellipsoid, \e GM, in
162  * m<sup>3</sup> s<sup>&minus;2</sup>.
163  **********************************************************************/
164  template<typename T> static inline T WGS84_GM()
165  { return T(3986004) * 100000000 + 41800000; }
166  /**
167  * A synonym for WGS84_GM<real>().
168  **********************************************************************/
169  static inline Math::real WGS84_GM() { return WGS84_GM<real>(); }
170  /**
171  * @tparam T the type of the returned value.
172  * @return the angular velocity of the WGS84 ellipsoid, &omega;, in rad
173  * s<sup>&minus;1</sup>.
174  **********************************************************************/
175  template<typename T> static inline T WGS84_omega()
176  { return 7292115 / (T(1000000) * 100000); }
177  /**
178  * A synonym for WGS84_omega<real>().
179  **********************************************************************/
180  static inline Math::real WGS84_omega() { return WGS84_omega<real>(); }
181  /// \cond SKIP
182  /**
183  * <b>DEPRECATED</b>
184  * @return the reciprocal flattening of WGS84 ellipsoid.
185  **********************************************************************/
186  template<typename T> static inline T WGS84_r()
187  { return 1/WGS84_f<T>(); }
188  /**
189  * <b>DEPRECATED</b>
190  * A synonym for WGS84_r<real>().
191  **********************************************************************/
192  static inline Math::real WGS84_r() { return WGS84_r<real>(); }
193  /// \endcond
194  /**
195  * @tparam T the type of the returned value.
196  * @return the equatorial radius of GRS80 ellipsoid, \e a, in m.
197  **********************************************************************/
198  template<typename T> static inline T GRS80_a()
199  { return 6378137 * meter<T>(); }
200  /**
201  * A synonym for GRS80_a<real>().
202  **********************************************************************/
203  static inline Math::real GRS80_a() { return GRS80_a<real>(); }
204  /**
205  * @tparam T the type of the returned value.
206  * @return the gravitational constant of the GRS80 ellipsoid, \e GM, in
207  * m<sup>3</sup> s<sup>&minus;2</sup>.
208  **********************************************************************/
209  template<typename T> static inline T GRS80_GM()
210  { return T(3986005) * 100000000; }
211  /**
212  * A synonym for GRS80_GM<real>().
213  **********************************************************************/
214  static inline Math::real GRS80_GM() { return GRS80_GM<real>(); }
215  /**
216  * @tparam T the type of the returned value.
217  * @return the angular velocity of the GRS80 ellipsoid, &omega;, in rad
218  * s<sup>&minus;1</sup>.
219  *
220  * This is about 2 &pi; 366.25 / (365.25 &times; 24 &times; 3600) rad
221  * s<sup>&minus;1</sup>. 365.25 is the number of days in a Julian year and
222  * 365.35/366.25 converts from solar days to sidereal days. Using the
223  * number of days in a Gregorian year (365.2425) results in a worse
224  * approximation (because the Gregorian year includes the precession of the
225  * earth's axis).
226  **********************************************************************/
227  template<typename T> static inline T GRS80_omega()
228  { return 7292115 / (T(1000000) * 100000); }
229  /**
230  * A synonym for GRS80_omega<real>().
231  **********************************************************************/
232  static inline Math::real GRS80_omega() { return GRS80_omega<real>(); }
233  /**
234  * @tparam T the type of the returned value.
235  * @return the dynamical form factor of the GRS80 ellipsoid,
236  * <i>J</i><sub>2</sub>.
237  **********************************************************************/
238  template<typename T> static inline T GRS80_J2()
239  { return T(108263) / 100000000; }
240  /**
241  * A synonym for GRS80_J2<real>().
242  **********************************************************************/
243  static inline Math::real GRS80_J2() { return GRS80_J2<real>(); }
244  /**
245  * @tparam T the type of the returned value.
246  * @return the central scale factor for UTM (0.9996).
247  **********************************************************************/
248  template<typename T> static inline T UTM_k0()
249  {return T(9996) / 10000; }
250  /**
251  * A synonym for UTM_k0<real>().
252  **********************************************************************/
253  static inline Math::real UTM_k0() { return UTM_k0<real>(); }
254  /**
255  * @tparam T the type of the returned value.
256  * @return the central scale factor for UPS (0.994).
257  **********************************************************************/
258  template<typename T> static inline T UPS_k0()
259  { return T(994) / 1000; }
260  /**
261  * A synonym for UPS_k0<real>().
262  **********************************************************************/
263  static inline Math::real UPS_k0() { return UPS_k0<real>(); }
264  ///@}
265 
266  /** \name SI units
267  **********************************************************************/
268  ///@{
269  /**
270  * @tparam T the type of the returned value.
271  * @return the number of meters in a meter.
272  *
273  * This is unity, but this lets the internal system of units be changed if
274  * necessary.
275  **********************************************************************/
276  template<typename T> static inline T meter() { return T(1); }
277  /**
278  * A synonym for meter<real>().
279  **********************************************************************/
280  static inline Math::real meter() { return meter<real>(); }
281  /**
282  * @return the number of meters in a kilometer.
283  **********************************************************************/
284  static inline Math::real kilometer()
285  { return 1000 * meter<real>(); }
286  /**
287  * @return the number of meters in a nautical mile (approximately 1 arc
288  * minute)
289  **********************************************************************/
290  static inline Math::real nauticalmile()
291  { return 1852 * meter<real>(); }
292 
293  /**
294  * @tparam T the type of the returned value.
295  * @return the number of square meters in a square meter.
296  *
297  * This is unity, but this lets the internal system of units be changed if
298  * necessary.
299  **********************************************************************/
300  template<typename T> static inline T square_meter()
301  { return meter<real>() * meter<real>(); }
302  /**
303  * A synonym for square_meter<real>().
304  **********************************************************************/
305  static inline Math::real square_meter()
306  { return square_meter<real>(); }
307  /**
308  * @return the number of square meters in a hectare.
309  **********************************************************************/
310  static inline Math::real hectare()
311  { return 10000 * square_meter<real>(); }
312  /**
313  * @return the number of square meters in a square kilometer.
314  **********************************************************************/
315  static inline Math::real square_kilometer()
316  { return kilometer() * kilometer(); }
317  /**
318  * @return the number of square meters in a square nautical mile.
319  **********************************************************************/
321  { return nauticalmile() * nauticalmile(); }
322  ///@}
323 
324  /** \name Anachronistic British units
325  **********************************************************************/
326  ///@{
327  /**
328  * @return the number of meters in an international foot.
329  **********************************************************************/
330  static inline Math::real foot()
331  { return real(254 * 12) / 10000 * meter<real>(); }
332  /**
333  * @return the number of meters in a yard.
334  **********************************************************************/
335  static inline Math::real yard() { return 3 * foot(); }
336  /**
337  * @return the number of meters in a fathom.
338  **********************************************************************/
339  static inline Math::real fathom() { return 2 * yard(); }
340  /**
341  * @return the number of meters in a chain.
342  **********************************************************************/
343  static inline Math::real chain() { return 22 * yard(); }
344  /**
345  * @return the number of meters in a furlong.
346  **********************************************************************/
347  static inline Math::real furlong() { return 10 * chain(); }
348  /**
349  * @return the number of meters in a statute mile.
350  **********************************************************************/
351  static inline Math::real mile() { return 8 * furlong(); }
352  /**
353  * @return the number of square meters in an acre.
354  **********************************************************************/
355  static inline Math::real acre() { return chain() * furlong(); }
356  /**
357  * @return the number of square meters in a square statute mile.
358  **********************************************************************/
359  static inline Math::real square_mile() { return mile() * mile(); }
360  ///@}
361 
362  /** \name Anachronistic US units
363  **********************************************************************/
364  ///@{
365  /**
366  * @return the number of meters in a US survey foot.
367  **********************************************************************/
368  static inline Math::real surveyfoot()
369  { return real(1200) / 3937 * meter<real>(); }
370  ///@}
371  };
372 
373  /**
374  * \brief Exception handling for %GeographicLib
375  *
376  * A class to handle exceptions. It's derived from std::runtime_error so it
377  * can be caught by the usual catch clauses.
378  *
379  * Example of use:
380  * \include example-GeographicErr.cpp
381  **********************************************************************/
382  class GeographicErr : public std::runtime_error {
383  public:
384 
385  /**
386  * Constructor
387  *
388  * @param[in] msg a string message, which is accessible in the catch
389  * clause via what().
390  **********************************************************************/
391  GeographicErr(const std::string& msg) : std::runtime_error(msg) {}
392  };
393 
394 } // namespace GeographicLib
395 
396 #endif // GEOGRAPHICLIB_CONSTANTS_HPP
static Math::real arcminute()
Definition: Constants.hpp:128
static Math::real mile()
Definition: Constants.hpp:351
static Math::real kilometer()
Definition: Constants.hpp:284
static Math::real yard()
Definition: Constants.hpp:335
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:90
static Math::real UPS_k0()
Definition: Constants.hpp:263
static Math::real square_nauticalmile()
Definition: Constants.hpp:320
static Math::real WGS84_omega()
Definition: Constants.hpp:180
GeographicLib::Math::real real
Definition: GeodSolve.cpp:32
static Math::real nauticalmile()
Definition: Constants.hpp:290
static Math::real arcsecond()
Definition: Constants.hpp:133
static Math::real foot()
Definition: Constants.hpp:330
static Math::real surveyfoot()
Definition: Constants.hpp:368
static Math::real furlong()
Definition: Constants.hpp:347
static Math::real hectare()
Definition: Constants.hpp:310
static Math::real GRS80_omega()
Definition: Constants.hpp:232
static Math::real meter()
Definition: Constants.hpp:280
static Math::real degree()
Definition: Constants.hpp:124
static Math::real fathom()
Definition: Constants.hpp:339
static Math::real UTM_k0()
Definition: Constants.hpp:253
static Math::real acre()
Definition: Constants.hpp:355
Header for GeographicLib::Math class.
static Math::real chain()
Definition: Constants.hpp:343
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:228
static Math::real WGS84_GM()
Definition: Constants.hpp:169
static Math::real square_meter()
Definition: Constants.hpp:305
Constants needed by GeographicLib
Definition: Constants.hpp:115
static Math::real WGS84_a()
Definition: Constants.hpp:148
Exception handling for GeographicLib.
Definition: Constants.hpp:382
static Math::real square_kilometer()
Definition: Constants.hpp:315
static Math::real GRS80_a()
Definition: Constants.hpp:203
static Math::real square_mile()
Definition: Constants.hpp:359
static Math::real GRS80_GM()
Definition: Constants.hpp:214
static Math::real GRS80_J2()
Definition: Constants.hpp:243
static Math::real WGS84_f()
Definition: Constants.hpp:158
GeographicErr(const std::string &msg)
Definition: Constants.hpp:391