GeographicLib  1.47
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-2016) <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 // Use GEOGRAPHICLIB_DEPRECATED to mark functions, types or variables as
94 // deprecated. Code inspired by Apache Subversion's svn_types.h file (via
95 // MPFR).
96 #if defined(__GNUC__)
97 # if __GNUC__ > 4
98 # define GEOGRAPHICLIB_DEPRECATED(msg) __attribute__((deprecated(msg)))
99 # else
100 # define GEOGRAPHICLIB_DEPRECATED(msg) __attribute__((deprecated))
101 # endif
102 #elif defined(_MSC_VER) && _MSC_VER >= 1300
103 # define GEOGRAPHICLIB_DEPRECATED(msg) __declspec(deprecated(msg))
104 #else
105 # define GEOGRAPHICLIB_DEPRECATED(msg)
106 #endif
107 
108 #include <stdexcept>
109 #include <string>
110 #include <GeographicLib/Math.hpp>
111 
112 /**
113  * \brief Namespace for %GeographicLib
114  *
115  * All of %GeographicLib is defined within the GeographicLib namespace. In
116  * addition all the header files are included via %GeographicLib/Class.hpp.
117  * This minimizes the likelihood of conflicts with other packages.
118  **********************************************************************/
119 namespace GeographicLib {
120 
121  /**
122  * \brief %Constants needed by %GeographicLib
123  *
124  * Define constants specifying the WGS84 ellipsoid, the UTM and UPS
125  * projections, and various unit conversions.
126  *
127  * Example of use:
128  * \include example-Constants.cpp
129  **********************************************************************/
131  private:
132  typedef Math::real real;
133  Constants(); // Disable constructor
134 
135  public:
136  /**
137  * A synonym for Math::degree<real>().
138  **********************************************************************/
139  static inline Math::real degree() { return Math::degree(); }
140  /**
141  * @return the number of radians in an arcminute.
142  **********************************************************************/
143  static inline Math::real arcminute()
144  { return Math::degree() / 60; }
145  /**
146  * @return the number of radians in an arcsecond.
147  **********************************************************************/
148  static inline Math::real arcsecond()
149  { return Math::degree() / 3600; }
150 
151  /** \name Ellipsoid parameters
152  **********************************************************************/
153  ///@{
154  /**
155  * @tparam T the type of the returned value.
156  * @return the equatorial radius of WGS84 ellipsoid (6378137 m).
157  **********************************************************************/
158  template<typename T> static inline T WGS84_a()
159  { return 6378137 * meter<T>(); }
160  /**
161  * A synonym for WGS84_a<real>().
162  **********************************************************************/
163  static inline Math::real WGS84_a() { return WGS84_a<real>(); }
164  /**
165  * @tparam T the type of the returned value.
166  * @return the flattening of WGS84 ellipsoid (1/298.257223563).
167  **********************************************************************/
168  template<typename T> static inline T WGS84_f() {
169  // Evaluating this as 1000000000 / T(298257223563LL) reduces the
170  // round-off error by about 10%. However, expressing the flattening as
171  // 1/298.257223563 is well ingrained.
172  return 1 / ( T(298257223563LL) / 1000000000 );
173  }
174  /**
175  * A synonym for WGS84_f<real>().
176  **********************************************************************/
177  static inline Math::real WGS84_f() { return WGS84_f<real>(); }
178  /**
179  * @tparam T the type of the returned value.
180  * @return the gravitational constant of the WGS84 ellipsoid, \e GM, in
181  * m<sup>3</sup> s<sup>&minus;2</sup>.
182  **********************************************************************/
183  template<typename T> static inline T WGS84_GM()
184  { return T(3986004) * 100000000 + 41800000; }
185  /**
186  * A synonym for WGS84_GM<real>().
187  **********************************************************************/
188  static inline Math::real WGS84_GM() { return WGS84_GM<real>(); }
189  /**
190  * @tparam T the type of the returned value.
191  * @return the angular velocity of the WGS84 ellipsoid, &omega;, in rad
192  * s<sup>&minus;1</sup>.
193  **********************************************************************/
194  template<typename T> static inline T WGS84_omega()
195  { return 7292115 / (T(1000000) * 100000); }
196  /**
197  * A synonym for WGS84_omega<real>().
198  **********************************************************************/
199  static inline Math::real WGS84_omega() { return WGS84_omega<real>(); }
200  /**
201  * @tparam T the type of the returned value.
202  * @return the equatorial radius of GRS80 ellipsoid, \e a, in m.
203  **********************************************************************/
204  template<typename T> static inline T GRS80_a()
205  { return 6378137 * meter<T>(); }
206  /**
207  * A synonym for GRS80_a<real>().
208  **********************************************************************/
209  static inline Math::real GRS80_a() { return GRS80_a<real>(); }
210  /**
211  * @tparam T the type of the returned value.
212  * @return the gravitational constant of the GRS80 ellipsoid, \e GM, in
213  * m<sup>3</sup> s<sup>&minus;2</sup>.
214  **********************************************************************/
215  template<typename T> static inline T GRS80_GM()
216  { return T(3986005) * 100000000; }
217  /**
218  * A synonym for GRS80_GM<real>().
219  **********************************************************************/
220  static inline Math::real GRS80_GM() { return GRS80_GM<real>(); }
221  /**
222  * @tparam T the type of the returned value.
223  * @return the angular velocity of the GRS80 ellipsoid, &omega;, in rad
224  * s<sup>&minus;1</sup>.
225  *
226  * This is about 2 &pi; 366.25 / (365.25 &times; 24 &times; 3600) rad
227  * s<sup>&minus;1</sup>. 365.25 is the number of days in a Julian year and
228  * 365.35/366.25 converts from solar days to sidereal days. Using the
229  * number of days in a Gregorian year (365.2425) results in a worse
230  * approximation (because the Gregorian year includes the precession of the
231  * earth's axis).
232  **********************************************************************/
233  template<typename T> static inline T GRS80_omega()
234  { return 7292115 / (T(1000000) * 100000); }
235  /**
236  * A synonym for GRS80_omega<real>().
237  **********************************************************************/
238  static inline Math::real GRS80_omega() { return GRS80_omega<real>(); }
239  /**
240  * @tparam T the type of the returned value.
241  * @return the dynamical form factor of the GRS80 ellipsoid,
242  * <i>J</i><sub>2</sub>.
243  **********************************************************************/
244  template<typename T> static inline T GRS80_J2()
245  { return T(108263) / 100000000; }
246  /**
247  * A synonym for GRS80_J2<real>().
248  **********************************************************************/
249  static inline Math::real GRS80_J2() { return GRS80_J2<real>(); }
250  /**
251  * @tparam T the type of the returned value.
252  * @return the central scale factor for UTM (0.9996).
253  **********************************************************************/
254  template<typename T> static inline T UTM_k0()
255  {return T(9996) / 10000; }
256  /**
257  * A synonym for UTM_k0<real>().
258  **********************************************************************/
259  static inline Math::real UTM_k0() { return UTM_k0<real>(); }
260  /**
261  * @tparam T the type of the returned value.
262  * @return the central scale factor for UPS (0.994).
263  **********************************************************************/
264  template<typename T> static inline T UPS_k0()
265  { return T(994) / 1000; }
266  /**
267  * A synonym for UPS_k0<real>().
268  **********************************************************************/
269  static inline Math::real UPS_k0() { return UPS_k0<real>(); }
270  ///@}
271 
272  /** \name SI units
273  **********************************************************************/
274  ///@{
275  /**
276  * @tparam T the type of the returned value.
277  * @return the number of meters in a meter.
278  *
279  * This is unity, but this lets the internal system of units be changed if
280  * necessary.
281  **********************************************************************/
282  template<typename T> static inline T meter() { return T(1); }
283  /**
284  * A synonym for meter<real>().
285  **********************************************************************/
286  static inline Math::real meter() { return meter<real>(); }
287  /**
288  * @return the number of meters in a kilometer.
289  **********************************************************************/
290  static inline Math::real kilometer()
291  { return 1000 * meter<real>(); }
292  /**
293  * @return the number of meters in a nautical mile (approximately 1 arc
294  * minute)
295  **********************************************************************/
296  static inline Math::real nauticalmile()
297  { return 1852 * meter<real>(); }
298 
299  /**
300  * @tparam T the type of the returned value.
301  * @return the number of square meters in a square meter.
302  *
303  * This is unity, but this lets the internal system of units be changed if
304  * necessary.
305  **********************************************************************/
306  template<typename T> static inline T square_meter()
307  { return meter<real>() * meter<real>(); }
308  /**
309  * A synonym for square_meter<real>().
310  **********************************************************************/
311  static inline Math::real square_meter()
312  { return square_meter<real>(); }
313  /**
314  * @return the number of square meters in a hectare.
315  **********************************************************************/
316  static inline Math::real hectare()
317  { return 10000 * square_meter<real>(); }
318  /**
319  * @return the number of square meters in a square kilometer.
320  **********************************************************************/
321  static inline Math::real square_kilometer()
322  { return kilometer() * kilometer(); }
323  /**
324  * @return the number of square meters in a square nautical mile.
325  **********************************************************************/
327  { return nauticalmile() * nauticalmile(); }
328  ///@}
329 
330  /** \name Anachronistic British units
331  **********************************************************************/
332  ///@{
333  /**
334  * @return the number of meters in an international foot.
335  **********************************************************************/
336  static inline Math::real foot()
337  { return real(254 * 12) / 10000 * meter<real>(); }
338  /**
339  * @return the number of meters in a yard.
340  **********************************************************************/
341  static inline Math::real yard() { return 3 * foot(); }
342  /**
343  * @return the number of meters in a fathom.
344  **********************************************************************/
345  static inline Math::real fathom() { return 2 * yard(); }
346  /**
347  * @return the number of meters in a chain.
348  **********************************************************************/
349  static inline Math::real chain() { return 22 * yard(); }
350  /**
351  * @return the number of meters in a furlong.
352  **********************************************************************/
353  static inline Math::real furlong() { return 10 * chain(); }
354  /**
355  * @return the number of meters in a statute mile.
356  **********************************************************************/
357  static inline Math::real mile() { return 8 * furlong(); }
358  /**
359  * @return the number of square meters in an acre.
360  **********************************************************************/
361  static inline Math::real acre() { return chain() * furlong(); }
362  /**
363  * @return the number of square meters in a square statute mile.
364  **********************************************************************/
365  static inline Math::real square_mile() { return mile() * mile(); }
366  ///@}
367 
368  /** \name Anachronistic US units
369  **********************************************************************/
370  ///@{
371  /**
372  * @return the number of meters in a US survey foot.
373  **********************************************************************/
374  static inline Math::real surveyfoot()
375  { return real(1200) / 3937 * meter<real>(); }
376  ///@}
377  };
378 
379  /**
380  * \brief Exception handling for %GeographicLib
381  *
382  * A class to handle exceptions. It's derived from std::runtime_error so it
383  * can be caught by the usual catch clauses.
384  *
385  * Example of use:
386  * \include example-GeographicErr.cpp
387  **********************************************************************/
388  class GeographicErr : public std::runtime_error {
389  public:
390 
391  /**
392  * Constructor
393  *
394  * @param[in] msg a string message, which is accessible in the catch
395  * clause via what().
396  **********************************************************************/
397  GeographicErr(const std::string& msg) : std::runtime_error(msg) {}
398  };
399 
400 } // namespace GeographicLib
401 
402 #endif // GEOGRAPHICLIB_CONSTANTS_HPP
static Math::real arcminute()
Definition: Constants.hpp:143
static Math::real mile()
Definition: Constants.hpp:357
static Math::real kilometer()
Definition: Constants.hpp:290
static Math::real yard()
Definition: Constants.hpp:341
#define GEOGRAPHICLIB_EXPORT
Definition: Constants.hpp:90
static Math::real UPS_k0()
Definition: Constants.hpp:269
static Math::real square_nauticalmile()
Definition: Constants.hpp:326
static Math::real WGS84_omega()
Definition: Constants.hpp:199
GeographicLib::Math::real real
Definition: GeodSolve.cpp:31
static Math::real nauticalmile()
Definition: Constants.hpp:296
static Math::real arcsecond()
Definition: Constants.hpp:148
static Math::real foot()
Definition: Constants.hpp:336
static Math::real surveyfoot()
Definition: Constants.hpp:374
static Math::real furlong()
Definition: Constants.hpp:353
static Math::real hectare()
Definition: Constants.hpp:316
static Math::real GRS80_omega()
Definition: Constants.hpp:238
static Math::real meter()
Definition: Constants.hpp:286
static Math::real degree()
Definition: Constants.hpp:139
static Math::real fathom()
Definition: Constants.hpp:345
static Math::real UTM_k0()
Definition: Constants.hpp:259
static Math::real acre()
Definition: Constants.hpp:361
Header for GeographicLib::Math class.
static Math::real chain()
Definition: Constants.hpp:349
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T degree()
Definition: Math.hpp:216
static Math::real WGS84_GM()
Definition: Constants.hpp:188
static Math::real square_meter()
Definition: Constants.hpp:311
Constants needed by GeographicLib
Definition: Constants.hpp:130
static Math::real WGS84_a()
Definition: Constants.hpp:163
Exception handling for GeographicLib.
Definition: Constants.hpp:388
static Math::real square_kilometer()
Definition: Constants.hpp:321
static Math::real GRS80_a()
Definition: Constants.hpp:209
static Math::real square_mile()
Definition: Constants.hpp:365
static Math::real GRS80_GM()
Definition: Constants.hpp:220
static Math::real GRS80_J2()
Definition: Constants.hpp:249
static Math::real WGS84_f()
Definition: Constants.hpp:177
GeographicErr(const std::string &msg)
Definition: Constants.hpp:397