File indexing completed on 2024-04-28 15:09:13

0001 /*
0002     SPDX-FileCopyrightText: 2022 Hy Murveit <hy@murveit.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <stellarsolver.h>
0010 
0011 #include <QObject>
0012 #include <QString>
0013 #include <QTimer>
0014 #include <QFutureWatcher>
0015 #include <mutex>
0016 #include <memory>
0017 #include <QSharedPointer>
0018 
0019 #ifdef _WIN32
0020 #undef Unused
0021 #endif
0022 
0023 class FITSData;
0024 
0025 // This is a wrapper to make calling the StellarSolver solver a bit simpler.
0026 // Must supply the imagedata and stellar solver parameters
0027 // and connect to the signals. Remote solving not supported.
0028 class SolverUtils : public QObject
0029 {
0030         Q_OBJECT
0031 
0032     public:
0033         SolverUtils(const SSolver::Parameters &parameters, double timeoutSeconds = 15,
0034                     SSolver::ProcessType type = SSolver::SOLVE);
0035         ~SolverUtils();
0036 
0037         void runSolver(const QSharedPointer<FITSData> &data);
0038         void runSolver(const QString &filename);
0039         SolverUtils &useScale(bool useIt, double scaleLowArcsecPerPixel, double scaleHighArcsecPerPixel);
0040         SolverUtils &usePosition(bool useIt, double raDegrees, double decDegrees);
0041         bool isRunning() const;
0042         void abort();
0043 
0044         void setHealpix(int indexToUse = -1, int healpixToUse = -1);
0045         void getSolutionHealpix(int *indexUsed, int *healpixUsed) const;        
0046 
0047         const FITSImage::Background &getBackground() const
0048         {
0049             // Better leak than crash. Warn?
0050             if (!m_StellarSolver) return *new FITSImage::Background();
0051             return m_StellarSolver->getBackground();
0052         }
0053         const QList<FITSImage::Star> &getStarList() const
0054         {
0055             // Better leak than crash. Warn?
0056             if (!m_StellarSolver) return *new QList<FITSImage::Star>();
0057             return m_StellarSolver->getStarList();
0058         }
0059         int getNumStarsFound() const
0060         {
0061             if (!m_StellarSolver) return 0;
0062             return m_StellarSolver->getNumStarsFound();
0063         };
0064 
0065         // We don't trust StellarSolver's mutli-processing algorithm MULTI_DEPTHS which is used
0066         // with multiAlgorithm==MULTI_AUTO && use_scale && !use_position. This disables that.
0067         static void patchMultiAlgorithm(StellarSolver *solver);
0068 
0069     signals:
0070         void done(bool timedOut, bool success, const FITSImage::Solution &solution, double elapsedSeconds);
0071         void newLog(const QString &logText);
0072 
0073     private:
0074         void solverDone();
0075         void solverTimeout();
0076         void executeSolver();
0077         void prepareSolver();
0078 
0079         std::unique_ptr<StellarSolver> m_StellarSolver;
0080 
0081         qint64 m_StartTime;
0082         QTimer m_SolverTimer;
0083         // Copy of parameters
0084         SSolver::Parameters m_Parameters;
0085         // Solver timeout in milliseconds.
0086         const uint32_t m_TimeoutMilliseconds {0};
0087         // Temporary file name in case of external solver.
0088         QString m_TemporaryFilename;
0089         QFutureWatcher<bool> m_Watcher;
0090         double m_ScaleLowArcsecPerPixel {0}, m_ScaleHighArcsecPerPixel {0};
0091 
0092         QSharedPointer<FITSData> m_ImageData;
0093 
0094         int m_IndexToUse { -1 };
0095         int m_HealpixToUse { -1 };
0096 
0097         bool m_UseScale { false };
0098         bool m_UsePosition { false };
0099         double m_raDegrees { 0.0 };
0100         double m_decDegrees { 0.0 };
0101 
0102         SSolver::ProcessType m_Type = SSolver::SOLVE;
0103         std::mutex deleteSolverMutex;
0104 };
0105