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

3 4 5 6 7
const double WimaArea::numericalAccuracy    = 1e-3; // meters
const char* WimaArea::maxAltitudeName       = "maxAltitude";
const char* WimaArea::wimaAreaName          = "WimaArea";
const char* WimaArea::areaTypeName          = "AreaType";

8

9 10


11 12
WimaArea::WimaArea(QObject *parent)
    :  QGCMapPolygon (parent)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
13
{
14
    init();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
15 16
    _maxAltitude = 30;
}
17

Valentin Platzgummer's avatar
Valentin Platzgummer committed
18
WimaArea::WimaArea(const WimaArea &other, QObject *parent)
19
    : QGCMapPolygon (other, parent)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
20
{
21
    init();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
22 23 24 25 26 27
    this->setPath(other.path());
    this->setCenter(other.center());
    this->setCenterDrag(other.centerDrag());
    this->setInteractive(other.interactive());
    _maxAltitude = other.maxAltitude();
}
28

29 30 31 32 33 34
WimaArea &WimaArea::operator=(WimaArea other)
{
    swap(*this, other); // copy-swap-idiom

    return *this;
}
35 36 37 38 39 40 41 42 43

void WimaArea::setMaxAltitude(double alt)
{
    if(alt > 0 && alt != _maxAltitude){
        _maxAltitude = alt;
        emit maxAltitudeChanged();
    }
}

44

45

46 47 48 49 50 51 52 53 54 55 56
/*QList<WimaArea *>* WimaArea::splitArea<T>(WimaArea *polygonToSplitt, int numberOfFractions)
{
    if(numberOfFractions > 0 && polygonToSplitt != nullptr){
        WimaArea* poly;
        if(p)
        = new WimaArea(polygonToSplitt, this);
        QList<WimaArea*>* list = new QList<WimaArea*>();
        list->append(poly);
        return list;
    }
    return nullptr;
57
}
58 59 60 61 62 63 64


QList<QGCMapPolygon*>* WimaArea::splitArea(int numberOfFractions)
{
    return splitPolygonArea(this, numberOfFractions);
}*/

65
int WimaArea::getClosestVertexIndex(const QGeoCoordinate &coordinate) const
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
{
    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;
    }
}

87
QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
88 89 90 91
{
    return this->vertexCoordinate(getClosestVertexIndex(coordinate));
}

92
QGCMapPolygon WimaArea::toQGCPolygon(const WimaArea &poly)
93
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
94 95 96 97 98
    QGCMapPolygon qgcPoly;
    qgcPoly.setPath(poly.path());
    qgcPoly.setCenter(poly.center());
    qgcPoly.setCenterDrag(poly.centerDrag());
    qgcPoly.setInteractive(poly.interactive());
99

Valentin Platzgummer's avatar
Valentin Platzgummer committed
100
    return QGCMapPolygon(qgcPoly);
101 102
}

103 104 105 106 107
QGCMapPolygon WimaArea::toQGCPolygon() const
{
    return toQGCPolygon(*this);
}

108
void WimaArea::join(QList<WimaArea *>* polyList,  WimaArea* joinedPoly)
109
{
110
    return;
111 112
}

113
bool WimaArea::join(WimaArea &poly1, WimaArea &poly2, WimaArea &joinedPoly)
114
{
115

116 117
        if (poly1.count() >= 3 && poly2.count() >= 3) {

118 119
            joinedPoly.clear();

Valentin Platzgummer's avatar
Valentin Platzgummer committed
120 121
            poly1.verifyClockwiseWinding();
            poly2.verifyClockwiseWinding();
122

Valentin Platzgummer's avatar
Valentin Platzgummer committed
123 124
            WimaArea* walkerPoly    = &poly1; // "walk" on this polygon towards higher indices
            WimaArea* crossPoly     = &poly2; // check for crossings with this polygon while "walking"
125 126
                                             // and swicht to this polygon on a intersection,
                                             // continue to walk towards higher indices
127 128 129 130 131 132 133 134 135 136 137 138 139 140

            // 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) {
141
                joinedPoly.appendVertices(crossPoly->coordinateList());
142
                return true;
143 144 145 146 147
            }


            QGeoCoordinate currentVertex    = walkerPoly->vertexCoordinate(startIndex);
            QGeoCoordinate startVertex      = currentVertex;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
148
            // possible nextVertex (if no intersection between currentVertex and protoVertex with crossPoly)
149 150 151 152
            QGeoCoordinate protoNextVertex  = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(startIndex));

            int nextVertexIndex = walkerPoly->nextVertexIndex(startIndex);
            while (1) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
153
                //qDebug("nextVertexIndex: %i", nextVertexIndex);
154
                joinedPoly.appendVertex(currentVertex);
155 156 157

                QGCMapPolyline walkerPolySegment;
                walkerPolySegment.appendVertex(currentVertex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
158
                walkerPolySegment.appendVertex(protoNextVertex);
159

160 161
                QList<QPair<int, int>> neighbourList;
                QList<QGeoCoordinate> intersectionList;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
162
                //qDebug("IntersectionList.size() on init: %i", intersectionList.size());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
163
                intersects(walkerPolySegment, *crossPoly, intersectionList, neighbourList);
164

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

167 168
                if (intersectionList.size() >= 1) {
                    int minDistIndex = 0;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
169

170 171 172 173
                    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
174

175 176 177 178 179 180
                            if ( minDist > currentDist ) {
                                minDist         = currentDist;
                                minDistIndex    = i;
                            }
                        }
                    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
181

Valentin Platzgummer's avatar
Valentin Platzgummer committed
182
                    //qDebug("MinDistIndex: %i", minDistIndex);
183 184 185 186 187 188 189 190 191 192 193 194
                    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
195
                    } else {
196 197 198
                        currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                        protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                        nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
199
                    }
200

201
                } else {
202 203 204 205 206 207
                    currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                    protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                    nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
                }

                if (currentVertex == startVertex) {
208 209 210 211 212
                    if (poly1.count() == joinedPoly.count()) { // is the case if poly1 and poly2 don't intersect
                        return false;
                    } else {
                        return true;
                    }
213
                }
214
            }
215 216

        } else {
217
            return false;
218
        }
219
}
220

221
bool WimaArea::join(WimaArea &poly)
222
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
223
    WimaArea joinedArea;
224 225 226 227 228 229
    if ( join(*this, poly, joinedArea) ) {
        this->setPath(joinedArea.path());
        return true;
    } else {
        return false;
    }
230
}
231

232
bool WimaArea::isDisjunct(QList<WimaArea *>* polyList)
233 234 235 236
{
    // needs improvement
    if (polyList != nullptr){
        for (int i = 0;i < polyList->size()-1; i++) {
237
            WimaArea* currPoly = polyList->value(i);
238
            for (int j = i+1; i < polyList->size(); j++) {
239
                if (isDisjunct(currPoly, polyList->value(j))) {
240 241 242 243 244 245 246 247 248 249
                    return false;
                }
            }
        }
        return true;
    } else {
        qWarning("WimaArea::isDisjunct(polyList): polyList == nullptr!");
        return false;
    }
}
250

251
bool WimaArea::isDisjunct(WimaArea *poly1, WimaArea *poly2)
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
    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;
    }
267 268
}

269
int WimaArea::nextVertexIndex(int index) const
270 271 272 273 274 275 276 277 278 279 280
{
    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;
    }
}

281
int WimaArea::previousVertexIndex(int index) const
282 283 284 285 286 287 288 289 290 291 292
{
    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;
    }
}

293
bool WimaArea::intersects(const QGCMapPolyline &line1, const QGCMapPolyline &line2, QGeoCoordinate &intersectionPt)
294
{
295 296

        if (line1.count() == 2 && line2.count() == 2 ) {
297 298 299
            QPointF pt11(0, 0);

            double x, y, z;
300 301
            QGeoCoordinate origin = line1.vertexCoordinate(0);
            convertGeoToNed(line1.vertexCoordinate(1), origin, &x, &y, &z);
302 303 304 305 306
            QPointF pt12(x, y);

            QLineF kartLine1(pt11, pt12);


307
            convertGeoToNed(line2.vertexCoordinate(0), origin, &x, &y, &z);
308 309
            QPointF pt21(x, y);

310
            convertGeoToNed(line2.vertexCoordinate(1), origin, &x, &y, &z);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
311
            QPointF pt22(x, y);;
312 313 314 315

            QLineF kartLine2(pt21, pt22);

            QPointF intersectionPoint;
316
            if (kartLine1.intersect(kartLine2, &intersectionPoint) == QLineF::BoundedIntersection) {
317
                convertNedToGeo(intersectionPoint.x(), intersectionPoint.y(), origin.altitude(), origin, &intersectionPt);
318
                return true;
319
            } else {
320
                return false;
321 322 323 324
            }


        } else {
325 326
            qWarning("WimaArea::intersect(line1, line2):  line1->count() != 2 || line2->count() != 2!");
            return false;
327 328 329
        }
}

330
bool WimaArea::intersects(const QGCMapPolyline& line, const WimaArea& poly, QList<QGeoCoordinate>& intersectionList, QList<QPair<int, int> >& neighbourlist)
331
{
332 333
        neighbourlist.clear();
        intersectionList.clear();
334

335
        if (line.count() == 2 && poly.count() >= 3) {
336

337
            for (int i = 0; i < poly.count(); i++) {
338
                QGCMapPolyline polySegment;
339 340
                QGeoCoordinate currentVertex = poly.vertexCoordinate(i);
                QGeoCoordinate nextVertex = poly.vertexCoordinate(poly.nextVertexIndex(i));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
341 342
                polySegment.appendVertex(currentVertex);
                polySegment.appendVertex(nextVertex);
343

344
                QGeoCoordinate intersectionPoint;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
345
                //bool retVal2 = intersects(line, line, &intersectionPoint);
346
                bool retVal = intersects(line, polySegment, intersectionPoint);
347 348 349


                if (retVal != false){
350
                    intersectionList.append(intersectionPoint);
351

352 353
                    QPair<int, int>     neighbours;
                    neighbours.first    = i;
354 355
                    neighbours.second   = poly.nextVertexIndex(i);
                    neighbourlist.append(neighbours);
356 357
                }
            }
358

359
            if (intersectionList.count() > 0) {
360 361 362 363
                return true;
            } else {
                return false;
            }
364 365
        } else {
            qWarning("WimaArea::intersects(line, poly): line->count() != 2 || poly->count() < 3");
366
            return false;
367 368 369
        }
}

370
double WimaArea::distInsidePoly(const QGeoCoordinate &c1, const QGeoCoordinate &c2, WimaArea poly)
371
{
372 373
        poly.offset(0.1); // hack to compensate for numerical issues, migh be replaced in the future...
        if ( poly.containsCoordinate(c1) && poly.containsCoordinate(c2)) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
374 375 376 377
            QList<QGeoCoordinate>   intersectionList;
            QList<QPair<int, int>>  neighbourlist;
            QGCMapPolyline line;

378 379
            line.appendVertex(c1);
            line.appendVertex(c2);
380
            intersects(line, poly, intersectionList, neighbourlist);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
381 382

            if ( intersectionList.size() == 0 ){
383
                return c1.distanceTo(c2);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
384 385
            } else {
                return std::numeric_limits<qreal>::infinity();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
386 387
            }

388 389
        } else {
            return std::numeric_limits<qreal>::infinity();
390 391 392
        }
}

393
bool WimaArea::dijkstraPath(const QGeoCoordinate &start, const QGeoCoordinate &end, const WimaArea &poly, QList<QGeoCoordinate> &dijkstraPath)
394
{
395 396 397 398
    if ( isSelfIntersecting(poly) ) {
        return false;
    }

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    struct Node{
        QGeoCoordinate coordinate;
        double distance = std::numeric_limits<qreal>::infinity();
        Node* predecessorNode = nullptr;
    };

    QList<Node> nodeList;
    QList<Node*> workingSet;


    // initialize
    // start
    Node startNode;
    startNode.coordinate    = start;
    startNode.distance      = 0;
    nodeList.append(startNode);

    //poly
    for (int i = 0; i < poly.count(); i++) {
        Node node;
        node.coordinate = poly.vertexCoordinate(i);
        nodeList.append(node);
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
422

423 424 425 426
    //end
    Node endNode;
    endNode.coordinate  = end;
    nodeList.append(endNode);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
427

428 429 430 431 432
    // working set
    for (int i = 0; i < nodeList.size(); i++) {
        Node* nodePtr = &nodeList[i];
        workingSet.append(nodePtr);
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
433 434


435 436 437 438 439 440 441 442 443 444 445 446
    // 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
447
            }
448 449
        }
        Node* u = workingSet.takeAt(minDistIndex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
450 451


452 453 454
        //update distance
        for (int i = 0; i < workingSet.size(); i++) {
            Node* v = workingSet[i];
Valentin Platzgummer's avatar
Valentin Platzgummer committed
455

456 457 458 459 460 461
            // is neighbour?
            double dist = distInsidePoly(u->coordinate, v->coordinate, poly);
            double alternative = u->distance + dist;
            if (alternative < v->distance)  {
                v->distance         = alternative;
                v->predecessorNode  = u;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
462 463 464
            }
        }

465
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
466

467 468 469 470
    // create path
    Node* Node = &nodeList.last();
    if (Node->predecessorNode == nullptr) {
        qWarning("WimaArea::dijkstraPath(): Error, no path found!");
471
        return false;
472
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
473

474 475 476 477 478 479 480
    while (1) {
        dijkstraPath.prepend(Node->coordinate);

        //Update Node
        Node = Node->predecessorNode;
        if (Node == nullptr) {
            break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
481
        }
482
    }
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

    return true;
}

bool WimaArea::isSelfIntersecting(const WimaArea &poly)
{
    int i = 0;
    if (poly.count() > 3) {
        while(i < poly.count()-1) {
            QGCMapPolyline refLine;
            refLine.appendVertex(poly.vertexCoordinate(i));
            refLine.appendVertex(poly.vertexCoordinate(poly.nextVertexIndex(i)));
            int j = poly.nextVertexIndex(i);
            while(j < poly.count()) {
                QGeoCoordinate dummy;
                QGCMapPolyline iteratorLine;
                iteratorLine.appendVertex(poly.vertexCoordinate(j));
                iteratorLine.appendVertex(poly.vertexCoordinate(poly.nextVertexIndex(j)));

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

                j++;
            }
            i++;
        }
    }

    return false;
}

bool WimaArea::isSelfIntersecting()
{
    return isSelfIntersecting(*this);
517
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
518

519 520 521 522 523 524 525
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
526

527 528 529 530 531 532 533
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 {
534
            errorString.append(tr("Could not load Maximum Altitude value!\n"));
535 536
            return false;
        }
537
    } else {
538 539
        qWarning() << errorString;
        return false;
540 541
    }
}
542

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
void WimaArea::init()
{
    this->setObjectName(wimaAreaName);
}

void print(const WimaArea &area)
{
    QString message;
    print(area, message);
    qWarning() << message;
}

void print(const WimaArea &area, QString &outputString)
{
    outputString.append(QString("Type: %s").arg(area.objectName()));
    print(static_cast<const QGCMapPolygon&>(area), outputString);
    outputString.append(QString("Maximum Altitude: %.3f").arg(area._maxAltitude));
}


void swap(WimaArea &area1, WimaArea &area2)
{
    using std::swap;
    swap(static_cast<QGCMapPolygon&>(area1), static_cast<QGCMapPolygon&>(area2));
    swap(area1._maxAltitude, area2._maxAltitude);
}