Instrument Neutral Distributed Interface INDI 2.2.5
test_ccd_simulator.cpp
Go to the documentation of this file.
1#include "indilogger.h"
2
3#include <gtest/gtest.h>
4
5#include "ccd_simulator.h"
6
7#include <cmath>
8#include <set>
9#include <utility>
10
11char _me[] = "MockCCDSimDriver";
12char *me = _me;
14{
15 public:
21
23 {
24 auto p = getNumber("SIMULATOR_SETTINGS");
25 ASSERT_TRUE(p.isValid());
26 ASSERT_NE(p.findWidgetByName("SIM_XRES"), nullptr);
27 ASSERT_NE(p.findWidgetByName("SIM_YRES"), nullptr);
28 ASSERT_NE(p.findWidgetByName("SIM_XSIZE"), nullptr);
29 ASSERT_NE(p.findWidgetByName("SIM_YSIZE"), nullptr);
30 ASSERT_NE(p.findWidgetByName("SIM_MAXVAL"), nullptr);
31 ASSERT_NE(p.findWidgetByName("SIM_SATURATION"), nullptr);
32 ASSERT_NE(p.findWidgetByName("SIM_LIMITINGMAG"), nullptr);
33 ASSERT_NE(p.findWidgetByName("SIM_NOISE"), nullptr);
34 ASSERT_NE(p.findWidgetByName("SIM_SKYGLOW"), nullptr);
35 ASSERT_NE(p.findWidgetByName("SIM_OAGOFFSET"), nullptr);
36 ASSERT_NE(p.findWidgetByName("SIM_POLAR"), nullptr);
37 ASSERT_NE(p.findWidgetByName("SIM_POLARDRIFT"), nullptr);
38
39 auto defects = getNumber("SIM_DEFECTS");
40 ASSERT_TRUE(defects.isValid());
41 ASSERT_NE(defects.findWidgetByName("SIM_HOT_PIXELS"), nullptr);
42 ASSERT_NE(defects.findWidgetByName("SIM_COLD_PIXELS"), nullptr);
43 ASSERT_NE(defects.findWidgetByName("SIM_HOT_RATE"), nullptr);
44 EXPECT_EQ(defects[SIM_HOT_PIXELS].getValue(), 0) << "defects must be off by default";
45 EXPECT_EQ(defects[SIM_COLD_PIXELS].getValue(), 0) << "defects must be off by default";
46 }
47
49 {
50 EXPECT_TRUE(isnan(currentRA)) << "Field 'currentRA' is undefined when initializing CCDSim.";
51 EXPECT_TRUE(isnan(currentDE)) << "Field 'currentDEC' is undefined when initializing CCDSim.";
52
53 EXPECT_EQ(GuideRate, 7 /* arcsec/s */);
54
55 EXPECT_EQ(guideNSOffset, 0);
56 EXPECT_EQ(guideWEOffset, 0);
57
58 double const arcsec = 1.0 / 3600.0;
59
60 EXPECT_EQ(GuideNorth(1000.0), IPS_OK);
61 EXPECT_NEAR(guideNSOffset, +7 * arcsec, 1 * arcsec);
62 EXPECT_EQ(GuideSouth(1000.0), IPS_OK);
63 EXPECT_NEAR(guideNSOffset, +0 * arcsec, 1 * arcsec);
64 EXPECT_EQ(GuideSouth(1000.0), IPS_OK);
65 EXPECT_NEAR(guideNSOffset, -7 * arcsec, 1 * arcsec);
66 EXPECT_EQ(GuideNorth(1000.0), IPS_OK);
67 EXPECT_NEAR(guideNSOffset, +0 * arcsec, 1 * arcsec);
68
69 currentDE = 0;
70
71 EXPECT_EQ(GuideWest(1000.0), IPS_OK);
72 EXPECT_NEAR(guideWEOffset, +7 * arcsec, 15 * arcsec);
73 EXPECT_EQ(GuideEast(1000.0), IPS_OK);
74 EXPECT_NEAR(guideWEOffset, +0 * arcsec, 15 * arcsec);
75 EXPECT_EQ(GuideEast(1000.0), IPS_OK);
76 EXPECT_NEAR(guideWEOffset, -7 * arcsec, 15 * arcsec);
77 EXPECT_EQ(GuideWest(1000.0), IPS_OK);
78 EXPECT_NEAR(guideWEOffset, +0 * arcsec, 15 * arcsec);
79 }
80};
81
82// ---------------------------------------------------------------------------
83// SkyRenderer tests -- exercise the renderer standalone, no INDI driver.
84// ---------------------------------------------------------------------------
85class SkyRendererTest : public ::testing::Test
86{
87 protected:
90
91 void setupChip(int xres, int yres, float pixelSize = 4.6f)
92 {
93 chip.setResolution(xres, yres);
94 chip.setPixelSize(pixelSize, pixelSize);
95 chip.setFrame(0, 0, xres, yres);
96 chip.setBPP(16);
97 chip.setFrameBufferSize(xres * yres * 2);
98 memset(chip.getFrameBuffer(), 0, chip.getFrameBufferSize());
99 }
100
101 uint16_t *fb()
102 {
103 return reinterpret_cast<uint16_t *>(chip.getFrameBuffer());
104 }
105
106 long sumFrame(int xres, int yres)
107 {
108 uint16_t *buf = fb();
109 long s = 0;
110 for (int i = 0; i < xres * yres; i++)
111 s += buf[i];
112 return s;
113 }
114
115 uint16_t peakPixel(int xres, int yres)
116 {
117 uint16_t *buf = fb();
118 uint16_t peak = 0;
119 for (int i = 0; i < xres * yres; i++)
120 if (buf[i] > peak) peak = buf[i];
121 return peak;
122 }
123};
124
125// --- flux behavior (tested through drawImageStar) ---
126
127TEST_F(SkyRendererTest, saturation_mag_produces_high_adu)
128{
129 int const xres = 33;
130 int const yres = 33;
131 int const maxval = 65000;
132
133 RenderConfig cfg;
134 cfg.maxVal = maxval;
135 cfg.saturationMag = 5.0f;
136 cfg.limitingMag = 20.0f;
137 cfg.seeing = 2.0f;
138 renderer.setConfig(cfg);
139 renderer.setImageScale(1.0f, 1.0f);
140 setupChip(xres, yres);
141
142 renderer.drawImageStar(&chip, cfg.saturationMag, xres / 2, yres / 2, 1.0f);
143
144 EXPECT_GT(peakPixel(xres, yres), static_cast<uint16_t>(maxval / 4))
145 << "a star at saturation magnitude for 1s should produce high ADU";
146}
147
148TEST_F(SkyRendererTest, limiting_mag_produces_minimal_adu)
149{
150 int const xres = 33;
151 int const yres = 33;
152
153 RenderConfig cfg;
154 cfg.maxVal = 65000;
155 cfg.saturationMag = 0.0f;
156 cfg.limitingMag = 20.0f;
157 cfg.seeing = 2.0f;
158 renderer.setConfig(cfg);
159 renderer.setImageScale(1.0f, 1.0f);
160 setupChip(xres, yres);
161
162 renderer.drawImageStar(&chip, cfg.limitingMag, xres / 2, yres / 2, 1.0f);
163
164 EXPECT_LE(peakPixel(xres, yres), 2u)
165 << "a star at limiting magnitude for 1s should produce ~1 ADU";
166}
167
168TEST_F(SkyRendererTest, degenerate_mag_range_does_not_crash)
169{
170 int const xres = 33;
171 int const yres = 33;
172
173 RenderConfig cfg;
174 cfg.maxVal = 65000;
175 cfg.saturationMag = 10.0f;
176 cfg.limitingMag = 10.0f;
177 cfg.seeing = 2.0f;
178 renderer.setConfig(cfg);
179 renderer.setImageScale(1.0f, 1.0f);
180 setupChip(xres, yres);
181
182 renderer.drawImageStar(&chip, 10.0f, xres / 2, yres / 2, 1.0f);
183 // Must not crash or produce NaN; some output is acceptable
184}
185
186TEST_F(SkyRendererTest, brighter_star_produces_more_flux)
187{
188 int const xres = 33;
189 int const yres = 33;
190 int const cx = xres / 2;
191 int const cy = yres / 2;
192
193 RenderConfig cfg;
194 cfg.maxVal = 65000;
195 cfg.saturationMag = 0.0f;
196 cfg.limitingMag = 20.0f;
197 cfg.seeing = 2.0f;
198 renderer.setConfig(cfg);
199 renderer.setImageScale(1.0f, 1.0f);
200
201 setupChip(xres, yres);
202 renderer.drawImageStar(&chip, 8.0f, cx, cy, 1.0f);
203 long const sum_bright = sumFrame(xres, yres);
204
205 setupChip(xres, yres);
206 renderer.drawImageStar(&chip, 12.0f, cx, cy, 1.0f);
207 long const sum_dim = sumFrame(xres, yres);
208
209 ASSERT_GT(sum_bright, 0L);
210 ASSERT_GT(sum_dim, 0L);
211 EXPECT_GT(sum_bright, sum_dim);
212}
213
214// --- pixel clamping (tested through drawImageStar) ---
215
216TEST_F(SkyRendererTest, pixel_values_clamped_at_max_val)
217{
218 int const xres = 33;
219 int const yres = 33;
220 int const maxval = 1000;
221
222 RenderConfig cfg;
223 cfg.maxVal = maxval;
224 cfg.saturationMag = 0.0f;
225 cfg.limitingMag = 20.0f;
226 cfg.seeing = 2.0f;
227 renderer.setConfig(cfg);
228 renderer.setImageScale(1.0f, 1.0f);
229 setupChip(xres, yres);
230
231 renderer.drawImageStar(&chip, 0.0f, xres / 2, yres / 2, 1000.0f);
232
233 uint16_t *buf = fb();
234 for (int i = 0; i < xres * yres; i++)
235 EXPECT_LE(buf[i], static_cast<uint16_t>(maxval))
236 << "no pixel may exceed maxVal";
237}
238
239// --- drawImageStar: behavioral properties ---
240
241TEST_F(SkyRendererTest, star_center_is_brightest)
242{
243 int const xres = 33;
244 int const yres = 33;
245
246 RenderConfig cfg;
247 cfg.maxVal = 65000;
248 cfg.saturationMag = 0.0f;
249 cfg.limitingMag = 20.0f;
250 cfg.seeing = 2.0f;
251 renderer.setConfig(cfg);
252 renderer.setImageScale(1.0f, 1.0f);
253 setupChip(xres, yres);
254
255 int const cx = xres / 2;
256 int const cy = yres / 2;
257 renderer.drawImageStar(&chip, 5.0f, cx, cy, 1.0f);
258
259 uint16_t *buf = fb();
260 uint16_t const peak = buf[cy * xres + cx];
261 EXPECT_GT(peak, 0u);
262 EXPECT_GT(peak, buf[cy * xres + cx + 1]);
263 EXPECT_GT(peak, buf[cy * xres + cx - 1]);
264 EXPECT_GT(peak, buf[(cy + 1) * xres + cx]);
265 EXPECT_GT(peak, buf[(cy - 1) * xres + cx]);
266}
267
268TEST_F(SkyRendererTest, star_profile_symmetric)
269{
270 int const xres = 33;
271 int const yres = 33;
272
273 RenderConfig cfg;
274 cfg.maxVal = 65000;
275 cfg.saturationMag = 0.0f;
276 cfg.limitingMag = 20.0f;
277 cfg.seeing = 2.0f;
278 renderer.setConfig(cfg);
279 renderer.setImageScale(1.0f, 1.0f);
280 setupChip(xres, yres);
281
282 int const cx = xres / 2;
283 int const cy = yres / 2;
284 renderer.drawImageStar(&chip, 5.0f, cx, cy, 1.0f);
285
286 uint16_t *buf = fb();
287 for (int d = 1; d <= 3; d++)
288 {
289 EXPECT_EQ(buf[cy * xres + cx + d], buf[cy * xres + cx - d])
290 << "horizontal symmetry at d=" << d;
291 EXPECT_EQ(buf[(cy + d) * xres + cx], buf[(cy - d) * xres + cx])
292 << "vertical symmetry at d=" << d;
293 }
294}
295
296TEST_F(SkyRendererTest, star_profile_monotonic_falloff)
297{
298 int const xres = 33;
299 int const yres = 33;
300
301 RenderConfig cfg;
302 cfg.maxVal = 65000;
303 cfg.saturationMag = 0.0f;
304 cfg.limitingMag = 20.0f;
305 cfg.seeing = 2.0f;
306 renderer.setConfig(cfg);
307 renderer.setImageScale(1.0f, 1.0f);
308 setupChip(xres, yres);
309
310 int const cx = xres / 2;
311 int const cy = yres / 2;
312 renderer.drawImageStar(&chip, 5.0f, cx, cy, 1.0f);
313
314 uint16_t *buf = fb();
315 for (int d = 0; d < 5; d++)
316 {
317 uint16_t const inner = buf[cy * xres + cx + d];
318 uint16_t const outer = buf[cy * xres + cx + d + 1];
319 EXPECT_GE(inner, outer)
320 << "profile must not increase at d=" << d << " -> " << d + 1;
321 }
322}
323
324TEST_F(SkyRendererTest, star_zero_beyond_render_box)
325{
326 int const xres = 65;
327 int const yres = 65;
328
329 RenderConfig cfg;
330 cfg.maxVal = 65000;
331 cfg.saturationMag = 0.0f;
332 cfg.limitingMag = 20.0f;
333 cfg.seeing = 1.0f;
334 renderer.setConfig(cfg);
335 renderer.setImageScale(1.0f, 1.0f);
336 setupChip(xres, yres);
337
338 int const cx = xres / 2;
339 int const cy = yres / 2;
340 renderer.drawImageStar(&chip, 10.0f, cx, cy, 1.0f);
341
342 uint16_t *buf = fb();
343 int const beyond = static_cast<int>(3.0f * cfg.seeing / 1.0f) + 2;
344 for (int d = beyond; d < cx; d++)
345 {
346 EXPECT_EQ(buf[cy * xres + cx + d], 0u)
347 << "pixel at d=" << d << " is beyond the render box";
348 }
349}
350
351// Regression guard for indilib/indi#2465: the PSF must be centred on the true
352// (sub-pixel) star position, not snapped to the truncated integer pixel. If the
353// fractional part of the coordinate is discarded, a sub-pixel shift produces no
354// change in the rendered centroid, which quantised guide-sim drift to a lattice
355// and broke calibration.
356TEST_F(SkyRendererTest, star_centroid_tracks_subpixel_shift)
357{
358 int const xres = 201;
359 int const yres = 201;
360
361 RenderConfig cfg;
362 cfg.maxVal = 65000;
363 cfg.saturationMag = 0.0f;
364 cfg.limitingMag = 20.0f;
365 cfg.seeing = 3.0f;
366 renderer.setConfig(cfg);
367 renderer.setImageScale(1.0f, 1.0f);
368
369 // Flux-weighted X centroid of the whole frame.
370 auto centroidX = [this](int nx, int ny) -> double
371 {
372 uint16_t *buf = fb();
373 double sumWeighted = 0.0;
374 double sumFlux = 0.0;
375 for (int y = 0; y < ny; y++)
376 for (int x = 0; x < nx; x++)
377 {
378 double const v = buf[y * nx + x];
379 sumWeighted += v * x;
380 sumFlux += v;
381 }
382 return sumFlux > 0 ? sumWeighted / sumFlux : -1.0;
383 };
384
385 // Integer y keeps the comparison on the X axis only.
386 float const y = 100.0f;
387
388 setupChip(xres, yres);
389 renderer.drawImageStar(&chip, 6.0f, 100.3f, y, 1.0f);
390 double const cxLow = centroidX(xres, yres);
391
392 setupChip(xres, yres);
393 renderer.drawImageStar(&chip, 6.0f, 100.7f, y, 1.0f);
394 double const cxHigh = centroidX(xres, yres);
395
396 ASSERT_GT(cxLow, 0.0);
397 ASSERT_GT(cxHigh, 0.0);
398
399 // Each centroid should sit near its true sub-pixel position...
400 EXPECT_NEAR(cxLow, 100.3, 0.15);
401 EXPECT_NEAR(cxHigh, 100.7, 0.15);
402 // ...and, crucially, the 0.4 px shift must be reflected in the render.
403 // The pre-fix code snapped both to pixel 100, giving a zero difference.
404 EXPECT_GT(cxHigh - cxLow, 0.2);
405}
406
407TEST_F(SkyRendererTest, star_flux_scales_with_exposure)
408{
409 int const xres = 33;
410 int const yres = 33;
411
412 RenderConfig cfg;
413 cfg.maxVal = 65000;
414 cfg.saturationMag = 0.0f;
415 cfg.limitingMag = 20.0f;
416 cfg.seeing = 2.0f;
417 renderer.setConfig(cfg);
418 renderer.setImageScale(1.0f, 1.0f);
419
420 int const cx = xres / 2;
421 int const cy = yres / 2;
422
423 setupChip(xres, yres);
424 renderer.drawImageStar(&chip, 10.0f, cx, cy, 1.0f);
425 long const sum1s = sumFrame(xres, yres);
426
427 setupChip(xres, yres);
428 renderer.drawImageStar(&chip, 10.0f, cx, cy, 4.0f);
429 long const sum4s = sumFrame(xres, yres);
430
431 ASSERT_GT(sum1s, 0L);
432 EXPECT_NEAR(static_cast<double>(sum4s) / sum1s, 4.0, 0.5)
433 << "total flux must scale linearly with exposure time";
434}
435
436TEST_F(SkyRendererTest, star_off_chip_draws_nothing)
437{
438 int const xres = 33;
439 int const yres = 33;
440
441 RenderConfig cfg;
442 cfg.maxVal = 65000;
443 cfg.saturationMag = 0.0f;
444 cfg.limitingMag = 20.0f;
445 cfg.seeing = 2.0f;
446 renderer.setConfig(cfg);
447 renderer.setImageScale(1.0f, 1.0f);
448 setupChip(xres, yres);
449
450 renderer.drawImageStar(&chip, 5.0f, -10.0f, -10.0f, 1.0f);
451 renderer.drawImageStar(&chip, 5.0f, xres + 10.0f, yres + 10.0f, 1.0f);
452 EXPECT_EQ(sumFrame(xres, yres), 0);
453}
454
455// --- renderFrame ---
456
457TEST_F(SkyRendererTest, render_frame_image_scale)
458{
459 int const xres = 100;
460 int const yres = 100;
461 float const pixelSizeUM = 5.0f;
462 double const focalLengthMM = 1000.0;
463
464 RenderConfig cfg;
465 cfg.maxVal = 65000;
466 cfg.skyGlow = 99.0f;
467 renderer.setConfig(cfg);
468 setupChip(xres, yres, pixelSizeUM);
469
470 renderer.renderFrame(&chip, 0.0, 0.0, focalLengthMM, 0.0, 1.0f, false);
471
472 float const expectedScale = static_cast<float>((pixelSizeUM / focalLengthMM) * 206.3);
473 EXPECT_NEAR(renderer.imageScaleX(), expectedScale, 0.001f);
474 EXPECT_NEAR(renderer.imageScaleY(), expectedScale, 0.001f);
475}
476
477TEST_F(SkyRendererTest, render_frame_no_stars_produces_sky_glow)
478{
479 int const xres = 33;
480 int const yres = 33;
481
482 RenderConfig cfg;
483 cfg.maxVal = 65000;
484 cfg.skyGlow = 15.0f;
485 cfg.saturationMag = 0.0f;
486 cfg.limitingMag = 20.0f;
487 renderer.setConfig(cfg);
488 setupChip(xres, yres);
489
490 int drawn = renderer.renderFrame(&chip, 0.0, 0.0, 500.0, 0.0, 10.0f, false);
491 EXPECT_EQ(drawn, 0) << "renderStars=false must draw zero stars";
492 EXPECT_GT(sumFrame(xres, yres), 0L) << "sky glow must still be rendered";
493}
494
495TEST_F(SkyRendererTest, render_frame_sky_glow_vignetting)
496{
497 int const xres = 65;
498 int const yres = 65;
499
500 RenderConfig cfg;
501 cfg.maxVal = 65000;
502 cfg.skyGlow = 15.0f;
503 cfg.saturationMag = 0.0f;
504 cfg.limitingMag = 20.0f;
505 renderer.setConfig(cfg);
506 setupChip(xres, yres);
507
508 renderer.renderFrame(&chip, 0.0, 0.0, 500.0, 0.0, 10.0f, false);
509
510 uint16_t *buf = fb();
511 uint16_t const center_val = buf[(yres / 2) * xres + xres / 2];
512 uint16_t const corner_val = buf[0];
513
514 EXPECT_GT(center_val, 0u);
515 EXPECT_GT(center_val, corner_val)
516 << "vignetting must make center brighter than corners";
517}
518
519TEST_F(SkyRendererTest, render_frame_clears_buffer)
520{
521 int const xres = 16;
522 int const yres = 16;
523
524 RenderConfig cfg;
525 cfg.maxVal = 65000;
526 cfg.skyGlow = 99.0f;
527 renderer.setConfig(cfg);
528 setupChip(xres, yres);
529
530 memset(chip.getFrameBuffer(), 0xFF, chip.getFrameBufferSize());
531
532 renderer.renderFrame(&chip, 0.0, 0.0, 500.0, 0.0, 0.001f, false);
533
534 uint16_t *buf = fb();
535 for (int i = 0; i < xres * yres; i++)
536 EXPECT_LT(buf[i], 10u) << "renderFrame must clear pre-existing data";
537}
538
539// --- applyReadoutNoise ---
540
541TEST_F(SkyRendererTest, readout_noise_adds_bias_and_noise)
542{
543 int const xres = 16;
544 int const yres = 16;
545 int const maxval = 65000;
546 int const bias = 1500;
547 int const maxNoise = 20;
548
549 RenderConfig cfg;
550 cfg.maxVal = maxval;
551 renderer.setConfig(cfg);
552 setupChip(xres, yres);
553
554 renderer.applyReadoutNoise(&chip, bias, maxNoise);
555
556 uint16_t *buf = fb();
557 for (int i = 0; i < xres * yres; i++)
558 {
559 EXPECT_GE(buf[i], static_cast<uint16_t>(bias));
560 EXPECT_LE(buf[i], static_cast<uint16_t>(bias + maxNoise));
561 }
562}
563
564TEST_F(SkyRendererTest, readout_noise_clamps_at_max_val)
565{
566 int const xres = 8;
567 int const yres = 8;
568 int const maxval = 100;
569
570 RenderConfig cfg;
571 cfg.maxVal = maxval;
572 renderer.setConfig(cfg);
573 setupChip(xres, yres);
574
575 // Bias alone exceeds maxVal
576 renderer.applyReadoutNoise(&chip, 200, 10);
577
578 uint16_t *buf = fb();
579 for (int i = 0; i < xres * yres; i++)
580 EXPECT_EQ(buf[i], static_cast<uint16_t>(maxval));
581}
582
583TEST_F(SkyRendererTest, readout_noise_skipped_when_zero)
584{
585 int const xres = 8;
586 int const yres = 8;
587
588 RenderConfig cfg;
589 cfg.maxVal = 65000;
590 renderer.setConfig(cfg);
591 setupChip(xres, yres);
592
593 renderer.applyReadoutNoise(&chip, 1500, 0);
594
595 EXPECT_EQ(sumFrame(xres, yres), 0)
596 << "maxNoise <= 0 must skip noise application entirely";
597}
598
599// --- sensor defects ---
600
601TEST_F(SkyRendererTest, defects_are_reproducible_distinct_and_in_bounds)
602{
603 int const xres = 200;
604 int const yres = 150;
605
606 auto const a = SkyRenderer::generateDefects(xres, yres, 300, 100, 1000.0f);
607 auto const b = SkyRenderer::generateDefects(xres, yres, 300, 100, 1000.0f);
608 auto const c = SkyRenderer::generateDefects(xres, yres, 300, 100, 1000.0f, 2);
609
610 ASSERT_EQ(a.size(), 400u);
611 ASSERT_EQ(b.size(), a.size());
612
613 std::set<std::pair<int, int>> positions;
614 int hot = 0, cold = 0;
615 bool differsFromOtherSeed = false;
616 for (size_t i = 0; i < a.size(); i++)
617 {
618 EXPECT_EQ(a[i].x, b[i].x);
619 EXPECT_EQ(a[i].y, b[i].y);
620 EXPECT_EQ(a[i].rate, b[i].rate);
621 if (a[i].x != c[i].x || a[i].y != c[i].y)
622 differsFromOtherSeed = true;
623
624 EXPECT_GE(a[i].x, 0);
625 EXPECT_LT(a[i].x, xres);
626 EXPECT_GE(a[i].y, 0);
627 EXPECT_LT(a[i].y, yres);
628 positions.insert({a[i].x, a[i].y});
629
630 if (a[i].cold)
631 {
632 cold++;
633 }
634 else
635 {
636 hot++;
637 EXPECT_GE(a[i].rate, 500.0f);
638 EXPECT_LT(a[i].rate, 1500.0f);
639 }
640 }
641 EXPECT_EQ(positions.size(), a.size()) << "defects must not share a pixel";
642 EXPECT_EQ(hot, 300);
643 EXPECT_EQ(cold, 100);
644 EXPECT_TRUE(differsFromOtherSeed);
645}
646
647TEST_F(SkyRendererTest, defects_are_clamped_to_sensor_size)
648{
649 auto const defects = SkyRenderer::generateDefects(4, 4, 10, 10, 100.0f);
650 EXPECT_EQ(defects.size(), 16u);
651 EXPECT_TRUE(SkyRenderer::generateDefects(0, 4, 10, 10, 100.0f).empty());
652 EXPECT_TRUE(SkyRenderer::generateDefects(4, 4, 0, 0, 100.0f).empty());
653}
654
655TEST_F(SkyRendererTest, hot_pixels_scale_with_exposure_and_cold_pixels_read_low)
656{
657 int const xres = 32;
658 int const yres = 32;
659 int const bias = 1000;
660
661 RenderConfig cfg;
662 cfg.maxVal = 65000;
663 renderer.setConfig(cfg);
664
665 std::vector<SensorDefect> defects(2);
666 defects[0].x = 5;
667 defects[0].y = 7;
668 defects[0].rate = 200.0f;
669 defects[1].x = 20;
670 defects[1].y = 3;
671 defects[1].cold = true;
672
673 for (float exposure : {1.0f, 3.0f})
674 {
675 setupChip(xres, yres);
676 renderer.applyReadoutNoise(&chip, bias, 1); // flat bias, no noise
677 renderer.applyDefects(&chip, defects, exposure, bias);
678
679 uint16_t *buf = fb();
680 EXPECT_EQ(buf[7 * xres + 5], static_cast<uint16_t>(bias + 200 * exposure));
681 EXPECT_EQ(buf[3 * xres + 20], static_cast<uint16_t>(bias / 2));
682 EXPECT_EQ(buf[0], static_cast<uint16_t>(bias)) << "other pixels must be left alone";
683 EXPECT_EQ(sumFrame(xres, yres), static_cast<long>(xres * yres * bias + 200 * exposure - bias / 2));
684 }
685}
686
687TEST_F(SkyRendererTest, hot_pixels_clamp_at_max_val)
688{
689 RenderConfig cfg;
690 cfg.maxVal = 4095;
691 renderer.setConfig(cfg);
692 setupChip(8, 8);
693
694 std::vector<SensorDefect> defects(1);
695 defects[0].x = 2;
696 defects[0].y = 2;
697 defects[0].rate = 60000.0f;
698 renderer.applyDefects(&chip, defects, 10.0f, 0);
699
700 EXPECT_EQ(fb()[2 * 8 + 2], 4095u);
701}
702
703TEST_F(SkyRendererTest, defects_follow_the_subframe)
704{
705 int const xres = 64;
706 int const yres = 64;
707
708 RenderConfig cfg;
709 cfg.maxVal = 65000;
710 renderer.setConfig(cfg);
711 setupChip(xres, yres);
712
713 // 16x16 subframe starting at (10, 20)
714 chip.setFrame(10, 20, 16, 16);
715 memset(chip.getFrameBuffer(), 0, chip.getFrameBufferSize());
716
717 std::vector<SensorDefect> defects(3);
718 defects[0].x = 12; // inside -> subframe (2, 3)
719 defects[0].y = 23;
720 defects[0].rate = 100.0f;
721 defects[1].x = 9; // left of the subframe
722 defects[1].y = 23;
723 defects[1].rate = 100.0f;
724 defects[2].x = 26; // right of the subframe (10 + 16)
725 defects[2].y = 25;
726 defects[2].rate = 100.0f;
727
728 renderer.applyDefects(&chip, defects, 1.0f, 0);
729
730 EXPECT_EQ(fb()[3 * 16 + 2], 100u);
731 EXPECT_EQ(sumFrame(16, 16), 100) << "defects outside the subframe must not be drawn";
732}
733
734// --- benchmark (no assertions, informational only) ---
735
736TEST_F(SkyRendererTest, draw_star_benchmark)
737{
738 int const xres = 65;
739 int const yres = 65;
740
741 RenderConfig cfg;
742 cfg.maxVal = 65000;
743 cfg.saturationMag = 0.0f;
744 cfg.limitingMag = 30.0f;
745 cfg.seeing = 1.0f;
746 renderer.setConfig(cfg);
747 renderer.setImageScale(1.0f, 1.0f);
748 setupChip(xres, yres);
749
750 auto const before = std::chrono::steady_clock::now();
751 int const loops = 200000;
752 for (int i = 0; i < loops; i++)
753 {
754 float const m = (15.0f * rand()) / static_cast<float>(RAND_MAX);
755 float const x = static_cast<float>(xres * rand()) / static_cast<float>(RAND_MAX);
756 float const y = static_cast<float>(yres * rand()) / static_cast<float>(RAND_MAX);
757 float const e = (100.0f * rand()) / static_cast<float>(RAND_MAX);
758 renderer.drawImageStar(&chip, m, x, y, e);
759 }
760 auto const after = std::chrono::steady_clock::now();
761 auto const duration = std::chrono::duration_cast<std::chrono::nanoseconds>(after - before).count() / loops;
762 std::cout << "[ ] drawImageStar benchmark: " << duration << " ns/call" << std::endl;
763}
764
765// --- CCDSim driver-level tests ---
766
767TEST(CCDSimulatorDriverTest, test_properties)
768{
770}
771
772TEST(CCDSimulatorDriverTest, test_guide_api)
773{
775}
776
777int main(int argc, char **argv)
778{
781
782 ::testing::InitGoogleTest(&argc, argv);
783 return RUN_ALL_TESTS();
784}
char _me[]
bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
float guideWEOffset
virtual IPState GuideSouth(uint32_t) override
Guide southward for ms milliseconds.
float GuideRate
Guide rate is 7 arcseconds per second.
void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
float guideNSOffset
virtual IPState GuideEast(uint32_t) override
Guide easward for ms milliseconds.
virtual IPState GuideWest(uint32_t) override
Guide westward for ms milliseconds.
double currentDE
virtual IPState GuideNorth(uint32_t) override
Guide northward for ms milliseconds.
@ SIM_COLD_PIXELS
double currentRA
INDI::PropertyNumber getNumber(const char *name) const
The CCDChip class provides functionality of a CCD Chip within a CCD.
Definition indiccdchip.h:39
static const loggerConf file_off
Definition indilogger.h:219
void configure(const std::string &outputFile, const loggerConf configuration, const int fileVerbosityLevel, const int screenVerbosityLevel)
Method to configure the logger. Called by the DEBUG_CONF() macro. To make implementation easier,...
static Logger & getInstance()
Method to get a reference to the object (i.e., Singleton) It is a static method.
uint16_t peakPixel(int xres, int yres)
long sumFrame(int xres, int yres)
void setupChip(int xres, int yres, float pixelSize=4.6f)
static std::vector< SensorDefect > generateDefects(int xres, int yres, int hotCount, int coldCount, float hotRate, uint32_t seed=1)
int main()
@ IPS_OK
Definition indiapi.h:160
char * me
Definition indidriver.c:50
float limitingMag
float saturationMag
TEST_F(SkyRendererTest, saturation_mag_produces_high_adu)
TEST(CCDSimulatorDriverTest, test_properties)
char * me