qsc.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // Boost.Geometry - gis-projections (based on PROJ4)
  2. // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2017, 2018.
  4. // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
  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)
  9. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  10. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  11. // PROJ4 is maintained by Frank Warmerdam
  12. // PROJ4 is converted to Boost.Geometry by Barend Gehrels
  13. // Last updated version of proj: 5.0.0
  14. // Original copyright notice:
  15. // This implements the Quadrilateralized Spherical Cube (QSC) projection.
  16. // Copyright (c) 2011, 2012 Martin Lambers <marlam@marlam.de>
  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:
  23. // The above copyright notice and this permission notice shall be included
  24. // in all copies or substantial portions of the Software.
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  26. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  28. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  31. // DEALINGS IN THE SOFTWARE.
  32. // The QSC projection was introduced in:
  33. // [OL76]
  34. // E.M. O'Neill and R.E. Laubscher, "Extended Studies of a Quadrilateralized
  35. // Spherical Cube Earth Data Base", Naval Environmental Prediction Research
  36. // Facility Tech. Report NEPRF 3-76 (CSC), May 1976.
  37. // The preceding shift from an ellipsoid to a sphere, which allows to apply
  38. // this projection to ellipsoids as used in the Ellipsoidal Cube Map model,
  39. // is described in
  40. // [LK12]
  41. // M. Lambers and A. Kolb, "Ellipsoidal Cube Maps for Accurate Rendering of
  42. // Planetary-Scale Terrain Data", Proc. Pacfic Graphics (Short Papers), Sep.
  43. // 2012
  44. // You have to choose one of the following projection centers,
  45. // corresponding to the centers of the six cube faces:
  46. // phi0 = 0.0, lam0 = 0.0 ("front" face)
  47. // phi0 = 0.0, lam0 = 90.0 ("right" face)
  48. // phi0 = 0.0, lam0 = 180.0 ("back" face)
  49. // phi0 = 0.0, lam0 = -90.0 ("left" face)
  50. // phi0 = 90.0 ("top" face)
  51. // phi0 = -90.0 ("bottom" face)
  52. // Other projection centers will not work!
  53. // In the projection code below, each cube face is handled differently.
  54. // See the computation of the face parameter in the ENTRY0(qsc) function
  55. // and the handling of different face values (FACE_*) in the forward and
  56. // inverse projections.
  57. // Furthermore, the projection is originally only defined for theta angles
  58. // between (-1/4 * PI) and (+1/4 * PI) on the current cube face. This area
  59. // of definition is named AREA_0 in the projection code below. The other
  60. // three areas of a cube face are handled by rotation of AREA_0.
  61. #ifndef BOOST_GEOMETRY_PROJECTIONS_QSC_HPP
  62. #define BOOST_GEOMETRY_PROJECTIONS_QSC_HPP
  63. #include <boost/core/ignore_unused.hpp>
  64. #include <boost/geometry/util/math.hpp>
  65. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  66. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  67. #include <boost/geometry/srs/projections/impl/projects.hpp>
  68. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  69. namespace boost { namespace geometry
  70. {
  71. namespace projections
  72. {
  73. #ifndef DOXYGEN_NO_DETAIL
  74. namespace detail { namespace qsc
  75. {
  76. /* The six cube faces. */
  77. enum face_type {
  78. face_front = 0,
  79. face_right = 1,
  80. face_back = 2,
  81. face_left = 3,
  82. face_top = 4,
  83. face_bottom = 5
  84. };
  85. template <typename T>
  86. struct par_qsc
  87. {
  88. face_type face;
  89. T a_squared;
  90. T b;
  91. T one_minus_f;
  92. T one_minus_f_squared;
  93. };
  94. static const double epsilon10 = 1.e-10;
  95. /* The four areas on a cube face. AREA_0 is the area of definition,
  96. * the other three areas are counted counterclockwise. */
  97. enum area_type {
  98. area_0 = 0,
  99. area_1 = 1,
  100. area_2 = 2,
  101. area_3 = 3
  102. };
  103. /* Helper function for forward projection: compute the theta angle
  104. * and determine the area number. */
  105. template <typename T>
  106. inline T qsc_fwd_equat_face_theta(T const& phi, T const& y, T const& x, area_type *area)
  107. {
  108. static const T fourth_pi = detail::fourth_pi<T>();
  109. static const T half_pi = detail::half_pi<T>();
  110. static const T pi = detail::pi<T>();
  111. T theta;
  112. if (phi < epsilon10) {
  113. *area = area_0;
  114. theta = 0.0;
  115. } else {
  116. theta = atan2(y, x);
  117. if (fabs(theta) <= fourth_pi) {
  118. *area = area_0;
  119. } else if (theta > fourth_pi && theta <= half_pi + fourth_pi) {
  120. *area = area_1;
  121. theta -= half_pi;
  122. } else if (theta > half_pi + fourth_pi || theta <= -(half_pi + fourth_pi)) {
  123. *area = area_2;
  124. theta = (theta >= 0.0 ? theta - pi : theta + pi);
  125. } else {
  126. *area = area_3;
  127. theta += half_pi;
  128. }
  129. }
  130. return theta;
  131. }
  132. /* Helper function: shift the longitude. */
  133. template <typename T>
  134. inline T qsc_shift_lon_origin(T const& lon, T const& offset)
  135. {
  136. static const T pi = detail::pi<T>();
  137. static const T two_pi = detail::two_pi<T>();
  138. T slon = lon + offset;
  139. if (slon < -pi) {
  140. slon += two_pi;
  141. } else if (slon > +pi) {
  142. slon -= two_pi;
  143. }
  144. return slon;
  145. }
  146. /* Forward projection, ellipsoid */
  147. // template class, using CRTP to implement forward/inverse
  148. template <typename T, typename Parameters>
  149. struct base_qsc_ellipsoid
  150. : public base_t_fi<base_qsc_ellipsoid<T, Parameters>, T, Parameters>
  151. {
  152. par_qsc<T> m_proj_parm;
  153. inline base_qsc_ellipsoid(const Parameters& par)
  154. : base_t_fi<base_qsc_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  155. {}
  156. // FORWARD(e_forward)
  157. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  158. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  159. {
  160. static const T fourth_pi = detail::fourth_pi<T>();
  161. static const T half_pi = detail::half_pi<T>();
  162. static const T pi = detail::pi<T>();
  163. T lat, lon;
  164. T theta, phi;
  165. T t, mu; /* nu; */
  166. area_type area;
  167. /* Convert the geodetic latitude to a geocentric latitude.
  168. * This corresponds to the shift from the ellipsoid to the sphere
  169. * described in [LK12]. */
  170. if (this->m_par.es != 0.0) {
  171. lat = atan(this->m_proj_parm.one_minus_f_squared * tan(lp_lat));
  172. } else {
  173. lat = lp_lat;
  174. }
  175. /* Convert the input lat, lon into theta, phi as used by QSC.
  176. * This depends on the cube face and the area on it.
  177. * For the top and bottom face, we can compute theta and phi
  178. * directly from phi, lam. For the other faces, we must use
  179. * unit sphere cartesian coordinates as an intermediate step. */
  180. lon = lp_lon;
  181. if (this->m_proj_parm.face == face_top) {
  182. phi = half_pi - lat;
  183. if (lon >= fourth_pi && lon <= half_pi + fourth_pi) {
  184. area = area_0;
  185. theta = lon - half_pi;
  186. } else if (lon > half_pi + fourth_pi || lon <= -(half_pi + fourth_pi)) {
  187. area = area_1;
  188. theta = (lon > 0.0 ? lon - pi : lon + pi);
  189. } else if (lon > -(half_pi + fourth_pi) && lon <= -fourth_pi) {
  190. area = area_2;
  191. theta = lon + half_pi;
  192. } else {
  193. area = area_3;
  194. theta = lon;
  195. }
  196. } else if (this->m_proj_parm.face == face_bottom) {
  197. phi = half_pi + lat;
  198. if (lon >= fourth_pi && lon <= half_pi + fourth_pi) {
  199. area = area_0;
  200. theta = -lon + half_pi;
  201. } else if (lon < fourth_pi && lon >= -fourth_pi) {
  202. area = area_1;
  203. theta = -lon;
  204. } else if (lon < -fourth_pi && lon >= -(half_pi + fourth_pi)) {
  205. area = area_2;
  206. theta = -lon - half_pi;
  207. } else {
  208. area = area_3;
  209. theta = (lon > 0.0 ? -lon + pi : -lon - pi);
  210. }
  211. } else {
  212. T q, r, s;
  213. T sinlat, coslat;
  214. T sinlon, coslon;
  215. if (this->m_proj_parm.face == face_right) {
  216. lon = qsc_shift_lon_origin(lon, +half_pi);
  217. } else if (this->m_proj_parm.face == face_back) {
  218. lon = qsc_shift_lon_origin(lon, +pi);
  219. } else if (this->m_proj_parm.face == face_left) {
  220. lon = qsc_shift_lon_origin(lon, -half_pi);
  221. }
  222. sinlat = sin(lat);
  223. coslat = cos(lat);
  224. sinlon = sin(lon);
  225. coslon = cos(lon);
  226. q = coslat * coslon;
  227. r = coslat * sinlon;
  228. s = sinlat;
  229. if (this->m_proj_parm.face == face_front) {
  230. phi = acos(q);
  231. theta = qsc_fwd_equat_face_theta(phi, s, r, &area);
  232. } else if (this->m_proj_parm.face == face_right) {
  233. phi = acos(r);
  234. theta = qsc_fwd_equat_face_theta(phi, s, -q, &area);
  235. } else if (this->m_proj_parm.face == face_back) {
  236. phi = acos(-q);
  237. theta = qsc_fwd_equat_face_theta(phi, s, -r, &area);
  238. } else if (this->m_proj_parm.face == face_left) {
  239. phi = acos(-r);
  240. theta = qsc_fwd_equat_face_theta(phi, s, q, &area);
  241. } else {
  242. /* Impossible */
  243. phi = theta = 0.0;
  244. area = area_0;
  245. }
  246. }
  247. /* Compute mu and nu for the area of definition.
  248. * For mu, see Eq. (3-21) in [OL76], but note the typos:
  249. * compare with Eq. (3-14). For nu, see Eq. (3-38). */
  250. mu = atan((12.0 / pi) * (theta + acos(sin(theta) * cos(fourth_pi)) - half_pi));
  251. // TODO: (cos(mu) * cos(mu)) could be replaced with sqr(cos(mu))
  252. t = sqrt((1.0 - cos(phi)) / (cos(mu) * cos(mu)) / (1.0 - cos(atan(1.0 / cos(theta)))));
  253. /* nu = atan(t); We don't really need nu, just t, see below. */
  254. /* Apply the result to the real area. */
  255. if (area == area_1) {
  256. mu += half_pi;
  257. } else if (area == area_2) {
  258. mu += pi;
  259. } else if (area == area_3) {
  260. mu += half_pi + pi;
  261. }
  262. /* Now compute x, y from mu and nu */
  263. /* t = tan(nu); */
  264. xy_x = t * cos(mu);
  265. xy_y = t * sin(mu);
  266. }
  267. /* Inverse projection, ellipsoid */
  268. // INVERSE(e_inverse)
  269. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  270. inline void inv(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  271. {
  272. static const T half_pi = detail::half_pi<T>();
  273. static const T pi = detail::pi<T>();
  274. T mu, nu, cosmu, tannu;
  275. T tantheta, theta, cosphi, phi;
  276. T t;
  277. int area;
  278. /* Convert the input x, y to the mu and nu angles as used by QSC.
  279. * This depends on the area of the cube face. */
  280. nu = atan(sqrt(xy_x * xy_x + xy_y * xy_y));
  281. mu = atan2(xy_y, xy_x);
  282. if (xy_x >= 0.0 && xy_x >= fabs(xy_y)) {
  283. area = area_0;
  284. } else if (xy_y >= 0.0 && xy_y >= fabs(xy_x)) {
  285. area = area_1;
  286. mu -= half_pi;
  287. } else if (xy_x < 0.0 && -xy_x >= fabs(xy_y)) {
  288. area = area_2;
  289. mu = (mu < 0.0 ? mu + pi : mu - pi);
  290. } else {
  291. area = area_3;
  292. mu += half_pi;
  293. }
  294. /* Compute phi and theta for the area of definition.
  295. * The inverse projection is not described in the original paper, but some
  296. * good hints can be found here (as of 2011-12-14):
  297. * http://fits.gsfc.nasa.gov/fitsbits/saf.93/saf.9302
  298. * (search for "Message-Id: <9302181759.AA25477 at fits.cv.nrao.edu>") */
  299. t = (pi / 12.0) * tan(mu);
  300. tantheta = sin(t) / (cos(t) - (1.0 / sqrt(2.0)));
  301. theta = atan(tantheta);
  302. cosmu = cos(mu);
  303. tannu = tan(nu);
  304. cosphi = 1.0 - cosmu * cosmu * tannu * tannu * (1.0 - cos(atan(1.0 / cos(theta))));
  305. if (cosphi < -1.0) {
  306. cosphi = -1.0;
  307. } else if (cosphi > +1.0) {
  308. cosphi = +1.0;
  309. }
  310. /* Apply the result to the real area on the cube face.
  311. * For the top and bottom face, we can compute phi and lam directly.
  312. * For the other faces, we must use unit sphere cartesian coordinates
  313. * as an intermediate step. */
  314. if (this->m_proj_parm.face == face_top) {
  315. phi = acos(cosphi);
  316. lp_lat = half_pi - phi;
  317. if (area == area_0) {
  318. lp_lon = theta + half_pi;
  319. } else if (area == area_1) {
  320. lp_lon = (theta < 0.0 ? theta + pi : theta - pi);
  321. } else if (area == area_2) {
  322. lp_lon = theta - half_pi;
  323. } else /* area == AREA_3 */ {
  324. lp_lon = theta;
  325. }
  326. } else if (this->m_proj_parm.face == face_bottom) {
  327. phi = acos(cosphi);
  328. lp_lat = phi - half_pi;
  329. if (area == area_0) {
  330. lp_lon = -theta + half_pi;
  331. } else if (area == area_1) {
  332. lp_lon = -theta;
  333. } else if (area == area_2) {
  334. lp_lon = -theta - half_pi;
  335. } else /* area == area_3 */ {
  336. lp_lon = (theta < 0.0 ? -theta - pi : -theta + pi);
  337. }
  338. } else {
  339. /* Compute phi and lam via cartesian unit sphere coordinates. */
  340. T q, r, s;
  341. q = cosphi;
  342. t = q * q;
  343. if (t >= 1.0) {
  344. s = 0.0;
  345. } else {
  346. s = sqrt(1.0 - t) * sin(theta);
  347. }
  348. t += s * s;
  349. if (t >= 1.0) {
  350. r = 0.0;
  351. } else {
  352. r = sqrt(1.0 - t);
  353. }
  354. /* Rotate q,r,s into the correct area. */
  355. if (area == area_1) {
  356. t = r;
  357. r = -s;
  358. s = t;
  359. } else if (area == area_2) {
  360. r = -r;
  361. s = -s;
  362. } else if (area == area_3) {
  363. t = r;
  364. r = s;
  365. s = -t;
  366. }
  367. /* Rotate q,r,s into the correct cube face. */
  368. if (this->m_proj_parm.face == face_right) {
  369. t = q;
  370. q = -r;
  371. r = t;
  372. } else if (this->m_proj_parm.face == face_back) {
  373. q = -q;
  374. r = -r;
  375. } else if (this->m_proj_parm.face == face_left) {
  376. t = q;
  377. q = r;
  378. r = -t;
  379. }
  380. /* Now compute phi and lam from the unit sphere coordinates. */
  381. lp_lat = acos(-s) - half_pi;
  382. lp_lon = atan2(r, q);
  383. if (this->m_proj_parm.face == face_right) {
  384. lp_lon = qsc_shift_lon_origin(lp_lon, -half_pi);
  385. } else if (this->m_proj_parm.face == face_back) {
  386. lp_lon = qsc_shift_lon_origin(lp_lon, -pi);
  387. } else if (this->m_proj_parm.face == face_left) {
  388. lp_lon = qsc_shift_lon_origin(lp_lon, +half_pi);
  389. }
  390. }
  391. /* Apply the shift from the sphere to the ellipsoid as described
  392. * in [LK12]. */
  393. if (this->m_par.es != 0.0) {
  394. int invert_sign;
  395. T tanphi, xa;
  396. invert_sign = (lp_lat < 0.0 ? 1 : 0);
  397. tanphi = tan(lp_lat);
  398. xa = this->m_proj_parm.b / sqrt(tanphi * tanphi + this->m_proj_parm.one_minus_f_squared);
  399. lp_lat = atan(sqrt(this->m_par.a * this->m_par.a - xa * xa) / (this->m_proj_parm.one_minus_f * xa));
  400. if (invert_sign) {
  401. lp_lat = -lp_lat;
  402. }
  403. }
  404. }
  405. static inline std::string get_name()
  406. {
  407. return "qsc_ellipsoid";
  408. }
  409. };
  410. // Quadrilateralized Spherical Cube
  411. template <typename Parameters, typename T>
  412. inline void setup_qsc(Parameters& par, par_qsc<T>& proj_parm)
  413. {
  414. static const T fourth_pi = detail::fourth_pi<T>();
  415. static const T half_pi = detail::half_pi<T>();
  416. /* Determine the cube face from the center of projection. */
  417. if (par.phi0 >= half_pi - fourth_pi / 2.0) {
  418. proj_parm.face = face_top;
  419. } else if (par.phi0 <= -(half_pi - fourth_pi / 2.0)) {
  420. proj_parm.face = face_bottom;
  421. } else if (fabs(par.lam0) <= fourth_pi) {
  422. proj_parm.face = face_front;
  423. } else if (fabs(par.lam0) <= half_pi + fourth_pi) {
  424. proj_parm.face = (par.lam0 > 0.0 ? face_right : face_left);
  425. } else {
  426. proj_parm.face = face_back;
  427. }
  428. /* Fill in useful values for the ellipsoid <-> sphere shift
  429. * described in [LK12]. */
  430. if (par.es != 0.0) {
  431. proj_parm.a_squared = par.a * par.a;
  432. proj_parm.b = par.a * sqrt(1.0 - par.es);
  433. proj_parm.one_minus_f = 1.0 - (par.a - proj_parm.b) / par.a;
  434. proj_parm.one_minus_f_squared = proj_parm.one_minus_f * proj_parm.one_minus_f;
  435. }
  436. }
  437. }} // namespace detail::qsc
  438. #endif // doxygen
  439. /*!
  440. \brief Quadrilateralized Spherical Cube projection
  441. \ingroup projections
  442. \tparam Geographic latlong point type
  443. \tparam Cartesian xy point type
  444. \tparam Parameters parameter type
  445. \par Projection characteristics
  446. - Azimuthal
  447. - Spheroid
  448. \par Example
  449. \image html ex_qsc.gif
  450. */
  451. template <typename T, typename Parameters>
  452. struct qsc_ellipsoid : public detail::qsc::base_qsc_ellipsoid<T, Parameters>
  453. {
  454. template <typename Params>
  455. inline qsc_ellipsoid(Params const& , Parameters const& par)
  456. : detail::qsc::base_qsc_ellipsoid<T, Parameters>(par)
  457. {
  458. detail::qsc::setup_qsc(this->m_par, this->m_proj_parm);
  459. }
  460. };
  461. #ifndef DOXYGEN_NO_DETAIL
  462. namespace detail
  463. {
  464. // Static projection
  465. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_qsc, qsc_ellipsoid, qsc_ellipsoid)
  466. // Factory entry(s)
  467. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(qsc_entry, qsc_ellipsoid)
  468. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(qsc_init)
  469. {
  470. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(qsc, qsc_entry)
  471. }
  472. } // namespace detail
  473. #endif // doxygen
  474. } // namespace projections
  475. }} // namespace boost::geometry
  476. #endif // BOOST_GEOMETRY_PROJECTIONS_QSC_HPP