Boost::几何体::与C++的交集

qyuhtwio  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(125)

我是一个土木工程的博士生,最近开始用C++编程,基本上我对两个多边形的重叠或相交区域感兴趣,这两个多边形代表了两个土壤颗粒的投影。
我做了大量的搜索,我发现Boost几何是最适合我的解决方案。我也做了大量的搜索,我所面临的具体问题,但我无法解决我的问题。
问题是这样的,我使用的软件叫做PFC 3D(粒子流代码),我必须使用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);

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

kuarbcqp

kuarbcqp1#

identifier { }只有在identifier是类型名时才有效。
如果希望统一初始化,可以使用{ }作为构造函数参数列表的开头,并将每个参数环 Package 在一组额外的{ }中:

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);
}
wpx232ag

wpx232ag2#

多边形中的点应该按顺时针排序。例如

#include <iostream>
#include <algorithm>
#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::d2::point_xy<double> point;
typedef bgm::polygon<point> polygon;

void testOverlap_anticlockwise();
void testOverlap_clockwise();

int main() {
    testOverlap_anticlockwise();
    testOverlap_clockwise();
}


void testOverlap_anticlockwise()
{
    polygon poly1;
    poly1.outer().push_back(point(15, 0.176766));
    poly1.outer().push_back(point(0.176767, 15));
    poly1.outer().push_back(point(-5, 16.6844));
    poly1.outer().push_back(point(-5, -5));
    poly1.outer().push_back(point(16.6844, -5));
    poly1.outer().push_back(point(15, 0.176766));

    polygon poly2;
    poly2.outer().push_back(point(15, 15));
    poly2.outer().push_back(point(0, 15));
    poly2.outer().push_back(point(0, 0));
    poly2.outer().push_back(point(15, 0));
    poly2.outer().push_back(point(15, 15));

    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 (std::deque<polygon>::const_iterator it = output.begin(); it != output.end(); ++it) {
        area += bg::area(*it);
    }
    std::cout << area << std::endl;
}

void testOverlap_clockwise()
{
    polygon poly1;
    poly1.outer().push_back(point(15, 0.176766));
    poly1.outer().push_back(point(0.176767, 15));
    poly1.outer().push_back(point(-5, 16.6844));
    poly1.outer().push_back(point(-5, -5));
    poly1.outer().push_back(point(16.6844, -5));
    poly1.outer().push_back(point(15, 0.176766));
    std::reverse(poly1.outer().begin(), poly1.outer().end());

    polygon poly2;
    poly2.outer().push_back(point(15, 15));
    poly2.outer().push_back(point(0, 15));
    poly2.outer().push_back(point(0, 0));
    poly2.outer().push_back(point(15, 0));
    poly2.outer().push_back(point(15, 15));
    std::reverse(poly2.outer().begin(), poly2.outer().end());

    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 (std::deque<polygon>::const_iterator it = output.begin(); it != output.end(); ++it) {
        area += bg::area(*it);
    }
    std::cout << area << std::endl;
}

结果为多边形((15 0.176766,0.176767 15,-5 16. 6844,-5 -5,16.6844 -5,15 0.176766))多边形((15 15,0 15,0 0,15 0,15 15))0
多边形((15 0.17676,16.6844 -5,-5 -5,-5 16. 6844,0.176767 15,15 0.176766))多边形((15 15,15 0,0 0,0 15,15 15))115.136

相关问题