2017-09-07 64 views
2

我在土木工程博士學位的學生,我最近開始在C++做一些編碼,基本上我感興趣的是得到兩個多邊形的重疊或交叉區域,其代表兩個土壤顆粒的投影。的boost ::幾何::交叉口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座標。 希望你能在這方面提供幫助。謝謝!

+0

什麼版本提升您使用的是? – mascoj

+0

感謝您的回覆。我正在使用boost_1_65_0 –

回答

2

嘛。 identifier { }只有在identifier是typename時纔有效。

如果你想統一初始化,您可以使用{ }開始構造函數的參數列表,並在一組額外的{ }包裝每個參數環:

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); 
} 
+0

Sehe ...感謝您提供這些優秀的信息。 在界定多邊形的點,例如:BG ::讀(POLY1, 「POLYGON((0 0,0 1,1 1,1 0,0 0,0.05 0))」).. ....在我看來,多邊形頂點的輸入是字符串,這意味着我們無法更新它們。我的代碼需要每個週期更新每個多邊形頂點。我的意思是我需要將多邊形初始化爲可以修改的點。 .....我嘗試使用第一種方法,多邊形poly1 {{{0.0,0.0},{0.0,1.0},{1.0,1.0},{1.0,0.0},{0.05,0.0}}},但是它也沒有工作 –

+0

當然[第一種方法工程](https://wandbox.org/permlink/qLHtMwmzN0SaUfwT)。你在使用C++ 11編譯器嗎?順便說一下,「更新點」的需要恰好是***爲什麼我建議從某處閱讀它們。我的意思是,在你的代碼中,你必須重新編譯「更新一個點」。 – sehe

+0

哦,嘿,Coliru停止編譯速度太慢:[同](http://coliru.stacked-crooked.com/a/1aec905ca7afef16) – sehe