Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / geometry / srs / projections / proj / chamb.hpp
1 // Boost.Geometry - gis-projections (based on PROJ4)
2
3 // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
4
5 // This file was modified by Oracle on 2017, 2018, 2019.
6 // Modifications copyright (c) 2017-2019, Oracle and/or its affiliates.
7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
8
9 // Use, modification and distribution is subject to the Boost Software License,
10 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
12
13 // This file is converted from PROJ4, http://trac.osgeo.org/proj
14 // PROJ4 is originally written by Gerald Evenden (then of the USGS)
15 // PROJ4 is maintained by Frank Warmerdam
16 // PROJ4 is converted to Boost.Geometry by Barend Gehrels
17
18 // Last updated version of proj: 5.0.0
19
20 // Original copyright notice:
21
22 // Permission is hereby granted, free of charge, to any person obtaining a
23 // copy of this software and associated documentation files (the "Software"),
24 // to deal in the Software without restriction, including without limitation
25 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
26 // and/or sell copies of the Software, and to permit persons to whom the
27 // Software is furnished to do so, subject to the following conditions:
28
29 // The above copyright notice and this permission notice shall be included
30 // in all copies or substantial portions of the Software.
31
32 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
33 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
35 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
37 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
38 // DEALINGS IN THE SOFTWARE.
39
40 #ifndef BOOST_GEOMETRY_PROJECTIONS_CHAMB_HPP
41 #define BOOST_GEOMETRY_PROJECTIONS_CHAMB_HPP
42
43 #include <cstdio>
44
45 #include <boost/geometry/srs/projections/impl/aasincos.hpp>
46 #include <boost/geometry/srs/projections/impl/base_static.hpp>
47 #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
48 #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
49 #include <boost/geometry/srs/projections/impl/pj_param.hpp>
50 #include <boost/geometry/srs/projections/impl/projects.hpp>
51
52 #include <boost/geometry/util/math.hpp>
53
54 namespace boost { namespace geometry
55 {
56
57 namespace projections
58 {
59     #ifndef DOXYGEN_NO_DETAIL
60     namespace detail { namespace chamb
61     {
62
63             //static const double third = 0.333333333333333333;
64             static const double tolerance = 1e-9;
65
66             // specific for 'chamb'
67             template <typename T>
68             struct vect_ra { T r, Az; };
69             template <typename T>
70             struct point_xy { T x, y; };
71
72             template <typename T>
73             struct par_chamb
74             {
75                 struct { /* control point data */
76                     T phi, lam;
77                     T cosphi, sinphi;
78                     vect_ra<T> v;
79                     point_xy<T> p;
80                     T Az;
81                 } c[3];
82                 point_xy<T> p;
83                 T beta_0, beta_1, beta_2;
84             };
85
86             /* distance and azimuth from point 1 to point 2 */
87             template <typename T>
88             inline vect_ra<T> vect(T const& dphi, T const& c1, T const& s1, T const& c2, T const& s2, T const& dlam)
89             {
90                 vect_ra<T> v;
91                 T cdl, dp, dl;
92
93                 cdl = cos(dlam);
94                 if (fabs(dphi) > 1. || fabs(dlam) > 1.)
95                     v.r = aacos(s1 * s2 + c1 * c2 * cdl);
96                 else { /* more accurate for smaller distances */
97                     dp = sin(.5 * dphi);
98                     dl = sin(.5 * dlam);
99                     v.r = 2. * aasin(sqrt(dp * dp + c1 * c2 * dl * dl));
100                 }
101                 if (fabs(v.r) > tolerance)
102                     v.Az = atan2(c2 * sin(dlam), c1 * s2 - s1 * c2 * cdl);
103                 else
104                     v.r = v.Az = 0.;
105                 return v;
106             }
107
108             /* law of cosines */
109             template <typename T>
110             inline T lc(T const& b, T const& c, T const& a)
111             {
112                 return aacos(.5 * (b * b + c * c - a * a) / (b * c));
113             }
114
115             template <typename T, typename Parameters>
116             struct base_chamb_spheroid
117             {
118                 par_chamb<T> m_proj_parm;
119
120                 // FORWARD(s_forward)  spheroid
121                 // Project coordinates from geographic (lon, lat) to cartesian (x, y)
122                 inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
123                 {
124                     static const T third = detail::third<T>();
125
126                     T sinphi, cosphi, a;
127                     vect_ra<T> v[3];
128                     int i, j;
129
130                     sinphi = sin(lp_lat);
131                     cosphi = cos(lp_lat);
132                     for (i = 0; i < 3; ++i) { /* dist/azimiths from control */
133                         v[i] = vect(lp_lat - this->m_proj_parm.c[i].phi, this->m_proj_parm.c[i].cosphi, this->m_proj_parm.c[i].sinphi,
134                             cosphi, sinphi, lp_lon - this->m_proj_parm.c[i].lam);
135                         if (v[i].r == 0.0)
136                             break;
137                         v[i].Az = adjlon(v[i].Az - this->m_proj_parm.c[i].v.Az);
138                     }
139                     if (i < 3) /* current point at control point */
140                         { xy_x = this->m_proj_parm.c[i].p.x; xy_y = this->m_proj_parm.c[i].p.y; }
141                     else { /* point mean of intersepts */
142                         { xy_x = this->m_proj_parm.p.x; xy_y = this->m_proj_parm.p.y; }
143                         for (i = 0; i < 3; ++i) {
144                             j = i == 2 ? 0 : i + 1;
145                             a = lc(this->m_proj_parm.c[i].v.r, v[i].r, v[j].r);
146                             if (v[i].Az < 0.)
147                                 a = -a;
148                             if (! i) { /* coord comp unique to each arc */
149                                 xy_x += v[i].r * cos(a);
150                                 xy_y -= v[i].r * sin(a);
151                             } else if (i == 1) {
152                                 a = this->m_proj_parm.beta_1 - a;
153                                 xy_x -= v[i].r * cos(a);
154                                 xy_y -= v[i].r * sin(a);
155                             } else {
156                                 a = this->m_proj_parm.beta_2 - a;
157                                 xy_x += v[i].r * cos(a);
158                                 xy_y += v[i].r * sin(a);
159                             }
160                         }
161                         xy_x *= third; /* mean of arc intercepts */
162                         xy_y *= third;
163                     }
164                 }
165
166                 static inline std::string get_name()
167                 {
168                     return "chamb_spheroid";
169                 }
170
171             };
172
173             template <typename T>
174             inline T chamb_init_lat(srs::detail::proj4_parameters const& params, int i)
175             {
176                 static const std::string lat[3] = {"lat_1", "lat_2", "lat_3"};
177                 return _pj_get_param_r<T>(params, lat[i]);
178             }
179             template <typename T>
180             inline T chamb_init_lat(srs::dpar::parameters<T> const& params, int i)
181             {
182                 static const srs::dpar::name_r lat[3] = {srs::dpar::lat_1, srs::dpar::lat_2, srs::dpar::lat_3};
183                 return _pj_get_param_r<T>(params, lat[i]);
184             }
185
186             template <typename T>
187             inline T chamb_init_lon(srs::detail::proj4_parameters const& params, int i)
188             {
189                 static const std::string lon[3] = {"lon_1", "lon_2", "lon_3"};
190                 return _pj_get_param_r<T>(params, lon[i]);
191             }
192             template <typename T>
193             inline T chamb_init_lon(srs::dpar::parameters<T> const& params, int i)
194             {
195                 static const srs::dpar::name_r lon[3] = {srs::dpar::lon_1, srs::dpar::lon_2, srs::dpar::lon_3};
196                 return _pj_get_param_r<T>(params, lon[i]);
197             }
198
199             // Chamberlin Trimetric
200             template <typename Params, typename Parameters, typename T>
201             inline void setup_chamb(Params const& params, Parameters& par, par_chamb<T>& proj_parm)
202             {
203                 static const T pi = detail::pi<T>();
204
205                 int i, j;
206
207                 for (i = 0; i < 3; ++i) { /* get control point locations */
208                     proj_parm.c[i].phi = chamb_init_lat<T>(params, i);
209                     proj_parm.c[i].lam = chamb_init_lon<T>(params, i);
210                     proj_parm.c[i].lam = adjlon(proj_parm.c[i].lam - par.lam0);
211                     proj_parm.c[i].cosphi = cos(proj_parm.c[i].phi);
212                     proj_parm.c[i].sinphi = sin(proj_parm.c[i].phi);
213                 }
214                 for (i = 0; i < 3; ++i) { /* inter ctl pt. distances and azimuths */
215                     j = i == 2 ? 0 : i + 1;
216                     proj_parm.c[i].v = vect(proj_parm.c[j].phi - proj_parm.c[i].phi, proj_parm.c[i].cosphi, proj_parm.c[i].sinphi,
217                         proj_parm.c[j].cosphi, proj_parm.c[j].sinphi, proj_parm.c[j].lam - proj_parm.c[i].lam);
218                     if (proj_parm.c[i].v.r == 0.0)
219                         BOOST_THROW_EXCEPTION( projection_exception(error_control_point_no_dist) );
220                     /* co-linearity problem ignored for now */
221                 }
222                 proj_parm.beta_0 = lc(proj_parm.c[0].v.r, proj_parm.c[2].v.r, proj_parm.c[1].v.r);
223                 proj_parm.beta_1 = lc(proj_parm.c[0].v.r, proj_parm.c[1].v.r, proj_parm.c[2].v.r);
224                 proj_parm.beta_2 = pi - proj_parm.beta_0;
225                 proj_parm.p.y = 2. * (proj_parm.c[0].p.y = proj_parm.c[1].p.y = proj_parm.c[2].v.r * sin(proj_parm.beta_0));
226                 proj_parm.c[2].p.y = 0.;
227                 proj_parm.c[0].p.x = - (proj_parm.c[1].p.x = 0.5 * proj_parm.c[0].v.r);
228                 proj_parm.p.x = proj_parm.c[2].p.x = proj_parm.c[0].p.x + proj_parm.c[2].v.r * cos(proj_parm.beta_0);
229
230                 par.es = 0.;
231             }
232
233     }} // namespace detail::chamb
234     #endif // doxygen
235
236     /*!
237         \brief Chamberlin Trimetric projection
238         \ingroup projections
239         \tparam Geographic latlong point type
240         \tparam Cartesian xy point type
241         \tparam Parameters parameter type
242         \par Projection characteristics
243          - Miscellaneous
244          - Spheroid
245          - no inverse
246         \par Projection parameters
247          - lat_1: Latitude of control point 1 (degrees)
248          - lon_1: Longitude of control point 1 (degrees)
249          - lat_2: Latitude of control point 2 (degrees)
250          - lon_2: Longitude of control point 2 (degrees)
251          - lat_3: Latitude of control point 3 (degrees)
252          - lon_3: Longitude of control point 3 (degrees)
253         \par Example
254         \image html ex_chamb.gif
255     */
256     template <typename T, typename Parameters>
257     struct chamb_spheroid : public detail::chamb::base_chamb_spheroid<T, Parameters>
258     {
259         template <typename Params>
260         inline chamb_spheroid(Params const& params, Parameters & par)
261         {
262             detail::chamb::setup_chamb(params, par, this->m_proj_parm);
263         }
264     };
265
266     #ifndef DOXYGEN_NO_DETAIL
267     namespace detail
268     {
269
270         // Static projection
271         BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_chamb, chamb_spheroid)
272
273         // Factory entry(s)
274         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_F(chamb_entry, chamb_spheroid)
275         
276         BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(chamb_init)
277         {
278             BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(chamb, chamb_entry);
279         }
280
281     } // namespace detail
282     #endif // doxygen
283
284 } // namespace projections
285
286 }} // namespace boost::geometry
287
288 #endif // BOOST_GEOMETRY_PROJECTIONS_CHAMB_HPP
289