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

3 4 5 6
/*!
 * \variable WimaArea::numericalAccuracy
 * \brief The accuracy used for calculations.
 */
7
const double WimaArea::numericalAccuracy    = 1e-3; // meters
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


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 (other, parent)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
36
{
37
    init();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
38 39 40 41 42 43
    this->setPath(other.path());
    this->setCenter(other.center());
    this->setCenterDrag(other.centerDrag());
    this->setInteractive(other.interactive());
    _maxAltitude = other.maxAltitude();
}
44

45 46 47 48 49 50 51
/*!
  \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)
52
{
53 54
    if ( altitude > 0 && qFuzzyCompare(altitude, _maxAltitude) ) {
        _maxAltitude = altitude;
55 56 57 58
        emit maxAltitudeChanged();
    }
}

59 60 61 62 63 64 65
/*!
 * \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
 */
66
int WimaArea::getClosestVertexIndex(const QGeoCoordinate &coordinate) const
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
    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;
    }
}

88 89 90 91 92 93
/*!
 * \fn  QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
 *  Returns the vertex of the polygon path with the least distance to \a coordinate.
 *
 * \sa QGeoCoordinate
 */
94
QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
95 96 97 98
{
    return this->vertexCoordinate(getClosestVertexIndex(coordinate));
}

99 100 101 102 103
/*!
 * \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)
104
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
105
    QGCMapPolygon qgcPoly;
106
    qgcPoly.setPath(area.path());
107

Valentin Platzgummer's avatar
Valentin Platzgummer committed
108
    return QGCMapPolygon(qgcPoly);
109 110
}

111 112 113 114
/*!
 * \fn QGCMapPolygon WimaArea::toQGCPolygon() const
 * Converts the calling \c WimaArea to \c QGCMapPolygon by copying the path only.
 */
115 116 117 118 119
QGCMapPolygon WimaArea::toQGCPolygon() const
{
    return toQGCPolygon(*this);
}

120 121 122 123 124 125
/*!
 * \fn void WimaArea::join(QList<WimaArea *>* polyList,  WimaArea* joinedPoly)
 * Not yet implemented \a polyList, \a joinedPoly.
 *
 * \sa QList
 */
126
void WimaArea::join(QList<WimaArea *>* polyList,  WimaArea* joinedPoly)
127
{
128
    return;
129 130
}

131 132 133 134 135 136 137 138 139
/*!
 * \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)
140
{
141

142
        if (area1.count() >= 3 && area2.count() >= 3) {
143

144
            joinedArea.clear();
145

146 147
            area1.verifyClockwiseWinding();
            area2.verifyClockwiseWinding();
148

149 150
            WimaArea* walkerPoly    = &area1; // "walk" on this polygon towards higher indices
            WimaArea* crossPoly     = &area2; // check for crossings with this polygon while "walking"
151 152
                                             // and swicht to this polygon on a intersection,
                                             // continue to walk towards higher indices
153 154 155 156 157 158 159 160 161 162 163 164 165 166

            // 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) {
167
                joinedArea.appendVertices(crossPoly->coordinateList());
168
                return true;
169 170 171 172 173
            }


            QGeoCoordinate currentVertex    = walkerPoly->vertexCoordinate(startIndex);
            QGeoCoordinate startVertex      = currentVertex;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
174
            // possible nextVertex (if no intersection between currentVertex and protoVertex with crossPoly)
175 176 177 178
            QGeoCoordinate protoNextVertex  = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(startIndex));

            int nextVertexIndex = walkerPoly->nextVertexIndex(startIndex);
            while (1) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
179
                //qDebug("nextVertexIndex: %i", nextVertexIndex);
180
                joinedArea.appendVertex(currentVertex);
181 182 183

                QGCMapPolyline walkerPolySegment;
                walkerPolySegment.appendVertex(currentVertex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
184
                walkerPolySegment.appendVertex(protoNextVertex);
185

186 187
                QList<QPair<int, int>> neighbourList;
                QList<QGeoCoordinate> intersectionList;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
188
                //qDebug("IntersectionList.size() on init: %i", intersectionList.size());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
189
                intersects(walkerPolySegment, *crossPoly, intersectionList, neighbourList);
190

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

193 194
                if (intersectionList.size() >= 1) {
                    int minDistIndex = 0;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
195

196 197 198 199
                    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
200

201 202 203 204 205 206
                            if ( minDist > currentDist ) {
                                minDist         = currentDist;
                                minDistIndex    = i;
                            }
                        }
                    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
207

Valentin Platzgummer's avatar
Valentin Platzgummer committed
208
                    //qDebug("MinDistIndex: %i", minDistIndex);
209 210 211 212 213 214 215 216 217 218 219 220
                    QGeoCoordinate protoCurrentVertex = intersectionList.value(minDistIndex);
                    // take numerical erros into account
                    if (protoCurrentVertex.distanceTo(currentVertex) > WimaArea::numericalAccuracy) {
                        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
221
                    } else {
222 223 224
                        currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                        protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                        nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
225
                    }
226

227
                } else {
228 229 230 231 232 233
                    currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                    protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                    nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
                }

                if (currentVertex == startVertex) {
234
                    if (area1.count() == joinedArea.count()) { // is the case if poly1 and poly2 don't intersect
235 236 237 238
                        return false;
                    } else {
                        return true;
                    }
239
                }
240
            }
241 242

        } else {
243
            return false;
244
        }
245
}
246

247 248 249 250 251 252 253 254 255
/*!
 * \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)
256
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
257
    WimaArea joinedArea;
258
    if ( join(*this, area, joinedArea) ) {
259 260 261 262 263
        this->setPath(joinedArea.path());
        return true;
    } else {
        return false;
    }
264
}
265

266 267 268 269 270 271
/*!
 * \fn bool WimaArea::isDisjunct(QList<WimaArea *>* polyList)
 * Not yet implemented.
 *
 * \sa QList
 */
272
bool WimaArea::isDisjunct(QList<WimaArea *>* polyList)
273 274 275 276
{
    // needs improvement
    if (polyList != nullptr){
        for (int i = 0;i < polyList->size()-1; i++) {
277
            WimaArea* currPoly = polyList->value(i);
278
            for (int j = i+1; i < polyList->size(); j++) {
279
                if (isDisjunct(currPoly, polyList->value(j))) {
280 281 282 283 284 285 286 287 288 289
                    return false;
                }
            }
        }
        return true;
    } else {
        qWarning("WimaArea::isDisjunct(polyList): polyList == nullptr!");
        return false;
    }
}
290

291 292 293 294
/*!
 * \fn bool WimaArea::isDisjunct(WimaArea *poly1, WimaArea *poly2)
 * Not yet implemented.
 */
295
bool WimaArea::isDisjunct(WimaArea *poly1, WimaArea *poly2)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
{
    if (poly1 != nullptr && poly2 != nullptr) {
        QGCMapPolygon* poly1Copy = new QGCMapPolygon(this);
        poly1Copy->setPath(poly1->path());
        poly1Copy->offset(numericalAccuracy);// take numerical errors in account
        for(int i = 0; i < poly2->count(); i++){
            if (poly1Copy->containsCoordinate(poly2->vertexCoordinate(i))){
                return false;
            }
        }
        return true;
    } else {
        qWarning("WimaArea::isDisjunct(poly1, poly2):  poly1 == nullptr || poly2 == nullptr!");
        return false;
    }
311 312
}

313 314 315 316 317 318
/*!
 * \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.
 */
319
int WimaArea::nextVertexIndex(int index) const
320 321 322 323 324 325 326 327 328 329 330
{
    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;
    }
}

331 332 333 334 335 336
/*!
 * \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.
 */
337
int WimaArea::previousVertexIndex(int index) const
338 339 340 341 342 343 344 345 346 347 348
{
    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;
    }
}

349 350 351 352 353 354 355
/*!
 * \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
 */
356
bool WimaArea::intersects(const QGCMapPolyline &line1, const QGCMapPolyline &line2, QGeoCoordinate &intersectionPt)
357
{
358 359

        if (line1.count() == 2 && line2.count() == 2 ) {
360 361 362
            QPointF pt11(0, 0);

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

            QLineF kartLine1(pt11, pt12);


370
            convertGeoToNed(line2.vertexCoordinate(0), origin, &x, &y, &z);
371 372
            QPointF pt21(x, y);

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

            QLineF kartLine2(pt21, pt22);

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


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

394 395 396 397 398 399 400 401 402 403 404 405 406 407
/*!
 * \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
 */
bool WimaArea::intersects(const QGCMapPolyline &line, const WimaArea &area, QList<QGeoCoordinate> &intersectionList, QList<QPair<int, int>> &neighbourList)
408
{
409
        intersectionList.clear();
410
        neighbourList.clear();
411 412


413
        if (line.count() == 2 && area.count() >= 3) { // are line a proper line and poly a proper poly?
414

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

418
                QGCMapPolyline interatorLine;
419 420
                QGeoCoordinate currentVertex    = area.vertexCoordinate(i);
                QGeoCoordinate nextVertex       = area.vertexCoordinate(area.nextVertexIndex(i));
421 422
                interatorLine.appendVertex(currentVertex);
                interatorLine.appendVertex(nextVertex);
423

424 425
                QGeoCoordinate intersectionPoint;
                if ( intersects(line, interatorLine, intersectionPoint) ){
426
                    intersectionList.append(intersectionPoint);
427

428 429
                    QPair<int, int>     neighbours;
                    neighbours.first    = i;
430
                    neighbours.second   = area.nextVertexIndex(i);
431
                    neighbourList.append(neighbours);
432 433
                }
            }
434

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

446 447 448 449 450 451 452 453 454
/*!
 * \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)
455
{
456 457
        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
458 459 460 461
            QList<QGeoCoordinate>   intersectionList;
            QList<QPair<int, int>>  neighbourlist;
            QGCMapPolyline line;

462 463
            line.appendVertex(c1);
            line.appendVertex(c2);
464
            intersects(line, area, intersectionList, neighbourlist);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
465

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

472 473
        } else {
            return std::numeric_limits<qreal>::infinity();
474 475 476
        }
}

477 478 479 480 481 482 483 484 485
/*!
 * \fn bool WimaArea::dijkstraPath(const QGeoCoordinate    &start, const QGeoCoordinate    &end, const WimaArea          &area, QList<QGeoCoordinate>   &dijkstraPath)
 * 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.
 * Returns \c true if successful, \c false else.
 *
 * \sa QList
 */
486 487
bool WimaArea::dijkstraPath(const QGeoCoordinate    &start,
                            const QGeoCoordinate    &end,
488
                            const WimaArea          &area,
489
                            QList<QGeoCoordinate>   &dijkstraPath)
490
{
491
    // Returns true if a valid path was found.
492
    if ( isSelfIntersecting(area) ) {
493 494 495
        return false;
    }

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

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

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

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

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

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


536 537 538 539 540 541 542 543 544 545 546 547
    // 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
548
            }
549 550
        }
        Node* u = workingSet.takeAt(minDistIndex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
551 552


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

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

567
    }
568 569
    // end Djikstra Algorithm

Valentin Platzgummer's avatar
Valentin Platzgummer committed
570

571
    // check it the Algorithm was sucessful
572 573 574
    Node* Node = &nodeList.last();
    if (Node->predecessorNode == nullptr) {
        qWarning("WimaArea::dijkstraPath(): Error, no path found!");
575
        return false;
576
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
577

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

        //Update Node
        Node = Node->predecessorNode;
        if (Node == nullptr) {
            break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
586
        }
587
    }
588 589 590 591

    return true;
}

592 593 594 595 596 597
/*!
 * \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)
598 599
{
    int i = 0;
600 601 602
    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) {
603
            QGCMapPolyline refLine;
604 605 606 607
            refLine.appendVertex(area.vertexCoordinate(i));
            refLine.appendVertex(area.vertexCoordinate(area.nextVertexIndex(i)));
            int j = area.nextVertexIndex(i);
            while(j < area.count()) {
608 609
                QGeoCoordinate dummy;
                QGCMapPolyline iteratorLine;
610 611
                iteratorLine.appendVertex(area.vertexCoordinate(j));
                iteratorLine.appendVertex(area.vertexCoordinate(area.nextVertexIndex(j)));
612 613 614 615 616 617 618 619 620 621 622 623 624

                if ( intersects(refLine, iteratorLine, dummy) )
                    return true;

                j++;
            }
            i++;
        }
    }

    return false;
}

625 626 627 628 629
/*!
 * \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}.
 */
630 631 632
bool WimaArea::isSelfIntersecting()
{
    return isSelfIntersecting(*this);
633
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
634

635 636 637 638 639 640
/*!
 * \fn void WimaArea::saveToJson(QJsonObject &json)
 * Saves the calling area to \c QJsonObject object and stores it inside \a json.
 *
 * \sa QJsonObject
 */
641 642 643 644 645 646 647
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
648

649 650 651 652 653 654 655 656
/*!
 * \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
 */
657 658 659 660 661 662 663
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 {
664
            errorString.append(tr("Could not load Maximum Altitude value!\n"));
665 666
            return false;
        }
667
    } else {
668 669
        qWarning() << errorString;
        return false;
670 671
    }
}
672

673 674 675 676
/*!
 * \fn void WimaArea::update(const WimaArea &area)
 * Not yet implemented.
 */
677 678 679
void WimaArea::update(const WimaArea &area)
{
    this->QGCMapPolygon::update(area);
680
    this->setMaxAltitude(area.maxAltitude());displays
681 682
}

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

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

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

// 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
*/