16084
|
1 |
#pragma once
|
|
2 |
|
|
3 |
#include <QObject>
|
|
4 |
#include <QPainter>
|
|
5 |
#include <QQmlEngine>
|
|
6 |
#include <QTemporaryDir>
|
|
7 |
|
|
8 |
enum PrimitiveType { Polygon, Circle };
|
|
9 |
|
|
10 |
struct Primitive {
|
|
11 |
PrimitiveType type;
|
|
12 |
QPen pen;
|
|
13 |
QBrush brush;
|
|
14 |
QPointF origin;
|
|
15 |
QList<QPointF> points; // polygon
|
|
16 |
double radius1{}, radius2{}, rotation{}; // ellipse
|
|
17 |
|
|
18 |
explicit Primitive(QSizeF size, const QList<QColor>& palette);
|
|
19 |
double cost() const;
|
|
20 |
};
|
|
21 |
|
|
22 |
struct Solution {
|
|
23 |
QList<Primitive> primitives;
|
|
24 |
double fitness;
|
|
25 |
QSizeF size;
|
16085
|
26 |
QString fileName;
|
16084
|
27 |
|
|
28 |
explicit Solution(QSizeF size, const QList<QColor>& palette);
|
16085
|
29 |
void calculateFitness(const QImage& target);
|
|
30 |
void render(const QString& fileName);
|
16084
|
31 |
double cost() const;
|
16085
|
32 |
void mutate(const QList<QColor>& palette);
|
|
33 |
void crossover(Solution &other);
|
16084
|
34 |
};
|
|
35 |
|
|
36 |
class Tracer : public QObject {
|
|
37 |
Q_OBJECT
|
|
38 |
QML_ELEMENT
|
|
39 |
|
|
40 |
Q_PROPERTY(QList<QColor> palette READ palette WRITE setPalette NOTIFY
|
|
41 |
paletteChanged FINAL)
|
|
42 |
Q_PROPERTY(
|
|
43 |
double bestSolution READ bestSolution NOTIFY bestSolutionChanged FINAL)
|
|
44 |
Q_PROPERTY(QStringList solutions READ solutions NOTIFY solutionsChanged FINAL)
|
|
45 |
|
|
46 |
public:
|
|
47 |
explicit Tracer(QObject *parent = nullptr);
|
|
48 |
|
|
49 |
QList<QColor> palette() const;
|
|
50 |
void setPalette(const QList<QColor>& newPalette);
|
|
51 |
|
|
52 |
double bestSolution() const;
|
|
53 |
|
|
54 |
Q_INVOKABLE void start(const QString& fileName);
|
|
55 |
Q_INVOKABLE void step();
|
|
56 |
|
|
57 |
QStringList solutions() const;
|
|
58 |
|
|
59 |
Q_SIGNALS:
|
|
60 |
void paletteChanged();
|
|
61 |
void bestSolutionChanged();
|
|
62 |
void solutionsChanged();
|
|
63 |
|
|
64 |
private:
|
|
65 |
QList<QColor> palette_;
|
|
66 |
double bestSolution_;
|
|
67 |
QStringList solutions_;
|
|
68 |
QList<Solution> generation_;
|
|
69 |
QTemporaryDir tempDir_;
|
16085
|
70 |
QImage referenceImage_;
|
16084
|
71 |
|
|
72 |
QString newFileName();
|
|
73 |
};
|