qsc.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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, 2019.
  4. // Modifications copyright (c) 2017-2019, 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. T a_squared;
  89. T b;
  90. T one_minus_f;
  91. T one_minus_f_squared;
  92. face_type face;
  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 <typename T, typename Parameters>
  148. struct base_qsc_ellipsoid
  149. {
  150. par_qsc<T> m_proj_parm;
  151. // FORWARD(e_forward)
  152. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  153. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  154. {
  155. static const T fourth_pi = detail::fourth_pi<T>();
  156. static const T half_pi = detail::half_pi<T>();
  157. static const T pi = detail::pi<T>();
  158. T lat, lon;
  159. T theta, phi;
  160. T t, mu; /* nu; */
  161. area_type area;
  162. /* Convert the geodetic latitude to a geocentric latitude.
  163. * This corresponds to the shift from the ellipsoid to the sphere
  164. * described in [LK12]. */
  165. if (par.es != 0.0) {
  166. lat = atan(this->m_proj_parm.one_minus_f_squared * tan(lp_lat));
  167. } else {
  168. lat = lp_lat;
  169. }
  170. /* Convert the input lat, lon into theta, phi as used by QSC.
  171. * This depends on the cube face and the area on it.
  172. * For the top and bottom face, we can compute theta and phi
  173. * directly from phi, lam. For the other faces, we must use
  174. * unit sphere cartesian coordinates as an intermediate step. */
  175. lon = lp_lon;
  176. if (this->m_proj_parm.face == face_top) {
  177. phi = half_pi - lat;
  178. if (lon >= fourth_pi && lon <= half_pi + fourth_pi) {
  179. area = area_0;
  180. theta = lon - half_pi;
  181. } else if (lon > half_pi + fourth_pi || lon <= -(half_pi + fourth_pi)) {
  182. area = area_1;
  183. theta = (lon > 0.0 ? lon - pi : lon + pi);
  184. } else if (lon > -(half_pi + fourth_pi) && lon <= -fourth_pi) {
  185. area = area_2;
  186. theta = lon + half_pi;
  187. } else {
  188. area = area_3;
  189. theta = lon;
  190. }
  191. } else if (this->m_proj_parm.face == face_bottom) {
  192. phi = half_pi + lat;
  193. if (lon >= fourth_pi && lon <= half_pi + fourth_pi) {
  194. area = area_0;
  195. theta = -lon + half_pi;
  196. } else if (lon < fourth_pi && lon >= -fourth_pi) {
  197. area = area_1;
  198. theta = -lon;
  199. } else if (lon < -fourth_pi && lon >= -(half_pi + fourth_pi)) {
  200. area = area_2;
  201. theta = -lon - half_pi;
  202. } else {
  203. area = area_3;
  204. theta = (lon > 0.0 ? -lon + pi : -lon - pi);
  205. }
  206. } else {
  207. T q, r, s;
  208. T sinlat, coslat;
  209. T sinlon, coslon;
  210. if (this->m_proj_parm.face == face_right) {
  211. lon = qsc_shift_lon_origin(lon, +half_pi);
  212. } else if (this->m_proj_parm.face == face_back) {
  213. lon = qsc_shift_lon_origin(lon, +pi);
  214. } else if (this->m_proj_parm.face == face_left) {
  215. lon = qsc_shift_lon_origin(lon, -half_pi);
  216. }
  217. sinlat = sin(lat);
  218. coslat = cos(lat);
  219. sinlon = sin(lon);
  220. coslon = cos(lon);
  221. q = coslat * coslon;
  222. r = coslat * sinlon;
  223. s = sinlat;
  224. if (this->m_proj_parm.face == face_front) {
  225. phi = acos(q);
  226. theta = qsc_fwd_equat_face_theta(phi, s, r, &area);
  227. } else if (this->m_proj_parm.face == face_right) {
  228. phi = acos(r);
  229. theta = qsc_fwd_equat_face_theta(phi, s, -q, &area);
  230. } else if (this->m_proj_parm.face == face_back) {
  231. phi = acos(-q);
  232. theta = qsc_fwd_equat_face_theta(phi, s, -r, &area);
  233. } else if (this->m_proj_parm.face == face_left) {
  234. phi = acos(-r);
  235. theta = qsc_fwd_equat_face_theta(phi, s, q, &area);
  236. } else {
  237. /* Impossible */
  238. phi = theta = 0.0;
  239. area = area_0;
  240. }
  241. }
  242. /* Compute mu and nu for the area of definition.
  243. * For mu, see Eq. (3-21) in [OL76], but note the typos:
  244. * compare with Eq. (3-14). For nu, see Eq. (3-38). */
  245. mu = atan((12.0 / pi) * (theta + acos(sin(theta) * cos(fourth_pi)) - half_pi));
  246. // TODO: (cos(mu) * cos(mu)) could be replaced with sqr(cos(mu))
  247. t = sqrt((1.0 - cos(phi)) / (cos(mu) * cos(mu)) / (1.0 - cos(atan(1.0 / cos(theta)))));
  248. /* nu = atan(t); We don't really need nu, just t, see below. */
  249. /* Apply the result to the real area. */
  250. if (area == area_1) {
  251. mu += half_pi;
  252. } else if (area == area_2) {
  253. mu += pi;
  254. } else if (area == area_3) {
  255. mu += half_pi + pi;
  256. }
  257. /* Now compute x, y from mu and nu */
  258. /* t = tan(nu); */
  259. xy_x = t * cos(mu);
  260. xy_y = t * sin(mu);
  261. }
  262. /* Inverse projection, ellipsoid */
  263. // INVERSE(e_inverse)
  264. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  265. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  266. {
  267. static const T half_pi = detail::half_pi<T>();
  268. static const T pi = detail::pi<T>();
  269. T mu, nu, cosmu, tannu;
  270. T tantheta, theta, cosphi, phi;
  271. T t;
  272. int area;
  273. /* Convert the input x, y to the mu and nu angles as used by QSC.
  274. * This depends on the area of the cube face. */
  275. nu = atan(sqrt(xy_x * xy_x + xy_y * xy_y));
  276. mu = atan2(xy_y, xy_x);
  277. if (xy_x >= 0.0 && xy_x >= fabs(xy_y)) {
  278. area = area_0;
  279. } else if (xy_y >= 0.0 && xy_y >= fabs(xy_x)) {
  280. area = area_1;
  281. mu -= half_pi;
  282. } else if (xy_x < 0.0 && -xy_x >= fabs(xy_y)) {
  283. area = area_2;
  284. mu = (mu < 0.0 ? mu + pi : mu - pi);
  285. } else {
  286. area = area_3;
  287. mu += half_pi;
  288. }
  289. /* Compute phi and theta for the area of definition.
  290. * The inverse projection is not described in the original paper, but some
  291. * good hints can be found here (as of 2011-12-14):
  292. * http://fits.gsfc.nasa.gov/fitsbits/saf.93/saf.9302
  293. * (search for "Message-Id: <9302181759.AA25477 at fits.cv.nrao.edu>") */
  294. t = (pi / 12.0) * tan(mu);
  295. tantheta = sin(t) / (cos(t) - (1.0 / sqrt(2.0)));
  296. theta = atan(tantheta);
  297. cosmu = cos(mu);
  298. tannu = tan(nu);
  299. cosphi = 1.0 - cosmu * cosmu * tannu * tannu * (1.0 - cos(atan(1.0 / cos(theta))));
  300. if (cosphi < -1.0) {
  301. cosphi = -1.0;
  302. } else if (cosphi > +1.0) {
  303. cosphi = +1.0;
  304. }
  305. /* Apply the result to the real area on the cube face.
  306. * For the top and bottom face, we can compute phi and lam directly.
  307. * For the other faces, we must use unit sphere cartesian coordinates
  308. * as an intermediate step. */
  309. if (this->m_proj_parm.face == face_top) {
  310. phi = acos(cosphi);
  311. lp_lat = half_pi - phi;
  312. if (area == area_0) {
  313. lp_lon = theta + half_pi;
  314. } else if (area == area_1) {
  315. lp_lon = (theta < 0.0 ? theta + pi : theta - pi);
  316. } else if (area == area_2) {
  317. lp_lon = theta - half_pi;
  318. } else /* area == AREA_3 */ {
  319. lp_lon = theta;
  320. }
  321. } else if (this->m_proj_parm.face == face_bottom) {
  322. phi = acos(cosphi);
  323. lp_lat = phi - half_pi;
  324. if (area == area_0) {
  325. lp_lon = -theta + half_pi;
  326. } else if (area == area_1) {
  327. lp_lon = -theta;
  328. } else if (area == area_2) {
  329. lp_lon = -theta - half_pi;
  330. } else /* area == area_3 */ {
  331. lp_lon = (theta < 0.0 ? -theta - pi : -theta + pi);
  332. }
  333. } else {
  334. /* Compute phi and lam via cartesian unit sphere coordinates. */
  335. T q, r, s;
  336. q = cosphi;
  337. t = q * q;
  338. if (t >= 1.0) {
  339. s = 0.0;
  340. } else {
  341. s = sqrt(1.0 - t) * sin(theta);
  342. }
  343. t += s * s;
  344. if (t >= 1.0) {
  345. r = 0.0;
  346. } else {
  347. r = sqrt(1.0 - t);
  348. }
  349. /* Rotate q,r,s into the correct area. */
  350. if (area == area_1) {
  351. t = r;
  352. r = -s;
  353. s = t;
  354. } else if (area == area_2) {
  355. r = -r;
  356. s = -s;
  357. } else if (area == area_3) {
  358. t = r;
  359. r = s;
  360. s = -t;
  361. }
  362. /* Rotate q,r,s into the correct cube face. */
  363. if (this->m_proj_parm.face == face_right) {
  364. t = q;
  365. q = -r;
  366. r = t;
  367. } else if (this->m_proj_parm.face == face_back) {
  368. q = -q;
  369. r = -r;
  370. } else if (this->m_proj_parm.face == face_left) {
  371. t = q;
  372. q = r;
  373. r = -t;
  374. }
  375. /* Now compute phi and lam from the unit sphere coordinates. */
  376. lp_lat = acos(-s) - half_pi;
  377. lp_lon = atan2(r, q);
  378. if (this->m_proj_parm.face == face_right) {
  379. lp_lon = qsc_shift_lon_origin(lp_lon, -half_pi);
  380. } else if (this->m_proj_parm.face == face_back) {
  381. lp_lon = qsc_shift_lon_origin(lp_lon, -pi);
  382. } else if (this->m_proj_parm.face == face_left) {
  383. lp_lon = qsc_shift_lon_origin(lp_lon, +half_pi);
  384. }
  385. }
  386. /* Apply the shift from the sphere to the ellipsoid as described
  387. * in [LK12]. */
  388. if (par.es != 0.0) {
  389. int invert_sign;
  390. T tanphi, xa;
  391. invert_sign = (lp_lat < 0.0 ? 1 : 0);
  392. tanphi = tan(lp_lat);
  393. xa = this->m_proj_parm.b / sqrt(tanphi * tanphi + this->m_proj_parm.one_minus_f_squared);
  394. lp_lat = atan(sqrt(par.a * par.a - xa * xa) / (this->m_proj_parm.one_minus_f * xa));
  395. if (invert_sign) {
  396. lp_lat = -lp_lat;
  397. }
  398. }
  399. }
  400. static inline std::string get_name()
  401. {
  402. return "qsc_ellipsoid";
  403. }
  404. };
  405. // Quadrilateralized Spherical Cube
  406. template <typename Parameters, typename T>
  407. inline void setup_qsc(Parameters const& par, par_qsc<T>& proj_parm)
  408. {
  409. static const T fourth_pi = detail::fourth_pi<T>();
  410. static const T half_pi = detail::half_pi<T>();
  411. /* Determine the cube face from the center of projection. */
  412. if (par.phi0 >= half_pi - fourth_pi / 2.0) {
  413. proj_parm.face = face_top;
  414. } else if (par.phi0 <= -(half_pi - fourth_pi / 2.0)) {
  415. proj_parm.face = face_bottom;
  416. } else if (fabs(par.lam0) <= fourth_pi) {
  417. proj_parm.face = face_front;
  418. } else if (fabs(par.lam0) <= half_pi + fourth_pi) {
  419. proj_parm.face = (par.lam0 > 0.0 ? face_right : face_left);
  420. } else {
  421. proj_parm.face = face_back;
  422. }
  423. /* Fill in useful values for the ellipsoid <-> sphere shift
  424. * described in [LK12]. */
  425. if (par.es != 0.0) {
  426. proj_parm.a_squared = par.a * par.a;
  427. proj_parm.b = par.a * sqrt(1.0 - par.es);
  428. proj_parm.one_minus_f = 1.0 - (par.a - proj_parm.b) / par.a;
  429. proj_parm.one_minus_f_squared = proj_parm.one_minus_f * proj_parm.one_minus_f;
  430. }
  431. }
  432. }} // namespace detail::qsc
  433. #endif // doxygen
  434. /*!
  435. \brief Quadrilateralized Spherical Cube projection
  436. \ingroup projections
  437. \tparam Geographic latlong point type
  438. \tparam Cartesian xy point type
  439. \tparam Parameters parameter type
  440. \par Projection characteristics
  441. - Azimuthal
  442. - Spheroid
  443. \par Example
  444. \image html ex_qsc.gif
  445. */
  446. template <typename T, typename Parameters>
  447. struct qsc_ellipsoid : public detail::qsc::base_qsc_ellipsoid<T, Parameters>
  448. {
  449. template <typename Params>
  450. inline qsc_ellipsoid(Params const& , Parameters const& par)
  451. {
  452. detail::qsc::setup_qsc(par, this->m_proj_parm);
  453. }
  454. };
  455. #ifndef DOXYGEN_NO_DETAIL
  456. namespace detail
  457. {
  458. // Static projection
  459. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_qsc, qsc_ellipsoid)
  460. // Factory entry(s)
  461. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(qsc_entry, qsc_ellipsoid)
  462. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(qsc_init)
  463. {
  464. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(qsc, qsc_entry)
  465. }
  466. } // namespace detail
  467. #endif // doxygen
  468. } // namespace projections
  469. }} // namespace boost::geometry
  470. #endif // BOOST_GEOMETRY_PROJECTIONS_QSC_HPP