10#include <unordered_set>
12double SkyRenderer::flux(
double mag)
const
14 if (m_Cfg.limitingMag == m_Cfg.saturationMag)
16 double const z = m_Cfg.limitingMag;
17 double const k = 2.5 * log10(m_Cfg.maxVal) / (m_Cfg.limitingMag - m_Cfg.saturationMag);
18 return pow(10.0, (z - mag) * k / 2.5);
21int SkyRenderer::addToPixel(
INDI::CCDChip *chip,
int x,
int y,
int val)
23 int const nwidth = chip->
getSubW();
24 int const nheight = chip->
getSubH();
29 if (x < 0 || x >= nwidth || y < 0 || y >= nheight)
35 int newval =
static_cast<int>(pt[0]) + val;
36 if (newval > m_Cfg.maxVal)
37 newval = m_Cfg.maxVal;
38 if (newval > m_MaxPix)
40 if (newval < m_MinPix)
42 pt[0] =
static_cast<uint16_t
>(newval);
51 int const subX = chip->
getSubX();
52 int const subY = chip->
getSubY();
53 int const subW = chip->
getSubW() + subX;
54 int const subH = chip->
getSubH() + subY;
56 if (x < subX || x > subW || y < subY || y > subH)
59 float const totalFlux =
static_cast<float>(flux(mag) * exp_s);
66 float effectiveSeeing = m_Cfg.seeing;
67 if (m_Cfg.tiltLR != 0.0f || m_Cfg.tiltTB != 0.0f)
69 float const cx = chip->
getXRes() / 2.0f;
70 float const cy = chip->
getYRes() / 2.0f;
71 float const dx = (x - cx) / cx;
72 float const dy = (y - cy) / cy;
75 float const tiltOffset = m_Cfg.tiltLR * dx + m_Cfg.tiltTB * dy;
76 effectiveSeeing += tiltOffset;
77 if (effectiveSeeing < 0.5f)
78 effectiveSeeing = 0.5f;
81 int const boxsizey =
static_cast<int>(3.0f * effectiveSeeing / m_ImageScaleY) + 1;
83 float const sigma = effectiveSeeing / (2.0f * std::sqrt(2.0f * std::log(2.0f)));
84 float const norm = 1.0f / (sigma * std::sqrt(2.0f *
float(
M_PI)));
85 float const inv2sig2 = 1.0f / (2.0f * sigma * sigma);
91 float const fracX = x -
static_cast<int>(x);
92 float const fracY = y -
static_cast<int>(y);
94 for (
int sy = -boxsizey; sy <= boxsizey; sy++)
96 for (
int sx = -boxsizey; sx <= boxsizey; sx++)
98 float const ddx = m_ImageScaleX * (sx - fracX);
99 float const ddy = m_ImageScaleY * (sy - fracY);
100 float const dc2 = ddx * ddx + ddy * ddy;
101 float const fa = norm * std::exp(-dc2 * inv2sig2);
102 int const fp =
static_cast<int>(fa * totalFlux);
106 static_cast<int>(x) + sx,
107 static_cast<int>(y) + sy, fp) != 0)
120 int const nx = chip->
getSubW();
121 int const ny = chip->
getSubH();
124 for (
int y = 0; y < ny; y++)
125 for (
int x = 0; x < nx; x++)
127 int newval =
static_cast<int>(buf[y * nx + x]) + bias + (random() % maxNoise);
128 if (newval > m_Cfg.maxVal)
129 newval = m_Cfg.maxVal;
130 buf[y * nx + x] =
static_cast<uint16_t
>(newval);
135 float hotRate, uint32_t seed)
137 std::vector<SensorDefect> defects;
138 if (xres <= 0 || yres <= 0)
141 uint64_t
const total =
static_cast<uint64_t
>(xres) * yres;
142 hotCount = std::max(0, hotCount);
143 coldCount = std::max(0, coldCount);
144 if (
static_cast<uint64_t
>(hotCount) + coldCount > total)
146 hotCount =
static_cast<int>(std::min<uint64_t>(hotCount, total));
147 coldCount =
static_cast<int>(total - hotCount);
152 std::mt19937 rng(seed);
153 std::unordered_set<uint64_t> used;
154 defects.reserve(hotCount + coldCount);
156 auto const pickFree = [&]()
162 uint64_t
const high = rng();
163 uint64_t
const low = rng();
164 index = ((high << 32) | low) % total;
166 while (!used.insert(index).second);
170 for (
int i = 0; i < hotCount + coldCount; i++)
172 uint64_t
const index = pickFree();
174 defect.
x =
static_cast<int>(index % xres);
175 defect.
y =
static_cast<int>(index / xres);
176 defect.
cold = (i >= hotCount);
178 defect.
rate = hotRate * (0.5f +
static_cast<float>(rng()) / 4294967296.0f);
179 defects.push_back(defect);
188 int const subX = chip->
getSubX();
189 int const subY = chip->
getSubY();
190 int const nx = chip->
getSubW();
191 int const ny = chip->
getSubH();
194 for (
const auto &defect : defects)
196 int const x = defect.x - subX;
197 int const y = defect.y - subY;
198 if (x < 0 || x >= nx || y < 0 || y >= ny)
201 uint16_t &pixel = buf[y * nx + x];
204 pixel =
static_cast<uint16_t
>(std::max(0, bias / 2));
208 double const value =
static_cast<double>(pixel) +
static_cast<double>(defect.rate) * exposure_s;
209 pixel =
static_cast<uint16_t
>(std::min(value,
static_cast<double>(m_Cfg.maxVal)));
213void SkyRenderer::drawSkyGlow(
INDI::CCDChip *chip,
float exp_s)
215 float const skyflux =
static_cast<float>(flux(m_Cfg.
skyGlow)) * exp_s;
217 int const nwidth = chip->
getSubW();
218 int const nheight = chip->
getSubH();
222 float const vig = std::min(nwidth, nheight) * m_ImageScaleX;
223 float const invVig2 = 1.0f / (vig * vig);
225 for (
int y = 0; y < nheight; y++)
227 float const sy = nheight / 2.0f - y;
229 for (
int x = 0; x < nwidth; x++)
231 float const sx = nwidth / 2.0f - x;
233 float const dc2 = sx * sx * m_ImageScaleX * m_ImageScaleX
234 + sy * sy * m_ImageScaleY * m_ImageScaleY;
236 float const fa = std::exp(-2.0f * 0.7f * dc2 * invVig2);
238 float fp = (pt[0] + skyflux) * fa;
240 if (fp > m_Cfg.
maxVal) fp =
static_cast<float>(m_Cfg.
maxVal);
241 if (fp < pt[0]) fp = pt[0];
242 if (fp > m_MaxPix) m_MaxPix =
static_cast<int>(fp);
243 if (fp < m_MinPix) m_MinPix =
static_cast<int>(fp);
245 pt[0] =
static_cast<uint16_t
>(fp);
254 double dec_j2000_deg,
255 double focal_length_mm,
259 double minSearchRadiusArcmin)
265 m_ImageScaleX =
static_cast<float>((chip->
getPixelSizeX() / focal_length_mm) * 206.3);
266 m_ImageScaleY =
static_cast<float>((chip->
getPixelSizeY() / focal_length_mm) * 206.3);
269 double const theta = rotation_deg * (
M_PI / 180.0);
270 double const pprx = focal_length_mm / chip->
getPixelSizeX() * 1000.0;
271 double const ppry = focal_length_mm / chip->
getPixelSizeY() * 1000.0;
273 double const pa = pprx * std::cos(theta);
274 double const pb = ppry * std::sin(theta);
275 double const pd = -pprx * std::sin(theta);
276 double const pe = ppry * std::cos(theta);
277 double const pc = chip->
getXRes() / 2.0;
278 double const pf = chip->
getYRes() / 2.0;
279 double const ccdW = chip->
getXRes();
282 double const rar = ra_j2000_deg * (
M_PI / 180.0);
283 double const decr = dec_j2000_deg * (
M_PI / 180.0);
286 double radius = std::sqrt(
287 m_ImageScaleX * m_ImageScaleX * chip->
getXRes() / 2.0 * chip->
getXRes() / 2.0 +
288 m_ImageScaleY * m_ImageScaleY * chip->
getYRes() / 2.0 * chip->
getYRes() / 2.0) / 60.0;
289 if (minSearchRadiusArcmin > 0 && radius < minSearchRadiusArcmin)
290 radius = minSearchRadiusArcmin;
293 double lookuplimit = m_Cfg.limitingMag;
308 snprintf(gsccmd,
sizeof(gsccmd),
309 "gsc -c %8.6f %+8.6f -r %4.1f -m 0 %4.2f -n 3000",
315 FILE *pp = popen(gsccmd,
"r");
320 while (fgets(line,
sizeof(line), pp) !=
nullptr)
322 char id[20], plate[6], ob[6];
323 float ra,
dec, pose, mag, mage, dist;
326 int rc = sscanf(line,
"%10s %f %f %f %f %f %d %d %4s %2s %f %d",
327 id, &
ra, &
dec, &pose, &mag, &mage,
328 &band, &c, plate, ob, &dist, &dir);
332 double const srar =
ra * (
M_PI / 180.0);
333 double const sdecr =
dec * (
M_PI / 180.0);
335 double const denom = cos(decr) * cos(sdecr) * cos(srar - rar)
336 + sin(decr) * sin(sdecr);
340 double const sx = cos(sdecr) * sin(srar - rar) / denom;
341 double const sy = (sin(decr) * cos(sdecr) * cos(srar - rar)
342 - cos(decr) * sin(sdecr)) / denom;
345 double const ccdx = ccdW - (pa * sx + pb * sy + pc);
346 double const ccdy = pd * sx + pe * sy + pf;
349 static_cast<float>(ccdx),
350 static_cast<float>(ccdy),
361 drawSkyGlow(chip, exposure_s);
The CCDChip class provides functionality of a CCD Chip within a CCD.
int getSubX() const
getSubX Get the starting left coordinates (X) of the frame.
int getSubY() const
getSubY Get the starting top coordinates (Y) of the frame.
float getPixelSizeY() const
getPixelSizeY Get vertical pixel size in microns.
int getSubH() const
getSubH Get the height of the frame
float getPixelSizeX() const
getPixelSizeX Get horizontal pixel size in microns.
int getXRes() const
getXRes Get the horizontal resolution in pixels of the CCD Chip.
uint8_t * getFrameBuffer()
getFrameBuffer Get raw frame buffer of the CCD chip.
int getSubW() const
getSubW Get the width of the frame
int getFrameBufferSize() const
getFrameBufferSize Get allocated frame buffer size to hold the CCD image frame.
int getYRes() const
Get the vertical resolution in pixels of the CCD Chip.
void applyDefects(INDI::CCDChip *chip, const std::vector< SensorDefect > &defects, float exposure_s, int bias) const
int renderFrame(INDI::CCDChip *chip, double ra_j2000_deg, double dec_j2000_deg, double focal_length_mm, double rotation_deg, float exposure_s, bool renderStars=true, double minSearchRadiusArcmin=0)
void applyReadoutNoise(INDI::CCDChip *chip, int bias, int maxNoise)
int drawImageStar(INDI::CCDChip *, float mag, float x, float y, float exp_s)
static std::vector< SensorDefect > generateDefects(int xres, int yres, int hotCount, int coldCount, float hotRate, uint32_t seed=1)
double range360(double r)
range360 Limits an angle to be between 0-360 degrees.
double rangeDec(double decdegrees)
rangeDec Limits declination value to be in -90 to 90 range.
Implementations for common driver routines.