Debian Codegraph @

cextern/astrometry.net/healpix-utils.c

raw
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 "bl.h"
7 #include "healpix.h"
8 #include "mathutil.h"
9 #include "starutil.h"
10 #include <stdio.h>
11
12 ll* healpix_region_search(int seed, ll* seeds, int Nside,
13 ll* accepted, ll* rejected,
14 int (*accept)(int hp, void* token),
15 void* token, int depth) {
16 ll* frontier;
17 anbool allocd_rej = FALSE;
18 int d;
19
20 if (!accepted)
21 accepted = ll_new(256);
22 if (!rejected) {
23 rejected = ll_new(256);
24 allocd_rej = TRUE;
25 }
26
27 if (seeds)
28 //frontier = seeds;
29 frontier = ll_dupe(seeds);
30 else {
31 frontier = ll_new(256);
32 ll_append(frontier, seed);
33 }
34
35 for (d=0; !depth || d<depth; d++) {
36 int j, N;
37 N = ll_size(frontier);
38 if (N == 0)
39 break;
40 for (j=0; j<N; j++) {
41 int64_t hp;
42 int i;
43 int64_t neigh[8];
44 hp = ll_get(frontier, j);
45 healpixl_get_neighbours(hp, neigh, Nside);
46 for (i=0; i<8; i++) {
47 if (neigh[i] < 0)
48 continue;
49 if (ll_contains(frontier, neigh[i]))
50 continue;
51 if (ll_contains(rejected, neigh[i]))
52 continue;
53 if (ll_contains(accepted, neigh[i]))
54 continue;
55 if (accept(neigh[i], token)) {
56 ll_append(accepted, neigh[i]);
57 ll_append(frontier, neigh[i]);
58 } else
59 ll_append(rejected, neigh[i]);
60 }
61 }
62 ll_remove_index_range(frontier, 0, N);
63 }
64
65 ll_free(frontier);
66 if (allocd_rej)
67 ll_free(rejected);
68 return accepted;
69 }
70
71
72 static ll* hp_rangesearch(const double* xyz, double radius, int Nside, ll* hps, anbool approx) {
73 int64_t hp;
74 double hprad = arcmin2dist(healpix_side_length_arcmin(Nside)) * sqrt(2);
75 ll* frontier = ll_new(256);
76 ll* bad = ll_new(256);
77 if (!hps)
78 hps = ll_new(256);
79
80 hp = xyzarrtohealpixl(xyz, Nside);
81 ll_append(frontier, hp);
82 ll_append(hps, hp);
83 while (ll_size(frontier)) {
84 int64_t neighbours[8];
85 int64_t i;
86 hp = ll_pop(frontier);
87 healpixl_get_neighbours(hp, neighbours, Nside);
88 for (i=0; i<8; i++) {
89 anbool tst;
90 double nxyz[3];
91 if (neighbours[i] < 0)
92 continue;
93 if (ll_contains(frontier, neighbours[i]))
94 continue;
95 if (ll_contains(bad, neighbours[i]))
96 continue;
97 if (ll_contains(hps, neighbours[i]))
98 continue;
99 if (approx) {
100 healpixl_to_xyzarr(neighbours[i], Nside, 0.5, 0.5, nxyz);
101 tst = (sqrt(distsq(xyz, nxyz, 3)) - hprad <= radius);
102 } else {
103 tst = healpixl_within_range_of_xyz(neighbours[i], Nside, xyz, radius);
104 }
105 if (tst) {
106 // in range!
107 ll_append(frontier, neighbours[i]);
108 ll_append(hps, neighbours[i]);
109 } else
110 ll_append(bad, neighbours[i]);
111 }
112 }
113
114 ll_free(bad);
115 ll_free(frontier);
116
117 return hps;
118 }
119
120 ll* healpix_rangesearch_xyz_approx(const double* xyz, double radius, int Nside, ll* hps) {
121 return hp_rangesearch(xyz, radius, Nside, hps, TRUE);
122 }
123
124 ll* healpix_rangesearch_xyz(const double* xyz, double radius, int Nside, ll* hps) {
125 return hp_rangesearch(xyz, radius, Nside, hps, FALSE);
126 }
127
128 ll* healpix_rangesearch_radec_approx(double ra, double dec, double radius, int Nside, ll* hps) {
129 double xyz[3];
130 radecdeg2xyzarr(ra, dec, xyz);
131 return hp_rangesearch(xyz, radius, Nside, hps, TRUE);
132 }
133
134 ll* healpix_rangesearch_radec(double ra, double dec, double radius, int Nside, ll* hps) {
135 double xyz[3];
136 radecdeg2xyzarr(ra, dec, xyz);
137 return hp_rangesearch(xyz, radius, Nside, hps, FALSE);
138 }
139
140 // The following is a version of the healpix_rangesearch_radec function
141 // that works with standard C types to make interfacing with Python easier.
142 int64_t healpix_rangesearch_radec_simple(double ra, double dec, double radius,
143 int Nside, int approx, int64_t **indices) {
144 double xyz[3];
145 ll* hps = ll_new(256);
146 int64_t n_pixels;
147 radecdeg2xyzarr(ra, dec, xyz);
148 hp_rangesearch(xyz, radius, Nside, hps, (anbool)approx);
149 n_pixels = ll_size(hps);
150 *indices = (int64_t *)malloc((size_t)ll_size(hps) * sizeof(int64_t));
151 if (*indices) {
152 ll_copy(hps, 0, hps->N, *indices);
153 } else {
154 fprintf(stderr, "malloc failed\n");
155 n_pixels = -1;
156 }
157 ll_free(hps);
158 return n_pixels;
159 }