-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_m51_mosaic.cpp
More file actions
343 lines (265 loc) · 11.6 KB
/
main_m51_mosaic.cpp
File metadata and controls
343 lines (265 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// main_m51_mosaic_simple.cpp - Simple 3x3 grid version
#include <QApplication>
#include <QDebug>
#include <QTimer>
#include <QDir>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrl>
#include <QNetworkRequest>
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include <QFile>
#include "ProperHipsClient.h"
class M51MosaicCreator : public QObject {
Q_OBJECT
public:
explicit M51MosaicCreator(QObject *parent = nullptr);
void createSimpleMosaic(SkyPosition);
private slots:
void onTileDownloaded();
void processNextTile();
void assembleFinalMosaic();
private:
ProperHipsClient* m_hipsClient;
QNetworkAccessManager* m_networkManager;
// Simple tile structure
struct SimpleTile {
int gridX, gridY;
long long healpixPixel;
QString filename;
QString url;
QImage image;
bool downloaded;
};
QList<SimpleTile> m_tiles;
int m_currentTileIndex;
QString m_outputDir;
QDateTime m_downloadStartTime;
void createTileGrid(SkyPosition);
void downloadTile(int tileIndex);
void saveProgressReport();
};
M51MosaicCreator::M51MosaicCreator(QObject *parent) : QObject(parent) {
m_hipsClient = new ProperHipsClient(this);
m_networkManager = new QNetworkAccessManager(this);
m_currentTileIndex = 0;
// Create output directory
m_outputDir = "m51_mosaic_tiles";
QDir().mkpath(m_outputDir);
if (false) qDebug() << "=== M51 Simple Mosaic Creator ===";
if (false) qDebug() << "Just placing tiles in a 3x3 grid - no fancy coordinate stuff!";
}
void M51MosaicCreator::createSimpleMosaic(SkyPosition pos) {
if (false) qDebug() << "\n=== Creating Simple Mosaic ===";
createTileGrid(pos);
if (false) qDebug() << QString("\nStarting download of %1 tiles...").arg(m_tiles.size());
m_currentTileIndex = 0;
processNextTile();
}
void M51MosaicCreator::createTileGrid(SkyPosition pos) {
m_tiles.clear();
int order = 8;
// Test M51 position with working survey
long long m51Pixel = m_hipsClient->calculateHealPixel(pos, order);
QList<QList<long long>> pgrid = m_hipsClient->createProper3x3Grid(m51Pixel, order);
if (false) qDebug() << "Creating 3×3 tile grid:";
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
SimpleTile tile;
tile.gridX = x;
tile.gridY = y;
tile.healpixPixel = pgrid[y][x];
if (false) qDebug() << "pgrid: " << pgrid[y][x];
tile.downloaded = false;
// Build filename and URL directly from HEALPix pixel
tile.filename = QString("%1/simple_tile_%2_%3_pixel%4.jpg")
.arg(m_outputDir).arg(x).arg(y).arg(tile.healpixPixel);
int dir = (tile.healpixPixel / 10000) * 10000;
tile.url = QString("http://alasky.u-strasbg.fr/DSS/DSSColor/Norder%1/Dir%2/Npix%3.jpg")
.arg(order).arg(dir).arg(tile.healpixPixel);
if (tile.healpixPixel == 176440) {
if (false) qDebug() << QString(" Grid(%1,%2): HEALPix %3 ★ M51 TILE! ★")
.arg(x).arg(y).arg(tile.healpixPixel);
} else {
if (false) qDebug() << QString(" Grid(%1,%2): HEALPix %3")
.arg(x).arg(y).arg(tile.healpixPixel);
}
m_tiles.append(tile);
}
}
if (false) qDebug() << QString("Created simple %1 tile grid").arg(m_tiles.size());
}
void M51MosaicCreator::processNextTile() {
if (m_currentTileIndex >= m_tiles.size()) {
assembleFinalMosaic();
return;
}
downloadTile(m_currentTileIndex);
}
void M51MosaicCreator::downloadTile(int tileIndex) {
if (tileIndex >= m_tiles.size()) return;
const SimpleTile& tile = m_tiles[tileIndex];
if (false) qDebug() << QString("Downloading tile %1/%2: Grid(%3,%4) HEALPix %5")
.arg(tileIndex + 1).arg(m_tiles.size())
.arg(tile.gridX).arg(tile.gridY)
.arg(tile.healpixPixel);
if (false) qDebug() << QString("URL: %1").arg(tile.url);
// Create network request
QNetworkRequest request(QUrl(tile.url));
request.setHeader(QNetworkRequest::UserAgentHeader, "M51SimpleMosaicCreator/1.0");
request.setRawHeader("Accept", "image/*");
m_downloadStartTime = QDateTime::currentDateTime();
QNetworkReply* reply = m_networkManager->get(request);
// Store tile index in reply
reply->setProperty("tileIndex", tileIndex);
connect(reply, &QNetworkReply::finished, this, &M51MosaicCreator::onTileDownloaded);
// Set timeout
QTimer::singleShot(15000, reply, &QNetworkReply::abort);
}
void M51MosaicCreator::onTileDownloaded() {
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (!reply) return;
int tileIndex = reply->property("tileIndex").toInt();
if (tileIndex >= m_tiles.size()) {
reply->deleteLater();
return;
}
SimpleTile& tile = m_tiles[tileIndex];
if (reply->error() == QNetworkReply::NoError) {
// Read image data
QByteArray imageData = reply->readAll();
// Load image
tile.image.loadFromData(imageData);
if (!tile.image.isNull()) {
// Save image to file
bool saved = tile.image.save(tile.filename);
tile.downloaded = true;
qint64 downloadTime = m_downloadStartTime.msecsTo(QDateTime::currentDateTime());
if (false) qDebug() << QString("✅ Tile %1/%2 downloaded: %3ms, %4 bytes, %5x%6 pixels%7")
.arg(tileIndex + 1).arg(m_tiles.size())
.arg(downloadTime)
.arg(imageData.size())
.arg(tile.image.width()).arg(tile.image.height())
.arg(saved ? ", saved" : ", save failed");
} else {
if (false) qDebug() << QString("❌ Tile %1/%2 - invalid image data")
.arg(tileIndex + 1).arg(m_tiles.size());
}
} else {
if (false) qDebug() << QString("❌ Tile %1/%2 download failed: %3")
.arg(tileIndex + 1).arg(m_tiles.size())
.arg(reply->errorString());
}
reply->deleteLater();
m_currentTileIndex++;
// Small delay between downloads
QTimer::singleShot(500, this, &M51MosaicCreator::processNextTile);
}
void M51MosaicCreator::assembleFinalMosaic() {
if (false) qDebug() << "\n=== Assembling Simple M51 Mosaic ===";
int successfulTiles = 0;
for (const SimpleTile& tile : m_tiles) {
if (tile.downloaded && !tile.image.isNull()) {
successfulTiles++;
}
}
if (false) qDebug() << QString("Downloaded %1/%2 tiles").arg(successfulTiles).arg(m_tiles.size());
if (successfulTiles == 0) {
if (false) qDebug() << "❌ No tiles downloaded successfully";
QTimer::singleShot(1000, qApp, &QApplication::quit);
return;
}
// Create simple 3x3 mosaic: 3*512 = 1536 pixels
int tileSize = 512;
int mosaicSize = 3 * tileSize; // 1536x1536
QImage finalMosaic(mosaicSize, mosaicSize, QImage::Format_RGB32);
finalMosaic.fill(Qt::black);
QPainter painter(&finalMosaic);
int tilesPlaced = 0;
if (false) qDebug() << "Placing tiles in simple grid:";
for (const SimpleTile& tile : m_tiles) {
if (!tile.downloaded || tile.image.isNull()) {
if (false) qDebug() << QString(" Skipping tile %1,%2 - not downloaded").arg(tile.gridX).arg(tile.gridY);
continue;
}
// Simple placement: gridX * 512, gridY * 512
int pixelX = tile.gridX * tileSize;
int pixelY = tile.gridY * tileSize;
// Draw the tile
painter.drawImage(pixelX, pixelY, tile.image);
tilesPlaced++;
if (tile.healpixPixel == 176440) {
if (false) qDebug() << QString(" ✅ PLACED M51 TILE (%1,%2) at pixel (%3,%4)")
.arg(tile.gridX).arg(tile.gridY).arg(pixelX).arg(pixelY);
} else {
if (false) qDebug() << QString(" ✅ Placed tile (%1,%2) at pixel (%3,%4)")
.arg(tile.gridX).arg(tile.gridY).arg(pixelX).arg(pixelY);
}
}
// Add simple crosshairs at M51 location (center of middle tile)
painter.setPen(QPen(Qt::yellow, 3));
int m51X = 1 * tileSize + tileSize/2; // Center of grid position (1,1)
int m51Y = 1 * tileSize + tileSize/2;
// Draw crosshairs
painter.drawLine(m51X - 30, m51Y, m51X + 30, m51Y);
painter.drawLine(m51X, m51Y - 30, m51X, m51Y + 30);
// Add label
painter.setPen(QPen(Qt::yellow, 1));
painter.setFont(QFont("Arial", 14, QFont::Bold));
painter.drawText(m51X + 40, m51Y - 10, "M51");
painter.end();
// Save final mosaic
QString mosaicFilename = QString("%1/m51_simple_mosaic_3x3.png").arg(m_outputDir);
bool saved = finalMosaic.save(mosaicFilename);
if (false) qDebug() << QString("\n🖼️ Simple mosaic complete!");
if (false) qDebug() << QString("📁 Size: %1×%2 pixels (%3 tiles placed)")
.arg(mosaicSize).arg(mosaicSize).arg(tilesPlaced);
if (false) qDebug() << QString("📁 Saved to: %1 (%2)")
.arg(mosaicFilename).arg(saved ? "SUCCESS" : "FAILED");
// Also save a smaller preview
QString previewFilename = QString("%1/m51_simple_preview.jpg").arg(m_outputDir);
QImage preview = finalMosaic.scaled(512, 512, Qt::KeepAspectRatio, Qt::SmoothTransformation);
preview.save(previewFilename);
if (false) qDebug() << QString("📁 Preview: %1").arg(previewFilename);
saveProgressReport();
if (false) qDebug() << "\n🎯 SIMPLE M51 MOSAIC COMPLETE!";
if (false) qDebug() << "✅ M51 should be clearly visible in the center tile with crosshairs";
QTimer::singleShot(2000, qApp, &QApplication::quit);
}
void M51MosaicCreator::saveProgressReport() {
QString reportFile = QString("%1/simple_mosaic_report.txt").arg(m_outputDir);
QFile file(reportFile);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
if (false) qDebug() << "Could not save progress report";
return;
}
QTextStream out(&file);
out << "M51 Simple Mosaic Report\n";
out << "Generated: " << QDateTime::currentDateTime().toString() << "\n\n";
out << "Simple 3x3 Grid Layout:\n";
out << "Grid_X,Grid_Y,HEALPix_Pixel,Downloaded,ImageSize,Filename\n";
for (const SimpleTile& tile : m_tiles) {
out << QString("%1,%2,%3,%4,%5x%6,%7\n")
.arg(tile.gridX).arg(tile.gridY)
.arg(tile.healpixPixel)
.arg(tile.downloaded ? "YES" : "NO")
.arg(tile.image.width()).arg(tile.image.height())
.arg(tile.filename);
}
file.close();
if (false) qDebug() << "Report saved:" << reportFile;
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
SkyPosition argpos = {argc > 4 ? atof(argv[1]) : 0.0, argc > 4 ? atof(argv[2]) : 0.0, argc > 4 ? argv[3] : "", argc > 4 ? argv[4] : ""};
SkyPosition dfltpos = {202.4695833, 47.1951667, "M51", "Whirlpool Galaxy"};
SkyPosition pos = argc > 4 ? argpos : dfltpos;
if (false) qDebug() << "Simple Mosaic Creator";
if (false) qDebug() << "No fancy coordinates - just a simple 3x3 grid!\n";
M51MosaicCreator creator;
creator.createSimpleMosaic(pos);
return app.exec();
}
#include "main_m51_mosaic.moc"