healpix.hpp 37 KB

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