Add sky grid painting

This commit is contained in:
2025-05-26 15:50:37 +02:00
parent 8b498bbe73
commit c01f2e328a
4 changed files with 159 additions and 1 deletions
+138
View File
@@ -26,6 +26,8 @@ struct RawImageType
QOpenGLTexture::PixelType dataType;
};
void prepareGrid(WCSDataT *wcs, uint32_t w, uint32_t h, QPainterPath &grid, QPainterPath &text);
RawImageType getRawImageType(const RawImage *img)
{
RawImageType type;
@@ -180,6 +182,10 @@ void ImageWidgetGL::setImage(std::shared_ptr<RawImage> image, int index)
void ImageWidgetGL::setWCS(std::shared_ptr<WCSDataT> wcs)
{
m_wcs = wcs;
m_grid.clear();
m_text.clear();
if(m_drawGrid)
prepareGrid(m_wcs.get(), m_imgWidth, m_imgHeight, m_grid, m_text);
}
void ImageWidgetGL::zoom(int zoom, const QPointF &mousePos)
@@ -496,6 +502,119 @@ void swPaint(std::shared_ptr<RawImage> &rawImage, float dx, float dy, float scal
painter.drawImage(0, 0, img);
}
void prepareGrid(WCSDataT *wcs, uint32_t w, uint32_t h, QPainterPath &grid, QPainterPath &text)
{
if(!wcs)return;
double minRa, maxRa, minDec, maxDec, crVal1, crVal2;
wcs->calculateBounds(minRa, maxRa, minDec, maxDec, crVal1, crVal2);
double raStep = 15;
double decStep = 15;
double raRange = maxRa - minRa;
double decRange = maxDec - minDec;
const QVector<double> raSteps = {15, 5, 2.5, 1.25, 0.25, 20/240.0, 10/240.0, 5/240.0, 1/240.0};
const QVector<double> decSteps = {20, 10, 5, 2, 1, 20/60.0, 10/60.0, 5/60.0, 2/60.0, 1/60.0, 20/3600.0, 10/3600.0, 5/3600.0, 2/3600.0, 1/3600.0};
for(double ra : raSteps)
{
if(ra * 5 <= raRange)
{
raStep = ra;
break;
}
}
for(double dec : decSteps)
{
if(dec * 5 <= decRange)
{
decStep = dec;
break;
}
}
minRa -= std::fmod(minRa, raStep);
minDec -= std::fmod(minDec, decStep);
if(minRa < 0)minRa -= raStep;
if(minDec < 0)minDec -= decStep;
QRectF clip(0, 0, w, h);
maxRa += raStep;
maxDec += decStep;
for(double ra = minRa; ra <= maxRa; ra += raStep)
{
QPointF p;
wcs->worldToPixel(SkyPoint(ra, minDec), p);
grid.moveTo(p);
for(double dec = minDec + decStep * 0.02; dec <= maxDec; dec += decStep * 0.02)
{
wcs->worldToPixel(SkyPoint(ra, dec), p);
grid.lineTo(p);
}
}
for(double dec = minDec; dec <= maxDec; dec += decStep)
{
QPointF p;
wcs->worldToPixel(SkyPoint(minRa, dec), p);
grid.moveTo(p);
for(double ra = minRa + raStep * 0.02; ra <= maxRa; ra += raStep * 0.02)
{
wcs->worldToPixel(SkyPoint(ra, dec), p);
grid.lineTo(p);
}
}
SkyPoint sp1, sp2,orig;
wcs->pixelToWorld(QPointF(-1, -1), orig);
sp1 = orig;
QFont font("Sans", 12);
for(uint32_t x = 0; x < w; x++)
{
QPointF p(x, 0);
if(!wcs->pixelToWorld(p, sp2))
break;
if(static_cast<int>(sp1.RA() / raStep) != static_cast<int>(sp2.RA() / raStep))
text.addText(p + QPointF(0, 14), font, std::abs(sp1.RA()) > std::abs(sp2.RA()) ? sp1.RAString() : sp2.RAString());
if(static_cast<int>(sp1.DEC() / decStep) != static_cast<int>(sp2.DEC() / decStep))
text.addText(p + QPointF(0, 14), font, std::abs(sp1.DEC()) > std::abs(sp2.DEC()) ? sp1.DECString() : sp2.DECString());
sp1 = sp2;
}
sp1 = orig;
for(uint32_t y = 0; y < h; y++)
{
QPointF p(0, y);
if(!wcs->pixelToWorld(p, sp2))
break;
if(static_cast<int>(sp1.RA() / raStep) != static_cast<int>(sp2.RA() / raStep))
text.addText(p + QPointF(2, 0), font, std::abs(sp1.RA()) > std::abs(sp2.RA()) ? sp1.RAString() : sp2.RAString());
if(static_cast<int>(sp1.DEC() / decStep) != static_cast<int>(sp2.DEC() / decStep))
text.addText(p + QPointF(2, 0), font, std::abs(sp1.DEC()) > std::abs(sp2.DEC()) ? sp1.DECString() : sp2.DECString());
sp1 = sp2;
}
}
void ImageWidgetGL::drawGrid(bool enable)
{
if(m_grid.elementCount() == 0)
prepareGrid(m_wcs.get(), m_imgWidth, m_imgHeight, m_grid, m_text);
if(enable != m_drawGrid)
{
m_drawGrid = enable;
update();
}
}
void ImageWidgetGL::paintGL()
{
float dx = m_dx;
@@ -613,6 +732,25 @@ void ImageWidgetGL::paintGL()
m_program->setUniformValue("colormapIdx", m_colormapIdx);
f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
m_vao->release();
if(m_drawGrid)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.setPen(QPen(Qt::yellow, 1.0 / m_scale));
QTransform tran;
tran.translate(-std::floor(dx), -std::floor(dy));
tran.scale(m_scale, m_scale);
painter.setTransform(tran);
painter.setClipRect(0, 0, m_imgWidth, m_imgHeight);
painter.drawPath(m_grid);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::yellow);
painter.drawPath(m_text);
}
}
}