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