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