RoutingThread.h 1.42 KB
Newer Older
1 2 3 4 5 6 7 8 9
#pragma once

#include <QGeoCoordinate>
#include <QSharedPointer>
#include <QThread>

#include "snake.h"
#include <atomic>
#include <condition_variable>
10
#include <functional>
11 12
#include <mutex>

13 14
struct RoutingData {
  snake::Transects transects;
15
  std::vector<snake::Solution> solutionVector;
16
  std::string errorString;
17 18
};

19
struct RoutingParameter {
20
  RoutingParameter() : numSolutions(1), numRuns(1) {}
21
  snake::FPolygon safeArea;
22
  std::size_t numSolutions;
23 24
  std::size_t numRuns;
};
25 26 27 28
//!
//! \brief The CSWorker class
//! \note Don't call QThread::start, QThread::quit, etc. onyl use Worker
//! members!
29
class RoutingThread : public QThread {
30 31 32 33
  Q_OBJECT
  using Lock = std::unique_lock<std::mutex>;

public:
Valentin Platzgummer's avatar
Valentin Platzgummer committed
34
  using PtrRoutingData = shared_ptr<RoutingData>;
35
  using Generator = std::function<bool(snake::Transects &)>;
36
  using Consumer = std::function<void(const RoutingData &)>;
37

38 39
  RoutingThread(QObject *parent = nullptr);
  ~RoutingThread() override;
40

41
  bool calculating() const;
42 43

public slots:
44
  void route(const RoutingParameter &par, const Generator &generator);
45 46

signals:
47
  void result(PtrRoutingData pTransects);
48 49 50 51 52 53 54 55 56
  void calculatingChanged();

protected:
  void run() override;

private:
  mutable std::mutex _mutex;
  mutable std::condition_variable _cv;
  // Internal data
57
  RoutingParameter _par;
58
  Generator _generator; // transect generator
59 60 61 62 63
  // State
  std::atomic_bool _calculating;
  std::atomic_bool _stop;
  std::atomic_bool _restart;
};