pj_mlfn.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/util/math.hpp>
  39. namespace boost { namespace geometry { namespace projections {
  40. namespace detail {
  41. template <typename T>
  42. struct en
  43. {
  44. static const std::size_t size = 5;
  45. T const& operator[](size_t i) const { return data[i]; }
  46. T & operator[](size_t i) { return data[i]; }
  47. private:
  48. T data[5];
  49. };
  50. template <typename T>
  51. inline en<T> pj_enfn(T const& es)
  52. {
  53. static const T C00 = 1.;
  54. static const T C02 = .25;
  55. static const T C04 = .046875;
  56. static const T C06 = .01953125;
  57. static const T C08 = .01068115234375;
  58. static const T C22 = .75;
  59. static const T C44 = .46875;
  60. static const T C46 = .01302083333333333333;
  61. static const T C48 = .00712076822916666666;
  62. static const T C66 = .36458333333333333333;
  63. static const T C68 = .00569661458333333333;
  64. static const T C88 = .3076171875;
  65. T t;
  66. detail::en<T> en;
  67. {
  68. en[0] = C00 - es * (C02 + es * (C04 + es * (C06 + es * C08)));
  69. en[1] = es * (C22 - es * (C04 + es * (C06 + es * C08)));
  70. en[2] = (t = es * es) * (C44 - es * (C46 + es * C48));
  71. en[3] = (t *= es) * (C66 - es * C68);
  72. en[4] = t * es * C88;
  73. }
  74. return en;
  75. }
  76. template <typename T>
  77. inline T pj_mlfn(T const& phi, T sphi, T cphi, detail::en<T> const& en)
  78. {
  79. cphi *= sphi;
  80. sphi *= sphi;
  81. return(en[0] * phi - cphi * (en[1] + sphi*(en[2]
  82. + sphi*(en[3] + sphi*en[4]))));
  83. }
  84. template <typename T>
  85. inline T pj_inv_mlfn(T const& arg, T const& es, detail::en<T> const& en)
  86. {
  87. static const T EPS = 1e-11;
  88. static const int MAX_ITER = 10;
  89. T s, t, phi, k = 1./(1.-es);
  90. int i;
  91. phi = arg;
  92. for (i = MAX_ITER; i ; --i) { /* rarely goes over 2 iterations */
  93. s = sin(phi);
  94. t = 1. - es * s * s;
  95. phi -= t = (pj_mlfn(phi, s, cos(phi), en) - arg) * (t * sqrt(t)) * k;
  96. if (geometry::math::abs(t) < EPS)
  97. return phi;
  98. }
  99. BOOST_THROW_EXCEPTION( projection_exception(error_non_conv_inv_meri_dist) );
  100. return phi;
  101. }
  102. } // namespace detail
  103. }}} // namespace boost::geometry::projections
  104. #endif