| 1 |
/* |
| 2 |
# This file is part of the Astrometry.net suite. |
| 3 |
# Licensed under a 3-clause BSD style license - see LICENSE |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <math.h> |
| 7 |
#include <assert.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include "os-features.h" |
| 12 |
#include "healpix.h" |
| 13 |
#include "mathutil.h" |
| 14 |
#include "starutil.h" |
| 15 |
#include "keywords.h" |
| 16 |
#include "permutedsort.h" |
| 17 |
|
| 18 |
#ifndef M_PI |
| 19 |
# define M_PI 3.14159265358979323846 |
| 20 |
#endif |
| 21 |
|
| 22 |
// Internal type |
| 23 |
struct hp_s { |
| 24 |
int bighp; |
| 25 |
int x; |
| 26 |
int y; |
| 27 |
}; |
| 28 |
typedef struct hp_s hp_t; |
| 29 |
|
| 30 |
static int64_t hptointl(hp_t hp, int Nside) { |
| 31 |
return healpixl_compose_xy(hp.bighp, hp.x, hp.y, Nside); |
| 32 |
} |
| 33 |
|
| 34 |
static void intltohp(int64_t pix, hp_t* hp, int Nside) { |
| 35 |
healpixl_decompose_xy(pix, &hp->bighp, &hp->x, &hp->y, Nside); |
| 36 |
} |
| 37 |
|
| 38 |
static void hp_decompose(hp_t* hp, int* php, int* px, int* py) { |
| 39 |
if (php) |
| 40 |
*php = hp->bighp; |
| 41 |
if (px) |
| 42 |
*px = hp->x; |
| 43 |
if (py) |
| 44 |
*py = hp->y; |
| 45 |
} |
| 46 |
|
| 47 |
// I've had troubles with rounding functions being declared properly |
| 48 |
// in other contexts... Declare it here so the compiler complains if |
| 49 |
// something is wrong. |
| 50 |
#ifdef _MSC_VER |
| 51 |
double round(double x) { |
| 52 |
return floor(x + 0.5); |
| 53 |
} |
| 54 |
#else |
| 55 |
double round(double x); |
| 56 |
#endif |
| 57 |
|
| 58 |
|
| 59 |
Const static Inline double mysquare(double d) { |
| 60 |
return d*d; |
| 61 |
} |
| 62 |
|
| 63 |
Const int64_t healpixl_xy_to_nested(int64_t hp, int Nside) { |
| 64 |
int bighp,x,y; |
| 65 |
int64_t index; |
| 66 |
int i; |
| 67 |
int64_t ns2 = (int64_t)Nside * (int64_t)Nside; |
| 68 |
|
| 69 |
if (hp < 0 || Nside < 0) |
| 70 |
return -1; |
| 71 |
|
| 72 |
healpixl_decompose_xy(hp, &bighp, &x, &y, Nside); |
| 73 |
if (!is_power_of_two(Nside)) { |
| 74 |
fprintf(stderr, "healpix_xy_to_nested: Nside must be a power of two.\n"); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
// We construct the index called p_n' in the healpix paper, whose bits |
| 79 |
// are taken from the bits of x and y: |
| 80 |
// x = ... b4 b2 b0 |
| 81 |
// y = ... b5 b3 b1 |
| 82 |
// We go through the bits of x,y, building up "index": |
| 83 |
index = 0; |
| 84 |
for (i=0; i<(8*sizeof(int64_t)/2); i++) { |
| 85 |
index |= (int64_t)(((y & 1) << 1) | (x & 1)) << (i*2); |
| 86 |
y >>= 1; |
| 87 |
x >>= 1; |
| 88 |
if (!x && !y) break; |
| 89 |
} |
| 90 |
|
| 91 |
return index + (int64_t)bighp * ns2; |
| 92 |
} |
| 93 |
|
| 94 |
Const int64_t healpixl_nested_to_xy(int64_t hp, int Nside) { |
| 95 |
int bighp, x, y; |
| 96 |
int64_t index; |
| 97 |
int64_t ns2 = (int64_t)Nside * (int64_t)Nside; |
| 98 |
int i; |
| 99 |
if (hp < 0 || Nside < 0) |
| 100 |
return -1; |
| 101 |
if (!is_power_of_two(Nside)) { |
| 102 |
fprintf(stderr, "healpix_xy_to_nested: Nside must be a power of two.\n"); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
bighp = (int)(hp / ns2); |
| 106 |
index = hp % ns2; |
| 107 |
x = y = 0; |
| 108 |
for (i=0; i<(8*sizeof(int64_t)/2); i++) { |
| 109 |
x |= (index & 0x1) << i; |
| 110 |
index >>= 1; |
| 111 |
y |= (index & 0x1) << i; |
| 112 |
index >>= 1; |
| 113 |
if (!index) break; |
| 114 |
} |
| 115 |
return healpixl_compose_xy(bighp, x, y, Nside); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
Const int64_t healpixl_compose_ring(int ring, int longind, int Nside) { |
| 120 |
if (ring <= Nside) |
| 121 |
// north polar |
| 122 |
return (int64_t)ring * ((int64_t)ring-1) * 2 + (int64_t)longind; |
| 123 |
if (ring < 3*Nside) |
| 124 |
// equatorial |
| 125 |
return (int64_t)Nside*((int64_t)Nside-1)*2 + (int64_t)Nside*4*((int64_t)ring-(int64_t)Nside) + (int64_t)longind; |
| 126 |
{ |
| 127 |
int64_t ri; |
| 128 |
ri = 4*(int64_t)Nside - (int64_t)ring; |
| 129 |
return 12*(int64_t)Nside*(int64_t)Nside-1 - ( ri*(ri-1)*2 + (ri*4 - 1 - (int64_t)longind) ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
void healpixl_decompose_ring(int64_t hp, int Nside, int* p_ring, int* p_longind) { |
| 135 |
int64_t longind; |
| 136 |
int64_t offset = 0; |
| 137 |
int64_t Nside64; |
| 138 |
int64_t ns2; |
| 139 |
int ring; |
| 140 |
double x; |
| 141 |
Nside64 = (int64_t)Nside; |
| 142 |
ns2 = Nside64 * Nside64; |
| 143 |
if (hp < 2 * ns2) { |
| 144 |
ring = (int)(0.5 + sqrt(0.25 + 0.5 * hp)); |
| 145 |
offset = 2 * (int64_t)ring * ((int64_t)ring - 1); |
| 146 |
// The sqrt above can introduce precision issues that can cause ring to |
| 147 |
// be off by 1, so we check whether the offset is now larger than the HEALPix |
| 148 |
// value, and if so we need to adjust ring and offset accordingly |
| 149 |
if (offset > hp) { |
| 150 |
ring -= 1; |
| 151 |
offset = 2 * (int64_t)ring * ((int64_t)ring - 1); |
| 152 |
} |
| 153 |
longind = hp - offset; |
| 154 |
} else { |
| 155 |
offset = 2 * Nside64 * (Nside64 - 1); |
| 156 |
if (hp < 10 * ns2) { |
| 157 |
ring = (int)((hp - offset) / ((int64_t)Nside * 4) + (int64_t)Nside); |
| 158 |
offset += 4 * (ring - Nside64) * Nside64; |
| 159 |
longind = hp - offset; |
| 160 |
} else { |
| 161 |
offset += 8 * ns2; |
| 162 |
x = (2 * Nside64 + 1 - sqrt((2 * Nside64 + 1) * (2 * Nside64 + 1) - 2 * (hp - offset)))*0.5; |
| 163 |
ring = (int)x; |
| 164 |
offset += 2 * (int64_t)ring * (2 * Nside64 + 1 - (int64_t)ring); |
| 165 |
// The sqrt above can introduce precision issues that can cause ring to |
| 166 |
// be off by 1, so we check whether the offset is now larger than the HEALPix |
| 167 |
// value, and if so we need to adjust ring and offset accordingly |
| 168 |
if (offset > hp) { |
| 169 |
ring -= 1; |
| 170 |
offset -= 4 * Nside64 - 4 * (int64_t)ring; |
| 171 |
} |
| 172 |
longind = (int)(hp - offset); |
| 173 |
ring += 3 * Nside; |
| 174 |
} |
| 175 |
} |
| 176 |
if (p_ring) |
| 177 |
*p_ring = ring; |
| 178 |
if (p_longind) |
| 179 |
*p_longind = (int)longind; |
| 180 |
} |
| 181 |
|
| 182 |
Const int64_t healpixl_ring_to_xy(int64_t ring, int Nside) { |
| 183 |
int bighp, x, y; |
| 184 |
int ringind, longind; |
| 185 |
healpixl_decompose_ring(ring, Nside, &ringind, &longind); |
| 186 |
if (ring < 0 || Nside < 0) { |
| 187 |
return -1; |
| 188 |
} else if (ringind <= Nside) { |
| 189 |
int64_t ind; |
| 190 |
int v; |
| 191 |
int F1; |
| 192 |
int frow; |
| 193 |
bighp = longind / ringind; |
| 194 |
ind = (int64_t)longind - (int64_t)bighp * (int64_t)ringind; |
| 195 |
y = (Nside - 1) - (int)ind; |
| 196 |
frow = bighp / 4; |
| 197 |
F1 = frow + 2; |
| 198 |
v = F1*Nside - ringind - 1; |
| 199 |
x = v - y; |
| 200 |
return healpixl_compose_xy(bighp, x, y, Nside); |
| 201 |
} else if (ringind < (int64_t)3*Nside) { |
| 202 |
int panel; |
| 203 |
int ind; |
| 204 |
int bottomleft; |
| 205 |
int topleft; |
| 206 |
int frow, F1, F2, s, v, h; |
| 207 |
int bighp = -1; |
| 208 |
int x, y; |
| 209 |
int R = 0; |
| 210 |
|
| 211 |
panel = longind / Nside; |
| 212 |
ind = longind % Nside; |
| 213 |
bottomleft = ind < (ringind - Nside + 1) / 2; |
| 214 |
topleft = ind < ((int64_t)3*Nside - ringind + 1)/2; |
| 215 |
|
| 216 |
if (!bottomleft && topleft) { |
| 217 |
// top row. |
| 218 |
bighp = panel; |
| 219 |
} else if (bottomleft && !topleft) { |
| 220 |
// bottom row. |
| 221 |
bighp = 8 + panel; |
| 222 |
} else if (bottomleft && topleft) { |
| 223 |
// left side. |
| 224 |
bighp = 4 + panel; |
| 225 |
} else if (!bottomleft && !topleft) { |
| 226 |
// right side. |
| 227 |
bighp = 4 + (panel + 1) % 4; |
| 228 |
if (bighp == 4) { |
| 229 |
longind -= ((int64_t)4*Nside - 1); |
| 230 |
// Gah! Wacky hack - it seems that since |
| 231 |
// "longind" is negative in this case, the |
| 232 |
// rounding behaves differently, so we end up |
| 233 |
// computing the wrong "h" and have to correct |
| 234 |
// for it. |
| 235 |
R = 1; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
frow = bighp / 4; |
| 240 |
F1 = frow + 2; |
| 241 |
F2 = 2*(bighp % 4) - (frow % 2) + 1; |
| 242 |
s = (ringind - Nside) % 2; |
| 243 |
v = F1*Nside - ringind - 1; |
| 244 |
h = 2*longind - s - F2*Nside; |
| 245 |
if (R) |
| 246 |
h--; |
| 247 |
x = (v + h) / 2; |
| 248 |
y = (v - h) / 2; |
| 249 |
//fprintf(stderr, "bighp=%i, frow=%i, F1=%i, F2=%i, s=%i, v=%i, h=%i, x=%i, y=%i.\n", bighp, frow, F1, F2, s, v, h, x, y); |
| 250 |
|
| 251 |
if ((v != (x+y)) || (h != (x-y))) { |
| 252 |
h++; |
| 253 |
x = (v + h) / 2; |
| 254 |
y = (v - h) / 2; |
| 255 |
//fprintf(stderr, "tweak h=%i, x=%i, y=%i\n", h, x, y); |
| 256 |
|
| 257 |
if ((v != (x+y)) || (h != (x-y))) { |
| 258 |
//fprintf(stderr, "still not right.\n"); |
| 259 |
} |
| 260 |
} |
| 261 |
return healpixl_compose_xy(bighp, x, y, Nside); |
| 262 |
} else { |
| 263 |
int ind; |
| 264 |
int v; |
| 265 |
int F1; |
| 266 |
int frow; |
| 267 |
int ri; |
| 268 |
ri = 4*Nside - ringind; |
| 269 |
bighp = 8 + longind / ri; |
| 270 |
ind = longind - (bighp%4) * ri; |
| 271 |
y = (ri-1) - ind; |
| 272 |
frow = bighp / 4; |
| 273 |
F1 = frow + 2; |
| 274 |
v = F1*Nside - ringind - 1; |
| 275 |
x = v - y; |
| 276 |
return healpixl_compose_xy(bighp, x, y, Nside); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
Const int64_t healpixl_xy_to_ring(int64_t hp, int Nside) { |
| 281 |
int bighp,x,y; |
| 282 |
int frow; |
| 283 |
int F1; |
| 284 |
int v; |
| 285 |
int ring; |
| 286 |
int64_t index; |
| 287 |
|
| 288 |
healpixl_decompose_xy(hp, &bighp, &x, &y, Nside); |
| 289 |
frow = bighp / 4; |
| 290 |
F1 = frow + 2; |
| 291 |
v = x + y; |
| 292 |
// "ring" starts from 1 at the north pole and goes to 4Nside-1 at |
| 293 |
// the south pole; the pixels in each ring have the same latitude. |
| 294 |
ring = F1*Nside - v - 1; |
| 295 |
/* |
| 296 |
ring: |
| 297 |
[1, Nside] : n pole |
| 298 |
(Nside, 2Nside] : n equatorial |
| 299 |
(2Nside+1, 3Nside) : s equat |
| 300 |
[3Nside, 4Nside-1] : s pole |
| 301 |
*/ |
| 302 |
// this probably can't happen... |
| 303 |
if ((ring < 1) || (ring >= (int64_t)4*Nside)) { |
| 304 |
//fprintf(stderr, "Invalid ring index: %i %i\n", ring, 4*Nside); |
| 305 |
return -1; |
| 306 |
} |
| 307 |
if (ring <= Nside) { |
| 308 |
// north polar. |
| 309 |
// left-to-right coordinate within this healpix |
| 310 |
index = (Nside - 1 - y); |
| 311 |
// offset from the other big healpixes |
| 312 |
index += ((bighp % 4) * ring); |
| 313 |
// offset from the other rings |
| 314 |
index += (int64_t)ring*(ring-1)*2; |
| 315 |
} else if (ring >= (int64_t)3*Nside) { |
| 316 |
// south polar. |
| 317 |
// Here I first flip everything so that we label the pixels |
| 318 |
// at zero starting in the southeast corner, increasing to the |
| 319 |
// west and north, then subtract that from the total number of |
| 320 |
// healpixels. |
| 321 |
int ri = (int64_t)4*Nside - ring; |
| 322 |
// index within this healpix |
| 323 |
index = (ri-1) - x; |
| 324 |
// big healpixes |
| 325 |
index += ((3-(bighp % 4)) * ri); |
| 326 |
// other rings |
| 327 |
index += (int64_t)ri*(ri-1)*2; |
| 328 |
// flip! |
| 329 |
index = 12*(int64_t)Nside*Nside - 1 - index; |
| 330 |
|
| 331 |
} else { |
| 332 |
// equatorial. |
| 333 |
int64_t s, F2, h; |
| 334 |
s = (ring - Nside) % 2; |
| 335 |
F2 = 2*((int)bighp % 4) - (frow % 2) + 1; |
| 336 |
h = x - y; |
| 337 |
|
| 338 |
index = (F2 * Nside + h + s) / 2; |
| 339 |
// offset from the north polar region: |
| 340 |
index += (int64_t)Nside * (Nside - 1) * 2; |
| 341 |
// offset within the equatorial region: |
| 342 |
index += (int64_t)Nside * 4 * (ring - Nside); |
| 343 |
// handle healpix #4 wrap-around |
| 344 |
if ((bighp == 4) && (y > x)) index += (4 * Nside - 1); |
| 345 |
//fprintf(stderr, "frow=%i, F1=%i, v=%i, ringind=%i, s=%i, F2=%i, h=%i, longind=%i.\n", frow, F1, v, ring, s, F2, h, (F2*(int)Nside+h+s)/2); |
| 346 |
} |
| 347 |
return index; |
| 348 |
} |
| 349 |
|
| 350 |
Const double healpix_side_length_arcmin(int Nside) { |
| 351 |
return sqrt((4.0 * M_PI * mysquare(180.0 * 60.0 / M_PI)) / |
| 352 |
(12.0 * Nside * Nside)); |
| 353 |
} |
| 354 |
|
| 355 |
double healpix_nside_for_side_length_arcmin(double arcmin) { |
| 356 |
return sqrt(4.0*M_PI / (mysquare(arcmin2rad(arcmin)) * 12.0)); |
| 357 |
} |
| 358 |
|
| 359 |
static Inline void swap(int* i1, int* i2) { |
| 360 |
int tmp; |
| 361 |
tmp = *i1; |
| 362 |
*i1 = *i2; |
| 363 |
*i2 = tmp; |
| 364 |
} |
| 365 |
|
| 366 |
static Inline void swap_double(double* i1, double* i2) { |
| 367 |
double tmp; |
| 368 |
tmp = *i1; |
| 369 |
*i1 = *i2; |
| 370 |
*i2 = tmp; |
| 371 |
} |
| 372 |
|
| 373 |
static Inline anbool ispolar(int healpix) |
| 374 |
{ |
| 375 |
// the north polar healpixes are 0,1,2,3 |
| 376 |
// the south polar healpixes are 8,9,10,11 |
| 377 |
return (healpix <= 3) || (healpix >= 8); |
| 378 |
} |
| 379 |
|
| 380 |
static Inline anbool isequatorial(int healpix) |
| 381 |
{ |
| 382 |
// the north polar healpixes are 0,1,2,3 |
| 383 |
// the south polar healpixes are 8,9,10,11 |
| 384 |
return (healpix >= 4) && (healpix <= 7); |
| 385 |
} |
| 386 |
|
| 387 |
static Inline anbool isnorthpolar(int healpix) |
| 388 |
{ |
| 389 |
return (healpix <= 3); |
| 390 |
} |
| 391 |
|
| 392 |
static Inline anbool issouthpolar(int healpix) |
| 393 |
{ |
| 394 |
return (healpix >= 8); |
| 395 |
} |
| 396 |
|
| 397 |
int64_t healpixl_compose_xy(int bighp, int x, int y, int Nside) { |
| 398 |
int64_t ns = Nside; |
| 399 |
assert(Nside > 0); |
| 400 |
assert(bighp >= 0); |
| 401 |
assert(bighp < 12); |
| 402 |
assert(x >= 0); |
| 403 |
assert(x < Nside); |
| 404 |
assert(y >= 0); |
| 405 |
assert(y < Nside); |
| 406 |
return ((((int64_t)bighp * ns) + x) * ns) + y; |
| 407 |
} |
| 408 |
|
| 409 |
void healpixl_convert_nside(int64_t hp, int nside, int outnside, int64_t* outhp) { |
| 410 |
int basehp, x, y; |
| 411 |
int ox, oy; |
| 412 |
healpixl_decompose_xy(hp, &basehp, &x, &y, nside); |
| 413 |
healpixl_convert_xy_nside(x, y, nside, outnside, &ox, &oy); |
| 414 |
*outhp = healpixl_compose_xy(basehp, ox, oy, outnside); |
| 415 |
} |
| 416 |
|
| 417 |
void healpixl_convert_xy_nside(int x, int y, int nside, int outnside, |
| 418 |
int* outx, int* outy) { |
| 419 |
double fx, fy; |
| 420 |
int ox, oy; |
| 421 |
assert(x >= 0); |
| 422 |
assert(x < nside); |
| 423 |
assert(y >= 0); |
| 424 |
assert(y < nside); |
| 425 |
|
| 426 |
// MAGIC 0.5: assume center of pixel... |
| 427 |
fx = (x + 0.5) / (double)nside; |
| 428 |
fy = (y + 0.5) / (double)nside; |
| 429 |
|
| 430 |
ox = floor(fx * outnside); |
| 431 |
oy = floor(fy * outnside); |
| 432 |
|
| 433 |
if (outx) |
| 434 |
*outx = ox; |
| 435 |
if (outy) |
| 436 |
*outy = oy; |
| 437 |
} |
| 438 |
|
| 439 |
void healpixl_decompose_xy(int64_t finehp, |
| 440 |
int* pbighp, int* px, int* py, |
| 441 |
int Nside) { |
| 442 |
int64_t hp; |
| 443 |
int64_t ns2 = (int64_t)Nside * (int64_t)Nside; |
| 444 |
assert(Nside > 0); |
| 445 |
assert(finehp < ((int64_t)12 * ns2)); |
| 446 |
assert(finehp >= 0); |
| 447 |
if (pbighp) { |
| 448 |
int bighp = (int)(finehp / ns2); |
| 449 |
assert(bighp >= 0); |
| 450 |
assert(bighp < 12); |
| 451 |
*pbighp = bighp; |
| 452 |
} |
| 453 |
hp = finehp % ns2; |
| 454 |
if (px) { |
| 455 |
*px = (int)(hp / Nside); |
| 456 |
assert(*px >= 0); |
| 457 |
assert(*px < Nside); |
| 458 |
} |
| 459 |
if (py) { |
| 460 |
*py = hp % Nside; |
| 461 |
assert(*py >= 0); |
| 462 |
assert(*py < Nside); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
Given a large-scale healpix number, computes its neighbour in the |
| 468 |
direction (dx,dy). Returns -1 if there is no such neighbour. |
| 469 |
*/ |
| 470 |
static int healpix_get_neighbour(int hp, int dx, int dy) |
| 471 |
{ |
| 472 |
if (isnorthpolar(hp)) { |
| 473 |
if ((dx == 1) && (dy == 0)) return (hp + 1) % 4; |
| 474 |
if ((dx == 0) && (dy == 1)) return (hp + 3) % 4; |
| 475 |
if ((dx == 1) && (dy == 1)) return (hp + 2) % 4; |
| 476 |
if ((dx == -1) && (dy == 0)) return (hp + 4); |
| 477 |
if ((dx == 0) && (dy == -1)) return 4 + ((hp + 1) % 4); |
| 478 |
if ((dx == -1) && (dy == -1)) return hp + 8; |
| 479 |
return -1; |
| 480 |
} else if (issouthpolar(hp)) { |
| 481 |
if ((dx == 1) && (dy == 0)) return 4 + ((hp + 1) % 4); |
| 482 |
if ((dx == 0) && (dy == 1)) return hp - 4; |
| 483 |
if ((dx == -1) && (dy == 0)) return 8 + ((hp + 3) % 4); |
| 484 |
if ((dx == 0) && (dy == -1)) return 8 + ((hp + 1) % 4); |
| 485 |
if ((dx == -1) && (dy == -1)) return 8 + ((hp + 2) % 4); |
| 486 |
if ((dx == 1) && (dy == 1)) return hp - 8; |
| 487 |
return -1; |
| 488 |
} else { |
| 489 |
if ((dx == 1) && (dy == 0)) return hp - 4; |
| 490 |
if ((dx == 0) && (dy == 1)) return (hp + 3) % 4; |
| 491 |
if ((dx == -1) && (dy == 0)) return 8 + ((hp + 3) % 4); |
| 492 |
if ((dx == 0) && (dy == -1)) return hp + 4; |
| 493 |
if ((dx == 1) && (dy == -1)) return 4 + ((hp + 1) % 4); |
| 494 |
if ((dx == -1) && (dy == 1)) return 4 + ((hp - 1) % 4); |
| 495 |
return -1; |
| 496 |
} |
| 497 |
return -1; |
| 498 |
} |
| 499 |
|
| 500 |
static void get_neighbours(hp_t hp, hp_t* neighbour, int Nside) { |
| 501 |
int base; |
| 502 |
int x, y; |
| 503 |
int nbase; |
| 504 |
int nx, ny; |
| 505 |
|
| 506 |
base = hp.bighp; |
| 507 |
x = hp.x; |
| 508 |
y = hp.y; |
| 509 |
|
| 510 |
// ( + , 0 ) |
| 511 |
nx = (x + 1) % Nside; |
| 512 |
ny = y; |
| 513 |
if (x == (Nside - 1)) { |
| 514 |
nbase = healpix_get_neighbour(base, 1, 0); |
| 515 |
if (isnorthpolar(base)) { |
| 516 |
nx = x; |
| 517 |
swap(&nx, &ny); |
| 518 |
} |
| 519 |
} else |
| 520 |
nbase = base; |
| 521 |
|
| 522 |
neighbour[0].bighp = nbase; |
| 523 |
neighbour[0].x = nx; |
| 524 |
neighbour[0].y = ny; |
| 525 |
|
| 526 |
// ( + , + ) |
| 527 |
nx = (x + 1) % Nside; |
| 528 |
ny = (y + 1) % Nside; |
| 529 |
if ((x == Nside - 1) && (y == Nside - 1)) { |
| 530 |
if (ispolar(base)) |
| 531 |
nbase = healpix_get_neighbour(base, 1, 1); |
| 532 |
else |
| 533 |
nbase = -1; |
| 534 |
} else if (x == (Nside - 1)) |
| 535 |
nbase = healpix_get_neighbour(base, 1, 0); |
| 536 |
else if (y == (Nside - 1)) |
| 537 |
nbase = healpix_get_neighbour(base, 0, 1); |
| 538 |
else |
| 539 |
nbase = base; |
| 540 |
|
| 541 |
if (isnorthpolar(base)) { |
| 542 |
if (x == (Nside - 1)) |
| 543 |
nx = Nside - 1; |
| 544 |
if (y == (Nside - 1)) |
| 545 |
ny = Nside - 1; |
| 546 |
if ((x == (Nside - 1)) || (y == (Nside - 1))) |
| 547 |
swap(&nx, &ny); |
| 548 |
} |
| 549 |
|
| 550 |
//printf("(+ +): nbase=%i, nx=%i, ny=%i, pix=%i\n", nbase, nx, ny, nbase*Ns2+xy_to_pnprime(nx,ny,Nside)); |
| 551 |
|
| 552 |
neighbour[1].bighp = nbase; |
| 553 |
neighbour[1].x = nx; |
| 554 |
neighbour[1].y = ny; |
| 555 |
|
| 556 |
// ( 0 , + ) |
| 557 |
nx = x; |
| 558 |
ny = (y + 1) % Nside; |
| 559 |
if (y == (Nside - 1)) { |
| 560 |
nbase = healpix_get_neighbour(base, 0, 1); |
| 561 |
if (isnorthpolar(base)) { |
| 562 |
ny = y; |
| 563 |
swap(&nx, &ny); |
| 564 |
} |
| 565 |
} else |
| 566 |
nbase = base; |
| 567 |
|
| 568 |
//printf("(0 +): nbase=%i, nx=%i, ny=%i, pix=%i\n", nbase, nx, ny, nbase*Ns2+xy_to_pnprime(nx,ny,Nside)); |
| 569 |
|
| 570 |
neighbour[2].bighp = nbase; |
| 571 |
neighbour[2].x = nx; |
| 572 |
neighbour[2].y = ny; |
| 573 |
|
| 574 |
// ( - , + ) |
| 575 |
nx = (x + Nside - 1) % Nside; |
| 576 |
ny = (y + 1) % Nside; |
| 577 |
if ((x == 0) && (y == (Nside - 1))) { |
| 578 |
if (isequatorial(base)) |
| 579 |
nbase = healpix_get_neighbour(base, -1, 1); |
| 580 |
else |
| 581 |
nbase = -1; |
| 582 |
} else if (x == 0) { |
| 583 |
nbase = healpix_get_neighbour(base, -1, 0); |
| 584 |
if (issouthpolar(base)) { |
| 585 |
nx = 0; |
| 586 |
swap(&nx, &ny); |
| 587 |
} |
| 588 |
} else if (y == (Nside - 1)) { |
| 589 |
nbase = healpix_get_neighbour(base, 0, 1); |
| 590 |
if (isnorthpolar(base)) { |
| 591 |
ny = y; |
| 592 |
swap(&nx, &ny); |
| 593 |
} |
| 594 |
} else |
| 595 |
nbase = base; |
| 596 |
|
| 597 |
//printf("(- +): nbase=%i, nx=%i, ny=%i, pix=%i\n", nbase, nx, ny, nbase*Ns2+xy_to_pnprime(nx,ny,Nside)); |
| 598 |
|
| 599 |
neighbour[3].bighp = nbase; |
| 600 |
neighbour[3].x = nx; |
| 601 |
neighbour[3].y = ny; |
| 602 |
|
| 603 |
// ( - , 0 ) |
| 604 |
nx = (x + Nside - 1) % Nside; |
| 605 |
ny = y; |
| 606 |
if (x == 0) { |
| 607 |
nbase = healpix_get_neighbour(base, -1, 0); |
| 608 |
if (issouthpolar(base)) { |
| 609 |
nx = 0; |
| 610 |
swap(&nx, &ny); |
| 611 |
} |
| 612 |
} else |
| 613 |
nbase = base; |
| 614 |
|
| 615 |
//printf("(- 0): nbase=%i, nx=%i, ny=%i, pix=%i\n", nbase, nx, ny, nbase*Ns2+xy_to_pnprime(nx,ny,Nside)); |
| 616 |
|
| 617 |
neighbour[4].bighp = nbase; |
| 618 |
neighbour[4].x = nx; |
| 619 |
neighbour[4].y = ny; |
| 620 |
|
| 621 |
// ( - , - ) |
| 622 |
nx = (x + Nside - 1) % Nside; |
| 623 |
ny = (y + Nside - 1) % Nside; |
| 624 |
if ((x == 0) && (y == 0)) { |
| 625 |
if (ispolar(base)) |
| 626 |
nbase = healpix_get_neighbour(base, -1, -1); |
| 627 |
else |
| 628 |
nbase = -1; |
| 629 |
} else if (x == 0) |
| 630 |
nbase = healpix_get_neighbour(base, -1, 0); |
| 631 |
else if (y == 0) |
| 632 |
nbase = healpix_get_neighbour(base, 0, -1); |
| 633 |
else |
| 634 |
nbase = base; |
| 635 |
|
| 636 |
if (issouthpolar(base)) { |
| 637 |
if (x == 0) |
| 638 |
nx = 0; |
| 639 |
if (y == 0) |
| 640 |
ny = 0; |
| 641 |
if ((x == 0) || (y == 0)) |
| 642 |
swap(&nx, &ny); |
| 643 |
} |
| 644 |
|
| 645 |
//printf("(- -): nbase=%i, nx=%i, ny=%i, pix=%i\n", nbase, nx, ny, nbase*Ns2+xy_to_pnprime(nx,ny,Nside)); |
| 646 |
|
| 647 |
neighbour[5].bighp = nbase; |
| 648 |
neighbour[5].x = nx; |
| 649 |
neighbour[5].y = ny; |
| 650 |
|
| 651 |
// ( 0 , - ) |
| 652 |
ny = (y + Nside - 1) % Nside; |
| 653 |
nx = x; |
| 654 |
if (y == 0) { |
| 655 |
nbase = healpix_get_neighbour(base, 0, -1); |
| 656 |
if (issouthpolar(base)) { |
| 657 |
ny = y; |
| 658 |
swap(&nx, &ny); |
| 659 |
} |
| 660 |
} else |
| 661 |
nbase = base; |
| 662 |
|
| 663 |
//printf("(0 -): nbase=%i, nx=%i, ny=%i, pix=%i\n", nbase, nx, ny, nbase*Ns2+xy_to_pnprime(nx,ny,Nside)); |
| 664 |
|
| 665 |
neighbour[6].bighp = nbase; |
| 666 |
neighbour[6].x = nx; |
| 667 |
neighbour[6].y = ny; |
| 668 |
|
| 669 |
// ( + , - ) |
| 670 |
nx = (x + 1) % Nside; |
| 671 |
ny = (y + Nside - 1) % Nside; |
| 672 |
if ((x == (Nside - 1)) && (y == 0)) { |
| 673 |
if (isequatorial(base)) { |
| 674 |
nbase = healpix_get_neighbour(base, 1, -1); |
| 675 |
} else |
| 676 |
nbase = -1; |
| 677 |
|
| 678 |
} else if (x == (Nside - 1)) { |
| 679 |
nbase = healpix_get_neighbour(base, 1, 0); |
| 680 |
if (isnorthpolar(base)) { |
| 681 |
nx = x; |
| 682 |
swap(&nx, &ny); |
| 683 |
} |
| 684 |
} else if (y == 0) { |
| 685 |
nbase = healpix_get_neighbour(base, 0, -1); |
| 686 |
if (issouthpolar(base)) { |
| 687 |
ny = y; |
| 688 |
swap(&nx, &ny); |
| 689 |
} |
| 690 |
} else |
| 691 |
nbase = base; |
| 692 |
|
| 693 |
//printf("(+ -): nbase=%i, nx=%i, ny=%i, pix=%i\n", nbase, nx, ny, nbase*Ns2+xy_to_pnprime(nx,ny,Nside)); |
| 694 |
|
| 695 |
neighbour[7].bighp = nbase; |
| 696 |
neighbour[7].x = nx; |
| 697 |
neighbour[7].y = ny; |
| 698 |
|
| 699 |
} |
| 700 |
|
| 701 |
void healpixl_get_neighbours(int64_t pix, int64_t* neighbour, int Nside) { |
| 702 |
hp_t neigh[8]; |
| 703 |
hp_t hp; |
| 704 |
int i; |
| 705 |
intltohp(pix, &hp, Nside); |
| 706 |
get_neighbours(hp, neigh, Nside); |
| 707 |
for (i=0; i<8; i++) |
| 708 |
if (neigh[i].bighp < 0) { |
| 709 |
neighbour[i] = -1; |
| 710 |
} else { |
| 711 |
neighbour[i] = hptointl(neigh[i], Nside); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
static hp_t xyztohp(double vx, double vy, double vz, double coz, |
| 716 |
int Nside, |
| 717 |
double* p_dx, double* p_dy) { |
| 718 |
double phi; |
| 719 |
double twothirds = 2.0 / 3.0; |
| 720 |
double pi = M_PI; |
| 721 |
double twopi = 2.0 * M_PI; |
| 722 |
double halfpi = 0.5 * M_PI; |
| 723 |
double root3 = sqrt(3.0); |
| 724 |
double dx, dy; |
| 725 |
int basehp; |
| 726 |
int x, y; |
| 727 |
double sector; |
| 728 |
int offset; |
| 729 |
double phi_t; |
| 730 |
hp_t hp; |
| 731 |
|
| 732 |
// only used in asserts() |
| 733 |
VarUnused double EPS = 1e-8; |
| 734 |
|
| 735 |
assert(Nside > 0); |
| 736 |
|
| 737 |
/* Convert our point into cylindrical coordinates for middle ring */ |
| 738 |
phi = atan2(vy, vx); |
| 739 |
if (phi < 0.0) |
| 740 |
phi += twopi; |
| 741 |
phi_t = fmod(phi, halfpi); |
| 742 |
assert (phi_t >= 0.0); |
| 743 |
|
| 744 |
// North or south polar cap. |
| 745 |
if ((vz >= twothirds) || (vz <= -twothirds)) { |
| 746 |
anbool north; |
| 747 |
int column; |
| 748 |
double xx, yy, kx, ky; |
| 749 |
|
| 750 |
// Which pole? |
| 751 |
if (vz >= twothirds) { |
| 752 |
north = TRUE; |
| 753 |
} else { |
| 754 |
north = FALSE; |
| 755 |
vz *= -1.0; |
| 756 |
} |
| 757 |
|
| 758 |
// if not passed, compute coz |
| 759 |
if (coz == 0.0) |
| 760 |
coz = hypot(vx, vy); |
| 761 |
|
| 762 |
// solve eqn 20: k = Ns - xx (in the northern hemi) |
| 763 |
kx = (coz / sqrt(1.0 + vz)) * root3 * fabs(Nside * (2.0 * phi_t - pi) / pi); |
| 764 |
|
| 765 |
// solve eqn 19 for k = Ns - yy |
| 766 |
ky = (coz / sqrt(1.0 + vz)) * root3 * Nside * 2.0 * phi_t / pi; |
| 767 |
|
| 768 |
if (north) { |
| 769 |
xx = Nside - kx; |
| 770 |
yy = Nside - ky; |
| 771 |
} else { |
| 772 |
xx = ky; |
| 773 |
yy = kx; |
| 774 |
} |
| 775 |
|
| 776 |
// xx, yy should be in [0, Nside]. |
| 777 |
x = MIN(Nside-1, floor(xx)); |
| 778 |
assert(x >= 0); |
| 779 |
assert(x < Nside); |
| 780 |
|
| 781 |
y = MIN(Nside-1, floor(yy)); |
| 782 |
assert(y >= 0); |
| 783 |
assert(y < Nside); |
| 784 |
|
| 785 |
dx = xx - x; |
| 786 |
dy = yy - y; |
| 787 |
|
| 788 |
sector = (phi - phi_t) / (halfpi); |
| 789 |
offset = (int)round(sector); |
| 790 |
assert(fabs(sector - offset) < EPS); |
| 791 |
offset = ((offset % 4) + 4) % 4; |
| 792 |
assert(offset >= 0); |
| 793 |
assert(offset <= 3); |
| 794 |
column = offset; |
| 795 |
|
| 796 |
if (north) |
| 797 |
basehp = column; |
| 798 |
else |
| 799 |
basehp = 8 + column; |
| 800 |
|
| 801 |
} else { |
| 802 |
// could be polar or equatorial. |
| 803 |
double sector; |
| 804 |
int offset; |
| 805 |
double u1, u2; |
| 806 |
double zunits, phiunits; |
| 807 |
double xx, yy; |
| 808 |
|
| 809 |
// project into the unit square z=[-2/3, 2/3], phi=[0, pi/2] |
| 810 |
zunits = (vz + twothirds) / (4.0 / 3.0); |
| 811 |
phiunits = phi_t / halfpi; |
| 812 |
// convert into diagonal units |
| 813 |
// (add 1 to u2 so that they both cover the range [0,2]. |
| 814 |
u1 = zunits + phiunits; |
| 815 |
u2 = zunits - phiunits + 1.0; |
| 816 |
assert(u1 >= 0.); |
| 817 |
assert(u1 <= 2.); |
| 818 |
assert(u2 >= 0.); |
| 819 |
assert(u2 <= 2.); |
| 820 |
// x is the northeast direction, y is the northwest. |
| 821 |
xx = u1 * Nside; |
| 822 |
yy = u2 * Nside; |
| 823 |
|
| 824 |
// now compute which big healpix it's in. |
| 825 |
// (note that we subtract off the modded portion used to |
| 826 |
// compute the position within the healpix, so this should be |
| 827 |
// very close to one of the boundaries.) |
| 828 |
sector = (phi - phi_t) / (halfpi); |
| 829 |
offset = (int)round(sector); |
| 830 |
assert(fabs(sector - offset) < EPS); |
| 831 |
offset = ((offset % 4) + 4) % 4; |
| 832 |
assert(offset >= 0); |
| 833 |
assert(offset <= 3); |
| 834 |
|
| 835 |
// we're looking at a square in z,phi space with an X dividing it. |
| 836 |
// we want to know which section we're in. |
| 837 |
// xx ranges from 0 in the bottom-left to 2Nside in the top-right. |
| 838 |
// yy ranges from 0 in the bottom-right to 2Nside in the top-left. |
| 839 |
// (of the phi,z unit box) |
| 840 |
if (xx >= Nside) { |
| 841 |
xx -= Nside; |
| 842 |
if (yy >= Nside) { |
| 843 |
// north polar. |
| 844 |
yy -= Nside; |
| 845 |
basehp = offset; |
| 846 |
} else { |
| 847 |
// right equatorial. |
| 848 |
basehp = ((offset + 1) % 4) + 4; |
| 849 |
} |
| 850 |
} else { |
| 851 |
if (yy >= Nside) { |
| 852 |
// left equatorial. |
| 853 |
yy -= Nside; |
| 854 |
basehp = offset + 4; |
| 855 |
} else { |
| 856 |
// south polar. |
| 857 |
basehp = 8 + offset; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
assert(xx >= -EPS); |
| 862 |
assert(xx < (Nside+EPS)); |
| 863 |
x = MAX(0, MIN(Nside-1, floor(xx))); |
| 864 |
assert(x >= 0); |
| 865 |
assert(x < Nside); |
| 866 |
dx = xx - x; |
| 867 |
|
| 868 |
assert(yy >= -EPS); |
| 869 |
assert(yy < (Nside+EPS)); |
| 870 |
y = MAX(0, MIN(Nside-1, floor(yy))); |
| 871 |
assert(y >= 0); |
| 872 |
assert(y < Nside); |
| 873 |
dy = yy - y; |
| 874 |
} |
| 875 |
|
| 876 |
hp.bighp = basehp; |
| 877 |
hp.x = x; |
| 878 |
hp.y = y; |
| 879 |
|
| 880 |
if (p_dx) *p_dx = dx; |
| 881 |
if (p_dy) *p_dy = dy; |
| 882 |
|
| 883 |
return hp; |
| 884 |
} |
| 885 |
|
| 886 |
int64_t xyztohealpixl(double x, double y, double z, int Nside) { |
| 887 |
return xyztohealpixlf(x, y, z, Nside, NULL, NULL); |
| 888 |
} |
| 889 |
|
| 890 |
int64_t xyztohealpixlf(double x, double y, double z, int Nside, |
| 891 |
double* p_dx, double* p_dy) { |
| 892 |
hp_t hp = xyztohp(x,y,z, 0., Nside, p_dx,p_dy); |
| 893 |
return hptointl(hp, Nside); |
| 894 |
} |
| 895 |
|
| 896 |
int64_t radec_to_healpixl(double ra, double dec, int Nside) { |
| 897 |
return xyztohealpixl(radec2x(ra,dec), radec2y(ra,dec), radec2z(ra,dec), Nside); |
| 898 |
} |
| 899 |
|
| 900 |
int64_t radec_to_healpixlf(double ra, double dec, int Nside, double* dx, double* dy) { |
| 901 |
hp_t hp = xyztohp(radec2x(ra,dec), radec2y(ra,dec), |
| 902 |
radec2z(ra,dec), cos(dec), Nside, dx, dy); |
| 903 |
return hptointl(hp, Nside); |
| 904 |
} |
| 905 |
|
| 906 |
Const int64_t radecdegtohealpixl(double ra, double dec, int Nside) { |
| 907 |
return radec_to_healpixl(deg2rad(ra), deg2rad(dec), Nside); |
| 908 |
} |
| 909 |
|
| 910 |
int64_t radecdegtohealpixlf(double ra, double dec, int Nside, double* dx, double* dy) { |
| 911 |
return radec_to_healpixlf(deg2rad(ra), deg2rad(dec), Nside, dx, dy); |
| 912 |
} |
| 913 |
|
| 914 |
int64_t xyzarrtohealpixl(const double* xyz, int Nside) { |
| 915 |
return xyztohealpixl(xyz[0], xyz[1], xyz[2], Nside); |
| 916 |
} |
| 917 |
|
| 918 |
int64_t xyzarrtohealpixlf(const double* xyz, int Nside, double* dx, double* dy) { |
| 919 |
return xyztohealpixlf(xyz[0], xyz[1], xyz[2], Nside, dx, dy); |
| 920 |
} |
| 921 |
|
| 922 |
static void hp_to_xyz(hp_t* hp, int Nside, |
| 923 |
double dx, double dy, |
| 924 |
double* rx, double *ry, double *rz) { |
| 925 |
int chp; |
| 926 |
anbool equatorial = TRUE; |
| 927 |
double zfactor = 1.0; |
| 928 |
int xp, yp; |
| 929 |
double x, y, z; |
| 930 |
double pi = M_PI, phi; |
| 931 |
double rad; |
| 932 |
|
| 933 |
hp_decompose(hp, &chp, &xp, &yp); |
| 934 |
|
| 935 |
// this is x,y position in the healpix reference frame |
| 936 |
x = xp + dx; |
| 937 |
y = yp + dy; |
| 938 |
|
| 939 |
if (isnorthpolar(chp)) { |
| 940 |
if ((x + y) > Nside) { |
| 941 |
equatorial = FALSE; |
| 942 |
zfactor = 1.0; |
| 943 |
} |
| 944 |
} |
| 945 |
if (issouthpolar(chp)) { |
| 946 |
if ((x + y) < Nside) { |
| 947 |
equatorial = FALSE; |
| 948 |
zfactor = -1.0; |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
if (equatorial) { |
| 953 |
double zoff=0; |
| 954 |
double phioff=0; |
| 955 |
x /= (double)Nside; |
| 956 |
y /= (double)Nside; |
| 957 |
|
| 958 |
if (chp <= 3) { |
| 959 |
// north |
| 960 |
phioff = 1.0; |
| 961 |
} else if (chp <= 7) { |
| 962 |
// equator |
| 963 |
zoff = -1.0; |
| 964 |
chp -= 4; |
| 965 |
} else if (chp <= 11) { |
| 966 |
// south |
| 967 |
phioff = 1.0; |
| 968 |
zoff = -2.0; |
| 969 |
chp -= 8; |
| 970 |
} else { |
| 971 |
// should never get here |
| 972 |
assert(0); |
| 973 |
} |
| 974 |
|
| 975 |
z = 2.0/3.0*(x + y + zoff); |
| 976 |
phi = pi/4*(x - y + phioff + 2*chp); |
| 977 |
rad = sqrt(1.0 - z*z); |
| 978 |
|
| 979 |
} else { |
| 980 |
/* |
| 981 |
Rearrange eqns (19) and (20) to find phi_t in terms of x,y. |
| 982 |
|
| 983 |
y = Ns - k in eq(19) |
| 984 |
x - Ns - k in eq(20) |
| 985 |
|
| 986 |
(Ns - y)^2 / (Ns - x)^2 = (2 phi_t)^2 / (2 phi_t - pi)^2 |
| 987 |
|
| 988 |
Recall than y<=Ns, x<=Ns and 0<=phi_t<pi/2, so we can choose the |
| 989 |
root we want by taking square roots: |
| 990 |
|
| 991 |
(Ns - y) (pi - 2 phi_t) = 2 phi_t (Ns - x) |
| 992 |
(Ns - y) pi = 2 phi_t (Ns - x + Ns - y) |
| 993 |
phi_t = pi (Ns-y) / (2 (Ns - x) + (Ns - y)) |
| 994 |
*/ |
| 995 |
double phi_t; |
| 996 |
double vv; |
| 997 |
|
| 998 |
if (zfactor == -1.0) { |
| 999 |
swap_double(&x, &y); |
| 1000 |
x = (Nside - x); |
| 1001 |
y = (Nside - y); |
| 1002 |
} |
| 1003 |
|
| 1004 |
if (y == Nside && x == Nside) |
| 1005 |
phi_t = 0.0; |
| 1006 |
else |
| 1007 |
phi_t = pi * (Nside-y) / (2.0 * ((Nside-x) + (Nside-y))); |
| 1008 |
|
| 1009 |
if (phi_t < pi/4.) { |
| 1010 |
// z = 1.0 - mysquare(pi * (Nside - x) / ((2.0 * phi_t - pi) * Nside)) / 3.0; |
| 1011 |
vv = fabs(pi * (Nside - x) / ((2.0 * phi_t - pi) * Nside) / sqrt(3)); |
| 1012 |
} else { |
| 1013 |
// z = 1.0 - mysquare(pi * (Nside - y) / (2.0 * phi_t * Nside)) / 3.0; |
| 1014 |
vv = fabs(pi * (Nside-y) / (2. * phi_t * Nside) / sqrt(3)); |
| 1015 |
} |
| 1016 |
z = (1 - vv) * (1 + vv); |
| 1017 |
rad = sqrt(1.0 + z) * vv; |
| 1018 |
|
| 1019 |
assert(0.0 <= fabs(z) && fabs(z) <= 1.0); |
| 1020 |
z *= zfactor; |
| 1021 |
assert(0.0 <= fabs(z) && fabs(z) <= 1.0); |
| 1022 |
|
| 1023 |
// The big healpix determines the phi offset |
| 1024 |
if (issouthpolar(chp)) |
| 1025 |
phi = pi/2.0* (chp-8) + phi_t; |
| 1026 |
else |
| 1027 |
phi = pi/2.0 * chp + phi_t; |
| 1028 |
} |
| 1029 |
|
| 1030 |
if (phi < 0.0) |
| 1031 |
phi += 2*pi; |
| 1032 |
|
| 1033 |
*rx = rad * cos(phi); |
| 1034 |
*ry = rad * sin(phi); |
| 1035 |
*rz = z; |
| 1036 |
} |
| 1037 |
|
| 1038 |
void healpixl_to_xyz(int64_t ihp, int Nside, |
| 1039 |
double dx, double dy, |
| 1040 |
double* px, double *py, double *pz) { |
| 1041 |
hp_t hp; |
| 1042 |
intltohp(ihp, &hp, Nside); |
| 1043 |
hp_to_xyz(&hp, Nside, dx, dy, px, py, pz); |
| 1044 |
} |
| 1045 |
|
| 1046 |
void healpixl_to_xyzarr(int64_t ihp, int Nside, |
| 1047 |
double dx, double dy, |
| 1048 |
double* xyz) { |
| 1049 |
hp_t hp; |
| 1050 |
intltohp(ihp, &hp, Nside); |
| 1051 |
hp_to_xyz(&hp, Nside, dx, dy, xyz, xyz+1, xyz+2); |
| 1052 |
} |
| 1053 |
|
| 1054 |
void healpixl_to_radec(int64_t ihp, int Nside, double dx, double dy, |
| 1055 |
double* ra, double* dec) { |
| 1056 |
hp_t hp; |
| 1057 |
double xyz[3]; |
| 1058 |
intltohp(ihp, &hp, Nside); |
| 1059 |
hp_to_xyz(&hp, Nside, dx, dy, xyz, xyz+1, xyz+2); |
| 1060 |
xyzarr2radec(xyz, ra, dec); |
| 1061 |
} |
| 1062 |
|
| 1063 |
void healpixl_to_radecdeg(int64_t ihp, int Nside, double dx, double dy, |
| 1064 |
double* ra, double* dec) { |
| 1065 |
hp_t hp; |
| 1066 |
double xyz[3]; |
| 1067 |
intltohp(ihp, &hp, Nside); |
| 1068 |
hp_to_xyz(&hp, Nside, dx, dy, xyz, xyz+1, xyz+2); |
| 1069 |
xyzarr2radecdeg(xyz, ra, dec); |
| 1070 |
} |
| 1071 |
|
| 1072 |
void healpixl_to_radecarr(int64_t hp, int Nside, |
| 1073 |
double dx, double dy, |
| 1074 |
double* radec) { |
| 1075 |
double xyz[3]; |
| 1076 |
healpixl_to_xyzarr(hp, Nside, dx, dy, xyz); |
| 1077 |
xyzarr2radec(xyz, radec, radec+1); |
| 1078 |
} |
| 1079 |
|
| 1080 |
void healpixl_to_radecdegarr(int64_t hp, int Nside, |
| 1081 |
double dx, double dy, |
| 1082 |
double* radec) { |
| 1083 |
double xyz[3]; |
| 1084 |
healpixl_to_xyzarr(hp, Nside, dx, dy, xyz); |
| 1085 |
xyzarr2radecdeg(xyz, radec, radec+1); |
| 1086 |
} |
| 1087 |
|
| 1088 |
struct neighbour_dirn { |
| 1089 |
double x, y; |
| 1090 |
double dx, dy; |
| 1091 |
}; |
| 1092 |
|
| 1093 |
int healpix_get_neighbours_within_range_radec(double ra, double dec, double radius, |
| 1094 |
int64_t* healpixes, int Nside) { |
| 1095 |
double xyz[3]; |
| 1096 |
double r; |
| 1097 |
radecdeg2xyzarr(ra, dec, xyz); |
| 1098 |
r = deg2dist(radius); |
| 1099 |
return healpix_get_neighbours_within_range(xyz, r, healpixes, Nside); |
| 1100 |
} |
| 1101 |
|
| 1102 |
int healpix_get_neighbours_within_range(double* xyz, double range, int64_t* out_healpixes, |
| 1103 |
int Nside) { |
| 1104 |
int64_t hp; |
| 1105 |
int i,j; |
| 1106 |
double fx, fy; |
| 1107 |
int nhp = 0; |
| 1108 |
|
| 1109 |
// HACK -- temp array to avoid cleverly avoiding duplicates |
| 1110 |
int64_t healpixes[100]; |
| 1111 |
|
| 1112 |
//assert(Nside > 0); |
| 1113 |
if (Nside <= 0) { |
| 1114 |
printf("healpix_get_neighbours_within_range: Nside must be > 0.\n"); |
| 1115 |
return -1; |
| 1116 |
} |
| 1117 |
|
| 1118 |
hp = xyzarrtohealpixlf(xyz, Nside, &fx, &fy); |
| 1119 |
healpixes[nhp] = hp; |
| 1120 |
nhp++; |
| 1121 |
|
| 1122 |
{ |
| 1123 |
struct neighbour_dirn dirs[] = { |
| 1124 |
// edges |
| 1125 |
{ fx, 0, 0, -1 }, |
| 1126 |
{ fx, 1, 0, 1 }, |
| 1127 |
{ 0 , fy,-1, 0 }, |
| 1128 |
{ 1 , fy, 1, 0 }, |
| 1129 |
// bottom corner |
| 1130 |
{ 0, 0, -1, 1 }, |
| 1131 |
{ 0, 0, -1, 0 }, |
| 1132 |
{ 0, 0, -1, -1 }, |
| 1133 |
{ 0, 0, 0, -1 }, |
| 1134 |
{ 0, 0, 1, -1 }, |
| 1135 |
// right corner |
| 1136 |
{ 1, 0, 1, 1 }, |
| 1137 |
{ 1, 0, 1, 0 }, |
| 1138 |
{ 1, 0, 1, -1 }, |
| 1139 |
{ 1, 0, 0, -1 }, |
| 1140 |
{ 1, 0, -1, -1 }, |
| 1141 |
// left corner |
| 1142 |
{ 0, 1, 1, 1 }, |
| 1143 |
{ 0, 1, 0, 1 }, |
| 1144 |
{ 0, 1, -1, 1 }, |
| 1145 |
{ 0, 1, -1, 0 }, |
| 1146 |
{ 0, 1, -1, -1 }, |
| 1147 |
// top corner |
| 1148 |
{ 1, 1, -1, 1 }, |
| 1149 |
{ 1, 1, 0, 1 }, |
| 1150 |
{ 1, 1, 1, 1 }, |
| 1151 |
{ 1, 1, 1, 0 }, |
| 1152 |
{ 1, 1, 1, -1 }, |
| 1153 |
}; |
| 1154 |
int ndirs = sizeof(dirs) / sizeof(struct neighbour_dirn); |
| 1155 |
|
| 1156 |
double ptx, pty, ptdx, ptdy; |
| 1157 |
int64_t pthp; |
| 1158 |
|
| 1159 |
for (i=0; i<ndirs; i++) { |
| 1160 |
double pt[3]; |
| 1161 |
double ptstepx[3]; |
| 1162 |
double ptstepy[3]; |
| 1163 |
double across[3]; |
| 1164 |
double step = 1e-3; |
| 1165 |
double d2; |
| 1166 |
double stepdirx, stepdiry; |
| 1167 |
struct neighbour_dirn* dir = dirs+i; |
| 1168 |
ptx = dir->x; |
| 1169 |
pty = dir->y; |
| 1170 |
ptdx = dir->dx; |
| 1171 |
ptdy = dir->dy; |
| 1172 |
|
| 1173 |
// pt = point on the edge nearest to the query point. |
| 1174 |
// FIXME -- check that this is true, esp in the polar regions! |
| 1175 |
healpixl_to_xyzarr(hp, Nside, ptx, pty, pt); |
| 1176 |
d2 = distsq(pt, xyz, 3); |
| 1177 |
|
| 1178 |
// delta vector should be outside the healpix |
| 1179 |
assert((ptx+step*ptdx < 0) || |
| 1180 |
(ptx+step*ptdx > 1) || |
| 1181 |
(pty+step*ptdy < 0) || |
| 1182 |
(pty+step*ptdy > 1)); |
| 1183 |
|
| 1184 |
if (d2 > range*range) |
| 1185 |
continue; |
| 1186 |
|
| 1187 |
// compute dx and dy directions that are toward the interior of |
| 1188 |
// the healpix. |
| 1189 |
stepdirx = (ptx < step) ? 1 : -1; |
| 1190 |
stepdiry = (pty < step) ? 1 : -1; |
| 1191 |
|
| 1192 |
// take steps in those directions. |
| 1193 |
healpixl_to_xyzarr(hp, Nside, ptx + stepdirx * step, pty, ptstepx); |
| 1194 |
healpixl_to_xyzarr(hp, Nside, ptx, pty + stepdiry * step, ptstepy); |
| 1195 |
|
| 1196 |
// convert the steps into dx,dy vectors. |
| 1197 |
for (j=0; j<3; j++) { |
| 1198 |
ptstepx[j] = stepdirx * (ptstepx[j] - pt[j]); |
| 1199 |
ptstepy[j] = stepdiry * (ptstepy[j] - pt[j]); |
| 1200 |
} |
| 1201 |
|
| 1202 |
// take a small step in the specified direction. |
| 1203 |
for (j=0; j<3; j++) |
| 1204 |
across[j] = pt[j] + ptdx * ptstepx[j] + ptdy * ptstepy[j]; |
| 1205 |
|
| 1206 |
// see which healpix is at the end of the step. |
| 1207 |
normalize_3(across); |
| 1208 |
pthp = xyzarrtohealpixl(across, Nside); |
| 1209 |
|
| 1210 |
healpixes[nhp] = pthp; |
| 1211 |
nhp++; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
// Remove duplicates... |
| 1216 |
for (i=0; i<nhp; i++) { |
| 1217 |
for (j=i+1; j<nhp; j++) { |
| 1218 |
if (healpixes[i] == healpixes[j]) { |
| 1219 |
int k; |
| 1220 |
for (k=j+1; k<nhp; k++) |
| 1221 |
healpixes[k-1] = healpixes[k]; |
| 1222 |
nhp--; |
| 1223 |
i=-1; |
| 1224 |
break; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
for (i=0; i<nhp; i++) |
| 1230 |
out_healpixes[i] = healpixes[i]; |
| 1231 |
|
| 1232 |
return nhp; |
| 1233 |
} |
| 1234 |
|
| 1235 |
double healpix_distance_to_xyz(int64_t hp, int Nside, const double* xyz, |
| 1236 |
double* closestxyz) { |
| 1237 |
int64_t thehp; |
| 1238 |
// corners |
| 1239 |
double cdx[4], cdy[4]; |
| 1240 |
double cdists[4]; |
| 1241 |
int corder[4]; |
| 1242 |
int i; |
| 1243 |
double dxA,dxB,dyA,dyB; |
| 1244 |
double dxmid, dymid; |
| 1245 |
double dist2A, dist2B; |
| 1246 |
double midxyz[3]; |
| 1247 |
double dist2mid = 0.0; |
| 1248 |
|
| 1249 |
double EPS = 1e-16; |
| 1250 |
|
| 1251 |
// If the point is actually inside the healpix, dist = 0. |
| 1252 |
thehp = xyzarrtohealpixl(xyz, Nside); |
| 1253 |
if (thehp == hp) { |
| 1254 |
if (closestxyz) |
| 1255 |
memcpy(closestxyz, xyz, 3*sizeof(double)); |
| 1256 |
return 0; |
| 1257 |
} |
| 1258 |
|
| 1259 |
// Find two nearest corners. |
| 1260 |
for (i=0; i<4; i++) { |
| 1261 |
double cxyz[3]; |
| 1262 |
cdx[i] = i/2; |
| 1263 |
cdy[i] = i%2; |
| 1264 |
healpixl_to_xyzarr(hp, Nside, cdx[i], cdy[i], cxyz); |
| 1265 |
cdists[i] = distsq(xyz, cxyz, 3); |
| 1266 |
} |
| 1267 |
permutation_init(corder, 4); |
| 1268 |
permuted_sort(cdists, sizeof(double), compare_doubles_asc, corder, 4); |
| 1269 |
// now "corder" [0] and [1] are the indices of the two nearest corners. |
| 1270 |
|
| 1271 |
// Binary search for closest dx,dy. |
| 1272 |
dxA = cdx[corder[0]]; |
| 1273 |
dyA = cdy[corder[0]]; |
| 1274 |
dist2A = cdists[corder[0]]; |
| 1275 |
dxB = cdx[corder[1]]; |
| 1276 |
dyB = cdy[corder[1]]; |
| 1277 |
dist2B = cdists[corder[1]]; |
| 1278 |
// the closest two points should share an edge... unless we're in some |
| 1279 |
// weird configuration like the opposite side of the sphere. |
| 1280 |
if (!((dxA == dxB) || (dyA == dyB))) { |
| 1281 |
// weird configuration -- return the closest corner. |
| 1282 |
if (closestxyz) |
| 1283 |
healpixl_to_xyzarr(hp, Nside, cdx[corder[0]], cdy[corder[0]], closestxyz); |
| 1284 |
return distsq2deg(cdists[corder[0]]); |
| 1285 |
} |
| 1286 |
assert(dxA == dxB || dyA == dyB); |
| 1287 |
assert(dist2A <= dist2B); |
| 1288 |
|
| 1289 |
while (1) { |
| 1290 |
dxmid = (dxA + dxB) / 2.0; |
| 1291 |
dymid = (dyA + dyB) / 2.0; |
| 1292 |
// converged to EPS? |
| 1293 |
if ((dxA != dxB && (fabs(dxmid - dxA) < EPS || fabs(dxmid - dxB) < EPS)) || |
| 1294 |
(dyA != dyB && (fabs(dymid - dyA) < EPS || fabs(dymid - dyB) < EPS))) |
| 1295 |
break; |
| 1296 |
healpixl_to_xyzarr(hp, Nside, dxmid, dymid, midxyz); |
| 1297 |
dist2mid = distsq(xyz, midxyz, 3); |
| 1298 |
//printf(" dx,dy (%g,%g) %g (%g,%g) %g (%g,%g) %g\n", dxA, dyA, dist2A, dxmid, dymid, dist2mid, dxB, dyB, dist2B); |
| 1299 |
if ((dist2mid >= dist2A) && (dist2mid >= dist2B)) |
| 1300 |
break; |
| 1301 |
if (dist2A < dist2B) { |
| 1302 |
dist2B = dist2mid; |
| 1303 |
dxB = dxmid; |
| 1304 |
dyB = dymid; |
| 1305 |
} else { |
| 1306 |
dist2A = dist2mid; |
| 1307 |
dxA = dxmid; |
| 1308 |
dyA = dymid; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
// Check whether endpoint A is actually closer. |
| 1313 |
dist2A = cdists[corder[0]]; |
| 1314 |
if (dist2A < dist2mid) { |
| 1315 |
dxA = cdx[corder[0]]; |
| 1316 |
dyA = cdy[corder[0]]; |
| 1317 |
healpixl_to_xyzarr(hp, Nside, dxA, dyA, midxyz); |
| 1318 |
dist2mid = dist2A; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if (closestxyz) |
| 1322 |
memcpy(closestxyz, midxyz, 3*sizeof(double)); |
| 1323 |
return distsq2deg(dist2mid); |
| 1324 |
} |
| 1325 |
|
| 1326 |
double healpix_distance_to_radec(int64_t hp, int Nside, double ra, double dec, |
| 1327 |
double* closestradec) { |
| 1328 |
double xyz[3]; |
| 1329 |
double closestxyz[3]; |
| 1330 |
double dist; |
| 1331 |
radecdeg2xyzarr(ra, dec, xyz); |
| 1332 |
dist = healpix_distance_to_xyz(hp, Nside, xyz, closestxyz); |
| 1333 |
if (closestradec) |
| 1334 |
xyzarr2radecdegarr(closestxyz, closestradec); |
| 1335 |
return dist; |
| 1336 |
} |
| 1337 |
|
| 1338 |
int healpix_within_range_of_radec(int64_t hp, int Nside, double ra, double dec, |
| 1339 |
double radius) { |
| 1340 |
// This is the dumb trivial implementation... |
| 1341 |
return (healpix_distance_to_radec(hp, Nside, ra, dec, NULL) <= radius); |
| 1342 |
} |
| 1343 |
int healpix_within_range_of_xyz(int64_t hp, int Nside, const double* xyz, |
| 1344 |
double radius) { |
| 1345 |
return (healpix_distance_to_xyz(hp, Nside, xyz, NULL) <= radius); |
| 1346 |
} |
| 1347 |
int healpixl_within_range_of_xyz(int64_t hp, int Nside, const double* xyz, |
| 1348 |
double radius) { |
| 1349 |
return (healpix_distance_to_xyz(hp, Nside, xyz, NULL) <= radius); |
| 1350 |
} |
| 1351 |
|
| 1352 |
void healpix_radec_bounds(int64_t hp, int nside, |
| 1353 |
double* p_ralo, double* p_rahi, |
| 1354 |
double* p_declo, double* p_dechi) { |
| 1355 |
// corners! |
| 1356 |
double ralo,rahi,declo,dechi; |
| 1357 |
double ra,dec; |
| 1358 |
double dx, dy; |
| 1359 |
ralo = declo = HUGE_VAL; |
| 1360 |
rahi = dechi = -HUGE_VAL; |
| 1361 |
for (dy=0; dy<2; dy+=1.0) { |
| 1362 |
for (dx=0; dx<2; dx+=1.0) { |
| 1363 |
healpixl_to_radecdeg(hp, nside, dx, dy, &ra, &dec); |
| 1364 |
// FIXME -- wrap-around. |
| 1365 |
ralo = MIN(ra, ralo); |
| 1366 |
rahi = MAX(ra, rahi); |
| 1367 |
declo = MIN(dec, declo); |
| 1368 |
dechi = MAX(dec, dechi); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
if (p_ralo) *p_ralo = ralo; |
| 1372 |
if (p_rahi) *p_rahi = rahi; |
| 1373 |
if (p_declo) *p_declo = declo; |
| 1374 |
if (p_dechi) *p_dechi = dechi; |
| 1375 |
} |