Boost::geometry::intersection 使用 C++
Boost::geometry::intersection with C++
我是土木工程专业的博士生,最近开始用 C++ 编写一些代码,基本上我感兴趣的是获取两个多边形的重叠或相交区域,它们代表两个土壤颗粒的投影。
我进行了大量搜索,发现 boost geometry 是最适合我的解决方案。我也对我面临的具体问题进行了大量搜索,但无法解决我的问题。
问题来了,我用的软件叫PFC3D(粒子流代码)。我必须使用 Microsoft visual studio 2010 才能与此软件交互,并在 PFC 中将 DLL 文件编译为 运行。
我的代码在没有重叠区域的情况下工作得很好。这是代码:
// 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 坐标。
希望你能在这方面提供帮助。谢谢!
嗯。 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);
}
我是土木工程专业的博士生,最近开始用 C++ 编写一些代码,基本上我感兴趣的是获取两个多边形的重叠或相交区域,它们代表两个土壤颗粒的投影。
我进行了大量搜索,发现 boost geometry 是最适合我的解决方案。我也对我面临的具体问题进行了大量搜索,但无法解决我的问题。
问题来了,我用的软件叫PFC3D(粒子流代码)。我必须使用 Microsoft visual studio 2010 才能与此软件交互,并在 PFC 中将 DLL 文件编译为 运行。
我的代码在没有重叠区域的情况下工作得很好。这是代码:
// 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 坐标。 希望你能在这方面提供帮助。谢谢!
嗯。 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);
}