RoutingThread.h 1.48 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 16 17
  std::vector<snake::Route> routeVector;
  std::vector<snake::RouteInfo> routeInfoVector;
  std::string errorString;
18 19
};

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

public:
35 36
  using PtrRoutingData = QSharedPointer<RoutingData>;
  using Generator = std::function<bool(snake::Transects &)>;
37
  using Consumer = std::function<void(const RoutingData &)>;
38

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

42
  bool calculating() const;
43 44

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

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

protected:
  void run() override;

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