CSWorker.cpp 13.6 KB
Newer Older
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 42 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
#include "CSWorker.h"
// Wima
#define CLIPPER_SCALE 10000
#include "clipper/clipper.hpp"
template <int k> ClipperLib::cInt get(ClipperLib::IntPoint &p);
#include "Geometry/GenericCircle.h"
// std
#include <chrono>
// Qt
#include <QDebug>

template <> ClipperLib::cInt get<0>(ClipperLib::IntPoint &p) { return p.X; }
template <> ClipperLib::cInt get<1>(ClipperLib::IntPoint &p) { return p.Y; }

CSWorker::CSWorker(QObject *parent)
    : QThread(parent), _deltaR(2 * bu::si::meter),
      _deltaAlpha(3 * bu::degree::degree), _minLength(10 * bu::si::meter),
      _calculating(false), _stop(false), _restart(false) {}

CSWorker::~CSWorker() {
  this->_stop = true;
  Lock lk(this->_mutex);
  this->_restart = true;
  this->_cv.notify_one();
  lk.unlock();
  this->wait();
}

bool CSWorker::calculating() { return this->_calculating; }

void CSWorker::update(const QList<QGeoCoordinate> &polygon,
                      const QGeoCoordinate &origin, snake::Length deltaR,
                      snake::Length minLength, snake::Angle deltaAlpha) {
  // Sample input.
  Lock lk(this->_mutex);
  this->_polygon = polygon;
  this->_origin = origin;
  this->_deltaR = deltaR;
  this->_deltaAlpha = deltaAlpha;
  this->_minLength = minLength;
  lk.unlock();

  if (!this->isRunning()) {
    this->start();
  } else {
    Lock lk(this->_mutex);
    this->_restart = true;
    this->_cv.notify_one();
  }
}

void CSWorker::run() {
  qWarning() << "CSWorker::run(): thread start.";
  while (!this->_stop) {
    // Copy input.
    Lock lk(this->_mutex);
    const auto polygon = this->_polygon;
    const auto origin = this->_origin;
    const auto deltaR = this->_deltaR;
    const auto deltaAlpha = this->_deltaAlpha;
    const auto minLength = this->_minLength;
    lk.unlock();
    // Check preconitions
    if (polygon.size() >= 3) {
#ifdef DEBUG_CIRCULAR_SURVEY
      qWarning() << "CSWorker::run(): calculation "
                    "started.";
#endif
#ifdef SHOW_CIRCULAR_SURVEY_TIME
      const auto start = std::chrono::high_resolution_clock::now();
#endif
      using namespace boost::units;
      this->_calculating = true;
      emit calculatingChanged();
      // Convert geo polygon to ENU polygon.
      snake::BoostPolygon polygonENU;
      snake::BoostPoint originENU{0, 0};
      snake::areaToEnu(origin, polygon, polygonENU);
      std::string error;
      // Check validity.
      if (!bg::is_valid(polygonENU, error)) {
#ifdef DEBUG_CIRCULAR_SURVEY
        qWarning() << "CSWorker::run(): "
                      "invalid polygon.";
        qWarning() << error.c_str();
#endif
      } else {
        // Calculate polygon distances and angles.
        std::vector<snake::Length> distances;
        distances.reserve(polygonENU.outer().size());
        std::vector<snake::Angle> angles;
        angles.reserve(polygonENU.outer().size());
#ifdef DEBUG_CIRCULAR_SURVEY
        qWarning() << "CSWorker::run():";
#endif
        for (const auto &p : polygonENU.outer()) {
          snake::Length distance = bg::distance(originENU, p) * si::meter;
          distances.push_back(distance);
          snake::Angle alpha =
              (std::atan2(p.get<1>(), p.get<0>())) * si::radian;
          alpha =
              alpha < 0 * si::radian ? alpha + 2 * M_PI * si::radian : alpha;
          angles.push_back(alpha);
#ifdef DEBUG_CIRCULAR_SURVEY
          qWarning() << "distances, angles, coordinates:";
          qWarning() << to_string(distance).c_str();
          qWarning() << to_string(snake::Degree(alpha)).c_str();
          qWarning() << "x = " << p.get<0>() << "y = " << p.get<1>();
#endif
        }

        auto rMin = deltaR; // minimal circle radius
        snake::Angle alpha1(0 * degree::degree);
        snake::Angle alpha2(360 * degree::degree);
        // Determine r_min by successive approximation
        if (!bg::within(originENU, polygonENU)) {
          rMin = bg::distance(originENU, polygonENU) * si::meter;
        }

        auto rMax =
            (*std::max_element(distances.begin(),
                               distances.end())); // maximal circle radius

        // Scale parameters and coordinates.
        const auto rMinScaled =
            ClipperLib::cInt(std::round(rMin.value() * CLIPPER_SCALE));
        const auto deltaRScaled =
            ClipperLib::cInt(std::round(deltaR.value() * CLIPPER_SCALE));
        auto originScaled = ClipperLib::IntPoint{
            ClipperLib::cInt(std::round(originENU.get<0>())),
            ClipperLib::cInt(std::round(originENU.get<1>()))};

#ifdef SHOW_CIRCULAR_SURVEY_TIME
        auto s1 = std::chrono::high_resolution_clock::now();
#endif
        // Generate circle sectors.
        auto rScaled = rMinScaled;
        const auto nTran = long(std::ceil(((rMax - rMin) / deltaR).value()));
        vector<ClipperLib::Path> sectors(nTran, ClipperLib::Path());
        const auto nSectors =
            long(std::round(((alpha2 - alpha1) / deltaAlpha).value()));
#ifdef DEBUG_CIRCULAR_SURVEY
        qWarning() << "CSWorker::run(): sector parameres:";
        qWarning() << "alpha1: " << to_string(snake::Degree(alpha1)).c_str();
        qWarning() << "alpha2: " << to_string(snake::Degree(alpha2)).c_str();
        qWarning() << "n: "
                   << to_string((alpha2 - alpha1) / deltaAlpha).c_str();
        qWarning() << "nSectors: " << nSectors;
        qWarning() << "rMin: " << to_string(rMin).c_str();
        qWarning() << "rMax: " << to_string(rMax).c_str();
        qWarning() << "nTran: " << nTran;
#endif
        using ClipperCircle =
            GenericCircle<ClipperLib::cInt, ClipperLib::IntPoint>;
        for (auto &sector : sectors) {
          ClipperCircle circle(rScaled, originScaled);
          approximate(circle, nSectors, sector);
          rScaled += deltaRScaled;
        }
        // Clip sectors to polygonENU.
        ClipperLib::Path polygonClipper;
        auto &outer = polygonENU.outer();
        polygonClipper.reserve(outer.size() - 1);
        for (auto it = outer.begin(); it < outer.end() - 1; ++it) {
          auto x = ClipperLib::cInt(std::round(it->get<0>() * CLIPPER_SCALE));
          auto y = ClipperLib::cInt(std::round(it->get<1>() * CLIPPER_SCALE));
          polygonClipper.push_back(ClipperLib::IntPoint{x, y});
        }
        ClipperLib::Clipper clipper;
        clipper.AddPath(polygonClipper, ClipperLib::ptClip, true);
        clipper.AddPaths(sectors, ClipperLib::ptSubject, false);
        ClipperLib::PolyTree transectsClipper;
        clipper.Execute(ClipperLib::ctIntersection, transectsClipper,
                        ClipperLib::pftNonZero, ClipperLib::pftNonZero);

        // Extract transects from  PolyTree and convert them to
        // BoostLineString
        snake::Transects transectsENU;
        for (const auto &child : transectsClipper.Childs) {
          snake::BoostLineString transect;
          transect.reserve(child->Contour.size());
          for (const auto &vertex : child->Contour) {
            auto x = static_cast<double>(vertex.X) / CLIPPER_SCALE;
            auto y = static_cast<double>(vertex.Y) / CLIPPER_SCALE;
            transect.push_back(snake::BoostPoint(x, y));
          }
          transectsENU.push_back(transect);
        }
        // Join sectors which where slit due to clipping.
        const double th = 0.01;
        for (auto ito = transectsENU.begin(); ito < transectsENU.end(); ++ito) {
          for (auto iti = ito + 1; iti < transectsENU.end(); ++iti) {
            auto dist1 = bg::distance(ito->front(), iti->front());
            if (dist1 < th) {
              snake::BoostLineString temp;
              for (auto it = iti->end() - 1; it >= iti->begin(); --it) {
                temp.push_back(*it);
              }
              temp.insert(temp.end(), ito->begin(), ito->end());
              *ito = temp;
              transectsENU.erase(iti);
              break;
            }
            auto dist2 = bg::distance(ito->front(), iti->back());
            if (dist2 < th) {
              snake::BoostLineString temp;
              temp.insert(temp.end(), iti->begin(), iti->end());
              temp.insert(temp.end(), ito->begin(), ito->end());
              *ito = temp;
              transectsENU.erase(iti);
              break;
            }
            auto dist3 = bg::distance(ito->back(), iti->front());
            if (dist3 < th) {
              snake::BoostLineString temp;
              temp.insert(temp.end(), ito->begin(), ito->end());
              temp.insert(temp.end(), iti->begin(), iti->end());
              *ito = temp;
              transectsENU.erase(iti);
              break;
            }
            auto dist4 = bg::distance(ito->back(), iti->back());
            if (dist4 < th) {
              snake::BoostLineString temp;
              temp.insert(temp.end(), ito->begin(), ito->end());
              for (auto it = iti->end() - 1; it >= iti->begin(); --it) {
                temp.push_back(*it);
              }
              *ito = temp;
              transectsENU.erase(iti);
              break;
            }
          }
        }
        // Remove short transects
        for (auto it = transectsENU.begin(); it < transectsENU.end();) {
          if (bg::length(*it) < minLength.value()) {
            it = transectsENU.erase(it);
          } else {
            ++it;
          }
        }
        // Move transect with min. distance to the front.
        auto minDist = std::numeric_limits<double>::max();
        auto minIt = transectsENU.begin();
        bool reverse = false;
        for (auto it = transectsENU.begin(); it < transectsENU.end(); ++it) {
          auto distFront = bg::distance(originENU, it->front());
          auto distBack = bg::distance(originENU, it->back());
          if (distFront < minDist) {
            minDist = distFront;
            minIt = it;
            reverse = false;
          }
          if (distBack < minDist) {
            minDist = distBack;
            minIt = it;
            reverse = true;
          }
        }
        // Swap and reverse (if necessary).
        if (minIt != transectsENU.begin()) {
          auto minTransect = *minIt;
          if (reverse) {
            snake::BoostLineString rev;
            for (auto it = minTransect.end() - 1; it >= minTransect.begin();
                 --it) {
              rev.push_back(*it);
            }
            minTransect = rev;
          }
          *minIt = *transectsENU.begin();
          *transectsENU.begin() = minTransect;
        }
#ifdef SHOW_CIRCULAR_SURVEY_TIME
        qWarning() << "CSWorker::run(): transect gen. time: "
                   << std::chrono::duration_cast<std::chrono::milliseconds>(
                          std::chrono::high_resolution_clock::now() - s1)
                          .count()
                   << " ms";
#endif

        if (transectsENU.size() == 0) {
#ifdef DEBUG_CIRCULAR_SURVEY
          qWarning() << "CSWorker::run(): "
                        "not able to generate transects.";
#endif
        } else if (this->_restart) {
#ifdef DEBUG_CIRCULAR_SURVEY
          qWarning() << "CSWorker::run(): "
                        "restart requested.";
#endif
        } else {
          // Prepare data for routing.
          std::vector<snake::TransectInfo> transectsInfo;
          snake::Route route;
          const auto routingStart = std::chrono::high_resolution_clock::now();
          const auto maxRoutingTime = std::chrono::minutes(1);
          const auto routingEnd = routingStart + maxRoutingTime;
          const auto &restart = this->_restart;
          auto stopLambda = [&restart, routingEnd] {
            bool expired =
                std::chrono::high_resolution_clock::now() > routingEnd;
            return restart || expired;
          };
          std::string errorString;
          // Route transects;
          bool success = snake::route(polygonENU, transectsENU, transectsInfo,
                                      route, stopLambda, errorString);
          if (!success && !this->_restart) {
#ifdef DEBUG_CIRCULAR_SURVEY
            qWarning() << "CSWorker::run(): "
                          "routing failed.";
#endif
          } else if (this->_restart) {
#ifdef DEBUG_CIRCULAR_SURVEY
            qWarning() << "CSWorker::run(): "
                          "restart requested.";
#endif
          } else {
            // Remove return path.
            const auto &info = transectsInfo.back();
            const auto &lastTransect = transectsENU[info.index];
            const auto &lastWaypoint =
                info.reversed ? lastTransect.front() : lastTransect.back();
            auto &wp = route.back();
            while (wp != lastWaypoint) {
              route.pop_back();
              wp = route.back();
            }
            // Convert to geo coordinates and notify main thread.
            auto pRoute = PtrRoute(new Route());
            for (const auto &vertex : route) {
              QGeoCoordinate c;
              snake::fromENU(origin, vertex, c);
              pRoute->append(c);
            }
            emit ready(pRoute);
#ifdef DEBUG_CIRCULAR_SURVEY
            qWarning() << "CSWorker::run(): "
                          "concurrent update success.";
#endif
          }
        }
      }
#ifdef SHOW_CIRCULAR_SURVEY_TIME
      qWarning() << "CSWorker::run(): execution time: "
                 << std::chrono::duration_cast<std::chrono::milliseconds>(
                        std::chrono::high_resolution_clock::now() - start)
                        .count()
                 << " ms";
#endif
      this->_calculating = false;
      emit calculatingChanged();
    }
#ifdef DEBUG_CIRCULAR_SURVEY
    else {
      qWarning() << "CSWorker::run(): preconditions failed.";
    }
#endif
    Lock lk2(this->_mutex);
    if (!this->_restart) {
      this->_cv.wait(lk2, [this] { return this->_restart.load(); });
    }
    this->_restart = false;
  }
  qWarning() << "CSWorker::run(): thread end.";
}