snake_geometry.h 4.01 KB
Newer Older
1 2 3
#pragma once
#include <vector>
#include <array>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
4
#include <boost/geometry.hpp>
Valentin Platzgummer's avatar
Valentin Platzgummer committed
5
#include <bits/stl_set.h>
6

Valentin Platzgummer's avatar
Valentin Platzgummer committed
7 8
namespace bg = boost::geometry;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
9
namespace snake_geometry {
10

Valentin Platzgummer's avatar
Valentin Platzgummer committed
11
typedef std::array<double, 3>   Point2D;
12 13
typedef std::array<double, 3>   Point3D;
typedef std::array<double, 3>   GeoPoint3D;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
14 15 16 17 18 19 20
typedef std::array<double, 2>   GeoPoint2D;
typedef std::vector<Point2D>    Point2DList;
typedef std::vector<Point3D>    Point3DList;
typedef std::vector<GeoPoint2D> GeoPoint2DList;
typedef std::vector<GeoPoint3D> GeoPoint3DList;

typedef bg::model::point<double, 2, bg::cs::cartesian>  BoostPoint;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
21
//typedef std::vector<BoostPoint>                        BoostPointList;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
22
typedef bg::model::polygon<BoostPoint>                  BoostPolygon;
23
typedef bg::model::linestring<BoostPoint>               BoostLineString;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
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
typedef std::vector<std::vector<int64_t>>               Int64Matrix;


template<class T>
class Matrix{
public:
    Matrix() :
        _elements(0),
        _m(0),
        _n(0),
        _isInitialized(false)
    {
    }
    Matrix(size_t m, size_t n) : Matrix(m, n, T{0}){}
    Matrix(size_t m, size_t n, T value) :
        _elements(m*n),
        _m(m),
        _n(n),
        _isInitialized(true)
    {
        assert((m > 0) || (n > 0));
        _matrix.resize(_elements, value);
    }

    double get(size_t i, size_t j) const
    {
        assert(_isInitialized);
        assert(i < _m);
        assert(j < _n);
        return _matrix[i*_m+j];
    }

    size_t getM() const { return _m;}
    size_t getN() const { return _n;}

    void set(size_t i, size_t j, const T &value)
    {
        assert(_isInitialized);
        assert(i < _m);
        assert(j < _n);
        _matrix[i*_m+j] = value;
    }

    void setDimension(size_t m, size_t n, const T &value)
    {
        assert((m > 0) || (n > 0));
        assert(!_isInitialized);
        if (!_isInitialized){
            _m = m;
            _n = n;
            _elements = n*m;
            _matrix.resize(_elements, value);
        }
    }

    void setDimension(size_t m, size_t n)
    {
        assert((m > 0) || (n > 0));
        assert(!_isInitialized);
        if (!_isInitialized){
            _m = m;
            _n = n;
            _elements = n*m;
            _matrix.resize(_elements);
        }
    }

private:
    size_t _elements;
    size_t _m;
    size_t _n;
    bool   _isInitialized;
    std::vector<T> _matrix;
};
98 99 100 101 102

typedef struct {
    double width;
    double height;
    double angle;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
103
    BoostPolygon corners;
104 105
}min_bbox_rt;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
106 107 108 109 110 111 112
void toENU(const GeoPoint3D &WGS84Reference, const GeoPoint3D &WGS84Position, Point3D &ENUPosition);
void fromENU(const Point3D &WGS84Reference, const Point3D &ENUPosition, GeoPoint3D &WGS84Position);

void polygonCenter(const BoostPolygon &polygon, BoostPoint &center);
void minimalBoundingBox(const BoostPolygon &polygon, min_bbox_rt &minBBox);
void offsetPolygon(const BoostPolygon &polygon, BoostPolygon &polygonOffset, double offset);

Valentin Platzgummer's avatar
Valentin Platzgummer committed
113 114
void graphFromPolygon(const BoostPolygon &polygon, const BoostLineString &vertices, Matrix<double> &graph);
void toDistanceMatrix(Matrix<double> &graph);
115 116 117 118 119
bool dijkstraAlgorithm(const size_t numElements,
                       size_t startIndex,
                       size_t endIndex,
                       std::vector<size_t> &elementPath,
                       std::function<double (const size_t, const size_t)> distanceDij);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
120 121 122 123
void shortestPathFromGraph(const Matrix<double> &graph,
                           size_t startIndex,
                           size_t endIndex,
                           std::vector<size_t> &pathIdx);
124 125


Valentin Platzgummer's avatar
Valentin Platzgummer committed
126 127 128 129
void rotateDeg(const Point2DList &point_list, Point2DList &rotated_point_list, double degree);
void rotateRad(const Point2DList &point_list, Point2DList &rotated_point_list, double rad);

bool isClockwise(const Point2DList &point_list);
130

Valentin Platzgummer's avatar
Valentin Platzgummer committed
131 132
void toBoost(const Point2D &point, BoostPoint &boost_point);
void toBoost(const Point2DList &point_list, BoostPolygon &boost_polygon);
133

Valentin Platzgummer's avatar
Valentin Platzgummer committed
134 135
void fromBoost(const BoostPoint &boost_point, Point2D &point);
void fromBoost(const BoostPolygon &boost_polygon, Point2DList &point_list);
136
}