WimaArea.cc 18.5 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
const char* WimaArea::borderPolygonOffsetName  = "BorderPolygonOffset";
26 27
const char* WimaArea::showBorderPolygonName    = "ShowBorderPolygon";
const char* WimaArea::settingsGroup            = "MeasurementArea";
28

29
// Constructors
30
WimaArea::WimaArea(QObject *parent)
31 32 33 34 35
    :     QGCMapPolygon (parent)
    ,     _metaDataMap            (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/WimaArea.SettingsGroup.json"), this /* QObject parent */))
    ,     _borderPolygonOffset    (SettingsFact(settingsGroup, _metaDataMap[borderPolygonOffsetName], this /* QObject parent */))
    ,     _showBorderPolygon      (SettingsFact(settingsGroup, _metaDataMap[showBorderPolygonName], this /* QObject parent */))
    ,     _borderPolygon          (QGCMapPolygon(this))
36
    ,     _wimaAreaInteractive            (false)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
37
{
38
    init();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
39 40
    _maxAltitude = 30;
}
41

Valentin Platzgummer's avatar
Valentin Platzgummer committed
42
WimaArea::WimaArea(const WimaArea &other, QObject *parent)
43
    :     QGCMapPolygon (parent)
44 45
    ,     _metaDataMap            (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/WimaArea.SettingsGroup.json"), this /* QObject parent */))
    ,     _borderPolygonOffset    (SettingsFact(settingsGroup, _metaDataMap[borderPolygonOffsetName], this /* QObject parent */))
46 47
    ,     _showBorderPolygon      (SettingsFact(settingsGroup, _metaDataMap[showBorderPolygonName], this /* QObject parent */))
    ,     _borderPolygon          (QGCMapPolygon(this))
48
    ,     _wimaAreaInteractive            (false)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
49
{
50
    init();
51 52 53 54 55 56 57 58 59 60 61 62 63 64
    *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
65
    this->setPath(other.path());
66 67

    return *this;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
68
}
69

70 71 72 73 74 75 76 77 78
void WimaArea::setWimaAreaInteractive(bool interactive)
{
    if (WimaArea::_wimaAreaInteractive != interactive) {
        WimaArea::_wimaAreaInteractive = interactive;

        emit WimaArea::wimaAreaInteractiveChanged();
    }
}

79 80 81 82 83 84 85
/*!
  \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)
86
{
87 88
    if ( altitude > 0 && qFuzzyCompare(altitude, _maxAltitude) ) {
        _maxAltitude = altitude;
89 90 91 92
        emit maxAltitudeChanged();
    }
}

93 94 95 96 97
void WimaArea::setShowBorderPolygon(bool showBorderPolygon)
{
    _showBorderPolygon.setRawValue(showBorderPolygon);
}

98 99 100 101 102 103 104 105 106 107 108


void WimaArea::setBorderPolygonOffset(double offset)
{
    if ( !qFuzzyCompare(_borderPolygonOffset.rawValue().toDouble(), offset) ) {
        _borderPolygonOffset.setRawValue(offset);

        emit borderPolygonOffsetChanged();
    }
}

109
void WimaArea::recalcPolygons()
110
{
111 112 113
    if (_showBorderPolygon.rawValue().toBool() == true) {

        if ( _borderPolygon.count() >= 3 ) {
114
            //_borderPolygon.verifyClockwiseWinding(); // causes seg. fault
115 116 117 118 119 120
            this->setPath(_borderPolygon.coordinateList());
            this->offset(-_borderPolygonOffset.rawValue().toDouble());
        }
    } else {

        if (this->count() >= 3){
121
            //this->verifyClockwiseWinding(); // causes seg. fault
122 123 124 125 126 127 128
            _borderPolygon.setPath(this->coordinateList());
            _borderPolygon.offset(_borderPolygonOffset.rawValue().toDouble());
        }

        emit borderPolygonChanged();
    }

129

130 131 132 133 134 135 136 137 138 139 140
}

void WimaArea::updatePolygonConnections(QVariant showBorderPolygon)
{
    if (showBorderPolygon.toBool() == true) {
        connect(&_borderPolygon, &QGCMapPolygon::pathChanged, this, &WimaArea::recalcPolygons);
        disconnect(this, &QGCMapPolygon::pathChanged, this, &WimaArea::recalcPolygons);
    } else {
        disconnect(&_borderPolygon, &QGCMapPolygon::pathChanged, this, &WimaArea::recalcPolygons);
        connect(this, &QGCMapPolygon::pathChanged, this, &WimaArea::recalcPolygons);
    }
141 142
}

143 144 145
void WimaArea::recalcInteractivity()
{
    if ( _wimaAreaInteractive == false) {
146
        QGCMapPolygon::setInteractive(false);
147 148 149 150
        _borderPolygon.setInteractive(false);
    } else {
        if (_showBorderPolygon.rawValue().toBool() == true) {
            _borderPolygon.setInteractive(true);
151
            QGCMapPolygon::setInteractive(false);
152 153
        } else {
            _borderPolygon.setInteractive(false);
154
            QGCMapPolygon::setInteractive(true);
155 156 157 158
        }
    }
}

159 160 161 162 163 164 165
/*!
 * \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
 */
166
int WimaArea::getClosestVertexIndex(const QGeoCoordinate &coordinate) const
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
    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;
    }
}

188 189 190 191 192 193
/*!
 * \fn  QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
 *  Returns the vertex of the polygon path with the least distance to \a coordinate.
 *
 * \sa QGeoCoordinate
 */
194
QGeoCoordinate WimaArea::getClosestVertex(const QGeoCoordinate& coordinate) const
195 196 197 198
{
    return this->vertexCoordinate(getClosestVertexIndex(coordinate));
}

199 200 201 202 203
/*!
 * \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)
204
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
205
    QGCMapPolygon qgcPoly;
206
    qgcPoly.setPath(area.path());
207

Valentin Platzgummer's avatar
Valentin Platzgummer committed
208
    return QGCMapPolygon(qgcPoly);
209 210
}

211 212 213 214
/*!
 * \fn QGCMapPolygon WimaArea::toQGCPolygon() const
 * Converts the calling \c WimaArea to \c QGCMapPolygon by copying the path only.
 */
215 216 217 218 219
QGCMapPolygon WimaArea::toQGCPolygon() const
{
    return toQGCPolygon(*this);
}

220
/*!
221
 * \fn bool WimaArea::join(WimaArea &area1, WimaArea &area2, WimaArea &joinedArea, QString &errorString)
222 223
 * Joins the areas \a area1 and \a area2 such that a \l {Simple Polygon} is created.
 * Stores the result inside \a joinedArea.
224
 * Stores error messages in \a errorString.
225 226 227 228
 * 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.
 */
229
bool WimaArea::join(const WimaArea &area1, const WimaArea &area2, WimaArea &joinedArea, QString &errorString)
230
{
231 232
    using namespace GeoUtilities;
    using namespace PolygonCalculus;
233

Valentin Platzgummer's avatar
Valentin Platzgummer committed
234 235
    Q_UNUSED(errorString);

236 237
    QList<QGeoCoordinate> GeoPolygon1 = area1.coordinateList();
    QList<QGeoCoordinate> GeoPolygon2 = area2.coordinateList();
238

239 240 241
//    qWarning("befor joining");
//    qWarning() << GeoPolygon1;
//    qWarning() << GeoPolygon2;
242

243
    QGeoCoordinate origin = GeoPolygon1[0];
244

245
//    QGeoCoordinate tset = GeoPolygon1[2];
246

247
//    qWarning() << tset;qWarning() << toGeo(toCartesian2D(tset, origin), origin);
248 249 250



251 252
    QPolygonF polygon1 = toQPolygonF(toCartesian2D(GeoPolygon1, origin));
    QPolygonF polygon2 = toQPolygonF(toCartesian2D(GeoPolygon2, origin));
253

254 255 256
//    qWarning("after 1 transform");
//    qWarning() << polygon1;
//    qWarning() << polygon2;
257 258 259 260 261

    QPolygonF joinedPolygon;
    JoinPolygonError retValue = PolygonCalculus::join(polygon1, polygon2, joinedPolygon);


262 263
//    qWarning("after joining");
//    qWarning() << joinedPolygon;
264 265 266 267 268 269 270 271

    if (retValue == JoinPolygonError::Disjoint) {
        qWarning("Polygons are disjoint.");
    } else if (retValue == JoinPolygonError::NotSimplePolygon) {
        qWarning("Not a simple polygon.");
    } else if (retValue == JoinPolygonError::PathSizeLow) {
        qWarning("Polygon vertex count is low.");
    } else {
272
        QVector<QGeoCoordinate> path = toGeo(toQPointFList(joinedPolygon), origin);
273 274
//        qWarning("after transform");
//        qWarning() << path;
275 276 277 278 279
        joinedArea.setPath(path);
        return true;
    }

    return false;
280
}
281

282 283 284 285 286 287 288 289 290

/*!
 * \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.
 */
Valentin Platzgummer's avatar
Valentin Platzgummer committed
291
bool WimaArea::join(const WimaArea &area1, const WimaArea &area2, WimaArea &joinedArea)
292 293 294 295 296
{
    QString dummy;
    return join(area1, area2, joinedArea, dummy);
}

297 298 299 300 301 302 303 304 305
/*!
 * \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)
306
{
Valentin Platzgummer's avatar
Valentin Platzgummer committed
307
    WimaArea joinedArea;
308
    if ( join(*this, area, joinedArea) ) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
309 310
        //qWarning("WimaArea::join(WimaArea &area)");
        //qWarning() << joinedArea.coordinateList();
311 312 313 314 315
        this->setPath(joinedArea.path());
        return true;
    } else {
        return false;
    }
316
}
317

318

319
/*!
320 321 322 323 324 325 326 327 328
 * \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.
329
 */
330
bool WimaArea::join(WimaArea &area, QString &errorString)
331
{
332 333 334
    WimaArea joinedArea;
    if ( join(*this, area, joinedArea, errorString) ) {
        this->setPath(joinedArea.path());
335 336 337 338
        return true;
    } else {
        return false;
    }
339 340
}

341 342 343 344 345 346
/*!
 * \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.
 */
347
int WimaArea::nextVertexIndex(int index) const
348 349 350 351 352 353 354 355 356 357 358
{
    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;
    }
}

359 360 361 362 363 364
/*!
 * \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.
 */
365
int WimaArea::previousVertexIndex(int index) const
366 367 368 369 370 371 372 373 374 375 376
{
    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;
    }
}

377 378 379 380 381
/*!
 * \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}.
 */
382
bool WimaArea::isSimplePolygon() const
383
{
384 385 386 387 388 389 390 391 392
    using namespace PolygonCalculus;
    using namespace GeoUtilities;

    if (this->count() > 2) {
        QPolygonF polygon = toQPolygonF(toCartesian2D(this->coordinateList(), this->vertexCoordinate(0)));
        return PolygonCalculus::isSimplePolygon(polygon);
    } else
        return false;

393
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
394

395 396 397 398 399 400 401 402 403 404 405 406 407
bool WimaArea::containsCoordinate(const QGeoCoordinate &coordinate) const
{
    using namespace PlanimetryCalculus;
    using namespace PolygonCalculus;
    using namespace GeoUtilities;

    if (this->count() > 2) {
        QPolygonF polygon = toQPolygonF(toCartesian2D(this->coordinateList(), coordinate));
        return PlanimetryCalculus::contains(polygon, QPointF(0,0));
    } else
        return false;
}

408 409 410 411 412 413
/*!
 * \fn void WimaArea::saveToJson(QJsonObject &json)
 * Saves the calling area to \c QJsonObject object and stores it inside \a json.
 *
 * \sa QJsonObject
 */
414 415 416
void WimaArea::saveToJson(QJsonObject &json)
{
    this->QGCMapPolygon::saveToJson(json);
417 418

    json[maxAltitudeName]           = _maxAltitude;
419
    json[borderPolygonOffsetName]   = _borderPolygonOffset.rawValue().toDouble();
420 421
    json[showBorderPolygonName]     = _showBorderPolygon.rawValue().toDouble();
    json[areaTypeName]              = wimaAreaName;
422
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
423

424 425 426 427 428 429 430 431
/*!
 * \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
 */
432 433 434
bool WimaArea::loadFromJson(const QJsonObject &json, QString& errorString)
{
    if ( this->QGCMapPolygon::loadFromJson(json, false /*no poly required*/, errorString) ) {
435
        if ( json.contains(maxAltitudeName) && json[maxAltitudeName].isDouble()) {
436 437
            _maxAltitude = json[maxAltitudeName].toDouble();
        } else {
438
            errorString.append(tr("Could not load Maximum Altitude value!\n"));
439 440
            return false;
        }
441 442 443 444 445 446 447

        if ( json.contains(borderPolygonOffsetName) && json[borderPolygonOffsetName].isDouble()) {
            _borderPolygonOffset.setRawValue(json[borderPolygonOffsetName].toDouble());
        } else {
            errorString.append(tr("Could not load border polygon offset value!\n"));
            return false;
        }
448 449 450 451 452 453 454

        if ( json.contains(showBorderPolygonName) && json[showBorderPolygonName].isDouble()) {
            _showBorderPolygon.setRawValue(json[showBorderPolygonName].toBool());
        } else {
            errorString.append(tr("Could not load border polygon offset value!\n"));
            return false;
        }
455
    } else {
456 457
        qWarning() << errorString;
        return false;
458
    }
459 460

    return true;
461
}
462

463 464 465 466
/*!
 * \fn void WimaArea::init()
 * Funtion to be called during construction.
 */
467 468 469
void WimaArea::init()
{
    this->setObjectName(wimaAreaName);
470 471 472 473 474 475 476 477

    if (_showBorderPolygon.rawValue().toBool() == true){
        connect(&_borderPolygon, &QGCMapPolygon::pathChanged, this, &WimaArea::recalcPolygons);

    } else {
        connect(this, &QGCMapPolygon::pathChanged, this, &WimaArea::recalcPolygons);
    }

478
    connect(&_borderPolygonOffset,  &SettingsFact::rawValueChanged, this,   &WimaArea::recalcPolygons);
479
    connect(&_showBorderPolygon, &SettingsFact::rawValueChanged, this, &WimaArea::updatePolygonConnections);
480 481
    connect(&_showBorderPolygon, &SettingsFact::rawValueChanged, this, &WimaArea::recalcInteractivity);
    connect(this, &WimaArea::wimaAreaInteractiveChanged, this, &WimaArea::recalcInteractivity);
482 483
}

484 485 486 487
/*!
 * \fn void print(const WimaArea &area)
 * Prints the data contained in \a area to the console.
 */
488 489 490 491 492 493 494
void print(const WimaArea &area)
{
    QString message;
    print(area, message);
    qWarning() << message;
}

495 496 497 498
/*!
 * \fn void print(const WimaArea &area)
 * Prints the data contained in \a area to the \a outputString.
 */
499 500
void print(const WimaArea &area, QString &outputString)
{
501
    outputString.append(QString("Type: %1\n").arg(area.objectName()));
502
    print(static_cast<const QGCMapPolygon&>(area), outputString);
503
    outputString.append(QString("Maximum Altitude: %1\n").arg(area._maxAltitude));
504 505 506 507 508 509 510
    outputString.append(QString("Border Polygon Offset: %1\n").arg(area._borderPolygonOffset.rawValue().toDouble()));

    outputString.append(QString("Border Polygon Coordinates\n").arg(area._borderPolygonOffset.rawValue().toDouble()));
    for (int i = 0; i < area._borderPolygon.count(); i++) {
        QGeoCoordinate coordinate = area._borderPolygon.vertexCoordinate(i);
        outputString.append(QString("%1\n").arg(coordinate.toString(QGeoCoordinate::Degrees)));
    }
511 512
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 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

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