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