Search code examples
c++boostboost-geometry

Path between two points inside a Boost Geometry polygon


I'm trying to find the "path" to connect two points in a c++ boost polygon.

I have a polygon like that

namespace bgm = bg::model;
using Point = bgm::d2::point_xy<double>;
bgm::polygon<Point> poly;

bg::read_wkt("POLYGON((" + points_poly + "))", poly);

And i have randoms points in the polygon.

And i would like to know if boost c++ have tools to help in a case like that. enter image description here


Solution

  • It's pretty unclear what the actual question is, so here's what asnwers directly to your question:

    Poly poly{{
        {-4, 14}, {17.75, 13.99}, {17.75, 7.95},  {10, 8},         {10, 2},
        {16, 2},  {16, -8},       {22, -8},       {13.97, -13.17}, {6, -8},
        {12, -8}, {12, -2},       {-0.99, -2.06}, {-0.9, -7.95},   {-18, -8},
        {-18, 2}, {-10, 2},       {-10, 6},       {-6, 6},         {-6, 2},
        {-2, 8},  {-7.05, 8},     {-7.15, 14},    {-4, 14},
    }};
    
    Point A{-7.08, 10.07};
    Point B{10, 4};
    
    LineString path{A, {4, 10}, {4, 4}, B};
    

    The WKT for those is:

    poly: POLYGON((-4 14,17.75 13.99,17.75 7.95,10 8,10 2,16 2,16 -8,22 -8,13.97
    -13.17,6 -8,12 -8,12 -2,-0.99 -2.06,-0.9 -7.95,-18 -8,-18 2,-10 2,-10 6,-6 6,
    -6 2,-2 8,-7.05 8,-7.15 14,-4 14))
    A: POINT(-7.08 10.07)
    B: POINT(10 4)
    path: LINESTRING(-7.08 10.07,4 10,4 4,10 4)
    

    Turning it into a SVG image:

    Live On Coliru

    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    #include <boost/geometry/geometries/linestring.hpp>
    #include <iostream>
    #include <fstream>
    
    namespace bg  = boost::geometry;
    namespace bgm = bg::model;
    
    using Point      = bgm::d2::point_xy<double>;
    using Poly       = bgm::polygon<Point>;
    using LineString = bgm::linestring<Point>;
    
    int main() {
        Poly poly{{
            {-4, 14}, {17.75, 13.99}, {17.75, 7.95},  {10, 8},         {10, 2},
            {16, 2},  {16, -8},       {22, -8},       {13.97, -13.17}, {6, -8},
            {12, -8}, {12, -2},       {-0.99, -2.06}, {-0.9, -7.95},   {-18, -8},
            {-18, 2}, {-10, 2},       {-10, 6},       {-6, 6},         {-6, 2},
            {-2, 8},  {-7.05, 8},     {-7.15, 14},    {-4, 14},
        }};
    
        Point A{-7.08, 10.07};
        Point B{10, 4};
    
        LineString path{A, {4, 10}, {4, 4}, B};
    
        std::cout << "poly: " << bg::wkt(poly) << "\n";
        std::cout << "A: " << bg::wkt(A) << "\n";
        std::cout << "B: " << bg::wkt(B) << "\n";
        std::cout << "path: " << bg::wkt(path) << "\n";
    
        {
            std::ofstream svg("output.svg");
            boost::geometry::svg_mapper<Point> mapper(svg, 400, 400);
            mapper.add(poly);
            mapper.add(path);
            mapper.add(A);
            mapper.add(B);
    
            mapper.map(poly, "fill-opacity:0.3;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
            mapper.map(path, "fill-opacity:0.05;fill:rgb(255,0,0);stroke:rgb(255,0,0);stroke-width:2");
            mapper.map(A, "fill:red;stroke-width:20");
            mapper.map(B, "fill:red;stroke-width:20");
    
            mapper.text(A, "A", "");
            mapper.text(B, "B", "");
        }
    }
    

    Printing the WKT (see above) as well as writing output.svg:

    enter image description here