RoutingThread.h 1.29 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 15 16 17 18
struct RoutingData {
  snake::BoostLineString route;
  snake::Transects transects;
  std::vector<snake::TransectInfo> transectsInfo;
};

19 20 21 22
//!
//! \brief The CSWorker class
//! \note Don't call QThread::start, QThread::quit, etc. onyl use Worker
//! members!
23
class RoutingThread : public QThread {
24 25 26 27
  Q_OBJECT
  using Lock = std::unique_lock<std::mutex>;

public:
28 29
  using PtrRoutingData = QSharedPointer<RoutingData>;
  using Generator = std::function<bool(snake::Transects &)>;
30
  using Consumer = std::function<void(const RoutingData &)>;
31

32 33
  RoutingThread(QObject *parent = nullptr);
  ~RoutingThread() override;
34

35
  bool calculating() const;
36 37

public slots:
38
  void route(const snake::BoostPolygon &safeArea, const Generator &generator);
39 40

signals:
41
  void result(PtrRoutingData pTransects);
42 43 44 45 46 47 48 49 50
  void calculatingChanged();

protected:
  void run() override;

private:
  mutable std::mutex _mutex;
  mutable std::condition_variable _cv;
  // Internal data
51 52
  snake::BoostPolygon _safeArea;
  Generator _generator; // transect generator
53 54 55 56 57
  // State
  std::atomic_bool _calculating;
  std::atomic_bool _stop;
  std::atomic_bool _restart;
};