healpix.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. // Purpose: Implementation of the HEALPix and rHEALPix projections.
  16. // For background see <http://code.scenzgrid.org/index.php/p/scenzgrid-py/source/tree/master/docs/rhealpix_dggs.pdf>.
  17. // Authors: Alex Raichev (raichev@cs.auckland.ac.nz)
  18. // Michael Speth (spethm@landcareresearch.co.nz)
  19. // Notes: Raichev implemented these projections in Python and
  20. // Speth translated them into C here.
  21. // Copyright (c) 2001, Thomas Flemming, tf@ttqv.com
  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. // The above copyright notice and this permission notice shall be included
  29. // in all copies or substantial portions of the Software.
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  31. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  32. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  33. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  34. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  35. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  36. // DEALINGS IN THE SOFTWARE.
  37. #ifndef BOOST_GEOMETRY_PROJECTIONS_HEALPIX_HPP
  38. #define BOOST_GEOMETRY_PROJECTIONS_HEALPIX_HPP
  39. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  40. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  41. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  42. #include <boost/geometry/srs/projections/impl/pj_auth.hpp>
  43. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  44. #include <boost/geometry/srs/projections/impl/pj_qsfn.hpp>
  45. #include <boost/geometry/srs/projections/impl/projects.hpp>
  46. #include <boost/geometry/util/math.hpp>
  47. namespace boost { namespace geometry
  48. {
  49. namespace projections
  50. {
  51. #ifndef DOXYGEN_NO_DETAIL
  52. namespace detail { namespace healpix
  53. {
  54. /* Fuzz to handle rounding errors: */
  55. static const double epsilon = 1e-15;
  56. template <typename T>
  57. struct par_healpix
  58. {
  59. T qp;
  60. detail::apa<T> apa;
  61. int north_square;
  62. int south_square;
  63. };
  64. template <typename T>
  65. struct cap_map
  66. {
  67. T x, y; /* Coordinates of the pole point (point of most extreme latitude on the polar caps). */
  68. int cn; /* An integer 0--3 indicating the position of the polar cap. */
  69. enum region_type {north, south, equatorial} region;
  70. };
  71. template <typename T>
  72. struct point_xy
  73. {
  74. T x, y;
  75. };
  76. /* IDENT, R1, R2, R3, R1 inverse, R2 inverse, R3 inverse:*/
  77. static double rot[7][2][2] = {
  78. /* Identity matrix */
  79. {{1, 0},{0, 1}},
  80. /* Matrix for counterclockwise rotation by pi/2: */
  81. {{ 0,-1},{ 1, 0}},
  82. /* Matrix for counterclockwise rotation by pi: */
  83. {{-1, 0},{ 0,-1}},
  84. /* Matrix for counterclockwise rotation by 3*pi/2: */
  85. {{ 0, 1},{-1, 0}},
  86. {{ 0, 1},{-1, 0}}, // 3*pi/2
  87. {{-1, 0},{ 0,-1}}, // pi
  88. {{ 0,-1},{ 1, 0}} // pi/2
  89. };
  90. /**
  91. * Returns the sign of the double.
  92. * @param v the parameter whose sign is returned.
  93. * @return 1 for positive number, -1 for negative, and 0 for zero.
  94. **/
  95. template <typename T>
  96. inline T pj_sign (T const& v)
  97. {
  98. return v > 0 ? 1 : (v < 0 ? -1 : 0);
  99. }
  100. /**
  101. * Return the index of the matrix in {{{1, 0},{0, 1}}, {{ 0,-1},{ 1, 0}}, {{-1, 0},{ 0,-1}}, {{ 0, 1},{-1, 0}}, {{ 0, 1},{-1, 0}}, {{-1, 0},{ 0,-1}}, {{ 0,-1},{ 1, 0}}}.
  102. * @param index ranges from -3 to 3.
  103. */
  104. inline int get_rotate_index(int index)
  105. {
  106. switch(index) {
  107. case 0:
  108. return 0;
  109. case 1:
  110. return 1;
  111. case 2:
  112. return 2;
  113. case 3:
  114. return 3;
  115. case -1:
  116. return 4;
  117. case -2:
  118. return 5;
  119. case -3:
  120. return 6;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * Return 1 if point (testx, testy) lies in the interior of the polygon
  126. * determined by the vertices in vert, and return 0 otherwise.
  127. * See http://paulbourke.net/geometry/polygonmesh/ for more details.
  128. * @param nvert the number of vertices in the polygon.
  129. * @param vert the (x, y)-coordinates of the polygon's vertices
  130. **/
  131. template <typename T>
  132. inline int pnpoly(int nvert, T vert[][2], T const& testx, T const& testy)
  133. {
  134. int i;
  135. int counter = 0;
  136. T xinters;
  137. point_xy<T> p1, p2;
  138. /* Check for boundrary cases */
  139. for (i = 0; i < nvert; i++) {
  140. if (testx == vert[i][0] && testy == vert[i][1]) {
  141. return 1;
  142. }
  143. }
  144. p1.x = vert[0][0];
  145. p1.y = vert[0][1];
  146. for (i = 1; i < nvert; i++) {
  147. p2.x = vert[i % nvert][0];
  148. p2.y = vert[i % nvert][1];
  149. if (testy > (std::min)(p1.y, p2.y) &&
  150. testy <= (std::max)(p1.y, p2.y) &&
  151. testx <= (std::max)(p1.x, p2.x) &&
  152. p1.y != p2.y)
  153. {
  154. xinters = (testy-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x;
  155. if (p1.x == p2.x || testx <= xinters)
  156. counter++;
  157. }
  158. p1 = p2;
  159. }
  160. if (counter % 2 == 0) {
  161. return 0;
  162. } else {
  163. return 1;
  164. }
  165. }
  166. /**
  167. * Return 1 if (x, y) lies in (the interior or boundary of) the image of the
  168. * HEALPix projection (in case proj=0) or in the image the rHEALPix projection
  169. * (in case proj=1), and return 0 otherwise.
  170. * @param north_square the position of the north polar square (rHEALPix only)
  171. * @param south_square the position of the south polar square (rHEALPix only)
  172. **/
  173. template <typename T>
  174. inline int in_image(T const& x, T const& y, int proj, int north_square, int south_square)
  175. {
  176. static const T pi = detail::pi<T>();
  177. static const T half_pi = detail::half_pi<T>();
  178. static const T fourth_pi = detail::fourth_pi<T>();
  179. if (proj == 0) {
  180. T healpixVertsJit[][2] = {
  181. {-pi - epsilon, fourth_pi},
  182. {-3.0*fourth_pi, half_pi + epsilon},
  183. {-half_pi, fourth_pi + epsilon},
  184. {-fourth_pi, half_pi + epsilon},
  185. {0.0, fourth_pi + epsilon},
  186. {fourth_pi, half_pi + epsilon},
  187. {half_pi, fourth_pi + epsilon},
  188. {3.0*fourth_pi, half_pi + epsilon},
  189. {pi + epsilon, fourth_pi},
  190. {pi + epsilon, -fourth_pi},
  191. {3.0*fourth_pi, -half_pi - epsilon},
  192. {half_pi, -fourth_pi - epsilon},
  193. {fourth_pi, -half_pi - epsilon},
  194. {0.0, -fourth_pi - epsilon},
  195. {-fourth_pi, -half_pi - epsilon},
  196. {-half_pi, -fourth_pi - epsilon},
  197. {-3.0*fourth_pi, -half_pi - epsilon},
  198. {-pi - epsilon, -fourth_pi}
  199. };
  200. return pnpoly((int)sizeof(healpixVertsJit)/
  201. sizeof(healpixVertsJit[0]), healpixVertsJit, x, y);
  202. } else {
  203. T rhealpixVertsJit[][2] = {
  204. {-pi - epsilon, fourth_pi + epsilon},
  205. {-pi + north_square*half_pi - epsilon, fourth_pi + epsilon},
  206. {-pi + north_square*half_pi - epsilon, 3.0*fourth_pi + epsilon},
  207. {-pi + (north_square + 1.0)*half_pi + epsilon, 3.0*fourth_pi + epsilon},
  208. {-pi + (north_square + 1.0)*half_pi + epsilon, fourth_pi + epsilon},
  209. {pi + epsilon, fourth_pi + epsilon},
  210. {pi + epsilon, -fourth_pi - epsilon},
  211. {-pi + (south_square + 1.0)*half_pi + epsilon, -fourth_pi - epsilon},
  212. {-pi + (south_square + 1.0)*half_pi + epsilon, -3.0*fourth_pi - epsilon},
  213. {-pi + south_square*half_pi - epsilon, -3.0*fourth_pi - epsilon},
  214. {-pi + south_square*half_pi - epsilon, -fourth_pi - epsilon},
  215. {-pi - epsilon, -fourth_pi - epsilon}
  216. };
  217. return pnpoly((int)sizeof(rhealpixVertsJit)/
  218. sizeof(rhealpixVertsJit[0]), rhealpixVertsJit, x, y);
  219. }
  220. }
  221. /**
  222. * Return the authalic latitude of latitude alpha (if inverse=0) or
  223. * return the approximate latitude of authalic latitude alpha (if inverse=1).
  224. * P contains the relavent ellipsoid parameters.
  225. **/
  226. template <typename Parameters, typename T>
  227. inline T auth_lat(const Parameters& par, const par_healpix<T>& proj_parm, T const& alpha, int inverse)
  228. {
  229. if (inverse == 0) {
  230. /* Authalic latitude. */
  231. T q = pj_qsfn(sin(alpha), par.e, 1.0 - par.es);
  232. T qp = proj_parm.qp;
  233. T ratio = q/qp;
  234. if (math::abs(ratio) > 1) {
  235. /* Rounding error. */
  236. ratio = pj_sign(ratio);
  237. }
  238. return asin(ratio);
  239. } else {
  240. /* Approximation to inverse authalic latitude. */
  241. return pj_authlat(alpha, proj_parm.apa);
  242. }
  243. }
  244. /**
  245. * Return the HEALPix projection of the longitude-latitude point lp on
  246. * the unit sphere.
  247. **/
  248. template <typename T>
  249. inline void healpix_sphere(T const& lp_lam, T const& lp_phi, T& xy_x, T& xy_y)
  250. {
  251. static const T pi = detail::pi<T>();
  252. static const T half_pi = detail::half_pi<T>();
  253. static const T fourth_pi = detail::fourth_pi<T>();
  254. T lam = lp_lam;
  255. T phi = lp_phi;
  256. T phi0 = asin(T(2.0)/T(3.0));
  257. /* equatorial region */
  258. if ( fabsl(phi) <= phi0) {
  259. xy_x = lam;
  260. xy_y = 3.0*pi/8.0*sin(phi);
  261. } else {
  262. T lamc;
  263. T sigma = sqrt(3.0*(1 - math::abs(sin(phi))));
  264. T cn = floor(2*lam / pi + 2);
  265. if (cn >= 4) {
  266. cn = 3;
  267. }
  268. lamc = -3*fourth_pi + half_pi*cn;
  269. xy_x = lamc + (lam - lamc)*sigma;
  270. xy_y = pj_sign(phi)*fourth_pi*(2 - sigma);
  271. }
  272. return;
  273. }
  274. /**
  275. * Return the inverse of healpix_sphere().
  276. **/
  277. template <typename T>
  278. inline void healpix_sphere_inverse(T const& xy_x, T const& xy_y, T& lp_lam, T& lp_phi)
  279. {
  280. static const T pi = detail::pi<T>();
  281. static const T half_pi = detail::half_pi<T>();
  282. static const T fourth_pi = detail::fourth_pi<T>();
  283. T x = xy_x;
  284. T y = xy_y;
  285. T y0 = fourth_pi;
  286. /* Equatorial region. */
  287. if (math::abs(y) <= y0) {
  288. lp_lam = x;
  289. lp_phi = asin(8.0*y/(3.0*pi));
  290. } else if (fabsl(y) < half_pi) {
  291. T cn = floor(2.0*x/pi + 2.0);
  292. T xc, tau;
  293. if (cn >= 4) {
  294. cn = 3;
  295. }
  296. xc = -3.0*fourth_pi + (half_pi)*cn;
  297. tau = 2.0 - 4.0*fabsl(y)/pi;
  298. lp_lam = xc + (x - xc)/tau;
  299. lp_phi = pj_sign(y)*asin(1.0 - math::pow(tau, 2)/3.0);
  300. } else {
  301. lp_lam = -1.0*pi;
  302. lp_phi = pj_sign(y)*half_pi;
  303. }
  304. return;
  305. }
  306. /**
  307. * Return the vector sum a + b, where a and b are 2-dimensional vectors.
  308. * @param ret holds a + b.
  309. **/
  310. template <typename T>
  311. inline void vector_add(const T a[2], const T b[2], T ret[2])
  312. {
  313. int i;
  314. for(i = 0; i < 2; i++) {
  315. ret[i] = a[i] + b[i];
  316. }
  317. }
  318. /**
  319. * Return the vector difference a - b, where a and b are 2-dimensional vectors.
  320. * @param ret holds a - b.
  321. **/
  322. template <typename T>
  323. inline void vector_sub(const T a[2], const T b[2], T ret[2])
  324. {
  325. int i;
  326. for(i = 0; i < 2; i++) {
  327. ret[i] = a[i] - b[i];
  328. }
  329. }
  330. /**
  331. * Return the 2 x 1 matrix product a*b, where a is a 2 x 2 matrix and
  332. * b is a 2 x 1 matrix.
  333. * @param ret holds a*b.
  334. **/
  335. template <typename T1, typename T2>
  336. inline void dot_product(const T1 a[2][2], const T2 b[2], T2 ret[2])
  337. {
  338. int i, j;
  339. int length = 2;
  340. for(i = 0; i < length; i++) {
  341. ret[i] = 0;
  342. for(j = 0; j < length; j++) {
  343. ret[i] += a[i][j]*b[j];
  344. }
  345. }
  346. }
  347. /**
  348. * Return the number of the polar cap, the pole point coordinates, and
  349. * the region that (x, y) lies in.
  350. * If inverse=0, then assume (x,y) lies in the image of the HEALPix
  351. * projection of the unit sphere.
  352. * If inverse=1, then assume (x,y) lies in the image of the
  353. * (north_square, south_square)-rHEALPix projection of the unit sphere.
  354. **/
  355. template <typename T>
  356. inline cap_map<T> get_cap(T x, T const& y, int north_square, int south_square,
  357. int inverse)
  358. {
  359. static const T pi = detail::pi<T>();
  360. static const T half_pi = detail::half_pi<T>();
  361. static const T fourth_pi = detail::fourth_pi<T>();
  362. cap_map<T> capmap;
  363. T c;
  364. capmap.x = x;
  365. capmap.y = y;
  366. if (inverse == 0) {
  367. if (y > fourth_pi) {
  368. capmap.region = cap_map<T>::north;
  369. c = half_pi;
  370. } else if (y < -fourth_pi) {
  371. capmap.region = cap_map<T>::south;
  372. c = -half_pi;
  373. } else {
  374. capmap.region = cap_map<T>::equatorial;
  375. capmap.cn = 0;
  376. return capmap;
  377. }
  378. /* polar region */
  379. if (x < -half_pi) {
  380. capmap.cn = 0;
  381. capmap.x = (-3.0*fourth_pi);
  382. capmap.y = c;
  383. } else if (x >= -half_pi && x < 0) {
  384. capmap.cn = 1;
  385. capmap.x = -fourth_pi;
  386. capmap.y = c;
  387. } else if (x >= 0 && x < half_pi) {
  388. capmap.cn = 2;
  389. capmap.x = fourth_pi;
  390. capmap.y = c;
  391. } else {
  392. capmap.cn = 3;
  393. capmap.x = 3.0*fourth_pi;
  394. capmap.y = c;
  395. }
  396. } else {
  397. if (y > fourth_pi) {
  398. capmap.region = cap_map<T>::north;
  399. capmap.x = (-3.0*fourth_pi + north_square*half_pi);
  400. capmap.y = half_pi;
  401. x = x - north_square*half_pi;
  402. } else if (y < -fourth_pi) {
  403. capmap.region = cap_map<T>::south;
  404. capmap.x = (-3.0*fourth_pi + south_square*pi/2);
  405. capmap.y = -half_pi;
  406. x = x - south_square*half_pi;
  407. } else {
  408. capmap.region = cap_map<T>::equatorial;
  409. capmap.cn = 0;
  410. return capmap;
  411. }
  412. /* Polar Region, find the HEALPix polar cap number that
  413. x, y moves to when rHEALPix polar square is disassembled. */
  414. if (capmap.region == cap_map<T>::north) {
  415. if (y >= -x - fourth_pi - epsilon && y < x + 5.0*fourth_pi - epsilon) {
  416. capmap.cn = (north_square + 1) % 4;
  417. } else if (y > -x -fourth_pi + epsilon && y >= x + 5.0*fourth_pi - epsilon) {
  418. capmap.cn = (north_square + 2) % 4;
  419. } else if (y <= -x -fourth_pi + epsilon && y > x + 5.0*fourth_pi + epsilon) {
  420. capmap.cn = (north_square + 3) % 4;
  421. } else {
  422. capmap.cn = north_square;
  423. }
  424. } else if (capmap.region == cap_map<T>::south) {
  425. if (y <= x + fourth_pi + epsilon && y > -x - 5.0*fourth_pi + epsilon) {
  426. capmap.cn = (south_square + 1) % 4;
  427. } else if (y < x + fourth_pi - epsilon && y <= -x - 5.0*fourth_pi + epsilon) {
  428. capmap.cn = (south_square + 2) % 4;
  429. } else if (y >= x + fourth_pi - epsilon && y < -x - 5.0*fourth_pi - epsilon) {
  430. capmap.cn = (south_square + 3) % 4;
  431. } else {
  432. capmap.cn = south_square;
  433. }
  434. }
  435. }
  436. return capmap;
  437. }
  438. /**
  439. * Rearrange point (x, y) in the HEALPix projection by
  440. * combining the polar caps into two polar squares.
  441. * Put the north polar square in position north_square and
  442. * the south polar square in position south_square.
  443. * If inverse=1, then uncombine the polar caps.
  444. * @param north_square integer between 0 and 3.
  445. * @param south_square integer between 0 and 3.
  446. **/
  447. template <typename T>
  448. inline void combine_caps(T& xy_x, T& xy_y, int north_square, int south_square,
  449. int inverse)
  450. {
  451. static const T half_pi = detail::half_pi<T>();
  452. static const T fourth_pi = detail::fourth_pi<T>();
  453. T v[2];
  454. T c[2];
  455. T vector[2];
  456. T v_min_c[2];
  457. T ret_dot[2];
  458. const double (*tmpRot)[2];
  459. int pole = 0;
  460. cap_map<T> capmap = get_cap(xy_x, xy_y, north_square, south_square, inverse);
  461. if (capmap.region == cap_map<T>::equatorial) {
  462. xy_x = capmap.x;
  463. xy_y = capmap.y;
  464. return;
  465. }
  466. v[0] = xy_x; v[1] = xy_y;
  467. c[0] = capmap.x; c[1] = capmap.y;
  468. if (inverse == 0) {
  469. /* Rotate (xy_x, xy_y) about its polar cap tip and then translate it to
  470. north_square or south_square. */
  471. if (capmap.region == cap_map<T>::north) {
  472. pole = north_square;
  473. tmpRot = rot[get_rotate_index(capmap.cn - pole)];
  474. } else {
  475. pole = south_square;
  476. tmpRot = rot[get_rotate_index(-1*(capmap.cn - pole))];
  477. }
  478. } else {
  479. /* Inverse function.
  480. Unrotate (xy_x, xy_y) and then translate it back. */
  481. /* disassemble */
  482. if (capmap.region == cap_map<T>::north) {
  483. pole = north_square;
  484. tmpRot = rot[get_rotate_index(-1*(capmap.cn - pole))];
  485. } else {
  486. pole = south_square;
  487. tmpRot = rot[get_rotate_index(capmap.cn - pole)];
  488. }
  489. }
  490. vector_sub(v, c, v_min_c);
  491. dot_product(tmpRot, v_min_c, ret_dot);
  492. {
  493. T a[2];
  494. /* Workaround cppcheck git issue */
  495. T* pa = a;
  496. // TODO: in proj4 5.0.0 this line is used instead
  497. //pa[0] = -3.0*fourth_pi + ((inverse == 0) ? 0 : capmap.cn) *half_pi;
  498. pa[0] = -3.0*fourth_pi + ((inverse == 0) ? pole : capmap.cn) *half_pi;
  499. pa[1] = half_pi;
  500. vector_add(ret_dot, a, vector);
  501. }
  502. xy_x = vector[0];
  503. xy_y = vector[1];
  504. }
  505. template <typename T, typename Parameters>
  506. struct base_healpix_ellipsoid
  507. {
  508. par_healpix<T> m_proj_parm;
  509. // FORWARD(e_healpix_forward) ellipsoid
  510. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  511. inline void fwd(Parameters const& par, T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
  512. {
  513. lp_lat = auth_lat(par, m_proj_parm, lp_lat, 0);
  514. return healpix_sphere(lp_lon, lp_lat, xy_x, xy_y);
  515. }
  516. // INVERSE(e_healpix_inverse) ellipsoid
  517. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  518. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  519. {
  520. /* Check whether (x, y) lies in the HEALPix image. */
  521. if (in_image(xy_x, xy_y, 0, 0, 0) == 0) {
  522. lp_lon = HUGE_VAL;
  523. lp_lat = HUGE_VAL;
  524. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_x_or_y) );
  525. }
  526. healpix_sphere_inverse(xy_x, xy_y, lp_lon, lp_lat);
  527. lp_lat = auth_lat(par, m_proj_parm, lp_lat, 1);
  528. }
  529. static inline std::string get_name()
  530. {
  531. return "healpix_ellipsoid";
  532. }
  533. };
  534. template <typename T, typename Parameters>
  535. struct base_healpix_spheroid
  536. {
  537. par_healpix<T> m_proj_parm;
  538. // FORWARD(s_healpix_forward) sphere
  539. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  540. inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  541. {
  542. return healpix_sphere(lp_lon, lp_lat, xy_x, xy_y);
  543. }
  544. // INVERSE(s_healpix_inverse) sphere
  545. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  546. inline void inv(Parameters const& , T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  547. {
  548. /* Check whether (x, y) lies in the HEALPix image */
  549. if (in_image(xy_x, xy_y, 0, 0, 0) == 0) {
  550. lp_lon = HUGE_VAL;
  551. lp_lat = HUGE_VAL;
  552. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_x_or_y) );
  553. }
  554. return healpix_sphere_inverse(xy_x, xy_y, lp_lon, lp_lat);
  555. }
  556. static inline std::string get_name()
  557. {
  558. return "healpix_spheroid";
  559. }
  560. };
  561. template <typename T, typename Parameters>
  562. struct base_rhealpix_ellipsoid
  563. {
  564. par_healpix<T> m_proj_parm;
  565. // FORWARD(e_rhealpix_forward) ellipsoid
  566. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  567. inline void fwd(Parameters const& par, T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
  568. {
  569. lp_lat = auth_lat(par, m_proj_parm, lp_lat, 0);
  570. healpix_sphere(lp_lon, lp_lat, xy_x, xy_y);
  571. combine_caps(xy_x, xy_y, this->m_proj_parm.north_square, this->m_proj_parm.south_square, 0);
  572. }
  573. // INVERSE(e_rhealpix_inverse) ellipsoid
  574. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  575. inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  576. {
  577. /* Check whether (x, y) lies in the rHEALPix image. */
  578. if (in_image(xy_x, xy_y, 1, this->m_proj_parm.north_square, this->m_proj_parm.south_square) == 0) {
  579. lp_lon = HUGE_VAL;
  580. lp_lat = HUGE_VAL;
  581. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_x_or_y) );
  582. }
  583. combine_caps(xy_x, xy_y, this->m_proj_parm.north_square, this->m_proj_parm.south_square, 1);
  584. healpix_sphere_inverse(xy_x, xy_y, lp_lon, lp_lat);
  585. lp_lat = auth_lat(par, m_proj_parm, lp_lat, 1);
  586. }
  587. static inline std::string get_name()
  588. {
  589. return "rhealpix_ellipsoid";
  590. }
  591. };
  592. template <typename T, typename Parameters>
  593. struct base_rhealpix_spheroid
  594. {
  595. par_healpix<T> m_proj_parm;
  596. // FORWARD(s_rhealpix_forward) sphere
  597. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  598. inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  599. {
  600. healpix_sphere(lp_lon, lp_lat, xy_x, xy_y);
  601. combine_caps(xy_x, xy_y, this->m_proj_parm.north_square, this->m_proj_parm.south_square, 0);
  602. }
  603. // INVERSE(s_rhealpix_inverse) sphere
  604. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  605. inline void inv(Parameters const& , T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  606. {
  607. /* Check whether (x, y) lies in the rHEALPix image. */
  608. if (in_image(xy_x, xy_y, 1, this->m_proj_parm.north_square, this->m_proj_parm.south_square) == 0) {
  609. lp_lon = HUGE_VAL;
  610. lp_lat = HUGE_VAL;
  611. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_x_or_y) );
  612. }
  613. combine_caps(xy_x, xy_y, this->m_proj_parm.north_square, this->m_proj_parm.south_square, 1);
  614. return healpix_sphere_inverse(xy_x, xy_y, lp_lon, lp_lat);
  615. }
  616. static inline std::string get_name()
  617. {
  618. return "rhealpix_spheroid";
  619. }
  620. };
  621. // HEALPix
  622. template <typename Parameters, typename T>
  623. inline void setup_healpix(Parameters& par, par_healpix<T>& proj_parm)
  624. {
  625. if (par.es != 0.0) {
  626. proj_parm.apa = pj_authset<T>(par.es); /* For auth_lat(). */
  627. proj_parm.qp = pj_qsfn(1.0, par.e, par.one_es); /* For auth_lat(). */
  628. par.a = par.a*sqrt(0.5*proj_parm.qp); /* Set par.a to authalic radius. */
  629. pj_calc_ellipsoid_params(par, par.a, par.es); /* Ensure we have a consistent parameter set */
  630. } else {
  631. }
  632. }
  633. // rHEALPix
  634. template <typename Params, typename Parameters, typename T>
  635. inline void setup_rhealpix(Params const& params, Parameters& par, par_healpix<T>& proj_parm)
  636. {
  637. proj_parm.north_square = pj_get_param_i<srs::spar::north_square>(params, "north_square", srs::dpar::north_square);
  638. proj_parm.south_square = pj_get_param_i<srs::spar::south_square>(params, "south_square", srs::dpar::south_square);
  639. /* Check for valid north_square and south_square inputs. */
  640. if ((proj_parm.north_square < 0) || (proj_parm.north_square > 3)) {
  641. BOOST_THROW_EXCEPTION( projection_exception(error_axis) );
  642. }
  643. if ((proj_parm.south_square < 0) || (proj_parm.south_square > 3)) {
  644. BOOST_THROW_EXCEPTION( projection_exception(error_axis) );
  645. }
  646. if (par.es != 0.0) {
  647. proj_parm.apa = pj_authset<T>(par.es); /* For auth_lat(). */
  648. proj_parm.qp = pj_qsfn(1.0, par.e, par.one_es); /* For auth_lat(). */
  649. par.a = par.a*sqrt(0.5*proj_parm.qp); /* Set par.a to authalic radius. */
  650. // TODO: why not the same as in healpix?
  651. //pj_calc_ellipsoid_params(par, par.a, par.es);
  652. par.ra = 1.0/par.a;
  653. } else {
  654. }
  655. }
  656. }} // namespace detail::healpix
  657. #endif // doxygen
  658. /*!
  659. \brief HEALPix projection
  660. \ingroup projections
  661. \tparam Geographic latlong point type
  662. \tparam Cartesian xy point type
  663. \tparam Parameters parameter type
  664. \par Projection characteristics
  665. - Spheroid
  666. - Ellipsoid
  667. \par Example
  668. \image html ex_healpix.gif
  669. */
  670. template <typename T, typename Parameters>
  671. struct healpix_ellipsoid : public detail::healpix::base_healpix_ellipsoid<T, Parameters>
  672. {
  673. template <typename Params>
  674. inline healpix_ellipsoid(Params const& , Parameters & par)
  675. {
  676. detail::healpix::setup_healpix(par, this->m_proj_parm);
  677. }
  678. };
  679. /*!
  680. \brief HEALPix projection
  681. \ingroup projections
  682. \tparam Geographic latlong point type
  683. \tparam Cartesian xy point type
  684. \tparam Parameters parameter type
  685. \par Projection characteristics
  686. - Spheroid
  687. - Ellipsoid
  688. \par Example
  689. \image html ex_healpix.gif
  690. */
  691. template <typename T, typename Parameters>
  692. struct healpix_spheroid : public detail::healpix::base_healpix_spheroid<T, Parameters>
  693. {
  694. template <typename Params>
  695. inline healpix_spheroid(Params const& , Parameters & par)
  696. {
  697. detail::healpix::setup_healpix(par, this->m_proj_parm);
  698. }
  699. };
  700. /*!
  701. \brief rHEALPix projection
  702. \ingroup projections
  703. \tparam Geographic latlong point type
  704. \tparam Cartesian xy point type
  705. \tparam Parameters parameter type
  706. \par Projection characteristics
  707. - Spheroid
  708. - Ellipsoid
  709. \par Projection parameters
  710. - north_square (integer)
  711. - south_square (integer)
  712. \par Example
  713. \image html ex_rhealpix.gif
  714. */
  715. template <typename T, typename Parameters>
  716. struct rhealpix_ellipsoid : public detail::healpix::base_rhealpix_ellipsoid<T, Parameters>
  717. {
  718. template <typename Params>
  719. inline rhealpix_ellipsoid(Params const& params, Parameters & par)
  720. {
  721. detail::healpix::setup_rhealpix(params, par, this->m_proj_parm);
  722. }
  723. };
  724. /*!
  725. \brief rHEALPix projection
  726. \ingroup projections
  727. \tparam Geographic latlong point type
  728. \tparam Cartesian xy point type
  729. \tparam Parameters parameter type
  730. \par Projection characteristics
  731. - Spheroid
  732. - Ellipsoid
  733. \par Projection parameters
  734. - north_square (integer)
  735. - south_square (integer)
  736. \par Example
  737. \image html ex_rhealpix.gif
  738. */
  739. template <typename T, typename Parameters>
  740. struct rhealpix_spheroid : public detail::healpix::base_rhealpix_spheroid<T, Parameters>
  741. {
  742. template <typename Params>
  743. inline rhealpix_spheroid(Params const& params, Parameters & par)
  744. {
  745. detail::healpix::setup_rhealpix(params, par, this->m_proj_parm);
  746. }
  747. };
  748. #ifndef DOXYGEN_NO_DETAIL
  749. namespace detail
  750. {
  751. // Static projection
  752. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_healpix, healpix_spheroid, healpix_ellipsoid)
  753. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_rhealpix, rhealpix_spheroid, rhealpix_ellipsoid)
  754. // Factory entry(s)
  755. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(healpix_entry, healpix_spheroid, healpix_ellipsoid)
  756. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(rhealpix_entry, rhealpix_spheroid, rhealpix_ellipsoid)
  757. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(healpix_init)
  758. {
  759. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(healpix, healpix_entry)
  760. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(rhealpix, rhealpix_entry)
  761. }
  762. } // namespace detail
  763. #endif // doxygen
  764. } // namespace projections
  765. }} // namespace boost::geometry
  766. #endif // BOOST_GEOMETRY_PROJECTIONS_HEALPIX_HPP