boost::geometry::read_wkt备选方案

boost::geometry::read_wkt alternative?

本文关键字:方案 wkt geometry read boost      更新时间:2023-10-16

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

这是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;

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

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

我对几何概念了解不多。然而,这看起来应该很接近,或者至少给你一些做事的灵感。

Update向WKT添加了序列化,这样我们就可以确认polyon是相同的。

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

如果像评论者所说的那样,你认为手动从矢量创建WKT更容易,这里有一种方法,在大约2行代码中完成,而不使用Boost Geometry:中的任何东西

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

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

相关文章: