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