Skip to content
snake.cpp 35.9 KiB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
#include <iostream>

#include "snake.h"

Valentin Platzgummer's avatar
Valentin Platzgummer committed
#include <mapbox/polylabel.hpp>
#include <mapbox/geometry.hpp>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
Valentin Platzgummer's avatar
Valentin Platzgummer committed

Valentin Platzgummer's avatar
Valentin Platzgummer committed
#include "clipper/clipper.hpp"
#define CLIPPER_SCALE 10000

#include "ortools/constraint_solver/routing.h"
#include "ortools/constraint_solver/routing_enums.pb.h"
#include "ortools/constraint_solver/routing_index_manager.h"
#include "ortools/constraint_solver/routing_parameters.h"

Valentin Platzgummer's avatar
Valentin Platzgummer committed
using namespace operations_research;

#ifndef NDEBUG
//#define SHOW_TIME
#endif

Valentin Platzgummer's avatar
Valentin Platzgummer committed
namespace bg = boost::geometry;
namespace trans = bg::strategy::transform;

namespace snake {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
//=========================================================================
// Geometry stuff.
//=========================================================================

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
void polygonCenter(const BoostPolygon &polygon, BoostPoint &center)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
 using namespace mapbox;
if (polygon.outer().empty())
    return;
geometry::polygon<double> p;
geometry::linear_ring<double> lr1;
for (size_t i = 0; i < polygon.outer().size(); ++i) {
    geometry::point<double> vertex(polygon.outer()[i].get<0>(), polygon.outer()[i].get<1>());
    lr1.push_back(vertex);
}
p.push_back(lr1);
geometry::point<double> c = polylabel(p);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
center.set<0>(c.x);
center.set<1>(c.y);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
void minimalBoundingBox(const BoostPolygon &polygon, BoundingBox &minBBox)
{
 /*
 Find the minimum-area bounding box of a set of 2D points

 The input is a 2D convex hull, in an Nx2 numpy array of x-y co-ordinates.
 The first and last points points must be the same, making a closed polygon.
 This program finds the rotation angles of each edge of the convex polygon,
 then tests the area of a bounding box aligned with the unique angles in
 90 degrees of the 1st Quadrant.
 Returns the

 Tested with Python 2.6.5 on Ubuntu 10.04.4 (original version)
 Results verified using Matlab

 Copyright (c) 2013, David Butterworth, University of Queensland
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
     * Neither the name of the Willow Garage, Inc. nor the names of its
       contributors may be used to endorse or promote products derived from
       this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 */

 if (polygon.outer().empty())
     return;
 BoostPolygon convex_hull;
 bg::convex_hull(polygon, convex_hull);

 //cout << "Convex hull: " << bg::wkt<BoostPolygon2D>(convex_hull) << endl;

 //# Compute edges (x2-x1,y2-y1)
 std::vector<BoostPoint> edges;
 auto convex_hull_outer = convex_hull.outer();
 for (long i=0; i < long(convex_hull_outer.size())-1; ++i) {
     BoostPoint p1      = convex_hull_outer.at(i);
     BoostPoint p2      = convex_hull_outer.at(i+1);
     double edge_x   = p2.get<0>() - p1.get<0>();
     double edge_y   = p2.get<1>() - p1.get<1>();
     edges.push_back(BoostPoint{edge_x, edge_y});
 }

//    cout << "Edges: ";
//    for (auto e : edges)
//        cout << e.get<0>() << " " << e.get<1>() << ",";
//    cout << endl;

 // Calculate unique edge angles  atan2(y/x)
 double angle_scale = 1e3;
 std::set<long> angles_long;
 for (auto vertex : edges) {
     double angle = std::fmod(atan2(vertex.get<1>(), vertex.get<0>()), M_PI / 2);
     angle = angle < 0 ? angle + M_PI / 2 : angle; // want strictly positive answers
     angles_long.insert(long(round(angle*angle_scale)));
 }
 std::vector<double> edge_angles;
 for (auto a : angles_long)
     edge_angles.push_back(double(a)/angle_scale);


//    cout << "Unique angles: ";
//    for (auto e : edge_angles)
//        cout << e*180/M_PI << ",";
//    cout << endl;

 double min_area = std::numeric_limits<double>::infinity();
 // Test each angle to find bounding box with smallest area
 // print "Testing", len(edge_angles), "possible rotations for bounding box... \n"
 for (double angle : edge_angles){

     trans::rotate_transformer<bg::degree, double, 2, 2> rotate(angle*180/M_PI);
     BoostPolygon hull_rotated;
     bg::transform(convex_hull, hull_rotated, rotate);
     //cout << "Convex hull rotated: " << bg::wkt<BoostPolygon2D>(hull_rotated) << endl;

     bg::model::box<BoostPoint> box;
     bg::envelope(hull_rotated, box);
//        cout << "Bounding box: " << bg::wkt<bg::model::box<BoostPoint2D>>(box) << endl;

     //# print "Rotated hull points are \n", rot_points
     BoostPoint min_corner = box.min_corner();
     BoostPoint max_corner = box.max_corner();
     double min_x = min_corner.get<0>();
     double max_x = max_corner.get<0>();
     double min_y = min_corner.get<1>();
     double max_y = max_corner.get<1>();
//        cout << "min_x: " << min_x << endl;
//        cout << "max_x: " << max_x << endl;
//        cout << "min_y: " << min_y << endl;
//        cout << "max_y: " << max_y << endl;

     // Calculate height/width/area of this bounding rectangle
     double width    = max_x - min_x;
     double height   = max_y - min_y;
     double area     = width * height;
//        cout << "Width: " << width << endl;
//        cout << "Height: " << height << endl;
//        cout << "area: " << area << endl;
//        cout << "angle: " << angle*180/M_PI << endl;

     // Store the smallest rect found first (a simple convex hull might have 2 answers with same area)
     if (area < min_area){
         min_area = area;
         minBBox.angle  = angle;
         minBBox.width  = width;
         minBBox.height = height;

         minBBox.corners.clear();
         minBBox.corners.outer().push_back(BoostPoint{min_x, min_y});
         minBBox.corners.outer().push_back(BoostPoint{min_x, max_y});
         minBBox.corners.outer().push_back(BoostPoint{max_x, max_y});
         minBBox.corners.outer().push_back(BoostPoint{max_x, min_y});
         minBBox.corners.outer().push_back(BoostPoint{min_x, min_y});
     }
     //cout << endl << endl;
 }


 // Transform corners of minimal bounding box.
 trans::rotate_transformer<bg::degree, double, 2, 2> rotate(-minBBox.angle*180/M_PI);
 BoostPolygon rotated_polygon;
 bg::transform(minBBox.corners, rotated_polygon, rotate);
 minBBox.corners = rotated_polygon;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
void offsetPolygon(const BoostPolygon &polygon, BoostPolygon &polygonOffset, double offset)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
 bg::strategy::buffer::distance_symmetric<double> distance_strategy(offset);
 bg::strategy::buffer::join_miter join_strategy(3);
 bg::strategy::buffer::end_flat end_strategy;
 bg::strategy::buffer::point_square point_strategy;
 bg::strategy::buffer::side_straight side_strategy;


 bg::model::multi_polygon<BoostPolygon> result;

 bg::buffer(polygon, result, distance_strategy, side_strategy, join_strategy, end_strategy, point_strategy);

 if (result.size() > 0)
     polygonOffset = result[0];

Valentin Platzgummer's avatar
Valentin Platzgummer committed

void graphFromPolygon(const BoostPolygon &polygon, const BoostLineString &vertices, Matrix<double> &graph)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
 size_t n = graph.getN();

 for (size_t i=0; i < n; ++i) {
     BoostPoint v1 = vertices[i];
     for (size_t j=i+1; j < n; ++j){
         BoostPoint v2 = vertices[j];
         BoostLineString path{v1, v2};

         double distance = 0;
         if (!bg::within(path, polygon))
             distance = std::numeric_limits<double>::infinity();
         else
             distance = bg::length(path);

         graph.set(i, j, distance);
         graph.set(j, i, distance);
     }
 }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
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
 if (    startIndex >= numElements
      || endIndex >= numElements
      || endIndex == startIndex) {
     return false;
 }
 // Node struct
 // predecessorIndex is the index of the predecessor node (nodeList[predecessorIndex])
 // distance is the distance between the node and the start node
 // node number is stored by the position in nodeList
 struct Node{
     int predecessorIndex = -1;
     double distance = std::numeric_limits<double>::infinity();
 };

 // The list with all Nodes (elements)
 std::vector<Node> nodeList(numElements);
 // This list will be initalized with indices referring to the elements of nodeList.
 // Elements will be successively remove during the execution of the Dijkstra Algorithm.
 std::vector<size_t> workingSet(numElements);

 //append elements to node list
 for (size_t i = 0; i < numElements; ++i) workingSet[i] = i;


 nodeList[startIndex].distance = 0;

 // Dijkstra Algorithm
 // https://de.wikipedia.org/wiki/Dijkstra-Algorithmus
 while (workingSet.size() > 0) {
     // serach Node with minimal distance
     double minDist = std::numeric_limits<double>::infinity();
     int minDistIndex_WS = -1; // WS = workinSet
     for (size_t i = 0; i < workingSet.size(); ++i) {
         const int nodeIndex = workingSet.at(i);
         const double dist = nodeList.at(nodeIndex).distance;
         if (dist < minDist) {
             minDist = dist;
             minDistIndex_WS = i;
         }
     }
     if (minDistIndex_WS == -1)
             return false;

     size_t indexU_NL = workingSet.at(minDistIndex_WS); // NL = nodeList
     workingSet.erase(workingSet.begin()+minDistIndex_WS);
     if (indexU_NL == endIndex) // shortest path found
         break;

     const double distanceU = nodeList.at(indexU_NL).distance;
     //update distance
     for (size_t i = 0; i < workingSet.size(); ++i) {
         int indexV_NL = workingSet[i]; // NL = nodeList
         Node* v = &nodeList[indexV_NL];
         double dist = distanceDij(indexU_NL, indexV_NL);
         // is ther an alternative path which is shorter?
         double alternative = distanceU + dist;
         if (alternative < v->distance)  {
             v->distance         = alternative;
             v->predecessorIndex = indexU_NL;
         }
     }

 }
 // end Djikstra Algorithm


 // reverse assemble path
 int e = endIndex;
 while (1) {
     if (e == -1) {
         if (elementPath[0] == startIndex)// check if starting point was reached
             break;
         return false;
     }
     elementPath.insert(elementPath.begin(), e);

     //Update Node
     e = nodeList[e].predecessorIndex;

 }
 return true;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
void toDistanceMatrix(Matrix<double> &graph)
{
 size_t n = graph.getN();

 auto distance = [graph](size_t i, size_t j){
     return graph.get(i,j);
 };


 std::vector<size_t> path;
 for (size_t i=0; i < n; ++i) {
     for (size_t j=i+1; j < n; ++j){
         double d = graph.get(i,j);
         if (!std::isinf(d))
             continue;
         path.clear();
         bool ret = dijkstraAlgorithm(n, i, j, path, distance);
         assert(ret);
         (void)ret;
//            cout << "(" << i << "," << j << ") d: " << d << endl;
//            cout << "Path size: " << path.size() << endl;
//            for (auto idx : path)
//                cout << idx << " ";
//            cout << endl;

         d = 0;
         for (long k=0; k < long(path.size())-1; ++k) {
             size_t idx0 = path[k];
             size_t idx1 = path[k+1];
             double d0 = graph.get(idx0, idx1);
             assert(std::isinf(d0) == false);
             d += d0;
         }

         graph.set(i, j, d);
         graph.set(j, i, d);
     }
 }
}
Valentin Platzgummer's avatar
Valentin Platzgummer committed
void shortestPathFromGraph(const Matrix<double> &graph, size_t startIndex, size_t endIndex, std::vector<size_t> &pathIdx)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
 if (!std::isinf(graph.get(startIndex, endIndex))){
     pathIdx.push_back(startIndex);
     pathIdx.push_back(endIndex);
 } else {
     auto distance = [graph](size_t i, size_t j){
         return graph.get(i, j);
     };
     bool ret = dijkstraAlgorithm(graph.getN(), startIndex, endIndex, pathIdx, distance);
     assert(ret);
     (void)ret;
 }

//=========================================================================
Valentin Platzgummer's avatar
Valentin Platzgummer committed
// Scenario calculation.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
//=========================================================================
Valentin Platzgummer's avatar
Valentin Platzgummer committed
}Scenario::Scenario() :
    _tileWidth(5)
  ,  _tileHeight(5)
  , _minTileArea(0)
  , _needsUpdate(true)
{

}

void Scenario::setMeasurementArea(const BoostPolygon &area)
{
    _needsUpdate = true;
    _mArea = area;
}

void Scenario::setServiceArea(const BoostPolygon &area)
{
    _needsUpdate = true;
    _sArea = area;
}

void Scenario::setCorridor(const BoostPolygon &area)
{
    _needsUpdate = true;
    _corridor = area;
}

BoostPolygon &Scenario::measurementArea() {
    _needsUpdate = true;
    return _mArea;
}

BoostPolygon &Scenario::serviceArea() {
    _needsUpdate = true;
    return _sArea;
}

BoostPolygon &Scenario::corridor() {
    _needsUpdate = true;
    return _corridor;
}

const BoundingBox &Scenario::mAreaBoundingBox() const
{
    return _mAreaBoundingBox;
}

const BoostPolygon &Scenario::measurementArea() const
{
    return _mArea;
}

const BoostPolygon &Scenario::serviceArea() const
{
    return _sArea;
}

const BoostPolygon &Scenario::corridor() const
{
    return _corridor;
}

BoostPolygon &Scenario::measurementArea()
{
    return _mArea;
}

BoostPolygon &Scenario::serviceArea()
{
    return _sArea;
}

BoostPolygon &Scenario::corridor()
{
    return _corridor;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
const BoostPolygon &Scenario::joinedArea() const {
    return _jArea;
}

const vector<BoostPolygon> &Scenario::tiles() const{
    return _tiles;
}

const BoostLineString &Scenario::tileCenterPoints() const{
    return _tileCenterPoints;
}

const BoundingBox &Scenario::measurementAreaBBox() const{
    return _mAreaBoundingBox;
}

const BoostPoint &Scenario::homePositon() const{
    return _homePosition;
}

bool Scenario::update()
{
    if ( !_needsUpdate )
        return true;
    if (!_calculateBoundingBox())
        return false;
    if (!_calculateTiles())
        return false;
    if (!_calculateJoinedArea())
        return false;
    _needsUpdate = false;
    return true;
}

bool Scenario::_calculateBoundingBox()
{
    minimalBoundingBox(_mArea, _mAreaBoundingBox);
    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 _mArea and \a _mAreaBoundingBox have correct values. \see \ref Scenario::_areas2enu() and \ref
 * Scenario::_calculateBoundingBox().
 *
 * @param tileWidth The width (>0) of a tile.
 * @param tileHeight The heigth (>0) of a tile.
 * @param minTileArea The minimal area (>0) of a tile.
 *
 * @return Returns true if successful.
 */
bool Scenario::_calculateTiles()
{
    _tiles.clear();
    _tileCenterPoints.clear();

    if (_tileWidth <= 0 || _tileHeight <= 0 || _minTileArea < 0) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        errorString = "Parameters tileWidth, tileHeight, minTileArea must be positive.";
        return false;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed
    double bbox_width = _mAreaBoundingBox.width;
    double bbox_height = _mAreaBoundingBox.height;

    BoostPoint origin = _mAreaBoundingBox.corners.outer()[0];
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    //cout << "Origin: " << origin[0] << " " << origin[1] << endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    // Transform _mArea polygon to bounding box coordinate system.
    trans::rotate_transformer<boost::geometry::degree, double, 2, 2> rotate(_mAreaBoundingBox.angle*180/M_PI);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    trans::translate_transformer<double, 2, 2> translate(-origin.get<0>(), -origin.get<1>());
    BoostPolygon translated_polygon;
    BoostPolygon rotated_polygon;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    boost::geometry::transform(_mArea, translated_polygon, translate);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    boost::geometry::transform(translated_polygon, rotated_polygon, rotate);
    bg::correct(rotated_polygon);

    //cout << bg::wkt<BoostPolygon2D>(rotated_polygon) << endl;

    size_t i_max = ceil(bbox_width/tileWidth);
    size_t j_max = ceil(bbox_height/tileHeight);

    if (i_max < 1 || j_max < 1) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        errorString = "Tile width or Tile height to large.";
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        return false;
    }

Valentin Platzgummer's avatar
Valentin Platzgummer committed

    trans::rotate_transformer<boost::geometry::degree, double, 2, 2> rotate_back(-_mAreaBoundingBox.angle*180/M_PI);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    trans::translate_transformer<double, 2, 2> translate_back(origin.get<0>(), origin.get<1>());
    for (size_t i = 0; i < i_max; ++i){
        double x_min = tileWidth*i;
        double x_max = x_min + tileWidth;
        for (size_t j = 0; j < j_max; ++j){
            double y_min = tileHeight*j;
            double y_max = y_min + tileHeight;

            BoostPolygon tile_unclipped;
            tile_unclipped.outer().push_back(BoostPoint{x_min, y_min});
            tile_unclipped.outer().push_back(BoostPoint{x_min, y_max});
            tile_unclipped.outer().push_back(BoostPoint{x_max, y_max});
            tile_unclipped.outer().push_back(BoostPoint{x_max, y_min});
            tile_unclipped.outer().push_back(BoostPoint{x_min, y_min});


            std::deque<BoostPolygon> boost_tiles;
            if (!boost::geometry::intersection(tile_unclipped, rotated_polygon, boost_tiles))
                continue;

           for (BoostPolygon t : boost_tiles)
           {
                if (bg::area(t) > minTileArea){
                    // Transform boost_tile to world coordinate system.
                    BoostPolygon rotated_tile;
                    BoostPolygon translated_tile;
                    boost::geometry::transform(t, rotated_tile, rotate_back);
                    boost::geometry::transform(rotated_tile, translated_tile, translate_back);

                    // Store tile and calculate center point.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
                    _tiles.push_back(translated_tile);
                    BoostPoint tile_center;
                    polygonCenter(translated_tile, tile_center);
                    _tileCenterPoints.push_back(tile_center);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    if (_tiles.size() < 1){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        errorString = "No tiles calculated. Is the minTileArea parameter large enough?";
        return false;
    }

    return true;
}

Valentin Platzgummer's avatar
Valentin Platzgummer committed
bool Scenario::_calculateJoinedArea()
{
    _jArea.clear();
    // Measurement area and service area overlapping?
    bool overlapingSerMeas = bg::intersects(_mArea, _sArea) ? true : false;
    bool corridorValid = _corridor.outer().size() > 0 ? true : false;


    // Check if corridor is connecting measurement area and service area.
    bool corridor_is_connection = false;
    if (corridorValid) {
        // Corridor overlaping with measurement area?
        if ( bg::intersects(_corridior, _mArea) ) {
            // Corridor overlaping with service area?
            if ( bg::intersects(_corridior, _sArea) ) {
                corridor_is_connection = true;
            }
        }
    }

    // Are areas joinable?
    std::deque<BoostPolygon> sol;
    BoostPolygon partialArea = _mArea;
    if (overlapingSerMeas){
        if(corridor_is_connection){
            bg::union_(partialArea, _corridior, sol);
        }
    } else if (corridor_is_connection){
        bg::union_(partialArea, _corridior, sol);
    } else {
        errorString = "Areas are not overlapping";
        return false;
    }

    if (sol.size() > 0) {
        partialArea = sol[0];
        sol.clear();
    }

    // Join areas.
    bg::union_(partialArea, _sArea, sol);

    if (sol.size() > 0) {
        _jArea = sol[0];
    } else {
        return false;
    }

    return true;
}

double Scenario::minTileArea() const
{
    return _minTileArea;
}

void Scenario::setMinTileArea(double minTileArea)
{
    if ( minTileArea >= 0){
        _needsUpdate = true;
        _minTileArea = minTileArea;
    }
}

double Scenario::tileHeight() const
{
    return _tileHeight;
}

void Scenario::setTileHeight(double tileHeight)
{
    if ( tileHeight > 0) {
        _needsUpdate = true;
        _tileHeight = tileHeight;
    }
}

double Scenario::tileWidth() const
{
    return _tileWidth;
}

void Scenario::setTileWidth(double tileWidth)
{
    if ( tileWidth > 0 ){
        _needsUpdate = true;
        _tileWidth = tileWidth;
    }
}

//=========================================================================
// Tile calculation.
//=========================================================================

Valentin Platzgummer's avatar
Valentin Platzgummer committed
bool joinAreas(const std::vector<BoostPolygon> &areas, BoostPolygon &joinedArea)
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    if (areas.size() < 1)
        return false;
    std::deque<std::size_t> idxList;
    for(size_t i = 1; i < areas.size(); ++i)
        idxList.push_back(i);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    joinedArea = areas[0];
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    std::deque<BoostPolygon> sol;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    while (idxList.size() > 0){
        bool success = false;
        for (auto it = idxList.begin(); it != idxList.end(); ++it){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
            bg::union_(joinedArea, areas[*it], sol);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
            if (sol.size() > 0) {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
                joinedArea = sol[0];
Valentin Platzgummer's avatar
Valentin Platzgummer committed
                sol.clear;
                idxList.erase(it);
                success = true;
                break;
            }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        if ( !success )
            return false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    return true;
}

BoundingBox::BoundingBox() :
    width(0)
  , height(0)
  , angle(0)
{

}

void BoundingBox::clear()
{
    width = 0;
    height = 0;
    angle = 0;
    corners.clear();
}

Flightplan::Flightplan(ScenarioCPtr s, ProgressCPtr p)
    : _scenario(s)
    , _progress(p)
{

}

double Flightplan::lineDistance() const
{
    return _lineDistance;
}

void Flightplan::setLineDistance(double lineDistance)
{
    _lineDistance = lineDistance;
}

double Flightplan::minTransectLength() const
{
    return _minTransectLength;
}

void Flightplan::setMinTransectLength(double minTransectLength)
{
    _minTransectLength = minTransectLength;
}

Flightplan::ScenarioCPtr Flightplan::scenario() const
{
    return _scenario;
}

void Flightplan::setScenario(ScenarioCPtr &scenario)
{
    _scenario = scenario;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
Flightplan::ProgressCPtr Flightplan::progress() const
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    return _progress;
}

void Flightplan::setProgress(ProgressCPtr &progress)
{
    _progress = progress;
}

struct Flightplan::RoutingDataModel{
    Matrix<int64_t>                 distanceMatrix;
    long                            numVehicles;
    RoutingIndexManager::NodeIndex  depot;
};

bool Flightplan::update()
{
    _waypoints.clear();
    _arrivalPath.clear();
    _returnPath.clear();

Valentin Platzgummer's avatar
Valentin Platzgummer committed
    auto start = std::chrono::high_resolution_clock::now();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    if (!_generateTransects())
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        return false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start);
    cout << endl;
   cout << "Execution time _generateTransects(): " << delta.count() << " ms" << endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    //=======================================
    // Route Transects using Google or-tools.
    //=======================================
    // Offset joined area.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    const BoostPolygon &jArea = _scenario->joinedArea();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    BoostPolygon jAreaOffset;
    offsetPolygon(jArea, jAreaOffset, detail::offsetConstant);

    // Create vertex list;
    BoostLineString vertices;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    size_t n_t = _transects.size()*2;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    size_t n0  = n_t+1;
    vertices.reserve(n0);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    for (auto& lstring : _transects){
        for (auto& vertex : lstring){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
            vertices.push_back(vertex);
        }
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    vertices.push_back(_scenario->homePositon());
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    for (long i=0; i<long(jArea.outer().size())-1; ++i) {
        vertices.push_back(jArea.outer()[i]);
    }
    for (auto ring : jArea.inners()) {
        for (auto vertex : ring)
            vertices.push_back(vertex);
    }
    size_t n1 = vertices.size();
    // Generate routing model.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    RoutingDataModel dataModel;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    Matrix<double> connectionGraph(n1, n1);

Valentin Platzgummer's avatar
Valentin Platzgummer committed
    start = std::chrono::high_resolution_clock::now();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    _generateRoutingModel(vertices, jAreaOffset, n0, dataModel, connectionGraph);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    delta = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start);
    cout << "Execution time _generateRoutingModel(): " << delta.count() << " ms" << endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    // Create Routing Index Manager.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    RoutingIndexManager manager(dataModel.distanceMatrix.getN(), dataModel.numVehicles,
Valentin Platzgummer's avatar
Valentin Platzgummer committed
                                dataModel.depot);
    // Create Routing Model.
    RoutingModel routing(manager);

    // Create and register a transit callback.
    const int transit_callback_index = routing.RegisterTransitCallback(
        [&dataModel, &manager](int64 from_index, int64 to_index) -> int64 {
          // Convert from routing variable Index to distance matrix NodeIndex.
          auto from_node = manager.IndexToNode(from_index).value();
          auto to_node = manager.IndexToNode(to_index).value();
          return dataModel.distanceMatrix.get(from_node, to_node);
        });

    // Define cost of each arc.
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index);

Valentin Platzgummer's avatar
Valentin Platzgummer committed

    // Define Constraints.
    size_t n = _transects.size()*2;
    Solver *solver = routing.solver();
    for (size_t i=0; i<n; i=i+2){
//        auto idx0 = manager.NodeToIndex(RoutingIndexManager::NodeIndex(i));
//        auto idx1 = manager.NodeToIndex(RoutingIndexManager::NodeIndex(i+1));
//        auto cond0 = routing.NextVar(idx0)->IsEqual(idx1);
//        auto cond1 = routing.NextVar(idx1)->IsEqual(idx0);
//        auto c = solver->MakeNonEquality(cond0, cond1);
//        solver->AddConstraint(c);

        // alternative
        auto idx0 = manager.NodeToIndex(RoutingIndexManager::NodeIndex(i));
        auto idx1 = manager.NodeToIndex(RoutingIndexManager::NodeIndex(i+1));
        auto cond0 = routing.NextVar(idx0)->IsEqual(idx1);
        auto cond1 = routing.NextVar(idx1)->IsEqual(idx0);
        vector<IntVar*> conds{cond0, cond1};
        auto c = solver->MakeAllDifferent(conds);
        solver->MakeRejectFilter();
        solver->AddConstraint(c);
    }


    // Setting first solution heuristic.
    RoutingSearchParameters searchParameters = DefaultRoutingSearchParameters();
    searchParameters.set_first_solution_strategy(
        FirstSolutionStrategy::PATH_CHEAPEST_ARC);
    google::protobuf::Duration *tMax = new google::protobuf::Duration(); // seconds
    tMax->set_seconds(10);
    searchParameters.set_allocated_time_limit(tMax);

    // Solve the problem.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    start = std::chrono::high_resolution_clock::now();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    const Assignment* solution = routing.SolveWithParameters(searchParameters);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    delta = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start);
    cout << "Execution time routing.SolveWithParameters(): " << delta.count() << " ms" << endl;
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    if (!solution || solution->Size() <= 1){
        errorString = "Not able to solve the routing problem.";
        return false;
    }

    // Extract waypoints from solution.
    long index = routing.Start(0);
    std::vector<size_t> route;
    route.push_back(manager.IndexToNode(index).value());
    while (!routing.IsEnd(index)){
        index = solution->Value(routing.NextVar(index));
        route.push_back(manager.IndexToNode(index).value());
    }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    long sz = route.size();

    // Helper Lambda.
    auto fromVertices = [&vertices](const std::vector<size_t> &idxArray,
                              std::vector<BoostPoint> &path){
    for (size_t j=1; j<idxArray.size(); ++j)
        path.push_back(vertices[idxArray[j]]);
    };

    // Fill arrival path.
    _arrivalPath.push_back(vertices[route[0]]);
    size_t idx0 = route[0];
    size_t idx1 = route[1];
    std::vector<size_t> pathIdx;
    shortestPathFromGraph(connectionGraph, idx0, idx1, pathIdx);
    fromVertices(pathIdx, _arrivalPath);

    if (_arrivalPath.size() < 2)
        return false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed

    // Fill waypoints.
    _waypoints.push_back(vertices[route[1]]);
    for (long i=1; i<sz-2; ++i){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        size_t idx0 = route[i];
        size_t idx1 = route[i+1];
        pathIdx.clear();
        shortestPathFromGraph(connectionGraph, idx0, idx1, pathIdx);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        fromVertices(pathIdx, _waypoints);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    _waypoints.push_back(vertices[route[sz-2]]);
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    if (_waypoints.size() < 2)
        return false;
Valentin Platzgummer's avatar
Valentin Platzgummer committed

Valentin Platzgummer's avatar
Valentin Platzgummer committed

    // Fill return path.
    _returnPath.push_back(vertices[route[sz-2]]);
    idx0 = route[sz-2];
    idx1 = route[sz-1];
    pathIdx.clear();
    shortestPathFromGraph(connectionGraph, idx0, idx1, pathIdx);
    fromVertices(pathIdx, _returnPath);

    if (_returnPath.size() < 2)
        return false;


    return true;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
bool Flightplan::_generateTransects()
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    _transects.clear();
    if (_scenario->tiles().size() != _progress->size()){
        ostringstream strstream;
        strstream << "Number of tiles ("
Valentin Platzgummer's avatar
Valentin Platzgummer committed
                  << _scenario->tiles().size()
                  << ") is not equal to progress array length ("
Valentin Platzgummer's avatar
Valentin Platzgummer committed
                  << _progress->size()
                  << ")";
        errorString = strstream.str();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        return false;
    }

    // Calculate processed tiles (_progress[i] == 100) and subtract them from measurement area.
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    size_t num_tiles = _progress->size();
Valentin Platzgummer's avatar
Valentin Platzgummer committed
    vector<BoostPolygon> processedTiles;
    {
Valentin Platzgummer's avatar
Valentin Platzgummer committed
        const auto &tiles = _scenario->tiles();
        for (size_t i=0; i<num_tiles; ++i) {
            if (_progress[i] == 100){
Valentin Platzgummer's avatar
Valentin Platzgummer committed
                processedTiles.push_back(tiles[i]);
            }
        }