2013-10-22 26 views
3

我想要使用Boost :: Polygon遞增地構建一組多邊形。下面代碼中的實時數據集取自我在使用Boost :: Polygon for的真實系統中找到的病理輸入。Boost多邊形丟棄輸入多邊形

這裏是最小生殖代碼(你需要提升開發頭在include路徑):

#include <iostream> 
#include <string> 
#include <vector> 

#include "boost/polygon/polygon.hpp" 

using namespace std; 
using namespace boost::polygon; 
using namespace boost::polygon::operators; 

/* Utility function to read a polygon from a string. This is ugly, but works for the purpose of this repro case */ 
polygon_data<float> readPoly(string str) 
{ 
    vector<point_data<float> > result; 

    size_t index = 0; 
    while(index != string::npos) 
    { 
     size_t xend = str.find(" ", index); 
     size_t yend = str.find(" ", xend + 1); 

     float x = atof(str.substr(index, xend - index).c_str()); 
     float y = atof(str.substr(xend + 1, yend - xend + 1).c_str()); 
     result.push_back(point_data<float>(x, y)); 

     if(yend == string::npos) 
      break; 

     index = yend + 1; 
    } 

    return polygon_data<float>(result.begin(), result.end()); 
} 

/* Utility function to dump a polygon set to cout, useful for visualizing with the python script */ 
void dumpPoly(vector<polygon_data<float> > polyset) 
{ 
    for(vector<polygon_data<float> >::iterator it = polyset.begin(); it != polyset.end(); it++) 
    { 
     polygon_data<float> poly = *it; 
     for(polygon_data<float>::iterator_type jt = poly.begin(); jt != poly.end(); jt++) 
     { 
      cout << (*jt).x() << " " << (*jt).y() << " "; 
     } 
     cout << endl; 
    } 
} 


int main() 
{ 
    std::vector<polygon_data<float> > data; 

    // Construct the polygon set 
    data += readPoly("-1309.77, 1323.99, -1324, 1309.76, -1324, -1309.76, -1309.77, -1323.99, -1240.23, -1323.99, -1226, -1309.76, -1226, 1309.76, -1240.23, 1323.99"); 
    data += readPoly("-1323.99, -1309.77, -1309.76, -1324, 1309.76, -1324, 1323.99, -1309.77, 1323.99, -1240.23, 1309.76, -1226, -1309.76, -1226, -1323.99, -1240.23"); 
    data += readPoly("1323.99, 1309.77, 1309.76, 1324, -1309.76, 1324, -1323.99, 1309.77, -1323.99, 1240.23, -1309.76, 1226, 1309.76, 1226, 1323.99, 1240.23"); 
    data += readPoly("-544.771, 686.49, -559, 672.261, -559, -544.761, -544.771, -558.99, -475.229, -558.99, -461, -544.761, -461, 672.261, -475.229, 686.49"); 
    data += readPoly("-558.99, -544.771, -544.761, -559, 672.261, -559, 686.49, -544.771, 686.49, -475.229, 672.261, -461, -544.761, -461, -558.99, -475.229"); 
    data += readPoly("686.49, 672.271, 672.261, 686.5, -544.761, 686.5, -558.99, 672.271, -558.99, 602.729, -544.761, 588.5, 672.261, 588.5, 686.49, 602.729"); 
    data += readPoly("-69.8842, -119.057, -49.7607, -119.057, 219.057, 149.76, 219.057, 169.884, 169.884, 219.057, 149.76, 219.057, -119.057, -49.7607, -119.057, -69.8842"); 
    data += readPoly("672.271, -558.99, 686.5, -544.761, 686.5, 672.261, 672.271, 686.49, 602.729, 686.49, 588.5, 672.261, 588.5, -544.761, 602.729, -558.99"); 
    data += readPoly("1309.77, -1323.99, 1324, -1309.76, 1324, 1309.76, 1309.77, 1323.99, 1240.23, 1323.99, 1226, 1309.76, 1226, -1309.76, 1240.23, -1323.99"); 

    /* 

    This alternative dataset shows that boost.polygon can handle nested holes, to some extent 

    data += readPoly("100 100 100 1900 1900 1900 1900 100"); 
    data -= readPoly("200 200 200 1800 1800 1800 1800 200"); 
    data += readPoly("300 300 300 1700 1700 1700 1700 300"); 
    data -= readPoly("400 400 400 1600 1600 1600 1600 400"); 
    data += readPoly("500 500 500 1500 1500 1500 1500 500"); 
    data -= readPoly("600 600 600 1400 1400 1400 1400 600"); 

    */ 

    dumpPoly(data); 

    return 0; 
} 

這裏有一個極小的一點可視化腳本中,我使用Python中matplotlib寫道。只需將上述程序的輸出傳遞給此腳本,您將看到結果。它工作正常,我只是包括它來幫你幫我:)

#!/usr/bin/env python 
import matplotlib.pyplot as plt 
import sys 

lines = sys.stdin.read().split("\n") 

for line in lines: 
    data = line.split() 

    if len(data) == 0: 
     continue 

    x = [] 
    y = [] 
    i = 0 
    while i < len(data): 
     x.append(float(data[i])) 
     y.append(float(data[i+1])) 
     i += 2 

    plt.fill(x, y) 

plt.show() 

這是我想到: Expected output from the above program

這裏就是我得到: Bad output from the above program

我的問題是:爲什麼Boost :: Polygon是否放棄了我的一些輸入,以及如何防止它這樣做?

回答

4

http://www.boost.org/doc/libs/1_54_0/libs/polygon/doc/index.htm

座標數據類型是由庫提供的所有數據類型和 算法的模板參數,並且有望成爲一體。 浮點座標數據類型不受庫中實現的 算法的支持,因爲實現浮點穩健性的事實意味着一組不同的算法,並且通常關於浮點的平臺特定假設表示。

+1

就是這樣,傻了我。謝謝一堆! – bkconrad

+1

現在一切都合情合理! (這應該是在這個--painful--手冊的第一頁中的警告)。 – alfC