RoutingThread.h 1.67 KB
Newer Older
1 2 3 4 5 6
#pragma once

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

7
#include "geometry/geometry.h"
8 9 10 11 12
#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Aux structs
struct TransectInfo {
  TransectInfo(size_t n, bool r) : index(n), reversed(r) {}
  size_t index;
  bool reversed;
};

struct Route {
  geometry::FLineString path;
  std::vector<TransectInfo> info;
};

typedef std::vector<Route>
    Solution; // Every route corresponds to one run/vehicle

struct RoutingResult {
  geometry::LineStringArray transects;
  std::vector<Solution> solutionVector;
31 32 33 34 35
  std::string errorString;
};

struct RoutingParameter {
  RoutingParameter() : numSolutions(1), numRuns(1) {}
36
  geometry::FPolygon safeArea;
37 38 39
  std::size_t numSolutions;
  std::size_t numRuns;
};
40

41 42 43 44 45 46 47 48 49
//!
//! \brief The CSWorker class
//! \note Don't call QThread::start, QThread::quit, etc. onyl use Worker
//! members!
class RoutingThread : public QThread {
  Q_OBJECT
  using Lock = std::unique_lock<std::mutex>;

public:
50 51
  using PtrRoutingData = std::shared_ptr<RoutingResult>;
  using Work = std::function<bool(geometry::LineStringArray &)>;
52 53 54 55 56 57 58

  RoutingThread(QObject *parent = nullptr);
  ~RoutingThread() override;

  bool calculating() const;

public slots:
59
  void route(const RoutingParameter &par, const Work &work);
60 61 62 63 64 65 66 67 68 69 70 71 72

signals:
  void result(PtrRoutingData pTransects);
  void calculatingChanged();

protected:
  void run() override;

private:
  mutable std::mutex _mutex;
  mutable std::condition_variable _cv;
  // Internal data
  RoutingParameter _par;
73
  Work _work; // transect worker
74 75 76 77 78
  // State
  std::atomic_bool _calculating;
  std::atomic_bool _stop;
  std::atomic_bool _restart;
};