2
votes

I am trying to check if a point is inside a polygon or not. For this purpose I want to use boost library. My question is how I can modify the example from boost so that instead of reading points by read_wkt, read them from std vector.

here is the sample code from boost:

#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>


int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;

    polygon_type poly;
    boost::geometry::read_wkt(
        "POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3)"
            "(4.0 2.0, 4.2 1.4, 4.8 1.9, 4.4 2.2, 4.0 2.0))", poly);

    point_type p(4, 1);

    std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;

    return 0;
}

and here is my vecPoints:

struct PNTS{
int x;
int y;
}
std::vector<PNTS> vecPoints;

please NOTE: My question is NOT related to the point in polygon algorithm.

3
Why not iterate over your vector and build the WKT string? Would that work?Anas Alkhatib
Not working, I guess...H'H
@AnasAlkhatib added this approach as my second answersehe

3 Answers

3
votes

if you want to add your vector member inside a polygon in boost:

using boost::geometry::append;
using boost::geometry::make;
using boost::geometry::correct;
std::vector<PNTS>::iterator iter;
for(iter = vecPoints.begin(); iter != vecPoints.end(); iter++)
{
  PNTS ver = *iter;
  append( poly, make<boost2dPoint>(ver.x, ver.y) );
}
// you have to close polygon by inserting first element as the last again
PNTS last = vecPoints[vecPoints.size()-1];
append( poly, make<boost2dPoint>(last.x, last.y) );
// you can also correct the polygon orientation
correct(poly);
2
votes

I don't know much about Geometry concepts. However, this looks like it should be close, or at least give you some inspiration on how to do things.

Update added serialization to WKT so we can confirm that the polyon is identical.

Live On Coliru

#include <iostream>
#include <list>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/io.hpp>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double> point_type;
    typedef boost::geometry::model::polygon<point_type> polygon_type;

    polygon_type poly;
    poly.outer().assign({
        point_type {     2,   1.3   },
        point_type {   2.4,   1.7   },
        point_type {   2.8,   1.8   },
        point_type {   3.4,   1.2   },
        point_type {   3.7,   1.6   },
        point_type {   3.4,     2   },
        point_type {   4.1,     3   },
        point_type {   5.3,   2.6   },
        point_type {   5.4,   1.2   },
        point_type {   4.9,   0.8   },
        point_type {   2.9,   0.7   },
        point_type {     2,   1.3   },
    });

    poly.inners().emplace_back();
    poly.inners().back().assign({
        {   4.0,   2.0   },
        {   4.2,   1.4   },
        {   4.8,   1.9   },
        {   4.4,   2.2   },
        {   4.0,   2.0   },
    });

    point_type p(4, 1);

    std::cout << "within: " << (boost::geometry::within(p, poly) ? "yes" : "no") << std::endl;

    std::cout << boost::geometry::wkt(poly) << "\n";
}

Prints

within: yes
POLYGON((2 1.3,2.4 1.7,2.8 1.8,3.4 1.2,3.7 1.6,3.4 2,4.1 3,5.3 2.6,5.4 1.2,4.9 0.8,2.9 0.7,2 1.3),(4 2,4.2 1.4,4.8 1.9,4.4 2.2,4 2))
2
votes

In case, like the commenter said, you think it's easier to create the WKT from the vectors manually, here's one way to do it, in ~2 lines of code, without using anything from Boost Geometry:

using namespace boost::spirit::karma;
std::cout << format_delimited("POLYGON(" << *('(' << auto_%',' << ')') << ")\n", ' ', rings);

Full demo Live On Coliru

#include <iostream>
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/spirit/include/karma.hpp>

int main()
{
    using Ring = std::vector<boost::tuple<double,double>>;

    std::vector<Ring> rings = {
        {   {   2, 1.3 }, { 2.4, 1.7 }, { 2.8, 1.8 }, { 3.4, 1.2 },
            { 3.7, 1.6 }, { 3.4,   2 }, { 4.1,   3 }, { 5.3, 2.6 },
            { 5.4, 1.2 }, { 4.9, 0.8 }, { 2.9, 0.7 }, {   2, 1.3 },
        },
        {   { 4.0, 2.0 }, { 4.2, 1.4 }, { 4.8, 1.9 }, { 4.4, 2.2 }, { 4.0, 2.0   },
        }
    };

    using namespace boost::spirit::karma;
    std::cout << format_delimited("POLYGON(" << *('(' << auto_%',' << ')') << ")\n", ' ', rings);
}

Prints:

POLYGON( ( 2.0 1.3 , 2.4 1.7 , 2.8 1.8 , 3.4 1.2 , 3.7 1.6 , 3.4 2.0 , 4.1 3.0 , 5.3 2.6 , 5.4 1.2 , 4.9 0.8 , 2.9 0.7 , 2.0 1.3 ) ( 4.0 2.0 , 4.2 1.4 , 4.8 1.9 , 4.4 2.2 , 4.0 2.0 ) )