geometry.h 8.45 KB
Newer Older
1 2 3 4 5
#pragma once

#include <array>
#include <atomic>
#include <functional>
6
#include <iostream>
7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <memory>
#include <string>
#include <vector>

#include <boost/geometry.hpp>
#include <boost/units/systems/angle/degrees.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/io.hpp>
#include <boost/units/systems/si/plane_angle.hpp>
#include <boost/units/systems/si/prefixes.hpp>

namespace bg = boost::geometry;
namespace bu = boost::units;

21 22
#include "QGCQGeoCoordinate.h"
#include "QmlObjectListModel.h"
23
#include <QPointF>
24

25
namespace geometry {
26 27 28 29 30
//=========================================================================
// Geometry stuff.
//=========================================================================

// Double geometry.
31
typedef double FloatType;
32 33 34 35
typedef bg::model::point<double, 2, bg::cs::cartesian> FPoint;
typedef bg::model::polygon<FPoint> FPolygon;
typedef bg::model::linestring<FPoint> FLineString;
typedef bg::model::box<FPoint> FBox;
36
typedef std::vector<FLineString> LineStringArray;
37 38

// Integer geometry.
39 40 41 42 43
typedef long long IntType;
typedef bg::model::point<IntType, 2, bg::cs::cartesian> IPoint;
typedef bg::model::polygon<IPoint> IPolygon;
typedef bg::model::ring<IPoint> IRing;
typedef bg::model::linestring<IPoint> ILineString;
44 45 46 47 48 49 50 51 52

FPoint int2Float(const IPoint &ip);
FPoint int2Float(const IPoint &ip, IntType scale);
IPoint float2Int(const FPoint &ip);
IPoint float2Int(const FPoint &ip, IntType scale);

template <class T> class Matrix;

template <class DataType>
53
std::ostream &operator<<(std::ostream &os, const Matrix<DataType> &matrix) {
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
  for (std::size_t i = 0; i < matrix.m(); ++i) {
    for (std::size_t j = 0; j < matrix.n(); ++j) {
      os << "(" << i << "," << j << "):" << matrix(i, j) << std::endl;
    }
  }
  return os;
}

// Matrix
template <class DataType> class Matrix {
public:
  using value_type = DataType;
  Matrix(std::size_t m, std::size_t n) : _m(m), _n(n) { _matrix.resize(m * n); }
  Matrix(std::size_t m, std::size_t n, DataType value) : _m(m), _n(n) {
    _matrix.resize(m * n, value);
  }

  DataType &operator()(std::size_t i, std::size_t j) {
    assert(i < _m);
    assert(j < _n);
    return _matrix[i * _m + j];
  }

  const DataType &operator()(std::size_t i, std::size_t j) const {
    assert(i < _m);
    assert(j < _n);
    return _matrix[i * _m + j];
  }

  std::size_t m() const { return _n; }
  std::size_t n() const { return _n; }

86 87
  friend std::ostream &operator<<<>(std::ostream &os,
                                    const Matrix<DataType> &dt);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

private:
  std::vector<DataType> _matrix;
  const std::size_t _m;
  const std::size_t _n;
}; // Matrix

struct BoundingBox {
  BoundingBox();

  void clear();

  double width;
  double height;
  double angle;
  FPolygon corners;
};

106 107 108
static constexpr int earth_radius = 6371000; // meters (m)
static constexpr double epsilon =
    std::numeric_limits<double>::epsilon(); // meters (m)
109

110 111 112
template <class GeoPoint1, class GeoPoint2, class Point>
void toENU(const GeoPoint1 &origin, const GeoPoint2 &in, Point &out) {

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  using namespace std;

  double lat_rad = in.latitude() * M_PI / 180;
  double lon_rad = in.longitude() * M_PI / 180;

  double ref_lon_rad = origin.longitude() * M_PI / 180;
  double ref_lat_rad = origin.latitude() * M_PI / 180;

  double sin_lat = sin(lat_rad);
  double cos_lat = cos(lat_rad);
  double cos_d_lon = cos(lon_rad - ref_lon_rad);

  double ref_sin_lat = sin(ref_lat_rad);
  double ref_cos_lat = cos(ref_lat_rad);

128 129 130 131
  double value = ref_sin_lat * sin_lat + ref_cos_lat * cos_lat * cos_d_lon;
  value = value > 1 ? 1 : value;
  value = value < -1 ? -1 : value;
  double c = acos(value);
132 133 134 135 136 137
  double k = (fabs(c) < epsilon) ? 1.0 : (c / sin(c));

  double x = k * cos_lat * sin(lon_rad - ref_lon_rad) * earth_radius;
  double y = k * (ref_cos_lat * sin_lat - ref_sin_lat * cos_lat * cos_d_lon) *
             earth_radius;

138 139 140 141
  out.setX(x);
  out.setY(y);
}

142 143
template <class GeoPoint1, class Point, class GeoPoint2>
void fromENU(const GeoPoint1 &origin, const Point &in, GeoPoint2 &out) {
144 145 146

  using namespace std;

147 148
  double x_rad = in.x() / earth_radius;
  double y_rad = in.y() / earth_radius;
149 150 151 152 153 154 155 156 157 158 159 160 161 162
  double c = sqrt(y_rad * y_rad + x_rad * x_rad);
  double sin_c = sin(c);
  double cos_c = cos(c);

  double ref_lon_rad = origin.longitude() * M_PI / 180;
  double ref_lat_rad = origin.latitude() * M_PI / 180;

  double ref_sin_lat = sin(ref_lat_rad);
  double ref_cos_lat = cos(ref_lat_rad);

  double lat_rad;
  double lon_rad;

  if (fabs(c) > epsilon) {
163 164 165 166 167
    double v1 = cos_c * ref_sin_lat + (y_rad * sin_c * ref_cos_lat) / c;
    v1 = v1 > 1 ? 1 : v1;
    v1 = v1 < -1 ? -1 : v1;
    lat_rad = asin(v1);

168 169 170 171 172 173 174 175 176 177 178 179
    lon_rad =
        (ref_lon_rad + atan2(x_rad * sin_c, c * ref_cos_lat * cos_c -
                                                y_rad * ref_sin_lat * sin_c));

  } else {
    lat_rad = ref_lat_rad;
    lon_rad = ref_lon_rad;
  }

  out.setLatitude(lat_rad * 180 / M_PI);
  out.setLongitude(lon_rad * 180 / M_PI);
  out.setAltitude(origin.altitude());
180 181
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
template <class GeoPoint1, class GeoPoint2>
void toENU(const GeoPoint1 &origin, const GeoPoint2 &in, FPoint &out) {

  QPointF p;
  toENU(origin, in, p);
  out.set<0>(p.x());
  out.set<1>(p.y());
}

template <class GeoPoint1, class GeoPoint2>
void fromENU(const GeoPoint1 &origin, const FPoint &in, GeoPoint2 &out) {

  QPointF p(in.get<0>(), in.get<1>());
  fromENU(origin, p, out);
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
template <class GeoPoint, class Container1, class Container2>
void areaToEnu(const GeoPoint &origin, const Container1 &in, Container2 &out) {
  for (auto &vertex : in) {
    typename Container2::value_type p;
    toENU(origin, vertex, p);
    out.push_back(p);
  }
}

template <class GeoPoint, class Container>
void areaToEnu(const GeoPoint &origin, const Container &in, FPolygon &out) {
  for (auto &vertex : in) {
    FPoint p;
    toENU(origin, vertex, p);
    out.outer().push_back(p);
  }
  bg::correct(out);
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
template <class GeoPoint>
void areaToEnu(const GeoPoint &origin, QmlObjectListModel &in, FPolygon &out) {
  FPolygon buffer;
  for (int i = 0; i < in.count(); ++i) {
    auto vertex = in.value<const QGCQGeoCoordinate *>(i);
    if (vertex != nullptr) {
      FPoint p;
      toENU(origin, vertex->coordinate(), p);
      buffer.outer().push_back(p);
    } else {
      return;
    }
  }
  bg::correct(buffer);
  out = std::move(buffer);
}

234
template <class GeoPoint, class Container1, class Container2>
235 236
void areaFromEnu(const GeoPoint &origin, const Container1 &in,
                 Container2 &out) {
237 238 239 240 241 242 243 244
  for (auto &vertex : in) {
    typename Container2::value_type p;
    fromENU(origin, vertex, p);
    out.push_back(p);
  }
}

template <class GeoPoint, class Container>
245
void areaFromEnu(const GeoPoint &origin, const FPolygon &in, Container &out) {
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
  for (auto &vertex : in.outer()) {
    typename Container::value_type p;
    fromENU(origin, vertex, p);
    out.push_back(p);
  }
}

void polygonCenter(const FPolygon &polygon, FPoint &center);
bool minimalBoundingBox(const FPolygon &polygon, BoundingBox &minBBox);
void offsetPolygon(const FPolygon &polygon, FPolygon &polygonOffset,
                   double offset);
void graphFromPolygon(const FPolygon &polygon, const FLineString &vertices,
                      Matrix<double> &graph);
bool toDistanceMatrix(Matrix<double> &graph);
bool dijkstraAlgorithm(size_t numElements, size_t startIndex, size_t endIndex,
                       std::vector<size_t> &elementPath, double &length,
                       std::function<double(size_t, size_t)> distanceDij);

bool shortestPathFromGraph(const Matrix<double> &graph, const size_t startIndex,
                           const size_t endIndex, std::vector<size_t> &pathIdx);

typedef bu::quantity<bu::si::length> Length;
typedef bu::quantity<bu::si::area> Area;
typedef bu::quantity<bu::si::plane_angle> Angle;
typedef bu::quantity<bu::si::plane_angle> Radian;
typedef bu::quantity<bu::degree::plane_angle> Degree;

bool joinedArea(const std::vector<FPolygon *> &areas, FPolygon &jArea);
bool joinedArea(const FPolygon &mArea, const FPolygon &sArea,
                const FPolygon &corridor, FPolygon &jArea,
                std::string &errorString);
277
} // namespace geometry
278 279 280 281 282 283

// operator== and operator!= for boost point
namespace boost {
namespace geometry {
namespace model {

284 285 286 287
bool operator==(::geometry::FPoint &p1, ::geometry::FPoint &p2);
bool operator!=(::geometry::FPoint &p1, ::geometry::FPoint &p2);
bool operator==(::geometry::IPoint &p1, ::geometry::IPoint &p2);
bool operator!=(::geometry::IPoint &p1, ::geometry::IPoint &p2);
288 289 290 291

} // namespace model
} // namespace geometry
} // namespace boost