Boost :: geometry ::与C ++的交集

时间:2017-09-07 20:31:36

标签: c++ boost boost-geometry

我是土木工程博士生,最近我开始用C ++编写一些代码,基本上我对获得两个多边形的重叠或交叉区域感兴趣,这两个多边形代表两个土壤粒子的投影。

我做了很多搜索,我发现增强几何是我的最佳解决方案。我也对我面临的具体问题进行了大量搜索,但我无法解决我的问题。

问题在于,我使用的软件称为PFC3D(粒子流代码)。我必须使用microsoft visual studio 2010与该软件交互并编译DLL文件以在PFC中运行它。

我的代码在没有重叠区域的情况下工作得很好。这是代码:

// Includes for overlapping
#include <boost/geometry.hpp>
#include <boost/geometry/core/point_type.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/register/point.hpp>enter code here
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;
polygon poly1, poly2;
poly1 {{0.0,  0.0}, {0.0, 1.0}, {1.0, 1.0}, {1.0,  0.0}, {0.05,  0.0}};
poly2 {{0.5, -0.5}, {0.5, 0.5}, {1.5, 0.5}, {1.5, -0.5},  {0.5, -0.5}};
std::deque<polygon> output;
boost::geometry::intersection(poly1, poly2, output);
double area = boost::geometry::area(output);

我得到的错误是分配poly1和poly2坐标。 希望你能在这方面提供帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

好。 identifier { }仅在identifier是类型名称时才有效。

如果您想要统一初始化,可以使用{ }开始构造函数参数列表,并将每个参数环包装在一组额外的{ }中:

polygon poly1 { { { 0.0, 0.0 }, { 0.0, 1.0 }, { 1.0, 1.0 }, { 1.0, 0.0 }, { 0.05, 0.0 } }  };
polygon poly2 { { { 0.5, -0.5 }, { 0.5, 0.5 }, { 1.5, 0.5 }, { 1.5, -0.5 }, { 0.5, -0.5 } }  };

接下来,area并不期望多边形,所以写一个循环:

double area = 0;
for (auto& p : output)
    area += boost::geometry::area(p);

我可以建议查看dsv解析输入:

polygon poly1, poly2;
bg::read<bg::format_wkt>(poly1, "POLYGON((0 0,0 1,1 1,1 0,0.05 0,0 0))");
bg::read<bg::format_wkt>(poly2, "POLYGON((0.5 -0.5,0.5 0.5,1.5 0.5,1.5 -0.5,0.5 -0.5))");

现场演示: Live On Coliru

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/io/io.hpp>
#include <boost/geometry/algorithms/area.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>

namespace bg = boost::geometry;
namespace bgm = bg::model;
typedef bgm::polygon<bgm::d2::point_xy<double> > polygon;

int main() {
    polygon poly1, poly2;
    bg::read<bg::format_wkt>(poly1, "POLYGON((0 0,0 1,1 1,1 0,0.05 0,0 0))");
    bg::read<bg::format_wkt>(poly2, "POLYGON((0.5 -0.5,0.5 0.5,1.5 0.5,1.5 -0.5,0.5 -0.5))");

    std::cout << bg::wkt(poly1) << "\n";
    std::cout << bg::wkt(poly2) << "\n";
    std::deque<polygon> output;
    bg::intersection(poly1, poly2, output);


    double area = 0;
    for (auto& p : output)
        area += bg::area(p);
}