2024-01-05 07:12:48 +08:00
|
|
|
#ifndef MULTITHREADED_THREAD_H
|
|
|
|
#define MULTITHREADED_THREAD_H
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
#include "jkqtplotter/jkqtpbaseplotter.h"
|
|
|
|
#include "jkqtplotter/graphs/jkqtplines.h"
|
|
|
|
#include "jkqtcommon/jkqtpmathtools.h"
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include <QRandomGenerator>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
class PlottingThread: public QThread {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2024-01-09 00:16:31 +08:00
|
|
|
inline PlottingThread(const QString& filenamepart, int plotindex, int NUM_GRAPHS, int NUM_DATAPOINTS, const QString& labeltemplate, QObject* parent):
|
2024-01-05 07:12:48 +08:00
|
|
|
QThread(parent),
|
|
|
|
m_plotindex(plotindex),
|
|
|
|
m_runtimeNanoseconds(0),
|
|
|
|
m_filenamepart(filenamepart),
|
|
|
|
m_filename(),
|
|
|
|
m_NUM_GRAPHS(NUM_GRAPHS),
|
2024-01-09 00:16:31 +08:00
|
|
|
m_NUM_DATAPOINTS(NUM_DATAPOINTS),
|
|
|
|
m_labeltemplate(labeltemplate)
|
2024-01-05 07:12:48 +08:00
|
|
|
{}
|
|
|
|
|
2024-01-09 00:16:31 +08:00
|
|
|
inline static QString plotAndSave(const QString& filenamepart, int plotIndex, int NUM_GRAPHS, int NUM_DATAPOINTS, const QString& labeltemplate, double* runtimeNanoseconds=nullptr) {
|
2024-01-05 07:12:48 +08:00
|
|
|
QElapsedTimer timer;
|
|
|
|
timer.start();
|
|
|
|
const QString filename=QDir(QDir::tempPath()).absoluteFilePath(QString("testimg_%1_%2.png").arg(filenamepart).arg(plotIndex));
|
|
|
|
JKQTBasePlotter plot(true);
|
|
|
|
|
|
|
|
const size_t colX=plot.getDatastore()->addLinearColumn(NUM_DATAPOINTS, 0, 10, "x");
|
|
|
|
QRandomGenerator rng;
|
|
|
|
for (int i=0; i<NUM_GRAPHS; i++) {
|
|
|
|
JKQTPXYLineGraph* g;
|
|
|
|
plot.addGraph(g=new JKQTPXYLineGraph(&plot));
|
|
|
|
g->setXColumn(colX);
|
2024-01-09 00:16:31 +08:00
|
|
|
g->setYColumn(plot.getDatastore()->addColumnCalculatedFromColumn(colX, [&](double x) { return cos(x+double(i+plotIndex)/8.0*JKQTPSTATISTICS_PI)+rng.generateDouble()*0.2-0.1;}));
|
|
|
|
g->setTitle(labeltemplate.arg(i+plotIndex+1));
|
2024-01-05 07:12:48 +08:00
|
|
|
g->setDrawLine(true);
|
|
|
|
g->setSymbolType(JKQTPNoSymbol);
|
|
|
|
|
|
|
|
}
|
2024-01-09 00:16:31 +08:00
|
|
|
plot.setPlotLabel(QString("Test Plot %1 (%2)").arg(plotIndex+1).arg(filenamepart));
|
|
|
|
plot.getXAxis()->setAxisLabel("x-axis $x$");
|
|
|
|
plot.getYAxis()->setAxisLabel("y-axis $f(x)$");
|
2024-01-05 07:12:48 +08:00
|
|
|
plot.zoomToFit();
|
|
|
|
plot.saveAsPixelImage(filename, false, "PNG");
|
|
|
|
|
|
|
|
if (runtimeNanoseconds) *runtimeNanoseconds=timer.nsecsElapsed();
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
inline double getRuntimeNanosends() const {
|
|
|
|
return m_runtimeNanoseconds;
|
|
|
|
}
|
|
|
|
inline QString getFilename() const {
|
|
|
|
return m_filename;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
inline virtual void run() {
|
2024-01-09 00:16:31 +08:00
|
|
|
m_filename=plotAndSave(m_filenamepart, m_plotindex, m_NUM_GRAPHS, m_NUM_DATAPOINTS, m_labeltemplate, &m_runtimeNanoseconds);
|
2024-01-05 07:12:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int m_plotindex;
|
|
|
|
double m_runtimeNanoseconds;
|
|
|
|
QString m_filename;
|
2024-01-09 00:16:31 +08:00
|
|
|
const QString m_filenamepart;
|
2024-01-05 07:12:48 +08:00
|
|
|
const int m_NUM_GRAPHS;
|
|
|
|
const int m_NUM_DATAPOINTS;
|
2024-01-09 00:16:31 +08:00
|
|
|
const QString m_labeltemplate;
|
2024-01-05 07:12:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MULTITHREADED_THREAD_H
|