SnakeThread.cc 17.5 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include "SnakeThread.h"

#include <QGeoCoordinate>
#include <QMutexLocker>

#include "QGCApplication.h"
#include "QGCToolbox.h"
#include "SettingsFact.h"
#include "SettingsManager.h"
#include "WimaSettings.h"

#include <iostream>
#include <memory>
#include <mutex>
#include <shared_mutex>

#include "Wima/Snake/SnakeTiles.h"
#include "Wima/Snake/SnakeTilesLocal.h"
#include "snake.h"

template <class Callable> class CommandRAII {
public:
  CommandRAII(Callable &fun) : _fun(fun) {}

  ~CommandRAII() { _fun(); }

private:
  Callable _fun;
};

using QVariantList = QList<QVariant>;

using UniqueLock = std::unique_lock<shared_timed_mutex>;
using SharedLock = std::shared_lock<shared_timed_mutex>;

class SnakeThread::Impl {
public:
  struct Input {

    Input()
        : tileWidth(5 * si::meter), tileHeight(5 * si::meter),
42
          minTileArea(5 * si::meter * si::meter), lineDistance(2 * si::meter),
Valentin Platzgummer's avatar
Valentin Platzgummer committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
          minTransectLength(1 * si::meter), scenarioChanged(true),
          progressChanged(true), routeParametersChanged(true) {}

    QList<QGeoCoordinate> mArea;
    QGeoCoordinate ENUOrigin;
    QList<QGeoCoordinate> sArea;
    QList<QGeoCoordinate> corridor;
    Length tileWidth;
    Length tileHeight;
    Area minTileArea;

    Length lineDistance;
    Length minTransectLength;

    QNemoProgress progress;

    std::atomic_bool scenarioChanged;
    std::atomic_bool progressChanged;
    std::atomic_bool routeParametersChanged;

    mutable std::shared_timed_mutex mutex;
  };

  struct Output {
    Output()
        : calcInProgress(false), tilesUpdated(false), waypointsUpdated(false),
          progressUpdated(false) {}

    SnakeTiles tiles;
    QVariantList tileCenterPoints;
    SnakeTilesLocal tilesENU;
    QVector<QPointF> tileCenterPointsENU;
    QGeoCoordinate ENUOrigin;

    QVector<QGeoCoordinate> waypoints;
    QVector<QGeoCoordinate> arrivalPath;
    QVector<QGeoCoordinate> returnPath;
    QVector<QPointF> waypointsENU;
    QVector<QPointF> arrivalPathENU;
    QVector<QPointF> returnPathENU;

    QString errorMessage;

    std::atomic_bool calcInProgress;
    bool tilesUpdated;
    bool waypointsUpdated;
    bool progressUpdated;

    mutable std::shared_timed_mutex mutex;
  };

  Impl(SnakeThread *p);

  bool precondition() const;
  bool doTopicServiceSetup();

  // Internal data.
  snake::Scenario scenario;
  SnakeThread *parent;

  // Input
  Input input;
  // Output
  Output output;
};

SnakeThread::Impl::Impl(SnakeThread *p) : parent(p) {}

SnakeThread::SnakeThread(QObject *parent)
    : QThread(parent), pImpl(std::make_unique<Impl>(this)) {}

SnakeThread::~SnakeThread() {}

void SnakeThread::setMeasurementArea(
    const QList<QGeoCoordinate> &measurementArea) {
  this->pImpl->input.scenarioChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->input.mArea = measurementArea;
  for (auto &vertex : this->pImpl->input.mArea) {
    vertex.setAltitude(0);
  }
}

void SnakeThread::setServiceArea(const QList<QGeoCoordinate> &serviceArea) {
  this->pImpl->input.scenarioChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->input.sArea = serviceArea;
  for (auto &vertex : this->pImpl->input.sArea) {
    vertex.setAltitude(0);
  }
}

void SnakeThread::setCorridor(const QList<QGeoCoordinate> &corridor) {
  this->pImpl->input.scenarioChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->input.corridor = corridor;
  for (auto &vertex : this->pImpl->input.corridor) {
    vertex.setAltitude(0);
  }
}

void SnakeThread::setProgress(const QVector<int> &progress) {
  this->pImpl->input.progressChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->input.progress.progress() = progress;
}

SnakeTiles SnakeThread::tiles() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.tiles;
}

SnakeTilesLocal SnakeThread::tilesENU() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.tilesENU;
}

QGeoCoordinate SnakeThread::ENUOrigin() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.ENUOrigin;
}

QVariantList SnakeThread::tileCenterPoints() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.tileCenterPoints;
}

QVector<int> SnakeThread::progress() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->input.progress.progress();
}

bool SnakeThread::calcInProgress() const {
  return this->pImpl->output.calcInProgress.load();
}

QString SnakeThread::errorMessage() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.errorMessage;
}

bool SnakeThread::tilesUpdated() {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.tilesUpdated;
}

bool SnakeThread::waypointsUpdated() {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.waypointsUpdated;
}

bool SnakeThread::progressUpdated() {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.progressUpdated;
}

QVector<QGeoCoordinate> SnakeThread::waypoints() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.waypoints;
}

QVector<QGeoCoordinate> SnakeThread::arrivalPath() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.arrivalPath;
}

QVector<QGeoCoordinate> SnakeThread::returnPath() const {
  SharedLock lk(this->pImpl->output.mutex);
  return this->pImpl->output.returnPath;
}

Length SnakeThread::lineDistance() const {
  SharedLock lk(this->pImpl->input.mutex);
  return this->pImpl->input.lineDistance;
}

void SnakeThread::setLineDistance(Length lineDistance) {
  this->pImpl->input.routeParametersChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->input.routeParametersChanged = true;
  this->pImpl->input.lineDistance = lineDistance;
}

Area SnakeThread::minTileArea() const {
  SharedLock lk(this->pImpl->input.mutex);
  return this->pImpl->scenario.minTileArea();
}

void SnakeThread::setMinTileArea(Area minTileArea) {
  this->pImpl->input.scenarioChanged = true;
  this->pImpl->input.scenarioChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->scenario.setMinTileArea(minTileArea);
}

Length SnakeThread::tileHeight() const {
  SharedLock lk(this->pImpl->input.mutex);
  return this->pImpl->scenario.tileHeight();
}

void SnakeThread::setTileHeight(Length tileHeight) {
  this->pImpl->input.scenarioChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->scenario.setTileHeight(tileHeight);
}

Length SnakeThread::tileWidth() const {
  SharedLock lk(this->pImpl->input.mutex);
  return this->pImpl->scenario.tileWidth();
}

void SnakeThread::setTileWidth(Length tileWidth) {
  this->pImpl->input.scenarioChanged = true;
  UniqueLock lk(this->pImpl->input.mutex);
  this->pImpl->scenario.setTileWidth(tileWidth);
}

void SnakeThread::run() {
#ifndef NDEBUG
  auto startTime = std::chrono::high_resolution_clock::now();
#endif
  // Signal that calculation is in progress.
  this->pImpl->output.calcInProgress.store(true);
  emit calcInProgressChanged(this->pImpl->output.calcInProgress.load());
#ifndef NDEBUG
  auto onExit = [this, &startTime] {
#else
  auto onExit = [this] {
#endif
#ifndef NDEBUG
    qDebug() << "SnakeThread::run() execution time: "
             << std::chrono::duration_cast<std::chrono::milliseconds>(
                    std::chrono::high_resolution_clock::now() - startTime)
                    .count()
             << " ms";
#endif
    this->pImpl->output.calcInProgress.store(false);
    emit calcInProgressChanged(this->pImpl->output.calcInProgress.load());
  };
  CommandRAII<decltype(onExit)> onExitRAII(onExit);

  bool tilesValid = true;
  QGeoCoordinate origin;
  {
    // Check preconditions.
    SharedLock lk(this->pImpl->input.mutex);
    if (this->pImpl->input.mArea.size() < 3) {
      UniqueLock uLock(this->pImpl->output.mutex);
      this->pImpl->output.errorMessage = "Measurement area invalid: size < 3.";
      tilesValid = false;
    } else if (this->pImpl->input.sArea.size() < 3) {
      UniqueLock uLock(this->pImpl->output.mutex);
      this->pImpl->output.errorMessage = "Service area invalid: size < 3.";
      tilesValid = false;
    } else {
      // Set Origin
      origin = this->pImpl->input.mArea.front();
      // Update Scenario if necessary
      if (this->pImpl->input.scenarioChanged) {
        // Update measurement area.
        auto &mAreaEnu = this->pImpl->scenario.measurementArea();
        auto &mArea = this->pImpl->input.mArea;
        mAreaEnu.clear();
        for (auto geoVertex : mArea) {
          snake::BoostPoint p;
          snake::toENU(origin, geoVertex, p);
          mAreaEnu.outer().push_back(p);
        }
        // Update service area.
        auto &sAreaEnu = this->pImpl->scenario.serviceArea();
        auto &sArea = this->pImpl->input.sArea;
        sAreaEnu.clear();
        for (auto geoVertex : sArea) {
          snake::BoostPoint p;
          snake::toENU(origin, geoVertex, p);
          sAreaEnu.outer().push_back(p);
        }
        // Update corridor.
        auto &corridorEnu = this->pImpl->scenario.corridor();
        auto &corridor = this->pImpl->input.corridor;
        corridorEnu.clear();
        for (auto geoVertex : corridor) {
          snake::BoostPoint p;
          snake::toENU(origin, geoVertex, p);
          corridorEnu.outer().push_back(p);
        }
        // Update parametes.
        this->pImpl->scenario.setTileHeight(this->pImpl->input.tileHeight);
        this->pImpl->scenario.setTileWidth(this->pImpl->input.tileWidth);
        this->pImpl->scenario.setMinTileArea(this->pImpl->input.minTileArea);

        if (!this->pImpl->scenario.update()) {
          this->pImpl->output.errorMessage =
              this->pImpl->scenario.errorString.c_str();
          tilesValid = false;
        }
      }
    }
  }

  bool waypointsValid = tilesValid;
  bool progressValid = tilesValid;
  snake::flight_plan::Transects transects;
  snake::flight_plan::Transects transectsRouted;
  snake::flight_plan::Route route;
  std::vector<int> progress;
  bool needWaypointUpdate = this->pImpl->input.scenarioChanged ||
                            this->pImpl->input.routeParametersChanged ||
                            this->pImpl->input.progressChanged;
  if (tilesValid) {
    if (needWaypointUpdate) {
      // Sample data
      SharedLock inputLock(this->pImpl->input.mutex);
      // Verify progress.
      size_t nTiles = this->pImpl->scenario.tiles().size();
      if (size_t(this->pImpl->input.progress.progress().size()) != nTiles) {
        for (size_t i = 0; i < nTiles; ++i) {
          progress.push_back(0);
        }
      } else {
        for (size_t i = 0; i < nTiles; ++i) {
          progress.push_back(this->pImpl->input.progress.progress()[i]);
        }
      }
      // Copy remaining parameters and release lock.
      const auto &scenario = this->pImpl->scenario;
      const auto lineDistance = this->pImpl->input.lineDistance;
      const auto minTransectLength = this->pImpl->input.minTransectLength;
      inputLock.unlock();

      // Create transects.
      std::string errorString;
      snake::Angle alpha(scenario.mAreaBoundingBox().angle * si::radian);
      std::cout << "Transects angle: " << alpha << std::endl;
      transects.push_back(
          bg::model::linestring<snake::BoostPoint>{scenario.homePositon()});

      bool success = snake::flight_plan::transectsFromScenario(
          lineDistance, minTransectLength, alpha, scenario, progress, transects,
          errorString);
      if (!success) {
        UniqueLock lk(this->pImpl->output.mutex);
        this->pImpl->output.errorMessage = errorString.c_str();
        waypointsValid = false;
        progressValid = true;
      } else if (transects.size() > 1) {
        success =
            snake::flight_plan::route(scenario.joinedArea(), transects,
                                      transectsRouted, route, errorString);
        if (!success) {
          UniqueLock lk(this->pImpl->output.mutex);
          this->pImpl->output.errorMessage = errorString.c_str();
          waypointsValid = false;
          progressValid = true;
        } else {
          waypointsValid = true;
          progressValid = true;
        }
      } else {
        // No transects
        waypointsValid = false;
        progressValid = true;
      }
    } else {
      // No update necessary
      waypointsValid = true;
      progressValid = true;
    }
  }

  UniqueLock lk(this->pImpl->output.mutex);
  // Store tiles.
  this->pImpl->output.tilesUpdated = false;
  if (!tilesValid) {
    // Clear some output data.
    this->pImpl->output.tiles.polygons().clear();
    this->pImpl->output.tileCenterPoints.clear();
    this->pImpl->output.tilesENU.polygons().clear();
    this->pImpl->output.tileCenterPointsENU.clear();
    this->pImpl->output.ENUOrigin = QGeoCoordinate(0.0, 0.0, 0.0);
    this->pImpl->output.tilesUpdated = true;
  } else if (this->pImpl->input.scenarioChanged) {
    this->pImpl->input.scenarioChanged = false;
    // Clear some output data.
    this->pImpl->output.tiles.polygons().clear();
    this->pImpl->output.tileCenterPoints.clear();
    this->pImpl->output.tilesENU.polygons().clear();
    this->pImpl->output.tileCenterPointsENU.clear();
    this->pImpl->output.ENUOrigin = origin;
    // Convert and store scenario data.
    const auto &tiles = this->pImpl->scenario.tiles();
    const auto &centerPoints = this->pImpl->scenario.tileCenterPoints();
    for (unsigned int i = 0; i < tiles.size(); ++i) {
      const auto &tile = tiles[i];
      SnakeTile geoTile;
      SnakeTileLocal enuTile;
      for (size_t i = 0; i < tile.outer().size() - 1; ++i) {
        auto &p = tile.outer()[i];
        QPointF enuVertex(p.get<0>(), p.get<1>());
        QGeoCoordinate geoVertex;
        snake::fromENU(origin, p, geoVertex);
        enuTile.polygon().points().push_back(enuVertex);
        geoTile.push_back(geoVertex);
      }
      const auto &boostPoint = centerPoints[i];
      QPointF enuVertex(boostPoint.get<0>(), boostPoint.get<1>());
      QGeoCoordinate geoVertex;
      snake::fromENU(origin, boostPoint, geoVertex);
      geoTile.setCenter(geoVertex);
      this->pImpl->output.tiles.polygons().push_back(geoTile);
      this->pImpl->output.tileCenterPoints.push_back(
          QVariant::fromValue(geoVertex));
      this->pImpl->output.tilesENU.polygons().push_back(enuTile);
      this->pImpl->output.tileCenterPointsENU.push_back(enuVertex);
    }
    this->pImpl->output.tilesUpdated = true;
  }
  // Store progress.
  this->pImpl->output.progressUpdated = false;
  if (!progressValid) {
    this->pImpl->input.progress.progress().clear();
    this->pImpl->output.progressUpdated = true;
  } else if (this->pImpl->input.progressChanged) {
    if (progress.size() == this->pImpl->scenario.tiles().size()) {
      auto &qProgress = this->pImpl->input.progress;
      qProgress.progress().clear();
      for (const auto &p : progress) {
        qProgress.progress().push_back(p);
      }
    }
    this->pImpl->output.progressUpdated = true;
  }
  // Store waypoints.
  if (!waypointsValid) {
    // Clear waypoints.
    this->pImpl->output.arrivalPath.clear();
    this->pImpl->output.returnPath.clear();
    this->pImpl->output.arrivalPathENU.clear();
    this->pImpl->output.returnPathENU.clear();
    this->pImpl->output.waypoints.clear();
    this->pImpl->output.waypointsENU.clear();
    this->pImpl->output.waypointsUpdated = true;
  } else if (needWaypointUpdate) {
    // Clear waypoints.
    this->pImpl->output.arrivalPath.clear();
    this->pImpl->output.returnPath.clear();
    this->pImpl->output.arrivalPathENU.clear();
    this->pImpl->output.returnPathENU.clear();
    this->pImpl->output.waypoints.clear();
    this->pImpl->output.waypointsENU.clear();
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    // Store arrival path.
    const auto &firstWaypoint = transectsRouted.front().front();
    long startIdx = 0;
    for (long i = 0; i < long(route.size()); ++i) {
      const auto &boostVertex = route[i];
      if (boostVertex == firstWaypoint) {
        startIdx = i;
        break;
      }
      QPointF enuVertex{boostVertex.get<0>(), boostVertex.get<1>()};
      QGeoCoordinate geoVertex;
      snake::fromENU(origin, boostVertex, geoVertex);
      this->pImpl->output.arrivalPathENU.push_back(enuVertex);
      this->pImpl->output.arrivalPath.push_back(geoVertex);
    }
    // Store return path.
    long endIdx = 0;
    const auto &lastWaypoint = transectsRouted.back().back();
    for (long i = route.size() - 1; i >= 0; --i) {
      const auto &boostVertex = route[i];
      if (boostVertex == lastWaypoint) {
        endIdx = i;
        break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
516
      }
517 518 519 520 521
      QPointF enuVertex{boostVertex.get<0>(), boostVertex.get<1>()};
      QGeoCoordinate geoVertex;
      snake::fromENU(origin, boostVertex, geoVertex);
      this->pImpl->output.returnPathENU.push_back(enuVertex);
      this->pImpl->output.returnPath.push_back(geoVertex);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
522
    }
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    // Store waypoints.
    for (long i = startIdx; i <= endIdx; ++i) {
      const auto &boostVertex = route[i];
      QPointF enuVertex{boostVertex.get<0>(), boostVertex.get<1>()};
      QGeoCoordinate geoVertex;
      snake::fromENU(origin, boostVertex, geoVertex);
      this->pImpl->output.waypointsENU.push_back(enuVertex);
      this->pImpl->output.waypoints.push_back(geoVertex);
    }
    // Store waypoints.
    //    for (const auto &t : transects) {
    //      for (const auto &v : t) {
    //        QPointF enuVertex{v.get<0>(), v.get<1>()};
    //        QGeoCoordinate geoVertex;
    //        snake::fromENU(origin, v, geoVertex);
    //        this->pImpl->output.waypointsENU.push_back(enuVertex);
    //        this->pImpl->output.waypoints.push_back(geoVertex);
    //      }
    //    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
542 543 544
    this->pImpl->output.waypointsUpdated = true;
  }
}