CircularSurvey.cc 21.6 KB
Newer Older
1
#include "CircularSurvey.h"
2
#include "RoutingThread.h"
3
// QGC
4 5
#include "JsonHelper.h"
#include "QGCApplication.h"
6
#include "QGCLoggingCategory.h"
7
// Wima
8
#include "snake.h"
9
#define CLIPPER_SCALE 1000000
10 11 12
#include "clipper/clipper.hpp"

#include "Geometry/GenericCircle.h"
13 14
#include "Snake/SnakeTile.h"

15
// boost
16 17 18
#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>

Valentin Platzgummer's avatar
Valentin Platzgummer committed
19 20 21
#include "CircularGenerator.h"
#include "LinearGenerator.h"

22 23
QGC_LOGGING_CATEGORY(CircularSurveyLog, "CircularSurveyLog")

24 25 26 27 28
template <typename T>
constexpr typename std::underlying_type<T>::type integral(T value) {
  return static_cast<typename std::underlying_type<T>::type>(value);
}

29 30
const char *CircularSurvey::settingsGroup = "CircularSurvey";
const char *CircularSurvey::CircularSurveyName = "CircularSurvey";
31
const char *CircularSurvey::variantName = "Variant";
32 33 34 35

CircularSurvey::CircularSurvey(Vehicle *vehicle, bool flyView,
                               const QString &kmlOrShpFile, QObject *parent)
    : TransectStyleComplexItem(vehicle, flyView, settingsGroup, parent),
Valentin Platzgummer's avatar
Valentin Platzgummer committed
36
      _state(STATE::IDLE),
37 38
      _metaDataMap(FactMetaData::createMapFromJsonFile(
          QStringLiteral(":/json/CircularSurvey.SettingsGroup.json"), this)),
39
      _variant(settingsGroup, _metaDataMap[variantName]),
40
      _pAreaData(std::make_shared<WimaPlanData>()),
Valentin Platzgummer's avatar
Valentin Platzgummer committed
41 42
      _pWorker(std::make_unique<RoutingThread>()) {

43 44 45
  Q_UNUSED(kmlOrShpFile)
  _editorQml = "qrc:/qml/CircularSurveyItemEditor.qml";

46
  // Connect facts.
47 48
  connect(&this->_variant, &Fact::rawValueChanged, this,
          &CircularSurvey::_changeVariant);
49

50
  // Connect worker.
51
  connect(this->_pWorker.get(), &RoutingThread::result, this,
52
          &CircularSurvey::_setTransects);
53
  connect(this->_pWorker.get(), &RoutingThread::calculatingChanged, this,
54
          &CircularSurvey::calculatingChanged);
55
  this->_transectsDirty = true;
56 57 58 59 60 61

  // Altitude
  connect(&_cameraCalc, &CameraCalc::distanceToSurfaceRelativeChanged, this,
          &CircularSurvey::coordinateHasRelativeAltitudeChanged);
  connect(&_cameraCalc, &CameraCalc::distanceToSurfaceRelativeChanged, this,
          &CircularSurvey::exitCoordinateHasRelativeAltitudeChanged);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
62 63

  // Register Generators.
64
  auto lg = std::make_shared<routing::LinearGenerator>(this->_pAreaData);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
65
  registerGenerator(lg->name(), lg);
66 67
  auto cg = std::make_shared<routing::CircularGenerator>(this->_pAreaData);
  registerGenerator(cg->name(), cg);
68 69
}

70 71 72
CircularSurvey::~CircularSurvey() {}

void CircularSurvey::reverse() {
73
  this->_state = STATE::REVERSE;
74 75 76
  this->_rebuildTransects();
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
77
void CircularSurvey::setPlanData(const WimaPlanData &d) {
78
  *this->_pAreaData = d;
79 80
}

81
QStringList CircularSurvey::variantNames() const { return _variantNames; }
82

83 84
bool CircularSurvey::load(const QJsonObject &complexObject, int sequenceNumber,
                          QString &errorString) {
85 86
  // We need to pull version first to determine what validation/conversion
  // needs to be performed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  QList<JsonHelper::KeyValidateInfo> versionKeyInfoList = {
      {JsonHelper::jsonVersionKey, QJsonValue::Double, true},
  };
  if (!JsonHelper::validateKeys(complexObject, versionKeyInfoList,
                                errorString)) {
    return false;
  }

  int version = complexObject[JsonHelper::jsonVersionKey].toInt();
  if (version != 1) {
    errorString = tr("Survey items do not support version %1").arg(version);
    return false;
  }

  QList<JsonHelper::KeyValidateInfo> keyInfoList = {
      {VisualMissionItem::jsonTypeKey, QJsonValue::String, true},
      {ComplexMissionItem::jsonComplexItemTypeKey, QJsonValue::String, true},
104
      {variantName, QJsonValue::Double, false},
105 106 107 108 109 110 111 112 113 114 115
  };

  if (!JsonHelper::validateKeys(complexObject, keyInfoList, errorString)) {
    return false;
  }

  QString itemType = complexObject[VisualMissionItem::jsonTypeKey].toString();
  QString complexType =
      complexObject[ComplexMissionItem::jsonComplexItemTypeKey].toString();
  if (itemType != VisualMissionItem::jsonTypeComplexItemValue ||
      complexType != CircularSurveyName) {
116 117 118 119 120
    errorString = tr("%1 does not support loading this complex mission item "
                     "type: %2:%3")
                      .arg(qgcApp()->applicationName())
                      .arg(itemType)
                      .arg(complexType);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    return false;
  }

  _ignoreRecalc = true;

  setSequenceNumber(sequenceNumber);

  if (!_surveyAreaPolygon.loadFromJson(complexObject, true /* required */,
                                       errorString)) {
    _surveyAreaPolygon.clear();
    return false;
  }

  if (!_load(complexObject, errorString)) {
    _ignoreRecalc = false;
    return false;
  }

139
  _variant.setRawValue(complexObject[variantName].toInt());
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

  _ignoreRecalc = false;

  _recalcComplexDistance();
  if (_cameraShots == 0) {
    // Shot count was possibly not available from plan file
    _recalcCameraShots();
  }

  return true;
}

QString CircularSurvey::mapVisualQML() const {
  return QStringLiteral("CircularSurveyMapVisual.qml");
}

void CircularSurvey::save(QJsonArray &planItems) {
  QJsonObject saveObject;

  _save(saveObject);

  saveObject[JsonHelper::jsonVersionKey] = 1;
  saveObject[VisualMissionItem::jsonTypeKey] =
      VisualMissionItem::jsonTypeComplexItemValue;
  saveObject[ComplexMissionItem::jsonComplexItemTypeKey] = CircularSurveyName;

166
  saveObject[variantName] = double(_variant.rawValue().toUInt());
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

  // Polygon shape
  _surveyAreaPolygon.saveToJson(saveObject);

  planItems.append(saveObject);
}

bool CircularSurvey::specifiesCoordinate() const { return true; }

void CircularSurvey::appendMissionItems(QList<MissionItem *> &items,
                                        QObject *missionItemParent) {
  if (_transectsDirty)
    return;
  if (_loadedMissionItems.count()) {
    // We have mission items from the loaded plan, use those
    _appendLoadedMissionItems(items, missionItemParent);
  } else {
    // Build the mission items on the fly
    _buildAndAppendMissionItems(items, missionItemParent);
  }
}

void CircularSurvey::_appendLoadedMissionItems(QList<MissionItem *> &items,
                                               QObject *missionItemParent) {
  if (_transectsDirty)
    return;
  int seqNum = _sequenceNumber;

  for (const MissionItem *loadedMissionItem : _loadedMissionItems) {
    MissionItem *item = new MissionItem(*loadedMissionItem, missionItemParent);
    item->setSequenceNumber(seqNum++);
    items.append(item);
  }
}

void CircularSurvey::_buildAndAppendMissionItems(QList<MissionItem *> &items,
                                                 QObject *missionItemParent) {
204
  if (_transectsDirty || _transects.count() == 0)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    return;

  MissionItem *item;
  int seqNum = _sequenceNumber;

  MAV_FRAME mavFrame =
      followTerrain() || !_cameraCalc.distanceToSurfaceRelative()
          ? MAV_FRAME_GLOBAL
          : MAV_FRAME_GLOBAL_RELATIVE_ALT;

  for (const QList<TransectStyleComplexItem::CoordInfo_t> &transect :
       _transects) {
    // bool transectEntry = true;

    for (const CoordInfo_t &transectCoordInfo : transect) {
      item = new MissionItem(
          seqNum++, MAV_CMD_NAV_WAYPOINT, mavFrame,
222 223
          0,   // Hold time (delay for hover and capture to settle vehicle
               // before image is taken)
224 225 226 227 228 229 230 231 232 233 234 235 236 237
          0.0, // No acceptance radius specified
          0.0, // Pass through waypoint
          std::numeric_limits<double>::quiet_NaN(), // Yaw unchanged
          transectCoordInfo.coord.latitude(),
          transectCoordInfo.coord.longitude(),
          transectCoordInfo.coord.altitude(),
          true,  // autoContinue
          false, // isCurrentItem
          missionItemParent);
      items.append(item);
    }
  }
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
238 239 240 241
bool CircularSurvey::_switchToGenerator(
    const CircularSurvey::PtrGenerator &newG) {
  if (this->_pGenerator != newG) {
    if (this->_pGenerator != nullptr) {
242 243 244
      disconnect(this->_pGenerator.get(),
                 &routing::GeneratorBase::generatorChanged, this,
                 &CircularSurvey::_rebuildTransects);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
245 246 247
    }

    this->_pGenerator = newG;
248 249 250
    connect(this->_pGenerator.get(), &routing::GeneratorBase::generatorChanged,
            this, &CircularSurvey::_rebuildTransects);
    emit generatorChanged();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
251 252 253 254 255 256 257 258 259 260

    this->_state = STATE::IDLE;
    _rebuildTransects();

    return true;
  } else {
    return false;
  }
}

261 262 263
void CircularSurvey::_changeVariant() {
  this->_state = STATE::VARIANT_CHANGE;
  this->_rebuildTransects();
264 265
}

266
void CircularSurvey::_updateWorker() {
267 268
  // Mark transects as dirty.
  this->_transectsDirty = true;
269 270
  // Reset data.
  this->_transects.clear();
271
  this->_variantVector.clear();
272 273 274
  this->_variantNames.clear();
  emit variantNamesChanged();

275
  if (this->_pAreaData->isValid()) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
276 277

    // Prepare data.
278 279 280
    auto origin = this->_pAreaData->origin();
    origin.setAltitude(0);
    if (!origin.isValid()) {
281
      qCDebug(CircularSurveyLog)
282 283 284 285
          << "_updateWorker(): origin invalid." << origin;
      return;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
286
    // Convert safe area.
287
    auto geoSafeArea = this->_pAreaData->joinedArea().coordinateList();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
288
    if (!(geoSafeArea.size() >= 3)) {
289
      qCDebug(CircularSurveyLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
290 291 292 293 294 295 296
          << "_updateWorker(): safe area invalid." << geoSafeArea;
      return;
    }
    for (auto &v : geoSafeArea) {
      if (v.isValid()) {
        v.setAltitude(0);
      } else {
297
        qCDebug(CircularSurveyLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
298 299 300
            << "_updateWorker(): safe area contains invalid coordinate."
            << geoSafeArea;
        return;
301 302 303
      }
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
304 305 306 307 308 309 310
    // Routing par.
    RoutingParameter par;
    par.numSolutions = 5;
    auto &safeAreaENU = par.safeArea;
    snake::areaToEnu(origin, geoSafeArea, safeAreaENU);

    // Create generator.
311 312 313 314 315 316
    if (this->_pGenerator) {
      routing::GeneratorBase::Generator g; // Transect generator.
      if (this->_pGenerator->get(g)) {
        // Start/Restart routing worker.
        this->_pWorker->route(par, g);
      } else {
317
        qCDebug(CircularSurveyLog)
318 319
            << "_updateWorker(): generator creation failed.";
      }
320
    } else {
321
      qCDebug(CircularSurveyLog)
322 323 324
          << "_updateWorker(): pGenerator == nullptr, number of registered "
             "generators: "
          << this->_generatorList.size();
325 326
    }
  } else {
327
    qCDebug(CircularSurveyLog) << "_updateWorker(): plan data invalid.";
328
  }
329 330
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
331
void CircularSurvey::_changeVariantWorker() {
332
  auto variant = this->_variant.rawValue().toUInt();
333 334 335 336

  // Find old variant and run. Old run corresponts with empty list.
  std::size_t old_variant = std::numeric_limits<std::size_t>::max();
  for (std::size_t i = 0; i < std::size_t(this->_variantVector.size()); ++i) {
337 338 339 340
    const auto &variantCoordinates = this->_variantVector.at(i);
    if (variantCoordinates.isEmpty()) {
      old_variant = i;
      break;
341 342
    }
  }
343

344
  // Swap route.
345
  if (variant != old_variant) {
346
    // Swap in new variant.
347
    if (variant < std::size_t(this->_variantVector.size())) {
348 349 350
      if (old_variant != std::numeric_limits<std::size_t>::max()) {
        // this->_transects containes a route, swap it back to
        // this->_solutionVector
351 352
        auto &oldVariantCoordinates = this->_variantVector[old_variant];
        oldVariantCoordinates.swap(this->_transects);
353
      }
354 355
      auto &newVariantCoordinates = this->_variantVector[variant];
      this->_transects.swap(newVariantCoordinates);
356 357

    } else { // error
358
      qCDebug(CircularSurveyLog)
359
          << "Variant out of bounds (variant =" << variant << ").";
360
      qCDebug(CircularSurveyLog) << "Resetting variant to zero.";
361 362 363

      disconnect(&this->_variant, &Fact::rawValueChanged, this,
                 &CircularSurvey::_changeVariant);
364
      this->_variant.setCookedValue(QVariant(0));
365 366
      connect(&this->_variant, &Fact::rawValueChanged, this,
              &CircularSurvey::_changeVariant);
367 368

      if (this->_variantVector.size() > 0) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
369
        this->_changeVariantWorker();
370 371
      }
    }
372 373
  }
}
374

375 376 377
void CircularSurvey::_reverseWorker() {
  if (this->_transects.size() > 0) {
    auto &t = this->_transects.front();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
378
    std::reverse(t.begin(), t.end());
379
  }
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
void CircularSurvey::applyNewAltitude(double newAltitude) {
  _cameraCalc.valueSetIsDistance()->setRawValue(true);
  _cameraCalc.distanceToSurface()->setRawValue(newAltitude);
  _cameraCalc.setDistanceToSurfaceRelative(true);
}

double CircularSurvey::timeBetweenShots() { return 1; }

QString CircularSurvey::commandDescription() const {
  return tr("Circular Survey");
}

QString CircularSurvey::commandName() const { return tr("Circular Survey"); }

QString CircularSurvey::abbreviation() const { return tr("C.S."); }

bool CircularSurvey::readyForSave() const {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
399 400
  return TransectStyleComplexItem::readyForSave() && !_transectsDirty &&
         this->_state == STATE::IDLE;
401 402 403 404
}

double CircularSurvey::additionalTimeDelay() const { return 0; }

405 406
bool CircularSurvey::registerGenerator(
    const QString &name, std::shared_ptr<routing::GeneratorBase> g) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
407
  if (name.isEmpty()) {
408
    qCDebug(CircularSurveyLog) << "registerGenerator(): empty name string.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
409 410 411
    return false;
  }

412
  if (!g) {
413
    qCDebug(CircularSurveyLog) << "registerGenerator(): empty generator.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
414 415 416 417
    return false;
  }

  if (this->_generatorNameList.contains(name)) {
418 419
    qCDebug(CircularSurveyLog) << "registerGenerator(): generator "
                                  "already registered.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
420 421 422 423 424
    return false;
  } else {
    this->_generatorNameList.push_back(name);
    this->_generatorList.push_back(g);
    if (this->_generatorList.size() == 1) {
425
      _switchToGenerator(g);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
426 427 428 429 430 431 432 433 434 435 436 437
    }

    emit generatorNameListChanged();
    return true;
  }
}

bool CircularSurvey::unregisterGenerator(const QString &name) {
  auto index = this->_generatorNameList.indexOf(name);
  if (index >= 0) {
    // Is this the current generator?
    const auto &g = this->_generatorList.at(index);
438
    if (g == this->_pGenerator) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
439 440 441 442
      if (index > 0) {
        _switchToGenerator(this->_generatorList.at(index - 1));
      } else {
        _switchToGenerator(nullptr);
443
        qCDebug(CircularSurveyLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
444 445 446 447 448 449 450 451 452 453
            << "unregisterGenerator(): last generator unregistered.";
      }
    }

    this->_generatorNameList.removeAt(index);
    this->_generatorList.removeAt(index);

    emit generatorNameListChanged();
    return true;
  } else {
454
    qCDebug(CircularSurveyLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
455 456 457 458 459 460 461 462 463
        << "unregisterGenerator(): generator " << name << " not registered.";
    return false;
  }
}

bool CircularSurvey::unregisterGenerator(int index) {
  if (index > 0 && index < this->_generatorNameList.size()) {
    return unregisterGenerator(this->_generatorNameList.at(index));
  } else {
464 465 466 467
    qCDebug(CircularSurveyLog) << "unregisterGenerator(): index (" << index
                               << ") out"
                                  "of bounds ( "
                               << this->_generatorList.size() << " ).";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
468 469 470 471 472 473 474 475 476 477
    return false;
  }
}

bool CircularSurvey::switchToGenerator(const QString &name) {
  auto index = this->_generatorNameList.indexOf(name);
  if (index >= 0) {
    _switchToGenerator(this->_generatorList.at(index));
    return true;
  } else {
478
    qCDebug(CircularSurveyLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
479 480 481 482 483 484 485 486 487 488
        << "switchToGenerator(): generator " << name << " not registered.";
    return false;
  }
}

bool CircularSurvey::switchToGenerator(int index) {
  if (index >= 0) {
    _switchToGenerator(this->_generatorList.at(index));
    return true;
  } else {
489 490 491 492
    qCDebug(CircularSurveyLog) << "unregisterGenerator(): index (" << index
                               << ") out"
                                  "of bounds ( "
                               << this->_generatorNameList.size() << " ).";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
493 494 495 496
    return false;
  }
}

497
QStringList CircularSurvey::generatorNameList() {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
498 499 500
  return this->_generatorNameList;
}

501 502 503 504 505 506 507 508
routing::GeneratorBase *CircularSurvey::generator() {
  return _pGenerator.get();
}

int CircularSurvey::generatorIndex() {
  return this->_generatorList.indexOf(this->_pGenerator);
}

509 510 511 512
void CircularSurvey::_rebuildTransectsPhase1(void) {
  auto start = std::chrono::high_resolution_clock::now();

  switch (this->_state) {
513 514
  case STATE::SKIPP:
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: skipp.";
515 516
    break;
  case STATE::VARIANT_CHANGE:
517
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: variant change.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
518
    this->_changeVariantWorker();
519 520
    break;
  case STATE::RUN_CHANGE:
521
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: run change.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
522
    this->_changeVariantWorker();
523 524
    break;
  case STATE::REVERSE:
525
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: reverse.";
526 527
    this->_reverseWorker();
    break;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
528
  case STATE::IDLE:
529
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: update.";
530 531
    this->_updateWorker();
    break;
532
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
533
  this->_state = STATE::IDLE;
534

535
  qCDebug(CircularSurveyLog)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
536
      << "rebuildTransectsPhase1(): "
537 538 539 540
      << std::chrono::duration_cast<std::chrono::milliseconds>(
             std::chrono::high_resolution_clock::now() - start)
             .count()
      << " ms";
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
}

void CircularSurvey::_recalcComplexDistance() {
  _complexDistance = 0;
  if (_transectsDirty)
    return;
  for (int i = 0; i < _visualTransectPoints.count() - 1; i++) {
    _complexDistance +=
        _visualTransectPoints[i].value<QGeoCoordinate>().distanceTo(
            _visualTransectPoints[i + 1].value<QGeoCoordinate>());
  }
  emit complexDistanceChanged();
}

// no cameraShots in Circular Survey, add if desired
void CircularSurvey::_recalcCameraShots() { _cameraShots = 0; }

558
void CircularSurvey::_setTransects(CircularSurvey::PtrRoutingData pRoute) {
559
  // Store solutions.
560 561
  auto ori = this->_pAreaData->origin();
  ori.setAltitude(0);
562 563 564
  const auto &transectsENU = pRoute->transects;
  QVector<Variant> variantVector;
  const auto nSolutions = pRoute->solutionVector.size();
565

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  for (std::size_t j = 0; j < nSolutions; ++j) {
    Variant var{QList<CoordInfo_t>()};
    const auto &solution = pRoute->solutionVector.at(j);
    if (solution.size() > 0) {
      const auto &route = solution.at(0);
      const auto &path = route.path;
      const auto &info = route.info;
      if (info.size() > 1) {
        // Find index of first waypoint.
        std::size_t idxFirst = 0;
        const auto &infoFirst = info.at(1);
        const auto &firstTransect = transectsENU[infoFirst.index];
        if (firstTransect.size() > 0) {
          const auto &firstWaypoint =
              infoFirst.reversed ? firstTransect.back() : firstTransect.front();
          double th = 0.01;
          for (std::size_t i = 0; i < path.size(); ++i) {
            auto dist = bg::distance(path[i], firstWaypoint);
            if (dist < th) {
              idxFirst = i;
              break;
            }
588 589
          }

590 591 592 593 594 595 596 597 598 599 600 601 602
          // Find index of last waypoint.
          std::size_t idxLast = path.size() - 1;
          const auto &infoLast = info.at(info.size() - 2);
          const auto &lastTransect = transectsENU[infoLast.index];
          if (lastTransect.size() > 0) {
            const auto &lastWaypoint =
                infoLast.reversed ? lastTransect.front() : lastTransect.back();
            for (long i = path.size() - 1; i >= 0; --i) {
              auto dist = bg::distance(path[i], lastWaypoint);
              if (dist < th) {
                idxLast = i;
                break;
              }
603
            }
604 605 606 607 608 609 610 611

            // Convert to geo coordinates.
            auto &list = var.front();
            for (std::size_t i = idxFirst; i <= idxLast; ++i) {
              auto &vertex = path[i];
              QGeoCoordinate c;
              snake::fromENU(ori, vertex, c);
              list.append(CoordInfo_t{c, CoordTypeInterior});
612
            }
613

614
          } else {
615
            qCDebug(CircularSurveyLog)
616 617
                << "_setTransects(): lastTransect.size() == 0";
          }
618
        } else {
619
          qCDebug(CircularSurveyLog)
620
              << "_setTransects(): firstTransect.size() == 0";
621
        }
622
      } else {
623
        qCDebug(CircularSurveyLog)
624
            << "_setTransects(): transectsInfo.size() <= 1";
625
      }
626
    } else {
627
      qCDebug(CircularSurveyLog) << "_setTransects(): solution.size() == 0";
628
    }
629

630 631
    if (var.size() > 0 && var.front().size() > 0) {
      variantVector.push_back(std::move(var));
632 633
    }
  }
634

635 636 637 638
  // Assign routes if no error occured.
  if (variantVector.size() > 0) {
    // Swap first route to _transects.
    this->_variantVector.swap(variantVector);
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

    // If the transects are getting rebuilt then any previously loaded
    // mission items are now invalid.
    if (_loadedMissionItemsParent) {
      _loadedMissionItems.clear();
      _loadedMissionItemsParent->deleteLater();
      _loadedMissionItemsParent = nullptr;
    }

    // Add route variant names.
    this->_variantNames.clear();
    for (std::size_t i = 1; i <= std::size_t(this->_variantVector.size());
         ++i) {
      this->_variantNames.append(QString::number(i));
    }
    emit variantNamesChanged();

    disconnect(&this->_variant, &Fact::rawValueChanged, this,
               &CircularSurvey::_changeVariant);
    this->_variant.setCookedValue(QVariant(0));
    connect(&this->_variant, &Fact::rawValueChanged, this,
            &CircularSurvey::_changeVariant);
    this->_changeVariantWorker();

    // Mark transect ready.
    this->_transectsDirty = false;

    this->_state = STATE::SKIPP;
667 668
    this->_rebuildTransects();
  } else {
669 670
    qCDebug(CircularSurveyLog)
        << "_setTransects(): failed, variantVector empty.";
671 672 673
    this->_state = STATE::IDLE;
  }
}
674

675
Fact *CircularSurvey::variant() { return &_variant; }
676

677 678
bool CircularSurvey::calculating() const {
  return this->_pWorker->calculating();
679
}
680

681 682 683 684
/*!
    \class CircularSurveyComplexItem
    \inmodule Wima

685 686
    \brief The \c CircularSurveyComplexItem class provides a survey mission
   item with circular transects around a point of interest.
687

688 689 690 691
    CircularSurveyComplexItem class provides a survey mission item with
   circular transects around a point of interest. Within the \c Wima module
   it's used to scan a defined area with constant angle (circular transects)
   to the base station (point of interest).
692 693 694

    \sa WimaArea
*/