WimaArea.cc 26.8 KB
Newer Older
1 2
#include "WimaArea.h"

3
/*!
4 5
 * \variable WimaArea::epsilonMeter
 * \brief The accuracy used for distance calculations (unit: m).
6
 */
7
const double WimaArea::epsilonMeter    = 1e-5;
8 9 10 11
/*!
 * \variable WimaArea::maxAltitudeName
 * \brief A string containing the name of the \c _maxAltitude member. Among other used for storing.
 */
12
const char* WimaArea::maxAltitudeName       = "maxAltitude";
13 14 15 16
/*!
 * \variable WimaArea::wimaAreaName
 * \brief A string containing the name of this \c WimaArea member. Among other used for storing.
 */
17
const char* WimaArea::wimaAreaName          = "WimaArea";
18 19 20 21
/*!
 * \variable WimaArea::areaTypeName
 * \brief A string containing \c {"AreaType"}. Among other used for stroing.
 */
22 23
const char* WimaArea::areaTypeName          = "AreaType";

24

25

26
// Constructors
27 28
WimaArea::WimaArea(QObject *parent)
    :  QGCMapPolygon (parent)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
29
{
30
    init();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
31 32
    _maxAltitude = 30;
}
33

Valentin Platzgummer's avatar
Valentin Platzgummer committed
34
WimaArea::WimaArea(const WimaArea &other, QObject *parent)
35
    : QGCMapPolygon (parent)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
36
{
37
    init();
38 39 40 41 42 43 44 45 46 47 48 49 50 51
    *this = other;
}

/*!
 *\fn WimaArea &WimaArea::operator=(const WimaArea &other)
 *
 * Assigns \a other to this \c WimaArea and returns a reference to this \c WimaArea.
 *
 * Copies only path and maximum altitude.
 */
WimaArea &WimaArea::operator=(const WimaArea &other)
{
    QGCMapPolygon::operator=(other);
    this->_maxAltitude = other.maxAltitude();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
52
    this->setPath(other.path());
53 54

    return *this;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
55
}
56

57 58 59 60 61 62 63
/*!
  \fn void WimaArea::setMaxAltitude(double altitude)

  Sets the \c _maxAltitude member to \a altitude and emits the signal \c maxAltitudeChanged()
  if \c _maxAltitude is not equal to altitude.
 */
void WimaArea::setMaxAltitude(double altitude)
64
{
65 66
    if ( altitude > 0 && qFuzzyCompare(altitude, _maxAltitude) ) {
        _maxAltitude = altitude;
67 68 69 70
        emit maxAltitudeChanged();
    }
}

71 72 73 74 75 76 77
/*!
 * \fn int WimaArea::getClosestVertexIndex(const QGeoCoordinate &coordinate) const
 * Returns the index of the vertex (element of the polygon path)
 * which has the least distance to \a coordinate.
 *
 * \sa QGeoCoordinate
 */
78
int WimaArea::getClosestVertexIndex(const QGeoCoordinate &coordinate) const
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
    if (this->count() == 0) {
        qWarning("Polygon count == 0!");
        return -1;
    }else if (this->count() == 1) {
        return 0;
    }else {
        int index = 0;
        double min_dist = coordinate.distanceTo(this->vertexCoordinate(index));
        for(int i = 1; i < this->count(); i++){
            double dist = coordinate.distanceTo(this->vertexCoordinate(i));
            if (dist < min_dist){
                min_dist = dist;
                index = i;
            }
        }

        return index;
    }
}

100 101 102 103 104 105
/*!
 * \fn  QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
 *  Returns the vertex of the polygon path with the least distance to \a coordinate.
 *
 * \sa QGeoCoordinate
 */
106
QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
107 108 109 110
{
    return this->vertexCoordinate(getClosestVertexIndex(coordinate));
}

111 112 113 114 115
/*!
 * \fn QGCMapPolygon WimaArea::toQGCPolygon(const WimaArea &area)
 * Converts the \c WimaArea \a area to \c QGCMapPolygon by copying the path only.
 */
QGCMapPolygon WimaArea::toQGCPolygon(const WimaArea &area)
116
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
117
    QGCMapPolygon qgcPoly;
118
    qgcPoly.setPath(area.path());
119

Valentin Platzgummer's avatar
Valentin Platzgummer committed
120
    return QGCMapPolygon(qgcPoly);
121 122
}

123 124 125 126
/*!
 * \fn QGCMapPolygon WimaArea::toQGCPolygon() const
 * Converts the calling \c WimaArea to \c QGCMapPolygon by copying the path only.
 */
127 128 129 130 131
QGCMapPolygon WimaArea::toQGCPolygon() const
{
    return toQGCPolygon(*this);
}

132
/*!
133
 * \fn bool WimaArea::join(WimaArea &area1, WimaArea &area2, WimaArea &joinedArea, QString &errorString)
134 135
 * Joins the areas \a area1 and \a area2 such that a \l {Simple Polygon} is created.
 * Stores the result inside \a joinedArea.
136
 * Stores error messages in \a errorString.
137 138 139 140
 * Returns \c true if the algorithm was able to join the areas; false else.
 * The algorithm will be able to join the areas, if either their edges intersect with each other,
 * or one area contains the other.
 */
141
bool WimaArea::join(WimaArea &area1, WimaArea &area2, WimaArea &joinedArea, QString &errorString)
142
{
143

144
        if (area1.count() >= 3 && area2.count() >= 3) {
145

146 147 148 149 150 151 152 153 154 155
            if ( isSelfIntersecting(area1) ) {
                errorString.append("Area 1 is self intersecting.\n");
                return false;
            }

            if ( isSelfIntersecting(area2) ) {
                errorString.append("Area 2 is self intersecting.\n");
                return false;
            }

156
            joinedArea.clear();
157

158 159
            area1.verifyClockwiseWinding();
            area2.verifyClockwiseWinding();
160

161 162
            WimaArea* walkerPoly    = &area1; // "walk" on this polygon towards higher indices
            WimaArea* crossPoly     = &area2; // check for crossings with this polygon while "walking"
163 164
                                             // and swicht to this polygon on a intersection,
                                             // continue to walk towards higher indices
165 166 167 168 169 170 171 172 173 174 175 176 177 178

            // begin with the first index which is not inside crosspoly, if all Vertices are inside crosspoly return crosspoly
            int startIndex = 0;
            bool crossContainsWalker = true;
            for (int i = 0; i < walkerPoly->count(); i++) {
                if ( !crossPoly->containsCoordinate(walkerPoly->vertexCoordinate(i)) ) {
                    crossContainsWalker = false;
                    startIndex = i;
                    break;
                }
            }


            if ( crossContainsWalker == true) {
179
                joinedArea.appendVertices(crossPoly->coordinateList());
180
                return true;
181 182 183 184 185
            }


            QGeoCoordinate currentVertex    = walkerPoly->vertexCoordinate(startIndex);
            QGeoCoordinate startVertex      = currentVertex;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
186
            // possible nextVertex (if no intersection between currentVertex and protoVertex with crossPoly)
187 188 189 190
            QGeoCoordinate protoNextVertex  = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(startIndex));

            int nextVertexIndex = walkerPoly->nextVertexIndex(startIndex);
            while (1) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
191
                //qDebug("nextVertexIndex: %i", nextVertexIndex);
192
                joinedArea.appendVertex(currentVertex);
193 194 195

                QGCMapPolyline walkerPolySegment;
                walkerPolySegment.appendVertex(currentVertex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
196
                walkerPolySegment.appendVertex(protoNextVertex);
197

198 199
                QList<QPair<int, int>> neighbourList;
                QList<QGeoCoordinate> intersectionList;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
200
                //qDebug("IntersectionList.size() on init: %i", intersectionList.size());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
201
                intersects(walkerPolySegment, *crossPoly, intersectionList, neighbourList);
202

Valentin Platzgummer's avatar
Valentin Platzgummer committed
203
                //qDebug("IntersectionList.size(): %i", intersectionList.size());
204

205 206
                if (intersectionList.size() >= 1) {
                    int minDistIndex = 0;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
207

208 209 210 211
                    if (intersectionList.size() > 1) {
                        double minDist = currentVertex.distanceTo(intersectionList.value(minDistIndex));
                        for (int i = 1; i < intersectionList.size(); i++) {
                            double currentDist = currentVertex.distanceTo(intersectionList.value(i));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
212

213 214 215 216 217 218
                            if ( minDist > currentDist ) {
                                minDist         = currentDist;
                                minDistIndex    = i;
                            }
                        }
                    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
219

Valentin Platzgummer's avatar
Valentin Platzgummer committed
220
                    //qDebug("MinDistIndex: %i", minDistIndex);
221 222
                    QGeoCoordinate protoCurrentVertex = intersectionList.value(minDistIndex);
                    // take numerical erros into account
223
                    if (protoCurrentVertex.distanceTo(currentVertex) > epsilonMeter) {
224 225 226 227 228 229 230 231 232
                        currentVertex                   = protoCurrentVertex;
                        QPair<int, int> neighbours      = neighbourList.value(minDistIndex);
                        protoNextVertex                 = crossPoly->vertexCoordinate(neighbours.second);
                        nextVertexIndex                 = neighbours.second;

                        // swap walker and cross poly
                        WimaArea* temp  = walkerPoly;
                        walkerPoly      = crossPoly;
                        crossPoly       = temp;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
233
                    } else {
234 235 236
                        currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                        protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                        nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
237
                    }
238

239
                } else {
240 241 242 243 244 245
                    currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                    protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                    nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
                }

                if (currentVertex == startVertex) {
246
                    if (area1.count() == joinedArea.count()) { // is the case if poly1 and poly2 don't intersect
247 248 249 250
                        return false;
                    } else {
                        return true;
                    }
251
                }
252
            }
253 254

        } else {
255
            return false;
256
        }
257
}
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

/*!
 * \fn bool WimaArea::join(WimaArea &area1, WimaArea &area2, WimaArea &joinedArea)
 * Joins the areas \a area1 and \a area2 such that a \l {Simple Polygon} is created.
 * Stores the result inside \a joinedArea.
 * Returns \c true if the algorithm was able to join the areas; false else.
 * The algorithm will be able to join the areas, if either their edges intersect with each other,
 * or one area contains the other.
 */
bool WimaArea::join(WimaArea &area1, WimaArea &area2, WimaArea &joinedArea)
{
    QString dummy;
    return join(area1, area2, joinedArea, dummy);
}

274 275 276 277 278 279 280 281 282
/*!
 * \fn bool WimaArea::join(WimaArea &area)
 * Joins the calling \c WimaArea and the \a area such that a \l {Simple Polygon} is created.
 * Overwrites the calling \c WimaArea with the result, if the algorithm was successful.
 * Returns \c true if the algorithm was able to join the areas; false else.
 * The algorithm will be able to join the areas, if either their edges intersect with each other,
 * or one area contains the other.
 */
bool WimaArea::join(WimaArea &area)
283
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
284
    WimaArea joinedArea;
285
    if ( join(*this, area, joinedArea) ) {
286 287 288 289 290
        this->setPath(joinedArea.path());
        return true;
    } else {
        return false;
    }
291
}
292

293

294
/*!
295 296 297 298 299 300 301 302 303
 * \fn bool WimaArea::join(WimaArea &area, QString &errorString)
 * Joins the calling \c WimaArea and the \a area such that a \l {Simple Polygon} is created.
 * Overwrites the calling \c WimaArea with the result, if the algorithm was successful.
 *
 * Returns \c true if the algorithm was able to join the areas; false else.
 * Stores error messages in \a errorString.
 *
 * The algorithm will be able to join the areas, if either their edges intersect with each other,
 * or one area contains the other.
304
 */
305
bool WimaArea::join(WimaArea &area, QString &errorString)
306
{
307 308 309
    WimaArea joinedArea;
    if ( join(*this, area, joinedArea, errorString) ) {
        this->setPath(joinedArea.path());
310 311 312 313
        return true;
    } else {
        return false;
    }
314 315
}

316 317 318 319 320 321
/*!
 * \fn int WimaArea::nextVertexIndex(int index) const
 * Returns the index of the next vertex (of the areas path), which is \a index + 1 if \a index is smaller than \c {area.count() - 1},
 * or 0 if \a index equals \c {area.count() - 1}, or -1 if the \a index is out of bounds.
 * \note The function \c {area.count()} (derived from \c QGCMapPolygon) returns the number of vertices defining the area.
 */
322
int WimaArea::nextVertexIndex(int index) const
323 324 325 326 327 328 329 330 331 332 333
{
    if (index >= 0 && index < count()-1) {
        return index + 1;
    } else if (index == count()-1) {
        return 0;
    } else {
        qWarning("WimaArea::nextVertexIndex(): Index out of bounds! index:count = %i:%i", index, count());
        return -1;
    }
}

334 335 336 337 338 339
/*!
 * \fn int WimaArea::previousVertexIndex(int index) const
 * Returns the index of the previous vertex (of the areas path), which is \a index - 1 if \a index is larger 0,
 * or \c {area.count() - 1} if \a index equals 0, or -1 if the \a index is out of bounds.
 * \note The function \c {area.count()} (derived from \c QGCMapPolygon) returns the number of vertices defining the area.
 */
340
int WimaArea::previousVertexIndex(int index) const
341 342 343 344 345 346 347 348 349 350 351
{
    if (index > 0 && index < count()) {
        return index - 1;
    } else if (index == 0) {
        return count()-1;
    } else {
        qWarning("WimaArea::previousVertexIndex(): Index out of bounds! index:count = %i:%i", index, count());
        return -1;
    }
}

352 353 354 355 356 357 358
/*!
 * \fn bool WimaArea::intersects(const QGCMapPolyline &line1, const QGCMapPolyline &line2, QGeoCoordinate &intersectionPt)
 * Returns \c true if \a line1 and \a line2 intersect with each other.
 * Stores the intersection point in \a intersectionPt
 *
 * \sa QGeoCoordinate
 */
359
bool WimaArea::intersects(const QGCMapPolyline &line1, const QGCMapPolyline &line2, QGeoCoordinate &intersectionPt)
360
{
361 362

        if (line1.count() == 2 && line2.count() == 2 ) {
363 364 365
            QPointF pt11(0, 0);

            double x, y, z;
366 367
            QGeoCoordinate origin = line1.vertexCoordinate(0);
            convertGeoToNed(line1.vertexCoordinate(1), origin, &x, &y, &z);
368 369 370 371 372
            QPointF pt12(x, y);

            QLineF kartLine1(pt11, pt12);


373
            convertGeoToNed(line2.vertexCoordinate(0), origin, &x, &y, &z);
374 375
            QPointF pt21(x, y);

376
            convertGeoToNed(line2.vertexCoordinate(1), origin, &x, &y, &z);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
377
            QPointF pt22(x, y);;
378 379 380 381

            QLineF kartLine2(pt21, pt22);

            QPointF intersectionPoint;
382
            if (kartLine1.intersect(kartLine2, &intersectionPoint) == QLineF::BoundedIntersection) {
383
                convertNedToGeo(intersectionPoint.x(), intersectionPoint.y(), origin.altitude(), origin, &intersectionPt);
384
                return true;
385 386
            }
            else {
387
                return false;
388 389 390 391
            }


        } else {
392 393
            qWarning("WimaArea::intersect(line1, line2):  line1->count() != 2 || line2->count() != 2!");
            return false;
394 395 396
        }
}

397 398 399 400 401 402 403 404 405 406 407 408 409
/*!
 * \fn bool WimaArea::intersects(const QGCMapPolyline &line, const WimaArea &area, QList<QGeoCoordinate> &intersectionList, QList<QPair<int, int>> &neighbourList)
 * Returns \c true if \a line and \a area intersect with each other at least once.bool WimaArea::intersects(const QGCMapPolyline &line, const WimaArea &area, QList<QGeoCoordinate> &intersectionList, QList<QPair<int, int>> &neighbourList)
 * Stores the intersection points in \a intersectionList.
 * Stores the indices of the closest two \a area vetices for each of coorespoinding intersection points in \a neighbourList.
 *
 * For example if an intersection point is found between the first and the second vertex of the \a area the intersection point will
 * be stored in \a intersectionList and the indices 1 and 2 will be stored in \a neighbourList.
 * \a neighbourList has entries of type \c {QPair<int, int>}, where \c{pair.first} would contain 1 and \c{pair.second} would contain 2, when
 * relating to the above example.
 *
 * \sa QPair, QList
 */
410
bool WimaArea::intersects(const QGCMapPolyline &line, const WimaArea &area, QList<QGeoCoordinate> &intersectionList, QList<QPair<int, int>> &neighbourList)// don't seperate parameters with new lines or documentation will break
411
{
412
        intersectionList.clear();
413
        neighbourList.clear();
414 415


416
        if (line.count() == 2 && area.count() >= 3) { // are line a proper line and poly a proper poly?other,
417

418
            // Asseble a line form each tow consecutive polygon vertices and check whether it intersects with line
419
            for (int i = 0; i < area.count(); i++) {
420

421
                QGCMapPolyline interatorLine;
422 423
                QGeoCoordinate currentVertex    = area.vertexCoordinate(i);
                QGeoCoordinate nextVertex       = area.vertexCoordinate(area.nextVertexIndex(i));
424 425
                interatorLine.appendVertex(currentVertex);
                interatorLine.appendVertex(nextVertex);
426

427 428
                QGeoCoordinate intersectionPoint;
                if ( intersects(line, interatorLine, intersectionPoint) ){
429
                    intersectionList.append(intersectionPoint);
430

431 432
                    QPair<int, int>     neighbours;
                    neighbours.first    = i;
433
                    neighbours.second   = area.nextVertexIndex(i);
434
                    neighbourList.append(neighbours);
435 436
                }
            }
437

438
            if (intersectionList.count() > 0) {
439 440 441 442
                return true;
            } else {
                return false;
            }
443 444
        } else {
            qWarning("WimaArea::intersects(line, poly): line->count() != 2 || poly->count() < 3");
445
            return false;
446 447 448
        }
}

449
/*!other,
450 451 452 453 454 455 456 457
 * \fn double WimaArea::distInsidePoly(const QGeoCoordinate &c1, const QGeoCoordinate &c2, WimaArea area)
 * Returns the distance between the coordinate \a c1 and coordinate \a c2, or infinity if the shortest path between
 * the two coordinates is not fully inside the \a area.
 * \note Both coordinates must lie inside the \a area.
 *
 * \sa QGeoCoordinate
 */
double WimaArea::distInsidePoly(const QGeoCoordinate &c1, const QGeoCoordinate &c2, WimaArea area)
458
{
459 460
        area.offset(0.1); // hack to compensate for numerical issues, migh be replaced in the future...
        if ( area.containsCoordinate(c1) && area.containsCoordinate(c2)) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
461 462 463 464
            QList<QGeoCoordinate>   intersectionList;
            QList<QPair<int, int>>  neighbourlist;
            QGCMapPolyline line;

465 466
            line.appendVertex(c1);
            line.appendVertex(c2);
467
            intersects(line, area, intersectionList, neighbourlist);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
468

469
            if ( intersectionList.size() == 0 ){ // if an intersection was found the path between c1 and c2 is not fully inside area.
470
                return c1.distanceTo(c2);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
471 472
            } else {
                return std::numeric_limits<qreal>::infinity();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
473 474
            }

475 476
        } else {
            return std::numeric_limits<qreal>::infinity();
477 478 479
        }
}

480
/*!
481
 * \fn bool WimaArea::dijkstraPath(const QGeoCoordinate &start, const QGeoCoordinate &end, const WimaArea &area, QList<QGeoCoordinate> &dijkstraPath, QString &errorstring)
482 483 484
 * Calculates the shortest path (inside \a area) between \a start and \a end.
 * The \l {Dijkstra Algorithm} is used to find the shorest path.
 * Stores the result inside \a dijkstraPath when sucessfull.
485
 * Stores error messages in \a errorString.
486 487 488 489
 * Returns \c true if successful, \c false else.
 *
 * \sa QList
 */
490
bool WimaArea::dijkstraPath(const QGeoCoordinate &start, const QGeoCoordinate &end, const WimaArea &area, QList<QGeoCoordinate> &dijkstraPath, QString &errorString) // don't seperate parameters with new lines or documentation will break
491
{
492
    if ( isSelfIntersecting(area) ) {
493
        errorString.append("Area is self intersecting and thus not a simple polygon. Only simple polygons allowed.\n");
494 495 496
        return false;
    }

497 498
    // Each QGeoCoordinate gets stuff into a Node
    /// @param distance is the distance between the Node and it's predecessor
499 500 501 502 503 504
    struct Node{
        QGeoCoordinate coordinate;
        double distance = std::numeric_limits<qreal>::infinity();
        Node* predecessorNode = nullptr;
    };

505
    // The list with all Nodes (start, end + poly.path())
506
    QList<Node> nodeList;
507 508
    // This list will be initalized with (pointer to) all elements of nodeList.
    // Elements will be successively remove during the execution of the Dijkstra Algorithm.
509 510
    QList<Node*> workingSet;

511
    // initialize nodeList_maxAltitude
512
    // start cooridnate
513 514 515 516 517
    Node startNode;
    startNode.coordinate    = start;
    startNode.distance      = 0;
    nodeList.append(startNode);

518
    //poly cooridnates
519
    for (int i = 0; i < area.count(); i++) {
520
        Node node;
521
        node.coordinate = area.vertexCoordinate(i);
522 523
        nodeList.append(node);
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
524

525
    //end coordinate
526 527 528
    Node endNode;
    endNode.coordinate  = end;
    nodeList.append(endNode);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
529

530
    // initialize working set
531 532 533 534
    for (int i = 0; i < nodeList.size(); i++) {
        Node* nodePtr = &nodeList[i];
        workingSet.append(nodePtr);
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
535 536


537 538 539 540 541 542 543 544 545 546 547 548
    // Dijkstra Algorithm
    // https://de.wikipedia.org/wiki/Dijkstra-Algorithmus
    while (workingSet.size() > 0) {
        // serach Node with minimal distance
        double minDist = std::numeric_limits<qreal>::infinity();
        int minDistIndex = 0;
        for (int i = 0; i < workingSet.size(); i++) {
            Node* node = workingSet.value(i);
            double dist = node->distance;
            if (dist < minDist) {
                minDist = dist;
                minDistIndex = i;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
549
            }
550 551
        }
        Node* u = workingSet.takeAt(minDistIndex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
552 553


554 555 556
        //update distance
        for (int i = 0; i < workingSet.size(); i++) {
            Node* v = workingSet[i];
Valentin Platzgummer's avatar
Valentin Platzgummer committed
557

558
            // is neighbour? dist == infinity if no neihbour
559
            double dist = distInsidePoly(u->coordinate, v->coordinate, area);
560
            // is ther a alternative path which is shorter?
561 562 563 564
            double alternative = u->distance + dist;
            if (alternative < v->distance)  {
                v->distance         = alternative;
                v->predecessorNode  = u;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
565 566 567
            }
        }

568
    }
569 570
    // end Djikstra Algorithm

Valentin Platzgummer's avatar
Valentin Platzgummer committed
571

572
    // check it the Algorithm was sucessfulepsilonMeter
573 574
    Node* Node = &nodeList.last();
    if (Node->predecessorNode == nullptr) {
575

576
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
577

578
    // reverse assemble path
579 580 581 582 583 584
    while (1) {
        dijkstraPath.prepend(Node->coordinate);

        //Update Node
        Node = Node->predecessorNode;
        if (Node == nullptr) {
585 586 587 588
            if (dijkstraPath[0].distanceTo(start) < epsilonMeter)// check if starting point was reached
                break;
            qWarning("WimaArea::dijkstraPath(): Error, no path found!\n");
            return false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
589
        }
590
    }
591 592 593 594

    return true;
}

595 596 597 598 599 600
/*!
 * \fn bool WimaArea::isSelfIntersecting(const WimaArea &area)
 * Returns \c true if the \a area is self intersecting, \c false else.
 * \note If the \a area is self intersecting, it's not a \l {Simple Polygon}.
 */
bool WimaArea::isSelfIntersecting(const WimaArea &area)
601 602
{
    int i = 0;
603 604 605
    if (area.count() > 3) {
        // check if any edge of the area (formed by two adjacent vertices) intersects with any other edge of the area
        while(i < area.count()-1) {
606 607
            QGeoCoordinate refBeginCoordinate = area.vertexCoordinate(i);
            QGeoCoordinate refEndCoordinate = area.vertexCoordinate(area.nextVertexIndex(i));
608
            QGCMapPolyline refLine;
609 610
            refLine.appendVertex(refBeginCoordinate);
            refLine.appendVertex(refEndCoordinate);
611 612
            int j = area.nextVertexIndex(i);
            while(j < area.count()) {
613
                QGeoCoordinate intersectionPt;
614
                QGCMapPolyline iteratorLine;
615 616
                iteratorLine.appendVertex(area.vertexCoordinate(j));
                iteratorLine.appendVertex(area.vertexCoordinate(area.nextVertexIndex(j)));
617

618 619 620 621 622 623 624 625
                if ( intersects(refLine, iteratorLine, intersectionPt) ){
                    if (    !(intersectionPt.distanceTo(refBeginCoordinate) < epsilonMeter)
                         && !(intersectionPt.distanceTo(refEndCoordinate)   < epsilonMeter) ) {
                        return true;
                    }
                }


626 627 628 629 630 631 632 633 634 635

                j++;
            }
            i++;
        }
    }

    return false;
}

636 637 638 639 640
/*!
 * \fn bool WimaArea::isSelfIntersecting()
 * Returns \c true if the calling area is self intersecting, \c false else.
 * \note If the calling area is self intersecting, it's not a \l {Simple Polygon}.
 */
641 642 643
bool WimaArea::isSelfIntersecting()
{
    return isSelfIntersecting(*this);
644
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
645

646 647 648 649 650 651
/*!
 * \fn void WimaArea::saveToJson(QJsonObject &json)
 * Saves the calling area to \c QJsonObject object and stores it inside \a json.
 *
 * \sa QJsonObject
 */
652 653 654 655 656 657 658
void WimaArea::saveToJson(QJsonObject &json)
{
    this->QGCMapPolygon::saveToJson(json);
    json[maxAltitudeName]   = _maxAltitude;
    json[areaTypeName]      = wimaAreaName;
    // add WimaVehicle if necessary..
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
659

660 661 662 663 664 665 666 667
/*!
 * \fn bool WimaArea::loadFromJson(const QJsonObject &json, QString& errorString)
 * Loads data from \a json and stores it inside the calling area.
 * Returns \c true if loading was successful, \c false else.
 * Stores error messages inside \a errorString.
 *
 * \sa QJsonObject
 */
668 669 670 671 672 673 674
bool WimaArea::loadFromJson(const QJsonObject &json, QString& errorString)
{
    if ( this->QGCMapPolygon::loadFromJson(json, false /*no poly required*/, errorString) ) {
        if ( json.contains(maxAltitudeName) && json[maxAltitudeName].isDouble()) {
            _maxAltitude = json[maxAltitudeName].toDouble();
            return true;
        } else {
675
            errorString.append(tr("Could not load Maximum Altitude value!\n"));
676 677
            return false;
        }
678
    } else {
679 680
        qWarning() << errorString;
        return false;
681 682
    }
}
683

684 685 686 687
/*!
 * \fn void WimaArea::init()
 * Funtion to be called during construction.
 */
688 689 690 691 692
void WimaArea::init()
{
    this->setObjectName(wimaAreaName);
}

693 694 695 696
/*!
 * \fn void print(const WimaArea &area)
 * Prints the data contained in \a area to the console.
 */
697 698 699 700 701 702 703
void print(const WimaArea &area)
{
    QString message;
    print(area, message);
    qWarning() << message;
}

704 705 706 707
/*!
 * \fn void print(const WimaArea &area)
 * Prints the data contained in \a area to the \a outputString.
 */
708 709
void print(const WimaArea &area, QString &outputString)
{
710
    outputString.append(QString("Type: %1\n").arg(area.objectName()));
711
    print(static_cast<const QGCMapPolygon&>(area), outputString);
712
    outputString.append(QString("Maximum Altitude: %1\n").arg(area._maxAltitude));
713 714
}

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

// QDoc Documentation

/*!
    \group WimaAreaGroup
    \title Group of WimaAreas

    Every \c WimaArea of the equally named group uses a \l {Simple Polygon} derived from \c {QGCMapPolygon}
    to define areas inside which certain taskts are performed.
*/

/*!
    \class WimaArea
    \inmodule Wima
    \ingroup WimaArea

    \brief The \c WimaArea class provides the a base class for
    all areas used within the Wima extension.

    \c WimaArea uses a \l {Simple Polygon} derived from \c {QGCMapPolygon}
    to define areas inside which certain taskts are performed. The polygon (often refered to as the path) can
    be displayed visually on a map.
*/

/*!
  \variable WimaArea::_maxAltitude
  \brief The maximum altitude vehicles are allowed to fly inside this area.
*/

/*!
  \property WimaArea::maxAltitude
  \brief The maximum altitude at which vehicles are allowed to fly.
*/

/*!
  \property WimaArea::mapVisualQML
  \brief A string containing the name of the QML file used to displays this area on a map.
*/

/*!
  \property WimaArea::editorQML
  \brief A string containing the name of the QML file allowing to edit the area's properties.
*/

/*!
    \externalpage https://en.wikipedia.org/wiki/Simple_polygon
    \title Simple Polygon
*/

/*!
    \externalpage https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
    \title Dijkstra Algorithm
*/