pj_gauss.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // This file is manually converted from PROJ4
  3. // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2017, 2018.
  5. // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  11. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  12. // PROJ4 is maintained by Frank Warmerdam
  13. // PROJ4 is converted to Geometry Library by Barend Gehrels (Geodan, Amsterdam)
  14. // Original copyright notice:
  15. // Permission is hereby granted, free of charge, to any person obtaining a
  16. // copy of this software and associated documentation files (the "Software"),
  17. // to deal in the Software without restriction, including without limitation
  18. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. // and/or sell copies of the Software, and to permit persons to whom the
  20. // Software is furnished to do so, subject to the following conditions:
  21. // The above copyright notice and this permission notice shall be included
  22. // in all copies or substantial portions of the Software.
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. #ifndef BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_GAUSS_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_GAUSS_HPP
  32. #include <boost/geometry/srs/projections/constants.hpp>
  33. #include <boost/geometry/srs/projections/exception.hpp>
  34. namespace boost { namespace geometry { namespace projections {
  35. namespace detail {
  36. template <typename T>
  37. struct gauss
  38. {
  39. T C;
  40. T K;
  41. T e;
  42. T ratexp;
  43. };
  44. template <typename T>
  45. inline T srat(T const& esinp, T const& exp)
  46. {
  47. return (math::pow((T(1) - esinp) / (T(1) + esinp), exp));
  48. }
  49. template <typename T>
  50. inline gauss<T> gauss_ini(T const& e, T const& phi0, T& chi, T& rc)
  51. {
  52. static const T fourth_pi = detail::fourth_pi<T>();
  53. using std::asin;
  54. using std::cos;
  55. using std::sin;
  56. using std::sqrt;
  57. using std::tan;
  58. T sphi = 0;
  59. T cphi = 0;
  60. T es = 0;
  61. gauss<T> en;
  62. es = e * e;
  63. en.e = e;
  64. sphi = sin(phi0);
  65. cphi = cos(phi0);
  66. cphi *= cphi;
  67. rc = sqrt(1.0 - es) / (1.0 - es * sphi * sphi);
  68. en.C = sqrt(1.0 + es * cphi * cphi / (1.0 - es));
  69. chi = asin(sphi / en.C);
  70. en.ratexp = 0.5 * en.C * e;
  71. en.K = tan(0.5 * chi + fourth_pi)
  72. / (math::pow(tan(T(0.5) * phi0 + fourth_pi), en.C) * srat(en.e * sphi, en.ratexp));
  73. return en;
  74. }
  75. template <typename T>
  76. inline void gauss_fwd(gauss<T> const& en, T& lam, T& phi)
  77. {
  78. static const T fourth_pi = detail::fourth_pi<T>();
  79. static const T half_pi = detail::half_pi<T>();
  80. phi = T(2) * atan(en.K * math::pow(tan(T(0.5) * phi + fourth_pi), en.C)
  81. * srat(en.e * sin(phi), en.ratexp) ) - half_pi;
  82. lam *= en.C;
  83. }
  84. template <typename T>
  85. inline void gauss_inv(gauss<T> const& en, T& lam, T& phi)
  86. {
  87. static const int max_iter = 20;
  88. static const T fourth_pi = detail::fourth_pi<T>();
  89. static const T half_pi = detail::half_pi<T>();
  90. static const T del_tol = 1e-14;
  91. lam /= en.C;
  92. const T num = math::pow(tan(T(0.5) * phi + fourth_pi) / en.K, T(1) / en.C);
  93. int i = 0;
  94. for (i = max_iter; i; --i)
  95. {
  96. const T elp_phi = 2.0 * atan(num * srat(en.e * sin(phi), - 0.5 * en.e)) - half_pi;
  97. if (geometry::math::abs(elp_phi - phi) < del_tol)
  98. {
  99. break;
  100. }
  101. phi = elp_phi;
  102. }
  103. /* convergence failed */
  104. if (!i)
  105. {
  106. BOOST_THROW_EXCEPTION( projection_exception(error_non_conv_inv_meri_dist) );
  107. }
  108. }
  109. } // namespace detail
  110. }}} // namespace boost::geometry::projections
  111. #endif // BOOST_GEOMETRY_PROJECTIONS_IMPL_PJ_GAUSS_HPP