catalogs: updated to new libastro API.
[nova.git] / src / objects / star.c
blob8b28a4b847279e1fd56b182c6a2a259c72a892ae
1 /*
2 * Copyright (C) 2008 Liam Girdwood
4 * This program 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.
9 * This program 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #define _GNU_SOURCE /* for NAN */
22 #include <errno.h>
23 #include <string.h>
24 #include <math.h>
25 #include <cairo.h>
26 #include "star.h"
28 struct star_colour_data {
29 gchar *sp;
30 gdouble r,g,b;
33 struct star_pixmap_cache {
34 cairo_t *cr[SP_NUM][RENDER_MAG_BANDS];
35 cairo_surface_t *surface[SP_NUM][RENDER_MAG_BANDS];
36 gint offset[SP_NUM][RENDER_MAG_BANDS];
37 gdouble object_size[SP_NUM][RENDER_MAG_BANDS];
41 struct star_render_size {
42 gdouble size;
43 gdouble alpha;
46 static const struct star_render_size star_sizes[RENDER_MAG_BANDS] = {
47 {1.0, 0.5},
48 {1.0, 0.8},
49 {1.0, 1.0},
50 {1.5, 1.0},
51 {2.0, 1.0},
52 {3.0, 1.0},
53 {4.0, 1.0},
54 {5.0, 1.0},
55 {7.0, 1.0},
56 {8.0, 1.0},
57 {9.0, 1.0},
58 {10.0, 1.0},
59 {11.0, 1.0},
60 {12.0, 1.0},
61 {13.0, 1.0},
62 {14.0, 1.0},
63 {15.0, 1.0},
64 {16.0, 1.0},
65 {17.0, 1.0},
66 {18.0, 1.0},
67 {19.0, 1.0},
68 {20.0, 1.0},
69 {21.0, 1.0},
70 {22.0, 1.0},
71 {23.0, 1.0},
72 {24.0, 1.0},
73 {25.0, 1.0},
74 {26.0, 1.0},
75 {27.0, 1.0},
76 {28.0, 1.0},
77 {29.0, 1.0},
78 {30.0, 1.0},
81 static struct star_pixmap_cache cache;
83 /* rgb star colours */
84 static const struct star_colour_data sp [SP_NUM] = {
85 {"O5V", 0.607843, 0.690196, 1.000000},
86 {"O6V", 0.635294, 0.721569, 1.000000},
87 {"O7V", 0.615686, 0.694118, 1.000000},
88 {"O8V", 0.615686, 0.694118, 1.000000},
89 {"O9V", 0.603922, 0.698039, 1.000000},
90 {"O9.5V", 0.643137, 0.729412, 1.000000},
91 {"B0V", 0.611765, 0.698039, 1.000000},
92 {"B0.5V", 0.654902, 0.737255, 1.000000},
93 {"B1V", 0.627451, 0.713725, 1.000000},
94 {"B2V", 0.627451, 0.705882, 1.000000},
95 {"B3V", 0.647059, 0.725490, 1.000000},
96 {"B4V", 0.643137, 0.721569, 1.000000},
97 {"B5V", 0.666667, 0.749020, 1.000000},
98 {"B6V", 0.674510, 0.741176, 1.000000},
99 {"B7V", 0.678431, 0.749020, 1.000000},
100 {"B8V", 0.694118, 0.764706, 1.000000},
101 {"B9V", 0.709804, 0.776471, 1.000000},
102 {"A0V", 0.725490, 0.788235, 1.000000},
103 {"A1V", 0.709804, 0.780392, 1.000000},
104 {"A2V", 0.733333, 0.796078, 1.000000},
105 {"A5V", 0.792157, 0.843137, 1.000000},
106 {"A6V", 0.780392, 0.831373, 1.000000},
107 {"A7V", 0.784314, 0.835294, 1.000000},
108 {"A8V", 0.835294, 0.870588, 1.000000},
109 {"A9V", 0.858824, 0.878431, 1.000000},
110 {"F0V", 0.878431, 0.898039, 1.000000},
111 {"F2V", 0.925490, 0.937255, 1.000000},
112 {"F4V", 0.878431, 0.886275, 1.000000},
113 {"F5V", 0.972549, 0.968627, 1.000000},
114 {"F6V", 0.956863, 0.945098, 1.000000},
115 {"F7V", 0.964706, 0.952941, 1.000000},
116 {"F8V", 1.000000, 0.968627, 0.988235},
117 {"F9V", 1.000000, 0.968627, 0.988235},
118 {"G0V", 1.000000, 0.972549, 0.988235},
119 {"G1V", 1.000000, 0.968627, 0.972549},
120 {"G2V", 1.000000, 0.960784, 0.949020},
121 {"G4V", 1.000000, 0.945098, 0.898039},
122 {"G5V", 1.000000, 0.956863, 0.917647},
123 {"G6V", 1.000000, 0.956863, 0.921569},
124 {"G7V", 1.000000, 0.956863, 0.921569},
125 {"G8V", 1.000000, 0.929412, 0.870588},
126 {"G9V", 1.000000, 0.937255, 0.866667},
127 {"K0V", 1.000000, 0.933333, 0.866667},
128 {"K1V", 1.000000, 0.878431, 0.737255},
129 {"K2V", 1.000000, 0.890196, 0.768627},
130 {"K3V", 1.000000, 0.870588, 0.764706},
131 {"K4V", 1.000000, 0.847059, 0.709804},
132 {"K5V", 1.000000, 0.823529, 0.631373},
133 {"K7V", 1.000000, 0.780392, 0.556863},
134 {"K8V", 1.000000, 0.819608, 0.682353},
135 {"M0V", 1.000000, 0.764706, 0.545098},
136 {"M1V", 1.000000, 0.800000, 0.556863},
137 {"M2V", 1.000000, 0.768627, 0.513725},
138 {"M3V", 1.000000, 0.807843, 0.505882},
139 {"M4V", 1.000000, 0.788235, 0.498039},
140 {"M5V", 1.000000, 0.800000, 0.435294},
141 {"M6V", 1.000000, 0.764706, 0.439216},
142 {"M8V", 1.000000, 0.776471, 0.427451},
143 {"B1IV", 0.615686, 0.705882, 1.000000},
144 {"B2IV", 0.623529, 0.701961, 1.000000},
145 {"B3IV", 0.650980, 0.737255, 1.000000},
146 {"B6IV", 0.686275, 0.760784, 1.000000},
147 {"B7IV", 0.666667, 0.741176, 1.000000},
148 {"B9IV", 0.705882, 0.772549, 1.000000},
149 {"A0IV", 0.701961, 0.772549, 1.000000},
150 {"A3IV", 0.745098, 0.803922, 1.000000},
151 {"A4IV", 0.764706, 0.823529, 1.000000},
152 {"A5IV", 0.831373, 0.862745, 1.000000},
153 {"A7IV", 0.752941, 0.811765, 1.000000},
154 {"A9IV", 0.878431, 0.890196, 1.000000},
155 {"F0IV", 0.854902, 0.878431, 1.000000},
156 {"F2IV", 0.890196, 0.901961, 1.000000},
157 {"F3IV", 0.890196, 0.901961, 1.000000},
158 {"F5IV", 0.945098, 0.937255, 1.000000},
159 {"F7IV", 0.941176, 0.937255, 1.000000},
160 {"F8IV", 1.000000, 0.988235, 0.992157},
161 {"G0IV", 1.000000, 0.972549, 0.960784},
162 {"G2IV", 1.000000, 0.956863, 0.949020},
163 {"G3IV", 1.000000, 0.933333, 0.886275},
164 {"G4IV", 1.000000, 0.960784, 0.933333},
165 {"G5IV", 1.000000, 0.921569, 0.835294},
166 {"G6IV", 1.000000, 0.949020, 0.917647},
167 {"G7IV", 1.000000, 0.905882, 0.803922},
168 {"G8IV", 1.000000, 0.913725, 0.827451},
169 {"K0IV", 1.000000, 0.882353, 0.741176},
170 {"K1IV", 1.000000, 0.847059, 0.670588},
171 {"K2IV", 1.000000, 0.898039, 0.792157},
172 {"K3IV", 1.000000, 0.858824, 0.654902},
173 {"O7III", 0.619608, 0.694118, 1.000000},
174 {"O8III", 0.615686, 0.698039, 1.000000},
175 {"O9III", 0.619608, 0.694118, 1.000000},
176 {"B0III", 0.619608, 0.694118, 1.000000},
177 {"B1III", 0.619608, 0.694118, 1.000000},
178 {"B2III", 0.623529, 0.705882, 1.000000},
179 {"B3III", 0.639216, 0.733333, 1.000000},
180 {"B5III", 0.658824, 0.741176, 1.000000},
181 {"B7III", 0.670588, 0.749020, 1.000000},
182 {"B9III", 0.698039, 0.764706, 1.000000},
183 {"A0III", 0.737255, 0.803922, 1.000000},
184 {"A3III", 0.741176, 0.796078, 1.000000},
185 {"A5III", 0.792157, 0.843137, 1.000000},
186 {"A6III", 0.819608, 0.858824, 1.000000},
187 {"A7III", 0.823529, 0.858824, 1.000000},
188 {"A8III", 0.819608, 0.858824, 1.000000},
189 {"A9III", 0.819608, 0.858824, 1.000000},
190 {"F0III", 0.835294, 0.870588, 1.000000},
191 {"F2III", 0.945098, 0.945098, 1.000000},
192 {"F4III", 0.945098, 0.941176, 1.000000},
193 {"F5III", 0.949020, 0.941176, 1.000000},
194 {"F6III", 0.945098, 0.941176, 1.000000},
195 {"F7III", 0.945098, 0.941176, 1.000000},
196 {"G0III", 1.000000, 0.949020, 0.913725},
197 {"G1III", 1.000000, 0.952941, 0.913725},
198 {"G2III", 1.000000, 0.952941, 0.913725},
199 {"G3III", 1.000000, 0.952941, 0.913725},
200 {"G4III", 1.000000, 0.952941, 0.913725},
201 {"G5III", 1.000000, 0.925490, 0.827451},
202 {"G6III", 1.000000, 0.925490, 0.843137},
203 {"G8III", 1.000000, 0.905882, 0.780392},
204 {"G9III", 1.000000, 0.905882, 0.768627},
205 {"K0III", 1.000000, 0.890196, 0.745098},
206 {"K1III", 1.000000, 0.874510, 0.709804},
207 {"K2III", 1.000000, 0.866667, 0.686275},
208 {"K3III", 1.000000, 0.847059, 0.654902},
209 {"K4III", 1.000000, 0.827451, 0.572549},
210 {"K5III", 1.000000, 0.800000, 0.541176},
211 {"K7III", 1.000000, 0.815686, 0.556863},
212 {"M0III", 1.000000, 0.796078, 0.517647},
213 {"M1III", 1.000000, 0.784314, 0.474510},
214 {"M2III", 1.000000, 0.776471, 0.462745},
215 {"M3III", 1.000000, 0.784314, 0.466667},
216 {"M4III", 1.000000, 0.807843, 0.498039},
217 {"M5III", 1.000000, 0.772549, 0.486275},
218 {"M6III", 1.000000, 0.698039, 0.474510},
219 {"M7III", 1.000000, 0.647059, 0.380392},
220 {"M8III", 1.000000, 0.654902, 0.380392},
221 {"M9III", 1.000000, 0.913725, 0.603922},
222 {"B2II", 0.647059, 0.752941, 1.000000},
223 {"B5II", 0.686275, 0.764706, 1.000000},
224 {"F0II", 0.796078, 0.850980, 1.000000},
225 {"F2II", 0.898039, 0.913725, 1.000000},
226 {"G5II", 1.000000, 0.921569, 0.796078},
227 {"M3II", 1.000000, 0.788235, 0.466667},
228 {"O9I", 0.643137, 0.725490, 1.000000},
229 {"B0I", 0.631373, 0.741176, 1.000000},
230 {"B1I", 0.658824, 0.756863, 1.000000},
231 {"B2I", 0.694118, 0.768627, 1.000000},
232 {"B3I", 0.686275, 0.760784, 1.000000},
233 {"B4I", 0.733333, 0.796078, 1.000000},
234 {"B5I", 0.701961, 0.792157, 1.000000},
235 {"B6I", 0.749020, 0.811765, 1.000000},
236 {"B7I", 0.764706, 0.819608, 1.000000},
237 {"B8I", 0.713725, 0.807843, 1.000000},
238 {"B9I", 0.800000, 0.847059, 1.000000},
239 {"A0I", 0.733333, 0.807843, 1.000000},
240 {"A1I", 0.839216, 0.874510, 1.000000},
241 {"A2I", 0.780392, 0.839216, 1.000000},
242 {"A5I", 0.874510, 0.898039, 1.000000},
243 {"F0I", 0.792157, 0.843137, 1.000000},
244 {"F2I", 0.956863, 0.952941, 1.000000},
245 {"F5I", 0.858824, 0.882353, 1.000000},
246 {"F8I", 1.000000, 0.988235, 0.968627},
247 {"G0I", 1.000000, 0.937255, 0.858824},
248 {"G2I", 1.000000, 0.925490, 0.803922},
249 {"G3I", 1.000000, 0.905882, 0.796078},
250 {"G5I", 1.000000, 0.901961, 0.717647},
251 {"G8I", 1.000000, 0.862745, 0.654902},
252 {"K0I", 1.000000, 0.866667, 0.709804},
253 {"K1I", 1.000000, 0.862745, 0.694118},
254 {"K2I", 1.000000, 0.827451, 0.529412},
255 {"K3I", 1.000000, 0.800000, 0.501961},
256 {"K4I", 1.000000, 0.788235, 0.462745},
257 {"K5I", 1.000000, 0.819608, 0.603922},
258 {"M0I", 1.000000, 0.800000, 0.560784},
259 {"M1I", 1.000000, 0.792157, 0.541176},
260 {"M2I", 1.000000, 0.756863, 0.407843},
261 {"M3I", 1.000000, 0.752941, 0.462745},
262 {"M4I", 1.000000, 0.725490, 0.407843},
263 {"N", 1.000000, 0.615686, 0.000000},
264 {"", 1.0, 1.0, 1.0}
267 static struct astrodb_schema_object star_fields[] = {
268 sky2k4v_schema
271 void star_object_render(struct render_object *robject)
273 struct sky2kv4_object *object = (struct sky2kv4_object*)robject->object;
274 guint i = object->type;
275 gint size;
277 size = 2 + ceil(
278 (robject->context.faintest_magnitude - object->object.posn_mag.Vmag)
279 * 2.5);
281 if (size >= RENDER_MAG_BANDS)
282 size = RENDER_MAG_BANDS - 1;
283 if (size < 0)
284 size = 0;
285 if (i >= SP_NUM)
286 i = SP_NUM - 1;
288 robject->coord[0].object_size = cache.object_size[i][size];
290 cairo_set_source_surface(robject->cr, cache.surface[i][size],
291 robject->coord[0].x - cache.offset[i][size],
292 robject->coord[0].y - cache.offset[i][size]);
293 cairo_paint(robject->cr);
296 static gint render_pixmap(gint spi, gint mag)
298 gint size = star_sizes[mag].size;
299 gint surface_size = size + 3;
300 gdouble radius = (gdouble)size / 2.0;
301 gdouble centre = ((gdouble)size / 2.0) + 1.5;
303 cache.surface[spi][mag] =
304 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, surface_size,
305 surface_size);
307 if (cache.surface[spi][mag] == NULL)
308 return -ENOMEM;
310 cache.cr[spi][mag] = cairo_create(cache.surface[spi][mag]);
311 if (cache.cr[spi][mag] == NULL)
312 return -ENOMEM;
314 cache.offset[spi][mag] = centre;
315 cache.object_size[spi][mag] = radius * 2.0;
317 /* render_object transparent background */
318 cairo_set_source_rgba(cache.cr[spi][mag], 0.0, 0.0, 0.0, 0.0);
319 cairo_rectangle(cache.cr[spi][mag], 0, 0, surface_size, surface_size);
320 cairo_fill (cache.cr[spi][mag]);
322 /* star shadow */
323 cairo_set_source_rgba(cache.cr[spi][mag], 0.0, 0.0, 0.0, 1.0);
324 cairo_arc (cache.cr[spi][mag], centre, centre,
325 radius * 1.1, 0, 2 * M_PI);
326 cairo_fill (cache.cr[spi][mag]);
328 /* star */
329 cairo_set_source_rgba(cache.cr[spi][mag],
330 sp[spi].r, sp[spi].g, sp[spi].b,
331 star_sizes[mag].alpha);
332 cairo_arc(cache.cr[spi][mag], centre, centre, radius, 0, 2 * M_PI);
333 cairo_fill(cache.cr[spi][mag]);
335 return 0;
338 static gint render_cache(struct astrodb_table *table)
340 gint spi, mag, ret;
342 for (spi = 0; spi < SP_NUM; spi++) {
343 for (mag = 0; mag < RENDER_MAG_BANDS; mag++) {
344 ret = render_pixmap(spi, mag);
345 if (ret < 0)
346 return ret;
349 return 0;
352 void star_object_free(struct astrodb_table *table)
354 gint spi, mag;
356 for (spi = 0; spi < SP_NUM; spi++) {
357 for (mag = 0; mag < RENDER_MAG_BANDS; mag++) {
358 cairo_destroy(cache.cr[spi][mag]);
363 gint star_object_init(struct astrodb_table *table)
365 if (astrodb_table_register_schema(table, star_fields,
366 astrodb_size(star_fields),
367 sizeof(struct sky2kv4_object)) < 0)
368 g_critical("%s: failed to register object type\n", __func__);
370 /* We want to quickly search the dataset based on object ID and HD number */
371 if (astrodb_table_hash_key(table, "ID"))
372 g_warning("%s: failed to hash on ID\n", __func__);
373 if (astrodb_table_hash_key(table, "HD"))
374 g_warning("%s: failed to hash on HD\n", __func__);
375 if (astrodb_table_hash_key(table, "SAO"))
376 g_warning("%s: failed to hash on SAO\n", __func__);
377 if (astrodb_table_hash_key(table, "PPM"))
378 g_warning("%s: failed to hash on PPM\n", __func__);
380 if (astrodb_table_open(table, 20, 10, 20) < 0)
381 g_critical("%s: table init failed\n", __func__);
383 /* create pixmap cache for fast rendering */
384 if (render_cache(table) < 0)
385 g_critical("%s: failed to render cache\n", __func__);
387 return 0;
390 static void sky2kv4_get_sp_index (struct sky2kv4_object *object, gchar *spect,
391 gint match)
393 gint m = match;
395 for (; m > 0; m--) {
396 for (object->type = 0; object->type < SP_NUM; object->type++) {
397 if (!strncmp(sp[object->type].sp, spect, m))
398 return;
403 void sky2kv4_sp_insert(void *dest, void *src)
405 struct sky2kv4_object *so =
406 (struct sky2kv4_object*)((glong)dest -
407 astrodb_offset(struct sky2kv4_object, sp));
409 strcpy(dest, src);
410 sky2kv4_get_sp_index(so, src, strlen(src));