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/aienux/blob/main/native/kernel/ui/tests/sky_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. 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 `aienux / native / kernel / ui / tests / sky_test.c` 347 lines ยท 17015 bytes Raw Blame History 1 /* sky_test.c -- host test of the real sky (ui/sky_real.c, ui/sky_data.c): 2 * 1. the kernel's integer projection puts the catalogue's stars where the 3 * generator (floating point) put them, within a pixel at 3840 x 2160; 4 * 2. a view built with integer trigonometry matches the generated one; 5 * 3. sidereal time matches published values (Meeus, Astronomical 6 * Algorithms, 2nd ed., examples 12.a and 12.b; USNO's value at J2000); 7 * 4. the zenith, taken back to J2000 by the kernel, goes forward again 8 * (Meeus's rigorous precession, chapter 21, in double precision) to 9 * the local sidereal time and the latitude; 10 * 5. the sky is made at 3840 x 2160 (timed). The renderer (sky_real.c) 11 * draws the diffuse Milky Way, in which nothing is black and the 12 * Magellanic Clouds glow at their places; the sky behind Allen 13 * (night.c, since `32c31dbf`) is that renderer's catalogue stars on black, 14 * with no diffuse light. Each contract is checked on its own frame, and 15 * pictures are written for a person to look at: the whole sky after the 16 * eyes have adjusted, and pieces at full size. 17 * Usage: sky_test OUTDIR. Ends "SKY_TEST: PASS" or "SKY_TEST: FAIL". */ 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 25 #include "../night.h" 26 #include "../sky_real.h" 27 28 static int fails, checks; 29 #define CHECK(c, ...) \ 30 do { \ 31 checks++; \ 32 if (!(c)) { \ 33 fails++; \ 34 printf("FAIL "); \ 35 } else \ 36 printf("ok "); \ 37 printf(__VA_ARGS__); \ 38 printf("\n"); \ 39 } while (0) 40 41 /* ---- a PNG writer (stored deflate) ---- */ 42 static uint32_t crc_t[256]; 43 static uint32_t crc(uint32_t c, const uint8_t *p, size_t n) 44 { 45 if (!crc_t[1]) 46 for (uint32_t i = 0; i < 256; i++) { 47 uint32_t v = i; 48 for (int k = 0; k < 8; k++) 49 v = v & 1 ? 0xedb88320u ^ (v >> 1) : v >> 1; 50 crc_t[i] = v; 51 } 52 c = ~c; 53 while (n--) 54 c = crc_t[(c ^ *p++) & 255] ^ (c >> 8); 55 return ~c; 56 } 57 static void be32(uint8_t *p, uint32_t v) { p[0] = v >> 24, p[1] = v >> 16, p[2] = v >> 8, p[3] = v; } 58 static void chunk(FILE *f, const char *t, const uint8_t *d, uint32_t n) 59 { 60 uint8_t h[8]; 61 be32(h, n), memcpy(h + 4, t, 4); 62 fwrite(h, 1, 8, f), fwrite(d, 1, n, f); 63 uint32_t c = crc(crc(0, (const uint8_t *)t, 4), d, n); 64 be32(h, c), fwrite(h, 1, 4, f); 65 } 66 /* writes the w x h piece at (x0, y0) of a pitch-wide XRGB buffer, shrunk by s (box average) */ 67 static void png(const char *path, const uint32_t *px, int pitch, int x0, int y0, int w, int h, int s) 68 { 69 int ow = w / s, oh = h / s; 70 size_t raw_n = (size_t)oh * (ow * 3 + 1); 71 uint8_t *raw = malloc(raw_n); 72 for (int y = 0; y < oh; y++) { 73 uint8_t *r = raw + (size_t)y * (ow * 3 + 1); 74 r[0] = 0; 75 for (int x = 0; x < ow; x++) 76 for (int k = 0; k < 3; k++) { 77 unsigned acc = 0; 78 for (int j = 0; j < s; j++) 79 for (int i = 0; i < s; i++) 80 acc += px[((size_t)y0 + (size_t)y * s + j) * pitch + x0 + (size_t)x * s + i] >> (16 - 8 * k) & 255; 81 r[1 + x * 3 + k] = (uint8_t)(acc / (s * s)); 82 } 83 } 84 size_t nb = (raw_n + 65534) / 65535, zn = 2 + raw_n + nb * 5 + 4; 85 uint8_t *z = malloc(zn), *q = z; 86 *q++ = 0x78, *q++ = 1; 87 uint32_t a = 1, b = 0; 88 for (size_t i = 0; i < raw_n; i++) 89 a = (a + raw[i]) % 65521, b = (b + a) % 65521; 90 for (size_t o = 0; o < raw_n; o += 65535) { 91 size_t n = raw_n - o < 65535 ? raw_n - o : 65535; 92 *q++ = o + n == raw_n; 93 *q++ = n & 255, *q++ = n >> 8, *q++ = ~n & 255, *q++ = (~n >> 8) & 255; 94 memcpy(q, raw + o, n), q += n; 95 } 96 be32(q, b << 16 | a), q += 4; 97 int f_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); 98 FILE *f = f_fd < 0 ? NULL : fdopen(f_fd, "wb"); 99 if (!f) { 100 printf("note: cannot write %s\n", path); 101 free(raw), free(z); 102 return; 103 } 104 fwrite("\x89PNG\r\n\x1a\n", 1, 8, f); 105 uint8_t ih[13]; 106 be32(ih, ow), be32(ih + 4, oh), ih[8] = 8, ih[9] = 2, ih[10] = ih[11] = ih[12] = 0; 107 chunk(f, "IHDR", ih, 13); 108 chunk(f, "IDAT", z, (uint32_t)(q - z)); 109 chunk(f, "IEND", NULL, 0); 110 fclose(f); 111 printf("note: wrote %s (%dx%d)\n", path, ow, oh); 112 free(raw), free(z); 113 } 114 115 static double now_ms(void) 116 { 117 struct timespec t; 118 clock_gettime(CLOCK_MONOTONIC, &t); 119 return t.tv_sec * 1e3 + t.tv_nsec / 1e6; 120 } 121 122 static int cmp_double(const void *a, const void *b) 123 { 124 double x = *(const double *)a, y = *(const double *)b; 125 return x < y ? -1 : x > y; 126 } 127 128 static const double D2R = M_PI / 180.0; 129 130 /* Meeus 21.4: J2000 -> mean equator and equinox of the date */ 131 static void precess_forward(double ra0, double dec0, double T, double *ra, double *dec) 132 { 133 double as = 1.0 / 3600 * D2R; 134 double zeta = (2306.2181 * T + 0.30188 * T * T + 0.017998 * T * T * T) * as; 135 double z = (2306.2181 * T + 1.09468 * T * T + 0.018203 * T * T * T) * as; 136 double th = (2004.3109 * T - 0.42665 * T * T - 0.041833 * T * T * T) * as; 137 double A = cos(dec0) * sin(ra0 + zeta); 138 double B = cos(th) * cos(dec0) * cos(ra0 + zeta) - sin(th) * sin(dec0); 139 double C = sin(th) * cos(dec0) * cos(ra0 + zeta) + cos(th) * sin(dec0); 140 *ra = atan2(A, B) + z; 141 *dec = asin(C); 142 } 143 144 static double wrap180(double d) 145 { 146 while (d > 180) 147 d -= 360; 148 while (d < -180) 149 d += 360; 150 return d; 151 } 152 153 int main(int argc, char **argv) 154 { 155 const char *dir = argc > 1 ? argv[1] : NULL; 156 const ck_sky_view *v = &ck_sky_the_view; 157 char path[512]; 158 159 /* 1. the projection */ 160 double worst = 0; 161 for (unsigned i = 0; i < ck_sky_nchecks; i++) { 162 const ck_sky_check *c = &ck_sky_checks[i]; 163 const ck_sky_star *s = &ck_sky_stars[c->star]; 164 int16_t d[3] = {s->x, s->y, s->z}; 165 int32_t x16, y16; 166 int ok = ck_sky_project(v, d, 3840, 1920, 864, &x16, &y16); 167 double e = ok ? hypot(x16 - c->x16, y16 - c->y16) / 16.0 : 1e9; 168 worst = e > worst ? e : worst; 169 } 170 CHECK(ck_sky_nchecks >= 4 && worst < 1.0, "projection: %u stars land where the generator put them, worst %.3f px", 171 ck_sky_nchecks, worst); 172 173 /* 2. the view, built with integer trigonometry */ 174 ck_sky_view w; 175 ck_sky_view_at(&w, v->ra_mdeg * 1000, v->dec_mdeg * 1000, v->fov_mdeg, v->roll_mdeg); 176 double dm = 0; 177 for (int r = 0; r < 3; r++) 178 for (int k = 0; k < 3; k++) { 179 double e = fabs((double)w.rot_q30[r][k] - v->rot_q30[r][k]) / (1 << 30); 180 dm = e > dm ? e : dm; 181 } 182 double dt = fabs((double)w.tan_half_q30 - v->tan_half_q30) / (1 << 30); 183 CHECK(dm < 1e-4 && dt < 1e-4, "view: built in integers it matches the generated one (worst %.2g, tan %.2g)", dm, dt); 184 185 /* 3. sidereal time */ 186 struct { 187 int64_t t; 188 double hours; 189 const char *what; 190 } st[] = { 191 {`946728000`, 18.`697374558`, "2000 Jan 1 12:00 UT (USNO)"}, 192 {`545011200`, 13 + 10 / 60.0 + 46.3668 / 3600, "1987 Apr 10 0:00 UT (Meeus 12.a)"}, 193 {`545080860`, 8 + 34 / 60.0 + 57.0896 / 3600, "1987 Apr 10 19:21 UT (Meeus 12.b)"}, 194 }; 195 for (unsigned i = 0; i < sizeof st / sizeof st[0]; i++) { 196 double h = ck_sky_gmst(st[i].t) / `4294967296`.0 * 24; 197 double e = fabs(h - st[i].hours) * 3600; 198 CHECK(e < 0.02, "sidereal time at %s: %.6f h, off by %.4f s", st[i].what, h, e); 199 } 200 201 /* 4. the zenith: back to J2000 in integers, forward again in doubles */ 202 struct { 203 int64_t t; 204 int32_t lat, lon; 205 } zs[] = {{`1792368000`, `29760427`, -`95369803`}, /* 2026 Oct 19 (Houston-like place) */ 206 {`1792368000`, -`33856000`, `151215000`}, 207 {`946728000`, 0, 0}, 208 {`2524608000`, `64000000`, -`21000000`}}; /* 2050 */ 209 double zworst = 0; 210 for (unsigned i = 0; i < sizeof zs / sizeof zs[0]; i++) { 211 int16_t d[3]; 212 int32_t ra, dec; 213 ck_sky_zenith(zs[i].t, zs[i].lat, zs[i].lon, d, &ra, &dec); 214 double T = (zs[i].t - `946728000`) / (36525.0 * 86400); 215 double rd, dd; 216 precess_forward(ra / 1e6 * D2R, dec / 1e6 * D2R, T, &rd, &dd); 217 double lst = ck_sky_gmst(zs[i].t) / `4294967296`.0 * 360 + zs[i].lon / 1e6; 218 double e1 = fabs(wrap180(rd / D2R - lst)) * cos(dd), e2 = fabs(dd / D2R - zs[i].lat / 1e6); 219 double e = hypot(e1, e2) * 3600; 220 zworst = e > zworst ? e : zworst; 221 printf("note: zenith at %lld, lat %.3f lon %.3f: J2000 RA %.4f Dec %.4f deg (date RA %.4f)\n", 222 (long long)zs[i].t, zs[i].lat / 1e6, zs[i].lon / 1e6, ra / 1e6, dec / 1e6, fmod(lst + 720, 360)); 223 } 224 CHECK(zworst < 30, "zenith: precessed back to J2000 and forward again, worst %.1f arcsec", zworst); 225 CHECK(!CK_SKY_HAVE_LOCATION, "the location setting is unset by default"); 226 227 /* 5. the sky at 3840 x 2160 */ 228 uint32_t W = 3840, H = 2160, k = W * 256 / 1920; 229 size_t nb = ck_night_mem_bytes(W, H, k); 230 void *mem = malloc(nb); 231 ck_night n; 232 double best = 1e9; 233 for (int r = 0; r < 3; r++) { 234 double t0 = now_ms(); 235 int rc = ck_night_init(&n, W, H, k, (int32_t)W / 2, (int32_t)(H * 40 / 100), mem, nb); 236 double t1 = now_ms() - t0; 237 best = t1 < best ? t1 : best; 238 CHECK(rc == 0, "the sky is made (%.1f ms)", t1); 239 } 240 printf("note: the sky made in %.1f ms at 3840x2160 (best of 3, this host, one core)\n", best); 241 unsigned ng = 0; 242 for (unsigned i = 0; i < CK_NIGHT_GALAXIES; i++) 243 ng += n.gal[i].box.w > 0; 244 CHECK(ng == ck_sky_ngalaxies, "galaxies in the view: %u (the catalogue's in the view: %u)", ng, ck_sky_ngalaxies); 245 CHECK(n.ntw > 0, "the brightest stars twinkle: %u of them", n.ntw); 246 ck_night_at(&n, CK_NIGHT_GONE_MS + 1000); /* the eyes adjusted, the holes gone */ 247 ck_rect all = {0, 0, (int32_t)W, (int32_t)H}; 248 /* The sky behind Allen: what night.c puts on the screen. */ 249 uint32_t *ui = malloc((size_t)W * H * 4); 250 ck_canvas uout = {ui, (int32_t)W, (int32_t)H}; 251 ck_night_base(&n, &uout, &all); 252 ck_night_lights(&n, &uout, &all, 0, 0); 253 /* The renderer's diffuse sky (sky_real.c), drawn directly at the same view 254 * and centre, with the same twinkles on top. */ 255 uint32_t *fb = calloc((size_t)W * H, 4); 256 ck_canvas out = {fb, (int32_t)W, (int32_t)H}; 257 ck_night_star dtw[CK_NIGHT_TWINKLES]; 258 unsigned dntw; 259 ck_sky_draw_milkyway(&out, (int32_t)W / 2, (int32_t)(H * 40 / 100), &n.view); 260 ck_sky_draw_stars(&out, (int32_t)W / 2, (int32_t)(H * 40 / 100), &n.view, dtw, CK_NIGHT_TWINKLES, &dntw); 261 ck_night_lights(&n, &out, &all, 0, 0); 262 /* The same catalogue stars on black, the same twinkles: the sky behind Allen must be exactly this. */ 263 uint32_t *stars = calloc((size_t)W * H, 4); 264 ck_canvas sout = {stars, (int32_t)W, (int32_t)H}; 265 ck_sky_draw_stars(&sout, (int32_t)W / 2, (int32_t)(H * 40 / 100), &n.view, dtw, CK_NIGHT_TWINKLES, &dntw); 266 ck_night_lights(&n, &sout, &all, 0, 0); 267 unsigned black = 0, uiblack = 0, mismatch = 0; 268 uint64_t sum = 0; 269 for (size_t i = 0; i < (size_t)W * H; i++) { 270 uint32_t c = fb[i] & 0xffffff; 271 black += c == 0; 272 uiblack += (ui[i] & 0xffffff) == 0; 273 mismatch += (ui[i] & 0xffffff) != (stars[i] & 0xffffff); 274 sum += (c >> 16 & 255) + (c >> 8 & 255) + (c & 255); 275 } 276 printf("note: mean level %.2f (renderer); %u of %u pixels black behind Allen\n", sum / 3.0 / W / H, uiblack, W * H); 277 CHECK(mismatch == 0, "the sky behind Allen is catalogue stars on black, no diffuse light: %u pixels differ", mismatch); 278 CHECK(black == 0, "no pixel of the renderer's sky is black (the night sky glows): %u black", black); 279 /* 6. the Magellanic Clouds glow where they are in the renderer's sky: the median light (linear luminance, so 280 * the stars do not count) at each catalogue galaxy's centre (inside 0.3 of its short half axis) over the 281 * median of a ring at 1.3 to 1.6 long half axes. The generator puts the Clouds' Gaia light, raised to the 282 * catalogue total, into the grid: about 2.5 times the surround at their centres (sky_data.txt, "cloud 283 * light"), about 2 after the 1 degree grid. Measured: LMC 1.99, SMC 1.81 at `176bc51`; LMC 2.06, SMC 1.67 284 * since native-pixel sampling (`6f4c6e8b`); with the screen-to-sky step of the grid off by two (the bug 285 * fixed at `9ab1281`) the glow lands elsewhere and both read 0.95 to 0.99. The bar is 1.5. */ 286 { 287 static double lin[256]; 288 for (int i = 0; i < 256; i++) 289 lin[i] = i <= 10 ? i / 255.0 / 12.92 : pow((i / 255.0 + 0.055) / 1.055, 2.4); 290 size_t cap = (size_t)W * H / 4; 291 double *in = malloc(cap * sizeof *in), *ring = malloc(cap * sizeof *ring); 292 for (unsigned i = 0; i < ck_sky_ngalaxies; i++) { 293 int32_t x16, y16; 294 if (!ck_sky_project(&n.view, ck_sky_galaxies[i].dir, (int32_t)W, (int32_t)W / 2, (int32_t)(H * 40 / 100), &x16, &y16)) 295 continue; 296 int32_t gx = (x16 + 8) >> 4, gy = (y16 + 8) >> 4; 297 double a = (double)ck_sky_galaxies[i].a_q16 * W / 65536, b = (double)ck_sky_galaxies[i].b_q16 * W / 65536; 298 size_t nin = 0, nring = 0; 299 int32_t r = (int32_t)(1.6 * a) + 1; 300 for (int32_t y = gy - r; y <= gy + r; y++) 301 for (int32_t x = gx - r; x <= gx + r; x++) { 302 if (x < 0 || y < 0 || x >= (int32_t)W || y >= (int32_t)H) 303 continue; 304 double d = sqrt((double)(x - gx) * (x - gx) + (double)(y - gy) * (y - gy)); 305 uint32_t c = fb[(size_t)y * W + (size_t)x]; 306 double l = 0.2126 * lin[c >> 16 & 255] + 0.7152 * lin[c >> 8 & 255] + 0.0722 * lin[c & 255]; 307 if (d <= 0.3 * b && nin < cap) 308 in[nin++] = l; 309 else if (d >= 1.3 * a && d <= 1.6 * a && nring < cap) 310 ring[nring++] = l; 311 } 312 qsort(in, nin, sizeof *in, cmp_double); 313 qsort(ring, nring, sizeof *ring, cmp_double); 314 double ratio = nin && nring && ring[nring / 2] > 0 ? in[nin / 2] / ring[nring / 2] : 0; 315 CHECK(ratio >= 1.5, "galaxy %u glows at its place in the renderer's sky: centre %.5f over surround %.5f linear = %.2f (at least 1.5)", i, 316 nin ? in[nin / 2] : 0, nring ? ring[nring / 2] : 0, ratio); 317 } 318 free(in); 319 free(ring); 320 } 321 if (dir) { 322 snprintf(path, sizeof path, "%s/sky-whole.png", dir); 323 png(path, fb, W, 0, 0, W, H, 2); 324 snprintf(path, sizeof path, "%s/sky-behind-allen-whole.png", dir); 325 png(path, ui, W, 0, 0, W, H, 2); 326 /* pieces at full size: the view centre (where GW150914 came from), the Large Magellanic Cloud, Canopus */ 327 snprintf(path, sizeof path, "%s/sky-centre-1to1.png", dir); 328 png(path, fb, W, 1920 - 640, 864 - 360, 1280, 720, 1); 329 for (unsigned i = 0; i < ck_sky_ngalaxies && i < 2; i++) { 330 ck_rect b = n.gal[i].box; 331 int cx = b.x + b.w / 2, cy = b.y + b.h / 2; 332 cx = cx < 640 ? 640 : cx > (int)W - 640 ? (int)W - 640 : cx; 333 cy = cy < 360 ? 360 : cy > (int)H - 360 ? (int)H - 360 : cy; 334 snprintf(path, sizeof path, "%s/sky-galaxy%u-1to1.png", dir, i); 335 png(path, fb, W, cx - 640, cy - 360, 1280, 720, 1); 336 } 337 if (n.ntw) { 338 int cx = n.tw[0].x >> 4, cy = n.tw[0].y >> 4; 339 cx = cx < 320 ? 320 : cx > (int)W - 320 ? (int)W - 320 : cx; 340 cy = cy < 180 ? 180 : cy > (int)H - 180 ? (int)H - 180 : cy; 341 snprintf(path, sizeof path, "%s/sky-brightest-1to1.png", dir); 342 png(path, fb, W, cx - 320, cy - 180, 640, 360, 1); 343 } 344 } 345 printf("SKY_TEST: %s (%d checks, %d failed)\n", fails ? "FAIL" : "PASS", checks, fails); 346 return fails ? 1 : 0; 347 }