boost :: geometry :: read_wkt替代?

时间:2014-11-07 15:18:30

标签: c++ boost boost-geometry

我正在尝试检查点是否在多边形内部。为此,我想使用boost库。我的问题是如何修改boost中的示例,以便不是通过read_wkt读取点,而是从std向量中读取它们。

这是来自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;
}

这是我的vecPoints:

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

请注意:我的问题是与多边形算法中的点相关。

3 个答案:

答案 0 :(得分:2)

我不太了解几何概念。但是,这看起来应该很接近,或者至少会给你一些如何做事的灵感。

更新将序列化添加到 WKT,以便我们确认polyon是相同的。

<强> 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";
}

打印

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))

答案 1 :(得分:2)

如果像评论者说的那样,你认为手动创建WKT更容易,这是一种方法,在〜2行代码中,不使用Boost几何中的任何东西:

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

完整演示 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);
}

打印:

  

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 ) )

答案 2 :(得分:2)

如果要在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);