|
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; |
|
26 |
|
27 explicit Solution(QSizeF size, const QList<QColor>& palette); |
|
28 void calculateFitness(const QImage& image); |
|
29 void render(const QString& fileName) const; |
|
30 double cost() const; |
|
31 }; |
|
32 |
|
33 class Tracer : public QObject { |
|
34 Q_OBJECT |
|
35 QML_ELEMENT |
|
36 |
|
37 Q_PROPERTY(QList<QColor> palette READ palette WRITE setPalette NOTIFY |
|
38 paletteChanged FINAL) |
|
39 Q_PROPERTY( |
|
40 double bestSolution READ bestSolution NOTIFY bestSolutionChanged FINAL) |
|
41 Q_PROPERTY(QStringList solutions READ solutions NOTIFY solutionsChanged FINAL) |
|
42 |
|
43 public: |
|
44 explicit Tracer(QObject *parent = nullptr); |
|
45 |
|
46 QList<QColor> palette() const; |
|
47 void setPalette(const QList<QColor>& newPalette); |
|
48 |
|
49 double bestSolution() const; |
|
50 |
|
51 Q_INVOKABLE void start(const QString& fileName); |
|
52 Q_INVOKABLE void step(); |
|
53 |
|
54 QStringList solutions() const; |
|
55 |
|
56 Q_SIGNALS: |
|
57 void paletteChanged(); |
|
58 void bestSolutionChanged(); |
|
59 void solutionsChanged(); |
|
60 |
|
61 private: |
|
62 QList<QColor> palette_; |
|
63 double bestSolution_; |
|
64 QStringList solutions_; |
|
65 QList<Solution> generation_; |
|
66 QTemporaryDir tempDir_; |
|
67 QImage image_; |
|
68 |
|
69 QString newFileName(); |
|
70 }; |