Healpix C++  3.82
healpix_base.cc
1 /*
2  * This file is part of Healpix_cxx.
3  *
4  * Healpix_cxx is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Healpix_cxx is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Healpix_cxx; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * For more information about HEALPix, see http://healpix.sourceforge.net
19  */
20 
21 /*
22  * Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
23  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
24  * (DLR).
25  */
26 
27 /*
28  * Copyright (C) 2003-2016 Max-Planck-Society
29  * Author: Martin Reinecke
30  */
31 
32 #include "healpix_base.h"
33 #include "geom_utils.h"
34 #include "lsconstants.h"
35 
36 using namespace std;
37 
38 template<typename I> int T_Healpix_Base<I>::nside2order (I nside)
39  {
40  planck_assert (nside>I(0), "invalid value for Nside");
41  return ((nside)&(nside-1)) ? -1 : ilog2(nside);
42  }
43 template<typename I> I T_Healpix_Base<I>::npix2nside (I npix)
44  {
45  I res=isqrt(npix/I(12));
46  planck_assert (npix==res*res*I(12), "invalid value for npix");
47  return res;
48  }
49 
50 template<typename I> I T_Healpix_Base<I>::ring_above (double z) const
51  {
52  double az=abs(z);
53  if (az<=twothird) // equatorial region
54  return I(nside_*(2-1.5*z));
55  I iring = I(nside_*sqrt(3*(1-az)));
56  return (z>0) ? iring : 4*nside_-iring-1;
57  }
58 
59 namespace {
60 
61 /* Short note on the "zone":
62  zone = 0: pixel lies completely outside the queried shape
63  1: pixel may overlap with the shape, pixel center is outside
64  2: pixel center is inside the shape, but maybe not the complete pixel
65  3: pixel lies completely inside the shape */
66 
67 template<typename I, typename I2> inline void check_pixel (int o, int order_,
68  int omax, int zone, rangeset<I2> &pixset, I pix, vector<pair<I,int> > &stk,
69  bool inclusive, int &stacktop)
70  {
71  if (zone==0) return;
72 
73  if (o<order_)
74  {
75  if (zone>=3)
76  {
77  int sdist=2*(order_-o); // the "bit-shift distance" between map orders
78  pixset.append(pix<<sdist,(pix+1)<<sdist); // output all subpixels
79  }
80  else // (1<=zone<=2)
81  for (int i=0; i<4; ++i)
82  stk.push_back(make_pair(4*pix+3-i,o+1)); // add children
83  }
84  else if (o>order_) // this implies that inclusive==true
85  {
86  if (zone>=2) // pixel center in shape
87  {
88  pixset.append(pix>>(2*(o-order_))); // output the parent pixel at order_
89  stk.resize(stacktop); // unwind the stack
90  }
91  else // (zone==1): pixel center in safety range
92  {
93  if (o<omax) // check sublevels
94  for (int i=0; i<4; ++i) // add children in reverse order
95  stk.push_back(make_pair(4*pix+3-i,o+1));
96  else // at resolution limit
97  {
98  pixset.append(pix>>(2*(o-order_))); // output the parent pixel at order_
99  stk.resize(stacktop); // unwind the stack
100  }
101  }
102  }
103  else // o==order_
104  {
105  if (zone>=2)
106  pixset.append(pix);
107  else if (inclusive) // and (zone>=1)
108  {
109  if (order_<omax) // check sublevels
110  {
111  stacktop=stk.size(); // remember current stack position
112  for (int i=0; i<4; ++i) // add children in reverse order
113  stk.push_back(make_pair(4*pix+3-i,o+1));
114  }
115  else // at resolution limit
116  pixset.append(pix); // output the pixel
117  }
118  }
119  }
120 
121 template<typename I> bool check_pixel_ring (const T_Healpix_Base<I> &b1,
122  const T_Healpix_Base<I> &b2, I pix, I nr, I ipix1, int fct,
123  double cz, double cphi, double cosrp2, I cpix)
124  {
125  if (pix>=nr) pix-=nr;
126  if (pix<0) pix+=nr;
127  pix+=ipix1;
128  if (pix==cpix) return false; // disk center in pixel => overlap
129  int px,py,pf;
130  b1.pix2xyf(pix,px,py,pf);
131  for (int i=0; i<fct-1; ++i) // go along the 4 edges
132  {
133  I ox=fct*px, oy=fct*py;
134  double pz,pphi;
135  b2.pix2zphi(b2.xyf2pix(ox+i,oy,pf),pz,pphi);
136  if (cosdist_zphi(pz,pphi,cz,cphi)>cosrp2) // overlap
137  return false;
138  b2.pix2zphi(b2.xyf2pix(ox+fct-1,oy+i,pf),pz,pphi);
139  if (cosdist_zphi(pz,pphi,cz,cphi)>cosrp2) // overlap
140  return false;
141  b2.pix2zphi(b2.xyf2pix(ox+fct-1-i,oy+fct-1,pf),pz,pphi);
142  if (cosdist_zphi(pz,pphi,cz,cphi)>cosrp2) // overlap
143  return false;
144  b2.pix2zphi(b2.xyf2pix(ox,oy+fct-1-i,pf),pz,pphi);
145  if (cosdist_zphi(pz,pphi,cz,cphi)>cosrp2) // overlap
146  return false;
147  }
148  return true;
149  }
150 
151 } // unnamed namespace
152 
153 template<typename I> template<typename I2>
155  (pointing ptg, double radius, int fact, rangeset<I2> &pixset) const
156  {
157  bool inclusive = (fact!=0);
158  pixset.clear();
159  ptg.normalize();
160 
161  if (scheme_==RING)
162  {
163  I fct=1;
164  if (inclusive)
165  {
166  planck_assert (((I(1)<<order_max)/nside_)>=fact,
167  "invalid oversampling factor");
168  fct = fact;
169  }
170  T_Healpix_Base b2;
171  double rsmall, rbig;
172  if (fct>1)
173  {
174  b2.SetNside(fct*nside_,RING);
175  rsmall = radius+b2.max_pixrad();
176  rbig = radius+max_pixrad();
177  }
178  else
179  rsmall = rbig = inclusive ? radius+max_pixrad() : radius;
180 
181  if (rsmall>=pi)
182  { pixset.append(0,npix_); return; }
183 
184  rbig = min(pi,rbig);
185 
186  double cosrsmall = cos(rsmall);
187  double cosrbig = cos(rbig);
188 
189  double z0 = cos(ptg.theta);
190  double xa = 1./sqrt((1-z0)*(1+z0));
191 
192  I cpix=zphi2pix(z0,ptg.phi);
193 
194  double rlat1 = ptg.theta - rsmall;
195  double zmax = cos(rlat1);
196  I irmin = ring_above (zmax)+1;
197 
198  if ((rlat1<=0) && (irmin>1)) // north pole in the disk
199  {
200  I sp,rp; bool dummy;
201  get_ring_info_small(irmin-1,sp,rp,dummy);
202  pixset.append(0,sp+rp);
203  }
204 
205  if ((fct>1) && (rlat1>0)) irmin=max(I(1),irmin-1);
206 
207  double rlat2 = ptg.theta + rsmall;
208  double zmin = cos(rlat2);
209  I irmax = ring_above (zmin);
210 
211  if ((fct>1) && (rlat2<pi)) irmax=min(4*nside_-1,irmax+1);
212 
213  for (I iz=irmin; iz<=irmax; ++iz)
214  {
215  double z=ring2z(iz);
216  double x = (cosrbig-z*z0)*xa;
217  double ysq = 1-z*z-x*x;
218  double dphi=-1;
219  if (ysq<=0) // no intersection, ring completely inside or outside
220  dphi = (fct==1) ? 0: pi-1e-15;
221  else
222  dphi = atan2(sqrt(ysq),x);
223  if (dphi>0)
224  {
225  I nr, ipix1;
226  bool shifted;
227  get_ring_info_small(iz,ipix1,nr,shifted);
228  double shift = shifted ? 0.5 : 0.;
229 
230  I ipix2 = ipix1 + nr - 1; // highest pixel number in the ring
231 
232  I ip_lo = ifloor<I>(nr*inv_twopi*(ptg.phi-dphi) - shift)+1;
233  I ip_hi = ifloor<I>(nr*inv_twopi*(ptg.phi+dphi) - shift);
234 
235  if (fct>1)
236  {
237  while ((ip_lo<=ip_hi) && check_pixel_ring
238  (*this,b2,ip_lo,nr,ipix1,fct,z0,ptg.phi,cosrsmall,cpix))
239  ++ip_lo;
240  while ((ip_hi>ip_lo) && check_pixel_ring
241  (*this,b2,ip_hi,nr,ipix1,fct,z0,ptg.phi,cosrsmall,cpix))
242  --ip_hi;
243  }
244 
245  if (ip_lo<=ip_hi)
246  {
247  if (ip_hi>=nr)
248  { ip_lo-=nr; ip_hi-=nr; }
249  if (ip_lo<0)
250  {
251  pixset.append(ipix1,ipix1+ip_hi+1);
252  pixset.append(ipix1+ip_lo+nr,ipix2+1);
253  }
254  else
255  pixset.append(ipix1+ip_lo,ipix1+ip_hi+1);
256  }
257  }
258  }
259  if ((rlat2>=pi) && (irmax+1<4*nside_)) // south pole in the disk
260  {
261  I sp,rp; bool dummy;
262  get_ring_info_small(irmax+1,sp,rp,dummy);
263  pixset.append(sp,npix_);
264  }
265  }
266  else // scheme_==NEST
267  {
268  if (radius>=pi) // disk covers the whole sphere
269  { pixset.append(0,npix_); return; }
270 
271  int oplus = 0;
272  if (inclusive)
273  {
274  planck_assert ((I(1)<<(order_max-order_))>=fact,
275  "invalid oversampling factor");
276  planck_assert ((fact&(fact-1))==0,
277  "oversampling factor must be a power of 2");
278  oplus=ilog2(fact);
279  }
280  int omax=order_+oplus; // the order up to which we test
281 
282  vec3 vptg(ptg);
283  arr<T_Healpix_Base<I> > base(omax+1);
284  arr<double> crpdr(omax+1), crmdr(omax+1);
285  double cosrad=cos(radius);
286  for (int o=0; o<=omax; ++o) // prepare data at the required orders
287  {
288  base[o].Set(o,NEST);
289  double dr=base[o].max_pixrad(); // safety distance
290  crpdr[o] = (radius+dr>pi) ? -1. : cos(radius+dr);
291  crmdr[o] = (radius-dr<0.) ? 1. : cos(radius-dr);
292  }
293  vector<pair<I,int> > stk; // stack for pixel numbers and their orders
294  stk.reserve(12+3*omax); // reserve maximum size to avoid reallocation
295  for (int i=0; i<12; ++i) // insert the 12 base pixels in reverse order
296  stk.push_back(make_pair(I(11-i),0));
297 
298  int stacktop=0; // a place to save a stack position
299 
300  while (!stk.empty()) // as long as there are pixels on the stack
301  {
302  // pop current pixel number and order from the stack
303  I pix=stk.back().first;
304  int o=stk.back().second;
305  stk.pop_back();
306 
307  double z,phi;
308  base[o].pix2zphi(pix,z,phi);
309  // cosine of angular distance between pixel center and disk center
310  double cangdist=cosdist_zphi(vptg.z,ptg.phi,z,phi);
311 
312  if (cangdist>crpdr[o])
313  {
314  int zone = (cangdist<cosrad) ? 1 : ((cangdist<=crmdr[o]) ? 2 : 3);
315 
316  check_pixel (o, order_, omax, zone, pixset, pix, stk, inclusive,
317  stacktop);
318  }
319  }
320  }
321  }
322 
323 template<typename I> void T_Healpix_Base<I>::query_disc
324  (pointing ptg, double radius, rangeset<I> &pixset) const
325  {
326  query_disc_internal (ptg, radius, 0, pixset);
327  }
328 
329 template<typename I> void T_Healpix_Base<I>::query_disc_inclusive
330  (pointing ptg, double radius, rangeset<I> &pixset, int fact) const
331  {
332  planck_assert(fact>0,"fact must be a positive integer");
333  if ((sizeof(I)<8) && (((I(1)<<order_max)/nside_)<fact))
334  {
335  T_Healpix_Base<int64> base2(nside_,scheme_,SET_NSIDE);
336  base2.query_disc_internal(ptg,radius,fact,pixset);
337  return;
338  }
339  query_disc_internal (ptg, radius, fact, pixset);
340  }
341 
342 template<typename I> template<typename I2>
343  void T_Healpix_Base<I>::query_multidisc (const arr<vec3> &norm,
344  const arr<double> &rad, int fact, rangeset<I2> &pixset) const
345  {
346  bool inclusive = (fact!=0);
347  tsize nv=norm.size();
348  planck_assert(nv==rad.size(),"inconsistent input arrays");
349  pixset.clear();
350 
351  if (scheme_==RING)
352  {
353  I fct=1;
354  if (inclusive)
355  {
356  planck_assert (((I(1)<<order_max)/nside_)>=fact,
357  "invalid oversampling factor");
358  fct = fact;
359  }
360  T_Healpix_Base b2;
361  double rpsmall, rpbig;
362  if (fct>1)
363  {
364  b2.SetNside(fct*nside_,RING);
365  rpsmall = b2.max_pixrad();
366  rpbig = max_pixrad();
367  }
368  else
369  rpsmall = rpbig = inclusive ? max_pixrad() : 0;
370 
371  I irmin=1, irmax=4*nside_-1;
372  vector<double> z0,xa,cosrsmall,cosrbig;
373  vector<pointing> ptg;
374  vector<I> cpix;
375  for (tsize i=0; i<nv; ++i)
376  {
377  double rsmall=rad[i]+rpsmall;
378  if (rsmall<pi)
379  {
380  double rbig=min(pi,rad[i]+rpbig);
381  pointing pnt=pointing(norm[i]);
382  cosrsmall.push_back(cos(rsmall));
383  cosrbig.push_back(cos(rbig));
384  double cth=cos(pnt.theta);
385  z0.push_back(cth);
386  if (fct>1) cpix.push_back(zphi2pix(cth,pnt.phi));
387  xa.push_back(1./sqrt((1-cth)*(1+cth)));
388  ptg.push_back(pnt);
389 
390  double rlat1 = pnt.theta - rsmall;
391  double zmax = cos(rlat1);
392  I irmin_t = (rlat1<=0) ? 1 : ring_above (zmax)+1;
393 
394  if ((fct>1) && (rlat1>0)) irmin_t=max(I(1),irmin_t-1);
395 
396  double rlat2 = pnt.theta + rsmall;
397  double zmin = cos(rlat2);
398  I irmax_t = (rlat2>=pi) ? 4*nside_-1 : ring_above (zmin);
399 
400  if ((fct>1) && (rlat2<pi)) irmax_t=min(4*nside_-1,irmax_t+1);
401 
402  if (irmax_t < irmax) irmax=irmax_t;
403  if (irmin_t > irmin) irmin=irmin_t;
404  }
405  }
406 
407  for (I iz=irmin; iz<=irmax; ++iz)
408  {
409  double z=ring2z(iz);
410  I ipix1,nr;
411  bool shifted;
412  get_ring_info_small(iz,ipix1,nr,shifted);
413  double shift = shifted ? 0.5 : 0.;
414  rangeset<I2> tr;
415  tr.append(ipix1,ipix1+nr);
416  for (tsize j=0; j<z0.size(); ++j)
417  {
418  double x = (cosrbig[j]-z*z0[j])*xa[j];
419  double ysq = 1.-z*z-x*x;
420  double dphi = (ysq<=0) ? pi-1e-15 : atan2(sqrt(ysq),x);
421  I ip_lo = ifloor<I>(nr*inv_twopi*(ptg[j].phi-dphi) - shift)+1;
422  I ip_hi = ifloor<I>(nr*inv_twopi*(ptg[j].phi+dphi) - shift);
423  if (fct>1)
424  {
425  while ((ip_lo<=ip_hi) && check_pixel_ring
426  (*this,b2,ip_lo,nr,ipix1,fct,z0[j],ptg[j].phi,cosrsmall[j],cpix[j]))
427  ++ip_lo;
428  while ((ip_hi>ip_lo) && check_pixel_ring
429  (*this,b2,ip_hi,nr,ipix1,fct,z0[j],ptg[j].phi,cosrsmall[j],cpix[j]))
430  --ip_hi;
431  }
432  if (ip_hi>=nr)
433  { ip_lo-=nr; ip_hi-=nr;}
434  if (ip_lo<0)
435  tr.remove(ipix1+ip_hi+1,ipix1+ip_lo+nr);
436  else
437  tr.intersect(ipix1+ip_lo,ipix1+ip_hi+1);
438  }
439  pixset.append(tr);
440  }
441  }
442  else // scheme_ == NEST
443  {
444  int oplus = 0;
445  if (inclusive)
446  {
447  planck_assert ((I(1)<<(order_max-order_))>=fact,
448  "invalid oversampling factor");
449  planck_assert ((fact&(fact-1))==0,
450  "oversampling factor must be a power of 2");
451  oplus=ilog2(fact);
452  }
453  int omax=order_+oplus; // the order up to which we test
454 
455  // TODO: ignore all disks with radius>=pi
456 
457  arr<T_Healpix_Base<I> > base(omax+1);
458  arr3<double> crlimit(omax+1,nv,3);
459  for (int o=0; o<=omax; ++o) // prepare data at the required orders
460  {
461  base[o].Set(o,NEST);
462  double dr=base[o].max_pixrad(); // safety distance
463  for (tsize i=0; i<nv; ++i)
464  {
465  crlimit(o,i,0) = (rad[i]+dr>pi) ? -1. : cos(rad[i]+dr);
466  crlimit(o,i,1) = (o==0) ? cos(rad[i]) : crlimit(0,i,1);
467  crlimit(o,i,2) = (rad[i]-dr<0.) ? 1. : cos(rad[i]-dr);
468  }
469  }
470 
471  vector<pair<I,int> > stk; // stack for pixel numbers and their orders
472  stk.reserve(12+3*omax); // reserve maximum size to avoid reallocation
473  for (int i=0; i<12; ++i) // insert the 12 base pixels in reverse order
474  stk.push_back(make_pair(I(11-i),0));
475 
476  int stacktop=0; // a place to save a stack position
477 
478  while (!stk.empty()) // as long as there are pixels on the stack
479  {
480  // pop current pixel number and order from the stack
481  I pix=stk.back().first;
482  int o=stk.back().second;
483  stk.pop_back();
484 
485  vec3 pv(base[o].pix2vec(pix));
486 
487  tsize zone=3;
488  for (tsize i=0; i<nv; ++i)
489  {
490  double crad=dotprod(pv,norm[i]);
491  for (tsize iz=0; iz<zone; ++iz)
492  if (crad<crlimit(o,i,iz))
493  if ((zone=iz)==0) goto bailout;
494  }
495 
496  check_pixel (o, order_, omax, zone, pixset, pix, stk, inclusive,
497  stacktop);
498  bailout:;
499  }
500  }
501  }
502 
503 template<typename I> void T_Healpix_Base<I>::query_multidisc_general
504  (const arr<vec3> &norm, const arr<double> &rad, bool inclusive,
505  const vector<int> &cmds, rangeset<I> &pixset) const
506  {
507  tsize nv=norm.size();
508  planck_assert(nv==rad.size(),"inconsistent input arrays");
509  pixset.clear();
510 
511  if (scheme_==RING)
512  {
513  planck_fail ("not yet implemented");
514  }
515  else // scheme_ == NEST
516  {
517  int oplus=inclusive ? 2 : 0;
518  int omax=min<int>(order_max,order_+oplus); // the order up to which we test
519 
520  // TODO: ignore all disks with radius>=pi
521 
522  arr<T_Healpix_Base<I> > base(omax+1);
523  arr3<double> crlimit(omax+1,nv,3);
524  for (int o=0; o<=omax; ++o) // prepare data at the required orders
525  {
526  base[o].Set(o,NEST);
527  double dr=base[o].max_pixrad(); // safety distance
528  for (tsize i=0; i<nv; ++i)
529  {
530  crlimit(o,i,0) = (rad[i]+dr>pi) ? -1. : cos(rad[i]+dr);
531  crlimit(o,i,1) = (o==0) ? cos(rad[i]) : crlimit(0,i,1);
532  crlimit(o,i,2) = (rad[i]-dr<0.) ? 1. : cos(rad[i]-dr);
533  }
534  }
535 
536  vector<pair<I,int> > stk; // stack for pixel numbers and their orders
537  stk.reserve(12+3*omax); // reserve maximum size to avoid reallocation
538  for (int i=0; i<12; ++i) // insert the 12 base pixels in reverse order
539  stk.push_back(make_pair(I(11-i),0));
540 
541  int stacktop=0; // a place to save a stack position
542  arr<tsize> zone(nv);
543 
544  vector<tsize> zstk; zstk.reserve(cmds.size());
545 
546  while (!stk.empty()) // as long as there are pixels on the stack
547  {
548  // pop current pixel number and order from the stack
549  I pix=stk.back().first;
550  int o=stk.back().second;
551  stk.pop_back();
552 
553  vec3 pv(base[o].pix2vec(pix));
554 
555  for (tsize i=0; i<nv; ++i)
556  {
557  zone[i]=3;
558  double crad=dotprod(pv,norm[i]);
559  for (tsize iz=0; iz<zone[i]; ++iz)
560  if (crad<crlimit(o,i,iz))
561  zone[i]=iz;
562  }
563 
564  for (tsize i=0; i<cmds.size(); ++i)
565  {
566  tsize tmp;
567  switch (cmds[i])
568  {
569  case -1: // union
570  tmp=zstk.back(); zstk.pop_back();
571  zstk.back() = max(zstk.back(),tmp);
572  break;
573  case -2: // intersection
574  tmp=zstk.back(); zstk.pop_back();
575  zstk.back() = min(zstk.back(),tmp);
576  break;
577  default: // add value
578  zstk.push_back(zone[cmds[i]]);
579  }
580  }
581  planck_assert(zstk.size()==1,"inconsistent commands");
582  tsize zn=zstk[0]; zstk.pop_back();
583 
584  check_pixel (o, order_, omax, zn, pixset, pix, stk, inclusive,
585  stacktop);
586  }
587  }
588  }
589 
590 #ifndef __BMI2__
591 
592 template<> inline int T_Healpix_Base<int>::spread_bits (int v) const
593  { return utab[v&0xff] | (utab[(v>>8)&0xff]<<16); }
594 template<> inline int64 T_Healpix_Base<int64>::spread_bits (int v) const
595  {
596  return int64(utab[ v &0xff]) | (int64(utab[(v>> 8)&0xff])<<16)
597  | (int64(utab[(v>>16)&0xff])<<32) | (int64(utab[(v>>24)&0xff])<<48);
598  }
599 
600 template<> inline int T_Healpix_Base<int>::compress_bits (int v) const
601  {
602  int raw = (v&0x5555) | ((v&0x55550000)>>15);
603  return ctab[raw&0xff] | (ctab[raw>>8]<<4);
604  }
605 template<> inline int T_Healpix_Base<int64>::compress_bits (int64 v) const
606  {
607  int64 raw = v&0x5555555555555555ull;
608  raw|=raw>>15;
609  return ctab[ raw &0xff] | (ctab[(raw>> 8)&0xff]<< 4)
610  | (ctab[(raw>>32)&0xff]<<16) | (ctab[(raw>>40)&0xff]<<20);
611  }
612 
613 #else
614 
615 #include <x86intrin.h>
616 
617 template<> inline int T_Healpix_Base<int>::spread_bits (int v) const
618  { return _pdep_u32(v,0x55555555u); }
619 template<> inline int64 T_Healpix_Base<int64>::spread_bits (int v) const
620  { return _pdep_u64(v,0x5555555555555555ull); }
621 template<> inline int T_Healpix_Base<int>::compress_bits (int v) const
622  { return _pext_u32(v,0x55555555u); }
623 template<> inline int T_Healpix_Base<int64>::compress_bits (int64 v) const
624  { return _pext_u64(v,0x5555555555555555ull); }
625 
626 #endif
627 
628 template<typename I> inline void T_Healpix_Base<I>::nest2xyf (I pix, int &ix,
629  int &iy, int &face_num) const
630  {
631  face_num = pix>>(2*order_);
632  pix &= (npface_-1);
633  ix = compress_bits(pix);
634  iy = compress_bits(pix>>1);
635  }
636 
637 template<typename I> inline I T_Healpix_Base<I>::xyf2nest (int ix, int iy,
638  int face_num) const
639  { return (I(face_num)<<(2*order_)) + spread_bits(ix) + (spread_bits(iy)<<1); }
640 
641 namespace {
642 
643 // low-level hack to accelerate divisions with a result of [0;3]
644 template<typename I> inline I special_div(I a, I b)
645  {
646  I t=(a>=(b<<1));
647  a-=t*(b<<1);
648  return (t<<1)+(a>=b);
649  }
650 
651 } // unnamed namespace
652 
653 template<typename I> void T_Healpix_Base<I>::ring2xyf (I pix, int &ix, int &iy,
654  int &face_num) const
655  {
656  I iring, iphi, kshift, nr;
657  I nl2 = 2*nside_;
658 
659  if (pix<ncap_) // North Polar cap
660  {
661  iring = (1+isqrt(1+2*pix))>>1; //counted from North pole
662  iphi = (pix+1) - 2*iring*(iring-1);
663  kshift = 0;
664  nr = iring;
665  face_num=special_div(iphi-1,nr);
666  }
667  else if (pix<(npix_-ncap_)) // Equatorial region
668  {
669  I ip = pix - ncap_;
670  I tmp = (order_>=0) ? ip>>(order_+2) : ip/(4*nside_);
671  iring = tmp+nside_;
672  iphi = ip-tmp*4*nside_ + 1;
673  kshift = (iring+nside_)&1;
674  nr = nside_;
675  I ire = tmp+1,
676  irm = nl2+1-tmp;
677  I ifm = iphi - (ire>>1) + nside_ -1,
678  ifp = iphi - (irm>>1) + nside_ -1;
679  if (order_>=0)
680  { ifm >>= order_; ifp >>= order_; }
681  else
682  { ifm /= nside_; ifp /= nside_; }
683  face_num = (ifp==ifm) ? (ifp|4) : ((ifp<ifm) ? ifp : (ifm+8));
684  }
685  else // South Polar cap
686  {
687  I ip = npix_ - pix;
688  iring = (1+isqrt(2*ip-1))>>1; //counted from South pole
689  iphi = 4*iring + 1 - (ip - 2*iring*(iring-1));
690  kshift = 0;
691  nr = iring;
692  iring = 2*nl2-iring;
693  face_num=special_div(iphi-1,nr)+8;
694  }
695 
696  I irt = iring - ((2+(face_num>>2))*nside_) + 1;
697  I ipt = 2*iphi- jpll[face_num]*nr - kshift -1;
698 // I ipt = 2*iphi- (((face_num&3)<<1)+1-((face_num>>2)&1))*nr - kshift -1;
699  if (ipt>=nl2) ipt-=8*nside_;
700 
701  ix = (ipt-irt) >>1;
702  iy = (-ipt-irt) >>1;
703  }
704 
705 template<typename I> I T_Healpix_Base<I>::xyf2ring (int ix, int iy,
706  int face_num) const
707  {
708  I nl4 = 4*nside_;
709  I jr = (jrll[face_num]*nside_) - ix - iy - 1;
710 
711  I nr, kshift, n_before;
712 
713  bool shifted;
714  get_ring_info_small(jr,n_before,nr,shifted);
715  nr>>=2;
716  kshift=1-shifted;
717  I jp = (jpll[face_num]*nr + ix - iy + 1 + kshift) / 2;
718  planck_assert(jp<=4*nr,"must not happen");
719  if (jp<1) jp+=nl4; // assumption: if this triggers, then nl4==4*nr
720 
721  return n_before + jp - 1;
722  }
723 
724 template<typename I> T_Healpix_Base<I>::T_Healpix_Base ()
725  : order_(-1), nside_(0), npface_(0), ncap_(0), npix_(0),
726  fact1_(0), fact2_(0), scheme_(RING) {}
727 
728 template<typename I> void T_Healpix_Base<I>::Set (int order,
730  {
731  planck_assert ((order>=0)&&(order<=order_max), "bad order");
732  order_ = order;
733  nside_ = I(1)<<order;
734  npface_ = nside_<<order_;
735  ncap_ = (npface_-nside_)<<1;
736  npix_ = 12*npface_;
737  fact2_ = 4./npix_;
738  fact1_ = (nside_<<1)*fact2_;
739  scheme_ = scheme;
740  }
741 template<typename I> void T_Healpix_Base<I>::SetNside (I nside,
743  {
744  order_ = nside2order(nside);
745  planck_assert ((scheme!=NEST) || (order_>=0),
746  "SetNside: nside must be power of 2 for nested maps");
747  nside_ = nside;
748  npface_ = nside_*nside_;
749  ncap_ = (npface_-nside_)<<1;
750  npix_ = 12*npface_;
751  fact2_ = 4./npix_;
752  fact1_ = (nside_<<1)*fact2_;
753  scheme_ = scheme;
754  }
755 
756 template<typename I> double T_Healpix_Base<I>::ring2z (I ring) const
757  {
758  if (ring<nside_)
759  return 1 - ring*ring*fact2_;
760  if (ring <=3*nside_)
761  return (2*nside_-ring)*fact1_;
762  ring=4*nside_ - ring;
763  return ring*ring*fact2_ - 1;
764  }
765 
766 template<typename I> I T_Healpix_Base<I>::pix2ring (I pix) const
767  {
768  if (scheme_==RING)
769  {
770  if (pix<ncap_) // North Polar cap
771  return (1+I(isqrt(1+2*pix)))>>1; // counted from North pole
772  else if (pix<(npix_-ncap_)) // Equatorial region
773  return (pix-ncap_)/(4*nside_) + nside_; // counted from North pole
774  else // South Polar cap
775  return 4*nside_-((1+I(isqrt(2*(npix_-pix)-1)))>>1);
776  }
777  else
778  {
779  int face_num, ix, iy;
780  nest2xyf(pix,ix,iy,face_num);
781  return (I(jrll[face_num])<<order_) - ix - iy - 1;
782  }
783  }
784 
785 template<typename I> I T_Healpix_Base<I>::nest2ring (I pix) const
786  {
787  planck_assert(order_>=0, "hierarchical map required");
788  int ix, iy, face_num;
789  nest2xyf (pix, ix, iy, face_num);
790  return xyf2ring (ix, iy, face_num);
791  }
792 
793 template<typename I> I T_Healpix_Base<I>::ring2nest (I pix) const
794  {
795  planck_assert(order_>=0, "hierarchical map required");
796  int ix, iy, face_num;
797  ring2xyf (pix, ix, iy, face_num);
798  return xyf2nest (ix, iy, face_num);
799  }
800 
801 template<typename I> inline I T_Healpix_Base<I>::nest_peano_helper
802  (I pix, int dir) const
803  {
804  int face = (pix>>(2*order_));
805  I result = 0;
806  int state = ((peano_face2path[dir][face]<<4))|(dir<<7);
807  int shift=2*order_-4;
808  for (; shift>=0; shift-=4)
809  {
810  state=peano_arr2[(state&0xF0) | ((pix>>shift)&0xF)];
811  result = (result<<4) | (state&0xF);
812  }
813  if (shift==-2)
814  {
815  state=peano_arr[((state>>2)&0xFC) | (pix&0x3)];
816  result = (result<<2) | (state&0x3);
817  }
818 
819  return result + (I(peano_face2face[dir][face])<<(2*order_));
820  }
821 
822 template<typename I> I T_Healpix_Base<I>::nest2peano (I pix) const
823  { return nest_peano_helper(pix,0); }
824 
825 template<typename I> I T_Healpix_Base<I>::peano2nest (I pix) const
826  { return nest_peano_helper(pix,1); }
827 
828 template<typename I> I T_Healpix_Base<I>::loc2pix (double z, double phi,
829  double sth, bool have_sth) const
830  {
831  double za = abs(z);
832  double tt = fmodulo(phi*inv_halfpi,4.0); // in [0,4)
833 
834  if (scheme_==RING)
835  {
836  if (za<=twothird) // Equatorial region
837  {
838  I nl4 = 4*nside_;
839  double temp1 = nside_*(0.5+tt);
840  double temp2 = nside_*z*0.75;
841  I jp = I(temp1-temp2); // index of ascending edge line
842  I jm = I(temp1+temp2); // index of descending edge line
843 
844  // ring number counted from z=2/3
845  I ir = nside_ + 1 + jp - jm; // in {1,2n+1}
846  I kshift = 1-(ir&1); // kshift=1 if ir even, 0 otherwise
847 
848  I t1 = jp+jm-nside_+kshift+1+nl4+nl4;
849  I ip = (order_>0) ?
850  (t1>>1)&(nl4-1) : ((t1>>1)%nl4); // in {0,4n-1}
851 
852  return ncap_ + (ir-1)*nl4 + ip;
853  }
854  else // North & South polar caps
855  {
856  double tp = tt-I(tt);
857  double tmp = ((za<0.99)||(!have_sth)) ?
858  nside_*sqrt(3*(1-za)) :
859  nside_*sth/sqrt((1.+za)/3.);
860 
861  I jp = I(tp*tmp); // increasing edge line index
862  I jm = I((1.0-tp)*tmp); // decreasing edge line index
863 
864  I ir = jp+jm+1; // ring number counted from the closest pole
865  I ip = I(tt*ir); // in {0,4*ir-1}
866  planck_assert((ip>=0)&&(ip<4*ir),"must not happen");
867  //ip %= 4*ir;
868 
869  return (z>0) ? 2*ir*(ir-1) + ip : npix_ - 2*ir*(ir+1) + ip;
870  }
871  }
872  else // scheme_ == NEST
873  {
874  if (za<=twothird) // Equatorial region
875  {
876  double temp1 = nside_*(0.5+tt);
877  double temp2 = nside_*(z*0.75);
878  I jp = I(temp1-temp2); // index of ascending edge line
879  I jm = I(temp1+temp2); // index of descending edge line
880  I ifp = jp >> order_; // in {0,4}
881  I ifm = jm >> order_;
882  int face_num = (ifp==ifm) ? (ifp|4) : ((ifp<ifm) ? ifp : (ifm+8));
883 
884  int ix = jm & (nside_-1),
885  iy = nside_ - (jp & (nside_-1)) - 1;
886  return xyf2nest(ix,iy,face_num);
887  }
888  else // polar region, za > 2/3
889  {
890  int ntt = min(3,int(tt));
891  double tp = tt-ntt;
892  double tmp = ((za<0.99)||(!have_sth)) ?
893  nside_*sqrt(3*(1-za)) :
894  nside_*sth/sqrt((1.+za)/3.);
895 
896  I jp = I(tp*tmp); // increasing edge line index
897  I jm = I((1.0-tp)*tmp); // decreasing edge line index
898  jp=min(jp,nside_-1); // for points too close to the boundary
899  jm=min(jm,nside_-1);
900  return (z>=0) ?
901  xyf2nest(nside_-jm -1,nside_-jp-1,ntt) : xyf2nest(jp,jm,ntt+8);
902  }
903  }
904  }
905 
906 template<typename I> void T_Healpix_Base<I>::pix2loc (I pix, double &z,
907  double &phi, double &sth, bool &have_sth) const
908  {
909  have_sth=false;
910  if (scheme_==RING)
911  {
912  if (pix<ncap_) // North Polar cap
913  {
914  I iring = (1+I(isqrt(1+2*pix)))>>1; // counted from North pole
915  I iphi = (pix+1) - 2*iring*(iring-1);
916 
917  double tmp=(iring*iring)*fact2_;
918  z = 1.0 - tmp;
919  if (z>0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
920  phi = (iphi-0.5) * halfpi/iring;
921  }
922  else if (pix<(npix_-ncap_)) // Equatorial region
923  {
924  I nl4 = 4*nside_;
925  I ip = pix - ncap_;
926  I tmp = (order_>=0) ? ip>>(order_+2) : ip/nl4;
927  I iring = tmp + nside_,
928  iphi = ip-nl4*tmp+1;
929  // 1 if iring+nside is odd, 1/2 otherwise
930  double fodd = ((iring+nside_)&1) ? 1 : 0.5;
931 
932  z = (2*nside_-iring)*fact1_;
933  phi = (iphi-fodd) * pi*0.75*fact1_;
934  }
935  else // South Polar cap
936  {
937  I ip = npix_ - pix;
938  I iring = (1+I(isqrt(2*ip-1)))>>1; // counted from South pole
939  I iphi = 4*iring + 1 - (ip - 2*iring*(iring-1));
940 
941  double tmp=(iring*iring)*fact2_;
942  z = tmp - 1.0;
943  if (z<-0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
944  phi = (iphi-0.5) * halfpi/iring;
945  }
946  }
947  else
948  {
949  int face_num, ix, iy;
950  nest2xyf(pix,ix,iy,face_num);
951 
952  I jr = (I(jrll[face_num])<<order_) - ix - iy - 1;
953 
954  I nr;
955  if (jr<nside_)
956  {
957  nr = jr;
958  double tmp=(nr*nr)*fact2_;
959  z = 1 - tmp;
960  if (z>0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
961  }
962  else if (jr > 3*nside_)
963  {
964  nr = nside_*4-jr;
965  double tmp=(nr*nr)*fact2_;
966  z = tmp - 1;
967  if (z<-0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
968  }
969  else
970  {
971  nr = nside_;
972  z = (2*nside_-jr)*fact1_;
973  }
974 
975  I tmp=I(jpll[face_num])*nr+ix-iy;
976  planck_assert(tmp<8*nr,"must not happen");
977  if (tmp<0) tmp+=8*nr;
978  phi = (nr==nside_) ? 0.75*halfpi*tmp*fact1_ :
979  (0.5*halfpi*tmp)/nr;
980  }
981  }
982 
983 template<typename I> template<typename I2>
985  (const vector<pointing> &vertex, int fact, rangeset<I2> &pixset) const
986  {
987  bool inclusive = (fact!=0);
988  tsize nv=vertex.size();
989  tsize ncirc = inclusive ? nv+1 : nv;
990  planck_assert(nv>=3,"not enough vertices in polygon");
991  vector<vec3> vv(nv);
992  for (tsize i=0; i<nv; ++i)
993  vv[i]=vertex[i].to_vec3();
994  arr<vec3> normal(ncirc);
995  int flip=0;
996  for (tsize i=0; i<nv; ++i)
997  {
998  normal[i]=crossprod(vv[i],vv[(i+1)%nv]).Norm();
999  double hnd=dotprod(normal[i],vv[(i+2)%nv]);
1000  planck_assert(abs(hnd)>1e-10,"degenerate corner");
1001  if (i==0)
1002  flip = (hnd<0.) ? -1 : 1;
1003  else
1004  planck_assert(flip*hnd>0,"polygon is not convex");
1005  normal[i]*=flip;
1006  }
1007  arr<double> rad(ncirc,halfpi);
1008  if (inclusive)
1009  {
1010  double cosrad;
1011  find_enclosing_circle (vv, normal[nv], cosrad);
1012  rad[nv]=acos(cosrad);
1013  }
1014  query_multidisc(normal,rad,fact,pixset);
1015  }
1016 
1017 template<typename I> void T_Healpix_Base<I>::query_polygon
1018  (const vector<pointing> &vertex, rangeset<I> &pixset) const
1019  {
1020  query_polygon_internal(vertex, 0, pixset);
1021  }
1022 
1023 template<typename I> void T_Healpix_Base<I>::query_polygon_inclusive
1024  (const vector<pointing> &vertex, rangeset<I> &pixset, int fact) const
1025  {
1026  planck_assert(fact>0,"fact must be a positive integer");
1027  if ((sizeof(I)<8) && (((I(1)<<order_max)/nside_)<fact))
1028  {
1029  T_Healpix_Base<int64> base2(nside_,scheme_,SET_NSIDE);
1030  base2.query_polygon_internal(vertex,fact,pixset);
1031  return;
1032  }
1033  query_polygon_internal(vertex, fact, pixset);
1034  }
1035 
1036 template<typename I> void T_Healpix_Base<I>::query_strip_internal
1037  (double theta1, double theta2, bool inclusive, rangeset<I> &pixset) const
1038  {
1039  if (scheme_==RING)
1040  {
1041  I ring1 = max(I(1),1+ring_above(cos(theta1))),
1042  ring2 = min(4*nside_-1,ring_above(cos(theta2)));
1043  if (inclusive)
1044  {
1045  ring1 = max(I(1),ring1-1);
1046  ring2 = min(4*nside_-1,ring2+1);
1047  }
1048 
1049  I sp1,rp1,sp2,rp2;
1050  bool dummy;
1051  get_ring_info_small(ring1,sp1,rp1,dummy);
1052  get_ring_info_small(ring2,sp2,rp2,dummy);
1053  I pix1 = sp1,
1054  pix2 = sp2+rp2;
1055  if (pix1<=pix2) pixset.append(pix1,pix2);
1056  }
1057  else
1058  planck_fail("query_strip not yet implemented for NESTED");
1059  }
1060 
1061 template<typename I> void T_Healpix_Base<I>::query_strip (double theta1,
1062  double theta2, bool inclusive, rangeset<I> &pixset) const
1063  {
1064  pixset.clear();
1065 
1066  if (theta1<theta2)
1067  query_strip_internal(theta1,theta2,inclusive,pixset);
1068  else
1069  {
1070  query_strip_internal(0.,theta2,inclusive,pixset);
1071  rangeset<I> ps2;
1072  query_strip_internal(theta1,pi,inclusive,ps2);
1073  pixset.append(ps2);
1074  }
1075  }
1076 
1077 template<typename I> inline void T_Healpix_Base<I>::get_ring_info_small
1078  (I ring, I &startpix, I &ringpix, bool &shifted) const
1079  {
1080  if (ring < nside_)
1081  {
1082  shifted = true;
1083  ringpix = 4*ring;
1084  startpix = 2*ring*(ring-1);
1085  }
1086  else if (ring < 3*nside_)
1087  {
1088  shifted = ((ring-nside_) & 1) == 0;
1089  ringpix = 4*nside_;
1090  startpix = ncap_ + (ring-nside_)*ringpix;
1091  }
1092  else
1093  {
1094  shifted = true;
1095  I nr= 4*nside_-ring;
1096  ringpix = 4*nr;
1097  startpix = npix_-2*nr*(nr+1);
1098  }
1099  }
1100 
1101 template<typename I> void T_Healpix_Base<I>::get_ring_info (I ring, I &startpix,
1102  I &ringpix, double &costheta, double &sintheta, bool &shifted) const
1103  {
1104  I northring = (ring>2*nside_) ? 4*nside_-ring : ring;
1105  if (northring < nside_)
1106  {
1107  double tmp = northring*northring*fact2_;
1108  costheta = 1 - tmp;
1109  sintheta = sqrt(tmp*(2-tmp));
1110  ringpix = 4*northring;
1111  shifted = true;
1112  startpix = 2*northring*(northring-1);
1113  }
1114  else
1115  {
1116  costheta = (2*nside_-northring)*fact1_;
1117  sintheta = sqrt((1+costheta)*(1-costheta));
1118  ringpix = 4*nside_;
1119  shifted = ((northring-nside_) & 1) == 0;
1120  startpix = ncap_ + (northring-nside_)*ringpix;
1121  }
1122  if (northring != ring) // southern hemisphere
1123  {
1124  costheta = -costheta;
1125  startpix = npix_ - startpix - ringpix;
1126  }
1127  }
1128 template<typename I> void T_Healpix_Base<I>::get_ring_info2 (I ring,
1129  I &startpix, I &ringpix, double &theta, bool &shifted) const
1130  {
1131  I northring = (ring>2*nside_) ? 4*nside_-ring : ring;
1132  if (northring < nside_)
1133  {
1134  double tmp = northring*northring*fact2_;
1135  double costheta = 1 - tmp;
1136  double sintheta = sqrt(tmp*(2-tmp));
1137  theta = atan2(sintheta,costheta);
1138  ringpix = 4*northring;
1139  shifted = true;
1140  startpix = 2*northring*(northring-1);
1141  }
1142  else
1143  {
1144  theta = acos((2*nside_-northring)*fact1_);
1145  ringpix = 4*nside_;
1146  shifted = ((northring-nside_) & 1) == 0;
1147  startpix = ncap_ + (northring-nside_)*ringpix;
1148  }
1149  if (northring != ring) // southern hemisphere
1150  {
1151  theta = pi-theta;
1152  startpix = npix_ - startpix - ringpix;
1153  }
1154  }
1155 
1156 template<typename I> void T_Healpix_Base<I>::neighbors (I pix,
1157  fix_arr<I,8> &result) const
1158  {
1159  int ix, iy, face_num;
1160  (scheme_==RING) ?
1161  ring2xyf(pix,ix,iy,face_num) : nest2xyf(pix,ix,iy,face_num);
1162 
1163  const I nsm1 = nside_-1;
1164  if ((ix>0)&&(ix<nsm1)&&(iy>0)&&(iy<nsm1))
1165  {
1166  if (scheme_==RING)
1167  for (int m=0; m<8; ++m)
1168  result[m] = xyf2ring(ix+nb_xoffset[m],iy+nb_yoffset[m],face_num);
1169  else
1170  {
1171  I fpix = I(face_num)<<(2*order_),
1172  px0=spread_bits(ix ), py0=spread_bits(iy )<<1,
1173  pxp=spread_bits(ix+1), pyp=spread_bits(iy+1)<<1,
1174  pxm=spread_bits(ix-1), pym=spread_bits(iy-1)<<1;
1175 
1176  result[0] = fpix+pxm+py0; result[1] = fpix+pxm+pyp;
1177  result[2] = fpix+px0+pyp; result[3] = fpix+pxp+pyp;
1178  result[4] = fpix+pxp+py0; result[5] = fpix+pxp+pym;
1179  result[6] = fpix+px0+pym; result[7] = fpix+pxm+pym;
1180  }
1181  }
1182  else
1183  {
1184  for (int i=0; i<8; ++i)
1185  {
1186  int x=ix+nb_xoffset[i], y=iy+nb_yoffset[i];
1187  int nbnum=4;
1188  if (x<0)
1189  { x+=nside_; nbnum-=1; }
1190  else if (x>=nside_)
1191  { x-=nside_; nbnum+=1; }
1192  if (y<0)
1193  { y+=nside_; nbnum-=3; }
1194  else if (y>=nside_)
1195  { y-=nside_; nbnum+=3; }
1196 
1197  int f = nb_facearray[nbnum][face_num];
1198  if (f>=0)
1199  {
1200  int bits = nb_swaparray[nbnum][face_num>>2];
1201  if (bits&1) x=nside_-x-1;
1202  if (bits&2) y=nside_-y-1;
1203  if (bits&4) std::swap(x,y);
1204  result[i] = (scheme_==RING) ? xyf2ring(x,y,f) : xyf2nest(x,y,f);
1205  }
1206  else
1207  result[i] = -1;
1208  }
1209  }
1210  }
1211 
1212 template<typename I> void T_Healpix_Base<I>::get_interpol (const pointing &ptg,
1213  fix_arr<I,4> &pix, fix_arr<double,4> &wgt) const
1214  {
1215  planck_assert((ptg.theta>=0)&&(ptg.theta<=pi),"invalid theta value");
1216  double z = cos (ptg.theta);
1217  I ir1 = ring_above(z);
1218  I ir2 = ir1+1;
1219  double theta1, theta2, w1, tmp, dphi;
1220  I sp,nr;
1221  bool shift;
1222  I i1,i2;
1223  if (ir1>0)
1224  {
1225  get_ring_info2 (ir1, sp, nr, theta1, shift);
1226  dphi = twopi/nr;
1227  tmp = (ptg.phi/dphi - .5*shift);
1228  i1 = (tmp<0) ? I(tmp)-1 : I(tmp);
1229  w1 = (ptg.phi-(i1+.5*shift)*dphi)/dphi;
1230  i2 = i1+1;
1231  if (i1<0) i1 +=nr;
1232  if (i2>=nr) i2 -=nr;
1233  pix[0] = sp+i1; pix[1] = sp+i2;
1234  wgt[0] = 1-w1; wgt[1] = w1;
1235  }
1236  if (ir2<(4*nside_))
1237  {
1238  get_ring_info2 (ir2, sp, nr, theta2, shift);
1239  dphi = twopi/nr;
1240  tmp = (ptg.phi/dphi - .5*shift);
1241  i1 = (tmp<0) ? I(tmp)-1 : I(tmp);
1242  w1 = (ptg.phi-(i1+.5*shift)*dphi)/dphi;
1243  i2 = i1+1;
1244  if (i1<0) i1 +=nr;
1245  if (i2>=nr) i2 -=nr;
1246  pix[2] = sp+i1; pix[3] = sp+i2;
1247  wgt[2] = 1-w1; wgt[3] = w1;
1248  }
1249 
1250  if (ir1==0)
1251  {
1252  double wtheta = ptg.theta/theta2;
1253  wgt[2] *= wtheta; wgt[3] *= wtheta;
1254  double fac = (1-wtheta)*0.25;
1255  wgt[0] = fac; wgt[1] = fac; wgt[2] += fac; wgt[3] +=fac;
1256  pix[0] = (pix[2]+2)&3;
1257  pix[1] = (pix[3]+2)&3;
1258  }
1259  else if (ir2==4*nside_)
1260  {
1261  double wtheta = (ptg.theta-theta1)/(pi-theta1);
1262  wgt[0] *= (1-wtheta); wgt[1] *= (1-wtheta);
1263  double fac = wtheta*0.25;
1264  wgt[0] += fac; wgt[1] += fac; wgt[2] = fac; wgt[3] =fac;
1265  pix[2] = ((pix[0]+2)&3)+npix_-4;
1266  pix[3] = ((pix[1]+2)&3)+npix_-4;
1267  }
1268  else
1269  {
1270  double wtheta = (ptg.theta-theta1)/(theta2-theta1);
1271  wgt[0] *= (1-wtheta); wgt[1] *= (1-wtheta);
1272  wgt[2] *= wtheta; wgt[3] *= wtheta;
1273  }
1274 
1275  if (scheme_==NEST)
1276  for (tsize m=0; m<pix.size(); ++m)
1277  pix[m] = ring2nest(pix[m]);
1278  }
1279 
1280 template<typename I> void T_Healpix_Base<I>::swap (T_Healpix_Base &other)
1281  {
1282  std::swap(order_,other.order_);
1283  std::swap(nside_,other.nside_);
1284  std::swap(npface_,other.npface_);
1285  std::swap(ncap_,other.ncap_);
1286  std::swap(npix_,other.npix_);
1287  std::swap(fact1_,other.fact1_);
1288  std::swap(fact2_,other.fact2_);
1289  std::swap(scheme_,other.scheme_);
1290  }
1291 
1292 template<typename I> double T_Healpix_Base<I>::max_pixrad() const
1293  {
1294  vec3 va,vb;
1295  va.set_z_phi (2./3., pi/(4*nside_));
1296  double t1 = 1.-1./nside_;
1297  t1*=t1;
1298  vb.set_z_phi (1-t1/3, 0);
1299  return v_angle(va,vb);
1300  }
1301 
1302 template<typename I> double T_Healpix_Base<I>::max_pixrad(I ring) const
1303  {
1304  if (ring>=2*nside_) ring=4*nside_-ring;
1305  double z=ring2z(ring), z_up=ring2z(ring-1);
1306  vec3 mypos, uppos;
1307  uppos.set_z_phi(z_up,0);
1308  if (ring<=nside_)
1309  {
1310  mypos.set_z_phi(z,pi/(4*ring));
1311  double v1=v_angle(mypos,uppos);
1312  if (ring!=1) return v1;
1313  uppos.set_z_phi(ring2z(ring+1),pi/(4*(min(nside_,ring+1))));
1314  return max(v1,v_angle(mypos,uppos));
1315  }
1316  mypos.set_z_phi(z,0);
1317  double vdist=v_angle(mypos,uppos);
1318  double hdist=sqrt(1.-z*z)*pi/(4*nside_);
1319  return max(hdist,vdist);
1320  }
1321 
1322 template<typename I> void T_Healpix_Base<I>::xyf2loc (double x, double y,
1323  int face, double &z, double &phi, double &sth, bool &have_sth) const
1324  {
1325  have_sth = false;
1326  double jr = jrll[face] - x - y;
1327  double nr;
1328  if (jr<1)
1329  {
1330  nr = jr;
1331  double tmp = nr*nr/3.;
1332  z = 1 - tmp;
1333  if (z > 0.99)
1334  {
1335  sth = std::sqrt(tmp*(2.0-tmp));
1336  have_sth = true;
1337  }
1338  }
1339  else if (jr>3)
1340  {
1341  nr = 4-jr;
1342  double tmp = nr*nr/3.;
1343  z = tmp - 1;
1344  if (z<-0.99)
1345  {
1346  sth = std::sqrt(tmp*(2.-tmp));
1347  have_sth = true;
1348  }
1349  }
1350  else
1351  {
1352  nr = 1;
1353  z = (2-jr)*2./3.;
1354  }
1355 
1356  double tmp=jpll[face]*nr+x-y;
1357  if (tmp<0) tmp+=8;
1358  if (tmp>=8) tmp-=8;
1359  phi = (nr<1e-15) ? 0 : (0.5*halfpi*tmp)/nr;
1360  }
1361 
1362 namespace {
1363 
1364 vec3 locToVec3 (double z, double phi, double sth, bool have_sth)
1365  {
1366  if (have_sth)
1367  return vec3(sth*cos(phi),sth*sin(phi),z);
1368  else
1369  {
1370  vec3 res;
1371  res.set_z_phi (z, phi);
1372  return res;
1373  }
1374  }
1375 
1376 } // unnamed namespace
1377 
1378 template<typename I> void T_Healpix_Base<I>::boundaries(I pix, tsize step,
1379  vector<vec3> &out) const
1380  {
1381  out.resize(4*step);
1382  int ix, iy, face;
1383  pix2xyf(pix, ix, iy, face);
1384  double dc = 0.5 / nside_;
1385  double xc = (ix + 0.5)/nside_, yc = (iy + 0.5)/nside_;
1386  double d = 1.0/(step*nside_);
1387  for (tsize i=0; i<step; ++i)
1388  {
1389  double z, phi, sth;
1390  bool have_sth;
1391  xyf2loc(xc+dc-i*d, yc+dc, face, z, phi, sth, have_sth);
1392  out[i] = locToVec3(z, phi, sth, have_sth);
1393  xyf2loc(xc-dc, yc+dc-i*d, face, z, phi, sth, have_sth);
1394  out[i+step] = locToVec3(z, phi, sth, have_sth);
1395  xyf2loc(xc-dc+i*d, yc-dc, face, z, phi, sth, have_sth);
1396  out[i+2*step] = locToVec3(z, phi, sth, have_sth);
1397  xyf2loc(xc+dc, yc-dc+i*d, face, z, phi, sth, have_sth);
1398  out[i+3*step] = locToVec3(z, phi, sth, have_sth);
1399  }
1400  }
1401 
1402 template<typename I> arr<int> T_Healpix_Base<I>::swap_cycles() const
1403  {
1404  planck_assert(order_>=0, "need hierarchical map");
1405  planck_assert(order_<=13, "map too large");
1406  arr<int> result(swap_clen[order_]);
1407  tsize ofs=0;
1408  for (int m=0; m<order_;++m) ofs+=swap_clen[m];
1409  for (tsize m=0; m<result.size();++m) result[m]=swap_cycle[m+ofs];
1410  return result;
1411  }
1412 
1413 template class T_Healpix_Base<int>;
1414 template class T_Healpix_Base<int64>;
I nest2peano(I pix) const
void query_disc(pointing ptg, double radius, rangeset< I > &pixset) const
void get_ring_info2(I ring, I &startpix, I &ringpix, double &theta, bool &shifted) const
I nest2ring(I pix) const
Healpix_Ordering_Scheme
void query_polygon_inclusive(const std::vector< pointing > &vertex, rangeset< I > &pixset, int fact=1) const
void query_polygon(const std::vector< pointing > &vertex, rangeset< I > &pixset) const
static int nside2order(I nside)
Definition: healpix_base.cc:38
void get_interpol(const pointing &ptg, fix_arr< I, 4 > &pix, fix_arr< double, 4 > &wgt) const
void SetNside(I nside, Healpix_Ordering_Scheme scheme)
void swap(T_Healpix_Base &other)
I ring2nest(I pix) const
void Set(int order, Healpix_Ordering_Scheme scheme)
void query_strip(double theta1, double theta2, bool inclusive, rangeset< I > &pixset) const
void pix2zphi(I pix, double &z, double &phi) const
Definition: healpix_base.h:176
void boundaries(I pix, tsize step, std::vector< vec3 > &out) const
static I npix2nside(I npix)
Definition: healpix_base.cc:43
void get_ring_info(I ring, I &startpix, I &ringpix, double &costheta, double &sintheta, bool &shifted) const
double max_pixrad() const
I ring_above(double z) const
Definition: healpix_base.cc:50
void query_disc_inclusive(pointing ptg, double radius, rangeset< I > &pixset, int fact=1) const
void get_ring_info_small(I ring, I &startpix, I &ringpix, bool &shifted) const
void neighbors(I pix, fix_arr< I, 8 > &result) const
I peano2nest(I pix) const
double ring2z(I ring) const
I pix2ring(I pix) const
Healpix_Ordering_Scheme scheme_
Definition: healpix_base.h:56

Generated on Thu Jul 28 2022 17:32:07 for Healpix C++