Constants.hpp 13 KB

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