1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // This file is manually converted from PROJ4
4 // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // 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)
15 // Original copyright notice:
17 // Permission is hereby granted, free of charge, to any person obtaining a
18 // copy of this software and associated documentation files (the "Software"),
19 // to deal in the Software without restriction, including without limitation
20 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 // and/or sell copies of the Software, and to permit persons to whom the
22 // Software is furnished to do so, subject to the following conditions:
24 // The above copyright notice and this permission notice shall be included
25 // in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 // DEALINGS IN THE SOFTWARE.
35 #ifndef BOOST_GEOMETRY_PROJECTIONS_PROJ_MDIST_HPP
36 #define BOOST_GEOMETRY_PROJECTIONS_PROJ_MDIST_HPP
39 #include <boost/geometry/util/math.hpp>
42 namespace boost { namespace geometry { namespace projections
46 static const int MDIST_MAX_ITER = 20;
57 template <typename CT>
58 inline bool proj_mdist_ini(CT const& es, MDIST<CT>& b)
60 CT numf, numfi, twon1, denf, denfi, ens, T, twon;
65 /* generate E(e^2) and its terms E[] */
67 numf = twon1 = denfi = 1.;
71 for (i = 1; i < MDIST_MAX_ITER ; ++i)
73 numf *= (twon1 * twon1);
74 den = twon * denf * denf * twon1;
76 Es -= (E[i] = T * ens);
81 if (Es == El) /* jump out if no change */
88 /* generate b_n coefficients--note: collapse with prefix ratios */
89 b.b[0] = Es = 1. - Es;
93 for (j = 1; j < i; ++j)
98 b.b[j] = Es * numf / denf;
105 template <typename T>
106 inline T proj_mdist(T const& phi, T const& sphi, T const& cphi, MDIST<T> const& b)
113 D = phi * b.E - b.es * sc / sqrt(1. - b.es * sphi2);
115 while (i) sum = b.b[--i] + sphi2 * sum;
116 return(D + sc * sum);
119 template <typename T>
120 inline T proj_inv_mdist(T const& dist, MDIST<T> const& b)
122 static const T TOL = 1e-14;
131 t = 1. - b.es * s * s;
132 phi -= t = (proj_mdist(phi, s, cos(phi), b) - dist) *
134 if (geometry::math::abs(t) < TOL) /* that is no change */
137 /* convergence failed */
138 BOOST_THROW_EXCEPTION( projection_exception(-17) );
140 } // namespace detail
142 }}} // namespace boost::geometry::projections