table: tile offset - more cleanup calc.
[libastrodb.git] / src / table_get.c
blob5f7e68743949e4548f1f9291808fb6283ed8ff09
1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * Copyright (C) 2008 Liam Girdwood
19 #include <math.h>
21 #include <libastrodb/db.h>
22 #include <libastrodb/astro.h>
23 #include <libastrodb/table.h>
24 #include <libastrodb/adbstdio.h>
26 #define D2R (1.7453292519943295769e-2) /* deg->radian */
27 #define R2D (5.7295779513082320877e1) /* radian->deg */
29 /*! \fn int table_get_objects_mem(astrodb_table * table, astrodb_slist ** result)
30 * \brief Get objects from memory based on posn
32 int table_get_objects_mem(struct astrodb_table *table,
33 struct astrodb_slist **result)
35 int count = 0, ra, dec, mag, offset;
37 *result = NULL;
38 for (ra = table->clip_min_ra; ra <= table->clip_max_ra; ra++) {
40 for (dec = table->clip_min_dec;
41 dec <= table->clip_max_dec; dec++) {
43 for (mag = table->clip_bright_mag;
44 mag <= table->clip_faint_mag; mag++) {
46 offset = table_calc_deep_offset(table, ra, dec, mag);
48 if (*(table->objects + offset)) {
49 *result = astrodb_slist_prepend(*result,
50 (char *) *(table->objects + offset));
51 count += (table->status + offset)->size;
56 return count;
59 /*! \fn int table_get_objects_cache(astrodb_table * table, astrodb_slist ** result, astrodb_progress progress)
60 * \brief Get objects from memory based on posn
62 int table_get_objects_cache(struct astrodb_table *table,
63 struct astrodb_slist **result)
65 return 0;
68 /*! \fn int table_get_objects_raw(astrodb_table * table, astrodb_slist ** result, astrodb_progress progress)
69 * \brief Get objects from memory based on posn
71 int table_get_objects_raw(struct astrodb_table *table,
72 struct astrodb_slist **result)
74 return 0;
77 /*! \fn int table_get_objects_online(astrodb_table * table, astrodb_slist ** result, astrodb_progress progress)
78 * \brief Get objects from memory based on posn
80 int table_get_objects_online(struct astrodb_table *table,
81 struct astrodb_slist **result)
83 return 0;
86 static inline void get_ra_bounds(struct astrodb_table *table, int theta,
87 int *ra, int *dec)
89 double dtheta = theta * D2R;
90 *ra = ceil(table->clip_radius * cos(dtheta));
91 *dec = ceil(table->clip_radius * sin(dtheta));
94 static int append_region(struct astrodb_table *table,
95 struct astrodb_slist **result)
97 int ra, dec = table->clip_min_dec, mag, count = 0, offset;
99 for (ra = table->clip_min_ra; ra <= table->clip_max_ra; ra++) {
101 for (mag = table->clip_bright_mag;
102 mag <= table->clip_faint_mag; mag++) {
104 offset = table_calc_deep_offset(table, ra, dec, mag);
106 if (*(table->objects + offset) &&
107 (((table->status + offset)->flags & ADB_SUSED) == 0)) {
109 *result = astrodb_slist_prepend(*result,
110 (char *) *(table->objects + offset));
111 count += (table->status + offset)->size;
112 (table->status + offset)->flags |= ADB_SUSED;
116 return count;
119 static int get_region(struct astrodb_table *table,
120 struct astrodb_slist **result,
121 int min_ra, int max_ra, int dec)
123 int count = 0;
125 table->clip_min_dec = (dec + 90) / table->dec_stride_size;
127 if (min_ra >=0 && max_ra <= 360) {
128 /* both inside */
130 min_ra -= table->db->ra_min;
131 max_ra -= table->db->ra_min;
132 table->clip_min_ra = min_ra / table->ra_stride_size;
133 table->clip_max_ra = (max_ra / table->ra_stride_size) + 1;
134 count = append_region(table, result);
136 } else if (min_ra >=0 && max_ra > 360) {
137 /* outside RHS */
138 int a, b;
140 a = min_ra - table->db->ra_min;
141 b = 360 - table->db->ra_min;
142 table->clip_min_ra = a / table->ra_stride_size;
143 table->clip_max_ra = (b / table->ra_stride_size) + 1;
144 count = append_region(table, result);
146 a = table->db->ra_min - 0;
147 b = (max_ra - 360) - table->db->ra_min;
148 table->clip_min_ra = a / table->ra_stride_size;
149 table->clip_max_ra = (b / table->ra_stride_size) + 1;
150 count += append_region(table, result);
151 } else if (min_ra < 0 && max_ra <= 360) {
152 /* outside LHS */
153 int a, b;
155 a = table->db->ra_min - 0;
156 b = max_ra - table->db->ra_min;
157 table->clip_min_ra = a / table->ra_stride_size;
158 table->clip_max_ra = (b / table->ra_stride_size) + 1;
159 count = append_region(table, result);
161 a = (360 + min_ra) - table->db->ra_min;
162 b = 360 - table->db->ra_min;
163 table->clip_min_ra = a / table->ra_stride_size;
164 table->clip_max_ra = (b / table->ra_stride_size) + 1;
165 count += append_region(table, result);
166 } else {
167 /* outside both */
168 table->clip_min_ra = 0;
169 table->clip_max_ra = table->ra_stride_size;
170 count = append_region(table, result);
172 return count;
176 /*! \fn int table_get_objects_memc(astrodb_table * table, astrodb_slist ** result)
177 * \brief Get objects from memory based on posn
179 int table_get_objects_memc_old(struct astrodb_table *table,
180 struct astrodb_slist **result)
182 int count = 0, i;
183 int ra, dec, ra_min, ra_max, cdec;
185 *result = NULL;
187 for(i = 0; i <= 90; i++) {
188 get_ra_bounds(table, i, &ra, &dec);
189 cdec = table->clip_centre_dec + dec + 1;
191 /* top */
192 if (cdec < 90) {
193 ra = ra + (ra * fabs(tan(D2R * cdec)));
194 ra_max = table->clip_centre_ra + ra;
195 ra_min = table->clip_centre_ra - ra;
197 if (ra_max > 540)
198 ra_max = 540;
200 if (ra_min < -180)
201 ra_min = -180;
203 } else {
204 cdec = 180 - cdec;
205 ra = 180;
206 ra_max = 360;
207 ra_min = 0;
209 count += get_region(table, result, ra_min, ra_max, cdec);
211 /* bottom */
212 get_ra_bounds(table, i, &ra, &dec);
213 cdec = table->clip_centre_dec - dec;
215 if (cdec > -90) {
216 ra = ra + (ra * tan(D2R * fabs(cdec)));
217 ra_max = table->clip_centre_ra + ra;
218 ra_min = table->clip_centre_ra - ra;
220 if (ra_max > 540)
221 ra_max = 540;
223 if (ra_min < -180)
224 ra_min = -180;
226 } else {
227 cdec = -180 - cdec;
228 ra = 180;
229 ra_max = 360;
230 ra_min = 0;
232 count += get_region(table, result, ra_min, ra_max, cdec);
234 return count;
236 #if 0
237 static inline void unit_vector(struct render *r, int i)
239 gdouble ra = r->c[i].posn->ra * D2R;
240 gdouble dec = r->c[i].posn->dec * D2R;
242 r->c[i].x = cos(dec) * sin(ra);
243 r->c[i].y = sin(dec);
244 r->c[i].z = cos(dec) * cos(ra);
246 #endif
248 /*! \fn int table_get_objects_memc(astrodb_table * table, astrodb_slist ** result)
249 * \brief Get objects from memory based on posn
251 int table_get_objects_memc(struct astrodb_table *table,
252 struct astrodb_slist **result)
254 int count = 0, i, ra, dec, ra_min, ra_max, cdec;
256 *result = NULL;
258 for (i = 0; i <= 90; i++) {
259 get_ra_bounds(table, i, &ra, &dec);
260 cdec = table->clip_centre_dec + dec + 1;
262 /* top */
263 if (cdec < 90) {
264 ra = ra + (ra * fabs(tan(D2R * cdec)));
265 ra_max = table->clip_centre_ra + ra;
266 ra_min = table->clip_centre_ra - ra;
268 if (ra_max > 540)
269 ra_max = 540;
271 if (ra_min < -180)
272 ra_min = -180;
274 } else {
275 cdec = 180 - cdec;
276 ra = 180;
277 ra_max = 360;
278 ra_min = 0;
280 count += get_region(table, result, ra_min, ra_max, cdec);
282 /* bottom */
283 get_ra_bounds(table, i, &ra, &dec);
284 cdec = table->clip_centre_dec - dec;
286 if (cdec > -90) {
287 ra = ra + (ra * tan(D2R * fabs(cdec)));
288 ra_max = table->clip_centre_ra + ra;
289 ra_min = table->clip_centre_ra - ra;
291 if (ra_max > 540)
292 ra_max = 540;
294 if (ra_min < -180)
295 ra_min = -180;
297 } else {
298 cdec = -180 - cdec;
299 ra = 180;
300 ra_max = 360;
301 ra_min = 0;
303 count += get_region(table, result, ra_min, ra_max, cdec);
305 return count;
308 /*! \fn int table_get_objects_cachec(astrodb_table * table, astrodb_slist ** result, astrodb_progress progress)
309 * \brief Get objects from memory based on posn
311 int table_get_objects_cachec(struct astrodb_table *table,
312 struct astrodb_slist **result)
314 return 0;
317 /*! \fn int table_get_objects_rawc(astrodb_table * table, astrodb_slist ** result, astrodb_progress progress)
318 * \brief Get objects from memory based on posn
320 int table_get_objects_rawc(struct astrodb_table *table,
321 struct astrodb_slist **result)
323 return 0;
326 /*! \fn int table_get_objects_onlinec(astrodb_table * table, astrodb_slist ** result, astrodb_progress progress)
327 * \brief Get objects from memory based on posn
329 int table_get_objects_onlinec(struct astrodb_table *table,
330 struct astrodb_slist **result)
332 return 0;
336 /*! \fn int table_get_objects_cache_near(astrodb_table * table, astrodb_slist ** result)
337 * \brief Get objects from memory based on distance
339 int table_get_objects_cache_near(struct astrodb_table *table,
340 struct astrodb_slist **result)
342 int count = 0, ra;
344 *result = NULL;
345 for (ra = table->clip_min_ra; ra <= table->clip_max_ra; ra++) {
347 if (*(table->objects + ra)) {
348 *result = astrodb_slist_prepend(*result,
349 (char *) *(table->objects + ra));
350 count += (table->status + ra)->size;
353 return count;