snake.cpp 7.57 KB
Newer Older
1 2
#include "snake.h"

Valentin Platzgummer's avatar
Valentin Platzgummer committed
3 4 5 6
#include "clipper.hpp"
#include "assert.h"


7 8
namespace snake {

Valentin Platzgummer's avatar
Valentin Platzgummer committed
9 10 11 12 13 14 15
Scenario::Scenario() :
    _mAreaBoundingBox(min_bbox_rt{0, 0, 0, Point2D{0,0}})
{

}

bool Scenario::setArea(Area &area)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    {
        if (area.geoPolygon.size() < 3){
            error_str = "Area has less than three vertices.";
            return false;
        }
        if (area.type == MeasurementArea)
            return Scenario::_setMeasurementArea(area);
        else if (area.type == ServiceArea)
            return Scenario::_setServiceArea(area);
        else if (area.type == Corridor)
            return Scenario::_setCorridor(area);
        return false;
    }

    bool Scenario::_areas2enu()
    {
        if (_measurementArea.geoPolygon.size() > 0){
            _measurementAreaENU.clear();
            for(auto vertex : _measurementArea.geoPolygon) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
35
                    Point3D ENUVertex = toENU(_geoOrigin, Point3D{vertex[0], vertex[1], _measurementArea.altitude});
36 37 38 39 40 41 42
                    _measurementAreaENU.push_back(Point2D{ENUVertex[0], ENUVertex[1]});
            }
            _homePositionENU = polygonCenter(_measurementAreaENU);

            if (_serviceArea.geoPolygon.size() > 0){
                _serviceAreaENU.clear();
                for(auto vertex : _serviceArea.geoPolygon) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
43
                        Point3D ENUVertex = toENU(_geoOrigin, Point3D{vertex[0], vertex[1], _serviceArea.altitude});
44 45 46 47 48 49 50 51 52 53
                        _serviceAreaENU.push_back(Point2D{ENUVertex[0], ENUVertex[1]});
                }
            } else{
                error_str = "Service area has no vertices.";
                return false;
            }

            if (_corridor.geoPolygon.size() > 0){
                _corridorENU.clear();
                for(auto vertex : _corridor.geoPolygon) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
54
                        Point3D ENUVertex = toENU(_geoOrigin, Point3D{vertex[0], vertex[1], _corridor.altitude});
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
                        _corridorENU.push_back(Point2D{ENUVertex[0], ENUVertex[1]});
                }
            }

            return true;
        }

        error_str = "Measurement area has no vertices.";
        return false;
    }

    bool Scenario::_setMeasurementArea(Area &area)
    {
        if (area.geoPolygon.size() <= 0)
            return false;
        _geoOrigin = area.geoPolygon[0];
        _measurementArea = area;
        _measurementAreaENU.clear();
        _serviceAreaENU.clear();
        _corridorENU.clear();
        return true;

    }

    bool Scenario::_setServiceArea(Area &area)
    {
        if (area.geoPolygon.size() <= 0)
            return false;
        _serviceArea = area;
        _serviceAreaENU.clear();
        return true;
    }

    bool Scenario::_setCorridor(Area &area)
    {
        if (area.geoPolygon.size() <= 0)
            return false;
        _corridor = area;
        _corridorENU.clear();
        return true;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
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
    bool Scenario::_calculateBoundingBox()
    {
        _mAreaBoundingBox = minimalBoundingBox(_measurementAreaENU);
        return true;
    }
    /**
     * Devides the (measurement area) bounding  box into tiles and clips it to the measurement area.
     *
     * Devides the (measurement area) bounding  box into tiles of width \p tileWidth and height \p tileHeight.
     * Clips the resulting tiles to the measurement area. Tiles are rejected, if their area is smaller than \p minTileArea.
     * The function assumes that \a _measurementAreaENU and \a _mAreaBoundingBox have correct values. \see \ref Scenario::_areas2enu() and \ref
     * Scenario::_calculateBoundingBox().
     *
     * @param tileWidth The width of a tile.
     * @param tileHeight The heigth of a tile.
     * @param minTileArea The minimal area of a tile.
     *
     * @return Returns true if successful.
     */
    bool Scenario::_calculateTiles(double tileWidth, double tileHeight, double minTileArea)
    {
        std::vector<Point2DList> tiles_unclipped;

        return true;
    }

    bool Scenario::_calculateJoinedArea()
    {
        ClipperLib::Path measurementArea;
        ClipperLib::Path serviceArea;
        ClipperLib::Path corridor;

        // Apply scaling and convert ENU polygons (double) to ClipperLib::cInt polygons.
        for (auto vertex : _measurementAreaENU){
            ClipperLib::IntPoint intVertex{ClipperLib::cInt(vertex[0]*clipper_scale),
                                           ClipperLib::cInt(vertex[1]*clipper_scale)};
            measurementArea.push_back(intVertex);
        }

        for (auto vertex : _serviceAreaENU){
            ClipperLib::IntPoint intVertex{ClipperLib::cInt(vertex[0]*clipper_scale),
                                           ClipperLib::cInt(vertex[1]*clipper_scale)};
            serviceArea.push_back(intVertex);
        }

        bool corridorValid = false;
        if (_corridorENU.size() > 0) {
            for (auto vertex : _corridorENU){
                ClipperLib::IntPoint intVertex{ClipperLib::cInt(vertex[0]*clipper_scale),
                                               ClipperLib::cInt(vertex[1]*clipper_scale)};
                corridor.push_back(intVertex);
            }
            corridorValid = true;
        }

        // Check if measurement area and service area are overlapping.
        ClipperLib::Clipper cp1;
        cp1.AddPath(measurementArea, ClipperLib::ptClip, true);
        cp1.AddPath(serviceArea, ClipperLib::ptSubject, true);

        // Execute clipper
        ClipperLib::Paths solution;
        cp1.Execute(ClipperLib::ctIntersection, solution, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd);

        // Measurement area and service area overlapping?
        bool overlaping_s_m = false;
        if (solution.size() > 0){
            overlaping_s_m = true;
        }


        // Check if corridor is connecting measurement area and service area.
        bool corridor_is_connection = false;
        if (corridorValid) {
            ClipperLib::Clipper cp2;
            solution.clear();
            cp2.AddPath(measurementArea, ClipperLib::ptClip, true);
            cp2.AddPath(corridor, ClipperLib::ptSubject, true);
            cp2.Execute(ClipperLib::ctIntersection, solution, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd);

            // Corridor overlaping with measurement area?
            if (solution.size() > 0) {
                // Check if corridor overlaps with service area.
                cp2.Clear();
                solution.clear();
                cp2.AddPath(serviceArea, ClipperLib::ptClip, true);
                cp2.AddPath(corridor, ClipperLib::ptSubject, true);
                cp2.Execute(ClipperLib::ctIntersection, solution, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd);

                // Corridor overlaping with service area?
                if (solution.size() > 0) {
                    corridor_is_connection = true;
                }
            }
        }

        // Are areas joinable?
        if (overlaping_s_m){
            if(corridor_is_connection){
                cp1.AddPath(corridor, ClipperLib::ptSubject, true);
            }
        } else if (corridor_is_connection){
            cp1.AddPath(corridor, ClipperLib::ptSubject, true);
        } else {
            error_str = "Areas are not overlapping";
            return false;
        }

        // Join areas.
        solution.clear();
        cp1.Execute(ClipperLib::ctUnion, solution, ClipperLib::pftEvenOdd, ClipperLib::pftEvenOdd);

        if (solution.size() != 1)
            assert(0);

        _joinedAreaENU.clear();
        for (auto intVertex : solution[0]){
            Point2D vertex{double(intVertex.X)/clipper_scale, double(intVertex.Y)/clipper_scale};
            _joinedAreaENU.push_back(vertex);
        }

        return true;
    }

221
}