Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienos.com/blob/main/om/allen-scene/native/sky_real.c # aienos.com Source of www.aienos.com, served from my own hardware Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienos.com / om / allen-scene / native / sky_real.c` 401 lines · 18726 bytes Raw Blame History 1 /* sky_real.c -- the real sky: projection, stars, Milky Way from Gaia DR3, 2 * galaxies, and the sidereal time for "the sky above you now"; see sky_real.h. 3 * 4 * Light is added the way light adds: each star's light is put on in linear 5 * light (the sRGB curve undone, the light added, the curve put back), so a 6 * faint star on the Milky Way brightens it by as much as it would in the 7 * sky. 8 * 9 * The Milky Way grid (sky_data.c) is a full-sphere equirectangular map from 10 * Gaia DR3 source counts and integrated G-band flux, reddened by measured 11 * extinction; rendering inverse-projects each screen point to (RA, Dec) and 12 * bilinear-samples the grid. */ 13 #include "sky_real.h" 14 15 /* ---- projection ---- */ 16 17 int ck_sky_project(const ck_sky_view *v, const int16_t d[3], int32_t w, int32_t cx, int32_t cy, int32_t *x16, 18 int32_t *y16) 19 { 20 int64_t a[3]; 21 for (int r = 0; r < 3; r++) 22 a[r] = (int64_t)v->rot_q30[r][0] * d[0] + (int64_t)v->rot_q30[r][1] * d[1] + (int64_t)v->rot_q30[r][2] * d[2]; 23 if (a[2] < (1ll << 40)) /* Q45: closer than 1/32 to edge-on, about 88 degrees from the centre */ 24 return 0; 25 int64_t z = a[2] >> 16; /* Q29 */ 26 int64_t u = a[0] * 16384 / z, t = a[1] * 16384 / z; /* tangent-plane x and y, Q30 */ 27 int64_t s = (int64_t)w * 8; /* 16ths of a pixel per half width */ 28 *x16 = cx * 16 + (int32_t)(u * s / v->tan_half_q30); 29 *y16 = cy * 16 - (int32_t)(t * s / v->tan_half_q30); 30 return 1; 31 } 32 33 /* ---- Milky Way: equirectangular full-sphere grid, bilinear sampling ---- */ 34 35 static int32_t bilinear_u8(const uint8_t *p, int32_t cols, int32_t rows, int64_t c, int64_t r) /* x 256 */ 36 { 37 c = c < 0 ? 0 : c > (int64_t)(cols - 1) << 16 ? (int64_t)(cols - 1) << 16 : c; 38 r = r < 0 ? 0 : r > (int64_t)(rows - 1) << 16 ? (int64_t)(rows - 1) << 16 : r; 39 int32_t i = (int32_t)(c >> 16), j = (int32_t)(r >> 16), i1 = ck_min(i + 1, cols - 1), j1 = ck_min(j + 1, rows - 1); 40 int32_t fx = (int32_t)(c >> 8 & 255), fy = (int32_t)(r >> 8 & 255); 41 int32_t a = p[j * cols + i] * (256 - fx) + p[j * cols + i1] * fx; 42 int32_t b = p[j1 * cols + i] * (256 - fx) + p[j1 * cols + i1] * fx; 43 return (a * (256 - fy) + b * fy) >> 8; 44 } 45 46 static int32_t bilinear_s8(const int8_t (*p)[2], int k, int32_t cols, int32_t rows, int64_t c, int64_t r) /* x 256 */ 47 { 48 c = c < 0 ? 0 : c > (int64_t)(cols - 1) << 16 ? (int64_t)(cols - 1) << 16 : c; 49 r = r < 0 ? 0 : r > (int64_t)(rows - 1) << 16 ? (int64_t)(rows - 1) << 16 : r; 50 int32_t i = (int32_t)(c >> 16), j = (int32_t)(r >> 16), i1 = ck_min(i + 1, cols - 1), j1 = ck_min(j + 1, rows - 1); 51 int32_t fx = (int32_t)(c >> 8 & 255), fy = (int32_t)(r >> 8 & 255); 52 int32_t a = p[j * cols + i][k] * (256 - fx) + p[j * cols + i1][k] * fx; 53 int32_t b = p[j1 * cols + i][k] * (256 - fx) + p[j1 * cols + i1][k] * fx; 54 return (a * (256 - fy) + b * fy) >> 8; 55 } 56 57 /* Inverse-project a half-pixel screen point to equirectangular grid coords. 58 * (x2, y2) in half-pixels; screen centre at (2*cx, 2*cy). 59 * Returns (col_q16, row_q16) for bilinear_u8/s8. The map covers the full 60 * sphere: col 0 = RA 0 deg, col cols-1 = RA ~360 deg; row 0 = Dec +90, 61 * row rows-1 = Dec -90. col wraps; row is clamped. 62 * Returns 0 if the point is behind the camera. */ 63 static int grid_at_eq(const ck_sky_view *v, int32_t x2, int32_t y2, int32_t w, int32_t cx, int32_t cy, 64 int64_t *col, int64_t *row) 65 { 66 const ck_sky_map *m = &ck_sky_milkyway; 67 68 /* Tangent-plane offset from the view centre, Q30. The forward projection (ck_sky_project) puts a 69 * tangent-plane offset u at (u / tan_half) half-widths, i.e. (u / tan_half) * w / 2 pixels, so the 70 * inverse is u = px * 2 * tan_half / w with px = half-pixels / 2: u = half_px * tan_half / w. */ 71 int64_t u = (int64_t)(x2 - 2 * cx) * (int64_t)v->tan_half_q30 / (int64_t)w; 72 int64_t vv = (int64_t)(2 * cy - y2) * (int64_t)v->tan_half_q30 / (int64_t)w; 73 74 /* Unnormalised sky direction in J2000 frame, ~Q30 */ 75 int64_t dx = (int64_t)v->rot_q30[2][0] + ((int64_t)v->rot_q30[0][0] * u >> 30) + ((int64_t)v->rot_q30[1][0] * vv >> 30); 76 int64_t dy = (int64_t)v->rot_q30[2][1] + ((int64_t)v->rot_q30[0][1] * u >> 30) + ((int64_t)v->rot_q30[1][1] * vv >> 30); 77 int64_t dz = (int64_t)v->rot_q30[2][2] + ((int64_t)v->rot_q30[0][2] * u >> 30) + ((int64_t)v->rot_q30[1][2] * vv >> 30); 78 79 /* Reject behind-camera directions (dz Q30 < 0 is not enough; check full forward component) */ 80 if ((int64_t)v->rot_q30[2][0] * dx + (int64_t)v->rot_q30[2][1] * dy + (int64_t)v->rot_q30[2][2] * dz < (1ll << 40)) 81 return 0; 82 83 /* RA: atan2(dy, dx) as turns [0,65536) */ 84 uint32_t ra_turn = ck_atan2((int32_t)(dy >> 8), (int32_t)(dx >> 8)); 85 /* col_q16: ra_turn * cols (since ra_turn / 65536 = RA fraction, col = fraction * cols * 65536 = ra_turn * cols) */ 86 *col = (int64_t)ra_turn * m->cols; 87 88 /* row: equirectangular Dec spacing (1 deg/row); use ck_atan2 for correct 89 * linear-in-Dec rows. Dec turn: ck_atan2(dz, h_flat) where h_flat is the 90 * horizontal magnitude. Dec=+90 → turn 16384, Dec=-90 → turn 49152. */ 91 int32_t dx15 = (int32_t)(dx >> 15), dy15 = (int32_t)(dy >> 15), dz15 = (int32_t)(dz >> 15); 92 uint32_t h15 = ck_isqrt((uint64_t)((int64_t)dx15 * dx15 + (int64_t)dy15 * dy15)); 93 int32_t dec_turn = (int32_t)ck_atan2(dz15, (int32_t)h15); /* turns [0,65536) */ 94 /* signed: > 32768 means south of equator */ 95 int32_t dec_turn_s = dec_turn > 32768 ? dec_turn - 65536 : dec_turn; 96 /* row 0 = Dec +90 (dec_turn_s=16384), row rows = Dec -90 (dec_turn_s=-16384) 97 * row_q16 = (16384 - dec_turn_s) * rows * 2 */ 98 *row = (int64_t)(16384 - dec_turn_s) * (int64_t)m->rows * 2; 99 return 1; 100 } 101 102 /* The equirectangular Milky Way at half-pixel (x2, y2) through view v: 103 * three channels in 16ths of a level */ 104 static void milkyway_at(const ck_sky_view *v, int32_t x2, int32_t y2, int32_t w, int32_t cx, int32_t cy, 105 int32_t rgb[3]) 106 { 107 const ck_sky_map *m = &ck_sky_milkyway; 108 int64_t col, row; 109 if (!grid_at_eq(v, x2, y2, w, cx, cy, &col, &row)) { 110 rgb[0] = rgb[1] = rgb[2] = 0; 111 return; 112 } 113 /* Wrap col in RA */ 114 int64_t maxcol = (int64_t)m->cols * 65536; 115 while (col < 0) col += maxcol; 116 while (col >= maxcol) col -= maxcol; 117 /* Colour grid is at half resolution */ 118 int32_t hc = (m->cols + 1) / 2, hr = (m->rows + 1) / 2; 119 int32_t y = bilinear_u8(ck_sky_luma, m->cols, m->rows, col, row) >> 4; /* 16ths */ 120 int64_t cc = (col - 32768) / 2, cr = (row - 32768) / 2; /* colour: half spacing */ 121 if (cc < 0) cc += (int64_t)hc * 65536; 122 int32_t co = bilinear_s8(ck_sky_chroma, 0, hc, hr, cc, cr) >> 5; /* 16ths */ 123 int32_t cg = bilinear_s8(ck_sky_chroma, 1, hc, hr, cc, cr) >> 5; 124 rgb[0] = ck_clamp(y + co - cg, 0, 4080); 125 rgb[1] = ck_clamp(y + cg, 0, 4080); 126 rgb[2] = ck_clamp(y - co - cg, 0, 4080); 127 } 128 129 void ck_sky_milkyway_q(uint16_t *q, int32_t qw, int32_t qh, int32_t w, int32_t cx, int32_t cy, 130 const ck_sky_view *v) 131 { 132 for (int32_t j = 0; j < qh; j++) 133 for (int32_t i = 0; i < qw; i++) { 134 int32_t rgb[3]; 135 milkyway_at(v, 8 * i + 1, 8 * j + 1, w, cx, cy, rgb); 136 uint16_t *c = q + ((size_t)j * (size_t)qw + (size_t)i) * 3; 137 c[0] = (uint16_t)rgb[0], c[1] = (uint16_t)rgb[1], c[2] = (uint16_t)rgb[2]; 138 } 139 } 140 141 /* ---- stars ---- */ 142 143 /* adds light round (x16, y16): radius r16, centre light peak (linear, Q16), colour col (linear, 4096 the 144 * brightest channel); the light falls off as (1 - d^2 / r^2)^3 */ 145 static void light_add(ck_canvas *cv, int32_t x16, int32_t y16, int32_t r16, uint64_t peak, const uint16_t col[3]) 146 { 147 if (r16 < 4 || peak < 4) 148 return; 149 int32_t x0 = ck_max(0, (x16 - r16) >> 4), x1 = ck_min(cv->w - 1, (x16 + r16) >> 4); 150 int32_t y0 = ck_max(0, (y16 - r16) >> 4), y1 = ck_min(cv->h - 1, (y16 + r16) >> 4); 151 int64_t r2 = (int64_t)r16 * r16; 152 uint64_t inv = (1ull << 40) / (uint64_t)r2; 153 uint64_t pc[3]; 154 for (int k = 0; k < 3; k++) 155 pc[k] = peak * col[k] >> 13; /* Q15 linear at the centre, per channel */ 156 for (int32_t y = y0; y <= y1; y++) { 157 int64_t dy = (int64_t)y * 16 + 8 - y16; 158 uint32_t *row = cv->px + (size_t)y * (size_t)cv->w; 159 for (int32_t x = x0; x <= x1; x++) { 160 int64_t dx = (int64_t)x * 16 + 8 - x16, d2 = dx * dx + dy * dy; 161 if (d2 >= r2) 162 continue; 163 uint64_t f = 65536u - ((uint64_t)d2 * inv >> 24); /* Q16 */ 164 uint64_t f3 = ((f * f >> 16) * f) >> 16; /* Q16 */ 165 uint32_t c = row[x], out = 0; 166 int any = 0; 167 for (int k = 0; k < 3; k++) { 168 uint32_t lv = c >> (16 - 8 * k) & 255u; 169 uint64_t add = pc[k] * f3 >> 16; 170 if (add) { 171 uint64_t l = ck_sky_lin[lv] + add; 172 lv = (ck_sky_srgb16[l >= 32767 ? 4095 : l >> 3] + 8) >> 4; 173 lv = lv > 255 ? 255 : lv; 174 any = 1; 175 } 176 out = out << 8 | lv; 177 } 178 if (any) 179 row[x] = (c & 0xff000000u) | out; 180 } 181 } 182 } 183 184 static uint32_t srgb_of(const uint16_t col[3]) 185 { 186 uint32_t c = 0; 187 for (int k = 0; k < 3; k++) { 188 uint32_t i = col[k] >= 4096 ? 4095 : col[k]; 189 c = c << 8 | ((ck_sky_srgb16[i] + 8) >> 4); 190 } 191 return c; 192 } 193 194 void ck_sky_draw_stars(ck_canvas *cv, uint32_t k, int32_t cx, int32_t cy, const ck_sky_view *v, 195 ck_night_star *tw, unsigned max, unsigned *ntw) 196 { 197 int32_t r0 = (int32_t)(k * 3 / 40); /* 16ths of a pixel: 1.2 design px */ 198 if (r0 < 12) 199 r0 = 12; 200 /* the core grows with the scale and its middle keeps its light, so a star's total light grows as the 201 * square of the scale and it looks the same on any screen */ 202 *ntw = 0; 203 for (unsigned i = 0; i < ck_sky_nstars; i++) { 204 const ck_sky_star *s = &ck_sky_stars[i]; 205 int16_t d[3] = {s->x, s->y, s->z}; 206 int32_t x16, y16; 207 if (!ck_sky_project(v, d, cv->w, cx, cy, &x16, &y16)) 208 continue; 209 int32_t reach = r0 * 12; 210 if (x16 < -reach || y16 < -reach || x16 > cv->w * 16 + reach || y16 > cv->h * 16 + reach) 211 continue; 212 const uint16_t *col = ck_sky_palette[s->bv < 128 ? s->bv : 127]; 213 uint64_t core = ck_sky_flux[s->mag]; /* the light at the middle of the core */ 214 light_add(cv, x16, y16, r0, core, col); 215 light_add(cv, x16, y16, r0 * 4, (core * 15) >> 7, col); 216 light_add(cv, x16, y16, r0 * 12, (core * 5) >> 7, col); 217 if (s->mag <= 59 && *ntw < max) { /* V <= 1.0: bright enough to twinkle */ 218 tw[*ntw].x = x16, tw[*ntw].y = y16, tw[*ntw].r = r0 * 4; 219 tw[*ntw].color = srgb_of(col); 220 tw[*ntw].period = (uint16_t)(1200 + (i * 137) % 2400); 221 tw[*ntw].phase = (uint16_t)((i * 919) % 65536); 222 (*ntw)++; 223 } 224 } 225 } 226 227 /* ---- galaxies ---- */ 228 229 ck_rect ck_sky_galaxy_bound(uint32_t w, unsigned i) 230 { 231 if (i >= ck_sky_ngalaxies) 232 return (ck_rect){0, 0, 0, 0}; 233 const ck_sky_galaxy *g = &ck_sky_galaxies[i]; 234 /* the bound uses the default view; galaxies only draw near their canonical position */ 235 int32_t a = (int32_t)((int64_t)g->a_q16 * (int32_t)w >> 16) + 4; 236 return (ck_rect){0, 0, a * 2 + 1, a * 2 + 1}; 237 } 238 239 int ck_sky_galaxy_in_view(uint32_t w, uint32_t h, int32_t cx, int32_t cy, const ck_sky_view *v, unsigned i, 240 int32_t *gx, int32_t *gy) 241 { 242 if (i >= ck_sky_ngalaxies) 243 return 0; 244 int32_t x16, y16; 245 if (!ck_sky_project(v, ck_sky_galaxies[i].dir, (int32_t)w, cx, cy, &x16, &y16)) 246 return 0; 247 *gx = (x16 + 8) >> 4; 248 *gy = (y16 + 8) >> 4; 249 return *gx >= 0 && *gx < (int32_t)w && *gy >= 0 && *gy < (int32_t)h; 250 } 251 252 ck_rect ck_sky_galaxy_box(uint32_t w, uint32_t h, int32_t cx, int32_t cy, const ck_sky_view *v, unsigned i) 253 { 254 int32_t gx, gy; 255 if (!ck_sky_galaxy_in_view(w, h, cx, cy, v, i, &gx, &gy)) 256 return (ck_rect){0, 0, 0, 0}; 257 const ck_sky_galaxy *g = &ck_sky_galaxies[i]; 258 int32_t a = (int32_t)((int64_t)g->a_q16 * (int32_t)w >> 16) + 4; 259 int32_t x0 = ck_max(0, gx - a), y0 = ck_max(0, gy - a); 260 int32_t x1 = ck_min((int32_t)w - 1, gx + a), y1 = ck_min((int32_t)h - 1, gy + a); 261 if (x0 >= x1 || y0 >= y1) 262 return (ck_rect){0, 0, 0, 0}; 263 return (ck_rect){x0, y0, x1 - x0, y1 - y0}; 264 } 265 266 void ck_sky_galaxy_maps(ck_night_galaxy *g, unsigned i, uint32_t w, int32_t cx, int32_t cy, 267 const ck_sky_view *v) 268 { 269 if (i >= ck_sky_ngalaxies || g->box.w == 0) 270 return; 271 int32_t gx, gy; 272 if (!ck_sky_galaxy_in_view(w, 0x7fffffff, cx, cy, v, i, &gx, &gy)) 273 return; 274 const ck_sky_galaxy *gal = &ck_sky_galaxies[i]; 275 int32_t a = (int32_t)((int64_t)gal->a_q16 * (int32_t)w >> 16); 276 int32_t b = (int32_t)((int64_t)gal->b_q16 * (int32_t)w >> 16) + 1; 277 int32_t ca = (int32_t)ck_cos(gal->angle), sa = (int32_t)ck_sin(gal->angle); 278 g->tint = gal->tint; 279 g->period = (uint16_t)(2400 + i * 400); 280 for (int32_t py = 0; py < g->box.h; py++) 281 for (int32_t px = 0; px < g->box.w; px++) { 282 int32_t dx = (g->box.x + px) - gx, dy = (g->box.y + py) - gy; 283 int32_t u = (dx * ca + dy * sa) >> 14; /* along the long axis */ 284 int32_t vv = (-dx * sa + dy * ca) >> 14; /* along the short */ 285 size_t off = (size_t)py * (size_t)g->box.w + (size_t)px; 286 if (a == 0 || b == 0 || (int64_t)u * u * b * b + (int64_t)vv * vv * a * a > (int64_t)a * a * b * b) { 287 g->light[off] = 0; 288 g->phase[off] = 128; 289 continue; 290 } 291 /* light falls off as (1 - (u/a)^2 - (v/b)^2)^2 */ 292 int64_t f = 65536 - (int64_t)u * u * 65536 / (a * a) - (int64_t)vv * vv * 65536 / (b * b); 293 if (f < 0) f = 0; 294 g->light[off] = (uint8_t)((f * f >> 16) * 255 >> 16); 295 g->phase[off] = (uint8_t)((u + a) * 255 / (2 * a + 1)); 296 } 297 } 298 299 /* ---- the sky above you now: sidereal time, the zenith, precession ---- */ 300 301 #define J2000_UNIX 946728000ll /* 2000 January 1, 12:00 UT */ 302 303 uint32_t ck_sky_gmst(int64_t t) 304 { 305 /* GMST = 18.`697374558` h + 24.`06570982441908` h x D, D days from J2000 (USNO; Aoki et al. 1982), 306 * here in turns x 2^48: 0.`7790572732` + 1.`0027379093508` D */ 307 int64_t s = t - J2000_UNIX, days = s / 86400, rem = s % 86400; 308 if (rem < 0) 309 rem += 86400, days -= 1; 310 uint64_t turns = 219285127830237ull + (uint64_t)days * 770652970752ull + (uint64_t)rem * 3266731825ull; 311 return (uint32_t)(turns >> 16); 312 } 313 314 /* (cos, sin) of a small angle a (radians, Q30), Q30 */ 315 static void small_cs(int64_t a, int64_t *c, int64_t *s) 316 { 317 int64_t a2 = a * a >> 30; 318 *c = (1ll << 30) - a2 / 2 + (a2 * a2 >> 30) / 24; 319 *s = a - (a2 * a >> 30) / 6; 320 } 321 322 static void rot_z(int64_t v[3], int64_t a) /* frame turned by a about z */ 323 { 324 int64_t c, s; 325 small_cs(a, &c, &s); 326 int64_t x = (v[0] * c + v[1] * s) >> 30, y = (v[1] * c - v[0] * s) >> 30; 327 v[0] = x, v[1] = y; 328 } 329 330 static void rot_y(int64_t v[3], int64_t a) /* frame turned by a about y */ 331 { 332 int64_t c, s; 333 small_cs(a, &c, &s); 334 int64_t x = (v[0] * c - v[2] * s) >> 30, z = (v[0] * s + v[2] * c) >> 30; 335 v[0] = x, v[2] = z; 336 } 337 338 /* sin and cos of a in millionths of a degree, Q30, from the Q14 table refined by one Newton-like step */ 339 static void cs_udeg(int64_t a_udeg, int64_t *c, int64_t *s) 340 { 341 a_udeg %= 360000000ll; 342 if (a_udeg < 0) 343 a_udeg += 360000000ll; 344 uint32_t turn = (uint32_t)(a_udeg * 65536 / 360000000ll); 345 /* the remainder past the table's step, radians Q30: (a - turn) x 2 pi / 360e6 */ 346 int64_t rest = a_udeg - (int64_t)turn * 360000000ll / 65536; 347 int64_t e = rest * 18740 / 1000; /* 2 pi / 360e6 x 2^30 = 18.740 */ 348 int64_t c0 = (int64_t)ck_cos(turn) * 65536, s0 = (int64_t)ck_sin(turn) * 65536; 349 *c = c0 - (s0 * e >> 30); 350 *s = s0 + (c0 * e >> 30); 351 } 352 353 void ck_sky_zenith(int64_t t, int32_t lat_udeg, int32_t lon_udeg, int16_t d[3], int32_t *ra_udeg, 354 int32_t *dec_udeg) 355 { 356 /* straight up: right ascension the local sidereal time, declination the latitude (of the date) */ 357 int64_t lst = (int64_t)((uint64_t)ck_sky_gmst(t) * 360000000ull >> 32) + lon_udeg; 358 int64_t ca, sa, cd, sd; 359 cs_udeg(lst, &ca, &sa); 360 cs_udeg(lat_udeg, &cd, &sd); 361 int64_t v[3] = {cd * ca >> 30, cd * sa >> 30, sd}; 362 /* back from the equator of the date to J2000 (Lieske et al. 1977, IAU 1976): 363 * v_J2000 = R3(zeta) R2(-theta) R3(z) v_date; the angles in radians Q40 per century */ 364 int64_t T = (t - J2000_UNIX) * (1ll << 30) / (36525ll * 86400); /* centuries, Q30 */ 365 int64_t T2 = T * T >> 30; 366 int64_t zeta = (12293486530ll * T + 1609196ll * T2) >> 40; /* 2306.2181" T + 0.30188" T^2, Q30 rad */ 367 int64_t zz = (12293486530ll * T + 5835282ll * T2) >> 40; /* 2306.2181" T + 1.09468" T^2 */ 368 int64_t th = (10684145203ll * T - 2274293ll * T2) >> 40; /* 2004.3109" T - 0.42665" T^2 */ 369 rot_z(v, zz); 370 rot_y(v, -th); 371 rot_z(v, zeta); 372 for (int k = 0; k < 3; k++) 373 d[k] = (int16_t)ck_clamp((int32_t)(v[k] >> 15), -32767, 32767); 374 if (ra_udeg) { 375 uint32_t ra = ck_atan2((int32_t)(v[1] >> 8), (int32_t)(v[0] >> 8)); 376 *ra_udeg = (int32_t)((int64_t)ra * 360000000ll / 65536); 377 } 378 if (dec_udeg) { 379 int64_t h = (int64_t)ck_isqrt((uint64_t)((v[0] >> 8) * (v[0] >> 8) + (v[1] >> 8) * (v[1] >> 8))); 380 uint32_t de = ck_atan2((int32_t)(v[2] >> 8), (int32_t)h); 381 *dec_udeg = (int32_t)((int64_t)(int16_t)de * 360000000ll / 65536); 382 } 383 } 384 385 void ck_sky_view_at(ck_sky_view *v, int32_t ra_udeg, int32_t dec_udeg, int32_t fov_mdeg, int32_t roll_mdeg) 386 { 387 int64_t ca, sa, cd, sd, cr, sr, ch, sh; 388 cs_udeg(ra_udeg, &ca, &sa); 389 cs_udeg(dec_udeg, &cd, &sd); 390 cs_udeg((int64_t)roll_mdeg * 1000, &cr, &sr); 391 cs_udeg((int64_t)fov_mdeg * 500, &ch, &sh); 392 int64_t west[3] = {sa, -ca, 0}, north[3] = {-(sd * ca >> 30), -(sd * sa >> 30), cd}; 393 int64_t fwd[3] = {cd * ca >> 30, cd * sa >> 30, sd}; 394 for (int k = 0; k < 3; k++) { 395 v->rot_q30[0][k] = (int32_t)((cr * west[k] + sr * north[k]) >> 30); 396 v->rot_q30[1][k] = (int32_t)((cr * north[k] - sr * west[k]) >> 30); 397 v->rot_q30[2][k] = (int32_t)fwd[k]; 398 } 399 v->tan_half_q30 = (int32_t)((sh << 30) / ch); 400 v->ra_mdeg = ra_udeg / 1000, v->dec_mdeg = dec_udeg / 1000, v->fov_mdeg = fov_mdeg, v->roll_mdeg = roll_mdeg; 401 }