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

问题描述 投票:2回答:1

我是土木工程博士生,最近我开始用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坐标。希望你能在这方面提供帮助。谢谢!

c++ boost boost-geometry
1个回答
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.