Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:35:47

0001 // Boost.Geometry - gis-projections (based on PROJ4)
0002 
0003 // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
0004 // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
0005 
0006 // This file was modified by Oracle on 2017, 2018, 2019, 2022.
0007 // Modifications copyright (c) 2017-2022, Oracle and/or its affiliates.
0008 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle.
0009 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
0010 
0011 // Use, modification and distribution is subject to the Boost Software License,
0012 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0013 // http://www.boost.org/LICENSE_1_0.txt)
0014 
0015 // This file is converted from PROJ4, http://trac.osgeo.org/proj
0016 // PROJ4 is originally written by Gerald Evenden (then of the USGS)
0017 // PROJ4 is maintained by Frank Warmerdam
0018 // PROJ4 is converted to Boost.Geometry by Barend Gehrels
0019 
0020 // Last updated version of proj: 8.2.1
0021 
0022 // Original copyright notice:
0023 
0024 // Permission is hereby granted, free of charge, to any person obtaining a
0025 // copy of this software and associated documentation files (the "Software"),
0026 // to deal in the Software without restriction, including without limitation
0027 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
0028 // and/or sell copies of the Software, and to permit persons to whom the
0029 // Software is furnished to do so, subject to the following conditions:
0030 
0031 // The above copyright notice and this permission notice shall be included
0032 // in all copies or substantial portions of the Software.
0033 
0034 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0035 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0036 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0037 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0038 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0039 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
0040 // DEALINGS IN THE SOFTWARE.
0041 
0042 #ifndef BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
0043 #define BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
0044 
0045 #include <boost/geometry/srs/projections/impl/base_static.hpp>
0046 #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
0047 #include <boost/geometry/srs/projections/impl/projects.hpp>
0048 #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
0049 #include <boost/geometry/srs/projections/impl/function_overloads.hpp>
0050 #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
0051 
0052 #include <boost/geometry/util/condition.hpp>
0053 #include <boost/geometry/util/math.hpp>
0054 
0055 
0056 namespace boost { namespace geometry
0057 {
0058 
0059 namespace projections
0060 {
0061     #ifndef DOXYGEN_NO_DETAIL
0062     namespace detail { namespace tmerc
0063     {
0064 
0065         static const double epsilon10 = 1.e-10;
0066 
0067         /* Constant for "exact" transverse mercator */
0068         static const int proj_etmerc_order = 6;
0069 
0070         template <typename T>
0071         inline T FC1() { return 1.; }
0072         template <typename T>
0073         inline T FC2() { return .5; }
0074         template <typename T>
0075         inline T FC3() { return .16666666666666666666666666666666666666; }
0076         template <typename T>
0077         inline T FC4() { return .08333333333333333333333333333333333333; }
0078         template <typename T>
0079         inline T FC5() { return .05; }
0080         template <typename T>
0081         inline T FC6() { return .03333333333333333333333333333333333333; }
0082         template <typename T>
0083         inline T FC7() { return .02380952380952380952380952380952380952; }
0084         template <typename T>
0085         inline T FC8() { return .01785714285714285714285714285714285714; }
0086 
0087         template <typename T>
0088         struct par_tmerc
0089         {
0090             T    esp;
0091             T    ml0;
0092             detail::en<T> en;
0093         };
0094 
0095         // More exact: Poder/Engsager
0096         template <typename T>
0097         struct par_tmerc_exact
0098         {
0099             T    Qn;     /* Merid. quad., scaled to the projection */
0100             T    Zb;     /* Radius vector in polar coord. systems  */
0101             T    cgb[6]; /* Constants for Gauss -> Geo lat */
0102             T    cbg[6]; /* Constants for Geo lat -> Gauss */
0103             T    utg[6]; /* Constants for transv. merc. -> geo */
0104             T    gtu[6]; /* Constants for geo -> transv. merc. */
0105         };
0106 
0107         template <typename T, typename Parameters>
0108         struct base_tmerc_ellipsoid
0109         {
0110             par_tmerc<T> m_proj_parm;
0111 
0112             // FORWARD(e_forward)  ellipse
0113             // Project coordinates from geographic (lon, lat) to cartesian (x, y)
0114             inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
0115             {
0116                 static const T half_pi = detail::half_pi<T>();
0117                 static const T FC1 = tmerc::FC1<T>();
0118                 static const T FC2 = tmerc::FC2<T>();
0119                 static const T FC3 = tmerc::FC3<T>();
0120                 static const T FC4 = tmerc::FC4<T>();
0121                 static const T FC5 = tmerc::FC5<T>();
0122                 static const T FC6 = tmerc::FC6<T>();
0123                 static const T FC7 = tmerc::FC7<T>();
0124                 static const T FC8 = tmerc::FC8<T>();
0125 
0126                 T al, als, n, cosphi, sinphi, t;
0127 
0128                 /*
0129                     * Fail if our longitude is more than 90 degrees from the
0130                     * central meridian since the results are essentially garbage.
0131                     * Is error -20 really an appropriate return value?
0132                     *
0133                     *  http://trac.osgeo.org/proj/ticket/5
0134                     */
0135                 if( lp_lon < -half_pi || lp_lon > half_pi )
0136                 {
0137                     xy_x = HUGE_VAL;
0138                     xy_y = HUGE_VAL;
0139                     BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
0140                     return;
0141                 }
0142 
0143                 sinphi = sin(lp_lat);
0144                 cosphi = cos(lp_lat);
0145                 t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
0146                 t *= t;
0147                 al = cosphi * lp_lon;
0148                 als = al * al;
0149                 al /= sqrt(1. - par.es * sinphi * sinphi);
0150                 n = this->m_proj_parm.esp * cosphi * cosphi;
0151                 xy_x = par.k0 * al * (FC1 +
0152                     FC3 * als * (1. - t + n +
0153                     FC5 * als * (5. + t * (t - 18.) + n * (14. - 58. * t)
0154                     + FC7 * als * (61. + t * ( t * (179. - t) - 479. ) )
0155                     )));
0156                 xy_y = par.k0 * (pj_mlfn(lp_lat, sinphi, cosphi, this->m_proj_parm.en) - this->m_proj_parm.ml0 +
0157                     sinphi * al * lp_lon * FC2 * ( 1. +
0158                     FC4 * als * (5. - t + n * (9. + 4. * n) +
0159                     FC6 * als * (61. + t * (t - 58.) + n * (270. - 330 * t)
0160                     + FC8 * als * (1385. + t * ( t * (543. - t) - 3111.) )
0161                     ))));
0162             }
0163 
0164             // INVERSE(e_inverse)  ellipsoid
0165             // Project coordinates from cartesian (x, y) to geographic (lon, lat)
0166             inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
0167             {
0168                 static const T half_pi = detail::half_pi<T>();
0169                 static const T FC1 = tmerc::FC1<T>();
0170                 static const T FC2 = tmerc::FC2<T>();
0171                 static const T FC3 = tmerc::FC3<T>();
0172                 static const T FC4 = tmerc::FC4<T>();
0173                 static const T FC5 = tmerc::FC5<T>();
0174                 static const T FC6 = tmerc::FC6<T>();
0175                 static const T FC7 = tmerc::FC7<T>();
0176                 static const T FC8 = tmerc::FC8<T>();
0177 
0178                 T n, con, cosphi, d, ds, sinphi, t;
0179 
0180                 lp_lat = pj_inv_mlfn(this->m_proj_parm.ml0 + xy_y / par.k0, par.es, this->m_proj_parm.en);
0181                 if (fabs(lp_lat) >= half_pi) {
0182                     lp_lat = xy_y < 0. ? -half_pi : half_pi;
0183                     lp_lon = 0.;
0184                 } else {
0185                     sinphi = sin(lp_lat);
0186                     cosphi = cos(lp_lat);
0187                     t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
0188                     n = this->m_proj_parm.esp * cosphi * cosphi;
0189                     d = xy_x * sqrt(con = 1. - par.es * sinphi * sinphi) / par.k0;
0190                     con *= t;
0191                     t *= t;
0192                     ds = d * d;
0193                     lp_lat -= (con * ds / (1.-par.es)) * FC2 * (1. -
0194                         ds * FC4 * (5. + t * (3. - 9. *  n) + n * (1. - 4 * n) -
0195                         ds * FC6 * (61. + t * (90. - 252. * n +
0196                             45. * t) + 46. * n
0197                         - ds * FC8 * (1385. + t * (3633. + t * (4095. + 1574. * t)) )
0198                         )));
0199                     lp_lon = d*(FC1 -
0200                         ds*FC3*( 1. + 2.*t + n -
0201                         ds*FC5*(5. + t*(28. + 24.*t + 8.*n) + 6.*n
0202                         - ds * FC7 * (61. + t * (662. + t * (1320. + 720. * t)) )
0203                     ))) / cosphi;
0204                 }
0205             }
0206 
0207             static inline std::string get_name()
0208             {
0209                 return "tmerc_ellipsoid";
0210             }
0211 
0212         };
0213 
0214         template <typename T, typename Parameters>
0215         struct base_tmerc_ellipsoid_exact
0216         {
0217             par_tmerc_exact<T> m_proj_parm;
0218 
0219             static inline std::string get_name()
0220             {
0221                 return "tmerc_ellipsoid";
0222             }
0223 
0224             /* Helper functions for "exact" transverse mercator */
0225             inline
0226             static T gatg(const T *p1, int len_p1, T B, T cos_2B, T sin_2B)
0227             {
0228                 T h = 0, h1, h2 = 0;
0229 
0230                 const T two_cos_2B = 2*cos_2B;
0231                 const T* p = p1 + len_p1;
0232                 h1 = *--p;
0233                 while (p - p1) {
0234                     h = -h2 + two_cos_2B*h1 + *--p;
0235                     h2 = h1;
0236                     h1 = h;
0237                 }
0238                 return (B + h*sin_2B);
0239             }
0240 
0241             /* Complex Clenshaw summation */
0242             inline
0243             static T clenS(const T *a, int size,
0244                             T sin_arg_r, T cos_arg_r,
0245                             T sinh_arg_i, T cosh_arg_i,
0246                             T *R, T *I)
0247             {
0248                 T r, i, hr, hr1, hr2, hi, hi1, hi2;
0249 
0250                 /* arguments */
0251                 const T* p = a + size;
0252                 r =  2*cos_arg_r*cosh_arg_i;
0253                 i = -2*sin_arg_r*sinh_arg_i;
0254 
0255                 /* summation loop */
0256                 hi1 = hr1 = hi = 0;
0257                 hr = *--p;
0258                 for (; a - p;) {
0259                     hr2 = hr1;
0260                     hi2 = hi1;
0261                     hr1 = hr;
0262                     hi1 = hi;
0263                     hr  = -hr2 + r*hr1 - i*hi1 + *--p;
0264                     hi  = -hi2 + i*hr1 + r*hi1;
0265                 }
0266 
0267                 r   = sin_arg_r*cosh_arg_i;
0268                 i   = cos_arg_r*sinh_arg_i;
0269                 *R  = r*hr - i*hi;
0270                 *I  = r*hi + i*hr;
0271                 return *R;
0272             }
0273 
0274             /* Real Clenshaw summation */
0275             static T clens(const T *a, int size, T arg_r)
0276             {
0277                 T r, hr, hr1, hr2, cos_arg_r;
0278 
0279                 const T* p = a + size;
0280                 cos_arg_r  = cos(arg_r);
0281                 r          =  2*cos_arg_r;
0282 
0283                 /* summation loop */
0284                 hr1 = 0;
0285                 hr = *--p;
0286                 for (; a - p;) {
0287                     hr2 = hr1;
0288                     hr1 = hr;
0289                     hr  = -hr2 + r*hr1 + *--p;
0290                 }
0291                 return sin(arg_r)*hr;
0292             }
0293 
0294             /* Ellipsoidal, forward */
0295             //static PJ_XY exact_e_fwd (PJ_LP lp, PJ *P)
0296             inline void fwd(Parameters const& /*par*/,
0297                             T const& lp_lon,
0298                             T const& lp_lat,
0299                             T& xy_x, T& xy_y) const
0300             {
0301                 //PJ_XY xy = {0.0,0.0};
0302                 //const auto *Q = &(static_cast<struct tmerc_data*>(par.opaque)->exact);
0303 
0304                 /* ell. LAT, LNG -> Gaussian LAT, LNG */
0305                 T Cn  = gatg (this->m_proj_parm.cbg, proj_etmerc_order, lp_lat,
0306                     cos(2*lp_lat), sin(2*lp_lat));
0307                 /* Gaussian LAT, LNG -> compl. sph. LAT */
0308                 const T sin_Cn = sin (Cn);
0309                 const T cos_Cn = cos (Cn);
0310                 const T sin_Ce = sin (lp_lon);
0311                 const T cos_Ce = cos (lp_lon);
0312 
0313                 const T cos_Cn_cos_Ce = cos_Cn*cos_Ce;
0314                 Cn = atan2 (sin_Cn, cos_Cn_cos_Ce);
0315 
0316                 const T inv_denom_tan_Ce = 1. / hypot (sin_Cn, cos_Cn_cos_Ce);
0317                 const T tan_Ce = sin_Ce*cos_Cn * inv_denom_tan_Ce;
0318             #if 0
0319                 // Variant of the above: found not to be measurably faster
0320                 const T sin_Ce_cos_Cn = sin_Ce*cos_Cn;
0321                 const T denom = sqrt(1 - sin_Ce_cos_Cn * sin_Ce_cos_Cn);
0322                 const T tan_Ce = sin_Ce_cos_Cn / denom;
0323             #endif
0324 
0325                 /* compl. sph. N, E -> ell. norm. N, E */
0326                 T Ce = asinh ( tan_Ce );     /* Replaces: Ce  = log(tan(FORTPI + Ce*0.5)); */
0327 
0328             /*
0329             *  Non-optimized version:
0330             *  const T sin_arg_r  = sin(2*Cn);
0331             *  const T cos_arg_r  = cos(2*Cn);
0332             *
0333             *  Given:
0334             *      sin(2 * Cn) = 2 sin(Cn) cos(Cn)
0335             *          sin(atan(y)) = y / sqrt(1 + y^2)
0336             *          cos(atan(y)) = 1 / sqrt(1 + y^2)
0337             *      ==> sin(2 * Cn) = 2 tan_Cn / (1 + tan_Cn^2)
0338             *
0339             *      cos(2 * Cn) = 2cos^2(Cn) - 1
0340             *                  = 2 / (1 + tan_Cn^2) - 1
0341             */
0342                 const T two_inv_denom_tan_Ce = 2 * inv_denom_tan_Ce;
0343                 const T two_inv_denom_tan_Ce_square = two_inv_denom_tan_Ce * inv_denom_tan_Ce;
0344                 const T tmp_r = cos_Cn_cos_Ce * two_inv_denom_tan_Ce_square;
0345                 const T sin_arg_r  = sin_Cn * tmp_r;
0346                 const T cos_arg_r  = cos_Cn_cos_Ce * tmp_r - 1;
0347 
0348             /*
0349             *  Non-optimized version:
0350             *  const T sinh_arg_i = sinh(2*Ce);
0351             *  const T cosh_arg_i = cosh(2*Ce);
0352             *
0353             *  Given
0354             *      sinh(2 * Ce) = 2 sinh(Ce) cosh(Ce)
0355             *          sinh(asinh(y)) = y
0356             *          cosh(asinh(y)) = sqrt(1 + y^2)
0357             *      ==> sinh(2 * Ce) = 2 tan_Ce sqrt(1 + tan_Ce^2)
0358             *
0359             *      cosh(2 * Ce) = 2cosh^2(Ce) - 1
0360             *                   = 2 * (1 + tan_Ce^2) - 1
0361             *
0362             * and 1+tan_Ce^2 = 1 + sin_Ce^2 * cos_Cn^2 / (sin_Cn^2 + cos_Cn^2 * cos_Ce^2)
0363             *                = (sin_Cn^2 + cos_Cn^2 * cos_Ce^2 + sin_Ce^2 * cos_Cn^2) / (sin_Cn^2 + cos_Cn^2 * cos_Ce^2)
0364             *                = 1. / (sin_Cn^2 + cos_Cn^2 * cos_Ce^2)
0365             *                = inv_denom_tan_Ce^2
0366             *
0367             */
0368                 const T sinh_arg_i = tan_Ce * two_inv_denom_tan_Ce;
0369                 const T cosh_arg_i = two_inv_denom_tan_Ce_square - 1;
0370 
0371                 T dCn, dCe;
0372                 Cn += clenS (this->m_proj_parm.gtu, proj_etmerc_order,
0373                             sin_arg_r, cos_arg_r, sinh_arg_i, cosh_arg_i,
0374                             &dCn, &dCe);
0375                 Ce += dCe;
0376                 if (fabs (Ce) <= 2.623395162778) {
0377                     xy_y  = this->m_proj_parm.Qn * Cn + this->m_proj_parm.Zb;  /* Northing */
0378                     xy_x  = this->m_proj_parm.Qn * Ce;          /* Easting  */
0379                 } else {
0380                     BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
0381                     xy_x = xy_y = HUGE_VAL;
0382                 }
0383             }
0384 
0385 
0386             /* Ellipsoidal, inverse */
0387             inline void inv(Parameters const& /*par*/,
0388                             T const& xy_x,
0389                             T const& xy_y,
0390                             T& lp_lon,
0391                             T& lp_lat) const
0392             {
0393                 //PJ_LP lp = {0.0,0.0};
0394                 //const auto *Q = &(static_cast<struct tmerc_data*>(par.opaque)->exact);
0395 
0396                 /* normalize N, E */
0397                 T Cn = (xy_y - this->m_proj_parm.Zb)/this->m_proj_parm.Qn;
0398                 T Ce = xy_x/this->m_proj_parm.Qn;
0399 
0400                 if (fabs(Ce) <= 2.623395162778) { /* 150 degrees */
0401                     /* norm. N, E -> compl. sph. LAT, LNG */
0402                     const T sin_arg_r  = sin(2*Cn);
0403                     const T cos_arg_r  = cos(2*Cn);
0404 
0405                     //const T sinh_arg_i = sinh(2*Ce);
0406                     //const T cosh_arg_i = cosh(2*Ce);
0407                     const T exp_2_Ce = exp(2*Ce);
0408                     const T half_inv_exp_2_Ce = 0.5 / exp_2_Ce;
0409                     const T sinh_arg_i = 0.5 * exp_2_Ce - half_inv_exp_2_Ce;
0410                     const T cosh_arg_i = 0.5 * exp_2_Ce + half_inv_exp_2_Ce;
0411 
0412                     T dCn_ignored, dCe;
0413                     Cn += clenS(this->m_proj_parm.utg, proj_etmerc_order,
0414                                 sin_arg_r, cos_arg_r, sinh_arg_i, cosh_arg_i,
0415                                 &dCn_ignored, &dCe);
0416                     Ce += dCe;
0417 
0418                     /* compl. sph. LAT -> Gaussian LAT, LNG */
0419                     const T sin_Cn = sin (Cn);
0420                     const T cos_Cn = cos (Cn);
0421 
0422             #if 0
0423                     // Non-optimized version:
0424                     T sin_Ce, cos_Ce;
0425                     Ce = atan (sinh (Ce));  // Replaces: Ce = 2*(atan(exp(Ce)) - FORTPI);
0426                     sin_Ce = sin (Ce);
0427                     cos_Ce = cos (Ce);
0428                     Ce = atan2 (sin_Ce, cos_Ce*cos_Cn);
0429                     Cn = atan2 (sin_Cn*cos_Ce,  hypot (sin_Ce, cos_Ce*cos_Cn));
0430             #else
0431             /*
0432             *      One can divide both member of Ce = atan2(...) by cos_Ce, which gives:
0433             *      Ce     = atan2 (tan_Ce, cos_Cn) = atan2(sinh(Ce), cos_Cn)
0434             *
0435             *      and the same for Cn = atan2(...)
0436             *      Cn     = atan2 (sin_Cn, hypot (sin_Ce, cos_Ce*cos_Cn)/cos_Ce)
0437             *             = atan2 (sin_Cn, hypot (sin_Ce/cos_Ce, cos_Cn))
0438             *             = atan2 (sin_Cn, hypot (tan_Ce, cos_Cn))
0439             *             = atan2 (sin_Cn, hypot (sinhCe, cos_Cn))
0440             */
0441                     const T sinhCe = sinh (Ce);
0442                     Ce = atan2 (sinhCe, cos_Cn);
0443                     const T modulus_Ce = hypot (sinhCe, cos_Cn);
0444                     Cn = atan2 (sin_Cn, modulus_Ce);
0445             #endif
0446 
0447                     /* Gaussian LAT, LNG -> ell. LAT, LNG */
0448 
0449                     // Optimization of the computation of cos(2*Cn) and sin(2*Cn)
0450                     const T tmp = 2 * modulus_Ce / (sinhCe * sinhCe + 1);
0451                     const T sin_2_Cn = sin_Cn * tmp;
0452                     const T cos_2_Cn = tmp * modulus_Ce - 1.;
0453                     //const T cos_2_Cn = cos(2 * Cn);
0454                     //const T sin_2_Cn = sin(2 * Cn);
0455 
0456                     lp_lat = gatg (this->m_proj_parm.cgb,  proj_etmerc_order, Cn, cos_2_Cn, sin_2_Cn);
0457                     lp_lon = Ce;
0458                 }
0459                 else {
0460                     BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
0461                     lp_lat = lp_lon = HUGE_VAL;
0462                 }
0463             }
0464 
0465         };
0466 
0467         template <typename T, typename Parameters>
0468         struct base_tmerc_spheroid
0469         {
0470             par_tmerc<T> m_proj_parm;
0471 
0472             // FORWARD(s_forward)  sphere
0473             // Project coordinates from geographic (lon, lat) to cartesian (x, y)
0474             inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
0475             {
0476                 static const T half_pi = detail::half_pi<T>();
0477 
0478                 T b, cosphi;
0479 
0480                 /*
0481                     * Fail if our longitude is more than 90 degrees from the
0482                     * central meridian since the results are essentially garbage.
0483                     * Is error -20 really an appropriate return value?
0484                     *
0485                     *  http://trac.osgeo.org/proj/ticket/5
0486                     */
0487                 if( lp_lon < -half_pi || lp_lon > half_pi )
0488                 {
0489                     xy_x = HUGE_VAL;
0490                     xy_y = HUGE_VAL;
0491                     BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
0492                     return;
0493                 }
0494 
0495                 cosphi = cos(lp_lat);
0496                 b = cosphi * sin(lp_lon);
0497                 if (fabs(fabs(b) - 1.) <= epsilon10)
0498                     BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
0499 
0500                 xy_x = this->m_proj_parm.ml0 * log((1. + b) / (1. - b));
0501                 xy_y = cosphi * cos(lp_lon) / sqrt(1. - b * b);
0502 
0503                 b = fabs( xy_y );
0504                 if (b >= 1.) {
0505                     if ((b - 1.) > epsilon10)
0506                         BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
0507                     else xy_y = 0.;
0508                 } else
0509                     xy_y = acos(xy_y);
0510 
0511                 if (lp_lat < 0.)
0512                     xy_y = -xy_y;
0513                 xy_y = this->m_proj_parm.esp * (xy_y - par.phi0);
0514             }
0515 
0516             // INVERSE(s_inverse)  sphere
0517             // Project coordinates from cartesian (x, y) to geographic (lon, lat)
0518             inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
0519             {
0520                 T h, g;
0521 
0522                 h = exp(xy_x / this->m_proj_parm.esp);
0523                 g = .5 * (h - 1. / h);
0524                 h = cos(par.phi0 + xy_y / this->m_proj_parm.esp);
0525                 lp_lat = asin(sqrt((1. - h * h) / (1. + g * g)));
0526 
0527                 /* Make sure that phi is on the correct hemisphere when false northing is used */
0528                 if (xy_y < 0. && -lp_lat+par.phi0 < 0.0) lp_lat = -lp_lat;
0529 
0530                 lp_lon = (g != 0.0 || h != 0.0) ? atan2(g, h) : 0.;
0531             }
0532 
0533             static inline std::string get_name()
0534             {
0535                 return "tmerc_spheroid";
0536             }
0537 
0538         };
0539 
0540         template <typename Parameters, typename T>
0541         inline void setup(Parameters const& par, par_tmerc<T>& proj_parm)
0542         {
0543             if (par.es != 0.0) {
0544                 proj_parm.en = pj_enfn<T>(par.es);
0545                 proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
0546                 proj_parm.esp = par.es / (1. - par.es);
0547             } else {
0548                 proj_parm.esp = par.k0;
0549                 proj_parm.ml0 = .5 * proj_parm.esp;
0550             }
0551         }
0552 
0553         template <typename Parameters, typename T>
0554         inline void setup_exact(Parameters const& par, par_tmerc_exact<T>& proj_parm)
0555         {
0556             assert( par.es > 0 );
0557 
0558             /* third flattening n */
0559             //since we do not keep n in parameters we compute it here;
0560             const T n = pow(tan(asin(par.e)/2),2);
0561             T np = n;
0562 
0563             /* COEF. OF TRIG SERIES GEO <-> GAUSS */
0564             /* cgb := Gaussian -> Geodetic, KW p190 - 191 (61) - (62) */
0565             /* cbg := Geodetic -> Gaussian, KW p186 - 187 (51) - (52) */
0566             /* PROJ_ETMERC_ORDER = 6th degree : Engsager and Poder: ICC2007 */
0567 
0568             proj_parm.cgb[0] = n*( 2 + n*(-2/3.0  + n*(-2      + n*(116/45.0 + n*(26/45.0 +
0569                         n*(-2854/675.0 ))))));
0570             proj_parm.cbg[0] = n*(-2 + n*( 2/3.0  + n*( 4/3.0  + n*(-82/45.0 + n*(32/45.0 +
0571                         n*( 4642/4725.0))))));
0572             np     *= n;
0573             proj_parm.cgb[1] = np*(7/3.0 + n*( -8/5.0  + n*(-227/45.0 + n*(2704/315.0 +
0574                         n*( 2323/945.0)))));
0575             proj_parm.cbg[1] = np*(5/3.0 + n*(-16/15.0 + n*( -13/9.0  + n*( 904/315.0 +
0576                         n*(-1522/945.0)))));
0577             np     *= n;
0578             /* n^5 coeff corrected from 1262/105 -> -1262/105 */
0579             proj_parm.cgb[2] = np*( 56/15.0  + n*(-136/35.0 + n*(-1262/105.0 +
0580                         n*( 73814/2835.0))));
0581             proj_parm.cbg[2] = np*(-26/15.0  + n*(  34/21.0 + n*(    8/5.0   +
0582                         n*(-12686/2835.0))));
0583             np     *= n;
0584             /* n^5 coeff corrected from 322/35 -> 332/35 */
0585             proj_parm.cgb[3] = np*(4279/630.0 + n*(-332/35.0 + n*(-399572/14175.0)));
0586             proj_parm.cbg[3] = np*(1237/630.0 + n*( -12/5.0  + n*( -24832/14175.0)));
0587             np     *= n;
0588             proj_parm.cgb[4] = np*(4174/315.0 + n*(-144838/6237.0 ));
0589             proj_parm.cbg[4] = np*(-734/315.0 + n*( 109598/31185.0));
0590             np     *= n;
0591             proj_parm.cgb[5] = np*(601676/22275.0 );
0592             proj_parm.cbg[5] = np*(444337/155925.0);
0593 
0594             /* Constants of the projections */
0595             /* Transverse Mercator (UTM, ITM, etc) */
0596             np = n*n;
0597             /* Norm. mer. quad, K&W p.50 (96), p.19 (38b), p.5 (2) */
0598             proj_parm.Qn = par.k0/(1 + n) * (1 + np*(1/4.0 + np*(1/64.0 + np/256.0)));
0599             /* coef of trig series */
0600             /* utg := ell. N, E -> sph. N, E,  KW p194 (65) */
0601             /* gtu := sph. N, E -> ell. N, E,  KW p196 (69) */
0602             proj_parm.utg[0] = n*(-0.5  + n*( 2/3.0 + n*(-37/96.0 + n*( 1/360.0 +
0603                         n*(  81/512.0 + n*(-96199/604800.0))))));
0604             proj_parm.gtu[0] = n*( 0.5  + n*(-2/3.0 + n*(  5/16.0 + n*(41/180.0 +
0605                         n*(-127/288.0 + n*(  7891/37800.0 ))))));
0606             proj_parm.utg[1] = np*(-1/48.0 + n*(-1/15.0 + n*(437/1440.0 + n*(-46/105.0 +
0607                         n*( 1118711/3870720.0)))));
0608             proj_parm.gtu[1] = np*(13/48.0 + n*(-3/5.0  + n*(557/1440.0 + n*(281/630.0 +
0609                         n*(-1983433/1935360.0)))));
0610             np      *= n;
0611             proj_parm.utg[2] = np*(-17/480.0 + n*(  37/840.0 + n*(  209/4480.0  +
0612                         n*( -5569/90720.0 ))));
0613             proj_parm.gtu[2] = np*( 61/240.0 + n*(-103/140.0 + n*(15061/26880.0 +
0614                         n*(167603/181440.0))));
0615             np      *= n;
0616             proj_parm.utg[3] = np*(-4397/161280.0 + n*(  11/504.0 + n*( 830251/7257600.0)));
0617             proj_parm.gtu[3] = np*(49561/161280.0 + n*(-179/168.0 + n*(6601661/7257600.0)));
0618             np     *= n;
0619             proj_parm.utg[4] = np*(-4583/161280.0 + n*(  108847/3991680.0));
0620             proj_parm.gtu[4] = np*(34729/80640.0  + n*(-3418889/1995840.0));
0621             np     *= n;
0622             proj_parm.utg[5] = np*(-20648693/638668800.0);
0623             proj_parm.gtu[5] = np*(212378941/319334400.0);
0624 
0625             /* Gaussian latitude value of the origin latitude */
0626             const T Z = base_tmerc_ellipsoid_exact<T, Parameters>::gatg (proj_parm.cbg, proj_etmerc_order, par.phi0, cos(2*par.phi0), sin(2*par.phi0));
0627 
0628             /* Origin northing minus true northing at the origin latitude */
0629             /* i.e. true northing = N - par.Zb                         */
0630             proj_parm.Zb  = - proj_parm.Qn*(Z + base_tmerc_ellipsoid_exact<T, Parameters>::clens(proj_parm.gtu, proj_etmerc_order, 2*Z));
0631         }
0632 
0633     }} // namespace detail::tmerc
0634     #endif // doxygen
0635 
0636     /*!
0637         \brief Transverse Mercator projection
0638         \ingroup projections
0639         \tparam Geographic latlong point type
0640         \tparam Cartesian xy point type
0641         \tparam Parameters parameter type
0642         \par Projection characteristics
0643          - Cylindrical
0644          - Spheroid
0645          - Ellipsoid
0646         \par Example
0647         \image html ex_tmerc.gif
0648     */
0649     //approximate tmerc algorithm
0650     /*
0651     template <typename T, typename Parameters>
0652     struct tmerc_ellipsoid : public detail::tmerc::base_tmerc_ellipsoid<T, Parameters>
0653     {
0654         template <typename Params>
0655         inline tmerc_ellipsoid(Params const&, Parameters const& par)
0656         {
0657             detail::tmerc::setup(par, this->m_proj_parm);
0658         }
0659     };
0660     */
0661     template <typename T, typename Parameters>
0662     struct tmerc_ellipsoid : public detail::tmerc::base_tmerc_ellipsoid_exact<T, Parameters>
0663     {
0664         template <typename Params>
0665         inline tmerc_ellipsoid(Params const&, Parameters const& par)
0666         {
0667             detail::tmerc::setup_exact(par, this->m_proj_parm);
0668         }
0669     };
0670 
0671     /*!
0672         \brief Transverse Mercator projection
0673         \ingroup projections
0674         \tparam Geographic latlong point type
0675         \tparam Cartesian xy point type
0676         \tparam Parameters parameter type
0677         \par Projection characteristics
0678          - Cylindrical
0679          - Spheroid
0680          - Ellipsoid
0681         \par Example
0682         \image html ex_tmerc.gif
0683     */
0684     template <typename T, typename Parameters>
0685     struct tmerc_spheroid : public detail::tmerc::base_tmerc_spheroid<T, Parameters>
0686     {
0687         template <typename Params>
0688         inline tmerc_spheroid(Params const&, Parameters const& par)
0689         {
0690             detail::tmerc::setup(par, this->m_proj_parm);
0691         }
0692     };
0693 
0694     #ifndef DOXYGEN_NO_DETAIL
0695     namespace detail
0696     {
0697 
0698         // Static projection
0699         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_tmerc, tmerc_spheroid, tmerc_ellipsoid)
0700 
0701         // Factory entry(s) - dynamic projection
0702         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(tmerc_entry, tmerc_spheroid, tmerc_ellipsoid)
0703 
0704         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(tmerc_init)
0705         {
0706             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(tmerc, tmerc_entry)
0707         }
0708 
0709     } // namespace detail
0710     #endif // doxygen
0711 
0712 } // namespace projections
0713 
0714 }} // namespace boost::geometry
0715 
0716 #endif // BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
0717