WimaArea.cc 16.5 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

Valentin Platzgummer's avatar
Valentin Platzgummer committed
9 10
WimaArea::WimaArea()
    : QGCMapPolygon (nullptr)
11
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
12 13 14
    this->setObjectName(wimaAreaName);
    _maxAltitude = 30;
    _wimaVehicle = new WimaVehicle(this);
15 16
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
17 18
WimaArea::WimaArea(QObject *parent)
    :   QGCMapPolygon (parent)
19
{
20
    this->setObjectName(wimaAreaName);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
21 22
    _maxAltitude = 30;
    _wimaVehicle = new WimaVehicle(this);
23 24
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
25 26 27 28 29 30 31
WimaArea::WimaArea(const QGCMapPolygon &other, QObject *parent)
    : QGCMapPolygon (other, parent)
{
    this->setObjectName(wimaAreaName);
    _maxAltitude = 30;
    _wimaVehicle = new WimaVehicle(this);
}
32

Valentin Platzgummer's avatar
Valentin Platzgummer committed
33 34 35 36 37 38 39 40 41 42 43
WimaArea::WimaArea(const WimaArea &other, QObject *parent)
    : WimaArea (parent)
{
    this->setObjectName(wimaAreaName);
    this->setPath(other.path());
    this->setCenter(other.center());
    this->setCenterDrag(other.centerDrag());
    this->setInteractive(other.interactive());
    _maxAltitude = other.maxAltitude();
    _wimaVehicle = other.vehicle();
}
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61


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

void WimaArea::setVehicle(WimaVehicle *vehicle)
{
    if(_wimaVehicle != vehicle){
        _wimaVehicle->deleteLater();
        _wimaVehicle =  vehicle;
        emit vehicleChanged();
    }
}
62

63 64 65 66 67 68 69 70 71 72 73
/*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;
74
}
75 76 77 78 79 80 81


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

82
int WimaArea::getClosestVertexIndex(const QGeoCoordinate &coordinate) const
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
    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;
    }
}

104
QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
105 106 107 108
{
    return this->vertexCoordinate(getClosestVertexIndex(coordinate));
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
109
QGCMapPolygon WimaArea::toQGCPolygon(const WimaArea &poly) const
110
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
111 112 113 114 115
    QGCMapPolygon qgcPoly;
    qgcPoly.setPath(poly.path());
    qgcPoly.setCenter(poly.center());
    qgcPoly.setCenterDrag(poly.centerDrag());
    qgcPoly.setInteractive(poly.interactive());
116

Valentin Platzgummer's avatar
Valentin Platzgummer committed
117
    return QGCMapPolygon(qgcPoly);
118 119
}

120
void WimaArea::join(QList<WimaArea *>* polyList,  WimaArea* joinedPoly)
121
{
122
    return;
123 124
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
125
void WimaArea::join(WimaArea &poly1, WimaArea &poly2, WimaArea &joinedPoly)
126
{
127

128 129
        if (poly1.count() >= 3 && poly2.count() >= 3) {

Valentin Platzgummer's avatar
Valentin Platzgummer committed
130 131
            poly1.verifyClockwiseWinding();
            poly2.verifyClockwiseWinding();
132

Valentin Platzgummer's avatar
Valentin Platzgummer committed
133 134
            WimaArea* walkerPoly    = &poly1; // "walk" on this polygon towards higher indices
            WimaArea* crossPoly     = &poly2; // check for crossings with this polygon while "walking"
135 136
                                             // and swicht to this polygon on a intersection,
                                             // continue to walk towards higher indices
137 138 139 140 141 142 143 144 145 146 147 148 149 150

            // 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) {
151
                joinedPoly.appendVertices(crossPoly->coordinateList());
152 153 154 155 156 157
                return;
            }


            QGeoCoordinate currentVertex    = walkerPoly->vertexCoordinate(startIndex);
            QGeoCoordinate startVertex      = currentVertex;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
158
            // possible nextVertex (if no intersection between currentVertex and protoVertex with crossPoly)
159 160 161 162
            QGeoCoordinate protoNextVertex  = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(startIndex));

            int nextVertexIndex = walkerPoly->nextVertexIndex(startIndex);
            while (1) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
163
                //qDebug("nextVertexIndex: %i", nextVertexIndex);
164
                joinedPoly.appendVertex(currentVertex);
165 166 167

                QGCMapPolyline walkerPolySegment;
                walkerPolySegment.appendVertex(currentVertex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
168
                walkerPolySegment.appendVertex(protoNextVertex);
169

170 171
                QList<QPair<int, int>> neighbourList;
                QList<QGeoCoordinate> intersectionList;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
172
                //qDebug("IntersectionList.size() on init: %i", intersectionList.size());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
173
                intersects(walkerPolySegment, *crossPoly, intersectionList, neighbourList);
174

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

177 178
                if (intersectionList.size() >= 1) {
                    int minDistIndex = 0;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
179

180 181 182 183
                    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
184

185 186 187 188 189 190
                            if ( minDist > currentDist ) {
                                minDist         = currentDist;
                                minDistIndex    = i;
                            }
                        }
                    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
191

Valentin Platzgummer's avatar
Valentin Platzgummer committed
192
                    //qDebug("MinDistIndex: %i", minDistIndex);
193 194 195 196 197 198 199 200 201 202 203 204
                    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
205
                    } else {
206 207 208
                        currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                        protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                        nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
209
                    }
210

211
                } else {
212 213 214 215 216 217 218
                    currentVertex   = walkerPoly->vertexCoordinate(nextVertexIndex);
                    protoNextVertex = walkerPoly->vertexCoordinate(walkerPoly->nextVertexIndex(nextVertexIndex));
                    nextVertexIndex = walkerPoly->nextVertexIndex(nextVertexIndex);
                }

                if (currentVertex == startVertex) {
                    return;
219
                }
220
            }
221 222 223

        } else {
            qWarning("WimaArea::joinPolygons(poly1, poly2): poly->count() < 3");
224
            return;
225
        }
226
}
227

Valentin Platzgummer's avatar
Valentin Platzgummer committed
228
void WimaArea::join(WimaArea &poly)
229
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
230 231
    WimaArea joinedArea;
    join(*this, poly, joinedArea);
232 233 234
    this->setPath(joinedArea.path());
    return;
}
235

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

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

273
int WimaArea::nextVertexIndex(int index) const
274 275 276 277 278 279 280 281 282 283 284
{
    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;
    }
}

285
int WimaArea::previousVertexIndex(int index) const
286 287 288 289 290 291 292 293 294 295 296
{
    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;
    }
}

297
bool WimaArea::intersects(const QGCMapPolyline &line1, const QGCMapPolyline &line2, QGeoCoordinate &intersectionPt)
298
{
299 300

        if (line1.count() == 2 && line2.count() == 2 ) {
301 302 303
            QPointF pt11(0, 0);

            double x, y, z;
304 305
            QGeoCoordinate origin = line1.vertexCoordinate(0);
            convertGeoToNed(line1.vertexCoordinate(1), origin, &x, &y, &z);
306 307 308 309 310
            QPointF pt12(x, y);

            QLineF kartLine1(pt11, pt12);


311
            convertGeoToNed(line2.vertexCoordinate(0), origin, &x, &y, &z);
312 313
            QPointF pt21(x, y);

314
            convertGeoToNed(line2.vertexCoordinate(1), origin, &x, &y, &z);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
315
            QPointF pt22(x, y);;
316 317 318 319

            QLineF kartLine2(pt21, pt22);

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


        } else {
329 330
            qWarning("WimaArea::intersect(line1, line2):  line1->count() != 2 || line2->count() != 2!");
            return false;
331 332 333
        }
}

334
bool WimaArea::intersects(const QGCMapPolyline& line, const WimaArea& poly, QList<QGeoCoordinate>& intersectionList, QList<QPair<int, int> >& neighbourlist)
335
{
336 337
        neighbourlist.clear();
        intersectionList.clear();
338

339
        if (line.count() == 2 && poly.count() >= 3) {
340

341
            for (int i = 0; i < poly.count(); i++) {
342
                QGCMapPolyline polySegment;
343 344
                QGeoCoordinate currentVertex = poly.vertexCoordinate(i);
                QGeoCoordinate nextVertex = poly.vertexCoordinate(poly.nextVertexIndex(i));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
345 346
                polySegment.appendVertex(currentVertex);
                polySegment.appendVertex(nextVertex);
347

348
                QGeoCoordinate intersectionPoint;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
349
                //bool retVal2 = intersects(line, line, &intersectionPoint);
350
                bool retVal = intersects(line, polySegment, intersectionPoint);
351 352 353


                if (retVal != false){
354
                    intersectionList.append(intersectionPoint);
355

356 357
                    QPair<int, int>     neighbours;
                    neighbours.first    = i;
358 359
                    neighbours.second   = poly.nextVertexIndex(i);
                    neighbourlist.append(neighbours);
360 361
                }
            }
362

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

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

383 384 385
            line.appendVertex(c1);
            line.appendVertex(c2);
            intersects(line, bigPoly, intersectionList, neighbourlist);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
386 387 388

            //if ( intersectionList.size() == (c1InPolyRim || c2InPolyRim ? 2:0) ){
            if ( intersectionList.size() == 0 ){
389
                return c1.distanceTo(c2);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
390 391
            } else {
                return std::numeric_limits<qreal>::infinity();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
392 393
            }

394 395
        } else {
            return std::numeric_limits<qreal>::infinity();
396 397 398
        }
}

399
void WimaArea::dijkstraPath(const QGeoCoordinate &start, const QGeoCoordinate &end, const WimaArea &poly, QList<QGeoCoordinate> &dijkstraPath)
400
{
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    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
424

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

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


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


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

458 459 460 461 462 463
            // 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
464 465 466
            }
        }

467
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
468

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

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

        //Update Node
        Node = Node->predecessorNode;
        if (Node == nullptr) {
            break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
483
        }
484 485
    }
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
486

487 488 489 490 491 492 493
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
494

495 496 497 498 499 500 501 502 503 504
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 {
            errorString.append("Could not load Maximum Altitude value!\n");
            return false;
        }
505
    } else {
506 507
        qWarning() << errorString;
        return false;
508 509
    }
}
510