SnakeWorker.cc 2 KB
Newer Older
1
#include "SnakeWorker.h"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
2 3 4 5 6 7 8
#include <QGeoCoordinate>


SnakeWorker::SnakeWorker(QObject *parent) : QThread(parent)
  , _lineDistance       (3) // meter
  , _minTransectLength  (2) // meter
{
9
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
10

11 12
SnakeWorker::~SnakeWorker()
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
13 14 15 16 17 18 19
}

void SnakeWorker::setScenario(const Scenario &scenario)
{
    _scenario = scenario;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
20
void SnakeWorker::setProgress(const QVector<int> &progress)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
21
{
22
    _progress.clear();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
23
    for (auto p : progress) {
24
        assert(p >= -1 && p <= 100);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
        _progress.push_back(p);
    }
}

void SnakeWorker::setLineDistance(double lineDistance)
{
    assert(lineDistance > 0);
    _lineDistance = lineDistance;
}

void SnakeWorker::setMinTransectLength(double minTransectLength)
{
    assert(minTransectLength > 0);
    _minTransectLength = minTransectLength;
}

41 42 43 44 45
const WorkerResult_t &SnakeWorker::getResult() const
{
    return _result;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
46 47
void SnakeWorker::run()
{
48 49 50 51 52 53
    // Reset _result struct.
    _result.success = false;
    _result.errorMessage = "";
    _result.waypoints.clear();
    _result.returnPathIdx.clear();
    _result.arrivalPathIdx.clear();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
54 55 56 57 58 59 60 61 62

    FlightPlan flightplan;
    flightplan.setScenario(_scenario);
    flightplan.setProgress(_progress);

    // Trying to generate flight plan.
    if ( !flightplan.generate(_lineDistance, _minTransectLength) ){
        // error
        for (auto c : flightplan.errorString){
63
            _result.errorMessage.push_back(QChar(c));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
64 65 66
        }
    } else {
        //success!!!
67
        _result.success = true;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
68 69 70 71

        // Fill result struct.
        auto waypoints = flightplan.getWaypoints();
        for (auto vertex : waypoints){
72
            _result.waypoints.append(QVariant::fromValue(QGeoCoordinate(vertex[0], vertex[1], 0)));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
73 74 75 76
        }

        auto arrivalPathIdx = flightplan.getArrivalPathIdx();
        for (auto idx : arrivalPathIdx){
77
            _result.arrivalPathIdx.append(idx);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
78 79 80 81
        }

        auto returnPathIdx = flightplan.getReturnPathIdx();
        for (auto idx : returnPathIdx){
82
            _result.returnPathIdx.append(idx);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
83 84 85 86
        }

    }
}