pj_mlfn.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /* meridional distance for ellipsoid and inverse
  31. ** 8th degree - accurate to < 1e-5 meters when used in conjunction
  32. ** with typical major axis values.
  33. ** Inverse determines phi to EPS (1e-11) radians, about 1e-6 seconds.
  34. */
  35. #ifndef BOOST_GEOMETRY_PROJECTIONS_PJ_MLFN_HPP
  36. #define BOOST_GEOMETRY_PROJECTIONS_PJ_MLFN_HPP
  37. #include <cstdlib>
  38. #include <boost/geometry/srs/projections/exception.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_strerrno.hpp>
  40. #include <boost/geometry/util/math.hpp>
  41. namespace boost { namespace geometry { namespace projections {
  42. namespace detail {
  43. template <typename T>
  44. struct en
  45. {
  46. static const std::size_t size = 5;
  47. T const& operator[](size_t i) const { return data[i]; }
  48. T & operator[](size_t i) { return data[i]; }
  49. private:
  50. T data[5];
  51. };
  52. template <typename T>
  53. inline en<T> pj_enfn(T const& es)
  54. {
  55. static const T C00 = 1.;
  56. static const T C02 = .25;
  57. static const T C04 = .046875;
  58. static const T C06 = .01953125;
  59. static const T C08 = .01068115234375;
  60. static const T C22 = .75;
  61. static const T C44 = .46875;
  62. static const T C46 = .01302083333333333333;
  63. static const T C48 = .00712076822916666666;
  64. static const T C66 = .36458333333333333333;
  65. static const T C68 = .00569661458333333333;
  66. static const T C88 = .3076171875;
  67. T t;
  68. detail::en<T> en;
  69. {
  70. en[0] = C00 - es * (C02 + es * (C04 + es * (C06 + es * C08)));
  71. en[1] = es * (C22 - es * (C04 + es * (C06 + es * C08)));
  72. en[2] = (t = es * es) * (C44 - es * (C46 + es * C48));
  73. en[3] = (t *= es) * (C66 - es * C68);
  74. en[4] = t * es * C88;
  75. }
  76. return en;
  77. }
  78. template <typename T>
  79. inline T pj_mlfn(T const& phi, T sphi, T cphi, detail::en<T> const& en)
  80. {
  81. cphi *= sphi;
  82. sphi *= sphi;
  83. return(en[0] * phi - cphi * (en[1] + sphi*(en[2]
  84. + sphi*(en[3] + sphi*en[4]))));
  85. }
  86. template <typename T>
  87. inline T pj_inv_mlfn(T const& arg, T const& es, detail::en<T> const& en)
  88. {
  89. static const T EPS = 1e-11;
  90. static const int MAX_ITER = 10;
  91. T s, t, phi, k = 1./(1.-es);
  92. int i;
  93. phi = arg;
  94. for (i = MAX_ITER; i ; --i) { /* rarely goes over 2 iterations */
  95. s = sin(phi);
  96. t = 1. - es * s * s;
  97. phi -= t = (pj_mlfn(phi, s, cos(phi), en) - arg) * (t * sqrt(t)) * k;
  98. if (geometry::math::abs(t) < EPS)
  99. return phi;
  100. }
  101. BOOST_THROW_EXCEPTION( projection_exception(error_non_conv_inv_meri_dist) );
  102. return phi;
  103. }
  104. } // namespace detail
  105. }}} // namespace boost::geometry::projections
  106. #endif