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