GeographicLib  1.21
Constants.hpp
Go to the documentation of this file.
00001 /**
00002  * \file Constants.hpp
00003  * \brief Header for GeographicLib::Constants class
00004  *
00005  * Copyright (c) Charles Karney (2008-2011) <charles@karney.com> and licensed
00006  * under the MIT/X11 License.  For more information, see
00007  * http://geographiclib.sourceforge.net/
00008  **********************************************************************/
00009 
00010 #if !defined(GEOGRAPHICLIB_CONSTANTS_HPP)
00011 #define GEOGRAPHICLIB_CONSTANTS_HPP \
00012   "$Id: 895e4bd91979aae347436bbf6be37964f05f5b6f $"
00013 
00014 #include <GeographicLib/Config.h>
00015 
00016 /**
00017  * A compile-time assert.  Use C++11 static_assert, if available.
00018  **********************************************************************/
00019 #if !defined(STATIC_ASSERT)
00020 #  if defined(__GXX_EXPERIMENTAL_CXX0X__)
00021 #    define STATIC_ASSERT static_assert
00022 #  elif defined(_MSC_VER) && _MSC_VER >= 1600
00023 #    define STATIC_ASSERT static_assert
00024 #  else
00025 #    define STATIC_ASSERT(cond,reason) \
00026             { enum{ STATIC_ASSERT_ENUM = 1/int(cond) }; }
00027 #  endif
00028 #endif
00029 
00030 #if defined(__GNUC__)
00031 // Suppress "defined but not used" warnings
00032 #  define RCSID_DECL(x) namespace \
00033           { char VAR_ ## x [] __attribute__((used)) = x; }
00034 #else
00035 /**
00036  * Insertion of RCS Id strings into the object file.
00037  **********************************************************************/
00038 #  define RCSID_DECL(x) namespace { char VAR_ ## x [] = x; }
00039 #endif
00040 
00041 #if defined(_WIN32) && defined(GEOGRAPHIC_SHARED_LIB)
00042 #  if defined(Geographic_EXPORTS)
00043 #    define GEOGRAPHIC_EXPORT __declspec(dllexport)
00044 #  else
00045 #    define GEOGRAPHIC_EXPORT __declspec(dllimport)
00046 #  endif
00047 #else
00048 #  define GEOGRAPHIC_EXPORT
00049 #endif
00050 
00051 #include <stdexcept>
00052 #include <GeographicLib/Math.hpp>
00053 
00054 /**
00055  * \brief Namespace for %GeographicLib
00056  *
00057  * All of %GeographicLib is defined within the GeographicLib namespace.  In
00058  * addition all the header files are included via %GeographicLib/filename.
00059  * This minimizes the likelihood of conflicts with other packages.
00060  **********************************************************************/
00061 namespace GeographicLib {
00062 
00063   /**
00064    * \brief %Constants needed by %GeographicLib
00065    *
00066    * Define constants specifying the WGS84 ellipsoid, the UTM and UPS
00067    * projections, and various unit conversions.
00068    *
00069    * Example of use:
00070    * \include example-Constants.cpp
00071    **********************************************************************/
00072   class GEOGRAPHIC_EXPORT Constants {
00073   private:
00074     typedef Math::real real;
00075     Constants();                // Disable constructor
00076 
00077   public:
00078     /**
00079      * A synonym for Math::degree<real>().
00080      **********************************************************************/
00081     static inline Math::real degree() throw() { return Math::degree<real>(); }
00082     /**
00083      * @return the number of radians in an arcminute.
00084      **********************************************************************/
00085     static inline Math::real arcminute() throw()
00086     { return Math::degree<real>() / 60; }
00087     /**
00088      * @return the number of radians in an arcsecond.
00089      **********************************************************************/
00090     static inline Math::real arcsecond() throw()
00091     { return Math::degree<real>() / 3600; }
00092 
00093     /** \name Ellipsoid parameters
00094      **********************************************************************/
00095     ///@{
00096     /**
00097      * @tparam T the type of the returned value.
00098      * @return the equatorial radius of WGS84 ellipsoid (6378137 m).
00099      **********************************************************************/
00100     template<typename T> static inline T WGS84_a() throw()
00101     { return T(6378137) * meter<T>(); }
00102     /**
00103      * A synonym for WGS84_a<real>().
00104      **********************************************************************/
00105     static inline Math::real WGS84_a() throw() { return WGS84_a<real>(); }
00106     /**
00107      * @tparam T the type of the returned value.
00108      * @return the flattening of WGS84 ellipsoid (1/298.257223563).
00109      **********************************************************************/
00110     template<typename T> static inline T WGS84_f() throw()
00111     { return T(1) / ( T(298) + T(257223563) / T(1000000000) ); }
00112     /**
00113      * A synonym for WGS84_f<real>().
00114      **********************************************************************/
00115     static inline Math::real WGS84_f() throw() { return WGS84_f<real>(); }
00116     /**
00117      * @tparam T the type of the returned value.
00118      * @return the gravitational constant of the WGS84 ellipsoid, \e GM, in
00119      *   m<sup>3</sup> s<sup>-2</sup>.
00120      **********************************************************************/
00121     template<typename T> static inline T WGS84_GM() throw()
00122     { return T(3986004) * T(100000000) + T(41800000); }
00123     /**
00124      * @tparam T the type of the returned value.
00125      * @return the angular velocity of the the WGS84 ellipsoid, \e omega, in
00126      *   rad s<sup>-1</sup>.
00127      **********************************************************************/
00128     template<typename T> static inline T WGS84_omega() throw()
00129     { return T(7292115) / (T(1000000) * T(100000)); }
00130     /// \cond SKIP
00131     /**
00132      * <b>DEPRECATED</b>
00133      * @return the reciprocal flattening of WGS84 ellipsoid.
00134      **********************************************************************/
00135     template<typename T> static inline T WGS84_r() throw()
00136     { return 1/WGS84_f<T>(); }
00137     /**
00138      * <b>DEPRECATED</b>
00139      * A synonym for WGS84_r<real>().
00140      **********************************************************************/
00141     /// \endcond
00142     static inline Math::real WGS84_r() throw() { return WGS84_r<real>(); }
00143     /**
00144      * @tparam T the type of the returned value.
00145      * @return the equatorial radius of GRS80 ellipsoid, \e a, in m.
00146      **********************************************************************/
00147     template<typename T> static inline T GRS80_a() throw()
00148     { return T(6378137); }
00149     /**
00150      * @tparam T the type of the returned value.
00151      * @return the gravitational constant of the GRS80 ellipsoid, \e GM, in
00152      *   m<sup>3</sup> s<sup>-2</sup>.
00153      **********************************************************************/
00154     template<typename T> static inline T GRS80_GM() throw()
00155     { return T(3986005) * T(100000000); }
00156     /**
00157      * @tparam T the type of the returned value.
00158      * @return the angular velocity of the the GRS80 ellipsoid, \e omega, in
00159      *   rad s<sup>-1</sup>.
00160      *
00161      * This is about 2*pi*366.25 / (365.25*24*3600) rad s<sup>-1</sup>.  365.25
00162      * is the number of days in a Julian year and 365.35/366.25 converts from
00163      * solar days to sidereal days.  Using the number of days in a Gregorian
00164      * year (365.2425) results in a worse approximation (because the Gregorian
00165      * year includes the precession of the earth's axis).
00166      **********************************************************************/
00167     template<typename T> static inline T GRS80_omega() throw()
00168     { return T(7292115) / (T(1000000) * T(100000)); }
00169     /**
00170      * @tparam T the type of the returned value.
00171      * @return the dynamical form factor of the GRS80 ellipsoid,
00172      *   <i>J</i><sub>2</sub>.
00173      **********************************************************************/
00174     template<typename T> static inline T GRS80_J2() throw()
00175     { return T(108263) / T(100000000); }
00176     /**
00177      * @tparam T the type of the returned value.
00178      * @return the central scale factor for UTM (0.9996).
00179      **********************************************************************/
00180     template<typename T> static inline T UTM_k0() throw()
00181     {return T(9996) / T(10000); }
00182     /**
00183      * A synonym for UTM_k0<real>().
00184      **********************************************************************/
00185     static inline Math::real UTM_k0() throw() { return UTM_k0<real>(); }
00186     /**
00187      * @tparam T the type of the returned value.
00188      * @return the central scale factor for UPS (0.994).
00189      **********************************************************************/
00190     template<typename T> static inline T UPS_k0() throw()
00191     { return T(994) / T(1000); }
00192     /**
00193      * A synonym for UPS_k0<real>().
00194      **********************************************************************/
00195     static inline Math::real UPS_k0() throw() { return UPS_k0<real>(); }
00196     ///@}
00197 
00198     /** \name SI units
00199      **********************************************************************/
00200     ///@{
00201     /**
00202      * @tparam T the type of the returned value.
00203      * @return the number of meters in a meter.
00204      *
00205      * This is unity, but this lets the internal system of units be changed if
00206      * necessary.
00207      **********************************************************************/
00208     template<typename T> static inline T meter() throw() { return T(1); }
00209     /**
00210      * A synonym for meter<real>().
00211      **********************************************************************/
00212     static inline Math::real meter() throw() { return meter<real>(); }
00213     /**
00214      * @return the number of meters in a kilometer.
00215      **********************************************************************/
00216     static inline Math::real kilometer() throw()
00217     { return 1000 * meter<real>(); }
00218     /**
00219      * @return the number of meters in a nautical mile (approximately 1 arc
00220      *   minute)
00221      **********************************************************************/
00222     static inline Math::real nauticalmile() throw()
00223     { return 1852 * meter<real>(); }
00224 
00225     /**
00226      * @tparam T the type of the returned value.
00227      * @return the number of square meters in a square meter.
00228      *
00229      * This is unity, but this lets the internal system of units be changed if
00230      * necessary.
00231      **********************************************************************/
00232     template<typename T> static inline T square_meter() throw()
00233     { return meter<real>() * meter<real>(); }
00234     /**
00235      * A synonym for square_meter<real>().
00236      **********************************************************************/
00237     static inline Math::real square_meter() throw()
00238     { return square_meter<real>(); }
00239     /**
00240      * @return the number of square meters in a hectare.
00241      **********************************************************************/
00242     static inline Math::real hectare() throw()
00243     { return 10000 * square_meter<real>(); }
00244     /**
00245      * @return the number of square meters in a square kilometer.
00246      **********************************************************************/
00247     static inline Math::real square_kilometer() throw()
00248     { return kilometer() * kilometer(); }
00249     /**
00250      * @return the number of square meters in a square nautical mile.
00251      **********************************************************************/
00252     static inline Math::real square_nauticalmile() throw()
00253     { return nauticalmile() * nauticalmile(); }
00254     ///@}
00255 
00256     /** \name Anachronistic British units
00257      **********************************************************************/
00258     ///@{
00259     /**
00260      * @return the number of meters in an international foot.
00261      **********************************************************************/
00262     static inline Math::real foot() throw()
00263     { return real(0.0254L) * 12 * meter<real>(); }
00264     /**
00265      * @return the number of meters in a yard.
00266      **********************************************************************/
00267     static inline Math::real yard() throw() { return 3 * foot(); }
00268     /**
00269      * @return the number of meters in a fathom.
00270      **********************************************************************/
00271     static inline Math::real fathom() throw() { return 2 * yard(); }
00272     /**
00273      * @return the number of meters in a chain.
00274      **********************************************************************/
00275     static inline Math::real chain() throw() { return 22 * yard(); }
00276     /**
00277      * @return the number of meters in a furlong.
00278      **********************************************************************/
00279     static inline Math::real furlong() throw() { return 10 * chain(); }
00280     /**
00281      * @return the number of meters in a statute mile.
00282      **********************************************************************/
00283     static inline Math::real mile() throw() { return 8 * furlong(); }
00284     /**
00285      * @return the number of square meters in an acre.
00286      **********************************************************************/
00287     static inline Math::real acre() throw() { return chain() * furlong(); }
00288     /**
00289      * @return the number of square meters in a square statute mile.
00290      **********************************************************************/
00291     static inline Math::real square_mile() throw() { return mile() * mile(); }
00292     ///@}
00293 
00294     /** \name Anachronistic US units
00295      **********************************************************************/
00296     ///@{
00297     /**
00298      * @return the number of meters in a US survey foot.
00299      **********************************************************************/
00300     static inline Math::real surveyfoot() throw()
00301     { return real(1200) / real(3937) * meter<real>(); }
00302     ///@}
00303   };
00304 
00305   /**
00306    * \brief Exception handling for %GeographicLib
00307    *
00308    * A class to handle exceptions.  It's derived from std::runtime_error so it
00309    * can be caught by the usual catch clauses.
00310    *
00311    * Example of use:
00312    * \include example-GeographicErr.cpp
00313    **********************************************************************/
00314   class GeographicErr : public std::runtime_error {
00315   public:
00316 
00317     /**
00318      * Constructor
00319      *
00320      * @param[in] msg a string message, which is accessible in the catch
00321      *   clause, via what().
00322      **********************************************************************/
00323     GeographicErr(const std::string& msg) : std::runtime_error(msg) {}
00324   };
00325 
00326 } // namespace GeographicLib
00327 
00328 #endif  // GEOGRAPHICLIB_CONSTANTS_HPP