WimaMeasurementArea.cc 12.7 KB
Newer Older
1
#include "WimaMeasurementArea.h"
2
#include "QtConcurrentRun"
Valentin Platzgummer's avatar
Valentin Platzgummer committed
3 4 5 6
#include "SnakeTile.h"
#include "snake.h"

#include <boost/units/systems/si.hpp>
7

8 9 10 11
#ifndef SNAKE_MAX_TILES
#define SNAKE_MAX_TILES 1000
#endif

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
TileData::TileData() : tiles(this) {}

TileData::~TileData() { tiles.clearAndDeleteContents(); }

TileData &TileData::operator=(const TileData &other) {
  this->tiles.clearAndDeleteContents();
  for (std::size_t i = 0; i < std::size_t(other.tiles.count()); ++i) {
    const auto *obj = other.tiles.get(i);
    const auto *tile = qobject_cast<const SnakeTile *>(obj);
    if (tile != nullptr) {
      this->tiles.append(new SnakeTile(*tile, this));
    } else {
      qWarning("TileData::operator=: nullptr");
    }
  }
  this->tileCenterPoints = other.tileCenterPoints;
  return *this;
}

31 32 33 34 35 36 37 38 39
bool TileData::operator==(const TileData &other) const {
  return this->tiles == other.tiles &&
         this->tileCenterPoints == other.tileCenterPoints;
}

bool TileData::operator!=(const TileData &other) const {
  return !this->operator==(other);
}

40 41 42 43 44 45 46 47 48 49 50 51 52
void TileData::clear() {
  this->tiles.clearAndDeleteContents();
  this->tileCenterPoints.clear();
}

size_t TileData::size() const {
  if (tiles.count() == tileCenterPoints.size()) {
    return tiles.count();
  } else {
    return 0;
  }
}

53
const char *WimaMeasurementArea::settingsGroup = "MeasurementArea";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
54 55 56 57 58 59
const char *WimaMeasurementArea::tileHeightName = "TileHeight";
const char *WimaMeasurementArea::tileWidthName = "TileWidth";
const char *WimaMeasurementArea::minTileAreaName = "MinTileArea";
const char *WimaMeasurementArea::transectDistanceName = "TransectDistance";
const char *WimaMeasurementArea::minTransectLengthName = "MinTransectLength";
const char *WimaMeasurementArea::showTilesName = "ShowTiles";
60
const char *WimaMeasurementArea::WimaMeasurementAreaName = "Measurement Area";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
61

62 63
void tileDeleter(QmlObjectListModel *tiles) { tiles->clearAndDeleteContents(); }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
64
WimaMeasurementArea::WimaMeasurementArea(QObject *parent)
65 66 67 68
    : WimaArea(parent),
      _metaDataMap(FactMetaData::createMapFromJsonFile(
          QStringLiteral(":/json/WimaMeasurementArea.SettingsGroup.json"),
          this /* QObject parent */)),
Valentin Platzgummer's avatar
Valentin Platzgummer committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
      _tileHeight(SettingsFact(settingsGroup, _metaDataMap[tileHeightName],
                               this /* QObject parent */)),
      _tileWidth(SettingsFact(settingsGroup, _metaDataMap[tileWidthName],
                              this /* QObject parent */)),
      _minTileArea(SettingsFact(settingsGroup, _metaDataMap[minTileAreaName],
                                this /* QObject parent */)),
      _transectDistance(SettingsFact(settingsGroup,
                                     _metaDataMap[transectDistanceName],
                                     this /* QObject parent */)),
      _minTransectLength(SettingsFact(settingsGroup,
                                      _metaDataMap[minTransectLengthName],
                                      this /* QObject parent */)),
      _showTiles(SettingsFact(settingsGroup, _metaDataMap[showTilesName],
                              this /* QObject parent */)),
83
      _calculating(false) {
84
  init();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
85 86
}

87 88 89 90 91 92
WimaMeasurementArea::WimaMeasurementArea(const WimaMeasurementArea &other,
                                         QObject *parent)
    : WimaArea(other, parent),
      _metaDataMap(FactMetaData::createMapFromJsonFile(
          QStringLiteral(":/json/WimaMeasurementArea.SettingsGroup.json"),
          this /* QObject parent */)),
Valentin Platzgummer's avatar
Valentin Platzgummer committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106
      _tileHeight(SettingsFact(settingsGroup, _metaDataMap[tileHeightName],
                               this /* QObject parent */)),
      _tileWidth(SettingsFact(settingsGroup, _metaDataMap[tileWidthName],
                              this /* QObject parent */)),
      _minTileArea(SettingsFact(settingsGroup, _metaDataMap[minTileAreaName],
                                this /* QObject parent */)),
      _transectDistance(SettingsFact(settingsGroup,
                                     _metaDataMap[transectDistanceName],
                                     this /* QObject parent */)),
      _minTransectLength(SettingsFact(settingsGroup,
                                      _metaDataMap[minTransectLengthName],
                                      this /* QObject parent */)),
      _showTiles(SettingsFact(settingsGroup, _metaDataMap[showTilesName],
                              this /* QObject parent */)),
107
      _calculating(false) {
108
  init();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
109 110
}

111 112 113 114 115
/*!
 * \overload operator=()
 *
 * Calls the inherited operator WimaArea::operator=().
 */
116 117 118 119 120 121 122
WimaMeasurementArea &WimaMeasurementArea::
operator=(const WimaMeasurementArea &other) {
  WimaArea::operator=(other);

  return *this;
}

123
WimaMeasurementArea::~WimaMeasurementArea() {}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
124

125 126 127
QString WimaMeasurementArea::mapVisualQML() const {
  return "WimaMeasurementAreaMapVisual.qml";
}
128

129 130
QString WimaMeasurementArea::editorQML() const {
  return "WimaMeasurementAreaEditor.qml";
131 132
}

133 134
Fact *WimaMeasurementArea::tileHeight() { return &_tileHeight; }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
135 136 137 138 139 140 141 142 143 144
Fact *WimaMeasurementArea::tileWidth() { return &_tileWidth; }

Fact *WimaMeasurementArea::minTileArea() { return &_minTileArea; }

Fact *WimaMeasurementArea::transectDistance() { return &_transectDistance; }

Fact *WimaMeasurementArea::minTransectLength() { return &_minTransectLength; }

Fact *WimaMeasurementArea::showTiles() { return &_showTiles; }

145 146 147
QmlObjectListModel *WimaMeasurementArea::tiles() {
  return &this->_tileData.tiles;
}
148

149
const QmlObjectListModel *WimaMeasurementArea::tiles() const {
150 151 152 153 154 155 156 157 158
  return &this->_tileData.tiles;
}

const QVariantList &WimaMeasurementArea::tileCenterPoints() const {
  return this->_tileData.tileCenterPoints;
}

const TileData &WimaMeasurementArea::tileData() const {
  return this->_tileData;
159 160 161
}

int WimaMeasurementArea::maxTiles() const { return SNAKE_MAX_TILES; }
162

163
bool WimaMeasurementArea::ready() const { return !_calculating; }
164

165 166
void WimaMeasurementArea::saveToJson(QJsonObject &json) {
  this->WimaArea::saveToJson(json);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
167 168 169 170 171 172
  json[tileHeightName] = _tileHeight.rawValue().toDouble();
  json[tileWidthName] = _tileWidth.rawValue().toDouble();
  json[minTileAreaName] = _minTileArea.rawValue().toDouble();
  json[transectDistanceName] = _transectDistance.rawValue().toDouble();
  json[minTransectLengthName] = _minTransectLength.rawValue().toDouble();
  json[showTilesName] = _showTiles.rawValue().toBool();
173
  json[areaTypeName] = WimaMeasurementAreaName;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
174 175
}

176 177 178 179 180
bool WimaMeasurementArea::loadFromJson(const QJsonObject &json,
                                       QString &errorString) {
  if (this->WimaArea::loadFromJson(json, errorString)) {
    bool retVal = true;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
181 182
    if (json.contains(tileHeightName) && json[tileHeightName].isDouble()) {
      _tileHeight.setRawValue(json[tileHeightName].toDouble());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
183
    } else {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
184
      errorString.append(tr("Could not load tile height!\n"));
185
      retVal = false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
186 187
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
188
    if (json.contains(tileWidthName) && json[tileWidthName].isDouble()) {
189
      _tileWidth.setRawValue(json[tileWidthName].toDouble());
190
    } else {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
191
      errorString.append(tr("Could not load tile width!\n"));
192 193
      retVal = false;
    }
194

Valentin Platzgummer's avatar
Valentin Platzgummer committed
195
    if (json.contains(minTileAreaName) && json[minTileAreaName].isDouble()) {
196
      _minTileArea.setRawValue(json[minTileAreaName].toDouble());
197
    } else {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
198
      errorString.append(tr("Could not load minimal tile area!\n"));
199
      retVal = false;
200
    }
201

Valentin Platzgummer's avatar
Valentin Platzgummer committed
202 203
    if (json.contains(transectDistanceName) &&
        json[transectDistanceName].isDouble()) {
204
      _transectDistance.setRawValue(json[transectDistanceName].toDouble());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
205 206 207 208 209 210 211
    } else {
      errorString.append(tr("Could not load transect distance!\n"));
      retVal = false;
    }

    if (json.contains(minTransectLengthName) &&
        json[minTransectLengthName].isDouble()) {
212
      _minTransectLength.setRawValue(json[minTransectLengthName].toDouble());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
213 214 215 216 217 218
    } else {
      errorString.append(tr("Could not load minimal transect length!\n"));
      retVal = false;
    }

    if (json.contains(showTilesName) && json[showTilesName].isBool()) {
219
      _showTiles.setRawValue(json[showTilesName].toBool());
Valentin Platzgummer's avatar
Valentin Platzgummer committed
220 221 222 223
    } else {
      errorString.append(tr("Could not load show tiles !\n"));
      retVal = false;
    }
224 225 226 227
    return retVal;
  } else {
    return false;
  }
228
}
229 230
//!
//! \brief WimaMeasurementArea::doUpdate
231 232
//! \pre WimaMeasurementArea::deferUpdate must be called first, don't call
//! this function directly!
Valentin Platzgummer's avatar
Valentin Platzgummer committed
233 234 235 236 237 238
void WimaMeasurementArea::doUpdate() {
  using namespace snake;
  using namespace boost::units;
#ifdef SNAKE_SHOW_TIME
  auto start = std::chrono::high_resolution_clock::now();
#endif
239 240 241 242 243 244 245 246 247
  const auto height = this->_tileHeight.rawValue().toDouble() * si::meter;
  const auto width = this->_tileWidth.rawValue().toDouble() * si::meter;
  const auto tileArea = width * height;
  const auto totalArea = this->area() * si::meter * si::meter;
  const auto estNumTiles = totalArea / tileArea;
  if (!this->_calculating &&
      long(std::ceil(estNumTiles.value())) <= SNAKE_MAX_TILES &&
      this->count() >= 3 && this->isSimplePolygon()) {
    this->_calculating = true;
248 249 250
    auto polygon = this->coordinateList();
    for (auto &v : polygon) {
      v.setAltitude(0);
251 252
    }
    const auto minArea =
Valentin Platzgummer's avatar
Valentin Platzgummer committed
253
        this->_minTileArea.rawValue().toDouble() * si::meter * si::meter;
254 255 256 257 258
    auto *th = this->thread();
    auto future = QtConcurrent::run([polygon, th, height, width, minArea] {
#ifdef SNAKE_SHOW_TIME
      auto start = std::chrono::high_resolution_clock::now();
#endif
259 260
      DataPtr pData(new TileData());
      // Convert to ENU system.
261 262 263 264 265 266
      QGeoCoordinate origin = polygon.first();
      BoostPolygon polygonENU;
      areaToEnu(origin, polygon, polygonENU);
      std::vector<BoostPolygon> tilesENU;
      BoundingBox bbox;
      std::string errorString;
267
      // Generate tiles.
268 269
      if (snake::tiles(polygonENU, height, width, minArea, tilesENU, bbox,
                       errorString)) {
270
        // Convert to geo system.
271
        for (const auto &t : tilesENU) {
272
          auto geoTile = new SnakeTile(pData.get());
273 274 275 276 277
          for (const auto &v : t.outer()) {
            QGeoCoordinate geoVertex;
            fromENU(origin, v, geoVertex);
            geoTile->push_back(geoVertex);
          }
278 279 280 281 282 283 284
          pData->tiles.append(geoTile);
          // Calculate center.
          snake::BoostPoint center;
          snake::polygonCenter(t, center);
          QGeoCoordinate geoCenter;
          fromENU(origin, center, geoCenter);
          pData->tileCenterPoints.append(QVariant::fromValue(geoCenter));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
285 286
        }
      }
287
      pData->moveToThread(th);
288
#ifdef SNAKE_SHOW_TIME
289 290 291 292 293 294
      qDebug() << "WimaMeasurementArea::doUpdate concurrent update execution "
                  "time: "
               << std::chrono::duration_cast<std::chrono::milliseconds>(
                      std::chrono::high_resolution_clock::now() - start)
                      .count()
               << " ms";
295
#endif
296
      return pData;
297 298 299
    }); // QtConcurrent::run()

    this->_watcher.setFuture(future);
300
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
301 302 303 304 305 306 307
#ifdef SNAKE_SHOW_TIME
  qDebug() << "WimaMeasurementArea::doUpdate execution time: "
           << std::chrono::duration_cast<std::chrono::milliseconds>(
                  std::chrono::high_resolution_clock::now() - start)
                  .count()
           << " ms";
#endif
Valentin Platzgummer's avatar
Valentin Platzgummer committed
308 309
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
310 311 312
void WimaMeasurementArea::deferUpdate() {
  if (this->_timer.isActive()) {
    this->_timer.stop();
313
  }
314 315
  if (this->_tileData.size() > 0) {
    this->_tileData.clear();
316 317
    emit this->tilesChanged();
  }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
318
  this->_timer.start(100);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
319 320
}

321 322 323 324
void WimaMeasurementArea::storeTiles() {
#ifdef SNAKE_SHOW_TIME
  auto start = std::chrono::high_resolution_clock::now();
#endif
325
  this->_tileData = *this->_watcher.result();
326
  this->_calculating = false;
327 328
  // This is expensive. Drawing tiles is expensive too.
  emit this->tilesChanged();
329 330 331 332 333 334 335 336 337
#ifdef SNAKE_SHOW_TIME
  qDebug() << "WimaMeasurementArea::storeTiles() execution time: "
           << std::chrono::duration_cast<std::chrono::milliseconds>(
                  std::chrono::high_resolution_clock::now() - start)
                  .count()
           << " ms";
#endif
}

338 339
void WimaMeasurementArea::init() {
  this->setObjectName(WimaMeasurementAreaName);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
340 341 342 343 344 345 346 347 348 349 350
  connect(&this->_tileHeight, &Fact::rawValueChanged, this,
          &WimaMeasurementArea::deferUpdate);
  connect(&this->_tileWidth, &Fact::rawValueChanged, this,
          &WimaMeasurementArea::deferUpdate);
  connect(&this->_minTileArea, &Fact::rawValueChanged, this,
          &WimaMeasurementArea::deferUpdate);
  connect(this, &WimaArea::pathChanged, this,
          &WimaMeasurementArea::deferUpdate);
  this->_timer.setSingleShot(true);
  connect(&this->_timer, &QTimer::timeout, this,
          &WimaMeasurementArea::doUpdate);
351 352 353
  connect(&this->_watcher,
          &QFutureWatcher<std::unique_ptr<QmlObjectListModel>>::finished, this,
          &WimaMeasurementArea::storeTiles);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
354 355
}

356 357
/*!
 * \class WimaMeasurementArea
358 359
 * \brief Class defining the area inside which the actual drone measurements
 * are performed.
360 361 362
 *
 * \sa WimaArea, WimaController, WimaPlaner
 */